/** * The editor's layout puts the text where the painter draws it: a CSS line box of * `lineHeight`, inside `padding`, centred for one line and from the top for several, gives the * painter's baselines, and a comb's even spacing gives a fixed-width face's cells. */ import { lastResortPaintFont, standardPaintFont, type PaintFont, type PaintFonts, } from '@embedpdf/core-text'; import type { FormField, StandardFont, WidgetAnnotation } from '@embedpdf/engine-core/runtime'; import { describe, expect, it } from 'vitest'; import { textEditorLayout, widgetDrawing, widgetText, type DrawOp, type TextRun } from '../src'; const widget = (patch: Partial = {}): WidgetAnnotation => ({ subtype: 'widget', rect: { x: 10, y: 20, width: 120, height: 24 }, box: { x: 10, y: 20, width: 120, height: 24 }, rotation: null, appearanceState: null, color: '#000000', interiorColor: null, strokeWidth: 1, borderStyle: 'solid', dashArray: null, fontFamily: 'helvetica', fontSize: 10, fontColor: null, textAlign: 'left', caption: null, checkStyle: null, ...patch, }) as WidgetAnnotation; const textField = (patch: Partial = {}): FormField => ({ kind: 'text', value: '', multiline: false, password: false, comb: false, maxLength: null, ...patch, }) as FormField; /** The painter's text: its size and runs. */ function paintedText(row: WidgetAnnotation, field: FormField) { const walk = (ops: readonly DrawOp[]): Extract[] => ops.flatMap((op) => (op.kind === 'text' ? [op] : op.kind === 'clip' ? walk(op.ops) : [])); const [text] = walk(widgetDrawing(row, field)!.ops); return text!; } /** Where the CSS line box puts the first baseline, from the box's top. */ function cssBaseline(row: WidgetAnnotation, field: FormField, text: string): number { const layout = textEditorLayout(row, field, text)!; const ascent = (standardPaintFont(row.fontFamily as StandardFont).ascent * layout.fontSize) / 1000; const content = row.box!.height - layout.padding.top - layout.padding.bottom; const multiline = field.kind === 'text' && field.multiline; const lineTop = layout.padding.top + (multiline ? 0 : (content - layout.lineHeight) / 2); return lineTop + ascent; } describe('textEditorLayout', () => { it('centres one line where the painter draws it, at the size it draws', () => { for (const [fontFamily, fontSize, borderStyle] of [ ['helvetica', 10, 'solid'], ['times-roman', 14, 'beveled'], ['courier-bold', 0, 'inset'], ] as const) { const row = widget({ fontFamily, fontSize, borderStyle }); const field = textField({ value: 'Ada Lovelace' }); const painted = paintedText(row, field); const layout = textEditorLayout(row, field, 'Ada Lovelace')!; expect(layout.fontSize).toBeCloseTo(painted.size, 5); const run = painted.runs[0] as TextRun; expect(layout.padding.left).toBeCloseTo(run.x, 2); expect(cssBaseline(row, field, 'Ada Lovelace')).toBeCloseTo(row.box!.height - run.y, 2); } }); it('starts several lines at the top, a line apart', () => { const row = widget({ box: { x: 10, y: 20, width: 120, height: 80 }, fontSize: 9 }); const field = textField({ value: 'one\ntwo', multiline: true }); const runs = paintedText(row, field).runs; const layout = textEditorLayout(row, field, 'one\ntwo')!; expect(cssBaseline(row, field, 'one\ntwo')).toBeCloseTo(80 - runs[0]!.y, 2); expect(runs[0]!.y - runs[1]!.y).toBeCloseTo(layout.lineHeight, 3); }); it('fits the size to the text being typed', () => { const row = widget({ fontSize: 0 }); const short = textEditorLayout(row, textField(), 'Ada')!; const long = textEditorLayout(row, textField(), 'Ada Lovelace, Countess of Lovelace')!; expect(long.fontSize).toBeLessThan(short.fontSize); }); it('spaces a comb by its cells', () => { const row = widget({ fontFamily: 'courier', fontSize: 12 }); const field = textField({ value: '1234', comb: true, maxLength: 6 }); const runs = paintedText(row, field).runs; const layout = textEditorLayout(row, field, '1234')!; expect(layout.comb).toBe(true); const advance = 600 * 0.012; runs.forEach((run, index) => expect(layout.padding.left + index * (advance + layout.letterSpacing)).toBeCloseTo(run.x, 2), ); // Empty, it says where the first character will go. expect( textEditorLayout(row, textField({ comb: true, maxLength: 6 }), '')!.padding.left, ).toBeCloseTo(runs[0]!.x, 2); }); it('reads the line in the direction the engine draws it: its first strong character', () => { const rtl = (text: string) => textEditorLayout(widget(), textField(), text)!.rtl; expect(rtl('שלום test 123')).toBe(true); expect(rtl('test 123 שלום')).toBe(false); expect(rtl('123 שלום')).toBe(true); expect(rtl('')).toBe(false); }); it('is only for text fields', () => { expect(textEditorLayout(widget(), { kind: 'checkbox' } as FormField, '')).toBeNull(); }); it('names the programs its text is drawn in: the face, the fallbacks it needs, the last resort', () => { const fallback = (name: string, first: number, last: number): PaintFont => ({ key: name, ascent: 900, descent: -200, standard: false, covers: (codePoint) => codePoint >= first && codePoint <= last, width: (text) => [...text].length * 600, fontFamily: `"epdf-${name}"`, fontWeight: 400, fontStyle: 'normal', program: { hash: name, family: `epdf-${name}`, weight: 400, style: 'normal', load: async () => new Uint8Array(), }, }); const fonts: PaintFonts = { face: standardPaintFont('helvetica'), fallbacks: [fallback('arabic', 0x0600, 0x06ff), fallback('hebrew', 0x0590, 0x05ff)], lastResort: lastResortPaintFont(), }; const families = (text: string) => textEditorLayout(widget(), textField(), text, fonts)!.programs.map( (program) => program.family, ); const face = standardPaintFont('helvetica').program.family; const lastResort = lastResortPaintFont().program.family; expect(families('Hello')).toEqual([face, lastResort]); expect(families('Hello שלום')).toEqual([face, 'epdf-hebrew', lastResort]); const layout = textEditorLayout(widget(), textField(), 'Hello שלום', fonts)!; expect(layout.fontFamily).toBe( `"${face}", "epdf-hebrew", "${lastResort}", Helvetica, Arial, sans-serif`, ); }); }); describe('widgetText', () => { it('is every character the painter draws for the widget', () => { expect(widgetText(widget(), textField({ value: 'שלום' }))).toBe('שלום'); expect(widgetText(widget(), textField({ value: '1234.5' }), { text: '$1,234.50' })).toBe( '$1,234.50', ); expect(widgetText(widget(), textField({ value: 'secret', password: true }))).toBe('*'); expect(widgetText(widget(), textField({ password: true }))).toBe(''); expect( widgetText(widget(), { kind: 'listbox', options: [ { label: 'One', selected: false }, { label: 'שתיים', selected: true }, ], topIndex: 0, } as unknown as FormField), ).toBe('One\nשתיים'); expect(widgetText(widget({ caption: 'שלח' }), { kind: 'pushbutton' } as FormField)).toBe('שלח'); expect(widgetText(widget(), { kind: 'checkbox' } as FormField)).toBe(''); }); });