From 27784f70ac8a706712bd5726c9a72088ba38eb5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Mon, 16 Nov 2026 13:14:40 +0100 Subject: [PATCH] :bug: Fix custom CJK fonts losing glyphs when converted to TTF (#13074) FontForge opens a CID-keyed OpenType font as several sub-fonts, and Generate() writes only the active one. Most CJK fonts use this layout, so the TTF used by the render-wasm renderer kept only a few hundred glyphs (the Latin sub-font) and lost its family name. Merge the sub-fonts with CIDFlatten() before generating, both in the backend and in the media-processor. The call is guarded because it fails on fonts that are not CID-keyed. Add a small synthetic CID-keyed font with two sub-fonts as a fixture and check that the converted TTF keeps all its glyphs. Closes #10976 AI-assisted-by: claude-opus-5-5 --- backend/src/app/media/local.clj | 6 ++++- backend/test/backend_tests/media_test.clj | 25 +++++++++++++++++- .../backend_tests/test_files/font-cid.otf | Bin 0 -> 896 bytes media-processor/src/services/font.ts | 7 ++--- media-processor/test/fixtures/font-cid.otf | Bin 0 -> 896 bytes media-processor/test/font.test.ts | 23 +++++++++++++++- 6 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 backend/test/backend_tests/test_files/font-cid.otf create mode 100644 media-processor/test/fixtures/font-cid.otf diff --git a/backend/src/app/media/local.clj b/backend/src/app/media/local.clj index f3811e83aa..6cd9b0c866 100644 --- a/backend/src/app/media/local.clj +++ b/backend/src/app/media/local.clj @@ -241,8 +241,12 @@ foutput (fs/path (str finput ".ttf"))] (try (io/write* finput data) + ;; CID-keyed fonts (common in CJK) load as several sub-fonts and + ;; Generate only writes the active one unless they are merged. (let [res (exec-font! system ["fontforge" "-lang=ff" "-c" - (str/fmt "Open('%s'); Generate('%s')" + (str/fmt (str "Open('%s'); " + "if ($cidfontname != \"\"); CIDFlatten(); endif; " + "Generate('%s')") (str finput) (str foutput))])] (when (zero? (:exit res)) diff --git a/backend/test/backend_tests/media_test.clj b/backend/test/backend_tests/media_test.clj index ddc8c6ca3d..aac2cd6c98 100644 --- a/backend/test/backend_tests/media_test.clj +++ b/backend/test/backend_tests/media_test.clj @@ -12,7 +12,10 @@ [backend-tests.helpers :as th] [clojure.test :as t] [cuerdas.core :as str] - [datoteka.fs :as fs])) + [datoteka.fs :as fs] + [datoteka.io :as io]) + (:import + java.nio.ByteBuffer)) (t/use-fixtures :once th/state-init) @@ -554,3 +557,23 @@ :height 200})] (t/is (= :webp (:format thumb))) (t/is (= "image/webp" (:mtype thumb)))))) + +(defn- sfnt-num-glyphs + "Returns numGlyphs from the maxp table of the sfnt font in `data`." + [^bytes data] + (let [buf (ByteBuffer/wrap data) + num-tables (.getShort buf 4)] + (some (fn [i] + (let [rec (+ 12 (* i 16))] + (when (= "maxp" (String. data rec 4 "ISO-8859-1")) + (.getShort buf (+ (.getInt buf (+ rec 8)) 4))))) + (range num-tables)))) + +(t/deftest generate-fonts-cid-keyed-otf + (t/testing "otf->ttf keeps the glyphs of every CID sub-font" + ;; font-cid.otf has 5 glyphs split across two CID sub-fonts + (let [data (io/read* (io/resource "backend_tests/test_files/font-cid.otf")) + result (media/run th/*system* {:cmd :generate-fonts + :input {"font/otf" data}}) + ttf (io/read* (get result "font/ttf"))] + (t/is (<= 5 (sfnt-num-glyphs ttf)))))) diff --git a/backend/test/backend_tests/test_files/font-cid.otf b/backend/test/backend_tests/test_files/font-cid.otf new file mode 100644 index 0000000000000000000000000000000000000000..d9fc9d18ca9e781e08d2f416bd532b5330c1ebb4 GIT binary patch literal 896 zcmZ`%O=uHQ5T3W2A6vGGN?jCdTx}vM1Y?jQsKl5U2nMpWIf#gMHA_=SHcPXW2t5?( zLChsk@Z{CIhg_@a&07wN=*5E#L|m4L#fuAlu4GL&VP2HZo_7(LO!JB%$VW1Bf7sVpEvXK5# z*<61ZzW$SVe#gmvmtIVcd=k|+cnO>*&WXZshSvwrvF=ck%KXOg2ZqW09q7XkjGC+s zSSO#C9sBzGL)w*&4Vw|Dpg6L73etAaHcu!RCc|O zuEI~!A>=r8ek+kp!dZ)5+c?qZmZQlZg>1`;jDQhY^H^nxhX`=!V&T_76`>|-b48B= zUd^#W@q%J3YvWUQ6ism^&v@^Za13`2$*B{uWU9ZmC0bEi{v8d3`fEQ#vun%0x#L|m4L#fuAlu4GL&VP2HZo_7(LO!JB%$VW1Bf7sVpEvXK5# z*<61ZzW$SVe#gmvmtIVcd=k|+cnO>*&WXZshSvwrvF=ck%KXOg2ZqW09q7XkjGC+s zSSO#C9sBzGL)w*&4Vw|Dpg6L73etAaHcu!RCc|O zuEI~!A>=r8ek+kp!dZ)5+c?qZmZQlZg>1`;jDQhY^H^nxhX`=!V&T_76`>|-b48B= zUd^#W@q%J3YvWUQ6ism^&v@^Za13`2$*B{uWU9ZmC0bEi{v8d3`fEQ#vun%0x { - [ttfData, otfData, woffData, woff2Data] = await Promise.all([ + [ttfData, otfData, woffData, woff2Data, cidOtfData] = await Promise.all([ readFile(join(FIXTURES, "font-1.ttf")), readFile(join(FIXTURES, "font-1.otf")), readFile(join(FIXTURES, "font-1.woff")), readFile(join(FIXTURES, "font-1.woff2")), + readFile(join(FIXTURES, "font-cid.otf")), ]); }); @@ -58,6 +72,13 @@ describe("convertFont", () => { expect(result).toBeInstanceOf(Buffer); expect(result!.length).toBeGreaterThan(0); }); + + it("otf→ttf keeps the glyphs of every CID sub-font", async () => { + // font-cid.otf has 5 glyphs split across two CID sub-fonts + const result = await convertFont(cidOtfData, "font/otf", "font/ttf"); + expect(result!.subarray(0, 4).toString("hex")).toBe("00010000"); + expect(sfntNumGlyphs(result!)).toBeGreaterThanOrEqual(5); + }); }); describe("sourceType=woff", () => {