UNPKG

4.26 kBJavaScriptView Raw
1import { Font as FontBase, parseFontFamily, FontStyle, FontWeight } from './font-common';
2import { Trace } from '../../trace';
3import * as fs from '../../file-system';
4export * from './font-common';
5export class Font extends FontBase {
6 constructor(family, size, style, weight, scale) {
7 super(family, size, style, weight, scale);
8 }
9 withFontFamily(family) {
10 return new Font(family, this.fontSize, this.fontStyle, this.fontWeight, this.fontScale);
11 }
12 withFontStyle(style) {
13 return new Font(this.fontFamily, this.fontSize, style, this.fontWeight, this.fontScale);
14 }
15 withFontWeight(weight) {
16 return new Font(this.fontFamily, this.fontSize, this.fontStyle, weight, this.fontScale);
17 }
18 withFontSize(size) {
19 return new Font(this.fontFamily, size, this.fontStyle, this.fontWeight, this.fontScale);
20 }
21 withFontScale(scale) {
22 return new Font(this.fontFamily, this.fontSize, this.fontStyle, this.fontWeight, scale);
23 }
24 getUIFont(defaultFont) {
25 return new WeakRef(NativeScriptUtils.createUIFont({
26 fontFamily: parseFontFamily(this.fontFamily),
27 fontSize: this.fontSize || defaultFont.pointSize,
28 fontWeight: getNativeFontWeight(this.fontWeight),
29 isBold: this.isBold,
30 isItalic: this.isItalic,
31 })).get();
32 }
33 getAndroidTypeface() {
34 return undefined;
35 }
36}
37Font.default = new Font(undefined, undefined, FontStyle.NORMAL, FontWeight.NORMAL, 1);
38function getNativeFontWeight(fontWeight) {
39 switch (fontWeight) {
40 case FontWeight.THIN:
41 return UIFontWeightUltraLight;
42 case FontWeight.EXTRA_LIGHT:
43 return UIFontWeightThin;
44 case FontWeight.LIGHT:
45 return UIFontWeightLight;
46 case FontWeight.NORMAL:
47 case '400':
48 case undefined:
49 case null:
50 return UIFontWeightRegular;
51 case FontWeight.MEDIUM:
52 return UIFontWeightMedium;
53 case FontWeight.SEMI_BOLD:
54 return UIFontWeightSemibold;
55 case FontWeight.BOLD:
56 case '700':
57 return UIFontWeightBold;
58 case FontWeight.EXTRA_BOLD:
59 return UIFontWeightHeavy;
60 case FontWeight.BLACK:
61 return UIFontWeightBlack;
62 default:
63 console.log(`Invalid font weight: "${fontWeight}"`);
64 }
65}
66export var ios;
67(function (ios) {
68 function registerFont(fontFile) {
69 let filePath = fs.path.join(fs.knownFolders.currentApp().path, 'fonts', fontFile);
70 if (!fs.File.exists(filePath)) {
71 filePath = fs.path.join(fs.knownFolders.currentApp().path, fontFile);
72 }
73 const fontData = NSFileManager.defaultManager.contentsAtPath(filePath);
74 if (!fontData) {
75 throw new Error('Could not load font from: ' + fontFile);
76 }
77 const provider = CGDataProviderCreateWithCFData(fontData);
78 const font = CGFontCreateWithDataProvider(provider);
79 if (!font) {
80 throw new Error('Could not load font from: ' + fontFile);
81 }
82 const error = new interop.Reference();
83 if (!CTFontManagerRegisterGraphicsFont(font, error)) {
84 if (Trace.isEnabled()) {
85 Trace.write('Error occur while registering font: ' + CFErrorCopyDescription(error.value), Trace.categories.Error, Trace.messageType.error);
86 }
87 }
88 }
89 ios.registerFont = registerFont;
90})(ios || (ios = {}));
91function registerFontsInFolder(fontsFolderPath) {
92 const fontsFolder = fs.Folder.fromPath(fontsFolderPath);
93 fontsFolder.eachEntity((fileEntity) => {
94 if (fs.Folder.exists(fs.path.join(fontsFolderPath, fileEntity.name))) {
95 return true;
96 }
97 if (fileEntity instanceof fs.File && (fileEntity.extension === '.ttf' || fileEntity.extension === '.otf')) {
98 ios.registerFont(fileEntity.name);
99 }
100 return true;
101 });
102}
103function registerCustomFonts() {
104 const appDir = fs.knownFolders.currentApp().path;
105 const fontsDir = fs.path.join(appDir, 'fonts');
106 if (fs.Folder.exists(fontsDir)) {
107 registerFontsInFolder(fontsDir);
108 }
109}
110registerCustomFonts();
111//# sourceMappingURL=font.ios.js.map
\No newline at end of file