Skip to content

Commit

Permalink
Add IdentityToUnicodeMap class.
Browse files Browse the repository at this point in the history
When loading the PDF from issue mozilla#4935, this change reduces peak RSS from
~2400 to ~300 MiB, and improves overall speed by ~81%, from 6336 ms to
1222 ms.
  • Loading branch information
nnethercote committed Aug 8, 2014
1 parent 9576047 commit 6c8cca1
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,38 @@ var ToUnicodeMap = (function ToUnicodeMapClosure() {
return ToUnicodeMap;
})();

var IdentityToUnicodeMap = (function IdentityToUnicodeMapClosure() {
function IdentityToUnicodeMap(firstChar, lastChar) {
this.firstChar = firstChar;
this.lastChar = lastChar;
}

IdentityToUnicodeMap.prototype = {
get length() {
error('should not access .length');
},

forEach: function(callback) {
for (var i = this.firstChar, ii = this.lastChar; i <= ii; i++) {
callback(i, i);
}
},

get: function(i) {
if (this.firstChar <= i && i <= this.lastChar) {
return String.fromCharCode(i);
}
return undefined;
},

charCodeOf: function(v) {
error('should not call .charCodeOf');
}
};

return IdentityToUnicodeMap;
})();

/**
* 'Font' is the class the outside world should use, it encapsulate all the font
* decoding logics whatever type it is (assuming the font type is supported).
Expand Down Expand Up @@ -4453,13 +4485,9 @@ var Font = (function FontClosure() {
}

// The viewer's choice, just use an identity map.
toUnicode = [];
var firstChar = properties.firstChar, lastChar = properties.lastChar;
for (var i = firstChar; i <= lastChar; i++) {
toUnicode[i] = String.fromCharCode(i);
}
map.isIdentity = true;
map.toUnicode = new ToUnicodeMap(toUnicode);
map.toUnicode =
new IdentityToUnicodeMap(properties.firstChar, properties.lastChar);
return map;
},

Expand Down

0 comments on commit 6c8cca1

Please sign in to comment.