Skip to content

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

TypeScript

fix(mcp-host): set Japanese and Korean PDF text in their CJK variant

marcoteb59c6d23fa6ff
diff --git a/mcp-host/src/workflow/__tests__/fonts.test.ts b/mcp-host/src/workflow/__tests__/fonts.test.ts
index 91b27af01..8df1a8097 100644
--- a/mcp-host/src/workflow/__tests__/fonts.test.ts
+++ b/mcp-host/src/workflow/__tests__/fonts.test.ts
@@ -172,6 +172,28 @@ describe('per-character fallback for PDFs', () => {
if (family) expect(descriptors[family]).toBeDefined()
})
+ it('takes Han characters in the variant of the document language from a CJK collection', async () => {
+ const family = glyphs.familyFor(0x76f4, PDF_FONT_FAMILY)
+ const faces = family ? glyphs.descriptors([family])[family] : undefined
+ const face = faces?.normal
+ // Only an image with the Noto CJK collection can run this.
+ if (!Array.isArray(face) || !/CJKsc-/.test(face[1])) 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 dir = fs.mkdtempSync(path.join(os.tmpdir(), 'clerum-cjk-'))
+ try {
+ const pdf = INTERNAL_TOOLS.find(t => t.name === 'clerum__generate_pdf')!
+ const r = await pdf.execute({ filename: 'ja.pdf', body: '直角の骨は誤写です。ひらがな' }, dir)
+ expect(r.success, r.error).toBe(true)
+ const raw = fs.readFileSync(r.artifact!.path).toString('latin1')
+ expect(raw).toMatch(/NotoSansCJKjp-/)
+ expect(raw).not.toMatch(/NotoSansCJKsc-/)
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true })
+ }
+ })
+
it('measures the body face at the size asked for', () => {
const at10 = glyphs.measure('Revenue', PDF_FONT_FAMILY, 10, false)
expect(at10).toBeGreaterThan(0)
diff --git a/mcp-host/src/workflow/fonts.ts b/mcp-host/src/workflow/fonts.ts
index 67e748edd..106eb38a1 100644
--- a/mcp-host/src/workflow/fonts.ts
+++ b/mcp-host/src/workflow/fonts.ts
@@ -546,6 +546,26 @@ function facesFromCollection(file: string, boldFile?: string): PdfFaces | undefi
return { normal, bold, italics: normal, bolditalics: bold }
}
+/**
+ * `faces` with each face addressed inside a CJK collection swapped for the
+ * collection's `variant` face of the same weight ("CJKsc-Bold" for
+ * "CJKjp-Bold"), when the collection holds one. The variants share their
+ * characters and widths and differ in glyph forms.
+ */
+function inVariant(faces: PdfFaces, variant: string): PdfFaces {
+ const swap = (face: PdfFace): PdfFace => {
+ if (!Array.isArray(face) || !/CJK(?:sc|jp|kr|tc|hk)-/i.test(face[1])) return face
+ const name = face[1].replace(/CJK(?:sc|jp|kr|tc|hk)-/i, `CJK${variant}-`)
+ return collectionFaceNames(face[0]).includes(name) ? [face[0], name] : face
+ }
+ return {
+ normal: swap(faces.normal),
+ bold: swap(faces.bold),
+ italics: swap(faces.italics),
+ bolditalics: swap(faces.bolditalics),
+ }
+}
+
/** Collections on this image, keyed by lowercased basename. */
function embeddableCollections(): Map<string, string> {
if (collectionIndex) return collectionIndex
@@ -767,8 +787,12 @@ export interface PdfGlyphSource {
measure(text: string, family: string, size: number, bold: boolean): number
/** Vertical metrics of `family`, or undefined when unknown. */
metrics(family: string): FaceMetrics | undefined
- /** pdfmake descriptors for `families`, always including the body and mono ones. */
- descriptors(families: Iterable<string>): Record<string, PdfFaces>
+ /**
+ * 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.
+ */
+ descriptors(families: Iterable<string>, language?: string): Record<string, PdfFaces>
}
function faceFile(face: PdfFace): string | undefined {
@@ -929,12 +953,16 @@ class GlyphSource implements PdfGlyphSource {
return this.ctx.measureText(text).width
}
- descriptors(families: Iterable<string>): Record<string, PdfFaces> {
+ descriptors(families: Iterable<string>, language?: string): Record<string, PdfFaces> {
const out = pdfFontDescriptors()
for (const family of families) {
const faces = this.facesOf(family)
if (faces) out[family] = faces
}
+ const variant = language === 'ja' ? 'jp' : language === 'ko' ? 'kr' : undefined
+ if (variant) {
+ for (const [family, faces] of Object.entries(out)) out[family] = inVariant(faces, variant)
+ }
return out
}
}
diff --git a/mcp-host/src/workflow/internalTools.ts b/mcp-host/src/workflow/internalTools.ts
index 54317db1b..5759749cb 100644
--- a/mcp-host/src/workflow/internalTools.ts
+++ b/mcp-host/src/workflow/internalTools.ts
@@ -2887,7 +2887,20 @@ const generatePdf: InternalToolDefinition = {
content,
}
- const printer = new PdfPrinter(glyphs.descriptors(typesetter.families))
+ // Han characters take the glyph forms of the document's language.
+ const language = documentEastAsianScript(
+ [
+ title,
+ body,
+ branding.companyName,
+ branding.footerText,
+ ...tables.map(t => JSON.stringify([t.headers, t.rows])),
+ ]
+ .filter(text => text !== undefined && text !== null)
+ .map(String)
+ .join('\n')
+ )?.lang.slice(0, 2)
+ const printer = new PdfPrinter(glyphs.descriptors(typesetter.families, language))
const pdfDoc = printer.createPdfKitDocument(docDef)
const pdfBuffer: Buffer = await new Promise((resolve, reject) => {