1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

Improve performance when reading very large TrueType "cmap" tables (issue 19319)

In the affected font the total number of mapping-entries is `1142348`, and no less than `997473` of them are duplicates.
Given that every duplicate causes a lot of Array elements to be moved this becomes extremely inefficient, which we can avoid by keeping track of seen `charCode`s and directly build the final mappings-Array instead.
This commit is contained in:
Jonas Jenwald 2025-01-13 13:03:59 +01:00
parent e1b972aac3
commit 5e569cade5
3 changed files with 21 additions and 5 deletions

View file

@ -1760,17 +1760,22 @@ class Font {
mappings.sort(function (a, b) {
return a.charCode - b.charCode;
});
for (let i = 1; i < mappings.length; i++) {
if (mappings[i - 1].charCode === mappings[i].charCode) {
mappings.splice(i, 1);
i--;
const finalMappings = [],
seenCharCodes = new Set();
for (const map of mappings) {
const { charCode } = map;
if (seenCharCodes.has(charCode)) {
continue;
}
seenCharCodes.add(charCode);
finalMappings.push(map);
}
return {
platformId: potentialTable.platformId,
encodingId: potentialTable.encodingId,
mappings,
mappings: finalMappings,
hasShortCmap,
};
}