From d950b91c4eb1cbeb10a258981d08d39d902f9692 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 17 Jul 2022 11:24:05 +0200 Subject: [PATCH] Introduce some logical assignment in the `src/core/` folder --- src/core/annotation.js | 13 +++++----- src/core/catalog.js | 6 +---- src/core/colorspace.js | 10 ++++---- src/core/core_utils.js | 7 ++---- src/core/document.js | 42 +++++++++++++-------------------- src/core/evaluator.js | 19 +++++++-------- src/core/fonts.js | 10 +++----- src/core/operator_list.js | 2 +- src/core/parser.js | 2 +- src/core/pattern.js | 7 ++---- src/core/pdf_manager.js | 3 +-- src/core/primitives.js | 6 ++--- src/core/ps_parser.js | 6 +---- src/core/worker.js | 7 ++---- src/core/xfa/formcalc_parser.js | 4 ++-- src/core/xfa/parser.js | 5 +--- src/core/xfa/template.js | 8 +++---- src/core/xfa/utils.js | 2 +- src/display/annotation_layer.js | 2 +- src/display/api.js | 4 +--- 20 files changed, 62 insertions(+), 103 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index 8963a807c..3a88c42f0 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -1556,11 +1556,10 @@ class WidgetAnnotation extends Annotation { this.setDefaultAppearance(params); - data.hasAppearance = - (this._needAppearances && - data.fieldValue !== undefined && - data.fieldValue !== null) || - data.hasAppearance; + data.hasAppearance ||= + this._needAppearances && + data.fieldValue !== undefined && + data.fieldValue !== null; const fieldType = getInheritableProperty({ dict, key: "FT" }); data.fieldType = fieldType instanceof Name ? fieldType.name : null; @@ -1808,7 +1807,7 @@ class WidgetAnnotation extends Annotation { if (!this._hasValueFromXFA && rotation === undefined) { return null; } - value = value || this.data.fieldValue; + value ||= this.data.fieldValue; } // Value can be an array (with choice list and multiple selections) @@ -3472,7 +3471,7 @@ class LinkAnnotation extends Annotation { } // The color entry for a link annotation is the color of the border. - this.data.borderColor = this.data.borderColor || this.data.color; + this.data.borderColor ||= this.data.color; Catalog.parseDestDictionary({ destDict: params.dict, diff --git a/src/core/catalog.js b/src/core/catalog.js index 6cb33baa3..92811ebe5 100644 --- a/src/core/catalog.js +++ b/src/core/catalog.js @@ -985,12 +985,8 @@ class Catalog { } else if (typeof js !== "string") { return; } - - if (javaScript === null) { - javaScript = new Map(); - } js = stringToPDFString(js).replaceAll("\x00", ""); - javaScript.set(name, js); + (javaScript ||= new Map()).set(name, js); } if (obj instanceof Dict && obj.has("JavaScript")) { diff --git a/src/core/colorspace.js b/src/core/colorspace.js index 640249e5e..bb72f5e86 100644 --- a/src/core/colorspace.js +++ b/src/core/colorspace.js @@ -1220,9 +1220,9 @@ const CalRGBCS = (function CalRGBCSClosure() { "WhitePoint missing - required for color space CalRGB" ); } - blackPoint = blackPoint || new Float32Array(3); - gamma = gamma || new Float32Array([1, 1, 1]); - matrix = matrix || new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]); + blackPoint ||= new Float32Array(3); + gamma ||= new Float32Array([1, 1, 1]); + matrix ||= new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]); // Translate arguments to spec variables. const XW = whitePoint[0]; @@ -1396,8 +1396,8 @@ const LabCS = (function LabCSClosure() { "WhitePoint missing - required for color space Lab" ); } - blackPoint = blackPoint || [0, 0, 0]; - range = range || [-100, 100, -100, 100]; + blackPoint ||= [0, 0, 0]; + range ||= [-100, 100, -100, 100]; // Translate args to spec variables this.XW = whitePoint[0]; diff --git a/src/core/core_utils.js b/src/core/core_utils.js index 44b9e8717..de73473f4 100644 --- a/src/core/core_utils.js +++ b/src/core/core_utils.js @@ -137,10 +137,7 @@ function getInheritableProperty({ if (stopWhenFound) { return value; } - if (!values) { - values = []; - } - values.push(value); + (values ||= []).push(value); } dict = dict.get("Parent"); } @@ -322,7 +319,7 @@ function _collectJS(entry, xref, list, parents) { } else if (typeof js === "string") { code = js; } - code = code && stringToPDFString(code).replaceAll("\x00", ""); + code &&= stringToPDFString(code).replaceAll("\x00", ""); if (code) { list.push(code); } diff --git a/src/core/document.js b/src/core/document.js index 13ebc1173..f36e77d50 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -495,12 +495,8 @@ class Page { for (const { opList, separateForm, separateCanvas } of opLists) { pageOpList.addOpList(opList); - if (separateForm) { - form = separateForm; - } - if (separateCanvas) { - canvas = separateCanvas; - } + form ||= separateForm; + canvas ||= separateCanvas; } pageOpList.flush( /* lastChunk = */ true, @@ -580,8 +576,8 @@ class Page { return []; } - const textContentPromises = []; - const annotationsData = []; + const annotationsData = [], + textContentPromises = []; let partialEvaluator; const intentAny = !!(intent & RenderingIntentFlag.ANY), @@ -597,19 +593,18 @@ class Page { } if (annotation.hasTextContent && isVisible) { - if (!partialEvaluator) { - partialEvaluator = new PartialEvaluator({ - xref: this.xref, - handler, - pageIndex: this.pageIndex, - idFactory: this._localIdFactory, - fontCache: this.fontCache, - builtInCMapCache: this.builtInCMapCache, - standardFontDataCache: this.standardFontDataCache, - globalImageCache: this.globalImageCache, - options: this.evaluatorOptions, - }); - } + partialEvaluator ||= new PartialEvaluator({ + xref: this.xref, + handler, + pageIndex: this.pageIndex, + idFactory: this._localIdFactory, + fontCache: this.fontCache, + builtInCMapCache: this.builtInCMapCache, + standardFontDataCache: this.standardFontDataCache, + globalImageCache: this.globalImageCache, + options: this.evaluatorOptions, + }); + textContentPromises.push( annotation .extractTextContent(partialEvaluator, task, this.view) @@ -665,10 +660,7 @@ class Page { continue; } if (annotation instanceof PopupAnnotation) { - if (!popupAnnotations) { - popupAnnotations = []; - } - popupAnnotations.push(annotation); + (popupAnnotations ||= []).push(annotation); continue; } sortedAnnotations.push(annotation); diff --git a/src/core/evaluator.js b/src/core/evaluator.js index d0248e8ee..40d12bf74 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -519,7 +519,7 @@ class PartialEvaluator { } if (smask && smask.backdrop) { - colorSpace = colorSpace || ColorSpace.singletons.rgb; + colorSpace ||= ColorSpace.singletons.rgb; smask.backdrop = colorSpace.getRgb(smask.backdrop, 0); } @@ -1272,10 +1272,7 @@ class PartialEvaluator { } if (hash && descriptor instanceof Dict) { - if (!descriptor.fontAliases) { - descriptor.fontAliases = Object.create(null); - } - const fontAliases = descriptor.fontAliases; + const fontAliases = (descriptor.fontAliases ||= Object.create(null)); if (fontAliases[hash]) { const aliasFontRef = fontAliases[hash].aliasRef; @@ -1668,8 +1665,8 @@ class PartialEvaluator { }) { // Ensure that `resources`/`initialState` is correctly initialized, // even if the provided parameter is e.g. `null`. - resources = resources || Dict.empty; - initialState = initialState || new EvalState(); + resources ||= Dict.empty; + initialState ||= new EvalState(); if (!operatorList) { throw new Error('getOperatorList: missing "operatorList" parameter'); @@ -2276,11 +2273,11 @@ class PartialEvaluator { }) { // Ensure that `resources`/`stateManager` is correctly initialized, // even if the provided parameter is e.g. `null`. - resources = resources || Dict.empty; - stateManager = stateManager || new StateManager(new TextState()); + resources ||= Dict.empty; + stateManager ||= new StateManager(new TextState()); if (includeMarkedContent) { - markedContentData = markedContentData || { level: 0 }; + markedContentData ||= { level: 0 }; } const textContent = { @@ -4255,7 +4252,7 @@ class PartialEvaluator { } } } - fontName = fontName || baseFont; + fontName ||= baseFont; if (!(fontName instanceof Name)) { throw new FormatError("invalid font name"); diff --git a/src/core/fonts.js b/src/core/fonts.js index 6ea70389f..12803fcd2 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -743,7 +743,7 @@ function validateOS2Table(os2, file) { } function createOS2Table(properties, charstrings, override) { - override = override || { + override ||= { unitsPerEm: 0, yMax: 0, yMin: 0, @@ -3090,10 +3090,7 @@ class Font { let charCodes = null; for (const charCode in charCodeToGlyphId) { if (glyphId === charCodeToGlyphId[charCode]) { - if (!charCodes) { - charCodes = []; - } - charCodes.push(charCode | 0); + (charCodes ||= []).push(charCode | 0); } } return charCodes; @@ -3285,8 +3282,7 @@ class Font { break; // the non-zero width found } } - width = width || this.defaultWidth; - return shadow(this, "spaceWidth", width); + return shadow(this, "spaceWidth", width || this.defaultWidth); } /** diff --git a/src/core/operator_list.js b/src/core/operator_list.js index 2815923b7..7a49091b1 100644 --- a/src/core/operator_list.js +++ b/src/core/operator_list.js @@ -25,7 +25,7 @@ function addState(parentState, pattern, checkFn, iterateFn, processFn) { let state = parentState; for (let i = 0, ii = pattern.length - 1; i < ii; i++) { const item = pattern[i]; - state = state[item] || (state[item] = []); + state = state[item] ||= []; } state[pattern.at(-1)] = { checkFn, diff --git a/src/core/parser.js b/src/core/parser.js index aad5f3371..94b32d9b2 100644 --- a/src/core/parser.js +++ b/src/core/parser.js @@ -896,7 +896,7 @@ class Lexer { throw new FormatError(msg); } - sign = sign || 1; + sign ||= 1; let baseValue = ch - 0x30; // '0' let powerValue = 0; let powerValueSign = 1; diff --git a/src/core/pattern.js b/src/core/pattern.js index ad89e4fb7..32471a283 100644 --- a/src/core/pattern.js +++ b/src/core/pattern.js @@ -366,13 +366,10 @@ const getB = (function getBClosure() { } return lut; } - const cache = []; + const cache = Object.create(null); return function (count) { - if (!cache[count]) { - cache[count] = buildB(count); - } - return cache[count]; + return (cache[count] ||= buildB(count)); }; })(); diff --git a/src/core/pdf_manager.js b/src/core/pdf_manager.js index 611b067e8..397661d59 100644 --- a/src/core/pdf_manager.js +++ b/src/core/pdf_manager.js @@ -48,8 +48,7 @@ class BasePdfManager { // Check `OffscreenCanvas` support once, rather than repeatedly throughout // the worker-thread code. - args.evaluatorOptions.isOffscreenCanvasSupported = - args.evaluatorOptions.isOffscreenCanvasSupported && + args.evaluatorOptions.isOffscreenCanvasSupported &&= FeatureTest.isOffscreenCanvasSupported; this.evaluatorOptions = args.evaluatorOptions; } diff --git a/src/core/primitives.js b/src/core/primitives.js index c1719416a..76ec4231a 100644 --- a/src/core/primitives.js +++ b/src/core/primitives.js @@ -41,7 +41,7 @@ class Name { static get(name) { // eslint-disable-next-line no-restricted-syntax - return NameCache[name] || (NameCache[name] = new Name(name)); + return (NameCache[name] ||= new Name(name)); } } @@ -58,7 +58,7 @@ class Cmd { static get(cmd) { // eslint-disable-next-line no-restricted-syntax - return CmdCache[cmd] || (CmdCache[cmd] = new Cmd(cmd)); + return (CmdCache[cmd] ||= new Cmd(cmd)); } } @@ -282,7 +282,7 @@ class Ref { static get(num, gen) { const key = gen === 0 ? `${num}R` : `${num}R${gen}`; // eslint-disable-next-line no-restricted-syntax - return RefCache[key] || (RefCache[key] = new Ref(num, gen)); + return (RefCache[key] ||= new Ref(num, gen)); } } diff --git a/src/core/ps_parser.js b/src/core/ps_parser.js index a509bbd61..7ab24e73e 100644 --- a/src/core/ps_parser.js +++ b/src/core/ps_parser.js @@ -120,11 +120,7 @@ class PostScriptToken { } static getOperator(op) { - const opValue = PostScriptToken.opCache[op]; - if (opValue) { - return opValue; - } - return (PostScriptToken.opCache[op] = new PostScriptToken( + return (PostScriptToken.opCache[op] ||= new PostScriptToken( PostScriptTokenTypes.OPERATOR, op )); diff --git a/src/core/worker.js b/src/core/worker.js index 7c76578b9..14f5a576a 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -261,8 +261,7 @@ class WorkerMessageHandler { pdfManagerArgs.source = pdfStream; pdfManagerArgs.length = fullRequest.contentLength; // We don't need auto-fetch when streaming is enabled. - pdfManagerArgs.disableAutoFetch = - pdfManagerArgs.disableAutoFetch || fullRequest.isStreamingSupported; + pdfManagerArgs.disableAutoFetch ||= fullRequest.isStreamingSupported; newPdfManager = new NetworkPdfManager(pdfManagerArgs); // There may be a chance that `newPdfManager` is not initialized for @@ -661,7 +660,6 @@ class WorkerMessageHandler { }); } - const lastXRefStreamPos = xref.lastXRefStreamPos; newXrefInfo = { rootRef: xref.trailer.getRaw("Root") || null, encryptRef: xref.trailer.getRaw("Encrypt") || null, @@ -669,8 +667,7 @@ class WorkerMessageHandler { infoRef: xref.trailer.getRaw("Info") || null, info: infoObj, fileIds: xref.trailer.get("ID") || null, - startXRef: - lastXRefStreamPos === null ? startXRef : lastXRefStreamPos, + startXRef: xref.lastXRefStreamPos ?? startXRef, filename, }; } diff --git a/src/core/xfa/formcalc_parser.js b/src/core/xfa/formcalc_parser.js index 81964c21c..55a6ba326 100644 --- a/src/core/xfa/formcalc_parser.js +++ b/src/core/xfa/formcalc_parser.js @@ -275,7 +275,7 @@ class SimpleExprParser { } parse(tok) { - tok = tok || this.lexer.next(); + tok ||= this.lexer.next(); while (true) { // Token ids (see form_lexer.js) are consecutive in order @@ -1005,7 +1005,7 @@ class Parser { } parseExpr(tok) { - tok = tok || this.lexer.next(); + tok ||= this.lexer.next(); switch (tok.id) { case TOKEN.identifier: return this.parseAssigmentOrExpr(tok); diff --git a/src/core/xfa/parser.js b/src/core/xfa/parser.js index f8c86e293..27c7d98ad 100644 --- a/src/core/xfa/parser.js +++ b/src/core/xfa/parser.js @@ -107,10 +107,7 @@ class XFAParser extends XMLParserBase { nsAttrs = attributeObj[$nsAttributes] = Object.create(null); } const [ns, attrName] = [name.slice(0, i), name.slice(i + 1)]; - let attrs = nsAttrs[ns]; - if (!attrs) { - attrs = nsAttrs[ns] = Object.create(null); - } + const attrs = (nsAttrs[ns] ||= Object.create(null)); attrs[attrName] = value; } } diff --git a/src/core/xfa/template.js b/src/core/xfa/template.js index 3e3865b3d..3f5ac44cb 100644 --- a/src/core/xfa/template.js +++ b/src/core/xfa/template.js @@ -5573,8 +5573,7 @@ class Template extends XFAObject { const flush = index => { const html = root[$flushHTML](); if (html) { - hasSomething = - hasSomething || (html.children && html.children.length !== 0); + hasSomething ||= !!html.children && html.children.length !== 0; htmlContentAreas[index].children.push(html); } }; @@ -5597,9 +5596,8 @@ class Template extends XFAObject { const html = root[$toHTML](space); if (html.success) { if (html.html) { - hasSomething = - hasSomething || - (html.html.children && html.html.children.length !== 0); + hasSomething ||= + !!html.html.children && html.html.children.length !== 0; htmlContentAreas[i].children.push(html.html); } else if (!hasSomething && mainHtml.children.length > 1) { mainHtml.children.pop(); diff --git a/src/core/xfa/utils.js b/src/core/xfa/utils.js index 5f93bd458..14509202f 100644 --- a/src/core/xfa/utils.js +++ b/src/core/xfa/utils.js @@ -75,7 +75,7 @@ function getStringOption(data, options) { } function getMeasurement(str, def = "0") { - def = def || "0"; + def ||= "0"; if (!str) { return getMeasurement(def); } diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index 25d747dc4..5dbdd0248 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -451,7 +451,7 @@ class AnnotationElement { _createPopup(trigger, data) { let container = this.container; if (this.quadrilaterals) { - trigger = trigger || this.quadrilaterals; + trigger ||= this.quadrilaterals; container = this.quadrilaterals[0]; } diff --git a/src/display/api.js b/src/display/api.js index 4edd7bf0e..10ba9a97a 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -3356,9 +3356,7 @@ class InternalRenderTask { operatorListChanged() { if (!this.graphicsReady) { - if (!this.graphicsReadyCallback) { - this.graphicsReadyCallback = this._continueBound; - } + this.graphicsReadyCallback ||= this._continueBound; return; } this.stepper?.updateOperatorList(this.operatorList);