@@ -41,7 +41,7 @@ export class Font extends FontBase {
41
41
42
42
getAndroidTypeface ( ) : android . graphics . Typeface {
43
43
if ( ! this . _typeface ) {
44
- this . _typeface = createTypeface ( this ) ;
44
+ this . _typeface = SDK_VERSION >= 28 ? createTypeface ( this ) : createTypefaceLegacy ( this ) ;
45
45
}
46
46
47
47
return this . _typeface ;
@@ -120,25 +120,58 @@ function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Type
120
120
121
121
function createTypeface ( font : Font ) : android . graphics . Typeface {
122
122
const fontFamilies = parseFontFamily ( font . fontFamily ) ;
123
- const fontWeight = font . fontWeight ;
124
- const isNumericFontWeightSupported = SDK_VERSION >= 28 ;
123
+ const fontWeightNum = getNumericFontWeight ( font . fontWeight ) ;
125
124
126
125
let result : android . graphics . Typeface ;
127
- let fontStyle : number = 0 ; // This will be empty if numeric font weight is supported
128
- // https://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
129
- let fontSuffix : string ;
130
126
131
- if ( isNumericFontWeightSupported ) {
132
- fontSuffix = '' ;
133
- } else {
134
- if ( font . isBold ) {
135
- fontStyle |= android . graphics . Typeface . BOLD ;
127
+ for ( const fontFamily of fontFamilies ) {
128
+ switch ( fontFamily . toLowerCase ( ) ) {
129
+ case genericFontFamilies . serif :
130
+ result = android . graphics . Typeface . create ( android . graphics . Typeface . SERIF , fontWeightNum , font . isItalic ) ;
131
+ break ;
132
+ case genericFontFamilies . sansSerif :
133
+ case genericFontFamilies . system :
134
+ result = android . graphics . Typeface . create ( android . graphics . Typeface . SANS_SERIF , fontWeightNum , font . isItalic ) ;
135
+ break ;
136
+ case genericFontFamilies . monospace :
137
+ result = android . graphics . Typeface . create ( android . graphics . Typeface . MONOSPACE , fontWeightNum , font . isItalic ) ;
138
+ break ;
139
+ default : {
140
+ result = loadFontFromFile ( fontFamily , font ) ;
141
+ if ( result ) {
142
+ result = android . graphics . Typeface . create ( result , fontWeightNum , font . isItalic ) ;
143
+ }
144
+ break ;
145
+ }
136
146
}
137
- if ( font . isItalic ) {
138
- fontStyle |= android . graphics . Typeface . ITALIC ;
147
+
148
+ // Found the font!
149
+ if ( result ) {
150
+ break ;
139
151
}
152
+ }
153
+
154
+ if ( ! result ) {
155
+ result = android . graphics . Typeface . create ( android . graphics . Typeface . SANS_SERIF , fontWeightNum , font . isItalic ) ;
156
+ }
157
+
158
+ return result ;
159
+ }
140
160
141
- fontSuffix = getFontWeightSuffix ( fontWeight ) ;
161
+ function createTypefaceLegacy ( font : Font ) : android . graphics . Typeface {
162
+ const fontFamilies = parseFontFamily ( font . fontFamily ) ;
163
+ const fontWeight = font . fontWeight ;
164
+ // https://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
165
+ const fontSuffix = getFontWeightSuffix ( fontWeight ) ;
166
+
167
+ let result : android . graphics . Typeface ;
168
+ let fontStyle : number = 0 ;
169
+
170
+ if ( font . isBold ) {
171
+ fontStyle |= android . graphics . Typeface . BOLD ;
172
+ }
173
+ if ( font . isItalic ) {
174
+ fontStyle |= android . graphics . Typeface . ITALIC ;
142
175
}
143
176
144
177
for ( const fontFamily of fontFamilies ) {
@@ -172,11 +205,6 @@ function createTypeface(font: Font): android.graphics.Typeface {
172
205
result = android . graphics . Typeface . create ( 'sans-serif' + fontSuffix , fontStyle ) ;
173
206
}
174
207
175
- // Newer android versions can accept a numeric font weight
176
- if ( isNumericFontWeightSupported ) {
177
- result = android . graphics . Typeface . create ( result , getNumericFontWeight ( fontWeight ) , font . isItalic ) ;
178
- }
179
-
180
208
return result ;
181
209
}
182
210
0 commit comments