From 634ce3c163f268290bba81b55be6a8a7cc1a0082 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 14 Apr 2026 12:18:56 +0200 Subject: [PATCH] Convert the return value in `createCmapTable` and `createNameTable` to a TypedArray Compared to the other TrueType table building functions, see previous patches, these ones are not trivial to convert to use TypedArrays properly. However, in order to simplify the `OpenTypeFileBuilder` implementation a little bit we can at least have these functions return TypedArray data. --- src/core/fonts.js | 16 ++++++++-------- src/core/opentype_file_builder.js | 10 +--------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/core/fonts.js b/src/core/fonts.js index 2f9533966..27bcd9fe4 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -21,6 +21,7 @@ import { info, shadow, string32, + stringToBytes, warn, } from "../shared/util.js"; import { CFFCompiler, CFFParser } from "./cff_parser.js"; @@ -741,13 +742,13 @@ function createCmapTable(glyphs, toUnicodeExtraMap, numGlyphs) { string32(format31012.length / 12); // nGroups } - return ( + return stringToBytes( cmap + - "\x00\x04" + // format - string16(format314.length + 4) + // length - format314 + - header31012 + - format31012 + "\x00\x04" + // format + string16(format314.length + 4) + // length + format314 + + header31012 + + format31012 ); } @@ -995,8 +996,7 @@ function createNameTable(name, proto) { } } - nameTable += strings.join("") + stringsUnicode.join(""); - return nameTable; + return stringToBytes(nameTable + strings.join("") + stringsUnicode.join("")); } /** diff --git a/src/core/opentype_file_builder.js b/src/core/opentype_file_builder.js index 9d8b1f625..c7d4f6b6c 100644 --- a/src/core/opentype_file_builder.js +++ b/src/core/opentype_file_builder.js @@ -63,15 +63,7 @@ class OpenTypeFileBuilder { // write the table data first (mostly for checksum) for (let i = 0; i < numTables; i++) { const table = tables.get(tablesNames[i]); - let tableOffset = tableOffsets[i]; - - if (table instanceof Uint8Array) { - file.set(table, tableOffset); - } else if (typeof table === "string") { - for (let j = 0, jj = table.length; j < jj; j++) { - file[tableOffset++] = table.charCodeAt(j) & 0xff; - } - } + file.set(table, tableOffsets[i]); } // sfnt version (4 bytes)