Skip to content

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

TypeScript

fix(mcp-host): give Traditional Chinese text its own glyph forms and language

marcoteb543443065fad
diff --git a/mcp-host/src/workflow/__tests__/fonts.test.ts b/mcp-host/src/workflow/__tests__/fonts.test.ts
index a09600023..a8e8dd5f2 100644
--- a/mcp-host/src/workflow/__tests__/fonts.test.ts
+++ b/mcp-host/src/workflow/__tests__/fonts.test.ts
@@ -190,8 +190,14 @@ describe('per-character fallback for PDFs', () => {
expect(process.env.CI, 'CI installs the Noto CJK collection').not.toBe('true')
return
}
- expect((glyphs.descriptors([family!], 'ja')[family!].normal as string[])[1]).toMatch(/CJKjp-/)
- expect((glyphs.descriptors([family!], 'ko')[family!].normal as string[])[1]).toMatch(/CJKkr-/)
+ const variant = (language: string) =>
+ (glyphs.descriptors([family!], language)[family!].normal as string[])[1]
+ expect(variant('ja-JP')).toMatch(/CJKjp-/)
+ expect(variant('ko-KR')).toMatch(/CJKkr-/)
+ expect(variant('zh-TW')).toMatch(/CJKtc-/)
+ expect(variant('zh-HK')).toMatch(/CJKhk-/)
+ expect(variant('zh-CN')).toMatch(/CJKsc-/)
+ expect(variant('toString')).toMatch(/CJKsc-/)
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'clerum-cjk-'))
try {
@@ -201,6 +207,12 @@ describe('per-character fallback for PDFs', () => {
const raw = fs.readFileSync(r.artifact!.path).toString('latin1')
expect(raw).toMatch(/NotoSansCJKjp-/)
expect(raw).not.toMatch(/NotoSansCJKsc-/)
+
+ const tc = await pdf.execute({ filename: 'tc.pdf', body: '這個季度的營收增長。' }, dir)
+ expect(tc.success, tc.error).toBe(true)
+ const tcRaw = fs.readFileSync(tc.artifact!.path).toString('latin1')
+ expect(tcRaw).toMatch(/NotoSansCJKtc-/)
+ expect(tcRaw).not.toMatch(/NotoSansCJKsc-/)
} finally {
fs.rmSync(dir, { recursive: true, force: true })
}
diff --git a/mcp-host/src/workflow/__tests__/internalTools.docxText.test.ts b/mcp-host/src/workflow/__tests__/internalTools.docxText.test.ts
index 7ae55ab6a..9a26af58f 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.docxText.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.docxText.test.ts
@@ -303,6 +303,22 @@ describe('generate_docx CJK text', () => {
expect(documentEastAsianScript('Plain English')).toBeUndefined()
})
+ it('tells Traditional Chinese, and Hong Kong text, from Simplified', () => {
+ expect(documentEastAsianScript('這個季度的營收增長\n市場份額繼續擴大')?.lang).toBe('zh-TW')
+ expect(documentEastAsianScript('呢個季度嘅營收增長咗')?.lang).toBe('zh-HK')
+ expect(documentEastAsianScript('这个季度的营收增长')?.lang).toBe('zh-CN')
+ // Characters both write the same way say nothing, so Simplified stays.
+ expect(documentEastAsianScript('中文名字')?.lang).toBe('zh-CN')
+ // Cantonese in Simplified text is not Hong Kong's.
+ expect(documentEastAsianScript('这个季度嘅营收')?.lang).toBe('zh-CN')
+ })
+
+ it('sets a Traditional Chinese document in the Traditional face and language', async () => {
+ const defaults = await docDefaults('# 季度報告\n\n這個季度的營收增長,市場份額繼續擴大。')
+ expect(defaults).toMatch(/<w:rFonts [^>]*w:eastAsia="Microsoft JhengHei"/)
+ expect(defaults).toMatch(/<w:lang [^>]*w:eastAsia="zh-TW"/)
+ })
+
it('gives Han characters alone the language of a Japanese document', async () => {
const body =
'# 売上概要\n\n今期の売上は前年比で増加しました。\n\n| 都市 | 売上 |\n|---|---|\n| 東京 | 120 |'
diff --git a/mcp-host/src/workflow/__tests__/internalTools.pptx.layout.test.ts b/mcp-host/src/workflow/__tests__/internalTools.pptx.layout.test.ts
index efc148317..e5169939e 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.pptx.layout.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.pptx.layout.test.ts
@@ -678,6 +678,20 @@ describe('clerum__generate_pptx — scripts other than Latin', () => {
expect(xml).toMatch(/lang="ko-KR"[^>]*>[\s\S]*?<a:t>한국어/)
})
+ it('tags a Traditional Chinese deck as Traditional', async () => {
+ const result = await generatePptx(
+ {
+ filename: 't.pptx',
+ slides: [{ layout: 'title-bullets', title: '季度報告', bullets: ['這個季度的營收增長'] }],
+ },
+ outputDir
+ )
+ expect(result.success, result.error).toBe(true)
+ const xml = slideXml(path.join(outputDir, 't.pptx'), 1)
+ expect(xml).toMatch(/lang="zh-TW"[^>]*>[\s\S]*?<a:t>季度報告<\/a:t>/)
+ expect(xml).not.toContain('zh-CN')
+ })
+
it('gives Han characters alone the language of the deck', async () => {
const result = await generatePptx(
{
diff --git a/mcp-host/src/workflow/docxScript.ts b/mcp-host/src/workflow/docxScript.ts
index edc43c659..15fdcaf03 100644
--- a/mcp-host/src/workflow/docxScript.ts
+++ b/mcp-host/src/workflow/docxScript.ts
@@ -102,6 +102,18 @@ export interface EastAsianScript {
const JAPANESE: EastAsianScript = { font: 'Yu Gothic', lang: 'ja-JP' }
const KOREAN: EastAsianScript = { font: 'Malgun Gothic', lang: 'ko-KR' }
const CHINESE: EastAsianScript = { font: 'Microsoft YaHei', lang: 'zh-CN' }
+const TRADITIONAL_CHINESE: EastAsianScript = { font: 'Microsoft JhengHei', lang: 'zh-TW' }
+const HONG_KONG_CHINESE: EastAsianScript = { font: 'Microsoft JhengHei', lang: 'zh-HK' }
+
+// Frequent characters written one way in Simplified Chinese and another in
+// Traditional, the two lists in the same order; neither form is Japanese kanji
+// that a line without kana could hold for both.
+const SIMPLIFIED_ONLY =
+ /[这们个说会国学时为与来对发从实动经过还进现么没样关长门问题应见业开东头车书买卖电话语让认识请读写观点体产务员达区级际华结统计设资价变报农习乐网码帮]/gu
+const TRADITIONAL_ONLY =
+ /[這們個說會國學時為與來對發從實動經過還進現麼沒樣關長門問題應見業開東頭車書買賣電話語讓認識請讀寫觀點體產務員達區級際華結統計設資價變報農習樂網碼幫]/gu
+/** Characters of written Cantonese, which Hong Kong text holds. */
+const CANTONESE = /[嘅咗唔喺冇嚟啲佢哋嘢嗰]/u
const CJK_CHAR = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/gu
@@ -120,18 +132,31 @@ export function eastAsianScript(text: string): EastAsianScript | undefined {
/**
* The East Asian script of a whole document: that of the lines holding most of
* its CJK characters, where a line of Han characters alone counts as Chinese.
+ * Chinese is Traditional when its lines hold more characters only Traditional
+ * writes than ones only Simplified writes, and Hong Kong's when they also hold
+ * written Cantonese; otherwise Simplified, as before.
*/
export function documentEastAsianScript(text: string): EastAsianScript | undefined {
const counts = new Map<EastAsianScript, number>()
+ let traditional = 0
+ let simplified = 0
+ let cantonese = false
for (const line of text.split('\n')) {
const cjk = line.match(CJK_CHAR)?.length ?? 0
if (cjk === 0) continue
const script = eastAsianScript(line) ?? CHINESE
counts.set(script, (counts.get(script) ?? 0) + cjk)
+ if (script !== CHINESE) continue
+ traditional += line.match(TRADITIONAL_ONLY)?.length ?? 0
+ simplified += line.match(SIMPLIFIED_ONLY)?.length ?? 0
+ cantonese ||= CANTONESE.test(line)
}
let best: EastAsianScript | undefined
for (const script of [JAPANESE, KOREAN, CHINESE]) {
if ((counts.get(script) ?? 0) > (best ? (counts.get(best) ?? 0) : 0)) best = script
}
+ if (best === CHINESE && traditional > simplified) {
+ return cantonese ? HONG_KONG_CHINESE : TRADITIONAL_CHINESE
+ }
return best
}
diff --git a/mcp-host/src/workflow/fonts.ts b/mcp-host/src/workflow/fonts.ts
index 56cb7be70..4dba6a2ce 100644
--- a/mcp-host/src/workflow/fonts.ts
+++ b/mcp-host/src/workflow/fonts.ts
@@ -16,6 +16,7 @@ import { GlobalFonts, type SKRSContext2D, createCanvas } from '@napi-rs/canvas'
import * as fs from 'fs'
import * as path from 'path'
import { logger } from '../logger'
+import { own } from './ownEntry'
/** Family the bundled Roboto is registered under. */
export const CHART_FONT_FAMILY = 'Clerum Sans'
@@ -565,6 +566,14 @@ function facesFromCollection(file: string, boldFile?: string): PdfFaces | undefi
return { normal, bold, italics: normal, bolditalics: bold }
}
+/** The CJK collection variant whose glyph forms the readers of a language expect. */
+const CJK_VARIANTS: Record<string, string> = {
+ 'ja-JP': 'jp',
+ 'ko-KR': 'kr',
+ 'zh-TW': 'tc',
+ 'zh-HK': 'hk',
+}
+
/**
* `faces` with each face addressed inside a CJK collection swapped for the
* collection's `variant` face of the same weight ("CJKsc-Bold" for
@@ -812,8 +821,9 @@ export interface PdfGlyphSource {
metrics(family: string): FaceMetrics | undefined
/**
* pdfmake descriptors for `families`, always including the body and mono
- * ones. With `language` 'ja' or 'ko', a face from a CJK collection is taken
- * in that language's variant, whose glyph forms its readers expect.
+ * ones. With `language` 'ja-JP', 'ko-KR', 'zh-TW' or 'zh-HK', a face from a
+ * CJK collection is taken in that language's variant, whose glyph forms its
+ * readers expect; Simplified Chinese is the collections' first variant.
*/
descriptors(families: Iterable<string>, language?: string): Record<string, PdfFaces>
}
@@ -1012,7 +1022,7 @@ class GlyphSource implements PdfGlyphSource {
const faces = this.facesOf(family)
if (faces) out[family] = faces
}
- const variant = language === 'ja' ? 'jp' : language === 'ko' ? 'kr' : undefined
+ const variant = language ? own(CJK_VARIANTS, language) : undefined
if (variant) {
for (const [family, faces] of Object.entries(out)) out[family] = inVariant(faces, variant)
}
diff --git a/mcp-host/src/workflow/pdfGenerator.ts b/mcp-host/src/workflow/pdfGenerator.ts
index 3d6e10cbc..4a5bd930d 100644
--- a/mcp-host/src/workflow/pdfGenerator.ts
+++ b/mcp-host/src/workflow/pdfGenerator.ts
@@ -1176,7 +1176,7 @@ export async function runGeneratePdf(
.filter(text => text !== undefined && text !== null)
.map(String)
.join('\n')
- )?.lang.slice(0, 2)
+ )?.lang
const printer = new PdfPrinter(glyphs.descriptors(typesetter.families, language))
const pdfDoc = printer.createPdfKitDocument(docDef)