Merge pull request #12101 from Snuffleupagus/Dict-size-getRawValues
Add a new `size` getter and `getRawValues` method, to `Dict` instances, to simplify some code
This commit is contained in:
commit
90689cf08e
@ -551,7 +551,7 @@ class PDFDocument {
|
|||||||
// Check if a Collection dictionary is present in the document.
|
// Check if a Collection dictionary is present in the document.
|
||||||
try {
|
try {
|
||||||
const collection = this.catalog.catDict.get("Collection");
|
const collection = this.catalog.catDict.get("Collection");
|
||||||
if (isDict(collection) && collection.getKeys().length > 0) {
|
if (isDict(collection) && collection.size > 0) {
|
||||||
this.collection = collection;
|
this.collection = collection;
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
|
@ -254,8 +254,7 @@ class PartialEvaluator {
|
|||||||
// First check the current resources for blend modes.
|
// First check the current resources for blend modes.
|
||||||
var graphicStates = node.get("ExtGState");
|
var graphicStates = node.get("ExtGState");
|
||||||
if (graphicStates instanceof Dict) {
|
if (graphicStates instanceof Dict) {
|
||||||
for (const key of graphicStates.getKeys()) {
|
for (let graphicState of graphicStates.getRawValues()) {
|
||||||
let graphicState = graphicStates.getRaw(key);
|
|
||||||
if (graphicState instanceof Ref) {
|
if (graphicState instanceof Ref) {
|
||||||
if (processed.has(graphicState)) {
|
if (processed.has(graphicState)) {
|
||||||
continue; // The ExtGState has already been processed.
|
continue; // The ExtGState has already been processed.
|
||||||
@ -301,8 +300,7 @@ class PartialEvaluator {
|
|||||||
if (!(xObjects instanceof Dict)) {
|
if (!(xObjects instanceof Dict)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for (const key of xObjects.getKeys()) {
|
for (let xObject of xObjects.getRawValues()) {
|
||||||
var xObject = xObjects.getRaw(key);
|
|
||||||
if (xObject instanceof Ref) {
|
if (xObject instanceof Ref) {
|
||||||
if (processed.has(xObject)) {
|
if (processed.has(xObject)) {
|
||||||
// The XObject has already been processed, and by avoiding a
|
// The XObject has already been processed, and by avoiding a
|
||||||
@ -3078,9 +3076,7 @@ class PartialEvaluator {
|
|||||||
} else if (isRef(encoding)) {
|
} else if (isRef(encoding)) {
|
||||||
hash.update(encoding.toString());
|
hash.update(encoding.toString());
|
||||||
} else if (isDict(encoding)) {
|
} else if (isDict(encoding)) {
|
||||||
var keys = encoding.getKeys();
|
for (const entry of encoding.getRawValues()) {
|
||||||
for (var i = 0, ii = keys.length; i < ii; i++) {
|
|
||||||
var entry = encoding.getRaw(keys[i]);
|
|
||||||
if (isName(entry)) {
|
if (isName(entry)) {
|
||||||
hash.update(entry.name);
|
hash.update(entry.name);
|
||||||
} else if (isRef(entry)) {
|
} else if (isRef(entry)) {
|
||||||
|
@ -2205,21 +2205,16 @@ const ObjectLoader = (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addChildren(node, nodesToVisit) {
|
function addChildren(node, nodesToVisit) {
|
||||||
if (node instanceof Dict || isStream(node)) {
|
if (node instanceof Dict) {
|
||||||
const dict = node instanceof Dict ? node : node.dict;
|
node = node.getRawValues();
|
||||||
const dictKeys = dict.getKeys();
|
} else if (isStream(node)) {
|
||||||
for (let i = 0, ii = dictKeys.length; i < ii; i++) {
|
node = node.dict.getRawValues();
|
||||||
const rawValue = dict.getRaw(dictKeys[i]);
|
} else if (!Array.isArray(node)) {
|
||||||
if (mayHaveChildren(rawValue)) {
|
return;
|
||||||
nodesToVisit.push(rawValue);
|
}
|
||||||
}
|
for (const rawValue of node) {
|
||||||
}
|
if (mayHaveChildren(rawValue)) {
|
||||||
} else if (Array.isArray(node)) {
|
nodesToVisit.push(rawValue);
|
||||||
for (let i = 0, ii = node.length; i < ii; i++) {
|
|
||||||
const value = node[i];
|
|
||||||
if (mayHaveChildren(value)) {
|
|
||||||
nodesToVisit.push(value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,6 +85,10 @@ var Dict = (function DictClosure() {
|
|||||||
this.xref = newXref;
|
this.xref = newXref;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
get size() {
|
||||||
|
return Object.keys(this._map).length;
|
||||||
|
},
|
||||||
|
|
||||||
// automatically dereferences Ref objects
|
// automatically dereferences Ref objects
|
||||||
get(key1, key2, key3) {
|
get(key1, key2, key3) {
|
||||||
let value = this._map[key1];
|
let value = this._map[key1];
|
||||||
@ -140,6 +144,11 @@ var Dict = (function DictClosure() {
|
|||||||
return Object.keys(this._map);
|
return Object.keys(this._map);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// no dereferencing
|
||||||
|
getRawValues: function Dict_getRawValues() {
|
||||||
|
return Object.values(this._map);
|
||||||
|
},
|
||||||
|
|
||||||
set: function Dict_set(key, value) {
|
set: function Dict_set(key, value) {
|
||||||
if (
|
if (
|
||||||
(typeof PDFJSDev === "undefined" ||
|
(typeof PDFJSDev === "undefined" ||
|
||||||
|
@ -117,6 +117,17 @@ describe("primitives", function () {
|
|||||||
expect(dict.xref).toEqual(xref);
|
expect(dict.xref).toEqual(xref);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should return correct size", function () {
|
||||||
|
const dict = new Dict(null);
|
||||||
|
expect(dict.size).toEqual(0);
|
||||||
|
|
||||||
|
dict.set("Type", Name.get("Page"));
|
||||||
|
expect(dict.size).toEqual(1);
|
||||||
|
|
||||||
|
dict.set("Contents", Ref.get(10, 0));
|
||||||
|
expect(dict.size).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
it("should return invalid values for unknown keys", function () {
|
it("should return invalid values for unknown keys", function () {
|
||||||
checkInvalidHasValues(emptyDict);
|
checkInvalidHasValues(emptyDict);
|
||||||
checkInvalidKeyValues(emptyDict);
|
checkInvalidKeyValues(emptyDict);
|
||||||
@ -264,6 +275,35 @@ describe("primitives", function () {
|
|||||||
expect(keys.sort()).toEqual(expectedKeys);
|
expect(keys.sort()).toEqual(expectedKeys);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should get all raw values", function () {
|
||||||
|
// Test direct objects:
|
||||||
|
const expectedRawValues1 = [testFontFile, testFontFile2, testFontFile3];
|
||||||
|
const rawValues1 = dictWithManyKeys.getRawValues();
|
||||||
|
|
||||||
|
expect(rawValues1.sort()).toEqual(expectedRawValues1);
|
||||||
|
|
||||||
|
// Test indirect objects:
|
||||||
|
const typeName = Name.get("Page");
|
||||||
|
const resources = new Dict(null),
|
||||||
|
resourcesRef = Ref.get(5, 0);
|
||||||
|
const contents = new StringStream("data"),
|
||||||
|
contentsRef = Ref.get(10, 0);
|
||||||
|
const xref = new XRefMock([
|
||||||
|
{ ref: resourcesRef, data: resources },
|
||||||
|
{ ref: contentsRef, data: contents },
|
||||||
|
]);
|
||||||
|
|
||||||
|
const dict = new Dict(xref);
|
||||||
|
dict.set("Type", typeName);
|
||||||
|
dict.set("Resources", resourcesRef);
|
||||||
|
dict.set("Contents", contentsRef);
|
||||||
|
|
||||||
|
const expectedRawValues2 = [contentsRef, resourcesRef, typeName];
|
||||||
|
const rawValues2 = dict.getRawValues();
|
||||||
|
|
||||||
|
expect(rawValues2.sort()).toEqual(expectedRawValues2);
|
||||||
|
});
|
||||||
|
|
||||||
it("should create only one object for Dict.empty", function () {
|
it("should create only one object for Dict.empty", function () {
|
||||||
const firstDictEmpty = Dict.empty;
|
const firstDictEmpty = Dict.empty;
|
||||||
const secondDictEmpty = Dict.empty;
|
const secondDictEmpty = Dict.empty;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user