Skip to content

Commit 29fd87b

Browse files
Merge pull request #14118 from adventhp/cmap-format-2
Implement TrueType character map "format 2" (fixes #14117)
2 parents 3945965 + 586295f commit 29fd87b

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/core/fonts.js

+49
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,55 @@ class Font {
14971497
});
14981498
}
14991499
hasShortCmap = true;
1500+
} else if (format === 2) {
1501+
const subHeaderKeys = [];
1502+
let maxSubHeaderKey = 0;
1503+
// Read subHeaderKeys. If subHeaderKeys[i] === 0, then i is a
1504+
// single-byte character. Otherwise, i is the first byte of a
1505+
// multi-byte character, and the value is 8*index into
1506+
// subHeaders.
1507+
for (let i = 0; i < 256; i++) {
1508+
const subHeaderKey = file.getUint16() >> 3;
1509+
subHeaderKeys.push(subHeaderKey);
1510+
maxSubHeaderKey = Math.max(subHeaderKey, maxSubHeaderKey);
1511+
}
1512+
// Read subHeaders. The number of entries is determined
1513+
// dynamically based on the subHeaderKeys found above.
1514+
const subHeaders = [];
1515+
for (let i = 0; i <= maxSubHeaderKey; i++) {
1516+
subHeaders.push({
1517+
firstCode: file.getUint16(),
1518+
entryCount: file.getUint16(),
1519+
idDelta: signedInt16(file.getByte(), file.getByte()),
1520+
idRangePos: file.pos + file.getUint16(),
1521+
});
1522+
}
1523+
for (let i = 0; i < 256; i++) {
1524+
if (subHeaderKeys[i] === 0) {
1525+
// i is a single-byte code.
1526+
file.pos = subHeaders[0].idRangePos + 2 * i;
1527+
glyphId = file.getUint16();
1528+
mappings.push({
1529+
charCode: i,
1530+
glyphId,
1531+
});
1532+
} else {
1533+
// i is the first byte of a two-byte code.
1534+
const s = subHeaders[subHeaderKeys[i]];
1535+
for (j = 0; j < s.entryCount; j++) {
1536+
const charCode = (i << 8) + j + s.firstCode;
1537+
file.pos = s.idRangePos + 2 * j;
1538+
glyphId = file.getUint16();
1539+
if (glyphId !== 0) {
1540+
glyphId = (glyphId + s.idDelta) % 65536;
1541+
}
1542+
mappings.push({
1543+
charCode,
1544+
glyphId,
1545+
});
1546+
}
1547+
}
1548+
}
15001549
} else if (format === 4) {
15011550
// re-creating the table in format 4 since the encoding
15021551
// might be changed

test/pdfs/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@
288288
!PDFJS-7562-reduced.pdf
289289
!issue11768_reduced.pdf
290290
!issue5039.pdf
291+
!issue14117.pdf
291292
!issue5070.pdf
292293
!issue5238.pdf
293294
!issue5244.pdf

test/pdfs/issue14117.pdf

79.5 KB
Binary file not shown.

test/test_manifest.json

+6
Original file line numberDiff line numberDiff line change
@@ -5589,6 +5589,12 @@
55895589
"type": "eq",
55905590
"annotations": true
55915591
},
5592+
{ "id": "issue14117",
5593+
"file": "pdfs/issue14117.pdf",
5594+
"md5": "9b1c33ad2f59f4e723c258e863149abf",
5595+
"rounds": 1,
5596+
"type": "eq"
5597+
},
55925598
{ "id": "annotation-text-widget-annotations",
55935599
"file": "pdfs/annotation-text-widget.pdf",
55945600
"md5": "b7b8923a12998fca8603fae53f73f19b",

0 commit comments

Comments
 (0)