Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(mcp-host): lay out PDF text by face, direction and measured width

marcotebffdc29cff12a
diff --git a/mcp-host/src/workflow/__tests__/pdfBidi.test.ts b/mcp-host/src/workflow/__tests__/pdfBidi.test.ts
new file mode 100644
index 000000000..44d0a283d
--- /dev/null
+++ b/mcp-host/src/workflow/__tests__/pdfBidi.test.ts
@@ -0,0 +1,67 @@
+import { describe, expect, it } from 'vitest'
+import { bidiClass, hasRtl, mirrored, resolveParagraph, visualOrder } from '../pdfBidi'
+
+/** `text` in display order, left to right, as characters. */
+function display(text: string): string {
+ const chars = [...text]
+ const paragraph = resolveParagraph(chars.map(c => c.codePointAt(0)!))
+ return visualOrder(paragraph, 0, chars.length)
+ .map(i => mirrored(chars[i], paragraph.levels[i]))
+ .join('')
+}
+
+describe('bidi classes', () => {
+ it('classifies the characters documents mix', () => {
+ expect(bidiClass('A'.codePointAt(0)!)).toBe('L')
+ expect(bidiClass('\u05E9'.codePointAt(0)!)).toBe('R')
+ expect(bidiClass('\u0639'.codePointAt(0)!)).toBe('AL')
+ expect(bidiClass('7'.codePointAt(0)!)).toBe('EN')
+ expect(bidiClass('\u0663'.codePointAt(0)!)).toBe('AN')
+ expect(bidiClass('\u064E'.codePointAt(0)!)).toBe('NSM')
+ expect(bidiClass('\u066A'.codePointAt(0)!)).toBe('ET')
+ expect(bidiClass('\u060C'.codePointAt(0)!)).toBe('CS')
+ expect(bidiClass(' '.codePointAt(0)!)).toBe('WS')
+ })
+
+ it('only flags text a right-to-left paragraph would reorder', () => {
+ expect(hasRtl('Revenue 2026 ≥ 5')).toBe(false)
+ expect(hasRtl('Revenue \u0645\u0631\u062D\u0628\u0627')).toBe(true)
+ })
+})
+
+describe('display order', () => {
+ it('reverses a Hebrew paragraph word by word and keeps numbers left to right', () => {
+ // "price 100" in Hebrew reads right to left, so the number ends up at the left.
+ expect(display('\u05DE\u05D7\u05D9\u05E8 100')).toBe('100 \u05E8\u05D9\u05D7\u05DE')
+ })
+
+ it('keeps Latin text in order inside a right-to-left paragraph', () => {
+ const para = resolveParagraph([...'\u05D0 PDF \u05D1'].map(c => c.codePointAt(0)!))
+ expect(para.base).toBe(1)
+ expect(display('\u05D0 PDF \u05D1')).toBe('\u05D1 PDF \u05D0')
+ })
+
+ it('puts an Arabic phrase inside an English paragraph in right-to-left order', () => {
+ expect(display('A \u0628\u062C C')).toBe('A \u062C\u0628 C')
+ })
+
+ it('treats digits after Arabic letters as Arabic numbers', () => {
+ const chars = [...'\u0628 12']
+ const para = resolveParagraph(chars.map(c => c.codePointAt(0)!))
+ expect(para.levels).toEqual([1, 1, 2, 2])
+ expect(display('\u0628 12')).toBe('12 \u0628')
+ })
+
+ it('mirrors brackets at a right-to-left level', () => {
+ expect(display('\u05D0 (\u05D1)')).toBe('(\u05D1) \u05D0')
+ })
+
+ it('keeps a bracket pair with the right-to-left text it encloses', () => {
+ // An Arabic phrase ending in "(increase 15%)" inside an English paragraph.
+ expect(display('Mixed: \u0628 (\u062C 15%)')).toBe('Mixed: (%15 \u062C) \u0628')
+ })
+
+ it('leaves left-to-right text untouched', () => {
+ expect(display('Plain text (100%)')).toBe('Plain text (100%)')
+ })
+})
diff --git a/mcp-host/src/workflow/__tests__/pdfTables.test.ts b/mcp-host/src/workflow/__tests__/pdfTables.test.ts
new file mode 100644
index 000000000..f00a919b0
--- /dev/null
+++ b/mcp-host/src/workflow/__tests__/pdfTables.test.ts
@@ -0,0 +1,179 @@
+import { describe, expect, it } from 'vitest'
+import { LANDSCAPE, PORTRAIT, type TableLayoutInput, layoutPdfTable } from '../pdfTables'
+
+/** Half an em per character, bold a little wider. */
+const measure = (text: string, bold: boolean) => text.length * (bold ? 0.55 : 0.5)
+
+function input(overrides: Partial<TableLayoutInput>): TableLayoutInput {
+ return {
+ headers: ['A', 'B'],
+ rows: [],
+ cellPadding: 8,
+ ruleWidth: 0,
+ allowLandscape: true,
+ label: 'tables[0]',
+ ...overrides,
+ }
+}
+
+const room = (page: { width: number }, columns: number) => page.width - columns * 8
+
+describe('PDF table layout', () => {
+ it('fills the printable width, giving wider content more of it', () => {
+ const fit = layoutPdfTable(
+ input({ rows: [['short', 'a much longer cell of text']] }),
+ measure,
+ []
+ )
+ expect(fit.widths.reduce((a, b) => a + b, 0)).toBeCloseTo(room(PORTRAIT, 2), 5)
+ expect(fit.widths[1]).toBeGreaterThan(fit.widths[0])
+ expect(fit.landscape).toBe(false)
+ expect(fit.fontSize).toBe(11)
+ })
+
+ it('never makes a column narrower than its longest ordinary word', () => {
+ const fit = layoutPdfTable(
+ input({ headers: ['Key', 'Notes'], rows: [['Identifier', 'word '.repeat(200)]] }),
+ measure,
+ []
+ )
+ expect(fit.widths[0]).toBeGreaterThanOrEqual(measure('Identifier', false) * 11)
+ })
+
+ it('lets a URL break inside its cell instead of widening the table', () => {
+ const url = `https://example.com/${'x'.repeat(400)}`
+ const fit = layoutPdfTable(
+ input({ headers: ['A', 'B', 'C'], rows: [['a', url, 'c']] }),
+ measure,
+ []
+ )
+ expect(fit.widths.reduce((a, b) => a + b, 0)).toBeLessThanOrEqual(room(PORTRAIT, 3) + 0.01)
+ expect(fit.landscape).toBe(false)
+ })
+
+ it('shrinks the type, then turns the page, for many columns', () => {
+ const headers = Array.from({ length: 13 }, (_, i) => `Column${i}`)
+ const rows = [headers.map(() => '$1,234,567.00')]
+ const warnings: string[] = []
+ const fit = layoutPdfTable(input({ headers, rows }), measure, warnings)
+ expect(fit.landscape).toBe(true)
+ expect(fit.widths.reduce((a, b) => a + b, 0)).toBeLessThanOrEqual(room(LANDSCAPE, 13) + 0.01)
+ expect(warnings[0]).toMatch(/tables\[0\] has more columns than a portrait page/)
+ })
+
+ it('stays portrait when landscape is not allowed', () => {
+ const headers = Array.from({ length: 13 }, (_, i) => `Column${i}`)
+ const fit = layoutPdfTable(
+ input({ headers, rows: [headers.map(() => '$1,234,567.00')], allowLandscape: false }),
+ measure,
+ []
+ )
+ expect(fit.landscape).toBe(false)
+ expect(fit.widths.reduce((a, b) => a + b, 0)).toBeLessThanOrEqual(room(PORTRAIT, 13) + 0.01)
+ })
+
+ it('lets rows break when one is taller than a page', () => {
+ const short = layoutPdfTable(input({ rows: [['a', 'b']] }), measure, [])
+ expect(short.dontBreakRows).toBe(true)
+ expect(short.keepWithHeaderRows).toBe(1)
+ const tall = layoutPdfTable(input({ rows: [['a', 'word '.repeat(3000)]] }), measure, [])
+ expect(tall.dontBreakRows).toBe(false)
+ expect(tall.keepWithHeaderRows).toBe(0)
+ })
+
+ it('leaves less room for rows when the footer grows the bottom margin', () => {
+ // About 420pt tall with its header: within 80% of the usual page, not of one with a 260pt margin.
+ const rows = [['a', 'word '.repeat(500)]]
+ expect(layoutPdfTable(input({ rows }), measure, []).dontBreakRows).toBe(true)
+ expect(layoutPdfTable(input({ rows, bottomMargin: 260 }), measure, []).dontBreakRows).toBe(
+ false
+ )
+ })
+
+ it('says which columns break words when no page and size can hold them', () => {
+ const headers = Array.from({ length: 20 }, (_, i) => `Column${i}`)
+ const warnings: string[] = []
+ layoutPdfTable(input({ headers, rows: [headers.map(() => '1,234,567.00')] }), measure, warnings)
+ expect(warnings.join(' ')).toMatch(
+ /tables\[0\]: words in 'Column0', 'Column1', 'Column2' and \d+ more column\(s\) are wider than those columns.*split the table/
+ )
+ })
+
+ it('uses the widths asked for: points, percentages, auto and star', () => {
+ const fit = layoutPdfTable(
+ input({
+ headers: ['A', 'B', 'C', 'D'],
+ rows: [['a', 'b', 'cc', 'd']],
+ requested: [60, '20%', 'auto', '*'],
+ }),
+ measure,
+ []
+ )
+ const space = room(PORTRAIT, 4)
+ expect(fit.widths[0]).toBe(60)
+ expect(fit.widths[1]).toBeCloseTo(space * 0.2, 5)
+ expect(fit.widths[2]).toBeLessThan(40)
+ expect(fit.widths.reduce((a, b) => a + b, 0)).toBeCloseTo(space, 5)
+ })
+
+ it('scales widths that add up to more than the page and says so', () => {
+ const warnings: string[] = []
+ const fit = layoutPdfTable(input({ requested: ['600', 400] }), measure, warnings)
+ expect(fit.widths.reduce((a, b) => a + b, 0)).toBeCloseTo(room(PORTRAIT, 2), 5)
+ expect(fit.widths[0] / fit.widths[1]).toBeCloseTo(1.5, 5)
+ expect(warnings[0]).toMatch(/tables\[0\]\.widths add up to 1000 pt/)
+ })
+
+ it('keeps a shared column as wide as its longest word beside wide fixed ones', () => {
+ const warnings: string[] = []
+ const fit = layoutPdfTable(
+ input({
+ headers: ['A', 'B', 'C'],
+ rows: [['a', 'b', 'WIDEEND alpha']],
+ requested: [400, 400, '*'],
+ }),
+ measure,
+ warnings
+ )
+ expect(fit.widths[2]).toBeGreaterThanOrEqual(measure('WIDEEND', false) * fit.fontSize)
+ expect(fit.widths.reduce((a, b) => a + b, 0)).toBeLessThanOrEqual(room(PORTRAIT, 3) + 0.01)
+ expect(warnings[0]).toMatch(/scaled down to fit/)
+ })
+
+ it('shrinks the type or turns the page for auto widths before breaking words', () => {
+ const headers = Array.from({ length: 14 }, (_, i) => `C${i}`)
+ const fit = layoutPdfTable(
+ input({
+ headers,
+ rows: [headers.map(() => '1,234,567.00')],
+ requested: headers.map(() => 'auto'),
+ }),
+ measure,
+ []
+ )
+ for (const w of fit.widths) {
+ expect(w).toBeGreaterThanOrEqual(measure('1,234,567.00', false) * fit.fontSize)
+ }
+ })
+
+ it('reports a fixed width too narrow for its words', () => {
+ const warnings: string[] = []
+ const fit = layoutPdfTable(
+ input({ headers: ['Key', 'B'], rows: [['Identification', 'b']], requested: [20, '*'] }),
+ measure,
+ warnings
+ )
+ expect(fit.widths[0]).toBe(20)
+ expect(warnings.join(' ')).toMatch(
+ /words in 'Key' are wider than that column.*widen it in tables\[0\]\.widths/
+ )
+ })
+
+ it('ignores widths of the wrong length or kind, with a note', () => {
+ const warnings: string[] = []
+ layoutPdfTable(input({ requested: [100] }), measure, warnings)
+ layoutPdfTable(input({ requested: ['wide', 100] }), measure, warnings)
+ expect(warnings[0]).toMatch(/has 1 entries for 2 columns/)
+ expect(warnings[1]).toMatch(/widths\[0\] \("wide"\) is not a number of points/)
+ })
+})
diff --git a/mcp-host/src/workflow/__tests__/pdfText.test.ts b/mcp-host/src/workflow/__tests__/pdfText.test.ts
new file mode 100644
index 000000000..d963d4c2c
--- /dev/null
+++ b/mcp-host/src/workflow/__tests__/pdfText.test.ts
@@ -0,0 +1,320 @@
+/**
+ * The typesetter against a made-up set of faces, so the per-character fallback
+ * and right-to-left ordering are checked on any machine, whatever fonts it has.
+ */
+import { describe, expect, it } from 'vitest'
+import type { Content } from 'pdfmake/interfaces'
+import type { PdfGlyphSource } from '../fonts'
+import { PdfTypesetter, type TypesetContext } from '../pdfText'
+
+const COVERAGE: Record<string, (cp: number) => boolean> = {
+ Body: cp => cp < 0x250 || cp === 0x2022,
+ ClerumMono: cp => cp >= 0x20 && cp <= 0x7e,
+ // Shapes, so fontkit orders its runs itself.
+ Arabic: cp => cp >= 0x600 && cp <= 0x6ff,
+ // No layout tables: its runs must arrive already in display order.
+ Hebrew: cp => cp >= 0x590 && cp <= 0x5ff,
+}
+
+const source: PdfGlyphSource = {
+ familyFor(cp, base) {
+ if (COVERAGE[base]?.(cp)) return base
+ return ['Body', 'Arabic', 'Hebrew'].find(f => COVERAGE[f](cp))
+ },
+ reversesRtl: family => family === 'Arabic',
+ measure: (text, _family, size) => [...text].length * size * 0.5,
+ metrics: family =>
+ ({
+ Body: { ascent: 0.9, lineHeight: 1.2 },
+ ClerumMono: { ascent: 0.6, lineHeight: 0.8 },
+ Arabic: { ascent: 1.4, lineHeight: 2.1 },
+ Hebrew: { ascent: 1.0, lineHeight: 1.3 },
+ })[family],
+ descriptors: () => ({}),
+}
+
+const ctx: TypesetContext = {
+ width: 500,
+ font: 'Body',
+ fontSize: 10,
+ bold: false,
+ lineHeight: 1.3,
+}
+
+function typeset(
+ node: Record<string, unknown>,
+ context = ctx
+): { node: Record<string, unknown>; setter: PdfTypesetter } {
+ const setter = new PdfTypesetter(source)
+ setter.typeset(node as unknown as Content, context)
+ return { node, setter }
+}
+
+function runs(node: Record<string, unknown>): Array<Record<string, unknown>> {
+ return node.text as Array<Record<string, unknown>>
+}
+
+describe('per-character faces', () => {
+ it('gives a run the face that can draw it and keeps plain text in the body face', () => {
+ const { node, setter } = typeset({ text: 'Total: \u05D0 x' })
+ const out = runs(node)
+ expect(out.map(r => r.font ?? 'Body')).toEqual(['Body', 'Hebrew', 'Body'])
+ expect([...setter.families].sort()).toEqual(['Body', 'Hebrew'])
+ })
+
+ it('keeps run properties when a run is split', () => {
+ const { node } = typeset({ text: [{ text: 'a\u05D0', bold: true, link: 'https://x.test' }] })
+ for (const run of runs(node)) {
+ expect(run.bold).toBe(true)
+ expect(run.link).toBe('https://x.test')
+ }
+ })
+
+ it('falls back from the code face to the body face before any other', () => {
+ const { node } = typeset(
+ { text: 'caf\u00E9', font: 'ClerumMono' },
+ { ...ctx, font: 'ClerumMono' }
+ )
+ expect(runs(node).map(r => [r.text, r.font])).toEqual([
+ ['caf', undefined],
+ ['\u00E9', 'Body'],
+ ])
+ })
+
+ it('expands a tab to spaces in code and to one space in text', () => {
+ expect(runs(typeset({ text: 'a\tb' }).node)[0].text).toBe('a b')
+ const code = typeset({ text: 'a\tb', font: 'ClerumMono' }, { ...ctx, font: 'ClerumMono' })
+ expect(runs(code.node)[0].text).toBe('a b')
+ })
+
+ it('writes a status emoji as text and reports what no face can draw', () => {
+ const { node, setter } = typeset({ text: 'done \u2705 \u{1F680} \u4E2D' })
+ const text = runs(node)
+ .map(r => r.text)
+ .join('')
+ expect(text).toBe('done OK ')
+ const [warning] = setter.warnings()
+ expect(warning).toMatch(/2 character\(s\) of emoji, Chinese text/)
+ expect(warning).toMatch(/U\+1F680/)
+ expect(warning).toMatch(/U\+4E2D/)
+ })
+
+ it('leaves an empty paragraph empty', () => {
+ expect(typeset({ text: '' }).node.text).toBe('')
+ })
+})
+
+describe('right-to-left text', () => {
+ it('hands a shaped script over in reading order, since fontkit reverses it', () => {
+ const { node } = typeset({ text: 'A \u0628\u062C C' })
+ expect(runs(node).map(r => r.text)).toEqual(['A ', '\u0628\u062C', ' C'])
+ })
+
+ it('puts an unshaped script into display order itself', () => {
+ const { node } = typeset({ text: 'A \u05D0\u05D1 C' })
+ expect(runs(node).map(r => r.text)).toEqual(['A ', '\u05D1\u05D0', ' C'])
+ })
+
+ it('right-aligns a right-to-left paragraph and orders its words', () => {
+ const { node } = typeset({ text: '\u05D0 \u05D1' })
+ expect(node.alignment).toBe('right')
+ expect(
+ runs(node)
+ .map(r => r.text)
+ .join('')
+ ).toBe('\u05D1 \u05D0')
+ })
+
+ it('keeps an alignment the caller set', () => {
+ expect(typeset({ text: '\u05D0', alignment: 'center' }).node.alignment).toBe('center')
+ })
+
+ it('breaks a long paragraph into lines itself so each line reads in order', () => {
+ // Ten 4-letter words at 5pt a letter: 20pt each plus spaces, about 4 a line in 100pt.
+ const words = Array.from({ length: 10 }, (_, i) => String.fromCodePoint(0x5d0 + i).repeat(4))
+ const { node } = typeset({ text: words.join(' ') }, { ...ctx, width: 100 })
+ const lines = runs(node)
+ .map(r => r.text)
+ .join('')
+ .split('\n')
+ expect(lines.length).toBeGreaterThan(2)
+ // The first line holds the first words, shown right to left.
+ expect(lines[0].trim().split(' ').reverse()[0]).toBe(words[0])
+ expect(lines.flatMap(l => l.trim().split(' ')).sort()).toEqual([...words].sort())
+ })
+
+ it('measures within the width a table cell gives it', () => {
+ const cell = { text: Array.from({ length: 8 }, () => '\u05D0\u05D0\u05D0').join(' ') }
+ const table = { table: { widths: [40], body: [[cell]] } }
+ typeset(table)
+ expect(runs(cell).some(r => String(r.text).includes('\n'))).toBe(true)
+ })
+
+ // fontkit reverses a whole run once its first letter is Arabic, and Arabic-Indic
+ // digits count as Arabic: the run is handed over as the reverse of what shows.
+ it('keeps Arabic-Indic digits in their order next to a percent sign', () => {
+ const { node } = typeset({
+ text: '\u0628\u0646\u0633\u0628\u0629 \u0661\u0662\u066A \u0641\u064A',
+ })
+ const number = runs(node).find(r => String(r.text).includes('\u0661'))
+ // Shown left to right as percent, one, two: twelve percent.
+ expect(number?.text).toBe('\u0662\u0661\u066A')
+ })
+
+ it('keeps Arabic-Indic and Persian digits in order on their own', () => {
+ expect(runs(typeset({ text: '\u0661\u0662\u0663' }).node).map(r => r.text)).toEqual([
+ '\u0663\u0662\u0661',
+ ])
+ const persian = typeset({
+ text: '\u0628\u0646\u0633\u0628\u0629 \u06F1\u06F2 \u0641\u064A',
+ }).node
+ expect(runs(persian).find(r => String(r.text).includes('\u06F1'))?.text).toBe('\u06F2\u06F1')
+ })
+
+ it('never puts Latin and Arabic letters in one run', () => {
+ const mixed: PdfGlyphSource = { ...source, familyFor: () => 'Arabic' }
+ const setter = new PdfTypesetter(mixed)
+ const node = { text: 'ab \u0628\u062C' }
+ setter.typeset(node as unknown as Content, { ...ctx, font: 'Arabic' })
+ const texts = runs(node).map(r => String(r.text))
+ expect(texts.some(t => /[a-z]/.test(t) && /[\u0600-\u06FF]/.test(t))).toBe(false)
+ })
+
+ it('breaks a right-to-left word wider than the line', () => {
+ const { node } = typeset({ text: '\u05D0'.repeat(300) }, { ...ctx, width: 100 })
+ const lines = runs(node)
+ .map(r => r.text)
+ .join('')
+ .split('\n')
+ // 5pt a letter in 94pt of line: 18 letters a line.
+ expect(lines.length).toBe(Math.ceil(300 / 18))
+ expect(lines.join('')).toBe('\u05D0'.repeat(300))
+ })
+})
+
+describe('long words', () => {
+ it('breaks a stretch with no break in it wherever it fills the line', () => {
+ const { node } = typeset({ text: `data: ${'A'.repeat(1000)} end` }, { ...ctx, width: 100 })
+ const text = runs(node)
+ .map(r => r.text)
+ .join('')
+ const lines = text.split('\n')
+ expect(lines.length).toBeGreaterThan(40)
+ for (const line of lines.slice(1, -1)) expect(line.length * 5).toBeLessThanOrEqual(94)
+ expect(text.replace(/\n/g, '')).toBe(`data: ${'A'.repeat(1000)} end`)
+ })
+
+ it('leaves words that fit, and ideographic text pdfmake breaks itself, alone', () => {
+ const { node } = typeset({ text: `${'word '.repeat(200)}${'\u4E2D'.repeat(300)}` })
+ expect(runs(node).some(r => String(r.text).includes('\n'))).toBe(false)
+ })
+})
+
+describe('line spacing', () => {
+ it('opens up the lines of a face shorter than an em', () => {
+ const { node } = typeset({ text: 'a b' }, { ...ctx, font: 'ClerumMono', lineHeight: 1.25 })
+ expect(runs(node)[0].lineHeight).toBeCloseTo((1.25 * 1.1) / 0.8, 5)
+ })
+
+ it('keeps a tall fallback face from spacing its lines far apart', () => {
+ const { node } = typeset({ text: 'a \u0628 \u05D0' })
+ const byFont = Object.fromEntries(runs(node).map(r => [String(r.font ?? 'Body'), r]))
+ expect(byFont.Arabic.lineHeight).toBeCloseTo((1.3 * 1.2 * 1.2) / 2.1, 5)
+ expect(byFont.Body.lineHeight).toBeUndefined()
+ expect(byFont.Hebrew.lineHeight).toBeUndefined()
+ })
+})
+
+describe('right-to-left lists and tables', () => {
+ type Row = Array<Record<string, unknown>>
+ const cellText = (cell: Record<string, unknown>): string =>
+ (cell.text as Array<Record<string, unknown>>).map(r => r.text).join('')
+
+ it('puts the bullets of a right-to-left list on the right', () => {
+ const node: Record<string, unknown> = {
+ ul: [{ text: '\u05D0 \u05D1' }, { text: '\u05D2' }],
+ margin: [0, 4, 0, 6],
+ }
+ typeset(node)
+ expect(node.ul).toBeUndefined()
+ const body = (node.table as { body: Row[] }).body
+ expect(body).toHaveLength(2)
+ expect(cellText(body[0][1])).toBe('\u2022')
+ expect(body[0][0].alignment).toBe('right')
+ expect(node.margin).toEqual([0, 4, 0, 6])
+ })
+
+ it('numbers a right-to-left list from its start, the number read first', () => {
+ const node: Record<string, unknown> = {
+ ol: [{ text: '\u05D0' }, [{ text: '\u05D1' }, { ul: [{ text: '\u05D2' }] }]],
+ start: 3,
+ }
+ typeset(node)
+ const body = (node.table as { body: Row[] }).body
+ expect(body.map(row => cellText(row[1]))).toEqual(['.3', '.4'])
+ // The nested list keeps its place under its item, and is mirrored too.
+ const nested = (body[1][0].stack as Array<Record<string, unknown>>)[1]
+ expect(nested.table).toBeDefined()
+ })
+
+ it('leaves a left-to-right list to pdfmake', () => {
+ const node: Record<string, unknown> = { ul: [{ text: 'one \u05D0' }, { text: 'two' }] }
+ typeset(node)
+ expect(node.ul).toHaveLength(2)
+ expect(node.table).toBeUndefined()
+ })
+
+ it('runs the columns of a right-to-left table from the right', () => {
+ const table = {
+ headerRows: 1,
+ widths: [100, 50],
+ body: [
+ [{ text: '\u05D0', id: 'h' }, { text: '\u05D1' }],
+ [{ text: 'one' }, { text: '2' }],
+ ],
+ }
+ typeset({ table })
+ expect(table.widths).toEqual([50, 100])
+ expect(table.body.map(row => row.map(cellText))).toEqual([
+ ['\u05D1', '\u05D0'],
+ ['2', 'one'],
+ ])
+ })
+
+ it('starts the cells of a mirrored table at the right', () => {
+ const table = {
+ headerRows: 1,
+ widths: [100, 50],
+ body: [
+ [{ text: '\u05D0' }, { text: '\u05D1' }],
+ [{ text: 'one' }, { text: '2', alignment: 'center' }],
+ ],
+ }
+ typeset({ table })
+ expect(table.body[1].map(cell => cell.alignment)).toEqual(['center', 'right'])
+ })
+
+ it('lines up the first baselines of cells set in faces of different heights', () => {
+ const arabic: Record<string, unknown> = { text: '\u0628' }
+ const latin: Record<string, unknown> = { text: 'x' }
+ const plain: Record<string, unknown> = { text: 'y' }
+ typeset({ table: { widths: [100, 100], body: [[arabic, latin], [plain]] } })
+ // (1.4 - 0.9) em at 10pt.
+ expect(latin.margin).toEqual([0, 5, 0, 0])
+ expect(arabic.margin).toBeUndefined()
+ expect(plain.margin).toBeUndefined()
+ })
+
+ it('keeps the column order of a table whose headers read left to right', () => {
+ const table = {
+ headerRows: 1,
+ widths: [100, 50],
+ body: [
+ [{ text: 'Name' }, { text: '\u05D0' }],
+ [{ text: 'x' }, { text: 'y' }],
+ ],
+ }
+ typeset({ table })
+ expect(table.widths).toEqual([100, 50])
+ })
+})
diff --git a/mcp-host/src/workflow/pdfBidi.ts b/mcp-host/src/workflow/pdfBidi.ts
new file mode 100644
index 000000000..e38551089
--- /dev/null
+++ b/mcp-host/src/workflow/pdfBidi.ts
@@ -0,0 +1,270 @@
+/**
+ * Display order for right-to-left text in PDFs.
+ *
+ * pdfmake lays every run out left to right, and fontkit only reverses the
+ * glyphs inside a single run of a right-to-left script. The order of words,
+ * numbers inside Arabic text and Latin inside Hebrew text therefore has to be
+ * settled before pdfmake sees the text. This is the implicit part of the
+ * Unicode Bidirectional Algorithm (UAX #9): paragraph direction from the first
+ * strong character, the weak rules, bracket pairs and the other neutral rules,
+ * implicit levels and line reordering. Explicit embeddings and isolates are not
+ * supported.
+ */
+
+export type BidiClass = 'L' | 'R' | 'AL' | 'EN' | 'AN' | 'ES' | 'ET' | 'CS' | 'NSM' | 'WS' | 'ON'
+
+const MARK = /[\p{Mn}\p{Me}]/u
+const LETTER = /[\p{L}\p{Mc}\p{Nd}\p{Nl}]/u
+
+function inRanges(cp: number, ranges: ReadonlyArray<readonly [number, number]>): boolean {
+ return ranges.some(([from, to]) => cp >= from && cp <= to)
+}
+
+/** Hebrew, NKo, Samaritan, Mandaic and the historic right-to-left blocks. */
+const STRONG_R: ReadonlyArray<readonly [number, number]> = [
+ [0x0590, 0x05ff],
+ [0x07c0, 0x085f],
+ [0xfb1d, 0xfb4f],
+ [0x10800, 0x10fff],
+ [0x1e800, 0x1edff],
+ [0x200f, 0x200f],
+]
+
+/** Arabic, Syriac and Thaana, and their presentation forms. */
+const STRONG_AL: ReadonlyArray<readonly [number, number]> = [
+ [0x0600, 0x07bf],
+ [0x0860, 0x08ff],
+ [0xfb50, 0xfdff],
+ [0xfe70, 0xfefe],
+ [0x1ee00, 0x1eeff],
+]
+
+const ARABIC_DIGITS: ReadonlyArray<readonly [number, number]> = [
+ [0x0600, 0x0605],
+ [0x0660, 0x0669],
+ [0x066b, 0x066c],
+ [0x08e2, 0x08e2],
+]
+
+const EUROPEAN_DIGITS: ReadonlyArray<readonly [number, number]> = [
+ [0x30, 0x39],
+ [0x06f0, 0x06f9],
+ [0xb2, 0xb3],
+ [0xb9, 0xb9],
+ [0x2070, 0x2079],
+ [0x2080, 0x2089],
+ [0xff10, 0xff19],
+]
+
+const TERMINATORS: ReadonlyArray<readonly [number, number]> = [
+ [0x23, 0x25],
+ [0xa2, 0xa5],
+ [0xb0, 0xb1],
+ [0x066a, 0x066a],
+ [0x2030, 0x2034],
+ [0x20a0, 0x20cf],
+ [0x2212, 0x2213],
+]
+
+const SEPARATORS = new Set([0x2c, 0x2e, 0x2f, 0x3a, 0xa0, 0x060c, 0x202f, 0x2044, 0xfe50, 0xfe52])
+
+export function bidiClass(cp: number): BidiClass {
+ const ch = String.fromCodePoint(cp)
+ if (MARK.test(ch)) return 'NSM'
+ if (inRanges(cp, EUROPEAN_DIGITS)) return 'EN'
+ if (inRanges(cp, ARABIC_DIGITS)) return 'AN'
+ if (inRanges(cp, TERMINATORS)) return 'ET'
+ if (SEPARATORS.has(cp)) return 'CS'
+ if (inRanges(cp, STRONG_AL)) return 'AL'
+ if (inRanges(cp, STRONG_R)) return 'R'
+ if (cp === 0x2b || cp === 0x2d) return 'ES'
+ if (cp === 0x20 || cp === 0x09 || cp === 0x0c || (cp >= 0x2000 && cp <= 0x200a) || cp === 0x3000)
+ return 'WS'
+ if (cp === 0x200e || LETTER.test(ch)) return 'L'
+ return 'ON'
+}
+
+/** Whether a code point belongs to a right-to-left script, marks included. */
+export function isRtlScript(cp: number): boolean {
+ return inRanges(cp, STRONG_R) || inRanges(cp, STRONG_AL)
+}
+
+/** Whether `text` holds anything a right-to-left paragraph would reorder. */
+export function hasRtl(text: string): boolean {
+ for (const ch of text) {
+ const cls = bidiClass(ch.codePointAt(0)!)
+ if (cls === 'R' || cls === 'AL' || cls === 'AN') return true
+ }
+ return false
+}
+
+export interface BidiParagraph {
+ /** 0 for a left-to-right paragraph, 1 for a right-to-left one. */
+ base: 0 | 1
+ levels: number[]
+ /** Classes before resolution, which line reordering needs for trailing space. */
+ classes: BidiClass[]
+}
+
+function strongDirection(cls: BidiClass): 'L' | 'R' | undefined {
+ if (cls === 'L') return 'L'
+ if (cls === 'R' || cls === 'AL') return 'R'
+ return undefined
+}
+
+/** Opening brackets and their closing partners, as BD16 pairs them. */
+const BRACKETS: Record<number, number> = { 0x28: 0x29, 0x5b: 0x5d, 0x7b: 0x7d }
+
+/** BD16: positions of matched bracket pairs, ordered by their opening bracket. */
+function bracketPairs(codePoints: number[], t: BidiClass[]): Array<[number, number]> {
+ const open: Array<{ close: number; at: number }> = []
+ const pairs: Array<[number, number]> = []
+ for (let i = 0; i < codePoints.length; i++) {
+ if (t[i] !== 'ON') continue
+ const cp = codePoints[i]
+ if (BRACKETS[cp] !== undefined) {
+ if (open.length === 63) break
+ open.push({ close: BRACKETS[cp], at: i })
+ continue
+ }
+ for (let k = open.length - 1; k >= 0; k--) {
+ if (open[k].close !== cp) continue
+ pairs.push([open[k].at, i])
+ open.length = k
+ break
+ }
+ }
+ return pairs.sort((a, b) => a[0] - b[0])
+}
+
+/** Resolve the embedding level of each code point of one paragraph. */
+export function resolveParagraph(codePoints: number[]): BidiParagraph {
+ const classes = codePoints.map(bidiClass)
+ const n = classes.length
+ const firstStrong = classes.map(strongDirection).find(Boolean)
+ const base: 0 | 1 = firstStrong === 'R' ? 1 : 0
+ const sos: BidiClass = base ? 'R' : 'L'
+ const t = [...classes]
+
+ // W1: a mark takes the class of the character it follows.
+ for (let i = 0; i < n; i++) if (t[i] === 'NSM') t[i] = i === 0 ? sos : t[i - 1]
+ // W2 and W3: digits after Arabic letters are Arabic numbers; AL is R.
+ let strong: BidiClass = sos
+ for (let i = 0; i < n; i++) {
+ if (t[i] === 'L' || t[i] === 'R' || t[i] === 'AL') strong = t[i]
+ else if (t[i] === 'EN' && strong === 'AL') t[i] = 'AN'
+ }
+ for (let i = 0; i < n; i++) if (t[i] === 'AL') t[i] = 'R'
+ // W4: one separator between two numbers of the same kind joins them.
+ for (let i = 1; i < n - 1; i++) {
+ if (t[i] === 'ES' && t[i - 1] === 'EN' && t[i + 1] === 'EN') t[i] = 'EN'
+ else if (t[i] === 'CS' && t[i - 1] === t[i + 1] && (t[i - 1] === 'EN' || t[i - 1] === 'AN'))
+ t[i] = t[i - 1]
+ }
+ // W5: currency and percent signs next to a European number join it.
+ for (let i = 0; i < n; i++) {
+ if (t[i] !== 'ET') continue
+ let end = i
+ while (end < n && t[end] === 'ET') end++
+ if ((i > 0 && t[i - 1] === 'EN') || (end < n && t[end] === 'EN')) {
+ for (let k = i; k < end; k++) t[k] = 'EN'
+ }
+ i = end - 1
+ }
+ // W6 and W7: leftover separators are neutral; numbers after Latin text are Latin.
+ for (let i = 0; i < n; i++) if (t[i] === 'ES' || t[i] === 'ET' || t[i] === 'CS') t[i] = 'ON'
+ strong = sos
+ for (let i = 0; i < n; i++) {
+ if (t[i] === 'L' || t[i] === 'R') strong = t[i]
+ else if (t[i] === 'EN' && strong === 'L') t[i] = 'L'
+ }
+ // N0: a bracket pair takes the direction of the text it encloses when that
+ // text and the text before the pair agree, else the paragraph's. Numbers
+ // count as right-to-left here and below.
+ const strongSide = (cls: BidiClass): 'L' | 'R' | undefined =>
+ cls === 'L' ? 'L' : cls === 'R' || cls === 'EN' || cls === 'AN' ? 'R' : undefined
+ const embedding: 'L' | 'R' = base ? 'R' : 'L'
+ for (const [open, close] of bracketPairs(codePoints, t)) {
+ let inside: 'L' | 'R' | undefined
+ for (let k = open + 1; k < close && inside !== embedding; k++)
+ inside = strongSide(t[k]) ?? inside
+ if (inside === undefined) continue
+ let dir = inside
+ if (inside !== embedding) {
+ let before: 'L' | 'R' = embedding
+ for (let k = open - 1; k >= 0; k--) {
+ const side = strongSide(t[k])
+ if (side) {
+ before = side
+ break
+ }
+ }
+ dir = before === inside ? inside : embedding
+ }
+ t[open] = dir
+ t[close] = dir
+ }
+ // N1 and N2: neutrals between two runs of one direction take it; the rest
+ // take the paragraph's.
+ const side = (cls: BidiClass): 'L' | 'R' => (cls === 'L' ? 'L' : 'R')
+ for (let i = 0; i < n; i++) {
+ if (t[i] !== 'WS' && t[i] !== 'ON') continue
+ let end = i
+ while (end < n && (t[end] === 'WS' || t[end] === 'ON')) end++
+ const before = i === 0 ? side(sos) : side(t[i - 1])
+ const after = end === n ? side(sos) : side(t[end])
+ const dir = before === after ? before : side(sos)
+ for (let k = i; k < end; k++) t[k] = dir
+ i = end - 1
+ }
+ // I1 and I2: implicit levels.
+ const levels = t.map(cls => {
+ if (base === 0) return cls === 'R' ? 1 : cls === 'AN' || cls === 'EN' ? 2 : 0
+ return cls === 'L' || cls === 'EN' || cls === 'AN' ? 2 : 1
+ })
+ return { base, levels, classes }
+}
+
+/**
+ * Indices of `start..end` (exclusive) in display order, for one line of a
+ * resolved paragraph. Trailing white space goes back to the paragraph level so
+ * it stays at the line's end.
+ */
+export function visualOrder(paragraph: BidiParagraph, start: number, end: number): number[] {
+ const levels = paragraph.levels.slice(start, end)
+ for (let i = levels.length - 1; i >= 0 && paragraph.classes[start + i] === 'WS'; i--) {
+ levels[i] = paragraph.base
+ }
+ const order = levels.map((_, i) => start + i)
+ const highest = Math.max(0, ...levels)
+ const lowestOdd = Math.min(...levels.filter(l => l % 2 === 1), highest + 1)
+ for (let level = highest; level >= lowestOdd; level--) {
+ for (let i = 0; i < levels.length; i++) {
+ if (levels[i] < level) continue
+ let j = i
+ while (j < levels.length && levels[j] >= level) j++
+ order.splice(i, j - i, ...order.slice(i, j).reverse())
+ levels.splice(i, j - i, ...levels.slice(i, j).reverse())
+ i = j
+ }
+ }
+ return order
+}
+
+const MIRRORED: Record<string, string> = {
+ '(': ')',
+ ')': '(',
+ '[': ']',
+ ']': '[',
+ '{': '}',
+ '}': '{',
+ '<': '>',
+ '>': '<',
+ '\u00AB': '\u00BB',
+ '\u00BB': '\u00AB',
+}
+
+/** The glyph a bracket shows at a right-to-left level, which faces open the other way. */
+export function mirrored(ch: string, level: number): string {
+ return level % 2 === 1 ? (MIRRORED[ch] ?? ch) : ch
+}
diff --git a/mcp-host/src/workflow/pdfTables.ts b/mcp-host/src/workflow/pdfTables.ts
new file mode 100644
index 000000000..745092ecf
--- /dev/null
+++ b/mcp-host/src/workflow/pdfTables.ts
@@ -0,0 +1,309 @@
+/**
+ * Column widths, type size, orientation and row splitting for PDF tables.
+ *
+ * pdfmake never makes a '*' column narrower than its longest unbreakable word,
+ * so a table of long tokens would run past the page edge and lose the columns
+ * beyond it. Numeric widths let pdfmake break an oversized token inside its
+ * cell instead, and the type size or the page orientation gives way before a
+ * table has to do that to ordinary words.
+ */
+
+/** Width of `text` at 1pt in the body face; widths scale linearly with size. */
+export type UnitMeasure = (text: string, bold: boolean) => number
+
+export interface PageBox {
+ width: number
+ height: number
+}
+
+export interface TableLayoutInput {
+ headers: string[]
+ /** Cell text as it will read, markdown markers removed. */
+ rows: string[][]
+ /** The `widths` argument, when the caller gave one. */
+ requested?: unknown
+ /** Horizontal padding pdfmake adds to each cell, both sides together. */
+ cellPadding: number
+ /** Width of the vertical rules the layout draws. */
+ ruleWidth: number
+ /** Whether the table may be moved onto landscape pages. */
+ allowLandscape: boolean
+ /** Argument path used in warnings, such as "tables[0]". */
+ label: string
+ /** The page's bottom margin, which a footer of several lines grows past the usual 60pt. */
+ bottomMargin?: number
+}
+
+export interface TableLayout {
+ widths: number[]
+ fontSize: number
+ landscape: boolean
+ dontBreakRows: boolean
+ keepWithHeaderRows: 0 | 1
+}
+
+/** A4 content boxes with the documents' 40pt side and 60pt top margins and the smallest bottom one. */
+export const PORTRAIT: PageBox = { width: 515, height: 722 }
+export const LANDSCAPE: PageBox = { width: 762, height: 475 }
+export const MIN_BOTTOM_MARGIN = 60
+
+export const BODY_FONT_SIZE = 11
+const LINE_HEIGHT = 1.3
+const ROW_PADDING = 4
+
+/**
+ * Longest a single token may make its column before it is left to break
+ * inside the cell. Longer tokens are URLs and hashes, not words.
+ */
+const LONG_TOKEN = 140
+
+/** Added to every measured width: pdfmake breaks a word that fills its column exactly. */
+const SLACK = 2
+
+/** Narrowest a flexible column is squeezed to beside fixed ones. */
+const MIN_FLEX = 24
+
+const ATTEMPTS: Array<[PageBox, number]> = [
+ [PORTRAIT, 11],
+ [PORTRAIT, 10],
+ [PORTRAIT, 9],
+ [PORTRAIT, 8],
+ [LANDSCAPE, 11],
+ [LANDSCAPE, 10],
+ [LANDSCAPE, 9],
+ [LANDSCAPE, 8],
+ [LANDSCAPE, 7],
+]
+
+interface ColumnExtent {
+ /** Longest word, capped at LONG_TOKEN, at 1pt. */
+ min: number
+ /** Longest line at 1pt. */
+ max: number
+}
+
+function extents(input: TableLayoutInput, measure: UnitMeasure): ColumnExtent[] {
+ const cols = input.headers.map(() => ({ min: 0, max: 0 }))
+ const take = (text: string, col: number, bold: boolean): void => {
+ for (const line of text.split('\n')) {
+ cols[col].max = Math.max(cols[col].max, measure(line, bold))
+ for (const word of line.split(/\s+/)) {
+ if (word) cols[col].min = Math.max(cols[col].min, measure(word, bold))
+ }
+ }
+ }
+ input.headers.forEach((h, c) => take(h, c, true))
+ for (const row of input.rows) row.forEach((cell, c) => c < cols.length && take(cell, c, false))
+ return cols
+}
+
+type Spec =
+ | { kind: 'fixed'; points: number }
+ | { kind: 'share'; percent: number }
+ | { kind: 'star' }
+ | { kind: 'auto' }
+
+function parseSpecs(input: TableLayoutInput, warnings: string[]): Spec[] | undefined {
+ const req = input.requested
+ if (req === undefined || req === null) return undefined
+ if (!Array.isArray(req) || req.length !== input.headers.length) {
+ warnings.push(
+ `${input.label}.widths has ${Array.isArray(req) ? req.length : 0} entries for ` +
+ `${input.headers.length} columns, so it was ignored; give one width per header.`
+ )
+ return undefined
+ }
+ return req.map((raw, i): Spec => {
+ const text = typeof raw === 'string' ? raw.trim() : raw
+ if (text === '*') return { kind: 'star' }
+ if (text === 'auto') return { kind: 'auto' }
+ const percent = typeof text === 'string' ? /^(\d+(?:\.\d+)?)%$/.exec(text) : null
+ if (percent) return { kind: 'share', percent: Number(percent[1]) }
+ const points = typeof text === 'number' ? text : typeof text === 'string' ? Number(text) : NaN
+ if (Number.isFinite(points) && points > 0) return { kind: 'fixed', points }
+ warnings.push(
+ `${input.label}.widths[${i}] (${JSON.stringify(raw)}) is not a number of points, ` +
+ `a percentage, '*' or 'auto', so that column shares the remaining space.`
+ )
+ return { kind: 'star' }
+ })
+}
+
+const sum = (xs: number[]): number => xs.reduce((a, b) => a + b, 0)
+
+/**
+ * Widths between each column's minimum and its full content: the full content
+ * when it fits, otherwise the minimum plus a share of what is left.
+ */
+function squeeze(min: number[], max: number[], room: number): number[] {
+ if (sum(max) <= room) return max
+ if (sum(min) <= room) {
+ const spread = sum(max) - sum(min)
+ return min.map((m, i) => m + ((room - sum(min)) * (max[i] - m)) / spread)
+ }
+ const total = sum(min)
+ return min.map(m => (total > 0 ? (room * m) / total : room / min.length))
+}
+
+/** Narrowest a column can be at `size` without breaking an ordinary word. */
+function minWidths(cols: ColumnExtent[], size: number): number[] {
+ return cols.map(c => Math.min(c.min * size, LONG_TOKEN) + SLACK)
+}
+
+interface RequestedFit {
+ widths: number[]
+ /** Set when the fixed widths had to be scaled down: what they added up to, and the room left. */
+ scaled?: { from: number; to: number }
+}
+
+const isFlexible = (spec: Spec): boolean => spec.kind === 'star' || spec.kind === 'auto'
+
+/**
+ * Widths from the caller's specs. Fixed widths give way before a '*' or
+ * 'auto' column is made narrower than its longest word.
+ */
+function requestedWidths(
+ specs: Spec[],
+ cols: ColumnExtent[],
+ size: number,
+ room: number
+): RequestedFit {
+ const floor = minWidths(cols, size).map(m => Math.max(m, MIN_FLEX))
+ const full = cols.map((c, i) => Math.max(c.max * size + SLACK, floor[i]))
+ let fixed = specs.map(s =>
+ s.kind === 'fixed' ? s.points : s.kind === 'share' ? (room * s.percent) / 100 : 0
+ )
+ const fixedSum = sum(fixed)
+ const fixedCount = specs.filter(s => !isFlexible(s)).length
+ const flexibleFloor = sum(specs.map((s, i) => (isFlexible(s) ? floor[i] : 0)))
+ const fixedRoom = Math.max(room - flexibleFloor, Math.min(fixedSum, fixedCount * MIN_FLEX))
+ let scaled: RequestedFit['scaled']
+ if (fixedSum > fixedRoom) {
+ fixed = fixed.map(w => (w * fixedRoom) / fixedSum)
+ scaled = { from: fixedSum, to: fixedRoom }
+ }
+ const rest = Math.max(0, room - sum(fixed))
+ const autos = specs.flatMap((s, i) => (s.kind === 'auto' ? [i] : []))
+ const stars = specs.flatMap((s, i) => (s.kind === 'star' ? [i] : []))
+ const starFloor = sum(stars.map(i => floor[i]))
+ const autoWidths = squeeze(
+ autos.map(i => floor[i]),
+ autos.map(i => full[i]),
+ Math.max(0, rest - starFloor)
+ )
+ const widths = [...fixed]
+ autos.forEach((col, k) => (widths[col] = autoWidths[k]))
+ // Stars share what is left evenly, as pdfmake does, unless that is narrower than a word.
+ const starRoom = Math.max(0, rest - sum(autoWidths))
+ const even = starRoom / Math.max(1, stars.length)
+ stars.forEach(col => {
+ if (stars.every(i => floor[i] <= even)) widths[col] = even
+ else if (starFloor <= starRoom) widths[col] = floor[col] + (starRoom - starFloor) / stars.length
+ else widths[col] = (starRoom * floor[col]) / starFloor
+ })
+ return { widths, scaled }
+}
+
+/** Widths that fill `room`, giving each column what its content asks for first. */
+function contentWidths(cols: ColumnExtent[], size: number, room: number): number[] {
+ const min = minWidths(cols, size)
+ const max = cols.map((c, i) => Math.max(c.max * size + SLACK, min[i]))
+ if (sum(max) <= room) {
+ // Spare room goes to columns in proportion to their content; empty ones share it evenly.
+ const weights = max.map(m => m || 1)
+ return max.map((m, i) => m + ((room - sum(max)) * weights[i]) / sum(weights))
+ }
+ return squeeze(min, max, room)
+}
+
+function rowHeight(
+ cells: string[],
+ widths: number[],
+ size: number,
+ bold: boolean,
+ measure: UnitMeasure
+): number {
+ let lines = 1
+ cells.forEach((cell, c) => {
+ const width = Math.max(1, widths[c] ?? 1)
+ let count = 0
+ for (const line of cell.split('\n'))
+ count += Math.max(1, Math.ceil((measure(line, bold) * size) / width))
+ lines = Math.max(lines, count)
+ })
+ return lines * size * LINE_HEIGHT + ROW_PADDING
+}
+
+/** Header names for a warning: the first three, then how many more. */
+function columnNames(headers: string[], columns: number[]): string {
+ const names = columns.map(c => `'${headers[c]?.trim() || `column ${c + 1}`}'`)
+ if (names.length === 1) return names[0]
+ if (names.length <= 3) return `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`
+ return `${names.slice(0, 3).join(', ')} and ${names.length - 3} more column(s)`
+}
+
+/** Lay out one table, reporting in `warnings` anything done to the caller's request. */
+export function layoutPdfTable(
+ input: TableLayoutInput,
+ measure: UnitMeasure,
+ warnings: string[]
+): TableLayout {
+ const n = Math.max(1, input.headers.length)
+ const roomOn = (page: PageBox): number =>
+ page.width - n * input.cellPadding - (n + 1) * input.ruleWidth
+ const cols = extents(input, measure)
+ const specs = parseSpecs(input, warnings)
+ const plan = (page: PageBox, size: number): RequestedFit =>
+ specs
+ ? requestedWidths(specs, cols, size, roomOn(page))
+ : { widths: contentWidths(cols, size, roomOn(page)) }
+ const broken = (widths: number[], size: number): number[] =>
+ minWidths(cols, size).flatMap((m, i) => (widths[i] + 0.01 < m ? [i] : []))
+
+ const attempts = ATTEMPTS.filter(([p]) => p === PORTRAIT || input.allowLandscape)
+ let chosen = attempts.find(([p, s]) => broken(plan(p, s).widths, s).length === 0)
+ // Nothing holds every word: the caller's widths are kept at full size, and
+ // otherwise the smallest type on the widest page breaks the fewest.
+ chosen ??= specs ? attempts[0] : attempts[attempts.length - 1]
+ const [page, size] = chosen
+ const fit = plan(page, size)
+ const widths = fit.widths
+
+ if (fit.scaled) {
+ warnings.push(
+ `${input.label}.widths add up to ${Math.round(fit.scaled.from)} pt, more than the ` +
+ `${Math.round(fit.scaled.to)} pt the page leaves for them, so they were scaled down to fit.`
+ )
+ }
+ if (page === LANDSCAPE) {
+ warnings.push(
+ `${input.label} has more columns than a portrait page can show legibly, so it was set on ` +
+ 'landscape pages.'
+ )
+ }
+ const narrow = broken(widths, size)
+ if (narrow.length > 0) {
+ const many = narrow.length > 1
+ warnings.push(
+ `${input.label}: words in ${columnNames(input.headers, narrow)} are wider than ` +
+ `${many ? 'those columns' : 'that column'}, so they break across lines; ` +
+ (specs
+ ? `widen ${many ? 'them' : 'it'} in ${input.label}.widths, or split the table.`
+ : 'split the table or leave out columns.')
+ )
+ }
+
+ // pdfmake drops a row taller than a page when rows may not break, and drops
+ // the header with the first row when keeping them together cannot fit.
+ const usable =
+ page.height - Math.max(0, (input.bottomMargin ?? MIN_BOTTOM_MARGIN) - MIN_BOTTOM_MARGIN)
+ const headerHeight = rowHeight(input.headers, widths, size, true, measure)
+ const heights = input.rows.map(row => rowHeight(row, widths, size, false, measure))
+ return {
+ widths,
+ fontSize: size,
+ landscape: page === LANDSCAPE,
+ dontBreakRows: heights.every(h => h + headerHeight <= usable * 0.8),
+ keepWithHeaderRows: headerHeight + (heights[0] ?? 0) <= usable * 0.5 ? 1 : 0,
+ }
+}
diff --git a/mcp-host/src/workflow/pdfText.ts b/mcp-host/src/workflow/pdfText.ts
new file mode 100644
index 000000000..4f0af6dae
--- /dev/null
+++ b/mcp-host/src/workflow/pdfText.ts
@@ -0,0 +1,556 @@
+/**
+ * Final pass over a pdfmake document: every piece of text is split into runs
+ * whose face can draw them, right-to-left text is put into display order, and
+ * whatever no face can draw is reported instead of vanishing.
+ *
+ * pdfmake names one family per run and pdfkit draws a missing glyph as a blank
+ * box, so runs are split by face. pdfmake has no right-to-left layout, so lists
+ * and tables whose text reads that way are mirrored here.
+ */
+import type { Content } from 'pdfmake/interfaces'
+import { PDF_MONO_FAMILY, type PdfGlyphSource, asciiStandIn } from './fonts'
+import {
+ type BidiParagraph,
+ hasRtl,
+ isRtlScript,
+ mirrored,
+ resolveParagraph,
+ visualOrder,
+} from './pdfBidi'
+
+type Props = Record<string, unknown>
+
+/** What a node inherits from its ancestors, for choosing and measuring faces. */
+export interface TypesetContext {
+ /** Width available to the node's text, in points. */
+ width: number
+ font: string
+ fontSize: number
+ bold: boolean
+ lineHeight: number
+}
+
+interface Glyph {
+ text: string
+ family: string
+ props: Props
+ cp: number
+}
+
+/** Dropped because they carry nothing to draw. Joiners are kept: Arabic shaping reads them. */
+const INVISIBLE = /^[\u200B\u200E\u200F\u2060\uFE00-\uFE0F\uFEFF]$/u
+const JOINER = /^[\u200C\u200D]$/u
+const EMOJI = /^\p{Emoji_Presentation}$/u
+const PLAIN = /^[\x20-\x7E\n]*$/
+
+/** Share of the width a line fills before it is broken, leaving room for measuring error. */
+export const LINE_FILL = 0.94
+
+/**
+ * Glyphs in a stretch with no break opportunity beyond which the typesetter
+ * breaks it itself. pdfmake splits an over-wide word by measuring a prefix and
+ * the rest of it for every line, and pdfkit caches each string it measures, so
+ * a long token (a base64 blob, a minified JSON line) costs time and memory
+ * quadratic in its length.
+ */
+const LONG_WORD = 64
+
+/** Characters pdfmake may break a line after without a space: ideographs and CJK punctuation. */
+const BREAKS_AFTER =
+ /[\s\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3000-\u303F\uFF00-\uFFEF]/u
+
+/** Combining marks, which stay on the line of the character they follow. */
+const MARK = /^[\p{Mn}\p{Me}]$/u
+
+/** Neither direction of its own: fontkit reads a run's direction from its first other character. */
+const NEUTRAL_SCRIPT = /^[\p{Script=Common}\p{Script=Inherited}]/u
+
+/** The scripts fontkit sets right to left. */
+const RTL_SCRIPT =
+ /^[\p{Script=Arabic}\p{Script=Hebrew}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Cypriot}\p{Script=Kharoshthi}\p{Script=Phoenician}\p{Script=Nko}\p{Script=Lydian}\p{Script=Avestan}\p{Script=Imperial_Aramaic}\p{Script=Inscriptional_Pahlavi}\p{Script=Inscriptional_Parthian}\p{Script=Old_South_Arabian}\p{Script=Old_Turkic}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Meroitic_Cursive}\p{Script=Meroitic_Hieroglyphs}\p{Script=Manichaean}\p{Script=Mende_Kikakui}\p{Script=Nabataean}\p{Script=Old_North_Arabian}\p{Script=Palmyrene}\p{Script=Psalter_Pahlavi}]/u
+
+/** The direction fontkit gives a run that starts with `ch`, if `ch` decides it. */
+function scriptDirection(ch: string): 'L' | 'R' | undefined {
+ if (NEUTRAL_SCRIPT.test(ch)) return undefined
+ return RTL_SCRIPT.test(ch) ? 'R' : 'L'
+}
+
+/**
+ * Bounds, in ems, on the line a run's face asks for. pdfkit sizes a line from
+ * the face's ascent and descent: Courier's leave code lines touching, and
+ * Noto Sans Arabic's space a paragraph as if it were double-spaced.
+ */
+const MIN_LINE_EM = 1.1
+const MAX_LINE_EM_OVER_BODY = 1.2
+
+/** A list's marker column, in ems of its type size, as pdfmake indents its items. */
+const MARKER_EMS = 2
+
+const SCRIPT_NAMES: Array<[RegExp, string]> = [
+ [/\p{Script=Arabic}/u, 'Arabic'],
+ [/\p{Script=Hebrew}/u, 'Hebrew'],
+ [/\p{Script=Han}/u, 'Chinese'],
+ [/[\p{Script=Hiragana}\p{Script=Katakana}]/u, 'Japanese'],
+ [/\p{Script=Hangul}/u, 'Korean'],
+ [/\p{Script=Thai}/u, 'Thai'],
+ [/\p{Script=Devanagari}/u, 'Devanagari'],
+ [/\p{Script=Bengali}/u, 'Bengali'],
+ [/\p{Script=Tamil}/u, 'Tamil'],
+ [/\p{Script=Cyrillic}/u, 'Cyrillic'],
+ [/\p{Script=Greek}/u, 'Greek'],
+ [/\p{Extended_Pictographic}/u, 'emoji'],
+]
+
+function scriptName(ch: string): string | undefined {
+ return SCRIPT_NAMES.find(([re]) => re.test(ch))?.[1]
+}
+
+function margins(node: Props): [number, number] {
+ const m = node.margin
+ if (typeof m === 'number') return [m, m]
+ if (Array.isArray(m) && m.length === 2) return [Number(m[0]) || 0, Number(m[0]) || 0]
+ if (Array.isArray(m) && m.length === 4) return [Number(m[0]) || 0, Number(m[2]) || 0]
+ return [0, 0]
+}
+
+function isObject(value: unknown): value is Props {
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
+}
+
+/** In a mirrored table or list, text with no alignment of its own starts at the right. */
+function startRight(cell: unknown): void {
+ if (isObject(cell) && cell.alignment === undefined) cell.alignment = 'right'
+}
+
+export class PdfTypesetter {
+ /** Every family the typeset content names, for the printer's font table. */
+ readonly families = new Set<string>()
+ private readonly missing = new Map<string, number>()
+ private readonly advances = new Map<string, number>()
+ /** Base direction of each typeset text node that has one: 0 left to right, 1 right to left. */
+ private readonly direction = new WeakMap<object, 0 | 1>()
+ private bodyEm?: number
+
+ constructor(
+ private readonly glyphs: PdfGlyphSource,
+ private readonly styles: Record<string, Props> = {}
+ ) {}
+
+ /** Typeset every text node in `content`, in place. */
+ typeset(content: Content | Content[], ctx: TypesetContext): void {
+ this.bodyEm ??= this.glyphs.metrics(ctx.font)?.lineHeight
+ this.visit(content as unknown, ctx)
+ }
+
+ /** Text that was left out, as a note the agent can act on. */
+ warnings(): string[] {
+ if (this.missing.size === 0) return []
+ const total = [...this.missing.values()].reduce((a, b) => a + b, 0)
+ const scripts = [...new Set([...this.missing.keys()].map(scriptName).filter(Boolean))]
+ const samples = [...this.missing.keys()]
+ .slice(0, 5)
+ .map(ch => `'${ch}' (U+${ch.codePointAt(0)!.toString(16).toUpperCase().padStart(4, '0')})`)
+ return [
+ `${total} character(s)${scripts.length ? ` of ${scripts.join(', ')} text` : ''} have no glyph in ` +
+ `the fonts available to the PDF renderer and were left out, for example ${samples.join(', ')}. ` +
+ 'clerum__generate_docx keeps such text.',
+ ]
+ }
+
+ private visit(node: unknown, ctx: TypesetContext): unknown {
+ if (typeof node === 'string') {
+ const holder: Props = { text: node }
+ this.textNode(holder, ctx)
+ return holder
+ }
+ if (Array.isArray(node)) {
+ for (let i = 0; i < node.length; i++) node[i] = this.visit(node[i], ctx)
+ return node
+ }
+ if (!isObject(node)) return node
+ const own = this.context(node, ctx)
+ if ('text' in node) {
+ this.textNode(node, own)
+ } else if (Array.isArray(node.stack)) {
+ this.visit(node.stack, own)
+ } else if (Array.isArray(node.ul) || Array.isArray(node.ol)) {
+ // Room for the bullet or number pdfmake draws before each item.
+ const items = { ...own, width: own.width - own.fontSize * MARKER_EMS }
+ const list = (node.ul ?? node.ol) as unknown[]
+ this.visit(list, items)
+ if (this.readsRtl(list.map(item => (Array.isArray(item) ? item[0] : item)))) {
+ this.mirrorList(node, list, own)
+ }
+ } else if (isObject(node.table) && Array.isArray(node.table.body)) {
+ const table = node.table
+ const widths = Array.isArray(table.widths) ? table.widths : []
+ const body = table.body as unknown[][]
+ for (const row of body) {
+ if (!Array.isArray(row)) continue
+ for (let c = 0; c < row.length; c++) {
+ const w = typeof widths[c] === 'number' ? (widths[c] as number) : own.width / row.length
+ row[c] = this.visit(row[c], { ...own, width: w })
+ }
+ this.alignBaselines(row, own)
+ }
+ // Only data tables: code blocks and quotes are one-cell tables without a header.
+ if (Number(table.headerRows) >= 1 && Array.isArray(body[0]) && this.readsRtl(body[0])) {
+ for (const row of body) {
+ if (!Array.isArray(row)) continue
+ row.reverse()
+ for (const cell of row) startRight(cell)
+ }
+ if (Array.isArray(table.widths)) table.widths.reverse()
+ }
+ } else if (Array.isArray(node.columns)) {
+ const width = own.width / Math.max(1, node.columns.length)
+ this.visit(node.columns, { ...own, width })
+ }
+ return node
+ }
+
+ /** Whether more of these typeset nodes read right to left than left to right. */
+ private readsRtl(nodes: unknown[]): boolean {
+ let balance = 0
+ for (const node of nodes) {
+ const dir = isObject(node) ? this.direction.get(node) : undefined
+ if (dir !== undefined) balance += dir === 1 ? 1 : -1
+ }
+ return balance > 0
+ }
+
+ /**
+ * pdfmake draws list markers only at the left, far from right-aligned text,
+ * so a right-to-left list becomes a table with its markers on the right.
+ */
+ private mirrorList(node: Props, items: unknown[], ctx: TypesetContext): void {
+ const ordered = Array.isArray(node.ol)
+ const start = typeof node.start === 'number' ? node.start : 1
+ const body = items.map((item, i) => {
+ // Display order: in a right-to-left line "3." reads with the number first, at the right.
+ const marker: Props = { text: ordered ? `.${start + i}` : '\u2022', alignment: 'right' }
+ this.textNode(marker, ctx)
+ const row = [Array.isArray(item) ? { stack: item } : item, marker]
+ startRight(row[0])
+ this.alignBaselines(row, ctx)
+ return row
+ })
+ delete node.ul
+ delete node.ol
+ delete node.start
+ node.table = { widths: ['*', ctx.fontSize * MARKER_EMS], body }
+ node.layout = {
+ hLineWidth: () => 0,
+ vLineWidth: () => 0,
+ paddingLeft: () => 0,
+ paddingRight: () => 0,
+ paddingTop: () => 0,
+ paddingBottom: () => 0,
+ }
+ }
+
+ /**
+ * pdfmake sets each cell's first baseline by the tallest face on its line,
+ * so an Arabic cell sits lower than a Latin one beside it. A top margin on
+ * the others puts their first lines level.
+ */
+ private alignBaselines(cells: unknown[], ctx: TypesetContext): void {
+ const ascents = cells.map(cell => this.firstLineAscent(cell, ctx))
+ const top = Math.max(0, ...ascents.map(a => a ?? 0))
+ cells.forEach((cell, i) => {
+ const gap = top - (ascents[i] ?? top)
+ if (gap < 0.5 || !isObject(cell)) return
+ const m = cell.margin
+ if (m === undefined) cell.margin = [0, gap, 0, 0]
+ else if (Array.isArray(m) && m.length === 4)
+ cell.margin = [m[0], Number(m[1]) + gap, m[2], m[3]]
+ })
+ }
+
+ /** Points above the first baseline of a typeset node, from the faces on its first line. */
+ private firstLineAscent(node: unknown, ctx: TypesetContext): number | undefined {
+ if (Array.isArray(node)) return this.firstLineAscent(node[0], ctx)
+ if (!isObject(node)) return undefined
+ const own = this.context(node, ctx)
+ if (Array.isArray(node.stack)) return this.firstLineAscent(node.stack[0], own)
+ if (!Array.isArray(node.text)) return undefined
+ let ascent = 0
+ for (const run of node.text as Props[]) {
+ const family = typeof run.font === 'string' ? run.font : own.font
+ const size = typeof run.fontSize === 'number' ? run.fontSize : own.fontSize
+ ascent = Math.max(ascent, (this.glyphs.metrics(family)?.ascent ?? 0) * size)
+ if (String(run.text).includes('\n')) break
+ }
+ return ascent || undefined
+ }
+
+ private context(node: Props, ctx: TypesetContext): TypesetContext {
+ const out = { ...ctx }
+ const names = Array.isArray(node.style) ? node.style : node.style ? [node.style] : []
+ for (const source of [...names.map(n => this.styles[String(n)] ?? {}), node]) {
+ if (typeof source.font === 'string') out.font = source.font
+ if (typeof source.fontSize === 'number') out.fontSize = source.fontSize
+ if (typeof source.bold === 'boolean') out.bold = source.bold
+ if (typeof source.lineHeight === 'number') out.lineHeight = source.lineHeight
+ }
+ const [left, right] = margins(node)
+ out.width = Math.max(1, ctx.width - left - right)
+ return out
+ }
+
+ /** The text of a node as pieces with the run properties each carries. */
+ private pieces(
+ text: unknown,
+ inherited: Props,
+ out: Array<{ text: string; props: Props }>
+ ): void {
+ if (typeof text === 'string' || typeof text === 'number') {
+ out.push({ text: String(text), props: inherited })
+ } else if (Array.isArray(text)) {
+ for (const item of text) this.pieces(item, inherited, out)
+ } else if (isObject(text)) {
+ const { text: inner, ...props } = text
+ this.pieces(inner, { ...inherited, ...props }, out)
+ }
+ }
+
+ private glyphsOf(text: string, props: Props, ctx: TypesetContext, out: Glyph[]): void {
+ const base = typeof props.font === 'string' ? props.font : ctx.font
+ if (PLAIN.test(text)) {
+ for (const ch of text) out.push({ text: ch, family: base, props, cp: ch.codePointAt(0)! })
+ return
+ }
+ for (const ch of text) {
+ const cp = ch.codePointAt(0)!
+ if (INVISIBLE.test(ch)) continue
+ if (JOINER.test(ch)) {
+ const previous = out[out.length - 1]
+ if (previous && isRtlScript(previous.cp))
+ out.push({ text: ch, family: previous.family, props, cp })
+ continue
+ }
+ if (ch === '\t') {
+ out.push({ text: base === PDF_MONO_FAMILY ? ' ' : ' ', family: base, props, cp: 0x20 })
+ continue
+ }
+ if (ch === '\n') {
+ out.push({ text: ch, family: base, props, cp })
+ continue
+ }
+ if (cp < 0x20) continue
+ const standIn = asciiStandIn(ch)
+ const family =
+ EMOJI.test(ch) && standIn !== undefined ? undefined : this.glyphs.familyFor(cp, base)
+ if (family) {
+ out.push({ text: ch, family, props, cp })
+ } else if (standIn !== undefined) {
+ for (const c of standIn) out.push({ text: c, family: base, props, cp: c.codePointAt(0)! })
+ } else {
+ this.missing.set(ch, (this.missing.get(ch) ?? 0) + 1)
+ }
+ }
+ }
+
+ private textNode(node: Props, ctx: TypesetContext): void {
+ const pieces: Array<{ text: string; props: Props }> = []
+ this.pieces(node.text, {}, pieces)
+ const glyphs: Glyph[] = []
+ for (const piece of pieces) this.glyphsOf(piece.text, piece.props, ctx, glyphs)
+
+ if (!pieces.some(p => hasRtl(p.text))) {
+ const runs = this.runs(this.breakLongWords(glyphs, ctx), ctx)
+ node.text = runs.length > 0 ? runs : ''
+ if (pieces.some(p => /\p{L}/u.test(p.text))) this.direction.set(node, 0)
+ return
+ }
+ const lines: Glyph[][] = []
+ let firstBase: 0 | 1 | undefined
+ let start = 0
+ for (let i = 0; i <= glyphs.length; i++) {
+ if (i < glyphs.length && glyphs[i].text !== '\n') continue
+ const paragraph = glyphs.slice(start, i)
+ const bidi = resolveParagraph(paragraph.map(g => g.cp))
+ if (firstBase === undefined && paragraph.length > 0) firstBase = bidi.base
+ for (const [from, to] of this.breakLines(paragraph, ctx)) {
+ lines.push(this.displayOrder(paragraph, bidi, from, to))
+ }
+ start = i + 1
+ }
+ const runs: Props[] = []
+ lines.forEach((line, i) => {
+ const lineRuns = this.runs(line, ctx)
+ // The break rides on the line's last run: a run holding only '\n' is
+ // laid out as an extra empty line.
+ if (i < lines.length - 1) {
+ const last = lineRuns[lineRuns.length - 1]
+ if (last) last.text = `${String(last.text)}\n`
+ else lineRuns.push({ text: '\n' })
+ }
+ runs.push(...lineRuns)
+ })
+ node.text = runs
+ if (firstBase !== undefined) this.direction.set(node, firstBase)
+ if (firstBase === 1 && node.alignment === undefined) node.alignment = 'right'
+ }
+
+ /**
+ * `glyphs` with a line break in every stretch too long and too wide for one
+ * line, wherever it fills the line. Only for text pdfmake lays out itself.
+ */
+ private breakLongWords(glyphs: Glyph[], ctx: TypesetContext): Glyph[] {
+ const limit = ctx.width * LINE_FILL
+ let out: Glyph[] | undefined
+ let start = 0
+ for (let i = 0; i <= glyphs.length; i++) {
+ if (i < glyphs.length && !BREAKS_AFTER.test(glyphs[i].text)) continue
+ if (i - start > LONG_WORD && this.width(glyphs.slice(start, i), ctx) > limit) {
+ out ??= glyphs.slice(0, start)
+ let width = 0
+ for (let k = start; k < i; k++) {
+ const g = glyphs[k]
+ const advance = this.advance(g, ctx)
+ if (width > 0 && width + advance > limit && !MARK.test(g.text)) {
+ out.push({ ...glyphs[k - 1], text: '\n', cp: 0x0a })
+ width = 0
+ }
+ out.push(g)
+ width += advance
+ }
+ } else if (out) {
+ for (let k = start; k < i; k++) out.push(glyphs[k])
+ }
+ if (out && i < glyphs.length) out.push(glyphs[i])
+ start = i + 1
+ }
+ return out ?? glyphs
+ }
+
+ /**
+ * Line ranges of one paragraph, broken at spaces to fit the context's width.
+ * A word wider than the line is broken wherever it fills one.
+ */
+ private breakLines(glyphs: Glyph[], ctx: TypesetContext): Array<[number, number]> {
+ const limit = ctx.width * LINE_FILL
+ const lines: Array<[number, number]> = []
+ let lineStart = 0
+ let lineWidth = 0
+ let i = 0
+ while (i < glyphs.length) {
+ let end = i
+ while (end < glyphs.length && glyphs[end].text !== ' ') end++
+ let wordWidth = this.width(glyphs.slice(i, end), ctx)
+ if (wordWidth > limit) {
+ if (lineWidth > 0) {
+ lines.push([lineStart, i])
+ lineStart = i
+ }
+ wordWidth = 0
+ for (let k = i; k < end; k++) {
+ const advance = this.advance(glyphs[k], ctx)
+ if (wordWidth > 0 && wordWidth + advance > limit && !MARK.test(glyphs[k].text)) {
+ lines.push([lineStart, k])
+ lineStart = k
+ wordWidth = 0
+ }
+ wordWidth += advance
+ }
+ lineWidth = 0
+ } else if (lineWidth > 0 && lineWidth + wordWidth > limit) {
+ lines.push([lineStart, i])
+ lineStart = i
+ lineWidth = 0
+ }
+ let next = end
+ while (next < glyphs.length && glyphs[next].text === ' ') next++
+ lineWidth += wordWidth + this.width(glyphs.slice(end, next), ctx)
+ i = next
+ }
+ lines.push([lineStart, glyphs.length])
+ return lines
+ }
+
+ private width(glyphs: Glyph[], ctx: TypesetContext): number {
+ let total = 0
+ for (const run of this.runs(glyphs, ctx)) {
+ const size = typeof run.fontSize === 'number' ? run.fontSize : ctx.fontSize
+ const bold = typeof run.bold === 'boolean' ? run.bold : ctx.bold
+ const family = typeof run.font === 'string' ? run.font : ctx.font
+ total += this.glyphs.measure(String(run.text), family, size, bold)
+ }
+ return total
+ }
+
+ /** Advance width of one glyph, measured once per face, size and weight. */
+ private advance(g: Glyph, ctx: TypesetContext): number {
+ const size = typeof g.props.fontSize === 'number' ? g.props.fontSize : ctx.fontSize
+ const bold = typeof g.props.bold === 'boolean' ? g.props.bold : ctx.bold
+ const key = `${g.family}\u0000${size}\u0000${bold}\u0000${g.text}`
+ let advance = this.advances.get(key)
+ if (advance === undefined) {
+ advance = this.glyphs.measure(g.text, g.family, size, bold)
+ this.advances.set(key, advance)
+ }
+ return advance
+ }
+
+ /** One line in display order, brackets mirrored where the text runs right to left. */
+ private displayOrder(glyphs: Glyph[], bidi: BidiParagraph, from: number, to: number): Glyph[] {
+ return visualOrder(bidi, from, to).map(at => ({
+ ...glyphs[at],
+ text: mirrored(glyphs[at].text, bidi.levels[at]),
+ }))
+ }
+
+ /** The line height a run needs to keep its face within the line bounds, if it differs. */
+ private lineHeightFor(family: string, props: Props, ctx: TypesetContext): number | undefined {
+ const em = this.glyphs.metrics(family)?.lineHeight
+ if (!em) return undefined
+ const cap = Math.max(MIN_LINE_EM, (this.bodyEm ?? em) * MAX_LINE_EM_OVER_BODY)
+ const target = Math.min(Math.max(em, MIN_LINE_EM), cap)
+ if (target === em) return undefined
+ const inherited = typeof props.lineHeight === 'number' ? props.lineHeight : ctx.lineHeight
+ return (inherited * target) / em
+ }
+
+ /**
+ * Adjacent glyphs sharing a face and properties, merged into pdfmake runs,
+ * without mixing left-to-right and right-to-left letters in one run. A run
+ * fontkit sets right to left is reversed by fontkit once shaped, so it is
+ * handed over in reverse display order.
+ */
+ private runs(glyphs: Glyph[], ctx: TypesetContext): Props[] {
+ const runs: Props[] = []
+ let current:
+ | { glyphs: Glyph[]; family: string; props: Props; direction?: 'L' | 'R' }
+ | undefined
+ const flush = (): void => {
+ if (!current) return
+ const reversed = current.direction === 'R' && this.glyphs.reversesRtl(current.family)
+ const ordered = reversed ? [...current.glyphs].reverse() : current.glyphs
+ const run: Props = { ...current.props, text: ordered.map(g => g.text).join('') }
+ if (current.family !== ctx.font || current.props.font !== undefined) run.font = current.family
+ const lineHeight = this.lineHeightFor(current.family, current.props, ctx)
+ if (lineHeight !== undefined) run.lineHeight = lineHeight
+ this.families.add(current.family)
+ runs.push(run)
+ current = undefined
+ }
+ for (const g of glyphs) {
+ const direction = scriptDirection(g.text)
+ if (
+ current &&
+ current.family === g.family &&
+ current.props === g.props &&
+ (!direction || !current.direction || direction === current.direction)
+ ) {
+ current.glyphs.push(g)
+ current.direction ??= direction
+ } else {
+ flush()
+ current = { glyphs: [g], family: g.family, props: g.props, direction }
+ }
+ }
+ flush()
+ return runs
+ }
+}