Skip to content

AI-native design editor. Open-source Figma alternative.

fix: leave installed fonts to the page instead of registering faces under their names (#1738)

Danila Poyarkove2d537fe0b0c
diff --git a/changes/1738/fixed.md b/changes/1738/fixed.md
new file mode 100644
index 000000000..36b56ccb2
--- /dev/null
+++ b/changes/1738/fixed.md
@@ -0,0 +1 @@
+- On Windows, page text in an installed font, such as interactive component previews on the canvas, draws in its bold, italic, and light faces instead of the regular one.
diff --git a/packages/core/src/text/font/manager.ts b/packages/core/src/text/font/manager.ts
index 9dea55f9c..33e3eacc9 100644
--- a/packages/core/src/text/font/manager.ts
+++ b/packages/core/src/text/font/manager.ts
@@ -900,7 +900,12 @@ export class FontManager {
}
/** Adds `buffer` to the files of `style` after its own, returning the copy of it they keep. */
- private registerSupplemental(family: string, style: string, buffer: ArrayBuffer): ArrayBuffer {
+ private registerSupplemental(
+ family: string,
+ style: string,
+ buffer: ArrayBuffer,
+ source?: FontLoadedSource
+ ): ArrayBuffer {
const key = `${family}|${style}`
const supplemental = this.supplementalFamilyData.get(key) ?? []
const kept = supplemental.find((file) => sameFontFile(file, buffer))
@@ -908,7 +913,7 @@ export class FontManager {
supplemental.push(buffer)
this.supplementalFamilyData.set(key, supplemental)
this.registerFontInCanvasKit(family, buffer)
- this.registerFontInBrowser(family, style, buffer)
+ if (!pageDrawsByName(source)) this.registerFontInBrowser(family, style, buffer)
return buffer
}
@@ -928,16 +933,16 @@ export class FontManager {
}
if (existing) {
const replaced = this.replacedFileData.get(key) ?? new Set<ArrayBuffer>()
- replaced.add(this.registerSupplemental(family, style, existing))
+ replaced.add(
+ this.registerSupplemental(family, style, existing, this.loadedFamilySources.get(key))
+ )
this.replacedFileData.set(key, replaced)
this.replacedFamilies.add(family)
}
this.loadedFamilies.set(key, buffer)
if (source) this.loadedFamilySources.set(key, source)
this.registerFontInCanvasKit(family, buffer)
- // The page's own text falls back to the system's fonts by itself, and a browser face would be
- // one more copy of a whole system file.
- if (source !== 'fallback') this.registerFontInBrowser(family, style, buffer)
+ if (!pageDrawsByName(source)) this.registerFontInBrowser(family, style, buffer)
for (const listener of this.faceListeners) listener(family, style)
return buffer
}
@@ -994,4 +999,14 @@ export class FontManager {
}
}
+/**
+ * Whether the page draws a face from `source` by itself: it draws installed fonts by name and
+ * falls back to the system's. A browser face would copy a whole system file, and one under an
+ * installed family's name stands in for all of that family's faces: WebView2 draws their bold
+ * and italic text in it.
+ */
+function pageDrawsByName(source: FontLoadedSource | undefined): boolean {
+ return source === 'local' || source === 'fallback'
+}
+
export const fontManager = new FontManager()
diff --git a/packages/core/tests/text/font/manager.test.ts b/packages/core/tests/text/font/manager.test.ts
index b829715fa..122fb6146 100644
--- a/packages/core/tests/text/font/manager.test.ts
+++ b/packages/core/tests/text/font/manager.test.ts
@@ -242,6 +242,28 @@ test('a fallback leaves another language its letters, which that language loads
}
})
+test('leaves installed faces to the page, which draws them by name', async () => {
+ const manager = offlineManager()
+ const registered: string[] = []
+ manager.registerFontInBrowser = (family, style) => {
+ registered.push(`${family} ${style}`)
+ }
+ const installed = await Bun.file(join(FIXTURES, 'fonts', 'InterRegular-Faces.ttf')).arrayBuffer()
+ manager.setHostFontLoader(async (family) => (family === 'Installed Sans' ? installed : null))
+
+ await manager.loadFont('Installed Sans', 'Regular')
+ await manager.loadFont('Inter', 'Regular')
+
+ expect(manager.isStyleLoaded('Installed Sans', 'Regular')).toBe(true)
+ // A page face under an installed family's name would stand in for all of its faces.
+ expect(registered).toEqual(['Inter Regular'])
+
+ // A file that replaces the installed one, as a web font does a fallback, keeps that one off it.
+ const replacement = await Bun.file(join(FIXTURES, 'fonts', 'InterVariable-Latin.ttf')).arrayBuffer()
+ await manager.markLoaded('Installed Sans', 'Regular', replacement)
+ expect(registered).toEqual(['Inter Regular', 'Installed Sans Regular'])
+})
+
test('a host read that outlives its loader never registers the replaced loader data', async () => {
const manager = offlineManager()
manager.setFallbackUserAgent('Windows')
diff --git a/src/app/editor/fonts/index.ts b/src/app/editor/fonts/index.ts
index e7538c22e..41ff37c09 100644
--- a/src/app/editor/fonts/index.ts
+++ b/src/app/editor/fonts/index.ts
@@ -89,8 +89,10 @@ async function getTauriFonts(): Promise<HostFontFamily[]> {
export function preloadFonts(): void {
configureTauriFontCache()
+ // The installed families are listed ahead for the font picker. The page draws them by name
+ // already; a face registered under a family's name would stand in for all of its faces.
if (isTauri()) {
- void getTauriFonts().then(registerFontFaces)
+ void getTauriFonts()
return
}
if (onlineFontsEnabled.value) fontManager.preloadWebFontFamilies()
@@ -122,14 +124,6 @@ export async function predownloadFallbackFonts() {
return fontManager.ensureFallbackPack()
}
-function registerFontFaces(fonts: HostFontFamily[]): void {
- if (typeof document === 'undefined') return
- for (const { family } of fonts) {
- const face = new FontFace(family, `local("${family}")`)
- document.fonts.add(face)
- }
-}
-
export async function listFamilies(): Promise<FontFamilyOption[]> {
configureTauriFontCache()
if (isTauri()) {