Files
genoffice/packages/font-metrics/tests/advance.test.ts
T
merrick-2002andGenOffice f68df70e22 Sync snapshot (2026-08-20) (#123)
- Add fully local PDF to Word, PowerPoint, and Excel conversion
- Add bring-your-own-key AI providers - Claude, OpenAI, Gemini, DeepSeek, Kimi, GLM, Qwen, Doubao, MiniMax, Grok, Mistral, OpenRouter, and custom OpenAI-compatible endpoints - with a refreshed AI settings UI
- Rework the Slides AI editing surface: canonical edit-op registry, atomic transactions with rollback, durable element ids, and batched apply_ops
- Improve Slides fidelity: chart, font, gradient, table-style, and text-shaping fixes, plus PPT-parity line editing and adjust handles
- Polish Docs: password-protection dialogs, anchored-image wrapping fixes, and selection and clipboard fixes
- Add themed dropdowns suite-wide with accent-derived selection states

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-08-21 00:33:18 +08:00

50 lines
1.9 KiB
TypeScript

import { existsSync } from 'node:fs'
import { describe, expect, it } from 'vitest'
import { advanceWidths, isFamilyInstalled } from '../src/index'
const darwin = process.platform === 'darwin'
const hasHelvetica = darwin && existsSync('/System/Library/Fonts/Helvetica.ttc')
describe('isFamilyInstalled', () => {
it('resolves exact normalized family names only', () => {
expect(isFamilyInstalled('No Such Font Family ZZZ')).toBe(false)
expect(isFamilyInstalled('')).toBe(false)
if (!hasHelvetica) return
expect(isFamilyInstalled('Helvetica')).toBe(true)
expect(isFamilyInstalled('helvetica')).toBe(true)
expect(isFamilyInstalled('Helveticaish')).toBe(false)
})
})
describe('advanceWidths', () => {
it('returns null for uninstalled families', () => {
expect(advanceWidths('No Such Font Family ZZZ', 'x', 12)).toBeNull()
})
it('returns per-codepoint twips that scale linearly with size', () => {
if (!hasHelvetica) return
const at12 = advanceWidths('Helvetica', 'Hi x', 12)!
expect(at12).toHaveLength(4)
for (const w of at12) expect(w).toBeGreaterThan(0)
const at24 = advanceWidths('Helvetica', 'Hi x', 24)!
at24.forEach((w, i) => expect(w).toBeCloseTo(2 * at12[i]!, 6))
// 'i' is narrower than 'H' in a proportional face
expect(at12[1]!).toBeLessThan(at12[0]!)
})
it('selects a heavier face for bold and reports wider stems', () => {
if (!hasHelvetica) return
const sum = (a: number[]) => a.reduce((x, y) => x + y, 0)
const reg = advanceWidths('Helvetica', 'Hamburgefonstiv', 12)!
const bold = advanceWidths('Helvetica', 'Hamburgefonstiv', 12, { bold: true })!
expect(sum(bold)).toBeGreaterThan(sum(reg))
})
it('marks unmapped codepoints as NaN instead of guessing', () => {
if (!hasHelvetica) return
const w = advanceWidths('Helvetica', '\u{10FFF0}x', 12)!
expect(Number.isNaN(w[0]!)).toBe(true)
expect(w[1]!).toBeGreaterThan(0)
})
})