2026-07-01 19:06:19 +00:00
|
|
|
import { describe, expect, test } from 'bun:test'
|
|
|
|
|
|
2026-08-12 17:09:04 +00:00
|
|
|
import {
|
|
|
|
|
collectGraphFontRequirements,
|
|
|
|
|
fontManager,
|
|
|
|
|
missingGraphFontScripts,
|
|
|
|
|
type FontFallbackScript
|
|
|
|
|
} from '@open-pencil/core/text'
|
2026-07-01 19:06:19 +00:00
|
|
|
import { SceneGraph } from '@open-pencil/scene-graph'
|
|
|
|
|
|
|
|
|
|
import { ensureGraphFonts } from '@/app/editor/fonts'
|
|
|
|
|
|
|
|
|
|
import { expectDefined } from '#tests/helpers/assert'
|
|
|
|
|
import { repoPath } from '#tests/helpers/paths'
|
|
|
|
|
|
|
|
|
|
describe('app font loading', () => {
|
2026-08-12 17:09:04 +00:00
|
|
|
test('requests platform fallback before parsing large native primary fonts', () => {
|
|
|
|
|
const graph = new SceneGraph()
|
|
|
|
|
const page = graph.getPages()[0]
|
|
|
|
|
const text = graph.createNode('TEXT', page.id, {
|
|
|
|
|
text: '현대 소나타',
|
|
|
|
|
fontFamily: 'Native Arial',
|
|
|
|
|
fontSize: 32
|
|
|
|
|
})
|
|
|
|
|
const requirements = collectGraphFontRequirements(graph, [text.id])
|
|
|
|
|
|
|
|
|
|
expect(missingGraphFontScripts(requirements, { treatUnknownCoverageAsMissing: true })).toEqual([
|
|
|
|
|
'cjk-kr'
|
|
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-01 19:06:19 +00:00
|
|
|
test('ensureGraphFonts loads fallback packs when loaded primary font misses CJK glyphs', async () => {
|
|
|
|
|
const interData = await Bun.file(repoPath('public/Inter-Regular.ttf')).arrayBuffer()
|
|
|
|
|
fontManager.markLoaded('Inter', 'Regular', interData)
|
|
|
|
|
|
|
|
|
|
const graph = new SceneGraph()
|
|
|
|
|
const page = graph.getPages()[0]
|
|
|
|
|
const text = graph.createNode('TEXT', page.id, {
|
|
|
|
|
text: '你好世界',
|
|
|
|
|
fontFamily: 'Inter',
|
|
|
|
|
fontSize: 32,
|
|
|
|
|
textPicture: new Uint8Array([1, 2, 3])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const originalEnsureFallbackPack = fontManager.ensureFallbackPack.bind(fontManager)
|
|
|
|
|
let requestedScripts: FontFallbackScript[] = []
|
|
|
|
|
fontManager.ensureFallbackPack = async (scripts = ['cjk', 'arabic']) => {
|
|
|
|
|
requestedScripts = [...scripts]
|
|
|
|
|
return { cjk: ['Regression CJK Fallback'], arabic: [] }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const changed = await ensureGraphFonts(graph, [text.id])
|
|
|
|
|
|
|
|
|
|
expect(changed).toBe(true)
|
2026-07-01 19:19:14 +00:00
|
|
|
expect(requestedScripts).toEqual(['cjk-sc'])
|
2026-07-01 19:06:19 +00:00
|
|
|
expect(expectDefined(graph.getNode(text.id), 'text node').textPicture).toBeNull()
|
|
|
|
|
} finally {
|
|
|
|
|
fontManager.ensureFallbackPack = originalEnsureFallbackPack
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|