Convert CMap and IdentityCMap to ES6 classes

Also changes `var` to `let`/`const` in code already touched in the patch.
This commit is contained in:
Jonas Jenwald 2018-07-08 22:41:52 +02:00
parent b773b356af
commit bf6d45f85a

View File

@ -14,8 +14,8 @@
*/ */
import { import {
CMapCompressionType, FormatError, isString, MissingDataException, CMapCompressionType, FormatError, isString, MissingDataException, unreachable,
unreachable, Util, warn warn
} from '../shared/util'; } from '../shared/util';
import { isCmd, isEOF, isName, isStream } from './primitives'; import { isCmd, isEOF, isName, isStream } from './primitives';
import { Lexer } from './parser'; import { Lexer } from './parser';
@ -194,8 +194,8 @@ var BUILT_IN_CMAPS = [
'WP-Symbol']; 'WP-Symbol'];
// CMap, not to be confused with TrueType's cmap. // CMap, not to be confused with TrueType's cmap.
var CMap = (function CMapClosure() { class CMap {
function CMap(builtInCMap) { constructor(builtInCMap = false) {
// Codespace ranges are stored as follows: // Codespace ranges are stored as follows:
// [[1BytePairs], [2BytePairs], [3BytePairs], [4BytePairs]] // [[1BytePairs], [2BytePairs], [3BytePairs], [4BytePairs]]
// where nBytePairs are ranges e.g. [low1, high1, low2, high2, ...] // where nBytePairs are ranges e.g. [low1, high1, low2, high2, ...]
@ -211,17 +211,17 @@ var CMap = (function CMapClosure() {
this.useCMap = null; this.useCMap = null;
this.builtInCMap = builtInCMap; this.builtInCMap = builtInCMap;
} }
CMap.prototype = {
addCodespaceRange(n, low, high) { addCodespaceRange(n, low, high) {
this.codespaceRanges[n - 1].push(low, high); this.codespaceRanges[n - 1].push(low, high);
this.numCodespaceRanges++; this.numCodespaceRanges++;
}, }
mapCidRange(low, high, dstLow) { mapCidRange(low, high, dstLow) {
while (low <= high) { while (low <= high) {
this._map[low++] = dstLow++; this._map[low++] = dstLow++;
} }
}, }
mapBfRange(low, high, dstLow) { mapBfRange(low, high, dstLow) {
var lastByte = dstLow.length - 1; var lastByte = dstLow.length - 1;
@ -231,28 +231,28 @@ var CMap = (function CMapClosure() {
dstLow = dstLow.substr(0, lastByte) + dstLow = dstLow.substr(0, lastByte) +
String.fromCharCode(dstLow.charCodeAt(lastByte) + 1); String.fromCharCode(dstLow.charCodeAt(lastByte) + 1);
} }
}, }
mapBfRangeToArray(low, high, array) { mapBfRangeToArray(low, high, array) {
var i = 0, ii = array.length; let i = 0, ii = array.length;
while (low <= high && i < ii) { while (low <= high && i < ii) {
this._map[low] = array[i++]; this._map[low] = array[i++];
++low; ++low;
} }
}, }
// This is used for both bf and cid chars. // This is used for both bf and cid chars.
mapOne(src, dst) { mapOne(src, dst) {
this._map[src] = dst; this._map[src] = dst;
}, }
lookup(code) { lookup(code) {
return this._map[code]; return this._map[code];
}, }
contains(code) { contains(code) {
return this._map[code] !== undefined; return this._map[code] !== undefined;
}, }
forEach(callback) { forEach(callback) {
// Most maps have fewer than 65536 entries, and for those we use normal // Most maps have fewer than 65536 entries, and for those we use normal
@ -273,12 +273,12 @@ var CMap = (function CMapClosure() {
callback(i, map[i]); callback(i, map[i]);
} }
} }
}, }
charCodeOf(value) { charCodeOf(value) {
// `Array.prototype.indexOf` is *extremely* inefficient for arrays which // `Array.prototype.indexOf` is *extremely* inefficient for arrays which
// are both very sparse and very large (see issue8372.pdf). // are both very sparse and very large (see issue8372.pdf).
let map = this._map; const map = this._map;
if (map.length <= 0x10000) { if (map.length <= 0x10000) {
return map.indexOf(value); return map.indexOf(value);
} }
@ -288,25 +288,24 @@ var CMap = (function CMapClosure() {
} }
} }
return -1; return -1;
}, }
getMap() { getMap() {
return this._map; return this._map;
}, }
readCharCode(str, offset, out) { readCharCode(str, offset, out) {
var c = 0; let c = 0;
var codespaceRanges = this.codespaceRanges; const codespaceRanges = this.codespaceRanges;
var codespaceRangesLen = this.codespaceRanges.length;
// 9.7.6.2 CMap Mapping // 9.7.6.2 CMap Mapping
// The code length is at most 4. // The code length is at most 4.
for (var n = 0; n < codespaceRangesLen; n++) { for (let n = 0, nn = codespaceRanges.length; n < nn; n++) {
c = ((c << 8) | str.charCodeAt(offset + n)) >>> 0; c = ((c << 8) | str.charCodeAt(offset + n)) >>> 0;
// Check each codespace range to see if it falls within. // Check each codespace range to see if it falls within.
var codespaceRange = codespaceRanges[n]; const codespaceRange = codespaceRanges[n];
for (var k = 0, kk = codespaceRange.length; k < kk;) { for (let k = 0, kk = codespaceRange.length; k < kk;) {
var low = codespaceRange[k++]; const low = codespaceRange[k++];
var high = codespaceRange[k++]; const high = codespaceRange[k++];
if (c >= low && c <= high) { if (c >= low && c <= high) {
out.charcode = c; out.charcode = c;
out.length = n + 1; out.length = n + 1;
@ -316,11 +315,11 @@ var CMap = (function CMapClosure() {
} }
out.charcode = 0; out.charcode = 0;
out.length = 1; out.length = 1;
}, }
get length() { get length() {
return this._map.length; return this._map.length;
}, }
get isIdentityCMap() { get isIdentityCMap() {
if (!(this.name === 'Identity-H' || this.name === 'Identity-V')) { if (!(this.name === 'Identity-H' || this.name === 'Identity-V')) {
@ -329,86 +328,76 @@ var CMap = (function CMapClosure() {
if (this._map.length !== 0x10000) { if (this._map.length !== 0x10000) {
return false; return false;
} }
for (var i = 0; i < 0x10000; i++) { for (let i = 0; i < 0x10000; i++) {
if (this._map[i] !== i) { if (this._map[i] !== i) {
return false; return false;
} }
} }
return true; return true;
}, }
}; }
return CMap;
})();
// A special case of CMap, where the _map array implicitly has a length of // A special case of CMap, where the _map array implicitly has a length of
// 65536 and each element is equal to its index. // 65536 and each element is equal to its index.
var IdentityCMap = (function IdentityCMapClosure() { class IdentityCMap extends CMap {
function IdentityCMap(vertical, n) { constructor(vertical, n) {
CMap.call(this); super();
this.vertical = vertical; this.vertical = vertical;
this.addCodespaceRange(n, 0, 0xffff); this.addCodespaceRange(n, 0, 0xffff);
} }
Util.inherit(IdentityCMap, CMap, {});
IdentityCMap.prototype = {
addCodespaceRange: CMap.prototype.addCodespaceRange,
mapCidRange(low, high, dstLow) { mapCidRange(low, high, dstLow) {
unreachable('should not call mapCidRange'); unreachable('should not call mapCidRange');
}, }
mapBfRange(low, high, dstLow) { mapBfRange(low, high, dstLow) {
unreachable('should not call mapBfRange'); unreachable('should not call mapBfRange');
}, }
mapBfRangeToArray(low, high, array) { mapBfRangeToArray(low, high, array) {
unreachable('should not call mapBfRangeToArray'); unreachable('should not call mapBfRangeToArray');
}, }
mapOne(src, dst) { mapOne(src, dst) {
unreachable('should not call mapCidOne'); unreachable('should not call mapCidOne');
}, }
lookup(code) { lookup(code) {
return (Number.isInteger(code) && code <= 0xffff) ? code : undefined; return (Number.isInteger(code) && code <= 0xffff) ? code : undefined;
}, }
contains(code) { contains(code) {
return Number.isInteger(code) && code <= 0xffff; return Number.isInteger(code) && code <= 0xffff;
}, }
forEach(callback) { forEach(callback) {
for (var i = 0; i <= 0xffff; i++) { for (let i = 0; i <= 0xffff; i++) {
callback(i, i); callback(i, i);
} }
}, }
charCodeOf(value) { charCodeOf(value) {
return (Number.isInteger(value) && value <= 0xffff) ? value : -1; return (Number.isInteger(value) && value <= 0xffff) ? value : -1;
}, }
getMap() { getMap() {
// Sometimes identity maps must be instantiated, but it's rare. // Sometimes identity maps must be instantiated, but it's rare.
var map = new Array(0x10000); const map = new Array(0x10000);
for (var i = 0; i <= 0xffff; i++) { for (let i = 0; i <= 0xffff; i++) {
map[i] = i; map[i] = i;
} }
return map; return map;
}, }
readCharCode: CMap.prototype.readCharCode,
get length() { get length() {
return 0x10000; return 0x10000;
}, }
get isIdentityCMap() { get isIdentityCMap() {
unreachable('should not access .isIdentityCMap'); unreachable('should not access .isIdentityCMap');
}, }
}; }
return IdentityCMap;
})();
var BinaryCMapReader = (function BinaryCMapReaderClosure() { var BinaryCMapReader = (function BinaryCMapReaderClosure() {
function hexToInt(a, size) { function hexToInt(a, size) {