Files
pdf.js/test/unit/char_code_map_spec.js
calixteman b4aa2afa17 Use CharCodeMap for font character mappings (bug 2077176)
Store CMap, ToUnicode and CIDToGIDMap entries in an Array for integer
codes in [0, 0xFFFF] and a Map for other codes. Track keys separately
to preserve insertion order without scanning array holes.

Cache reverse lookups after eight linear searches, preserving the
first matching code. This avoids repeated scans during CFF glyph
mapping and string encoding.
2026-10-06 21:56:07 +02:00

116 lines
3.2 KiB
JavaScript

/* Copyright 2026 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CharCodeMap } from "../../src/core/char_code_map.js";
describe("CharCodeMap", function () {
function getEntries(map) {
const entries = [];
map.forEach((code, value) => {
entries.push([code, value]);
});
return entries;
}
it("stores both dense and sparse codes", function () {
const map = new CharCodeMap();
map.set(0x20, "a");
map.set(0xffff, "b");
map.set(0x10000, "c");
map.set(0xffffffff, "d");
map.set(0x20, "e"); // Overwrite.
expect(map.size).toEqual(4);
expect(map.get(0x20)).toEqual("e");
expect(map.get(0xffff)).toEqual("b");
expect(map.get(0x10000)).toEqual("c");
expect(map.get(0xffffffff)).toEqual("d");
expect(map.has(0x21)).toEqual(false);
expect(map.has(0x10001)).toEqual(false);
expect(map.get(-1)).toBeUndefined();
expect(map.get(0.5)).toBeUndefined();
});
it("iterates in insertion order", function () {
const map = new CharCodeMap();
map.set(0x20000, "a");
map.set(0x41, "b");
map.set(0x10000, "c");
map.set(0x20, "d");
map.set(0x41, "e"); // Keep the original position.
expect(getEntries(map)).toEqual([
[0x20000, "a"],
[0x41, "e"],
[0x10000, "c"],
[0x20, "d"],
]);
});
it("finds the first code mapping to a value", function () {
const map = new CharCodeMap();
map.set(0x42, "a");
map.set(0x30000, "b");
map.set(0x41, "a");
map.set(0x20000, "b");
expect(map.charCodeOf("a")).toEqual(0x42);
expect(map.charCodeOf("b")).toEqual(0x30000);
expect(map.charCodeOf("c")).toEqual(-1);
});
it("finds the first code mapping to a value, after many lookups", function () {
const map = new CharCodeMap();
map.set(0x42, "a");
map.set(0x30000, "b");
map.set(0x41, "a");
// Build the reverse lookup cache.
for (let i = 0; i < 200; i++) {
expect(map.charCodeOf("a")).withContext(`lookup ${i}`).toEqual(0x42);
}
expect(map.charCodeOf("b")).toEqual(0x30000);
expect(map.charCodeOf("c")).toEqual(-1);
map.set(0x20, "a");
map.set(0x50, "c");
expect(map.charCodeOf("a")).toEqual(0x42);
expect(map.charCodeOf("c")).toEqual(0x50);
});
it("creates an independent copy", function () {
const map = new CharCodeMap();
map.set(0x41, "a");
map.set(0x10000, "b");
const copy = map.clone();
copy.set(0x42, "c");
copy.set(0x10001, "d");
expect(map.size).toEqual(2);
expect(copy.size).toEqual(4);
expect(getEntries(map)).toEqual([
[0x41, "a"],
[0x10000, "b"],
]);
expect(getEntries(copy)).toEqual([
[0x41, "a"],
[0x10000, "b"],
[0x42, "c"],
[0x10001, "d"],
]);
});
});