Skip to content

Commit

Permalink
Add fallback fontFamily on Web env. (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenoslin committed May 13, 2022
1 parent c8ef89f commit f8dbc60
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions web/src/core/scaler-context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NativeImage } from './native-image';
import { measureText } from '../utils/measure-text';
import { defaultFontNames, getFontFamilys } from '../utils/font-family';
import { defaultFontNames, getFontFamilies } from '../utils/font-family';
import { Rect } from '../types';

const canvas = ((): HTMLCanvasElement | OffscreenCanvas => {
Expand Down Expand Up @@ -68,7 +68,7 @@ export class ScalerContext {
attributes.push(`${this.size}px`);
// css font-family
const fallbackFontNames = defaultFontNames.concat();
fallbackFontNames.unshift(...getFontFamilys(this.fontName, this.fontStyle));
fallbackFontNames.unshift(...getFontFamilies(this.fontName, this.fontStyle));
attributes.push(`${fallbackFontNames.join(',')}`);
return attributes.join(' ');
}
Expand Down
9 changes: 7 additions & 2 deletions web/src/utils/font-family.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const defaultFontNames = (() => {
return ['emoji'].concat(...fontNames);
})();

export const getFontFamilys = (name: string, style = ''): string[] => {
export const getFontFamilies = (name: string, style = ''): string[] => {
if (!name) return [];
const nameChars = name.split(' ');
let names = [];
Expand All @@ -14,7 +14,7 @@ export const getFontFamilys = (name: string, style = ''): string[] => {
names.push(nameChars.join(''));
names.push(nameChars.join(' '));
}
return names.reduce((pre: string[], cur: string) => {
const fontFamilies = names.reduce((pre: string[], cur: string) => {
if (!style) {
pre.push(`${cur}`);
} else {
Expand All @@ -23,4 +23,9 @@ export const getFontFamilys = (name: string, style = ''): string[] => {
}
return pre;
}, []);
// Fallback font when style is not found.
if (style !== '') {
fontFamilies.push(name);
}
return fontFamilies;
};

0 comments on commit f8dbc60

Please sign in to comment.