mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 20:11:24 +08:00
perf: Avoid creating new typeface instances for system fonts
This commit is contained in:

committed by
Nathan Walker

parent
30da2752ad
commit
4902e27818
@ -41,7 +41,7 @@ export class Font extends FontBase {
|
|||||||
|
|
||||||
getAndroidTypeface(): android.graphics.Typeface {
|
getAndroidTypeface(): android.graphics.Typeface {
|
||||||
if (!this._typeface) {
|
if (!this._typeface) {
|
||||||
this._typeface = createTypeface(this);
|
this._typeface = SDK_VERSION >= 28 ? createTypeface(this) : createTypefaceLegacy(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._typeface;
|
return this._typeface;
|
||||||
@ -120,17 +120,53 @@ function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Type
|
|||||||
|
|
||||||
function createTypeface(font: Font): android.graphics.Typeface {
|
function createTypeface(font: Font): android.graphics.Typeface {
|
||||||
const fontFamilies = parseFontFamily(font.fontFamily);
|
const fontFamilies = parseFontFamily(font.fontFamily);
|
||||||
const fontWeight = font.fontWeight;
|
const fontWeightNum = getNumericFontWeight(font.fontWeight);
|
||||||
const isNumericFontWeightSupported = SDK_VERSION >= 28;
|
|
||||||
|
|
||||||
let result: android.graphics.Typeface;
|
let result: android.graphics.Typeface;
|
||||||
let fontStyle: number = 0; // This will be empty if numeric font weight is supported
|
|
||||||
// https://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
|
|
||||||
let fontSuffix: string;
|
|
||||||
|
|
||||||
if (isNumericFontWeightSupported) {
|
for (const fontFamily of fontFamilies) {
|
||||||
fontSuffix = '';
|
switch (fontFamily.toLowerCase()) {
|
||||||
} else {
|
case genericFontFamilies.serif:
|
||||||
|
result = android.graphics.Typeface.create(android.graphics.Typeface.SERIF, fontWeightNum, font.isItalic);
|
||||||
|
break;
|
||||||
|
case genericFontFamilies.sansSerif:
|
||||||
|
case genericFontFamilies.system:
|
||||||
|
result = android.graphics.Typeface.create(android.graphics.Typeface.SANS_SERIF, fontWeightNum, font.isItalic);
|
||||||
|
break;
|
||||||
|
case genericFontFamilies.monospace:
|
||||||
|
result = android.graphics.Typeface.create(android.graphics.Typeface.MONOSPACE, fontWeightNum, font.isItalic);
|
||||||
|
break;
|
||||||
|
default: {
|
||||||
|
result = loadFontFromFile(fontFamily, font);
|
||||||
|
if (result) {
|
||||||
|
result = android.graphics.Typeface.create(result, fontWeightNum, font.isItalic);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Found the font!
|
||||||
|
if (result) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
result = android.graphics.Typeface.create(android.graphics.Typeface.SANS_SERIF, fontWeightNum, font.isItalic);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createTypefaceLegacy(font: Font): android.graphics.Typeface {
|
||||||
|
const fontFamilies = parseFontFamily(font.fontFamily);
|
||||||
|
const fontWeight = font.fontWeight;
|
||||||
|
// https://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
|
||||||
|
const fontSuffix = getFontWeightSuffix(fontWeight);
|
||||||
|
|
||||||
|
let result: android.graphics.Typeface;
|
||||||
|
let fontStyle: number = 0;
|
||||||
|
|
||||||
if (font.isBold) {
|
if (font.isBold) {
|
||||||
fontStyle |= android.graphics.Typeface.BOLD;
|
fontStyle |= android.graphics.Typeface.BOLD;
|
||||||
}
|
}
|
||||||
@ -138,9 +174,6 @@ function createTypeface(font: Font): android.graphics.Typeface {
|
|||||||
fontStyle |= android.graphics.Typeface.ITALIC;
|
fontStyle |= android.graphics.Typeface.ITALIC;
|
||||||
}
|
}
|
||||||
|
|
||||||
fontSuffix = getFontWeightSuffix(fontWeight);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const fontFamily of fontFamilies) {
|
for (const fontFamily of fontFamilies) {
|
||||||
switch (fontFamily.toLowerCase()) {
|
switch (fontFamily.toLowerCase()) {
|
||||||
case genericFontFamilies.serif:
|
case genericFontFamilies.serif:
|
||||||
@ -172,11 +205,6 @@ function createTypeface(font: Font): android.graphics.Typeface {
|
|||||||
result = android.graphics.Typeface.create('sans-serif' + fontSuffix, fontStyle);
|
result = android.graphics.Typeface.create('sans-serif' + fontSuffix, fontStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Newer android versions can accept a numeric font weight
|
|
||||||
if (isNumericFontWeightSupported) {
|
|
||||||
result = android.graphics.Typeface.create(result, getNumericFontWeight(fontWeight), font.isItalic);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user