Skip to content

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

fix(core): reuse native font buffers across fallback preparation (#1442)

simiooo2831a0cb0404
diff --git a/changes/1442/fixed.md b/changes/1442/fixed.md
new file mode 100644
index 000000000..1fe2590d7
--- /dev/null
+++ b/changes/1442/fixed.md
@@ -0,0 +1 @@
+- CJK text in the desktop app and with local fonts in the browser no longer runs CanvasKit out of memory after repeated edits, saves, and exports: system fallback fonts are read once and registered once, instead of on every preparation.
diff --git a/packages/core/src/text/fonts.ts b/packages/core/src/text/fonts.ts
index 83ba7cf74..2d7656516 100644
--- a/packages/core/src/text/fonts.ts
+++ b/packages/core/src/text/fonts.ts
@@ -75,6 +75,31 @@ export type BundledFontLocator = (file: string) => string
const defaultBundledFontLocator: BundledFontLocator = (file) => `/${file}`
+/**
+ * Shares the in-flight and successful reads of a key, and forgets a null answer so it can be
+ * retried. A host or browser read copies the whole face again each time, tens of megabytes for a
+ * system CJK face, only for registration to find the same bytes and drop it.
+ */
+class SharedReads {
+ private readonly reads = new Map<string, Promise<ArrayBuffer | null>>()
+
+ read(key: string, load: () => Promise<ArrayBuffer | null>): Promise<ArrayBuffer | null> {
+ const existing = this.reads.get(key)
+ if (existing) return existing
+ const pending = load()
+ this.reads.set(key, pending)
+ void pending.then((data) => {
+ if (!data && this.reads.get(key) === pending) this.reads.delete(key)
+ return undefined
+ })
+ return pending
+ }
+
+ clear(): void {
+ this.reads.clear()
+ }
+}
+
/** Whether two font files hold the same bytes, as one downloaded or read again does. */
function sameFontFile(a: ArrayBuffer, b: ArrayBuffer): boolean {
if (a === b) return true
@@ -119,6 +144,7 @@ export class FontManager {
private registrationGeneration = 0
private providerRegistrations = new WeakMap<TypefaceFontProvider, Map<string, Set<ArrayBuffer>>>()
private localFonts: FontInfo[] | null = null
+ private readonly localFontRequests = new SharedReads()
private hostStyles = new Map<string, readonly string[]>()
private familyFaceLoads = new Map<string, Promise<FamilyFace[] | null>>()
private knownFamilyFaces = new Map<string, FamilyFace[] | null>()
@@ -127,6 +153,7 @@ export class FontManager {
private downloadedFontCache: DownloadedFontCache | null = null
private fallbackUserAgent: string | undefined
private hostFontLoader: HostFontLoader | null = null
+ private readonly hostFontRequests = new SharedReads()
private webFonts = new WebFontResolver()
private cjkFallbackFamilies: string[] = []
private cjkFallbackPromise: Promise<string[]> | null = null
@@ -206,6 +233,7 @@ export class FontManager {
setHostFontLoader(loader: HostFontLoader | null): void {
this.hostFontLoader = loader
+ this.hostFontRequests.clear()
}
/** Lets a host that bundles `@open-pencil/core/assets` itself say where each file ended up. */
@@ -776,6 +804,7 @@ export class FontManager {
signal?.throwIfAborted()
const buffer =
(await this.loadHostFont(family, 'Regular')) ?? (await this.findLocalFont(family))
+ signal?.throwIfAborted()
if (
buffer &&
this.registerAndCache(family, 'Regular', buffer, 'fallback') &&
@@ -808,14 +837,29 @@ export class FontManager {
return targetFamilies
}
- private async loadHostFont(family: string, style: string): Promise<ArrayBuffer | null> {
- if (!this.hostFontLoader) return null
+ private loadHostFont(family: string, style: string): Promise<ArrayBuffer | null> {
+ const loader = this.hostFontLoader
+ if (!loader) return Promise.resolve(null)
+ return this.hostFontRequests.read(`${family}|${style}`, () =>
+ this.readHostFont(loader, family, style)
+ )
+ }
+
+ private async readHostFont(
+ loader: HostFontLoader,
+ family: string,
+ style: string
+ ): Promise<ArrayBuffer | null> {
const key = `${family}|${style}`
try {
- const data = await this.hostFontLoader(family, style)
+ const data = await loader(family, style)
+ // A read that started under a loader the host has replaced must not register its bytes.
+ if (this.hostFontLoader !== loader) return null
if (data) this.unavailableFaces.delete(key)
return data
} catch (e) {
+ // A failure from a replaced loader says nothing about the face the new loader serves.
+ if (this.hostFontLoader !== loader) return null
if (e instanceof UnsupportedFontFormatError) {
this.unavailableFaces.set(key, e.reason)
return null
@@ -853,11 +897,19 @@ export class FontManager {
}
}
- private async findLocalFont(family: string, style?: string): Promise<ArrayBuffer | null> {
- if (!IS_BROWSER || !window.queryLocalFonts) return null
- if (this.localFontAccessState !== 'granted') return null
+ private findLocalFont(family: string, style?: string): Promise<ArrayBuffer | null> {
+ if (!IS_BROWSER || !window.queryLocalFonts) return Promise.resolve(null)
+ if (this.localFontAccessState !== 'granted') return Promise.resolve(null)
+ // A query answers with a new blob, and so new bytes, every time; the face decides the match.
+ return this.localFontRequests.read(`${family}|${style ?? ''}`, () =>
+ this.readLocalFont(family, style)
+ )
+ }
+
+ private async readLocalFont(family: string, style?: string): Promise<ArrayBuffer | null> {
try {
- const fonts = await window.queryLocalFonts()
+ const fonts = await window.queryLocalFonts?.()
+ if (!fonts) return null
const match = chooseLocalFontMatch(fonts, family, style)
if (!match) return null
const blob: Blob = await match.blob()
diff --git a/packages/core/tests/text/fonts.test.ts b/packages/core/tests/text/fonts.test.ts
index 43aff06c1..ec0718349 100644
--- a/packages/core/tests/text/fonts.test.ts
+++ b/packages/core/tests/text/fonts.test.ts
@@ -3,6 +3,8 @@ import { join } from 'node:path'
import { FIXTURES } from '#core-tests/helpers/paths'
+import type { CanvasKit, TypefaceFontProvider } from 'canvaskit-wasm'
+
import { FontManager } from '@open-pencil/core/text'
/** A font manager that asks no web provider, so a family's faces come from its catalog. */
@@ -12,6 +14,17 @@ function offlineManager() {
return manager
}
+/** Records what it registers, so a second registration of a face's bytes shows up. */
+function countingProvider() {
+ const registrations = new Map<string, number>()
+ const provider = {
+ registerFont(_data: ArrayBuffer, family: string) {
+ registrations.set(family, (registrations.get(family) ?? 0) + 1)
+ }
+ } as TypefaceFontProvider
+ return { provider, registrations }
+}
+
test('a face lookup that outlives a new catalog does not overwrite what the catalog says', async () => {
const manager = offlineManager()
const before = manager.familyFaces('Oswald')
@@ -34,6 +47,92 @@ test('a new catalog tells listeners to ask for their families again', () => {
expect(resets).toBe(1)
})
+test('repeated and concurrent fallback preparation reuses host font bytes', async () => {
+ const manager = offlineManager()
+ manager.setFallbackUserAgent('Windows')
+ const reads = new Map<string, number>()
+ manager.setHostFontLoader(async (family, style) => {
+ const key = `${family}|${style}`
+ reads.set(key, (reads.get(key) ?? 0) + 1)
+ return new ArrayBuffer(16)
+ })
+ const prepare = () => manager.ensureFallbackPack(['cjk-sc'], '', new AbortController().signal)
+ await Promise.all([prepare(), prepare(), prepare()])
+ const first = manager.loadedData('Microsoft YaHei', 'Regular')
+ for (let i = 0; i < 10; i++) await prepare()
+ expect(first).not.toBeNull()
+ expect(manager.loadedData('Microsoft YaHei', 'Regular')).toBe(first)
+ expect(reads.get('Microsoft YaHei|Regular')).toBe(1)
+ expect([...reads.values()].every((count) => count === 1)).toBe(true)
+})
+
+test('an unavailable host font can be retried and replacing its loader invalidates cached bytes', async () => {
+ const manager = offlineManager()
+ let reads = 0
+ const data = new ArrayBuffer(16)
+ manager.setHostFontLoader(async () => (++reads === 1 ? null : data))
+ await expect(manager.loadFont('Host Retry', 'Regular')).resolves.toBeNull()
+ await expect(manager.loadFont('Host Retry', 'Regular')).resolves.toBe(data)
+ manager.setFallbackUserAgent('Windows')
+ await manager.ensureFallbackPack(['cjk-sc'], '', new AbortController().signal)
+ // Other bytes than the first loader's, which would otherwise be kept as the same file.
+ const next = new Uint8Array(16).fill(1).buffer
+ manager.setHostFontLoader(async () => next)
+ await manager.ensureFallbackPack(['cjk-sc'], '', new AbortController().signal)
+ expect(manager.loadedData('Microsoft YaHei', 'Regular')).toBe(next)
+})
+
+test('cancelled fallback preparation does not register a late host font', async () => {
+ const manager = offlineManager()
+ manager.setFallbackUserAgent('Windows')
+ const data = Promise.withResolvers<ArrayBuffer | null>()
+ manager.setHostFontLoader(() => data.promise)
+ const controller = new AbortController()
+ const pending = manager.ensureFallbackPack(['cjk-sc'], '', controller.signal)
+ controller.abort()
+ data.resolve(new ArrayBuffer(16))
+ await expect(pending).rejects.toHaveProperty('name', 'AbortError')
+ expect(manager.loadedData('Microsoft YaHei', 'Regular')).toBeNull()
+})
+
+test('macOS fallback preparation after a document edit reads and registers each face once', async () => {
+ const manager = offlineManager()
+ manager.setFallbackUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0)')
+ const { provider, registrations } = countingProvider()
+ manager.attachProvider({} as CanvasKit, provider)
+ const reads = new Map<string, number>()
+ manager.setHostFontLoader(async (family, style) => {
+ const key = `${family}|${style}`
+ reads.set(key, (reads.get(key) ?? 0) + 1)
+ return new ArrayBuffer(16)
+ })
+
+ // What the editor runs after every AI or MCP edit, with the characters its text needs.
+ for (let i = 0; i < 5; i++) await manager.ensureFallbackPack(['cjk-tc'], '設計規範')
+
+ expect(reads.size).toBeGreaterThan(0)
+ expect([...reads.values()].every((count) => count === 1)).toBe(true)
+ expect(registrations.size).toBe(reads.size)
+ expect([...registrations.values()].every((count) => count === 1)).toBe(true)
+})
+
+test('a host read that outlives its loader never registers the replaced loader data', async () => {
+ const manager = offlineManager()
+ manager.setFallbackUserAgent('Windows')
+ const previous = Promise.withResolvers<ArrayBuffer | null>()
+ manager.setHostFontLoader(() => previous.promise)
+ const preparing = manager.ensureFallbackPack(['cjk-sc'], '', new AbortController().signal)
+ const replacement = new ArrayBuffer(16)
+ manager.setHostFontLoader(async () => replacement)
+ previous.resolve(new ArrayBuffer(16))
+ await preparing
+
+ expect(manager.loadedData('Microsoft YaHei', 'Regular')).toBeNull()
+
+ await manager.ensureFallbackPack(['cjk-sc'], '', new AbortController().signal)
+ expect(manager.loadedData('Microsoft YaHei', 'Regular')).toBe(replacement)
+})
+
test('draws every weight its variable font covers from the one file, in the same slant', async () => {
const manager = offlineManager()
const variable = await Bun.file(join(FIXTURES, 'fonts', 'InterVariable-Latin.ttf')).arrayBuffer()