1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

Add support, in Dict.merge, for merging of "sub"-dictionaries

This allows for merging of dictionaries one level deeper than previously. This could be useful e.g. for /Resources dictionaries, where you want to e.g. merge their respective /Font dictionaries (and other) together rather than picking just the first one.
This commit is contained in:
Jonas Jenwald 2020-08-28 01:05:33 +02:00
parent aa27e7fb8d
commit 784a420027
5 changed files with 115 additions and 18 deletions

View file

@ -114,7 +114,7 @@ class Page {
if (value.length === 1 || !isDict(value[0])) {
return value[0];
}
return Dict.merge(this.xref, value);
return Dict.merge({ xref: this.xref, dictArray: value });
}
get content() {

View file

@ -726,8 +726,10 @@ class PartialEvaluator {
const tilingOpList = new OperatorList();
// Merge the available resources, to prevent issues when the patternDict
// is missing some /Resources entries (fixes issue6541.pdf).
const resourcesArray = [patternDict.get("Resources"), resources];
const patternResources = Dict.merge(this.xref, resourcesArray);
const patternResources = Dict.merge({
xref: this.xref,
dictArray: [patternDict.get("Resources"), resources],
});
return this.getOperatorList({
stream: pattern,

View file

@ -173,22 +173,61 @@ var Dict = (function DictClosure() {
Dict.empty = new Dict(null);
Dict.merge = function (xref, dictArray) {
Dict.merge = function ({ xref, dictArray, mergeSubDicts = false }) {
const mergedDict = new Dict(xref);
for (let i = 0, ii = dictArray.length; i < ii; i++) {
const dict = dictArray[i];
if (!isDict(dict)) {
continue;
}
for (const keyName in dict._map) {
if (mergedDict._map[keyName] !== undefined) {
if (!mergeSubDicts) {
for (const dict of dictArray) {
if (!(dict instanceof Dict)) {
continue;
}
mergedDict._map[keyName] = dict._map[keyName];
for (const [key, value] of Object.entries(dict._map)) {
if (mergedDict._map[key] === undefined) {
mergedDict._map[key] = value;
}
}
}
return mergedDict.size > 0 ? mergedDict : Dict.empty;
}
const properties = new Map();
for (const dict of dictArray) {
if (!(dict instanceof Dict)) {
continue;
}
for (const [key, value] of Object.entries(dict._map)) {
let property = properties.get(key);
if (property === undefined) {
property = [];
properties.set(key, property);
}
property.push(value);
}
}
return mergedDict;
for (const [name, values] of properties) {
if (values.length === 1 || !(values[0] instanceof Dict)) {
mergedDict._map[name] = values[0];
continue;
}
const subDict = new Dict(xref);
for (const dict of values) {
if (!(dict instanceof Dict)) {
continue;
}
for (const [key, value] of Object.entries(dict._map)) {
if (subDict._map[key] === undefined) {
subDict._map[key] = value;
}
}
}
if (subDict.size > 0) {
mergedDict._map[name] = subDict;
}
}
properties.clear();
return mergedDict.size > 0 ? mergedDict : Dict.empty;
};
return Dict;

View file

@ -390,4 +390,13 @@ if (
}
Object.values = require("core-js/es/object/values.js");
})();
// Provides support for Object.entries in legacy browsers.
// Support: IE, Chrome<54
(function checkObjectEntries() {
if (Object.entries) {
return;
}
Object.entries = require("core-js/es/object/entries.js");
})();
}