1
- import { Font as FontBase , parseFontFamily , genericFontFamilies , FontWeight , FontVariationSettings } from './font-common' ;
1
+ import { Font as FontBase , parseFontFamily , genericFontFamilies , FontWeight , FontVariationSettings , FONTS_BASE_PATH } from './font-common' ;
2
2
import { FontStyleType , FontWeightType , FontVariationSettingsType } from './font-interfaces' ;
3
3
import { Trace } from '../../trace' ;
4
4
import { SDK_VERSION } from '../../utils/constants' ;
@@ -7,7 +7,6 @@ import { ad } from '../../utils';
7
7
8
8
export * from './font-common' ;
9
9
10
- const FONTS_BASE_PATH = '/fonts/' ;
11
10
const typefaceCache = new Map < string , android . graphics . Typeface > ( ) ;
12
11
let appAssets : android . content . res . AssetManager ;
13
12
@@ -66,24 +65,28 @@ function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Type
66
65
return null ;
67
66
}
68
67
69
- let result = typefaceCache . get ( cacheKey ) ;
70
- // Check for undefined explicitly as null mean we tried to load the font, but failed.
71
- if ( result === undefined ) {
72
- result = null ;
68
+ let result : android . graphics . Typeface ;
69
+
70
+ if ( typefaceCache . has ( cacheKey ) ) {
71
+ result = typefaceCache . get ( cacheKey ) ;
72
+ } else {
73
+ const basePath = fs . path . join ( fs . knownFolders . currentApp ( ) . path , FONTS_BASE_PATH , fontFamily ) ;
73
74
74
75
let fontAssetPath : string ;
75
- const basePath = fs . path . join ( fs . knownFolders . currentApp ( ) . path , 'fonts' , fontFamily ) ;
76
+
76
77
if ( fs . File . exists ( basePath + '.ttf' ) ) {
77
- fontAssetPath = FONTS_BASE_PATH + fontFamily + '.ttf' ;
78
+ fontAssetPath = basePath + '.ttf' ;
78
79
} else if ( fs . File . exists ( basePath + '.otf' ) ) {
79
- fontAssetPath = FONTS_BASE_PATH + fontFamily + '.otf' ;
80
+ fontAssetPath = basePath + '.otf' ;
80
81
} else if ( Trace . isEnabled ( ) ) {
82
+ fontAssetPath = null ;
81
83
Trace . write ( 'Could not find font file for ' + fontFamily , Trace . categories . Error , Trace . messageType . error ) ;
82
84
}
83
85
86
+ result = null ; // Default
87
+
84
88
if ( fontAssetPath ) {
85
89
try {
86
- fontAssetPath = fs . path . join ( fs . knownFolders . currentApp ( ) . path , fontAssetPath ) ;
87
90
if ( SDK_VERSION >= 26 ) {
88
91
const builder = new android . graphics . Typeface . Builder ( fontAssetPath ) ;
89
92
if ( builder ) {
@@ -104,35 +107,48 @@ function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Type
104
107
}
105
108
}
106
109
}
110
+
111
+ // The value might be null if there has already been an attempt to load the font but failed
107
112
typefaceCache . set ( cacheKey , result ) ;
108
113
}
109
114
110
115
return result ;
111
116
}
112
117
113
118
function createTypeface ( font : Font ) : android . graphics . Typeface {
114
- let fontStyle = 0 ;
115
- if ( font . isBold ) {
116
- fontStyle |= android . graphics . Typeface . BOLD ;
117
- }
118
- if ( font . isItalic ) {
119
- fontStyle |= android . graphics . Typeface . ITALIC ;
119
+ const fontFamilies = parseFontFamily ( font . fontFamily ) ;
120
+ const fontWeight = font . fontWeight ;
121
+ const supportsFontWeight = SDK_VERSION > 27 ;
122
+
123
+ let result : android . graphics . Typeface ;
124
+ let fontStyle : number = 0 ;
125
+ // https://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
126
+ let fontSuffix : string ;
127
+
128
+ if ( supportsFontWeight ) {
129
+ fontSuffix = '' ;
130
+ } else {
131
+ if ( font . isBold ) {
132
+ fontStyle |= android . graphics . Typeface . BOLD ;
133
+ }
134
+ if ( font . isItalic ) {
135
+ fontStyle |= android . graphics . Typeface . ITALIC ;
136
+ }
137
+
138
+ fontSuffix = getFontWeightSuffix ( fontWeight ) ;
120
139
}
121
140
122
- //http://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
123
- const fontFamilies = parseFontFamily ( font . fontFamily ) ;
124
- let result : android . graphics . Typeface = null ;
125
141
for ( const fontFamily of fontFamilies ) {
126
142
switch ( fontFamily . toLowerCase ( ) ) {
127
143
case genericFontFamilies . serif :
128
- result = android . graphics . Typeface . create ( 'serif' + getFontWeightSuffix ( font . fontWeight ) , fontStyle ) ;
144
+ result = android . graphics . Typeface . create ( 'serif' + fontSuffix , fontStyle ) ;
129
145
break ;
130
146
case genericFontFamilies . sansSerif :
131
147
case genericFontFamilies . system :
132
- result = android . graphics . Typeface . create ( 'sans-serif' + getFontWeightSuffix ( font . fontWeight ) , fontStyle ) ;
148
+ result = android . graphics . Typeface . create ( 'sans-serif' + fontSuffix , fontStyle ) ;
133
149
break ;
134
150
case genericFontFamilies . monospace :
135
- result = android . graphics . Typeface . create ( 'monospace' + getFontWeightSuffix ( font . fontWeight ) , fontStyle ) ;
151
+ result = android . graphics . Typeface . create ( 'monospace' + fontSuffix , fontStyle ) ;
136
152
break ;
137
153
default : {
138
154
result = loadFontFromFile ( fontFamily , font ) ;
@@ -143,14 +159,19 @@ function createTypeface(font: Font): android.graphics.Typeface {
143
159
}
144
160
}
145
161
162
+ // Found the font!
146
163
if ( result ) {
147
- // Found the font!
148
164
break ;
149
165
}
150
166
}
151
167
152
168
if ( ! result ) {
153
- result = android . graphics . Typeface . create ( 'sans-serif' + getFontWeightSuffix ( font . fontWeight ) , fontStyle ) ;
169
+ result = android . graphics . Typeface . create ( 'sans-serif' + fontSuffix , fontStyle ) ;
170
+ }
171
+
172
+ // Newer versions can accept a specific font weight number
173
+ if ( supportsFontWeight ) {
174
+ result = android . graphics . Typeface . create ( result , getFontWeightNumber ( fontWeight ) , font . isItalic ) ;
154
175
}
155
176
156
177
return result ;
@@ -184,3 +205,27 @@ function getFontWeightSuffix(fontWeight: FontWeightType): string {
184
205
throw new Error ( `Invalid font weight: "${ fontWeight } "` ) ;
185
206
}
186
207
}
208
+
209
+ function getFontWeightNumber ( fontWeight : FontWeightType ) : number {
210
+ let value : number ;
211
+
212
+ if ( typeof fontWeight === 'number' ) {
213
+ value = fontWeight ;
214
+ } else {
215
+ switch ( fontWeight ) {
216
+ case FontWeight . NORMAL :
217
+ case undefined :
218
+ case null :
219
+ value = 400 ;
220
+ break ;
221
+ case FontWeight . BOLD :
222
+ value = 700 ;
223
+ break ;
224
+ default :
225
+ value = parseInt ( fontWeight ) ;
226
+ break ;
227
+ }
228
+ }
229
+
230
+ return value ;
231
+ }
0 commit comments