Skip to content

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

fix(core): draw a style from its own file, not a file it replaced (#1618)

Dzulfikar Ats Tsauri3aa66bb23289
diff --git a/changes/1618/fixed.md b/changes/1618/fixed.md
new file mode 100644
index 000000000..b14f54213
--- /dev/null
+++ b/changes/1618/fixed.md
@@ -0,0 +1 @@
+- Text in a font whose file was replaced, such as a web font fetched again for more characters, saves to `.fig` files with its own letters and weight instead of reopening with other letters.
diff --git a/packages/core/src/text/font/manager.ts b/packages/core/src/text/font/manager.ts
index d5ca84c94..631bdfe2b 100644
--- a/packages/core/src/text/font/manager.ts
+++ b/packages/core/src/text/font/manager.ts
@@ -54,6 +54,12 @@ export class FontManager {
* first file it has for a style, so these draw from each file's own name.
*/
private replacedFamilies = new Set<string>()
+ /**
+ * The files each style's own file replaced, by `family|style`. They stay its fallbacks under
+ * names of their own, never beside the file that replaced them, where CanvasKit would pick
+ * between the two by the weight each declares.
+ */
+ private replacedFileData = new Map<string, Set<ArrayBuffer>>()
private remoteCoverage = new Map<string, Set<string>>()
private instanceVariations = new WeakMap<ArrayBuffer, Map<string, FontVariation[] | null>>()
private variations = new WeakMap<
@@ -517,15 +523,19 @@ export class FontManager {
* one, CanvasKit would pick among them by the weight each file declares, which can tie; each
* style then draws from the file loaded for it, registered with its shards under a name of the
* file's own. So does a family a style's file was replaced in, whose old file CanvasKit would
- * keep drawing under the family's name.
+ * keep drawing under the family's name. Only the style's coverage shards join its file under
+ * that name: a file it replaced, such as a static face cached before a variable one was
+ * fetched, could declare the weight asked for and be drawn instead of the file it is outlined
+ * from, so it draws only as a fallback, from `renderShardFamilies`.
*/
renderFamily(family: string, style: string): string {
const data = this.loadedData(family, style)
if (!data || (this.familyFileCount(family) < 2 && !this.replacedFamilies.has(family)))
return family
const name = this.fileFamily(family, data)
+ const replaced = this.replacedFileData.get(`${family}|${style}`)
for (const shard of this.supplementalFamilyData.get(`${family}|${style}`) ?? [])
- this.registerFontInCanvasKit(name, shard)
+ if (!replaced?.has(shard)) this.registerFontInCanvasKit(name, shard)
return name
}
@@ -743,14 +753,17 @@ export class FontManager {
}
}
- private registerSupplemental(family: string, style: string, buffer: ArrayBuffer): void {
+ /** 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 {
const key = `${family}|${style}`
const supplemental = this.supplementalFamilyData.get(key) ?? []
- if (supplemental.some((file) => sameFontFile(file, buffer))) return
+ const kept = supplemental.find((file) => sameFontFile(file, buffer))
+ if (kept) return kept
supplemental.push(buffer)
this.supplementalFamilyData.set(key, supplemental)
this.registerFontInCanvasKit(family, buffer)
this.registerFontInBrowser(family, style, buffer)
+ return buffer
}
private registerAndCache(
@@ -767,7 +780,9 @@ export class FontManager {
return existing
}
if (existing) {
- this.registerSupplemental(family, style, existing)
+ const replaced = this.replacedFileData.get(key) ?? new Set<ArrayBuffer>()
+ replaced.add(this.registerSupplemental(family, style, existing))
+ this.replacedFileData.set(key, replaced)
this.replacedFamilies.add(family)
}
this.loadedFamilies.set(key, buffer)
diff --git a/packages/core/tests/canvas/text/shape.test.ts b/packages/core/tests/canvas/text/shape.test.ts
index f1744ecf8..e0c0b7f49 100644
--- a/packages/core/tests/canvas/text/shape.test.ts
+++ b/packages/core/tests/canvas/text/shape.test.ts
@@ -13,8 +13,8 @@ import { pixelLength } from '@open-pencil/scene-graph/text'
import { withFigExportRuntime, withTextShaper } from '#core/canvas/text/shape'
import { getCanvasKit } from '#core/canvaskit'
import { EMOJI_METRICS_FAMILY } from '#core/text/emoji'
+import { getGlyphOutlineMetricsSync, glyphOutlineSourceSync } from '#core/text/font/glyphs'
import { fontManager } from '#core/text/font/manager'
-import { getGlyphOutlineMetricsSync } from '#core/text/font/glyphs'
const testFont = (name: string) => join(FIXTURES, 'fonts', name)
@@ -372,6 +372,35 @@ describe('text shaping for saved glyphs', () => {
}
expect(width(bold)).toBeGreaterThan(width(regular) * 1.2)
})
+
+ /**
+ * A style first drawn from a static file, such as a web font cached before variable families
+ * were fetched whole, and then from a variable file fetched for more characters. The static
+ * file declares the style's weight and the variable file its default, so the text must still
+ * be drawn, and outlined, from the file that replaced the static one.
+ */
+ test('outlines a style from the file that replaced its first, whatever weight each declares', async () => {
+ const family = 'Shape Test Replaced Static'
+ const semibold = expectDefined(await fontManager.fetchBundledFont('/Inter-SemiBold.ttf'))
+ const variable = expectDefined(await fontManager.fetchBundledFont('/InterVariable.ttf'))
+ await fontManager.markLoaded(family, 'SemiBold', semibold.slice(0))
+ await fontManager.markLoaded(family, 'SemiBold', variable.slice(0))
+ const text = 'Bat'
+ const shaped = expectDefined(
+ await shape({ text, fontFamily: family, fontWeight: 600 }),
+ 'shaped text'
+ )
+ const replacement = expectDefined(
+ glyphOutlineSourceSync(family, 'SemiBold', [{ axis: 'wght', value: 600 }]),
+ 'replacement outlines'
+ )
+
+ // The files number `B` and `a` alike but not `t`, which the static file would turn into
+ // another letter.
+ expect(shaped.glyphs.map((glyph) => glyph.commands)).toEqual(
+ Array.from(text, (character) => replacement.outline(replacement.glyphOf(character), 13))
+ )
+ })
})
test('overlapping shaping calls share their provider until the last one finishes', async () => {
diff --git a/tests/engine/text/fonts/lifecycle.test.ts b/tests/engine/text/fonts/lifecycle.test.ts
index c6d7c00a9..cdff0bf1e 100644
--- a/tests/engine/text/fonts/lifecycle.test.ts
+++ b/tests/engine/text/fonts/lifecycle.test.ts
@@ -39,23 +39,28 @@ describe('font lifecycle', () => {
test('draws a style from the cumulative subset that replaced its file', async () => {
const manager = new FontManager()
- const registrations: string[] = []
+ const registrations: Array<{ data: ArrayBuffer; family: string }> = []
const provider = {
- registerFont(_data: ArrayBuffer, family: string) {
- registrations.push(family)
+ registerFont(data: ArrayBuffer, family: string) {
+ registrations.push({ data, family })
}
} as TypefaceFontProvider
manager.attachProvider({} as CanvasKit, provider)
await manager.markLoaded('Subset Font', 'Regular', new ArrayBuffer(8))
const firstGeneration = manager.generation()
- await manager.markLoaded('Subset Font', 'Regular', new ArrayBuffer(12))
+ const replacement = new ArrayBuffer(12)
+ await manager.markLoaded('Subset Font', 'Regular', replacement)
- // CanvasKit draws from the first file registered under a name, which is the replaced one.
+ // CanvasKit draws from the first file registered under a name, which is the replaced one,
+ // and picks between files under one name by the weight each declares, so the name the
+ // style draws under holds the replacement alone.
const drawn = manager.renderFamily('Subset Font', 'Regular')
expect(drawn).not.toBe('Subset Font')
expect(manager.generation()).toBeGreaterThan(firstGeneration)
- expect(registrations).toEqual(['Subset Font', 'Subset Font', drawn, drawn])
+ expect(registrations.filter(({ family }) => family === drawn).map(({ data }) => data)).toEqual([
+ replacement
+ ])
})
test('tracks nodes gated by pre-render font resolution', () => {