diff --git a/src/core/catalog.js b/src/core/catalog.js index 044979a9f..920cc26ba 100644 --- a/src/core/catalog.js +++ b/src/core/catalog.js @@ -13,19 +13,6 @@ * limitations under the License. */ -import { - clearPrimitiveCaches, - Dict, - isDict, - isName, - isRef, - isRefsEqual, - isStream, - Name, - Ref, - RefSet, - RefSetCache, -} from "./primitives.js"; import { collectActions, MissingDataException, @@ -48,8 +35,21 @@ import { stringToUTF8String, warn, } from "../shared/util.js"; +import { + Dict, + isDict, + isName, + isRef, + isRefsEqual, + isStream, + Name, + Ref, + RefSet, + RefSetCache, +} from "./primitives.js"; import { NameTree, NumberTree } from "./name_number_tree.js"; import { BaseStream } from "./base_stream.js"; +import { clearGlobalCaches } from "./cleanup_helper.js"; import { ColorSpace } from "./colorspace.js"; import { FileSpec } from "./file_spec.js"; import { GlobalImageCache } from "./image_utils.js"; @@ -1069,7 +1069,7 @@ class Catalog { } cleanup(manuallyTriggered = false) { - clearPrimitiveCaches(); + clearGlobalCaches(); this.globalImageCache.clear(/* onlyData = */ manuallyTriggered); this.pageKidsCountCache.clear(); this.pageIndexCache.clear(); diff --git a/src/core/cleanup_helper.js b/src/core/cleanup_helper.js new file mode 100644 index 000000000..f014519bd --- /dev/null +++ b/src/core/cleanup_helper.js @@ -0,0 +1,24 @@ +/* Copyright 2022 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { clearPrimitiveCaches } from "./primitives.js"; +import { clearUnicodeCaches } from "./unicode.js"; + +function clearGlobalCaches() { + clearPrimitiveCaches(); + clearUnicodeCaches(); +} + +export { clearGlobalCaches }; diff --git a/src/core/document.js b/src/core/document.js index 8d113abf8..19d2e66b4 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -35,16 +35,6 @@ import { Util, warn, } from "../shared/util.js"; -import { - clearPrimitiveCaches, - Dict, - isDict, - isName, - isRef, - isStream, - Name, - Ref, -} from "./primitives.js"; import { collectActions, getInheritableProperty, @@ -54,12 +44,22 @@ import { XRefEntryException, XRefParseException, } from "./core_utils.js"; +import { + Dict, + isDict, + isName, + isRef, + isStream, + Name, + Ref, +} from "./primitives.js"; import { getXfaFontDict, getXfaFontName } from "./xfa_fonts.js"; import { NullStream, Stream } from "./stream.js"; import { AnnotationFactory } from "./annotation.js"; import { BaseStream } from "./base_stream.js"; import { calculateMD5 } from "./crypto.js"; import { Catalog } from "./catalog.js"; +import { clearGlobalCaches } from "./cleanup_helper.js"; import { Linearization } from "./parser.js"; import { ObjectLoader } from "./object_loader.js"; import { OperatorList } from "./operator_list.js"; @@ -1449,7 +1449,7 @@ class PDFDocument { async cleanup(manuallyTriggered = false) { return this.catalog ? this.catalog.cleanup(manuallyTriggered) - : clearPrimitiveCaches(); + : clearGlobalCaches(); } /** diff --git a/src/core/unicode.js b/src/core/unicode.js index 647272834..6116fab6d 100644 --- a/src/core/unicode.js +++ b/src/core/unicode.js @@ -1641,16 +1641,29 @@ function reverseIfRtl(chars) { } const SpecialCharRegExp = new RegExp("^(\\s)|(\\p{Mn})|(\\p{Cf})$", "u"); +const CategoryCache = new Map(); + function getCharUnicodeCategory(char) { + const cachedCategory = CategoryCache.get(char); + if (cachedCategory) { + return cachedCategory; + } const groups = char.match(SpecialCharRegExp); - return { + const category = { isWhitespace: !!(groups && groups[1]), isZeroWidthDiacritic: !!(groups && groups[2]), isInvisibleFormatMark: !!(groups && groups[3]), }; + CategoryCache.set(char, category); + return category; +} + +function clearUnicodeCaches() { + CategoryCache.clear(); } export { + clearUnicodeCaches, getCharUnicodeCategory, getNormalizedUnicodes, getUnicodeForGlyph, diff --git a/src/core/worker.js b/src/core/worker.js index 24694c0a8..328ffb7f2 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -32,8 +32,9 @@ import { VerbosityLevel, warn, } from "../shared/util.js"; -import { clearPrimitiveCaches, Dict, Ref } from "./primitives.js"; +import { Dict, Ref } from "./primitives.js"; import { LocalPdfManager, NetworkPdfManager } from "./pdf_manager.js"; +import { clearGlobalCaches } from "./cleanup_helper.js"; import { incrementalUpdate } from "./writer.js"; import { isNodeJS } from "../shared/is_node.js"; import { MessageHandler } from "../shared/message_handler.js"; @@ -795,7 +796,7 @@ class WorkerMessageHandler { pdfManager = null; } else { - clearPrimitiveCaches(); + clearGlobalCaches(); } if (cancelXHRs) { cancelXHRs(new AbortException("Worker was terminated."));