fix: add special hyphenation callback for Chinese script in PDF font registration

This commit is contained in:
Amruth Pillai
2026-05-10 23:54:37 +02:00
parent 02973a1eb1
commit 83a407bc10
3 changed files with 8 additions and 0 deletions
+1
View File
@@ -58,6 +58,7 @@ export default defineConfig({
server: {
host: true,
strictPort: true,
port: Number.parseInt(process.env.PORT ?? "3000", 10),
},
@@ -44,6 +44,7 @@ describe("registerFonts", () => {
it("registers CJK PDF fallbacks for normal and italic text styles", async () => {
const registerSpy = vi.spyOn(Font, "register").mockImplementation(() => {});
vi.spyOn(Font, "registerHyphenationCallback").mockImplementation(() => {});
const cjkFallbackSource = getWebFontSource("Noto Serif SC", "400", false);
const { registerFonts } = await import("./use-register-fonts");
@@ -71,6 +72,7 @@ describe("registerFonts", () => {
it("uses the full CJK font source for synthetic italic variants when the CJK font is primary", async () => {
const registerSpy = vi.spyOn(Font, "register").mockImplementation(() => {});
vi.spyOn(Font, "registerHyphenationCallback").mockImplementation(() => {});
const cjkFallbackSource = getWebFontSource("Noto Serif SC", "400", false);
const { registerFonts } = await import("./use-register-fonts");
@@ -65,6 +65,11 @@ const resolvePdfTypography = (typography: Typography): Typography => {
};
export const registerFonts = (typography: Typography): PdfTypography => {
Font.registerHyphenationCallback((word) => {
if (word.match(/\p{Script=Han}/u)) return word.split("").flatMap((l) => [l, ""]);
return [word];
});
const pdfTypography = resolvePdfTypography(typography);
const bodyFontFamily = pdfTypography.body.fontFamily;
const headingFontFamily = pdfTypography.heading.fontFamily;