Merge pull request #14598 from Snuffleupagus/rm-isBool

Re-factor the `Catalog.viewerPreferences` method and remove the `isBool` helper function
This commit is contained in:
Tim van der Meij 2022-02-23 20:36:56 +01:00 committed by GitHub
commit 2bb96a708c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 104 deletions

View File

@ -25,7 +25,6 @@ import {
DocumentActionEventType, DocumentActionEventType,
FormatError, FormatError,
info, info,
isBool,
isString, isString,
objectSize, objectSize,
PermissionFlag, PermissionFlag,
@ -221,7 +220,7 @@ class Catalog {
continue; continue;
} }
const value = obj.get(key); const value = obj.get(key);
if (!isBool(value)) { if (typeof value !== "boolean") {
continue; continue;
} }
markInfo[key] = value; markInfo[key] = value;
@ -776,44 +775,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) { const value = obj.get(key);
if (!obj.has(key)) { let prefValue;
continue;
}
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;
switch (key) { switch (key) {
case "NonFullScreenPageMode": case "HideToolbar":
case "HideMenubar":
case "HideWindowUI":
case "FitWindow":
case "CenterWindow":
case "DisplayDocTitle":
case "PickTrayByPDFSize":
if (typeof value === "boolean") {
prefValue = value;
}
break;
case "NonFullScreenPageMode":
if (value instanceof Name) {
switch (value.name) { switch (value.name) {
case "UseNone": case "UseNone":
case "UseOutlines": case "UseOutlines":
@ -824,8 +809,10 @@ class Catalog {
default: default:
prefValue = "UseNone"; prefValue = "UseNone";
} }
break; }
case "Direction": break;
case "Direction":
if (value instanceof Name) {
switch (value.name) { switch (value.name) {
case "L2R": case "L2R":
case "R2L": case "R2L":
@ -834,11 +821,13 @@ class Catalog {
default: default:
prefValue = "L2R"; prefValue = "L2R";
} }
break; }
case "ViewArea": break;
case "ViewClip": case "ViewArea":
case "PrintArea": case "ViewClip":
case "PrintClip": case "PrintArea":
case "PrintClip":
if (value instanceof Name) {
switch (value.name) { switch (value.name) {
case "MediaBox": case "MediaBox":
case "CropBox": case "CropBox":
@ -850,8 +839,10 @@ class Catalog {
default: default:
prefValue = "CropBox"; prefValue = "CropBox";
} }
break; }
case "PrintScaling": break;
case "PrintScaling":
if (value instanceof Name) {
switch (value.name) { switch (value.name) {
case "None": case "None":
case "AppDefault": case "AppDefault":
@ -860,8 +851,10 @@ class Catalog {
default: default:
prefValue = "AppDefault"; prefValue = "AppDefault";
} }
break; }
case "Duplex": break;
case "Duplex":
if (value instanceof Name) {
switch (value.name) { switch (value.name) {
case "Simplex": case "Simplex":
case "DuplexFlipShortEdge": case "DuplexFlipShortEdge":
@ -871,13 +864,11 @@ class Catalog {
default: default:
prefValue = "None"; prefValue = "None";
} }
break; }
case "PrintPageRange": break;
const length = value.length; case "PrintPageRange":
if (length % 2 !== 0) { // The number of elements must be even.
// The number of elements must be even. if (Array.isArray(value) && value.length % 2 === 0) {
break;
}
const isValid = value.every((page, i, arr) => { const isValid = value.every((page, i, arr) => {
return ( return (
Number.isInteger(page) && Number.isInteger(page) &&
@ -889,30 +880,26 @@ class Catalog {
if (isValid) { if (isValid) {
prefValue = value; prefValue = value;
} }
break;
case "NumCopies":
if (value > 0) {
prefValue = value;
}
break;
default:
if (typeof value !== "boolean") {
throw new FormatError(
`viewerPreferences - expected a boolean value for: ${key}`
);
}
prefValue = value;
}
if (prefValue !== undefined) {
if (!prefs) {
prefs = Object.create(null);
} }
prefs[key] = prefValue; break;
} else { case "NumCopies":
info(`Bad value in ViewerPreferences for "${key}".`); if (Number.isInteger(value) && value > 0) {
} prefValue = value;
}
break;
default:
warn(`Ignoring non-standard key in ViewerPreferences: ${key}.`);
continue;
} }
if (prefValue === undefined) {
warn(`Bad value, for key "${key}", in ViewerPreferences: ${value}.`);
continue;
}
if (!prefs) {
prefs = Object.create(null);
}
prefs[key] = prefValue;
} }
return shadow(this, "viewerPreferences", prefs); return shadow(this, "viewerPreferences", prefs);
} }
@ -1533,7 +1520,7 @@ class Catalog {
} }
// The 'NewWindow' property, equal to `LinkTarget.BLANK`. // The 'NewWindow' property, equal to `LinkTarget.BLANK`.
const newWindow = action.get("NewWindow"); const newWindow = action.get("NewWindow");
if (isBool(newWindow)) { if (typeof newWindow === "boolean") {
resultObj.newWindow = newWindow; resultObj.newWindow = newWindow;
} }
break; break;

View File

@ -17,7 +17,6 @@ import { Dict, Ref } from "./primitives.js";
import { import {
FormatError, FormatError,
info, info,
isBool,
IsEvalSupportedCached, IsEvalSupportedCached,
shadow, shadow,
unreachable, unreachable,
@ -627,7 +626,7 @@ class PostScriptEvaluator {
case "and": case "and":
b = stack.pop(); b = stack.pop();
a = stack.pop(); a = stack.pop();
if (isBool(a) && isBool(b)) { if (typeof a === "boolean" && typeof b === "boolean") {
stack.push(a && b); stack.push(a && b);
} else { } else {
stack.push(a & b); stack.push(a & b);
@ -751,7 +750,7 @@ class PostScriptEvaluator {
break; break;
case "not": case "not":
a = stack.pop(); a = stack.pop();
if (isBool(a)) { if (typeof a === "boolean") {
stack.push(!a); stack.push(!a);
} else { } else {
stack.push(~a); stack.push(~a);
@ -760,7 +759,7 @@ class PostScriptEvaluator {
case "or": case "or":
b = stack.pop(); b = stack.pop();
a = stack.pop(); a = stack.pop();
if (isBool(a) && isBool(b)) { if (typeof a === "boolean" && typeof b === "boolean") {
stack.push(a || b); stack.push(a || b);
} else { } else {
stack.push(a | b); stack.push(a | b);
@ -802,7 +801,7 @@ class PostScriptEvaluator {
case "xor": case "xor":
b = stack.pop(); b = stack.pop();
a = stack.pop(); a = stack.pop();
if (isBool(a) && isBool(b)) { if (typeof a === "boolean" && typeof b === "boolean") {
stack.push(a !== b); stack.push(a !== b);
} else { } else {
stack.push(a ^ b); stack.push(a ^ b);

View File

@ -1030,10 +1030,6 @@ function utf8StringToString(str) {
return unescape(encodeURIComponent(str)); return unescape(encodeURIComponent(str));
} }
function isBool(v) {
return typeof v === "boolean";
}
function isString(v) { function isString(v) {
return typeof v === "string"; return typeof v === "string";
} }
@ -1139,7 +1135,6 @@ export {
isArrayBuffer, isArrayBuffer,
isArrayEqual, isArrayEqual,
isAscii, isAscii,
isBool,
IsEvalSupportedCached, IsEvalSupportedCached,
IsLittleEndianCached, IsLittleEndianCached,
isSameOrigin, isSameOrigin,

View File

@ -21,7 +21,6 @@ import {
getModificationDate, getModificationDate,
isArrayBuffer, isArrayBuffer,
isAscii, isAscii,
isBool,
isSameOrigin, isSameOrigin,
isString, isString,
string32, string32,
@ -74,22 +73,6 @@ describe("util", function () {
}); });
}); });
describe("isBool", function () {
it("handles boolean values", function () {
expect(isBool(true)).toEqual(true);
expect(isBool(false)).toEqual(true);
});
it("handles non-boolean values", function () {
expect(isBool("true")).toEqual(false);
expect(isBool("false")).toEqual(false);
expect(isBool(1)).toEqual(false);
expect(isBool(0)).toEqual(false);
expect(isBool(null)).toEqual(false);
expect(isBool(undefined)).toEqual(false);
});
});
describe("isString", function () { describe("isString", function () {
it("handles string values", function () { it("handles string values", function () {
expect(isString("foo")).toEqual(true); expect(isString("foo")).toEqual(true);