MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / loadFontFromFile

Function loadFontFromFile

packages/core/ui/styling/font.android.ts:60–119  ·  view source on GitHub ↗
(fontFamily: string, font: Font)

Source from the content-addressed store, hash-verified

58}
59
60function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Typeface {
61 const cacheKey = SDK_VERSION >= 26 ? computeFontCacheKey(fontFamily, font) : fontFamily;
62
63 appAssets = appAssets || getNativeApp<android.app.Application>().getApplicationContext().getAssets();
64 if (!appAssets) {
65 return null;
66 }
67
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);
74
75 let fontAssetPath: string;
76
77 if (fs.File.exists(basePath + '.ttf')) {
78 fontAssetPath = basePath + '.ttf';
79 } else if (fs.File.exists(basePath + '.otf')) {
80 fontAssetPath = basePath + '.otf';
81 } else {
82 fontAssetPath = null;
83
84 if (Trace.isEnabled()) {
85 Trace.write('Could not find font file for ' + fontFamily, Trace.categories.Error, Trace.messageType.error);
86 }
87 }
88
89 result = null; // Default
90
91 if (fontAssetPath) {
92 try {
93 if (SDK_VERSION >= 26) {
94 const builder = new android.graphics.Typeface.Builder(fontAssetPath);
95 if (builder) {
96 builder.setFontVariationSettings(font.fontVariationSettings?.length ? FontVariationSettings.toString(font.fontVariationSettings) : '');
97 result = builder.build();
98 } else {
99 result = android.graphics.Typeface.createFromFile(fontAssetPath);
100 if (Trace.isEnabled()) {
101 Trace.write('Could not create builder for ' + fontFamily, Trace.categories.Error, Trace.messageType.error);
102 }
103 }
104 } else {
105 result = android.graphics.Typeface.createFromFile(fontAssetPath);
106 }
107 } catch (e) {
108 if (Trace.isEnabled()) {
109 Trace.write('Error loading font asset: ' + fontAssetPath, Trace.categories.Error, Trace.messageType.error);
110 }
111 }
112 }
113
114 // The value will be null if there has already been an attempt to load the font but failed
115 typefaceCache.set(cacheKey, result);
116 }
117

Callers 2

createTypefaceFunction · 0.85
createTypefaceLegacyFunction · 0.85

Calls 10

getNativeAppFunction · 0.90
joinMethod · 0.80
isEnabledMethod · 0.80
buildMethod · 0.80
computeFontCacheKeyFunction · 0.70
getMethod · 0.65
writeMethod · 0.65
existsMethod · 0.45
toStringMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected