openpencil/tests/engine/app/font-loading.test.ts

63 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

import { describe, expect, test } from 'bun:test'
import {
collectGraphFontRequirements,
fontManager,
missingGraphFontScripts,
type FontFallbackScript
} from '@open-pencil/core/text'
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', () => {
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'
])
})
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)
expect(requestedScripts).toEqual(['cjk-sc'])
expect(expectDefined(graph.getNode(text.id), 'text node').textPicture).toBeNull()
} finally {
fontManager.ensureFallbackPack = originalEnsureFallbackPack
}
})
})