1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Represent cid chars using integers, not strings.

cid chars are 16-bit unsigned integers. Currently we convert them to
single-char strings when inserting them into the CMap, and then convert
them back to integers when extracting them from the CMap. This patch
changes CMap so that cid chars stay in integer format throughout, saving
both time and space.

When loading the PDF from issue #4580, this change reduces peak RSS from
~600 to ~370 MiB. It also improves overall speed on that PDF by ~26%,
going from 724 ms to 533 ms.
This commit is contained in:
Nicholas Nethercote 2014-07-31 23:46:37 -07:00
parent ad2ea78280
commit adf58ed687
3 changed files with 46 additions and 37 deletions

View file

@ -3899,8 +3899,7 @@ var Font = (function FontClosure() {
var cidToGidMap = properties.cidToGidMap || [];
var cidToGidMapLength = cidToGidMap.length;
properties.cMap.forEach(function(charCode, cid) {
assert(cid.length === 1, 'Max size of CID is 65,535');
cid = cid.charCodeAt(0);
assert(cid <= 0xffff, 'Max size of CID is 65,535');
var glyphId = -1;
if (cidToGidMapLength === 0) {
glyphId = charCode;
@ -4370,10 +4369,10 @@ var Font = (function FontClosure() {
var cMap = properties.cMap;
toUnicode = [];
cMap.forEach(function(charcode, cid) {
assert(cid.length === 1, 'Max size of CID is 65,535');
assert(cid <= 0xffff, 'Max size of CID is 65,535');
// e) Map the CID obtained in step (a) according to the CMap obtained
// in step (d), producing a Unicode value.
var ucs2 = ucs2CMap.lookup(cid.charCodeAt(0));
var ucs2 = ucs2CMap.lookup(cid);
if (ucs2) {
toUnicode[charcode] =
String.fromCharCode((ucs2.charCodeAt(0) << 8) +
@ -4415,7 +4414,7 @@ var Font = (function FontClosure() {
var charcode = 0;
if (this.composite) {
if (this.cMap.contains(glyphUnicode)) {
charcode = this.cMap.lookup(glyphUnicode).charCodeAt(0);
charcode = this.cMap.lookup(glyphUnicode);
}
}
// ... via toUnicode map
@ -4444,7 +4443,7 @@ var Font = (function FontClosure() {
var widthCode = charcode;
if (this.cMap && this.cMap.contains(charcode)) {
widthCode = this.cMap.lookup(charcode).charCodeAt(0);
widthCode = this.cMap.lookup(charcode);
}
width = this.widths[widthCode];
width = isNum(width) ? width : this.defaultWidth;
@ -5626,8 +5625,8 @@ var CFFFont = (function CFFFontClosure() {
// If the font is actually a CID font then we should use the charset
// to map CIDs to GIDs.
for (glyphId = 0; glyphId < charsets.length; glyphId++) {
var cidString = String.fromCharCode(charsets[glyphId]);
var charCode = properties.cMap.charCodeOf(cidString);
var cid = charsets[glyphId];
var charCode = properties.cMap.charCodeOf(cid);
charCodeToGlyphId[charCode] = glyphId;
}
} else {