Re-factor the Catalog.viewerPreferences method

This removes the `ViewerPreferencesValidators` structure, and thus (slightly) simplifies the code overall. With these changes we only have to iterate through, and validate, the actually available Dictionary entries.
This commit is contained in:
Jonas Jenwald 2022-02-23 12:20:35 +01:00
parent b2f6844ce3
commit 82f1ee1755

View File

@ -776,44 +776,30 @@ class Catalog {
} }
get viewerPreferences() { get viewerPreferences() {
const ViewerPreferencesValidators = {
HideToolbar: isBool,
HideMenubar: isBool,
HideWindowUI: isBool,
FitWindow: isBool,
CenterWindow: isBool,
DisplayDocTitle: isBool,
NonFullScreenPageMode: isName,
Direction: isName,
ViewArea: isName,
ViewClip: isName,
PrintArea: isName,
PrintClip: isName,
PrintScaling: isName,
Duplex: isName,
PickTrayByPDFSize: isBool,
PrintPageRange: Array.isArray,
NumCopies: Number.isInteger,
};
const obj = this._catDict.get("ViewerPreferences"); const obj = this._catDict.get("ViewerPreferences");
if (!(obj instanceof Dict)) {
return shadow(this, "viewerPreferences", null);
}
let prefs = null; let prefs = null;
if (obj instanceof Dict) { for (const key of obj.getKeys()) {
for (const key in ViewerPreferencesValidators) {
if (!obj.has(key)) {
continue;
}
const value = obj.get(key); const value = obj.get(key);
// Make sure the (standard) value conforms to the specification.
if (!ViewerPreferencesValidators[key](value)) {
info(`Bad value in ViewerPreferences for "${key}".`);
continue;
}
let prefValue; let prefValue;
switch (key) { switch (key) {
case "HideToolbar":
case "HideMenubar":
case "HideWindowUI":
case "FitWindow":
case "CenterWindow":
case "DisplayDocTitle":
case "PickTrayByPDFSize":
if (typeof value === "boolean") {
prefValue = value;
}
break;
case "NonFullScreenPageMode": case "NonFullScreenPageMode":
if (value instanceof Name) {
switch (value.name) { switch (value.name) {
case "UseNone": case "UseNone":
case "UseOutlines": case "UseOutlines":
@ -824,8 +810,10 @@ class Catalog {
default: default:
prefValue = "UseNone"; prefValue = "UseNone";
} }
}
break; break;
case "Direction": case "Direction":
if (value instanceof Name) {
switch (value.name) { switch (value.name) {
case "L2R": case "L2R":
case "R2L": case "R2L":
@ -834,11 +822,13 @@ class Catalog {
default: default:
prefValue = "L2R"; prefValue = "L2R";
} }
}
break; break;
case "ViewArea": case "ViewArea":
case "ViewClip": case "ViewClip":
case "PrintArea": case "PrintArea":
case "PrintClip": case "PrintClip":
if (value instanceof Name) {
switch (value.name) { switch (value.name) {
case "MediaBox": case "MediaBox":
case "CropBox": case "CropBox":
@ -850,8 +840,10 @@ class Catalog {
default: default:
prefValue = "CropBox"; prefValue = "CropBox";
} }
}
break; break;
case "PrintScaling": case "PrintScaling":
if (value instanceof Name) {
switch (value.name) { switch (value.name) {
case "None": case "None":
case "AppDefault": case "AppDefault":
@ -860,8 +852,10 @@ class Catalog {
default: default:
prefValue = "AppDefault"; prefValue = "AppDefault";
} }
}
break; break;
case "Duplex": case "Duplex":
if (value instanceof Name) {
switch (value.name) { switch (value.name) {
case "Simplex": case "Simplex":
case "DuplexFlipShortEdge": case "DuplexFlipShortEdge":
@ -871,13 +865,11 @@ class Catalog {
default: default:
prefValue = "None"; prefValue = "None";
} }
}
break; break;
case "PrintPageRange": case "PrintPageRange":
const length = value.length;
if (length % 2 !== 0) {
// The number of elements must be even. // The number of elements must be even.
break; if (Array.isArray(value) && value.length % 2 === 0) {
}
const isValid = value.every((page, i, arr) => { const isValid = value.every((page, i, arr) => {
return ( return (
Number.isInteger(page) && Number.isInteger(page) &&
@ -889,30 +881,26 @@ class Catalog {
if (isValid) { if (isValid) {
prefValue = value; prefValue = value;
} }
}
break; break;
case "NumCopies": case "NumCopies":
if (value > 0) { if (Number.isInteger(value) && value > 0) {
prefValue = value; prefValue = value;
} }
break; break;
default: default:
if (typeof value !== "boolean") { warn(`Ignoring non-standard key in ViewerPreferences: ${key}.`);
throw new FormatError( continue;
`viewerPreferences - expected a boolean value for: ${key}`
);
}
prefValue = value;
} }
if (prefValue !== undefined) { if (prefValue === undefined) {
warn(`Bad value, for key "${key}", in ViewerPreferences: ${value}.`);
continue;
}
if (!prefs) { if (!prefs) {
prefs = Object.create(null); prefs = Object.create(null);
} }
prefs[key] = prefValue; prefs[key] = prefValue;
} else {
info(`Bad value in ViewerPreferences for "${key}".`);
}
}
} }
return shadow(this, "viewerPreferences", prefs); return shadow(this, "viewerPreferences", prefs);
} }