Fix escapePDFName producing malformed name escapes for control characters

escapePDFName emitted a single hex digit for character codes below 0x10
(TAB became #9, not #09). PDF 32000-1 7.3.5 requires exactly two hex digits
after #. On re-save (annotations, form fields, font names) such a Name is
written malformed and the lexer mis-parses it on reload, dropping bytes.
Pad the hex to two digits; a no-op for codes 0x10 to 0xFF.
This commit is contained in:
Yarchik 2026-07-02 10:43:59 +01:00
parent 614349086b
commit 9710372a1b
2 changed files with 6 additions and 1 deletions

View File

@ -376,7 +376,7 @@ function escapePDFName(str) {
if (start < i) {
buffer.push(str.substring(start, i));
}
buffer.push(`#${char.toString(16)}`);
buffer.push(`#${char.toString(16).padStart(2, "0")}`);
start = i + 1;
}
}

View File

@ -290,6 +290,11 @@ describe("core_utils", function () {
"#23#28#29#3c#3e#5b#5d#7b#7d#2f#25"
);
});
it("should escape control characters using two hexadecimal digits", function () {
expect(escapePDFName("\x00\x09\x0a\x1f")).toEqual("#00#09#0a#1f");
expect(escapePDFName("a\tb")).toEqual("a#09b");
});
});
describe("escapeString", function () {