UNPKG

7.23 kBJavaScriptView Raw
1import { Font as FontBase, parseFontFamily, FontWeight, FontVariationSettings, fuzzySearch } from './font-common';
2import { Trace } from '../../trace';
3import * as fs from '../../file-system';
4export { FontStyle, FontWeight, FontVariationSettings, parseFont } from './font-common';
5const uiFontCache = new Map();
6function computeFontCacheKey(fontDescriptor) {
7 const { fontFamily, fontSize, fontWeight, fontVariationSettings, isBold, isItalic } = fontDescriptor;
8 const sep = ':';
9 return [fontFamily.join(sep), fontSize, fontWeight, String(FontVariationSettings.toString(fontVariationSettings)).replace(/'/g, '').replace(/[\s,]/g, '_'), isBold, isItalic].join(sep);
10}
11function getUIFontCached(fontDescriptor) {
12 const cacheKey = computeFontCacheKey(fontDescriptor);
13 if (uiFontCache.has(cacheKey)) {
14 if (Trace.isEnabled()) {
15 Trace.write(`UIFont reuse from cache: ${JSON.stringify(fontDescriptor)}, cache size: ${uiFontCache.size}`, Trace.categories.Style, Trace.messageType.info);
16 }
17 return uiFontCache.get(cacheKey);
18 }
19 let uiFont = NativeScriptUtils.createUIFont(fontDescriptor);
20 if (fontDescriptor.fontVariationSettings?.length) {
21 let font = CGFontCreateWithFontName(uiFont.fontName);
22 const variationAxes = CGFontCopyVariationAxes(font);
23 // This can be null if font doesn't support axes
24 if (variationAxes?.count) {
25 const variationSettings = NSMutableDictionary.new();
26 const variationAxesCount = variationAxes.count;
27 const variationAxesNames = [];
28 for (let i = 0, length = variationAxes.count; i < length; i++) {
29 variationAxesNames.push(String(variationAxes[i].objectForKey('kCGFontVariationAxisName')));
30 }
31 for (const variationSetting of fontDescriptor.fontVariationSettings) {
32 const axisName = fuzzySearch(variationSetting.axis, variationAxesNames);
33 if (axisName?.length) {
34 variationSettings.setValueForKey(variationSetting.value, axisName[0]);
35 }
36 }
37 font = CGFontCreateCopyWithVariations(font, variationSettings);
38 uiFont = CTFontCreateWithGraphicsFont(font, fontDescriptor.fontSize, null, null);
39 }
40 }
41 uiFontCache.set(cacheKey, uiFont);
42 if (Trace.isEnabled()) {
43 Trace.write(`UIFont creation: ${JSON.stringify(fontDescriptor)}, cache size: ${uiFontCache.size}`, Trace.categories.Style, Trace.messageType.info);
44 }
45 return uiFont;
46}
47export class Font extends FontBase {
48 constructor(family, size, style, weight, scale, variationSettings) {
49 super(family, size, style, weight, scale, variationSettings);
50 }
51 withFontFamily(family) {
52 return new Font(family, this.fontSize, this.fontStyle, this.fontWeight, this.fontScale, this.fontVariationSettings);
53 }
54 withFontStyle(style) {
55 return new Font(this.fontFamily, this.fontSize, style, this.fontWeight, this.fontScale, this.fontVariationSettings);
56 }
57 withFontWeight(weight) {
58 return new Font(this.fontFamily, this.fontSize, this.fontStyle, weight, this.fontScale, this.fontVariationSettings);
59 }
60 withFontSize(size) {
61 return new Font(this.fontFamily, size, this.fontStyle, this.fontWeight, this.fontScale, this.fontVariationSettings);
62 }
63 withFontScale(scale) {
64 return new Font(this.fontFamily, this.fontSize, this.fontStyle, this.fontWeight, scale, this.fontVariationSettings);
65 }
66 withFontVariationSettings(variationSettings) {
67 return new Font(this.fontFamily, this.fontSize, this.fontStyle, this.fontWeight, this.fontScale, variationSettings);
68 }
69 getUIFont(defaultFont) {
70 return getUIFontCached({
71 fontFamily: parseFontFamily(this.fontFamily),
72 // Apply a11y scale and calculate proper font size (avoid applying multiplier to native point size as it's messing calculations)
73 fontSize: this.fontSize ? this.fontSize * this.fontScale : defaultFont.pointSize,
74 fontWeight: getNativeFontWeight(this.fontWeight),
75 fontVariationSettings: this.fontVariationSettings,
76 isBold: this.isBold,
77 isItalic: this.isItalic,
78 });
79 }
80 getAndroidTypeface() {
81 return undefined;
82 }
83}
84Font.default = new Font(undefined, undefined);
85function getNativeFontWeight(fontWeight) {
86 if (typeof fontWeight === 'number') {
87 fontWeight = (fontWeight + '');
88 }
89 switch (fontWeight) {
90 case FontWeight.THIN:
91 return UIFontWeightUltraLight;
92 case FontWeight.EXTRA_LIGHT:
93 return UIFontWeightThin;
94 case FontWeight.LIGHT:
95 return UIFontWeightLight;
96 case FontWeight.NORMAL:
97 case '400':
98 case undefined:
99 case null:
100 return UIFontWeightRegular;
101 case FontWeight.MEDIUM:
102 return UIFontWeightMedium;
103 case FontWeight.SEMI_BOLD:
104 return UIFontWeightSemibold;
105 case FontWeight.BOLD:
106 case '700':
107 return UIFontWeightBold;
108 case FontWeight.EXTRA_BOLD:
109 return UIFontWeightHeavy;
110 case FontWeight.BLACK:
111 return UIFontWeightBlack;
112 default:
113 console.log(`Invalid font weight: "${fontWeight}"`);
114 }
115}
116export var ios;
117(function (ios) {
118 function registerFont(fontFile) {
119 let filePath = fs.path.join(fs.knownFolders.currentApp().path, 'fonts', fontFile);
120 if (!fs.File.exists(filePath)) {
121 filePath = fs.path.join(fs.knownFolders.currentApp().path, fontFile);
122 }
123 const fontData = NSFileManager.defaultManager.contentsAtPath(filePath);
124 if (!fontData) {
125 throw new Error('Could not load font from: ' + fontFile);
126 }
127 const provider = CGDataProviderCreateWithCFData(fontData);
128 const font = CGFontCreateWithDataProvider(provider);
129 if (!font) {
130 throw new Error('Could not load font from: ' + fontFile);
131 }
132 const error = new interop.Reference();
133 if (!CTFontManagerRegisterGraphicsFont(font, error)) {
134 if (Trace.isEnabled()) {
135 Trace.write('Error occur while registering font: ' + CFErrorCopyDescription(error.value), Trace.categories.Error, Trace.messageType.error);
136 }
137 }
138 }
139 ios.registerFont = registerFont;
140})(ios || (ios = {}));
141function registerFontsInFolder(fontsFolderPath) {
142 const fontsFolder = fs.Folder.fromPath(fontsFolderPath);
143 fontsFolder.eachEntity((fileEntity) => {
144 if (fs.Folder.exists(fs.path.join(fontsFolderPath, fileEntity.name))) {
145 return true;
146 }
147 if (fileEntity instanceof fs.File && (fileEntity.extension === '.ttf' || fileEntity.extension === '.otf')) {
148 ios.registerFont(fileEntity.name);
149 }
150 return true;
151 });
152}
153function registerCustomFonts() {
154 const appDir = fs.knownFolders.currentApp().path;
155 const fontsDir = fs.path.join(appDir, 'fonts');
156 if (fs.Folder.exists(fontsDir)) {
157 registerFontsInFolder(fontsDir);
158 }
159}
160registerCustomFonts();
161//# sourceMappingURL=font.ios.js.map
\No newline at end of file