Convert NameOrNumberTree, NameTree, and NumberTree 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:31:31 +02:00
parent ba1af46709
commit b773b356af

View File

@ -1573,32 +1573,35 @@ var XRef = (function XRefClosure() {
* see the specification (7.9.6 and 7.9.7) for additional details. * see the specification (7.9.6 and 7.9.7) for additional details.
* TODO: implement all the Dict functions and make this more efficient. * TODO: implement all the Dict functions and make this more efficient.
*/ */
var NameOrNumberTree = (function NameOrNumberTreeClosure() { class NameOrNumberTree {
function NameOrNumberTree(root, xref) { constructor(root, xref, type) {
if (this.constructor === NameOrNumberTree) {
unreachable('Cannot initialize NameOrNumberTree.'); unreachable('Cannot initialize NameOrNumberTree.');
} }
this.root = root;
this.xref = xref;
this._type = type;
}
NameOrNumberTree.prototype = { getAll() {
getAll: function NameOrNumberTree_getAll() { const dict = Object.create(null);
var dict = Object.create(null);
if (!this.root) { if (!this.root) {
return dict; return dict;
} }
var xref = this.xref; const xref = this.xref;
// Reading Name/Number tree. // Reading Name/Number tree.
var processed = new RefSet(); const processed = new RefSet();
processed.put(this.root); processed.put(this.root);
var queue = [this.root]; const queue = [this.root];
while (queue.length > 0) { while (queue.length > 0) {
var i, n; const obj = xref.fetchIfRef(queue.shift());
var obj = xref.fetchIfRef(queue.shift());
if (!isDict(obj)) { if (!isDict(obj)) {
continue; continue;
} }
if (obj.has('Kids')) { if (obj.has('Kids')) {
var kids = obj.get('Kids'); const kids = obj.get('Kids');
for (i = 0, n = kids.length; i < n; i++) { for (let i = 0, ii = kids.length; i < ii; i++) {
var kid = kids[i]; const kid = kids[i];
if (processed.has(kid)) { if (processed.has(kid)) {
throw new FormatError(`Duplicate entry in "${this._type}" tree.`); throw new FormatError(`Duplicate entry in "${this._type}" tree.`);
} }
@ -1607,26 +1610,24 @@ var NameOrNumberTree = (function NameOrNumberTreeClosure() {
} }
continue; continue;
} }
var entries = obj.get(this._type); const entries = obj.get(this._type);
if (Array.isArray(entries)) { if (Array.isArray(entries)) {
for (i = 0, n = entries.length; i < n; i += 2) { for (let i = 0, ii = entries.length; i < ii; i += 2) {
dict[xref.fetchIfRef(entries[i])] = xref.fetchIfRef(entries[i + 1]); dict[xref.fetchIfRef(entries[i])] = xref.fetchIfRef(entries[i + 1]);
} }
} }
} }
return dict; return dict;
}, }
get: function NameOrNumberTree_get(key) { get(key) {
if (!this.root) { if (!this.root) {
return null; return null;
} }
const xref = this.xref;
var xref = this.xref; let kidsOrEntries = xref.fetchIfRef(this.root);
var kidsOrEntries = xref.fetchIfRef(this.root); let loopCount = 0;
var loopCount = 0; const MAX_LEVELS = 10;
var MAX_LEVELS = 10;
var l, r, m;
// Perform a binary search to quickly find the entry that // Perform a binary search to quickly find the entry that
// contains the key we are looking for. // contains the key we are looking for.
@ -1636,17 +1637,16 @@ var NameOrNumberTree = (function NameOrNumberTreeClosure() {
return null; return null;
} }
var kids = kidsOrEntries.get('Kids'); const kids = kidsOrEntries.get('Kids');
if (!Array.isArray(kids)) { if (!Array.isArray(kids)) {
return null; return null;
} }
l = 0; let l = 0, r = kids.length - 1;
r = kids.length - 1;
while (l <= r) { while (l <= r) {
m = (l + r) >> 1; const m = (l + r) >> 1;
var kid = xref.fetchIfRef(kids[m]); const kid = xref.fetchIfRef(kids[m]);
var limits = kid.get('Limits'); const limits = kid.get('Limits');
if (key < xref.fetchIfRef(limits[0])) { if (key < xref.fetchIfRef(limits[0])) {
r = m - 1; r = m - 1;
@ -1664,16 +1664,15 @@ var NameOrNumberTree = (function NameOrNumberTreeClosure() {
// If we get here, then we have found the right entry. Now go through the // If we get here, then we have found the right entry. Now go through the
// entries in the dictionary until we find the key we're looking for. // entries in the dictionary until we find the key we're looking for.
var entries = kidsOrEntries.get(this._type); const entries = kidsOrEntries.get(this._type);
if (Array.isArray(entries)) { if (Array.isArray(entries)) {
// Perform a binary search to reduce the lookup time. // Perform a binary search to reduce the lookup time.
l = 0; let l = 0, r = entries.length - 2;
r = entries.length - 2;
while (l <= r) { while (l <= r) {
// Check only even indices (0, 2, 4, ...) because the // Check only even indices (0, 2, 4, ...) because the
// odd indices contain the actual data. // odd indices contain the actual data.
m = (l + r) & ~1; const m = (l + r) & ~1;
var currentKey = xref.fetchIfRef(entries[m]); const currentKey = xref.fetchIfRef(entries[m]);
if (key < currentKey) { if (key < currentKey) {
r = m - 2; r = m - 2;
} else if (key > currentKey) { } else if (key > currentKey) {
@ -1684,34 +1683,20 @@ var NameOrNumberTree = (function NameOrNumberTreeClosure() {
} }
} }
return null; return null;
}, }
};
return NameOrNumberTree;
})();
var NameTree = (function NameTreeClosure() {
function NameTree(root, xref) {
this.root = root;
this.xref = xref;
this._type = 'Names';
} }
Util.inherit(NameTree, NameOrNumberTree, {}); class NameTree extends NameOrNumberTree {
constructor(root, xref) {
return NameTree; super(root, xref, 'Names');
})(); }
var NumberTree = (function NumberTreeClosure() {
function NumberTree(root, xref) {
this.root = root;
this.xref = xref;
this._type = 'Nums';
} }
Util.inherit(NumberTree, NameOrNumberTree, {}); class NumberTree extends NameOrNumberTree {
constructor(root, xref) {
return NumberTree; super(root, xref, 'Nums');
})(); }
}
/** /**
* "A PDF file can refer to the contents of another file by using a File * "A PDF file can refer to the contents of another file by using a File