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' ;
22import { FontStyleType , FontWeightType , FontVariationSettingsType } from './font-interfaces' ;
33import { Trace } from '../../trace' ;
44import { SDK_VERSION } from '../../utils/constants' ;
@@ -7,7 +7,6 @@ import { ad } from '../../utils';
77
88export * from './font-common' ;
99
10- const FONTS_BASE_PATH = '/fonts/' ;
1110const typefaceCache = new Map < string , android . graphics . Typeface > ( ) ;
1211let appAssets : android . content . res . AssetManager ;
1312
@@ -66,24 +65,28 @@ function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Type
6665 return null ;
6766 }
6867
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 ) ;
7374
7475 let fontAssetPath : string ;
75- const basePath = fs . path . join ( fs . knownFolders . currentApp ( ) . path , 'fonts' , fontFamily ) ;
76+
7677 if ( fs . File . exists ( basePath + '.ttf' ) ) {
77- fontAssetPath = FONTS_BASE_PATH + fontFamily + '.ttf' ;
78+ fontAssetPath = basePath + '.ttf' ;
7879 } else if ( fs . File . exists ( basePath + '.otf' ) ) {
79- fontAssetPath = FONTS_BASE_PATH + fontFamily + '.otf' ;
80+ fontAssetPath = basePath + '.otf' ;
8081 } else if ( Trace . isEnabled ( ) ) {
82+ fontAssetPath = null ;
8183 Trace . write ( 'Could not find font file for ' + fontFamily , Trace . categories . Error , Trace . messageType . error ) ;
8284 }
8385
86+ result = null ; // Default
87+
8488 if ( fontAssetPath ) {
8589 try {
86- fontAssetPath = fs . path . join ( fs . knownFolders . currentApp ( ) . path , fontAssetPath ) ;
8790 if ( SDK_VERSION >= 26 ) {
8891 const builder = new android . graphics . Typeface . Builder ( fontAssetPath ) ;
8992 if ( builder ) {
@@ -104,35 +107,48 @@ function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Type
104107 }
105108 }
106109 }
110+
111+ // The value might be null if there has already been an attempt to load the font but failed
107112 typefaceCache . set ( cacheKey , result ) ;
108113 }
109114
110115 return result ;
111116}
112117
113118function 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 ) ;
120139 }
121140
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 ;
125141 for ( const fontFamily of fontFamilies ) {
126142 switch ( fontFamily . toLowerCase ( ) ) {
127143 case genericFontFamilies . serif :
128- result = android . graphics . Typeface . create ( 'serif' + getFontWeightSuffix ( font . fontWeight ) , fontStyle ) ;
144+ result = android . graphics . Typeface . create ( 'serif' + fontSuffix , fontStyle ) ;
129145 break ;
130146 case genericFontFamilies . sansSerif :
131147 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 ) ;
133149 break ;
134150 case genericFontFamilies . monospace :
135- result = android . graphics . Typeface . create ( 'monospace' + getFontWeightSuffix ( font . fontWeight ) , fontStyle ) ;
151+ result = android . graphics . Typeface . create ( 'monospace' + fontSuffix , fontStyle ) ;
136152 break ;
137153 default : {
138154 result = loadFontFromFile ( fontFamily , font ) ;
@@ -143,14 +159,19 @@ function createTypeface(font: Font): android.graphics.Typeface {
143159 }
144160 }
145161
162+ // Found the font!
146163 if ( result ) {
147- // Found the font!
148164 break ;
149165 }
150166 }
151167
152168 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 ) ;
154175 }
155176
156177 return result ;
@@ -184,3 +205,27 @@ function getFontWeightSuffix(fontWeight: FontWeightType): string {
184205 throw new Error ( `Invalid font weight: "${ fontWeight } "` ) ;
185206 }
186207}
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