From e4758beaaa6b8af90e6f1875cf33bc2fe789941f Mon Sep 17 00:00:00 2001 From: Jonas Jenwald <jonas.jenwald@gmail.com> Date: Mon, 10 Feb 2020 09:38:33 +0100 Subject: [PATCH 1/2] Move `IsLittleEndianCached` and `IsEvalSupportedCached` to `src/shared/util.js` Rather than duplicating the lookup and caching in multiple files, it seems easier to simply move all of this functionality into `src/shared/util.js` instead. This will also help avoid a bunch of ESLint errors once the `no-shadow` rule is eventually enabled. --- src/core/function.js | 9 +-------- src/display/canvas.js | 8 +------- src/display/font_loader.js | 8 +------- src/shared/util.js | 17 +++++++++++++---- 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/core/function.js b/src/core/function.js index 5cc4247d9..a7ca6e837 100644 --- a/src/core/function.js +++ b/src/core/function.js @@ -17,19 +17,12 @@ import { FormatError, info, isBool, - isEvalSupported, - shadow, + IsEvalSupportedCached, unreachable, } from "../shared/util.js"; import { isDict, isStream } from "./primitives.js"; import { PostScriptLexer, PostScriptParser } from "./ps_parser.js"; -const IsEvalSupportedCached = { - get value() { - return shadow(this, "value", isEvalSupported()); - }, -}; - class PDFFunctionFactory { constructor({ xref, isEvalSupported = true }) { this.xref = xref; diff --git a/src/display/canvas.js b/src/display/canvas.js index 833c538b7..8fc8c03cc 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -18,7 +18,7 @@ import { IDENTITY_MATRIX, ImageKind, info, - isLittleEndian, + IsLittleEndianCached, isNum, OPS, shadow, @@ -46,12 +46,6 @@ var MAX_SIZE_TO_COMPILE = 1000; var FULL_CHUNK_HEIGHT = 16; -var IsLittleEndianCached = { - get value() { - return shadow(IsLittleEndianCached, "value", isLittleEndian()); - }, -}; - function addContextCurrentTransform(ctx) { // If the context doesn't expose a `mozCurrentTransform`, add a JS based one. if (!ctx.mozCurrentTransform) { diff --git a/src/display/font_loader.js b/src/display/font_loader.js index c7cdd2625..e1f385a98 100644 --- a/src/display/font_loader.js +++ b/src/display/font_loader.js @@ -16,7 +16,7 @@ import { assert, bytesToString, - isEvalSupported, + IsEvalSupportedCached, shadow, string32, unreachable, @@ -337,12 +337,6 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { }; } // End of PDFJSDev.test('CHROME || GENERIC') -const IsEvalSupportedCached = { - get value() { - return shadow(this, "value", isEvalSupported()); - }, -}; - class FontFaceObject { constructor( translatedData, diff --git a/src/shared/util.js b/src/shared/util.js index 6df2ee036..a2a4bfc59 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -547,14 +547,18 @@ function string32(value) { ); } -// Lazy test the endianness of the platform -// NOTE: This will be 'true' for simulated TypedArrays +// Checks the endianness of the platform. function isLittleEndian() { const buffer8 = new Uint8Array(4); buffer8[0] = 1; const view32 = new Uint32Array(buffer8.buffer, 0, 1); return view32[0] === 1; } +const IsLittleEndianCached = { + get value() { + return shadow(this, "value", isLittleEndian()); + }, +}; // Checks if it's possible to eval JS expressions. function isEvalSupported() { @@ -565,6 +569,11 @@ function isEvalSupported() { return false; } } +const IsEvalSupportedCached = { + get value() { + return shadow(this, "value", isEvalSupported()); + }, +}; const rgbBuf = ["rgb(", 0, ",", 0, ",", 0, ")"]; @@ -918,8 +927,8 @@ export { isString, isSameOrigin, createValidAbsoluteUrl, - isLittleEndian, - isEvalSupported, + IsLittleEndianCached, + IsEvalSupportedCached, removeNullCharacters, setVerbosityLevel, shadow, From c5f67300e9b36542fcb1b5df517bfa6b9ac811fe Mon Sep 17 00:00:00 2001 From: Jonas Jenwald <jonas.jenwald@gmail.com> Date: Mon, 10 Feb 2020 09:38:57 +0100 Subject: [PATCH 2/2] Rename the `isSpace` helper function to `isWhiteSpace` Trying to enable the ESLint rule `no-shadow`, against the `master` branch, would result in a fair number of errors in the `Glyph` class in `src/core/fonts.js`. Since the glyphs are exposed through the API, we can't very well change the `isSpace` property on `Glyph` instances. Thus the best approach seems, at least to me, to simply rename the `isSpace` helper function to `isWhiteSpace` which shouldn't cause any issues given that it's only used in the `src/core/` folder. --- src/core/core_utils.js | 4 ++-- src/core/document.js | 4 ++-- src/core/fonts.js | 8 ++++++-- src/core/parser.js | 10 +++++----- src/core/ps_parser.js | 4 ++-- src/core/stream.js | 6 +++--- src/core/type1_parser.js | 6 +++--- test/unit/core_utils_spec.js | 18 +++++++++--------- 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/core/core_utils.js b/src/core/core_utils.js index 1be5c978a..dd20bf006 100644 --- a/src/core/core_utils.js +++ b/src/core/core_utils.js @@ -161,7 +161,7 @@ function readUint32(data, offset) { } // Checks if ch is one of the following characters: SPACE, TAB, CR or LF. -function isSpace(ch) { +function isWhiteSpace(ch) { return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a; } @@ -176,5 +176,5 @@ export { readInt8, readUint16, readUint32, - isSpace, + isWhiteSpace, }; diff --git a/src/core/document.js b/src/core/document.js index 0def19020..32ea4740a 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -42,7 +42,7 @@ import { } from "./primitives.js"; import { getInheritableProperty, - isSpace, + isWhiteSpace, MissingDataException, XRefEntryException, XRefParseException, @@ -598,7 +598,7 @@ class PDFDocument { let ch; do { ch = stream.getByte(); - } while (isSpace(ch)); + } while (isWhiteSpace(ch)); let str = ""; while (ch >= /* Space = */ 0x20 && ch <= /* '9' = */ 0x39) { str += String.fromCharCode(ch); diff --git a/src/core/fonts.js b/src/core/fonts.js index 09fafec1c..98e29f3b6 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -58,7 +58,11 @@ import { getUnicodeRangeFor, mapSpecialUnicodeValues, } from "./unicode.js"; -import { isSpace, MissingDataException, readUint32 } from "./core_utils.js"; +import { + isWhiteSpace, + MissingDataException, + readUint32, +} from "./core_utils.js"; import { FontRendererFactory } from "./font_renderer.js"; import { IdentityCMap } from "./cmap.js"; import { Stream } from "./stream.js"; @@ -3413,7 +3417,7 @@ var Type1Font = (function Type1FontClosure() { if (j >= signatureLength) { // `signature` found, skip over whitespace. i += j; - while (i < streamBytesLength && isSpace(streamBytes[i])) { + while (i < streamBytesLength && isWhiteSpace(streamBytes[i])) { i++; } found = true; diff --git a/src/core/parser.js b/src/core/parser.js index 1a64455e1..7f0871daf 100644 --- a/src/core/parser.js +++ b/src/core/parser.js @@ -43,7 +43,7 @@ import { Name, Ref, } from "./primitives.js"; -import { isSpace, MissingDataException } from "./core_utils.js"; +import { isWhiteSpace, MissingDataException } from "./core_utils.js"; import { CCITTFaxStream } from "./ccitt_stream.js"; import { Jbig2Stream } from "./jbig2_stream.js"; import { JpegStream } from "./jpeg_stream.js"; @@ -270,7 +270,7 @@ class Parser { // Ensure that we don't accidentally truncate the inline image, when the // data is immediately followed by the "EI" marker (fixes issue10388.pdf). - if (!isSpace(ch)) { + if (!isWhiteSpace(ch)) { endOffset--; } return stream.pos - endOffset - startPos; @@ -394,7 +394,7 @@ class Parser { ch = stream.peekByte(); // Handle corrupt PDF documents which contains whitespace "inside" of // the EOD marker (fixes issue10614.pdf). - while (isSpace(ch)) { + while (isWhiteSpace(ch)) { stream.skip(); ch = stream.peekByte(); } @@ -640,7 +640,7 @@ class Parser { // Ensure that the byte immediately following the truncated // endstream command is a space, to prevent false positives. const lastByte = stream.peekBytes(end + 1)[end]; - if (!isSpace(lastByte)) { + if (!isWhiteSpace(lastByte)) { break; } info( @@ -886,7 +886,7 @@ class Lexer { if ( divideBy === 10 && sign === 0 && - (isSpace(ch) || ch === /* EOF = */ -1) + (isWhiteSpace(ch) || ch === /* EOF = */ -1) ) { // This is consistent with Adobe Reader (fixes issue9252.pdf). warn("Lexer.getNumber - treating a single decimal point as zero."); diff --git a/src/core/ps_parser.js b/src/core/ps_parser.js index b3e873fcd..34d70e981 100644 --- a/src/core/ps_parser.js +++ b/src/core/ps_parser.js @@ -16,7 +16,7 @@ import { FormatError, shadow } from "../shared/util.js"; import { EOF } from "./primitives.js"; -import { isSpace } from "./core_utils.js"; +import { isWhiteSpace } from "./core_utils.js"; class PostScriptParser { constructor(lexer) { @@ -193,7 +193,7 @@ class PostScriptLexer { } } else if (ch === /* '%' = */ 0x25) { comment = true; - } else if (!isSpace(ch)) { + } else if (!isWhiteSpace(ch)) { break; } ch = this.nextChar(); diff --git a/src/core/stream.js b/src/core/stream.js index b0b213011..072c78cdc 100644 --- a/src/core/stream.js +++ b/src/core/stream.js @@ -21,7 +21,7 @@ import { FormatError, stringToBytes, unreachable } from "../shared/util.js"; import { isDict } from "./primitives.js"; -import { isSpace } from "./core_utils.js"; +import { isWhiteSpace } from "./core_utils.js"; var Stream = (function StreamClosure() { function Stream(arrayBuffer, start, length, dict) { @@ -1001,7 +1001,7 @@ var Ascii85Stream = (function Ascii85StreamClosure() { var str = this.str; var c = str.getByte(); - while (isSpace(c)) { + while (isWhiteSpace(c)) { c = str.getByte(); } @@ -1026,7 +1026,7 @@ var Ascii85Stream = (function Ascii85StreamClosure() { input[0] = c; for (i = 1; i < 5; ++i) { c = str.getByte(); - while (isSpace(c)) { + while (isWhiteSpace(c)) { c = str.getByte(); } diff --git a/src/core/type1_parser.js b/src/core/type1_parser.js index d87c7986d..a8d4dd920 100644 --- a/src/core/type1_parser.js +++ b/src/core/type1_parser.js @@ -14,7 +14,7 @@ */ import { getEncoding } from "./encodings.js"; -import { isSpace } from "./core_utils.js"; +import { isWhiteSpace } from "./core_utils.js"; import { Stream } from "./stream.js"; import { warn } from "../shared/util.js"; @@ -524,7 +524,7 @@ var Type1Parser = (function Type1ParserClosure() { } } else if (ch === /* '%' = */ 0x25) { comment = true; - } else if (!isSpace(ch)) { + } else if (!isWhiteSpace(ch)) { break; } ch = this.nextChar(); @@ -537,7 +537,7 @@ var Type1Parser = (function Type1ParserClosure() { do { token += String.fromCharCode(ch); ch = this.nextChar(); - } while (ch >= 0 && !isSpace(ch) && !isSpecial(ch)); + } while (ch >= 0 && !isWhiteSpace(ch) && !isSpecial(ch)); return token; }, diff --git a/test/unit/core_utils_spec.js b/test/unit/core_utils_spec.js index 9e0590847..3d13f12e8 100644 --- a/test/unit/core_utils_spec.js +++ b/test/unit/core_utils_spec.js @@ -16,7 +16,7 @@ import { Dict, Ref } from "../../src/core/primitives.js"; import { getInheritableProperty, - isSpace, + isWhiteSpace, log2, toRomanNumerals, } from "../../src/core/core_utils.js"; @@ -197,18 +197,18 @@ describe("core_utils", function() { }); }); - describe("isSpace", function() { + describe("isWhiteSpace", function() { it("handles space characters", function() { - expect(isSpace(0x20)).toEqual(true); - expect(isSpace(0x09)).toEqual(true); - expect(isSpace(0x0d)).toEqual(true); - expect(isSpace(0x0a)).toEqual(true); + expect(isWhiteSpace(0x20)).toEqual(true); + expect(isWhiteSpace(0x09)).toEqual(true); + expect(isWhiteSpace(0x0d)).toEqual(true); + expect(isWhiteSpace(0x0a)).toEqual(true); }); it("handles non-space characters", function() { - expect(isSpace(0x0b)).toEqual(false); - expect(isSpace(null)).toEqual(false); - expect(isSpace(undefined)).toEqual(false); + expect(isWhiteSpace(0x0b)).toEqual(false); + expect(isWhiteSpace(null)).toEqual(false); + expect(isWhiteSpace(undefined)).toEqual(false); }); }); });