UNPKG

5.36 kBJavaScriptView Raw
1import { Font as FontBase, parseFontFamily, genericFontFamilies, FontWeight } from './font-common';
2import { Trace } from '../../trace';
3import * as application from '../../application';
4import * as fs from '../../file-system';
5export * from './font-common';
6const FONTS_BASE_PATH = '/fonts/';
7const typefaceCache = new Map();
8let appAssets;
9export class Font extends FontBase {
10 constructor(family, size, style, weight) {
11 super(family, size, style, weight, 1);
12 }
13 withFontFamily(family) {
14 return new Font(family, this.fontSize, this.fontStyle, this.fontWeight);
15 }
16 withFontStyle(style) {
17 return new Font(this.fontFamily, this.fontSize, style, this.fontWeight);
18 }
19 withFontWeight(weight) {
20 return new Font(this.fontFamily, this.fontSize, this.fontStyle, weight);
21 }
22 withFontSize(size) {
23 return new Font(this.fontFamily, size, this.fontStyle, this.fontWeight);
24 }
25 withFontScale(scale) {
26 return new Font(this.fontFamily, this.fontSize, this.fontStyle, this.fontWeight);
27 }
28 getAndroidTypeface() {
29 if (!this._typeface) {
30 this._typeface = createTypeface(this);
31 }
32 return this._typeface;
33 }
34 getUIFont(defaultFont) {
35 return undefined;
36 }
37}
38Font.default = new Font(undefined, undefined, 'normal', 'normal');
39function loadFontFromFile(fontFamily) {
40 appAssets = appAssets || application.android.context.getAssets();
41 if (!appAssets) {
42 return null;
43 }
44 let result = typefaceCache.get(fontFamily);
45 // Check for undefined explicitly as null mean we tried to load the font, but failed.
46 if (result === undefined) {
47 result = null;
48 let fontAssetPath;
49 const basePath = fs.path.join(fs.knownFolders.currentApp().path, 'fonts', fontFamily);
50 if (fs.File.exists(basePath + '.ttf')) {
51 fontAssetPath = FONTS_BASE_PATH + fontFamily + '.ttf';
52 }
53 else if (fs.File.exists(basePath + '.otf')) {
54 fontAssetPath = FONTS_BASE_PATH + fontFamily + '.otf';
55 }
56 else {
57 if (Trace.isEnabled()) {
58 Trace.write('Could not find font file for ' + fontFamily, Trace.categories.Error, Trace.messageType.error);
59 }
60 }
61 if (fontAssetPath) {
62 try {
63 fontAssetPath = fs.path.join(fs.knownFolders.currentApp().path, fontAssetPath);
64 result = android.graphics.Typeface.createFromFile(fontAssetPath);
65 }
66 catch (e) {
67 if (Trace.isEnabled()) {
68 Trace.write('Error loading font asset: ' + fontAssetPath, Trace.categories.Error, Trace.messageType.error);
69 }
70 }
71 }
72 typefaceCache.set(fontFamily, result);
73 }
74 return result;
75}
76function createTypeface(font) {
77 let fontStyle = 0;
78 if (font.isBold) {
79 fontStyle |= android.graphics.Typeface.BOLD;
80 }
81 if (font.isItalic) {
82 fontStyle |= android.graphics.Typeface.ITALIC;
83 }
84 //http://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
85 const fonts = parseFontFamily(font.fontFamily);
86 let result = null;
87 for (let i = 0; i < fonts.length; i++) {
88 switch (fonts[i].toLowerCase()) {
89 case genericFontFamilies.serif:
90 result = android.graphics.Typeface.create('serif' + getFontWeightSuffix(font.fontWeight), fontStyle);
91 break;
92 case genericFontFamilies.sansSerif:
93 case genericFontFamilies.system:
94 result = android.graphics.Typeface.create('sans-serif' + getFontWeightSuffix(font.fontWeight), fontStyle);
95 break;
96 case genericFontFamilies.monospace:
97 result = android.graphics.Typeface.create('monospace' + getFontWeightSuffix(font.fontWeight), fontStyle);
98 break;
99 default:
100 result = loadFontFromFile(fonts[i]);
101 if (result && fontStyle) {
102 result = android.graphics.Typeface.create(result, fontStyle);
103 }
104 break;
105 }
106 if (result) {
107 // Found the font!
108 break;
109 }
110 }
111 if (!result) {
112 result = android.graphics.Typeface.create('sans-serif' + getFontWeightSuffix(font.fontWeight), fontStyle);
113 }
114 return result;
115}
116function getFontWeightSuffix(fontWeight) {
117 switch (fontWeight) {
118 case FontWeight.THIN:
119 return android.os.Build.VERSION.SDK_INT >= 16 ? '-thin' : '';
120 case FontWeight.EXTRA_LIGHT:
121 case FontWeight.LIGHT:
122 return android.os.Build.VERSION.SDK_INT >= 16 ? '-light' : '';
123 case FontWeight.NORMAL:
124 case '400':
125 case undefined:
126 case null:
127 return '';
128 case FontWeight.MEDIUM:
129 case FontWeight.SEMI_BOLD:
130 return android.os.Build.VERSION.SDK_INT >= 21 ? '-medium' : '';
131 case FontWeight.BOLD:
132 case '700':
133 case FontWeight.EXTRA_BOLD:
134 return '';
135 case FontWeight.BLACK:
136 return android.os.Build.VERSION.SDK_INT >= 21 ? '-black' : '';
137 default:
138 throw new Error(`Invalid font weight: "${fontWeight}"`);
139 }
140}
141//# sourceMappingURL=font.android.js.map
\No newline at end of file