Use a TypedArray when building the "head" TrueType table

This commit is contained in:
Jonas Jenwald 2026-04-13 12:35:39 +02:00
parent 5096982cfc
commit 2437f5f961

View File

@ -312,22 +312,6 @@ function string16(value) {
return String.fromCharCode((value >> 8) & 0xff, value & 0xff); return String.fromCharCode((value >> 8) & 0xff, value & 0xff);
} }
function safeString16(value) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert(
typeof value === "number" && !Number.isNaN(value),
`safeString16: Unexpected input "${value}".`
);
}
// clamp value to the 16-bit int range
if (value > 0x7fff) {
value = 0x7fff;
} else if (value < -0x8000) {
value = -0x8000;
}
return String.fromCharCode((value >> 8) & 0xff, value & 0xff);
}
function setArray(data, pos, arr) { function setArray(data, pos, arr) {
data.set(arr, pos); data.set(arr, pos);
return pos + arr.length; return pos + arr.length;
@ -3286,23 +3270,32 @@ class Font {
// Font header // Font header
builder.addTable( builder.addTable(
"head", "head",
"\x00\x01\x00\x00" + // Version number (function fontTableHead() {
"\x00\x00\x10\x00" + // fontRevision const dateArr = [0x00, 0x00, 0x00, 0x00, 0x9e, 0x0b, 0x7e, 0x27];
"\x00\x00\x00\x00" + // checksumAdjustement const data = new Uint8Array(54),
"\x5F\x0F\x3C\xF5" + // magicNumber view = new DataView(data.buffer);
"\x00\x00" + // Flags let pos = 0;
safeString16(unitsPerEm) + // unitsPerEM
"\x00\x00\x00\x00\x9e\x0b\x7e\x27" + // creation date pos = setArray(data, pos, [0x00, 0x01, 0x00, 0x00]); // Version number
"\x00\x00\x00\x00\x9e\x0b\x7e\x27" + // modifification date pos = setArray(data, pos, [0x00, 0x00, 0x10, 0x00]); // fontRevision
"\x00\x00" + // xMin pos += 4; // checksumAdjustement, skip redundant "\x00\x00\x00\x00"
safeString16(properties.descent) + // yMin pos = setArray(data, pos, [0x5f, 0x0f, 0x3c, 0xf5]); // magicNumber
"\x0F\xFF" + // xMax pos += 2; // Flags, skip redundant "\x00\x00"
safeString16(properties.ascent) + // yMax pos = setSafeInt16(view, pos, unitsPerEm); // unitsPerEM
string16(properties.italicAngle ? 2 : 0) + // macStyle pos = setArray(data, pos, dateArr); // creation date
"\x00\x11" + // lowestRecPPEM pos = setArray(data, pos, dateArr); // modifification date
"\x00\x00" + // fontDirectionHint pos += 2; // xMin, skip redundant "\x00\x00"
"\x00\x00" + // indexToLocFormat pos = setSafeInt16(view, pos, properties.descent); // yMin
"\x00\x00" // glyphDataFormat pos = setArray(data, pos, [0x0f, 0xff]); // xMax
pos = setSafeInt16(view, pos, properties.ascent); // yMax
pos = setInt16(view, pos, properties.italicAngle ? 2 : 0); // macStyle
setArray(data, pos, [0x00, 0x11]); // lowestRecPPEM
// fontDirectionHint, skip redundant "\x00\x00"
// indexToLocFormat, skip redundant "\x00\x00"
// glyphDataFormat, skip redundant "\x00\x00"
return data;
})()
); );
// Horizontal header // Horizontal header