Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor(mcp-host): tell right-to-left text in slides with the DOCX helpers
marcoteb8a1a3050725e
diff --git a/mcp-host/src/workflow/__tests__/pptxText.test.ts b/mcp-host/src/workflow/__tests__/pptxText.test.tsnew file mode 100644index 000000000..e12cf0a6d--- /dev/null+++ b/mcp-host/src/workflow/__tests__/pptxText.test.ts@@ -0,0 +1,33 @@+/**+ * Language and direction tags for slide text, and the width measure that+ * lays it out.+ */+import { describe, expect, it } from 'vitest'+import { scriptOptions, textWidth } from '../pptxText'++describe('scriptOptions', () => {+ it('tags each script with its language', () => {+ expect(scriptOptions('ひらがな')).toEqual({ lang: 'ja-JP' })+ expect(scriptOptions('한국어')).toEqual({ lang: 'ko-KR' })+ expect(scriptOptions('营收')).toEqual({ lang: 'zh-CN' })+ expect(scriptOptions('營收', 'zh-TW')).toEqual({ lang: 'zh-TW' })+ expect(scriptOptions('Revenue')).toEqual({})+ })++ it('sets right to left from the first letter, in every right-to-left script', () => {+ expect(scriptOptions('مرحبا world')).toEqual({ lang: 'ar-SA', rtlMode: true })+ expect(scriptOptions('world مرحبا').rtlMode).toBeUndefined()+ // Syriac and Thaana read right to left too.+ expect(scriptOptions('ܫܠܡܐ').rtlMode).toBe(true)+ expect(scriptOptions('ދިވެހި').rtlMode).toBe(true)+ })+})++describe('textWidth', () => {+ it('grows with the size, and sets CJK one em wide', () => {+ const at10 = textWidth('Revenue', 10)+ expect(at10).toBeGreaterThan(0)+ expect(textWidth('Revenue', 20)).toBeCloseTo(at10 * 2, 5)+ expect(textWidth('売上', 72)).toBeCloseTo(2, 5)+ })+})diff --git a/mcp-host/src/workflow/docxScript.ts b/mcp-host/src/workflow/docxScript.tsindex 15fdcaf03..251daa120 100644--- a/mcp-host/src/workflow/docxScript.ts+++ b/mcp-host/src/workflow/docxScript.ts@@ -37,6 +37,11 @@ function span(letter: RegExp): string {}const RTL_SPAN = new RegExp(`${span(HEBREW_LETTER)}|${span(ARABIC_LETTER)}`, 'gu')+/** Whether `text` holds a letter of a right-to-left script anywhere. */+export function hasRtlLetter(text: string): boolean {+ return RTL_LETTER.test(text)+}+/** Whether `text` reads right to left: its first letter, as the bidi algorithm decides, is RTL. */export function isRtlText(text: string): boolean {const first = /\p{L}/u.exec(text)?.[0]diff --git a/mcp-host/src/workflow/pptxDeck.ts b/mcp-host/src/workflow/pptxDeck.tsindex d5e0942b3..75e834322 100644--- a/mcp-host/src/workflow/pptxDeck.ts+++ b/mcp-host/src/workflow/pptxDeck.ts@@ -7,7 +7,13 @@* values shrink to fit their boxes, and pictures keep their proportions.*/import JSZip from 'jszip'-import { bidiLanguage, documentEastAsianScript, isRtlText, scriptSegments } from './docxScript'+import {+ bidiLanguage,+ documentEastAsianScript,+ hasRtlLetter,+ isRtlText,+ scriptSegments,+} from './docxScript'import { fitImageSize } from './embeddedImages'import { own } from './ownEntry'import { nativeChartArgs } from './pptxCharts'@@ -341,7 +347,6 @@ function runOptions(slide: Slide, text: string, align?: unknown): Record<string,* eyebrow leaves it unspaced.*/const ARABIC_SCRIPT = /\p{Script=Arabic}/u-const RTL_LETTER = /[\p{Script=Arabic}\p{Script=Hebrew}]/utype TextRun = { text: string; options: Record<string, unknown> }@@ -366,7 +371,7 @@ function paragraphOptions(slide: Slide, text: string, align?: unknown): Record<sfunction addText(slide: Slide, text: string, box: Box, options: Record<string, unknown>): void {const upright = ARABIC_SCRIPT.test(text) ? { italic: false } : {}- if (!RTL_LETTER.test(text)) {+ if (!hasRtlLetter(text)) {slide.addText(text, {...box,...options,@@ -388,7 +393,7 @@ function bulletRuns(slide: Slide, items: string[]): TextRun[] {// into one paragraph whenever an alignment is set, as it is for RTL items.return items.flatMap(text => {const runs = text.split('\n').flatMap((line, i) => {- const lineRuns = RTL_LETTER.test(text)+ const lineRuns = hasRtlLetter(text)? directionRuns(line, paragraphOptions(slide, text)): [{ text: line, options: runOptions(slide, text) }]if (i > 0) lineRuns[0].options.softBreakBefore = true@@ -785,9 +790,7 @@ function drawTable(slide: Slide, planned: PlannedSlide, ctx: DrawContext): voidconst p = ctx.paletteconst data = rows.map((row, r) =>row.map(text => ({- text: RTL_LETTER.test(text)- ? directionRuns(text, paragraphOptions(slide, text, 'left'))- : text,+ text: hasRtlLetter(text) ? directionRuns(text, paragraphOptions(slide, text, 'left')) : text,options: {...(r === 0? { bold: true, color: 'FFFFFF', fill: { color: hex(p.primary) } }diff --git a/mcp-host/src/workflow/pptxText.ts b/mcp-host/src/workflow/pptxText.tsindex c8b77053a..069694e84 100644--- a/mcp-host/src/workflow/pptxText.ts+++ b/mcp-host/src/workflow/pptxText.ts@@ -5,6 +5,7 @@* 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'/** The one family the deck uses, for slides and charts alike. */@@ -230,8 +231,6 @@ export interface ScriptOptions {rtlMode?: boolean}-const RTL_SCRIPT = /[\p{Script=Arabic}\p{Script=Hebrew}]/u-/*** Language and direction for `text`. Untagged runs are en-US, and PowerPoint* then mixes CJK glyphs from several fallback fonts; without `rtl` an Arabic@@ -246,10 +245,8 @@ export function scriptOptions(text: string, hanLang = 'zh-CN'): ScriptOptions {else if (/\p{Script=Han}/u.test(text)) lang = hanLangelse if (/\p{Script=Arabic}/u.test(text)) lang = 'ar-SA'else if (/\p{Script=Hebrew}/u.test(text)) lang = 'he-IL'- const firstLetter = /\p{L}/u.exec(text)?.[0]- const rtl = firstLetter !== undefined && RTL_SCRIPT.test(firstLetter)const out: ScriptOptions = {}if (lang) out.lang = lang- if (rtl) out.rtlMode = true+ if (isRtlText(text)) out.rtlMode = truereturn out}