mirror of
https://github.com/penpot/penpot.git
synced 2026-11-18 08:01:21 +00:00
🐛 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
This commit is contained in:
parent
65b882d044
commit
27784f70ac
@ -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))
|
||||
|
||||
@ -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))))))
|
||||
|
||||
BIN
backend/test/backend_tests/test_files/font-cid.otf
Normal file
BIN
backend/test/backend_tests/test_files/font-cid.otf
Normal file
Binary file not shown.
@ -122,9 +122,10 @@ async function fontConvert(
|
||||
// FontForge's own lexer uses doubled single quotes for escaping.
|
||||
const escInput = inputPath.replace(/'/g, "''");
|
||||
const escOutput = outputPath.replace(/'/g, "''");
|
||||
await execCommand("fontforge", ["-lang=ff", "-c", `Open('${escInput}'); Generate('${escOutput}')`], undefined, {
|
||||
signal,
|
||||
});
|
||||
// CID-keyed fonts (common in CJK) load as several sub-fonts and Generate
|
||||
// only writes the active one unless they are merged first.
|
||||
const script = `Open('${escInput}'); if ($cidfontname != ""); CIDFlatten(); endif; Generate('${escOutput}')`;
|
||||
await execCommand("fontforge", ["-lang=ff", "-c", script], undefined, { signal });
|
||||
return await readFile(outputPath);
|
||||
} catch (err: unknown) {
|
||||
const error = err as NodeJS.ErrnoException & { killed?: boolean; signal?: string };
|
||||
|
||||
BIN
media-processor/test/fixtures/font-cid.otf
vendored
Normal file
BIN
media-processor/test/fixtures/font-cid.otf
vendored
Normal file
Binary file not shown.
@ -11,13 +11,27 @@ let ttfData: Buffer;
|
||||
let otfData: Buffer;
|
||||
let woffData: Buffer;
|
||||
let woff2Data: Buffer;
|
||||
let cidOtfData: Buffer;
|
||||
|
||||
// Returns numGlyphs from the maxp table of an sfnt font.
|
||||
function sfntNumGlyphs(data: Buffer): number {
|
||||
const numTables = data.readUInt16BE(4);
|
||||
for (let i = 0; i < numTables; i++) {
|
||||
const rec = 12 + i * 16;
|
||||
if (data.toString("latin1", rec, rec + 4) === "maxp") {
|
||||
return data.readUInt16BE(data.readUInt32BE(rec + 8) + 4);
|
||||
}
|
||||
}
|
||||
throw new Error("maxp table not found");
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
[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", () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user