Skip to content

Commit

Permalink
Resolved sans-serif font issue for firefox browser (#418)
Browse files Browse the repository at this point in the history
The FireFox browser was throwing an error when using the default font
for canvas.

When the canvas text renderer tries to add the default font (fallback
font). However during the creation of the WebFont (line: 120
CanvasTextRenderer) it creates a FontFace which creates a family
property which is an empty string in FireFox. This is where we run into
problems because that empty string is used as a key later on.

I have resolved the issue by building in a fallback that in case the
family returns an empty string it falls back to the fontFamily property
of its super class.
  • Loading branch information
wouterlucas authored Oct 22, 2024
2 parents c3daa91 + d08a901 commit f704121
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/core/text-rendering/TrFontManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function resolveFontToUse(

for (const fontFamiles of familyMapsByPriority) {
const fontFaces = fontFamiles[family];

if (!fontFaces) {
continue;
}
Expand Down
1 change: 0 additions & 1 deletion src/core/text-rendering/font-face-types/TrFontFace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ export class TrFontFace extends EventEmitter {
lineGap: metrics.lineGap / metrics.unitsPerEm,
};
}

this.fontFamily = fontFamily;
this.descriptors = {
style: 'normal',
Expand Down
13 changes: 6 additions & 7 deletions src/core/text-rendering/renderers/CanvasTextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,22 @@ export class CanvasTextRenderer extends TextRenderer<CanvasTextRendererState> {
// the `isFontFaceSupported` check)
assertTruthy(fontFace instanceof WebTrFontFace);

const fontFamily = fontFace.fontFamily;

// Add the font face to the document
// Except for the 'sans-serif' font family, which the Renderer provides
// as a special default fallback.
if (fontFace.fontFamily !== 'sans-serif') {
if (fontFamily !== 'sans-serif') {
// @ts-expect-error `add()` method should be available from a FontFaceSet
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
globalFontSet.add(fontFace.fontFace);
}

const { fontFamilies } = this;
const familyName = fontFace.fontFace.family;

let faceSet = fontFamilies[familyName];
let faceSet = this.fontFamilies[fontFamily];
if (!faceSet) {
faceSet = new Set();
fontFamilies[familyName] = faceSet;
this.fontFamilies[fontFamily] = faceSet;
}

faceSet.add(fontFace);
}

Expand Down Expand Up @@ -384,6 +382,7 @@ export class CanvasTextRenderer extends TextRenderer<CanvasTextRendererState> {

loadFont = (state: CanvasTextRendererState): void => {
const cssString = getFontCssString(state.props);

const trFontFace = this.stage.fontManager.resolveFontFace(
this.fontFamilyArray,
state.props,
Expand Down

0 comments on commit f704121

Please sign in to comment.