Skip to content

Commit 4902e27

Browse files
CatchABusNathanWalker
authored andcommitted
perf: Avoid creating new typeface instances for system fonts
1 parent 30da275 commit 4902e27

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

packages/core/ui/styling/font.android.ts

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Font extends FontBase {
4141

4242
getAndroidTypeface(): android.graphics.Typeface {
4343
if (!this._typeface) {
44-
this._typeface = createTypeface(this);
44+
this._typeface = SDK_VERSION >= 28 ? createTypeface(this) : createTypefaceLegacy(this);
4545
}
4646

4747
return this._typeface;
@@ -120,25 +120,58 @@ function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Type
120120

121121
function createTypeface(font: Font): android.graphics.Typeface {
122122
const fontFamilies = parseFontFamily(font.fontFamily);
123-
const fontWeight = font.fontWeight;
124-
const isNumericFontWeightSupported = SDK_VERSION >= 28;
123+
const fontWeightNum = getNumericFontWeight(font.fontWeight);
125124

126125
let result: android.graphics.Typeface;
127-
let fontStyle: number = 0; // This will be empty if numeric font weight is supported
128-
// https://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
129-
let fontSuffix: string;
130126

131-
if (isNumericFontWeightSupported) {
132-
fontSuffix = '';
133-
} else {
134-
if (font.isBold) {
135-
fontStyle |= android.graphics.Typeface.BOLD;
127+
for (const fontFamily of fontFamilies) {
128+
switch (fontFamily.toLowerCase()) {
129+
case genericFontFamilies.serif:
130+
result = android.graphics.Typeface.create(android.graphics.Typeface.SERIF, fontWeightNum, font.isItalic);
131+
break;
132+
case genericFontFamilies.sansSerif:
133+
case genericFontFamilies.system:
134+
result = android.graphics.Typeface.create(android.graphics.Typeface.SANS_SERIF, fontWeightNum, font.isItalic);
135+
break;
136+
case genericFontFamilies.monospace:
137+
result = android.graphics.Typeface.create(android.graphics.Typeface.MONOSPACE, fontWeightNum, font.isItalic);
138+
break;
139+
default: {
140+
result = loadFontFromFile(fontFamily, font);
141+
if (result) {
142+
result = android.graphics.Typeface.create(result, fontWeightNum, font.isItalic);
143+
}
144+
break;
145+
}
136146
}
137-
if (font.isItalic) {
138-
fontStyle |= android.graphics.Typeface.ITALIC;
147+
148+
// Found the font!
149+
if (result) {
150+
break;
139151
}
152+
}
153+
154+
if (!result) {
155+
result = android.graphics.Typeface.create(android.graphics.Typeface.SANS_SERIF, fontWeightNum, font.isItalic);
156+
}
157+
158+
return result;
159+
}
140160

141-
fontSuffix = getFontWeightSuffix(fontWeight);
161+
function createTypefaceLegacy(font: Font): android.graphics.Typeface {
162+
const fontFamilies = parseFontFamily(font.fontFamily);
163+
const fontWeight = font.fontWeight;
164+
// https://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
165+
const fontSuffix = getFontWeightSuffix(fontWeight);
166+
167+
let result: android.graphics.Typeface;
168+
let fontStyle: number = 0;
169+
170+
if (font.isBold) {
171+
fontStyle |= android.graphics.Typeface.BOLD;
172+
}
173+
if (font.isItalic) {
174+
fontStyle |= android.graphics.Typeface.ITALIC;
142175
}
143176

144177
for (const fontFamily of fontFamilies) {
@@ -172,11 +205,6 @@ function createTypeface(font: Font): android.graphics.Typeface {
172205
result = android.graphics.Typeface.create('sans-serif' + fontSuffix, fontStyle);
173206
}
174207

175-
// Newer android versions can accept a numeric font weight
176-
if (isNumericFontWeightSupported) {
177-
result = android.graphics.Typeface.create(result, getNumericFontWeight(fontWeight), font.isItalic);
178-
}
179-
180208
return result;
181209
}
182210

0 commit comments

Comments
 (0)