1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-25 17:48:07 +02:00

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:
Jonas Jenwald 2022-02-21 12:45:00 +01:00
parent 4df82ad31e
commit b282814e38
13 changed files with 85 additions and 73 deletions

View file

@ -124,7 +124,7 @@ function normalizeBlendMode(value, parsingArray = false) {
return "source-over";
}
if (!isName(value)) {
if (!(value instanceof Name)) {
if (parsingArray) {
return null;
}
@ -1450,7 +1450,7 @@ class PartialEvaluator {
}
const length = array.length;
const operator = this.xref.fetchIfRef(array[0]);
if (length < 2 || !isName(operator)) {
if (length < 2 || !(operator instanceof Name)) {
warn("Invalid visibility expression");
return;
}
@ -1481,7 +1481,7 @@ class PartialEvaluator {
async parseMarkedContentProps(contentProperties, resources) {
let optionalContent;
if (isName(contentProperties)) {
if (contentProperties instanceof Name) {
const properties = resources.get("Properties");
optionalContent = properties.get(contentProperties.name);
} else if (contentProperties instanceof Dict) {
@ -1527,9 +1527,10 @@ class PartialEvaluator {
return {
type: optionalContentType,
ids: groupIds,
policy: isName(optionalContent.get("P"))
? optionalContent.get("P").name
: null,
policy:
optionalContent.get("P") instanceof Name
? optionalContent.get("P").name
: null,
expression: null,
};
} else if (optionalContentGroups instanceof Ref) {
@ -1658,7 +1659,7 @@ class PartialEvaluator {
}
const type = xobj.dict.get("Subtype");
if (!isName(type)) {
if (!(type instanceof Name)) {
throw new FormatError("XObject should have a Name subtype");
}
@ -2058,7 +2059,7 @@ class PartialEvaluator {
// but doing so is meaningless without knowing the semantics.
continue;
case OPS.beginMarkedContentProps:
if (!isName(args[0])) {
if (!(args[0] instanceof Name)) {
warn(`Expected name for beginMarkedContentProps arg0=${args[0]}`);
continue;
}
@ -2980,7 +2981,7 @@ class PartialEvaluator {
}
const type = xobj.dict.get("Subtype");
if (!isName(type)) {
if (!(type instanceof Name)) {
throw new FormatError("XObject should have a Name subtype");
}
@ -3116,7 +3117,7 @@ class PartialEvaluator {
if (includeMarkedContent) {
textContent.items.push({
type: "beginMarkedContent",
tag: isName(args[0]) ? args[0].name : null,
tag: args[0] instanceof Name ? args[0].name : null,
});
}
break;
@ -3132,7 +3133,7 @@ class PartialEvaluator {
id: Number.isInteger(mcid)
? `${self.idFactory.getPageObjId()}_mcid${mcid}`
: null,
tag: isName(args[0]) ? args[0].name : null,
tag: args[0] instanceof Name ? args[0].name : null,
});
}
break;
@ -3215,9 +3216,8 @@ class PartialEvaluator {
encoding = dict.get("Encoding");
if (encoding instanceof Dict) {
baseEncodingName = encoding.get("BaseEncoding");
baseEncodingName = isName(baseEncodingName)
? baseEncodingName.name
: null;
baseEncodingName =
baseEncodingName instanceof Name ? baseEncodingName.name : null;
// Load the differences between the base and original
if (encoding.has("Differences")) {
const diffEncoding = encoding.get("Differences");
@ -3226,7 +3226,7 @@ class PartialEvaluator {
const data = xref.fetchIfRef(diffEncoding[j]);
if (isNum(data)) {
index = data;
} else if (isName(data)) {
} else if (data instanceof Name) {
differences[index++] = data.name;
} else {
throw new FormatError(
@ -3235,7 +3235,7 @@ class PartialEvaluator {
}
}
}
} else if (isName(encoding)) {
} else if (encoding instanceof Name) {
baseEncodingName = encoding.name;
} else {
throw new FormatError("Encoding is not a Name nor a Dict");
@ -3487,7 +3487,7 @@ class PartialEvaluator {
if (!cmapObj) {
return Promise.resolve(null);
}
if (isName(cmapObj)) {
if (cmapObj instanceof Name) {
return CMapFactory.create({
encoding: cmapObj,
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
@ -3639,7 +3639,7 @@ class PartialEvaluator {
} else {
// Trying get the BaseFont metrics (see comment above).
const baseFontName = dict.get("BaseFont");
if (isName(baseFontName)) {
if (baseFontName instanceof Name) {
const metrics = this.getBaseFontMetrics(baseFontName.name);
glyphsWidths = this.buildCharCodeToWidth(metrics.widths, properties);
@ -3737,7 +3737,7 @@ class PartialEvaluator {
preEvaluateFont(dict) {
const baseDict = dict;
let type = dict.get("Subtype");
if (!isName(type)) {
if (!(type instanceof Name)) {
throw new FormatError("invalid font Subtype");
}
@ -3758,7 +3758,7 @@ class PartialEvaluator {
throw new FormatError("Descendant font is not a dictionary.");
}
type = dict.get("Subtype");
if (!isName(type)) {
if (!(type instanceof Name)) {
throw new FormatError("invalid font Subtype");
}
composite = true;
@ -3771,13 +3771,13 @@ class PartialEvaluator {
hash = new MurmurHash3_64();
const encoding = baseDict.getRaw("Encoding");
if (isName(encoding)) {
if (encoding instanceof Name) {
hash.update(encoding.name);
} else if (encoding instanceof Ref) {
hash.update(encoding.toString());
} else if (encoding instanceof Dict) {
for (const entry of encoding.getRawValues()) {
if (isName(entry)) {
if (entry instanceof Name) {
hash.update(entry.name);
} else if (entry instanceof Ref) {
hash.update(entry.toString());
@ -3788,7 +3788,7 @@ class PartialEvaluator {
for (let j = 0; j < diffLength; j++) {
const diffEntry = entry[j];
if (isName(diffEntry)) {
if (diffEntry instanceof Name) {
diffBuf[j] = diffEntry.name;
} else if (isNum(diffEntry) || diffEntry instanceof Ref) {
diffBuf[j] = diffEntry.toString();
@ -3812,7 +3812,7 @@ class PartialEvaluator {
stream.end - stream.start
);
hash.update(uint8array);
} else if (isName(toUnicode)) {
} else if (toUnicode instanceof Name) {
hash.update(toUnicode.name);
}
@ -3900,7 +3900,7 @@ class PartialEvaluator {
// FontDescriptor was not required.
// This case is here for compatibility.
let baseFontName = dict.get("BaseFont");
if (!isName(baseFontName)) {
if (!(baseFontName instanceof Name)) {
throw new FormatError("Base font is not specified");
}
@ -3996,7 +3996,7 @@ class PartialEvaluator {
}
fontName = fontName || baseFont;
if (!isName(fontName)) {
if (!(fontName instanceof Name)) {
throw new FormatError("invalid font name");
}
@ -4080,7 +4080,7 @@ class PartialEvaluator {
if (composite) {
const cidEncoding = baseDict.get("Encoding");
if (isName(cidEncoding)) {
if (cidEncoding instanceof Name) {
properties.cidEncoding = cidEncoding.name;
}
const cMap = await CMapFactory.create({