Prefer instanceof Name
rather than calling isName()
with one argument
Unless you actually need to check that something is both a `Name` and also of the *correct* type, using `instanceof Name` directly should be a tiny bit more efficient since it avoids one function call and an unnecessary `undefined` check. This patch uses ESLint to enforce this, since we obviously still want to keep the `isName` helper function for where it makes sense.
This commit is contained in:
parent
4df82ad31e
commit
b282814e38
@ -187,6 +187,10 @@
|
|||||||
"selector": "CallExpression[callee.name='isDict'][arguments.length<2]",
|
"selector": "CallExpression[callee.name='isDict'][arguments.length<2]",
|
||||||
"message": "Use `instanceof Dict` rather than `isDict()` with one argument.",
|
"message": "Use `instanceof Dict` rather than `isDict()` with one argument.",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"selector": "CallExpression[callee.name='isName'][arguments.length<2]",
|
||||||
|
"message": "Use `instanceof Name` rather than `isName()` with one argument.",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"selector": "NewExpression[callee.name='Cmd']",
|
"selector": "NewExpression[callee.name='Cmd']",
|
||||||
"message": "Use `Cmd.get()` rather than `new Cmd()`.",
|
"message": "Use `Cmd.get()` rather than `new Cmd()`.",
|
||||||
|
@ -104,7 +104,7 @@ class AnnotationFactory {
|
|||||||
|
|
||||||
// Determine the annotation's subtype.
|
// Determine the annotation's subtype.
|
||||||
let subtype = dict.get("Subtype");
|
let subtype = dict.get("Subtype");
|
||||||
subtype = isName(subtype) ? subtype.name : null;
|
subtype = subtype instanceof Name ? subtype.name : null;
|
||||||
|
|
||||||
// Return the right annotation object based on the subtype and field type.
|
// Return the right annotation object based on the subtype and field type.
|
||||||
const parameters = {
|
const parameters = {
|
||||||
@ -128,7 +128,7 @@ class AnnotationFactory {
|
|||||||
|
|
||||||
case "Widget":
|
case "Widget":
|
||||||
let fieldType = getInheritableProperty({ dict, key: "FT" });
|
let fieldType = getInheritableProperty({ dict, key: "FT" });
|
||||||
fieldType = isName(fieldType) ? fieldType.name : null;
|
fieldType = fieldType instanceof Name ? fieldType.name : null;
|
||||||
|
|
||||||
switch (fieldType) {
|
switch (fieldType) {
|
||||||
case "Tx":
|
case "Tx":
|
||||||
@ -698,7 +698,7 @@ class Annotation {
|
|||||||
// In case the normal appearance is a dictionary, the `AS` entry provides
|
// In case the normal appearance is a dictionary, the `AS` entry provides
|
||||||
// the key of the stream in this dictionary.
|
// the key of the stream in this dictionary.
|
||||||
const as = dict.get("AS");
|
const as = dict.get("AS");
|
||||||
if (!isName(as) || !normalAppearanceState.has(as.name)) {
|
if (!(as instanceof Name) || !normalAppearanceState.has(as.name)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.appearance = normalAppearanceState.get(as.name);
|
this.appearance = normalAppearanceState.get(as.name);
|
||||||
@ -912,7 +912,7 @@ class AnnotationBorderStyle {
|
|||||||
|
|
||||||
// Some corrupt PDF generators may provide the width as a `Name`,
|
// Some corrupt PDF generators may provide the width as a `Name`,
|
||||||
// rather than as a number (fixes issue 10385).
|
// rather than as a number (fixes issue 10385).
|
||||||
if (isName(width)) {
|
if (width instanceof Name) {
|
||||||
this.width = 0; // This is consistent with the behaviour in Adobe Reader.
|
this.width = 0; // This is consistent with the behaviour in Adobe Reader.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -946,7 +946,7 @@ class AnnotationBorderStyle {
|
|||||||
* @see {@link shared/util.js}
|
* @see {@link shared/util.js}
|
||||||
*/
|
*/
|
||||||
setStyle(style) {
|
setStyle(style) {
|
||||||
if (!isName(style)) {
|
if (!(style instanceof Name)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (style.name) {
|
switch (style.name) {
|
||||||
@ -1055,7 +1055,8 @@ class MarkupAnnotation extends Annotation {
|
|||||||
this.data.inReplyTo = rawIRT instanceof Ref ? rawIRT.toString() : null;
|
this.data.inReplyTo = rawIRT instanceof Ref ? rawIRT.toString() : null;
|
||||||
|
|
||||||
const rt = dict.get("RT");
|
const rt = dict.get("RT");
|
||||||
this.data.replyType = isName(rt) ? rt.name : AnnotationReplyType.REPLY;
|
this.data.replyType =
|
||||||
|
rt instanceof Name ? rt.name : AnnotationReplyType.REPLY;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.data.replyType === AnnotationReplyType.GROUP) {
|
if (this.data.replyType === AnnotationReplyType.GROUP) {
|
||||||
@ -1265,7 +1266,7 @@ class WidgetAnnotation extends Annotation {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const fieldType = getInheritableProperty({ dict, key: "FT" });
|
const fieldType = getInheritableProperty({ dict, key: "FT" });
|
||||||
data.fieldType = isName(fieldType) ? fieldType.name : null;
|
data.fieldType = fieldType instanceof Name ? fieldType.name : null;
|
||||||
|
|
||||||
const localResources = getInheritableProperty({ dict, key: "DR" });
|
const localResources = getInheritableProperty({ dict, key: "DR" });
|
||||||
const acroFormResources = params.acroForm.get("DR");
|
const acroFormResources = params.acroForm.get("DR");
|
||||||
@ -1306,7 +1307,7 @@ class WidgetAnnotation extends Annotation {
|
|||||||
return formValue
|
return formValue
|
||||||
.filter(item => isString(item))
|
.filter(item => isString(item))
|
||||||
.map(item => stringToPDFString(item));
|
.map(item => stringToPDFString(item));
|
||||||
} else if (isName(formValue)) {
|
} else if (formValue instanceof Name) {
|
||||||
return stringToPDFString(formValue.name);
|
return stringToPDFString(formValue.name);
|
||||||
} else if (isString(formValue)) {
|
} else if (isString(formValue)) {
|
||||||
return stringToPDFString(formValue);
|
return stringToPDFString(formValue);
|
||||||
@ -2321,7 +2322,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
|
|||||||
if (fieldParent instanceof Dict) {
|
if (fieldParent instanceof Dict) {
|
||||||
this.parent = params.dict.getRaw("Parent");
|
this.parent = params.dict.getRaw("Parent");
|
||||||
const fieldParentValue = fieldParent.get("V");
|
const fieldParentValue = fieldParent.get("V");
|
||||||
if (isName(fieldParentValue)) {
|
if (fieldParentValue instanceof Name) {
|
||||||
this.data.fieldValue = this._decodeFormValue(fieldParentValue);
|
this.data.fieldValue = this._decodeFormValue(fieldParentValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2566,7 +2567,8 @@ class PopupAnnotation extends Annotation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const parentSubtype = parentItem.get("Subtype");
|
const parentSubtype = parentItem.get("Subtype");
|
||||||
this.data.parentType = isName(parentSubtype) ? parentSubtype.name : null;
|
this.data.parentType =
|
||||||
|
parentSubtype instanceof Name ? parentSubtype.name : null;
|
||||||
const rawParent = parameters.dict.getRaw("Parent");
|
const rawParent = parameters.dict.getRaw("Parent");
|
||||||
this.data.parentId = rawParent instanceof Ref ? rawParent.toString() : null;
|
this.data.parentId = rawParent instanceof Ref ? rawParent.toString() : null;
|
||||||
|
|
||||||
|
@ -529,9 +529,10 @@ class Catalog {
|
|||||||
creator: isString(config.get("Creator"))
|
creator: isString(config.get("Creator"))
|
||||||
? stringToPDFString(config.get("Creator"))
|
? stringToPDFString(config.get("Creator"))
|
||||||
: null,
|
: null,
|
||||||
baseState: isName(config.get("BaseState"))
|
baseState:
|
||||||
? config.get("BaseState").name
|
config.get("BaseState") instanceof Name
|
||||||
: null,
|
? config.get("BaseState").name
|
||||||
|
: null,
|
||||||
on: parseOnOff(config.get("ON")),
|
on: parseOnOff(config.get("ON")),
|
||||||
off: parseOnOff(config.get("OFF")),
|
off: parseOnOff(config.get("OFF")),
|
||||||
order: parseOrder(config.get("Order")),
|
order: parseOrder(config.get("Order")),
|
||||||
@ -667,7 +668,7 @@ class Catalog {
|
|||||||
|
|
||||||
if (labelDict.has("S")) {
|
if (labelDict.has("S")) {
|
||||||
const s = labelDict.get("S");
|
const s = labelDict.get("S");
|
||||||
if (!isName(s)) {
|
if (!(s instanceof Name)) {
|
||||||
throw new FormatError("Invalid style in PageLabel dictionary.");
|
throw new FormatError("Invalid style in PageLabel dictionary.");
|
||||||
}
|
}
|
||||||
style = s.name;
|
style = s.name;
|
||||||
@ -743,7 +744,7 @@ class Catalog {
|
|||||||
// affect the Scroll mode (continuous/non-continuous) used in Adobe Reader.
|
// affect the Scroll mode (continuous/non-continuous) used in Adobe Reader.
|
||||||
let pageLayout = "";
|
let pageLayout = "";
|
||||||
|
|
||||||
if (isName(obj)) {
|
if (obj instanceof Name) {
|
||||||
switch (obj.name) {
|
switch (obj.name) {
|
||||||
case "SinglePage":
|
case "SinglePage":
|
||||||
case "OneColumn":
|
case "OneColumn":
|
||||||
@ -761,7 +762,7 @@ class Catalog {
|
|||||||
const obj = this._catDict.get("PageMode");
|
const obj = this._catDict.get("PageMode");
|
||||||
let pageMode = "UseNone"; // Default value.
|
let pageMode = "UseNone"; // Default value.
|
||||||
|
|
||||||
if (isName(obj)) {
|
if (obj instanceof Name) {
|
||||||
switch (obj.name) {
|
switch (obj.name) {
|
||||||
case "UseNone":
|
case "UseNone":
|
||||||
case "UseOutlines":
|
case "UseOutlines":
|
||||||
@ -1465,7 +1466,7 @@ class Catalog {
|
|||||||
|
|
||||||
if (action instanceof Dict) {
|
if (action instanceof Dict) {
|
||||||
const actionType = action.get("S");
|
const actionType = action.get("S");
|
||||||
if (!isName(actionType)) {
|
if (!(actionType instanceof Name)) {
|
||||||
warn("parseDestDictionary: Invalid type in Action dictionary.");
|
warn("parseDestDictionary: Invalid type in Action dictionary.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1519,7 +1520,7 @@ class Catalog {
|
|||||||
// NOTE: the destination is relative to the *remote* document.
|
// NOTE: the destination is relative to the *remote* document.
|
||||||
let remoteDest = action.get("D");
|
let remoteDest = action.get("D");
|
||||||
if (remoteDest) {
|
if (remoteDest) {
|
||||||
if (isName(remoteDest)) {
|
if (remoteDest instanceof Name) {
|
||||||
remoteDest = remoteDest.name;
|
remoteDest = remoteDest.name;
|
||||||
}
|
}
|
||||||
if (isString(url)) {
|
if (isString(url)) {
|
||||||
@ -1540,7 +1541,7 @@ class Catalog {
|
|||||||
|
|
||||||
case "Named":
|
case "Named":
|
||||||
const namedAction = action.get("N");
|
const namedAction = action.get("N");
|
||||||
if (isName(namedAction)) {
|
if (namedAction instanceof Name) {
|
||||||
resultObj.action = namedAction.name;
|
resultObj.action = namedAction.name;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -1587,7 +1588,7 @@ class Catalog {
|
|||||||
resultObj.unsafeUrl = url;
|
resultObj.unsafeUrl = url;
|
||||||
}
|
}
|
||||||
if (dest) {
|
if (dest) {
|
||||||
if (isName(dest)) {
|
if (dest instanceof Name) {
|
||||||
dest = dest.name;
|
dest = dest.name;
|
||||||
}
|
}
|
||||||
if (isString(dest) || Array.isArray(dest)) {
|
if (isString(dest) || Array.isArray(dest)) {
|
||||||
|
@ -20,7 +20,7 @@ import {
|
|||||||
unreachable,
|
unreachable,
|
||||||
warn,
|
warn,
|
||||||
} from "../shared/util.js";
|
} from "../shared/util.js";
|
||||||
import { Cmd, EOF, isCmd, isName } from "./primitives.js";
|
import { Cmd, EOF, isCmd, Name } from "./primitives.js";
|
||||||
import { BaseStream } from "./base_stream.js";
|
import { BaseStream } from "./base_stream.js";
|
||||||
import { Lexer } from "./parser.js";
|
import { Lexer } from "./parser.js";
|
||||||
import { MissingDataException } from "./core_utils.js";
|
import { MissingDataException } from "./core_utils.js";
|
||||||
@ -901,7 +901,7 @@ const CMapFactory = (function CMapFactoryClosure() {
|
|||||||
|
|
||||||
function parseCMapName(cMap, lexer) {
|
function parseCMapName(cMap, lexer) {
|
||||||
const obj = lexer.getObj();
|
const obj = lexer.getObj();
|
||||||
if (isName(obj) && isString(obj.name)) {
|
if (obj instanceof Name && isString(obj.name)) {
|
||||||
cMap.name = obj.name;
|
cMap.name = obj.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -913,7 +913,7 @@ const CMapFactory = (function CMapFactoryClosure() {
|
|||||||
const obj = lexer.getObj();
|
const obj = lexer.getObj();
|
||||||
if (obj === EOF) {
|
if (obj === EOF) {
|
||||||
break;
|
break;
|
||||||
} else if (isName(obj)) {
|
} else if (obj instanceof Name) {
|
||||||
if (obj.name === "WMode") {
|
if (obj.name === "WMode") {
|
||||||
parseWMode(cMap, lexer);
|
parseWMode(cMap, lexer);
|
||||||
} else if (obj.name === "CMapName") {
|
} else if (obj.name === "CMapName") {
|
||||||
@ -925,7 +925,7 @@ const CMapFactory = (function CMapFactoryClosure() {
|
|||||||
case "endcmap":
|
case "endcmap":
|
||||||
break objLoop;
|
break objLoop;
|
||||||
case "usecmap":
|
case "usecmap":
|
||||||
if (isName(previous)) {
|
if (previous instanceof Name) {
|
||||||
embeddedUseCMap = previous.name;
|
embeddedUseCMap = previous.name;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -1024,7 +1024,7 @@ const CMapFactory = (function CMapFactoryClosure() {
|
|||||||
const fetchBuiltInCMap = params.fetchBuiltInCMap;
|
const fetchBuiltInCMap = params.fetchBuiltInCMap;
|
||||||
const useCMap = params.useCMap;
|
const useCMap = params.useCMap;
|
||||||
|
|
||||||
if (isName(encoding)) {
|
if (encoding instanceof Name) {
|
||||||
return createBuiltInCMap(encoding.name, fetchBuiltInCMap);
|
return createBuiltInCMap(encoding.name, fetchBuiltInCMap);
|
||||||
} else if (encoding instanceof BaseStream) {
|
} else if (encoding instanceof BaseStream) {
|
||||||
const parsedCMap = await parseCMap(
|
const parsedCMap = await parseCMap(
|
||||||
|
@ -21,7 +21,7 @@ import {
|
|||||||
unreachable,
|
unreachable,
|
||||||
warn,
|
warn,
|
||||||
} from "../shared/util.js";
|
} from "../shared/util.js";
|
||||||
import { Dict, isName, Name, Ref } from "./primitives.js";
|
import { Dict, Name, Ref } from "./primitives.js";
|
||||||
import { BaseStream } from "./base_stream.js";
|
import { BaseStream } from "./base_stream.js";
|
||||||
import { MissingDataException } from "./core_utils.js";
|
import { MissingDataException } from "./core_utils.js";
|
||||||
|
|
||||||
@ -378,7 +378,7 @@ class ColorSpace {
|
|||||||
*/
|
*/
|
||||||
static _parse(cs, xref, resources = null, pdfFunctionFactory) {
|
static _parse(cs, xref, resources = null, pdfFunctionFactory) {
|
||||||
cs = xref.fetchIfRef(cs);
|
cs = xref.fetchIfRef(cs);
|
||||||
if (isName(cs)) {
|
if (cs instanceof Name) {
|
||||||
switch (cs.name) {
|
switch (cs.name) {
|
||||||
case "G":
|
case "G":
|
||||||
case "DeviceGray":
|
case "DeviceGray":
|
||||||
@ -397,7 +397,7 @@ class ColorSpace {
|
|||||||
if (colorSpaces instanceof Dict) {
|
if (colorSpaces instanceof Dict) {
|
||||||
const resourcesCS = colorSpaces.get(cs.name);
|
const resourcesCS = colorSpaces.get(cs.name);
|
||||||
if (resourcesCS) {
|
if (resourcesCS) {
|
||||||
if (isName(resourcesCS)) {
|
if (resourcesCS instanceof Name) {
|
||||||
return this._parse(
|
return this._parse(
|
||||||
resourcesCS,
|
resourcesCS,
|
||||||
xref,
|
xref,
|
||||||
|
@ -1647,7 +1647,7 @@ const CipherTransformFactory = (function CipherTransformFactoryClosure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function buildCipherConstructor(cf, name, num, gen, key) {
|
function buildCipherConstructor(cf, name, num, gen, key) {
|
||||||
if (!isName(name)) {
|
if (!(name instanceof Name)) {
|
||||||
throw new FormatError("Invalid crypt filter name.");
|
throw new FormatError("Invalid crypt filter name.");
|
||||||
}
|
}
|
||||||
const cryptFilter = cf.get(name.name);
|
const cryptFilter = cf.get(name.name);
|
||||||
@ -1713,7 +1713,7 @@ const CipherTransformFactory = (function CipherTransformFactoryClosure() {
|
|||||||
// Trying to find default handler -- it usually has Length.
|
// Trying to find default handler -- it usually has Length.
|
||||||
const cfDict = dict.get("CF");
|
const cfDict = dict.get("CF");
|
||||||
const streamCryptoName = dict.get("StmF");
|
const streamCryptoName = dict.get("StmF");
|
||||||
if (cfDict instanceof Dict && isName(streamCryptoName)) {
|
if (cfDict instanceof Dict && streamCryptoName instanceof Name) {
|
||||||
cfDict.suppressEncryption = true; // See comment below.
|
cfDict.suppressEncryption = true; // See comment below.
|
||||||
const handlerDict = cfDict.get(streamCryptoName.name);
|
const handlerDict = cfDict.get(streamCryptoName.name);
|
||||||
keyLength = (handlerDict && handlerDict.get("Length")) || 128;
|
keyLength = (handlerDict && handlerDict.get("Length")) || 128;
|
||||||
|
@ -1196,7 +1196,7 @@ class PDFDocument {
|
|||||||
let customValue;
|
let customValue;
|
||||||
if (isString(value)) {
|
if (isString(value)) {
|
||||||
customValue = stringToPDFString(value);
|
customValue = stringToPDFString(value);
|
||||||
} else if (isName(value) || isNum(value) || isBool(value)) {
|
} else if (value instanceof Name || isNum(value) || isBool(value)) {
|
||||||
customValue = value;
|
customValue = value;
|
||||||
} else {
|
} else {
|
||||||
info(`Unsupported value in document info for (custom) "${key}".`);
|
info(`Unsupported value in document info for (custom) "${key}".`);
|
||||||
|
@ -124,7 +124,7 @@ function normalizeBlendMode(value, parsingArray = false) {
|
|||||||
return "source-over";
|
return "source-over";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isName(value)) {
|
if (!(value instanceof Name)) {
|
||||||
if (parsingArray) {
|
if (parsingArray) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -1450,7 +1450,7 @@ class PartialEvaluator {
|
|||||||
}
|
}
|
||||||
const length = array.length;
|
const length = array.length;
|
||||||
const operator = this.xref.fetchIfRef(array[0]);
|
const operator = this.xref.fetchIfRef(array[0]);
|
||||||
if (length < 2 || !isName(operator)) {
|
if (length < 2 || !(operator instanceof Name)) {
|
||||||
warn("Invalid visibility expression");
|
warn("Invalid visibility expression");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1481,7 +1481,7 @@ class PartialEvaluator {
|
|||||||
|
|
||||||
async parseMarkedContentProps(contentProperties, resources) {
|
async parseMarkedContentProps(contentProperties, resources) {
|
||||||
let optionalContent;
|
let optionalContent;
|
||||||
if (isName(contentProperties)) {
|
if (contentProperties instanceof Name) {
|
||||||
const properties = resources.get("Properties");
|
const properties = resources.get("Properties");
|
||||||
optionalContent = properties.get(contentProperties.name);
|
optionalContent = properties.get(contentProperties.name);
|
||||||
} else if (contentProperties instanceof Dict) {
|
} else if (contentProperties instanceof Dict) {
|
||||||
@ -1527,9 +1527,10 @@ class PartialEvaluator {
|
|||||||
return {
|
return {
|
||||||
type: optionalContentType,
|
type: optionalContentType,
|
||||||
ids: groupIds,
|
ids: groupIds,
|
||||||
policy: isName(optionalContent.get("P"))
|
policy:
|
||||||
? optionalContent.get("P").name
|
optionalContent.get("P") instanceof Name
|
||||||
: null,
|
? optionalContent.get("P").name
|
||||||
|
: null,
|
||||||
expression: null,
|
expression: null,
|
||||||
};
|
};
|
||||||
} else if (optionalContentGroups instanceof Ref) {
|
} else if (optionalContentGroups instanceof Ref) {
|
||||||
@ -1658,7 +1659,7 @@ class PartialEvaluator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const type = xobj.dict.get("Subtype");
|
const type = xobj.dict.get("Subtype");
|
||||||
if (!isName(type)) {
|
if (!(type instanceof Name)) {
|
||||||
throw new FormatError("XObject should have a Name subtype");
|
throw new FormatError("XObject should have a Name subtype");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2058,7 +2059,7 @@ class PartialEvaluator {
|
|||||||
// but doing so is meaningless without knowing the semantics.
|
// but doing so is meaningless without knowing the semantics.
|
||||||
continue;
|
continue;
|
||||||
case OPS.beginMarkedContentProps:
|
case OPS.beginMarkedContentProps:
|
||||||
if (!isName(args[0])) {
|
if (!(args[0] instanceof Name)) {
|
||||||
warn(`Expected name for beginMarkedContentProps arg0=${args[0]}`);
|
warn(`Expected name for beginMarkedContentProps arg0=${args[0]}`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -2980,7 +2981,7 @@ class PartialEvaluator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const type = xobj.dict.get("Subtype");
|
const type = xobj.dict.get("Subtype");
|
||||||
if (!isName(type)) {
|
if (!(type instanceof Name)) {
|
||||||
throw new FormatError("XObject should have a Name subtype");
|
throw new FormatError("XObject should have a Name subtype");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3116,7 +3117,7 @@ class PartialEvaluator {
|
|||||||
if (includeMarkedContent) {
|
if (includeMarkedContent) {
|
||||||
textContent.items.push({
|
textContent.items.push({
|
||||||
type: "beginMarkedContent",
|
type: "beginMarkedContent",
|
||||||
tag: isName(args[0]) ? args[0].name : null,
|
tag: args[0] instanceof Name ? args[0].name : null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -3132,7 +3133,7 @@ class PartialEvaluator {
|
|||||||
id: Number.isInteger(mcid)
|
id: Number.isInteger(mcid)
|
||||||
? `${self.idFactory.getPageObjId()}_mcid${mcid}`
|
? `${self.idFactory.getPageObjId()}_mcid${mcid}`
|
||||||
: null,
|
: null,
|
||||||
tag: isName(args[0]) ? args[0].name : null,
|
tag: args[0] instanceof Name ? args[0].name : null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -3215,9 +3216,8 @@ class PartialEvaluator {
|
|||||||
encoding = dict.get("Encoding");
|
encoding = dict.get("Encoding");
|
||||||
if (encoding instanceof Dict) {
|
if (encoding instanceof Dict) {
|
||||||
baseEncodingName = encoding.get("BaseEncoding");
|
baseEncodingName = encoding.get("BaseEncoding");
|
||||||
baseEncodingName = isName(baseEncodingName)
|
baseEncodingName =
|
||||||
? baseEncodingName.name
|
baseEncodingName instanceof Name ? baseEncodingName.name : null;
|
||||||
: null;
|
|
||||||
// Load the differences between the base and original
|
// Load the differences between the base and original
|
||||||
if (encoding.has("Differences")) {
|
if (encoding.has("Differences")) {
|
||||||
const diffEncoding = encoding.get("Differences");
|
const diffEncoding = encoding.get("Differences");
|
||||||
@ -3226,7 +3226,7 @@ class PartialEvaluator {
|
|||||||
const data = xref.fetchIfRef(diffEncoding[j]);
|
const data = xref.fetchIfRef(diffEncoding[j]);
|
||||||
if (isNum(data)) {
|
if (isNum(data)) {
|
||||||
index = data;
|
index = data;
|
||||||
} else if (isName(data)) {
|
} else if (data instanceof Name) {
|
||||||
differences[index++] = data.name;
|
differences[index++] = data.name;
|
||||||
} else {
|
} else {
|
||||||
throw new FormatError(
|
throw new FormatError(
|
||||||
@ -3235,7 +3235,7 @@ class PartialEvaluator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isName(encoding)) {
|
} else if (encoding instanceof Name) {
|
||||||
baseEncodingName = encoding.name;
|
baseEncodingName = encoding.name;
|
||||||
} else {
|
} else {
|
||||||
throw new FormatError("Encoding is not a Name nor a Dict");
|
throw new FormatError("Encoding is not a Name nor a Dict");
|
||||||
@ -3487,7 +3487,7 @@ class PartialEvaluator {
|
|||||||
if (!cmapObj) {
|
if (!cmapObj) {
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
}
|
}
|
||||||
if (isName(cmapObj)) {
|
if (cmapObj instanceof Name) {
|
||||||
return CMapFactory.create({
|
return CMapFactory.create({
|
||||||
encoding: cmapObj,
|
encoding: cmapObj,
|
||||||
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
|
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
|
||||||
@ -3639,7 +3639,7 @@ class PartialEvaluator {
|
|||||||
} else {
|
} else {
|
||||||
// Trying get the BaseFont metrics (see comment above).
|
// Trying get the BaseFont metrics (see comment above).
|
||||||
const baseFontName = dict.get("BaseFont");
|
const baseFontName = dict.get("BaseFont");
|
||||||
if (isName(baseFontName)) {
|
if (baseFontName instanceof Name) {
|
||||||
const metrics = this.getBaseFontMetrics(baseFontName.name);
|
const metrics = this.getBaseFontMetrics(baseFontName.name);
|
||||||
|
|
||||||
glyphsWidths = this.buildCharCodeToWidth(metrics.widths, properties);
|
glyphsWidths = this.buildCharCodeToWidth(metrics.widths, properties);
|
||||||
@ -3737,7 +3737,7 @@ class PartialEvaluator {
|
|||||||
preEvaluateFont(dict) {
|
preEvaluateFont(dict) {
|
||||||
const baseDict = dict;
|
const baseDict = dict;
|
||||||
let type = dict.get("Subtype");
|
let type = dict.get("Subtype");
|
||||||
if (!isName(type)) {
|
if (!(type instanceof Name)) {
|
||||||
throw new FormatError("invalid font Subtype");
|
throw new FormatError("invalid font Subtype");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3758,7 +3758,7 @@ class PartialEvaluator {
|
|||||||
throw new FormatError("Descendant font is not a dictionary.");
|
throw new FormatError("Descendant font is not a dictionary.");
|
||||||
}
|
}
|
||||||
type = dict.get("Subtype");
|
type = dict.get("Subtype");
|
||||||
if (!isName(type)) {
|
if (!(type instanceof Name)) {
|
||||||
throw new FormatError("invalid font Subtype");
|
throw new FormatError("invalid font Subtype");
|
||||||
}
|
}
|
||||||
composite = true;
|
composite = true;
|
||||||
@ -3771,13 +3771,13 @@ class PartialEvaluator {
|
|||||||
hash = new MurmurHash3_64();
|
hash = new MurmurHash3_64();
|
||||||
|
|
||||||
const encoding = baseDict.getRaw("Encoding");
|
const encoding = baseDict.getRaw("Encoding");
|
||||||
if (isName(encoding)) {
|
if (encoding instanceof Name) {
|
||||||
hash.update(encoding.name);
|
hash.update(encoding.name);
|
||||||
} else if (encoding instanceof Ref) {
|
} else if (encoding instanceof Ref) {
|
||||||
hash.update(encoding.toString());
|
hash.update(encoding.toString());
|
||||||
} else if (encoding instanceof Dict) {
|
} else if (encoding instanceof Dict) {
|
||||||
for (const entry of encoding.getRawValues()) {
|
for (const entry of encoding.getRawValues()) {
|
||||||
if (isName(entry)) {
|
if (entry instanceof Name) {
|
||||||
hash.update(entry.name);
|
hash.update(entry.name);
|
||||||
} else if (entry instanceof Ref) {
|
} else if (entry instanceof Ref) {
|
||||||
hash.update(entry.toString());
|
hash.update(entry.toString());
|
||||||
@ -3788,7 +3788,7 @@ class PartialEvaluator {
|
|||||||
|
|
||||||
for (let j = 0; j < diffLength; j++) {
|
for (let j = 0; j < diffLength; j++) {
|
||||||
const diffEntry = entry[j];
|
const diffEntry = entry[j];
|
||||||
if (isName(diffEntry)) {
|
if (diffEntry instanceof Name) {
|
||||||
diffBuf[j] = diffEntry.name;
|
diffBuf[j] = diffEntry.name;
|
||||||
} else if (isNum(diffEntry) || diffEntry instanceof Ref) {
|
} else if (isNum(diffEntry) || diffEntry instanceof Ref) {
|
||||||
diffBuf[j] = diffEntry.toString();
|
diffBuf[j] = diffEntry.toString();
|
||||||
@ -3812,7 +3812,7 @@ class PartialEvaluator {
|
|||||||
stream.end - stream.start
|
stream.end - stream.start
|
||||||
);
|
);
|
||||||
hash.update(uint8array);
|
hash.update(uint8array);
|
||||||
} else if (isName(toUnicode)) {
|
} else if (toUnicode instanceof Name) {
|
||||||
hash.update(toUnicode.name);
|
hash.update(toUnicode.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3900,7 +3900,7 @@ class PartialEvaluator {
|
|||||||
// FontDescriptor was not required.
|
// FontDescriptor was not required.
|
||||||
// This case is here for compatibility.
|
// This case is here for compatibility.
|
||||||
let baseFontName = dict.get("BaseFont");
|
let baseFontName = dict.get("BaseFont");
|
||||||
if (!isName(baseFontName)) {
|
if (!(baseFontName instanceof Name)) {
|
||||||
throw new FormatError("Base font is not specified");
|
throw new FormatError("Base font is not specified");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3996,7 +3996,7 @@ class PartialEvaluator {
|
|||||||
}
|
}
|
||||||
fontName = fontName || baseFont;
|
fontName = fontName || baseFont;
|
||||||
|
|
||||||
if (!isName(fontName)) {
|
if (!(fontName instanceof Name)) {
|
||||||
throw new FormatError("invalid font name");
|
throw new FormatError("invalid font name");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4080,7 +4080,7 @@ class PartialEvaluator {
|
|||||||
|
|
||||||
if (composite) {
|
if (composite) {
|
||||||
const cidEncoding = baseDict.get("Encoding");
|
const cidEncoding = baseDict.get("Encoding");
|
||||||
if (isName(cidEncoding)) {
|
if (cidEncoding instanceof Name) {
|
||||||
properties.cidEncoding = cidEncoding.name;
|
properties.cidEncoding = cidEncoding.name;
|
||||||
}
|
}
|
||||||
const cMap = await CMapFactory.create({
|
const cMap = await CMapFactory.create({
|
||||||
|
@ -14,12 +14,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { assert, FormatError, ImageKind, info, warn } from "../shared/util.js";
|
import { assert, FormatError, ImageKind, info, warn } from "../shared/util.js";
|
||||||
import { isName, Name } from "./primitives.js";
|
|
||||||
import { BaseStream } from "./base_stream.js";
|
import { BaseStream } from "./base_stream.js";
|
||||||
import { ColorSpace } from "./colorspace.js";
|
import { ColorSpace } from "./colorspace.js";
|
||||||
import { DecodeStream } from "./decode_stream.js";
|
import { DecodeStream } from "./decode_stream.js";
|
||||||
import { JpegStream } from "./jpeg_stream.js";
|
import { JpegStream } from "./jpeg_stream.js";
|
||||||
import { JpxImage } from "./jpx.js";
|
import { JpxImage } from "./jpx.js";
|
||||||
|
import { Name } from "./primitives.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decode and clamp a value. The formula is different from the spec because we
|
* Decode and clamp a value. The formula is different from the spec because we
|
||||||
@ -95,7 +95,7 @@ class PDFImage {
|
|||||||
const dict = image.dict;
|
const dict = image.dict;
|
||||||
|
|
||||||
const filter = dict.get("F", "Filter");
|
const filter = dict.get("F", "Filter");
|
||||||
if (isName(filter)) {
|
if (filter instanceof Name) {
|
||||||
switch (filter.name) {
|
switch (filter.name) {
|
||||||
case "JPXDecode":
|
case "JPXDecode":
|
||||||
const jpxImage = new JpxImage();
|
const jpxImage = new JpxImage();
|
||||||
|
@ -22,7 +22,7 @@ import {
|
|||||||
StreamType,
|
StreamType,
|
||||||
warn,
|
warn,
|
||||||
} from "../shared/util.js";
|
} from "../shared/util.js";
|
||||||
import { Cmd, Dict, EOF, isCmd, isName, Name, Ref } from "./primitives.js";
|
import { Cmd, Dict, EOF, isCmd, Name, Ref } from "./primitives.js";
|
||||||
import {
|
import {
|
||||||
isWhiteSpace,
|
isWhiteSpace,
|
||||||
MissingDataException,
|
MissingDataException,
|
||||||
@ -128,7 +128,7 @@ class Parser {
|
|||||||
case "<<": // dictionary or stream
|
case "<<": // dictionary or stream
|
||||||
const dict = new Dict(this.xref);
|
const dict = new Dict(this.xref);
|
||||||
while (!isCmd(this.buf1, ">>") && this.buf1 !== EOF) {
|
while (!isCmd(this.buf1, ">>") && this.buf1 !== EOF) {
|
||||||
if (!isName(this.buf1)) {
|
if (!(this.buf1 instanceof Name)) {
|
||||||
info("Malformed dictionary: key must be a name object");
|
info("Malformed dictionary: key must be a name object");
|
||||||
this.shift();
|
this.shift();
|
||||||
continue;
|
continue;
|
||||||
@ -489,7 +489,7 @@ class Parser {
|
|||||||
const dict = new Dict(this.xref);
|
const dict = new Dict(this.xref);
|
||||||
let dictLength;
|
let dictLength;
|
||||||
while (!isCmd(this.buf1, "ID") && this.buf1 !== EOF) {
|
while (!isCmd(this.buf1, "ID") && this.buf1 !== EOF) {
|
||||||
if (!isName(this.buf1)) {
|
if (!(this.buf1 instanceof Name)) {
|
||||||
throw new FormatError("Dictionary key must be a name object");
|
throw new FormatError("Dictionary key must be a name object");
|
||||||
}
|
}
|
||||||
const key = this.buf1.name;
|
const key = this.buf1.name;
|
||||||
@ -506,11 +506,11 @@ class Parser {
|
|||||||
// Extract the name of the first (i.e. the current) image filter.
|
// Extract the name of the first (i.e. the current) image filter.
|
||||||
const filter = dict.get("F", "Filter");
|
const filter = dict.get("F", "Filter");
|
||||||
let filterName;
|
let filterName;
|
||||||
if (isName(filter)) {
|
if (filter instanceof Name) {
|
||||||
filterName = filter.name;
|
filterName = filter.name;
|
||||||
} else if (Array.isArray(filter)) {
|
} else if (Array.isArray(filter)) {
|
||||||
const filterZero = this.xref.fetchIfRef(filter[0]);
|
const filterZero = this.xref.fetchIfRef(filter[0]);
|
||||||
if (isName(filterZero)) {
|
if (filterZero instanceof Name) {
|
||||||
filterName = filterZero.name;
|
filterName = filterZero.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -695,7 +695,7 @@ class Parser {
|
|||||||
let filter = dict.get("F", "Filter");
|
let filter = dict.get("F", "Filter");
|
||||||
let params = dict.get("DP", "DecodeParms");
|
let params = dict.get("DP", "DecodeParms");
|
||||||
|
|
||||||
if (isName(filter)) {
|
if (filter instanceof Name) {
|
||||||
if (Array.isArray(params)) {
|
if (Array.isArray(params)) {
|
||||||
warn("/DecodeParms should not be an Array, when /Filter is a Name.");
|
warn("/DecodeParms should not be an Array, when /Filter is a Name.");
|
||||||
}
|
}
|
||||||
@ -708,7 +708,7 @@ class Parser {
|
|||||||
const paramsArray = params;
|
const paramsArray = params;
|
||||||
for (let i = 0, ii = filterArray.length; i < ii; ++i) {
|
for (let i = 0, ii = filterArray.length; i < ii; ++i) {
|
||||||
filter = this.xref.fetchIfRef(filterArray[i]);
|
filter = this.xref.fetchIfRef(filterArray[i]);
|
||||||
if (!isName(filter)) {
|
if (!(filter instanceof Name)) {
|
||||||
throw new FormatError(`Bad filter name "${filter}"`);
|
throw new FormatError(`Bad filter name "${filter}"`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Dict, isName, Ref } from "./primitives.js";
|
import { Dict, isName, Name, Ref } from "./primitives.js";
|
||||||
import { isString, stringToPDFString, warn } from "../shared/util.js";
|
import { isString, stringToPDFString, warn } from "../shared/util.js";
|
||||||
import { NumberTree } from "./name_number_tree.js";
|
import { NumberTree } from "./name_number_tree.js";
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ class StructTreeRoot {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
roleMapDict.forEach((key, value) => {
|
roleMapDict.forEach((key, value) => {
|
||||||
if (!isName(value)) {
|
if (!(value instanceof Name)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.roleMap.set(key, value.name);
|
this.roleMap.set(key, value.name);
|
||||||
@ -64,7 +64,7 @@ class StructElementNode {
|
|||||||
|
|
||||||
get role() {
|
get role() {
|
||||||
const nameObj = this.dict.get("S");
|
const nameObj = this.dict.get("S");
|
||||||
const name = isName(nameObj) ? nameObj.name : "";
|
const name = nameObj instanceof Name ? nameObj.name : "";
|
||||||
const { root } = this.tree;
|
const { root } = this.tree;
|
||||||
if (root.roleMap.has(name)) {
|
if (root.roleMap.has(name)) {
|
||||||
return root.roleMap.get(name);
|
return root.roleMap.get(name);
|
||||||
@ -123,7 +123,8 @@ class StructElementNode {
|
|||||||
pageObjId = pageRef.toString();
|
pageObjId = pageRef.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
const type = isName(kidDict.get("Type")) ? kidDict.get("Type").name : null;
|
const type =
|
||||||
|
kidDict.get("Type") instanceof Name ? kidDict.get("Type").name : null;
|
||||||
if (type === "MCR") {
|
if (type === "MCR") {
|
||||||
if (this.tree.pageDict.objId !== pageObjId) {
|
if (this.tree.pageDict.objId !== pageObjId) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { bytesToString, escapeString, warn } from "../shared/util.js";
|
import { bytesToString, escapeString, warn } from "../shared/util.js";
|
||||||
import { Dict, isName, Name, Ref } from "./primitives.js";
|
import { Dict, Name, Ref } from "./primitives.js";
|
||||||
import { escapePDFName, parseXFAPath } from "./core_utils.js";
|
import { escapePDFName, parseXFAPath } from "./core_utils.js";
|
||||||
import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js";
|
import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js";
|
||||||
import { BaseStream } from "./base_stream.js";
|
import { BaseStream } from "./base_stream.js";
|
||||||
@ -71,7 +71,7 @@ function numberToString(value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function writeValue(value, buffer, transform) {
|
function writeValue(value, buffer, transform) {
|
||||||
if (isName(value)) {
|
if (value instanceof Name) {
|
||||||
buffer.push(`/${escapePDFName(value.name)}`);
|
buffer.push(`/${escapePDFName(value.name)}`);
|
||||||
} else if (value instanceof Ref) {
|
} else if (value instanceof Ref) {
|
||||||
buffer.push(`${value.num} ${value.gen} R`);
|
buffer.push(`${value.num} ${value.gen} R`);
|
||||||
|
@ -470,6 +470,8 @@ describe("primitives", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("isName", function () {
|
describe("isName", function () {
|
||||||
|
/* eslint-disable no-restricted-syntax */
|
||||||
|
|
||||||
it("handles non-names", function () {
|
it("handles non-names", function () {
|
||||||
const nonName = {};
|
const nonName = {};
|
||||||
expect(isName(nonName)).toEqual(false);
|
expect(isName(nonName)).toEqual(false);
|
||||||
@ -493,6 +495,8 @@ describe("primitives", function () {
|
|||||||
expect(isName(emptyName, "")).toEqual(true);
|
expect(isName(emptyName, "")).toEqual(true);
|
||||||
expect(isName(emptyName, "string")).toEqual(false);
|
expect(isName(emptyName, "string")).toEqual(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* eslint-enable no-restricted-syntax */
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("isCmd", function () {
|
describe("isCmd", function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user