Skip to content

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

Extract collectFontKeys into core and add font tests

Danila Poyarkov9e3a6c0e7f30
diff --git a/packages/core/src/fonts.ts b/packages/core/src/fonts.ts
index bb0c7a35c..0292d51ed 100644
--- a/packages/core/src/fonts.ts
+++ b/packages/core/src/fonts.ts
@@ -1,5 +1,8 @@
import type { CanvasKit, TypefaceFontProvider } from 'canvaskit-wasm'
+import { DEFAULT_FONT_FAMILY } from './constants'
+import type { SceneGraph } from './scene-graph'
+
export interface FontInfo {
family: string
fullName: string
@@ -207,6 +210,30 @@ export function isFontLoaded(family: string): boolean {
return [...loadedFamilies.keys()].some((k) => k.startsWith(`${family}|`))
}
+export function collectFontKeys(graph: SceneGraph, nodeIds: string[]): Array<[string, string]> {
+ const fontKeys = new Set<string>()
+ const collect = (id: string) => {
+ const node = graph.getNode(id)
+ if (!node) return
+ if (node.type === 'TEXT') {
+ const family = node.fontFamily || DEFAULT_FONT_FAMILY
+ fontKeys.add(`${family}\0${weightToStyle(node.fontWeight || 400, node.italic)}`)
+ for (const run of node.styleRuns) {
+ const f = run.style.fontFamily ?? family
+ const w = run.style.fontWeight ?? node.fontWeight ?? 400
+ const i = run.style.italic ?? node.italic
+ fontKeys.add(`${f}\0${weightToStyle(w, i)}`)
+ }
+ }
+ for (const childId of node.childIds) collect(childId)
+ }
+ for (const id of nodeIds) collect(id)
+
+ return [...fontKeys]
+ .map((k) => k.split('\0') as [string, string])
+ .filter(([family]) => family !== DEFAULT_FONT_FAMILY)
+}
+
export function weightToStyle(weight: number, italic = false): string {
let label = 'Regular'
if (weight <= 100) label = 'Thin'
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index e3b8859b3..27477f142 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -52,6 +52,7 @@ export { computeLayout, computeAllLayouts, setTextMeasurer } from './layout'
export type { TextMeasurer } from './layout'
export { getCanvasKit, getGpuBackend, type CanvasKitOptions, type GpuBackend } from './canvaskit'
export {
+ collectFontKeys,
loadFont,
listFamilies,
initFontService,
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index 3a85fe8db..b2a5d9dc4 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -1,4 +1,4 @@
-import { weightToStyle } from '@open-pencil/core'
+import { collectFontKeys } from '@open-pencil/core'
import { shallowReactive, shallowRef, computed, watch } from 'vue'
import {
@@ -10,8 +10,7 @@ import {
CANVAS_BG_COLOR,
ZOOM_DIVISOR,
ZOOM_SCALE_MIN,
- ZOOM_SCALE_MAX,
- DEFAULT_FONT_FAMILY
+ ZOOM_SCALE_MAX
} from '@/constants'
import {
parseFigmaClipboard,
@@ -1698,27 +1697,7 @@ export function createEditorStore() {
}
async function loadFontsForNodes(nodeIds: string[]) {
- const fontKeys = new Set<string>()
- const collect = (id: string) => {
- const node = graph.getNode(id)
- if (!node) return
- if (node.type === 'TEXT') {
- const family = node.fontFamily || DEFAULT_FONT_FAMILY
- fontKeys.add(`${family}\0${weightToStyle(node.fontWeight || 400, node.italic)}`)
- for (const run of node.styleRuns) {
- const f = run.style.fontFamily ?? family
- const w = run.style.fontWeight ?? node.fontWeight ?? 400
- const i = run.style.italic ?? node.italic
- fontKeys.add(`${f}\0${weightToStyle(w, i)}`)
- }
- }
- for (const childId of node.childIds) collect(childId)
- }
- for (const id of nodeIds) collect(id)
-
- const toLoad = [...fontKeys]
- .map((k) => k.split('\0') as [string, string])
- .filter(([family]) => family !== DEFAULT_FONT_FAMILY)
+ const toLoad = collectFontKeys(graph, nodeIds)
if (toLoad.length === 0) return
await Promise.all(toLoad.map(([family, style]) => loadFont(family, style)))
diff --git a/tests/engine/fonts.test.ts b/tests/engine/fonts.test.ts
new file mode 100644
index 000000000..0f58a2363
--- /dev/null
+++ b/tests/engine/fonts.test.ts
@@ -0,0 +1,291 @@
+import { describe, test, expect } from 'bun:test'
+
+import {
+ collectFontKeys,
+ isFontLoaded,
+ markFontLoaded,
+ styleToWeight,
+ weightToStyle,
+} from '../../packages/core/src/fonts'
+import { SceneGraph } from '../../packages/core/src/scene-graph'
+
+function pageId(graph: SceneGraph) {
+ return graph.getPages()[0].id
+}
+
+describe('styleToWeight', () => {
+ test('maps common style names', () => {
+ expect(styleToWeight('Regular')).toBe(400)
+ expect(styleToWeight('Bold')).toBe(700)
+ expect(styleToWeight('Light')).toBe(300)
+ expect(styleToWeight('Thin')).toBe(100)
+ expect(styleToWeight('Medium')).toBe(500)
+ expect(styleToWeight('SemiBold')).toBe(600)
+ expect(styleToWeight('ExtraBold')).toBe(800)
+ expect(styleToWeight('Black')).toBe(900)
+ })
+
+ test('handles italic variants', () => {
+ expect(styleToWeight('Bold Italic')).toBe(700)
+ expect(styleToWeight('Light Italic')).toBe(300)
+ })
+
+ test('case insensitive', () => {
+ expect(styleToWeight('bold')).toBe(700)
+ expect(styleToWeight('THIN')).toBe(100)
+ expect(styleToWeight('semibold')).toBe(600)
+ })
+})
+
+describe('weightToStyle', () => {
+ test('maps weights to style names', () => {
+ expect(weightToStyle(100)).toBe('Thin')
+ expect(weightToStyle(200)).toBe('ExtraLight')
+ expect(weightToStyle(300)).toBe('Light')
+ expect(weightToStyle(400)).toBe('Medium')
+ expect(weightToStyle(500)).toBe('Medium')
+ expect(weightToStyle(600)).toBe('SemiBold')
+ expect(weightToStyle(700)).toBe('Bold')
+ expect(weightToStyle(800)).toBe('ExtraBold')
+ expect(weightToStyle(900)).toBe('Black')
+ })
+
+ test('appends Italic suffix', () => {
+ expect(weightToStyle(400, true)).toBe('Medium Italic')
+ expect(weightToStyle(700, true)).toBe('Bold Italic')
+ })
+})
+
+describe('markFontLoaded / isFontLoaded', () => {
+ test('marks and checks font', () => {
+ const family = `TestFont_${Date.now()}`
+ expect(isFontLoaded(family)).toBe(false)
+ markFontLoaded(family, 'Regular', new ArrayBuffer(8))
+ expect(isFontLoaded(family)).toBe(true)
+ })
+
+ test('different styles for same family', () => {
+ const family = `MultiStyle_${Date.now()}`
+ expect(isFontLoaded(family)).toBe(false)
+ markFontLoaded(family, 'Bold', new ArrayBuffer(8))
+ expect(isFontLoaded(family)).toBe(true)
+ })
+})
+
+describe('collectFontKeys', () => {
+ test('returns empty for non-text nodes', () => {
+ const graph = new SceneGraph()
+ const id = graph.createNode('RECTANGLE', pageId(graph), {
+ name: 'Rect',
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 100,
+ }).id
+ expect(collectFontKeys(graph, [id])).toEqual([])
+ })
+
+ test('returns empty for text nodes using default font (Inter)', () => {
+ const graph = new SceneGraph()
+ const id = graph.createNode('TEXT', pageId(graph), {
+ name: 'Label',
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 20,
+ text: 'Hello',
+ fontFamily: 'Inter',
+ fontWeight: 400,
+ }).id
+ expect(collectFontKeys(graph, [id])).toEqual([])
+ })
+
+ test('collects non-default font family', () => {
+ const graph = new SceneGraph()
+ const id = graph.createNode('TEXT', pageId(graph), {
+ name: 'Label',
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 20,
+ text: 'Hello',
+ fontFamily: 'Roboto',
+ fontWeight: 400,
+ }).id
+ const keys = collectFontKeys(graph, [id])
+ expect(keys).toEqual([['Roboto', 'Medium']])
+ })
+
+ test('collects bold weight', () => {
+ const graph = new SceneGraph()
+ const id = graph.createNode('TEXT', pageId(graph), {
+ name: 'Label',
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 20,
+ text: 'Hello',
+ fontFamily: 'Roboto',
+ fontWeight: 700,
+ }).id
+ const keys = collectFontKeys(graph, [id])
+ expect(keys).toEqual([['Roboto', 'Bold']])
+ })
+
+ test('collects italic variant', () => {
+ const graph = new SceneGraph()
+ const id = graph.createNode('TEXT', pageId(graph), {
+ name: 'Label',
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 20,
+ text: 'Hello',
+ fontFamily: 'Roboto',
+ fontWeight: 400,
+ italic: true,
+ }).id
+ const keys = collectFontKeys(graph, [id])
+ expect(keys).toEqual([['Roboto', 'Medium Italic']])
+ })
+
+ test('deduplicates same family+style', () => {
+ const graph = new SceneGraph()
+ const page = pageId(graph)
+ graph.createNode('TEXT', page, {
+ name: 'A',
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 20,
+ text: 'A',
+ fontFamily: 'Roboto',
+ fontWeight: 400,
+ })
+ graph.createNode('TEXT', page, {
+ name: 'B',
+ x: 0,
+ y: 30,
+ width: 100,
+ height: 20,
+ text: 'B',
+ fontFamily: 'Roboto',
+ fontWeight: 400,
+ })
+ const ids = graph.getChildren(page).map((n) => n.id)
+ const keys = collectFontKeys(graph, ids)
+ expect(keys).toEqual([['Roboto', 'Medium']])
+ })
+
+ test('collects multiple families', () => {
+ const graph = new SceneGraph()
+ const page = pageId(graph)
+ graph.createNode('TEXT', page, {
+ name: 'A',
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 20,
+ text: 'A',
+ fontFamily: 'Roboto',
+ fontWeight: 400,
+ })
+ graph.createNode('TEXT', page, {
+ name: 'B',
+ x: 0,
+ y: 30,
+ width: 100,
+ height: 20,
+ text: 'B',
+ fontFamily: 'Open Sans',
+ fontWeight: 700,
+ })
+ const ids = graph.getChildren(page).map((n) => n.id)
+ const keys = collectFontKeys(graph, ids)
+ expect(keys).toHaveLength(2)
+ expect(keys).toContainEqual(['Roboto', 'Medium'])
+ expect(keys).toContainEqual(['Open Sans', 'Bold'])
+ })
+
+ test('walks into nested children', () => {
+ const graph = new SceneGraph()
+ const page = pageId(graph)
+ const frame = graph.createNode('FRAME', page, {
+ name: 'Frame',
+ x: 0,
+ y: 0,
+ width: 400,
+ height: 400,
+ }).id
+ graph.createNode('TEXT', frame, {
+ name: 'Nested',
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 20,
+ text: 'Hello',
+ fontFamily: 'Poppins',
+ fontWeight: 600,
+ })
+ const keys = collectFontKeys(graph, [frame])
+ expect(keys).toEqual([['Poppins', 'SemiBold']])
+ })
+
+ test('collects fonts from style runs', () => {
+ const graph = new SceneGraph()
+ const id = graph.createNode('TEXT', pageId(graph), {
+ name: 'Mixed',
+ x: 0,
+ y: 0,
+ width: 200,
+ height: 20,
+ text: 'Hello World',
+ fontFamily: 'Roboto',
+ fontWeight: 400,
+ styleRuns: [
+ {
+ start: 0,
+ end: 5,
+ style: { fontFamily: 'Roboto', fontWeight: 400 },
+ },
+ {
+ start: 6,
+ end: 11,
+ style: { fontFamily: 'Montserrat', fontWeight: 700 },
+ },
+ ],
+ }).id
+ const keys = collectFontKeys(graph, [id])
+ expect(keys).toHaveLength(2)
+ expect(keys).toContainEqual(['Roboto', 'Medium'])
+ expect(keys).toContainEqual(['Montserrat', 'Bold'])
+ })
+
+ test('style run inherits node font when not specified', () => {
+ const graph = new SceneGraph()
+ const id = graph.createNode('TEXT', pageId(graph), {
+ name: 'Partial',
+ x: 0,
+ y: 0,
+ width: 200,
+ height: 20,
+ text: 'Hello',
+ fontFamily: 'Lato',
+ fontWeight: 300,
+ styleRuns: [
+ {
+ start: 0,
+ end: 5,
+ style: {},
+ },
+ ],
+ }).id
+ const keys = collectFontKeys(graph, [id])
+ expect(keys).toEqual([['Lato', 'Light']])
+ })
+
+ test('skips invalid node IDs', () => {
+ const graph = new SceneGraph()
+ expect(collectFontKeys(graph, ['nonexistent'])).toEqual([])
+ })
+})