Files
genoffice/apps/docs/tests/font-check.test.ts
T
merrick-2002andGenOffice e249c3ba81 Sync snapshot (2026-08-09) (#75)
- PDF: in-place text editing — paragraph block selection with reflow, bold/italic and alignment controls, selection-level text color, original-font preservation
- PDF: AI image tools (search/generate, insert, transform, delete) plus image rotation and in-place replacement
- Docs: picture rotation and mirror flips, in-place image byte swap for crop/background removal, and several Word-fidelity rendering fixes
- Docs: fix a renderer hang on tab-heavy forms and a crash when exporting large documents
- Slides: AI picture tools (crop, opacity, replace), picture border controls, in-place picture source swap
- Linux: AppImage and deb packaging with stable/beta auto-update channels
- Refresh the README and clean up internal design-audit artifacts

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-08-10 01:43:24 +08:00

75 lines
2.7 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { parseDocx } from '@genoffice/docx-engine'
import { buildDocx } from '../../../packages/docx-engine/tests/helpers/build-docx'
import { checkMissingFonts, collectDocFonts } from '../src/renderer/font-check'
/** fake 2d context: known families get distinct widths, unknown ones inherit the generic fallback */
function stubCanvas(availableFamilies: string[]) {
const known = new Map(availableFamilies.map((f, i) => [f, 100 + (i + 1) * 10]))
let current = 50
const fake = {
set font(spec: string) {
const family = /"([^"]+)"/.exec(spec)?.[1]
current =
family !== undefined && known.has(family)
? known.get(family)!
: spec.includes('monospace')
? 50
: 60
},
measureText: () => ({ width: current }),
}
vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue(
fake as unknown as CanvasRenderingContext2D,
)
}
afterEach(() => {
vi.restoreAllMocks()
})
describe('collectDocFonts', () => {
it('gathers distinct run fonts from the parsed model', async () => {
const doc = await parseDocx(
await buildDocx({
bodyXml:
'<w:p><w:r><w:rPr><w:rFonts w:eastAsia="仿宋_GB2312"/></w:rPr><w:t>正文</w:t></w:r>' +
'<w:r><w:rPr><w:rFonts w:eastAsia="楷体_GB2312"/></w:rPr><w:t>(承办)</w:t></w:r></w:p>' +
'<w:p><w:r><w:rPr><w:rFonts w:eastAsia="仿宋_GB2312"/></w:rPr><w:t>再来一段</w:t></w:r></w:p>',
}),
)
const fonts = collectDocFonts(doc)
expect(fonts).toContain('仿宋_GB2312')
expect(fonts).toContain('楷体_GB2312')
expect(fonts.filter((f) => f === '仿宋_GB2312')).toHaveLength(1)
})
})
describe('checkMissingFonts', () => {
it('reports a missing font with its first available substitute', () => {
stubCanvas(['STFangsong', 'STKaiti'])
// fresh module per stub: the 2d context is cached module-wide
expect(checkMissingFonts(['仿宋_GB2312'])).toEqual([
{ name: '仿宋_GB2312', substitute: 'STFangsong' },
])
})
it('reports null substitute when only the bundled fallback is left', () => {
stubCanvas([])
expect(checkMissingFonts(['楷体_GB2312'])).toEqual([{ name: '楷体_GB2312', substitute: null }])
})
it('says nothing when the declared font resolves', () => {
stubCanvas(['黑体'])
expect(checkMissingFonts(['黑体'])).toEqual([])
})
it('declared bundled subset faces still count as missing', () => {
// 'GenOffice Songti SC' (internal alias) resolves but is never reported
stubCanvas(['Noto Sans CJK SC', 'GenOffice Songti SC', 'STSong'])
expect(checkMissingFonts(['Noto Sans CJK SC'])).toEqual([
{ name: 'Noto Sans CJK SC', substitute: 'STSong' },
])
})
})