Skip to content

Commit fca8b16

Browse files
author
Alexander Vakrilov
authored
Fonts refactored (NativeScript#4436)
* Fonts refactored * Fix: never return null font
1 parent f399f6c commit fca8b16

4 files changed

Lines changed: 116 additions & 227 deletions

File tree

apps/app/ui-tests-app/font/all-fonts.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { Label } from "tns-core-modules/ui/label";
66
import { FontStyle, FontWeight } from "tns-core-modules/ui/enums";
77
import * as typeUtils from "tns-core-modules/utils/types";
88
import { Color } from "tns-core-modules/color";
9-
import * as font from "tns-core-modules/ui/styling/font";
9+
import * as utils from "tns-core-modules/utils/utils";
10+
import { isIOS } from "tns-core-modules/platform";
1011

1112
const genericFontFamilies = [
1213
"system",
@@ -46,18 +47,20 @@ let compareIgnoreCase = function (a, b) {
4647
return a.toLowerCase().localeCompare(b.toLowerCase());
4748
};
4849

49-
if (font.ios) {
50-
// for (let f = 0; f < embeddedFontNames.length; f++) {
51-
// font.ios.registerFont(`fonts/${embeddedFontNames[f]}.ttf`);
52-
// }
50+
if (isIOS) {
51+
const nsFontFamilies = utils.ios.getter(UIFont, UIFont.familyNames);
52+
for (let i = 0; i < nsFontFamilies.count; i++) {
53+
const family = nsFontFamilies.objectAtIndex(i);
54+
fontFamilies.push(family)
5355

54-
let font_internal = <any>font;
55-
font_internal.ensureSystemFontSets();
56+
const nsFonts = UIFont.fontNamesForFamilyName(family);
57+
for (let j = 0; j < nsFonts.count; j++) {
58+
const font = nsFonts.objectAtIndex(j);
59+
fontNames.push(font)
60+
}
61+
}
5662

57-
(<Set<string>>font_internal.systemFontFamilies).forEach(f => fontFamilies.push(f));
5863
fontFamilies = fontFamilies.sort(compareIgnoreCase);
59-
60-
(<Set<string>>font_internal.systemFonts).forEach(f => fontNames.push(f));
6164
fontNames = fontNames.sort(compareIgnoreCase);
6265
}
6366

@@ -82,8 +85,7 @@ function generateLabels(layout: StackLayout) {
8285
}
8386
}
8487

85-
if (fontFamilies.length > 0)
86-
{
88+
if (fontFamilies.length > 0) {
8789
layout.addChild(prepareTitle("Font Families", 24));
8890
}
8991
for (let f = 0; f < fontFamilies.length; f++) {
@@ -134,12 +136,12 @@ function prepareLabel(fontFamily: string, fontStyle: string, fontWeight: string)
134136
let fontStyleCss = fontStyle !== FontStyle.normal ? `font-style: ${fontStyle}; ` : "";
135137
let fontWeightCss = fontWeight !== FontWeight.normal ? `font-weight: ${fontWeight}; ` : "";
136138
let css = `${fontFamilyCss}${fontStyleCss}${fontWeightCss}`;
137-
label.text = `${typeUtils.getClass(label) } {${css}};`;
139+
label.text = `${typeUtils.getClass(label)} {${css}};`;
138140
label.textWrap = true;
139141
label.style.textAlignment = "left";
140142
label.borderWidth = 1;
141143
label.borderColor = black;
142-
label.style.padding = "2";
144+
label.style.padding = "2";
143145
label.setInlineStyle(css);
144146
label.on("loaded", args => {
145147
let sender = <Label>args.object;

tns-core-modules/ui/styling/font-common.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ export abstract class FontBase implements FontDefinition {
99
}
1010

1111
get isBold(): boolean {
12-
return this.fontWeight === FontWeight.BOLD
13-
|| this.fontWeight === "700";
12+
return this.fontWeight === FontWeight.SEMI_BOLD ||
13+
this.fontWeight === FontWeight.BOLD ||
14+
this.fontWeight === "700" ||
15+
this.fontWeight === FontWeight.EXTRA_BOLD ||
16+
this.fontWeight === FontWeight.BLACK;
1417
}
1518

1619
protected constructor(

tns-core-modules/ui/styling/font.android.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,7 @@ export class Font extends FontBase {
3636

3737
public getAndroidTypeface(): android.graphics.Typeface {
3838
if (!this._typeface) {
39-
let fontStyle = 0;
40-
if (this.isBold) {
41-
fontStyle |= android.graphics.Typeface.BOLD;
42-
}
43-
if (this.isItalic) {
44-
fontStyle |= android.graphics.Typeface.ITALIC;
45-
}
46-
47-
this._typeface = createTypeface(this, fontStyle);
39+
this._typeface = createTypeface(this);
4840
}
4941
return this._typeface;
5042
}
@@ -95,11 +87,19 @@ function loadFontFromFile(fontFamily: string): android.graphics.Typeface {
9587
return result;
9688
}
9789

98-
function createTypeface(font: Font, fontStyle: number): android.graphics.Typeface {
90+
function createTypeface(font: Font): android.graphics.Typeface {
91+
let fontStyle = 0;
92+
if (font.isBold) {
93+
fontStyle |= android.graphics.Typeface.BOLD;
94+
}
95+
if (font.isItalic) {
96+
fontStyle |= android.graphics.Typeface.ITALIC;
97+
}
98+
9999
//http://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
100100
const fonts = parseFontFamily(font.fontFamily);
101101
let result = null;
102-
for (let i = 0; i < fonts.length && !result; i++) {
102+
for (let i = 0; i < fonts.length; i++) {
103103
switch (fonts[i].toLowerCase()) {
104104
case genericFontFamilies.serif:
105105
result = android.graphics.Typeface.create("serif" + getFontWeightSuffix(font.fontWeight), fontStyle);
@@ -116,17 +116,22 @@ function createTypeface(font: Font, fontStyle: number): android.graphics.Typefac
116116

117117
default:
118118
result = loadFontFromFile(fonts[i]);
119-
if (fontStyle) {
119+
if (result && fontStyle) {
120120
result = android.graphics.Typeface.create(result, fontStyle);
121121
}
122122
break;
123123
}
124+
125+
if (result) {
126+
// Found the font!
127+
break;
128+
}
124129
}
125130

126-
if (fontStyle && !result) {
127-
result = android.graphics.Typeface.create(result, fontStyle);
131+
if (!result) {
132+
result = android.graphics.Typeface.create("sans-serif" + getFontWeightSuffix(font.fontWeight), fontStyle);
128133
}
129-
134+
130135
return result;
131136
}
132137

0 commit comments

Comments
 (0)