Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor(mcp-host): measure DOCX and slide text through one body-face measure
marcotebc0824cf9b637
diff --git a/mcp-host/src/workflow/docxTable.ts b/mcp-host/src/workflow/docxTable.tsindex f57febdc8..96842a30b 100644--- a/mcp-host/src/workflow/docxTable.ts+++ b/mcp-host/src/workflow/docxTable.ts@@ -1,4 +1,3 @@-import { type SKRSContext2D, createCanvas } from '@napi-rs/canvas'import {BorderStyle,Paragraph,@@ -17,7 +16,7 @@ import {type DocxPalette,docxHex,} from './docxStyle'-import { CHART_FONT_FAMILY, ensureFontsReady } from './fonts'+import { CJK_WIDE_CHAR, bodyFaceEm } from './fonts'import { inlineSpans } from './inlineMarkup'import { columnNames, headerText } from './tableRows'@@ -50,34 +49,24 @@ const LONG_TOKEN_TWIPS = 2800/** Added to every column: Word wraps a word that fills its cell exactly. */const SLACK_TWIPS = 40-/**- * CJK characters, counted one em wide as every CJK face sets them, and the- * characters Roboto has no glyph for, counted at a generous 0.6 em, so neither- * depends on the faces the runtime image happens to ship.- */-const WIDE_CHAR =- /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3000-\u303F\uFF01-\uFF60\uFFE0-\uFFE6]/u+/** Characters Roboto has a glyph for; any other is counted, not measured. */const MEASURED_CHAR = /[\u0000-\u052F\u1E00-\u206F\u20A0-\u20BF]/u-let context: SKRSContext2D | undefinedconst advances = [new Map<string, number>(), new Map<string, number>()]/*** Advance of one character at 1pt in ems. Characters in MEASURED_CHAR are* measured once each in Roboto, which runs about 7-10% wider than Calibri* (Medium against Bold included), so a sum of advances errs toward fitting.+ * CJK counts one em and the rest a generous 0.6, so neither depends on the+ * faces the runtime image happens to ship.*/function advanceEm(ch: string, bold: boolean): number {- if (!MEASURED_CHAR.test(ch)) return WIDE_CHAR.test(ch) ? 1 : 0.6+ if (!MEASURED_CHAR.test(ch)) return CJK_WIDE_CHAR.test(ch) ? 1 : 0.6const cache = advances[bold ? 1 : 0]const known = cache.get(ch)if (known !== undefined) return known- if (!context) {- ensureFontsReady()- context = createCanvas(4, 4).getContext('2d')- }- context.font = `100px "${bold ? `${CHART_FONT_FAMILY} Bold` : CHART_FONT_FAMILY}"`- const em = context.measureText(ch).width / 100+ const em = bodyFaceEm(ch, bold)cache.set(ch, em)return em}diff --git a/mcp-host/src/workflow/fonts.ts b/mcp-host/src/workflow/fonts.tsindex 4dba6a2ce..881218108 100644--- a/mcp-host/src/workflow/fonts.ts+++ b/mcp-host/src/workflow/fonts.ts@@ -133,6 +133,28 @@ function errorText(err: unknown): string {return err instanceof Error ? err.message : String(err)}+/**+ * CJK characters, which every CJK face sets one em wide. Text measures count+ * them so rather than measure them, since the image may have no CJK face.+ */+export const CJK_WIDE_CHAR =+ /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3000-\u303F\uFF01-\uFF60\uFFE0-\uFFE6]/u++let measuring: SKRSContext2D | undefined++/**+ * Width of `text` in ems of the bundled body face (CHART_FONT_FAMILY), as the+ * canvas sets it. Each document format scales it to the face it is shown in.+ */+export function bodyFaceEm(text: string, bold: boolean): number {+ if (!measuring) {+ ensureFontsReady()+ measuring = createCanvas(4, 4).getContext('2d')+ }+ measuring.font = `100px "${bold ? `${CHART_FONT_FAMILY} Bold` : CHART_FONT_FAMILY}"`+ return measuring.measureText(text).width / 100+}+/*** Families a PDF can be built from, widest coverage first.*diff --git a/mcp-host/src/workflow/pptxText.ts b/mcp-host/src/workflow/pptxText.tsindex 069694e84..4cb085209 100644--- a/mcp-host/src/workflow/pptxText.ts+++ b/mcp-host/src/workflow/pptxText.ts@@ -4,9 +4,8 @@* another slide, so the generator lays text out itself. It measures with the* Roboto faces fonts.ts registers, within a few percent of Arial, the deck font.*/-import { type SKRSContext2D, createCanvas } from '@napi-rs/canvas'import { isRtlText } from './docxScript'-import { CHART_FONT_FAMILY, ensureFontsReady } from './fonts'+import { CJK_WIDE_CHAR, bodyFaceEm } from './fonts'/** The one family the deck uses, for slides and charts alike. */export const PPTX_FONT_FACE = 'Arial'@@ -27,29 +26,12 @@ const WIDE_LINE_HEIGHT = 1.35export const TEXT_INSET_X = 0.2export const TEXT_INSET_Y = 0.1-/**- * CJK characters, counted one em wide: every CJK face sets them so, and the- * runtime image may have no CJK face to measure them with.- */-const WIDE_CHAR =- /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3000-\u303F\uFF01-\uFF60\uFFE0-\uFFE6]/u-/** Runs of spaces, single CJK characters, and words: the points where a line may break. */const BREAK_TOKENS = new RegExp(- `\\s+|${WIDE_CHAR.source}|[^\\s]+?(?=\\s|${WIDE_CHAR.source}|$)`,+ `\\s+|${CJK_WIDE_CHAR.source}|[^\\s]+?(?=\\s|${CJK_WIDE_CHAR.source}|$)`,'gu')-let context: SKRSContext2D | undefined--function canvasContext(): SKRSContext2D {- if (!context) {- ensureFontsReady()- context = createCanvas(4, 4).getContext('2d')- }- return context-}-const widthCache = new Map<string, number>()/** Longest text whose width is kept, and how many widths are kept at once. */const CACHED_TEXT_LENGTH = 64@@ -60,15 +42,13 @@ function widthEm(text: string, bold: boolean): number {const key = `${bold ? 'b' : 'r'}${text}`const cached = widthCache.get(key)if (cached !== undefined) return cached- const ctx = canvasContext()- ctx.font = `100px "${bold ? `${CHART_FONT_FAMILY} Bold` : CHART_FONT_FAMILY}"`let wide = 0let narrow = ''for (const ch of text) {- if (WIDE_CHAR.test(ch)) wide+++ if (CJK_WIDE_CHAR.test(ch)) wide++else narrow += ch}- const measured = narrow ? ctx.measureText(narrow).width / 100 : 0+ const measured = narrow ? bodyFaceEm(narrow, bold) : 0const em = measured * (bold ? BOLD_WIDTH_FACTOR : REGULAR_WIDTH_FACTOR) + wide// Only short texts repeat (words, labels); a whole cell is measured once.if (text.length <= CACHED_TEXT_LENGTH) {@@ -143,7 +123,7 @@ export function wrapLines(text: string, sizePt: number, widthIn: number, bold =}export function lineHeightIn(sizePt: number, text: string): number {- return ((WIDE_CHAR.test(text) ? WIDE_LINE_HEIGHT : LINE_HEIGHT) * sizePt) / 72+ return ((CJK_WIDE_CHAR.test(text) ? WIDE_LINE_HEIGHT : LINE_HEIGHT) * sizePt) / 72}/** Height, in inches, of `paragraphs` set in a box `widthIn` wide, without the box insets. */