From 51e87b924812b8ba406cbf59b558bfe525be2426 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 17 Jun 2020 18:29:50 +0200 Subject: [PATCH] Add a proper `LocalColorSpaceCache` class, rather than piggybacking on the image one (PR 12001 follow-up) This will allow caching of ColorSpaces by either `Name` *or* `Ref`, which doesn't really make sense for images, thus allowing (better) caching for ColorSpaces used with e.g. Images and Patterns. --- src/core/evaluator.js | 4 ++-- src/core/image_utils.js | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 8bf2f78bf..7baf16ee2 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -73,13 +73,13 @@ import { } from "./standard_fonts.js"; import { getTilingPatternIR, Pattern } from "./pattern.js"; import { Lexer, Parser } from "./parser.js"; +import { LocalColorSpaceCache, LocalImageCache } from "./image_utils.js"; import { bidi } from "./bidi.js"; import { ColorSpace } from "./colorspace.js"; import { DecodeStream } from "./stream.js"; import { getGlyphsUnicode } from "./glyphlist.js"; import { getMetrics } from "./metrics.js"; import { isPDFFunction } from "./function.js"; -import { LocalImageCache } from "./image_utils.js"; import { MurmurHash3_64 } from "./murmurhash3.js"; import { OperatorList } from "./operator_list.js"; import { PDFImage } from "./image.js"; @@ -1224,7 +1224,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { var xref = this.xref; let parsingText = false; const localImageCache = new LocalImageCache(); - const localColorSpaceCache = new LocalImageCache(); + const localColorSpaceCache = new LocalColorSpaceCache(); var xobjs = resources.get("XObject") || Dict.empty; var patterns = resources.get("Pattern") || Dict.empty; diff --git a/src/core/image_utils.js b/src/core/image_utils.js index 2fbb93eb0..d255cc098 100644 --- a/src/core/image_utils.js +++ b/src/core/image_utils.js @@ -14,11 +14,14 @@ */ /* eslint no-var: error */ -import { assert, info, shadow } from "../shared/util.js"; +import { assert, info, shadow, unreachable } from "../shared/util.js"; import { RefSetCache } from "./primitives.js"; -class LocalImageCache { +class BaseLocalCache { constructor() { + if (this.constructor === BaseLocalCache) { + unreachable("Cannot initialize BaseLocalCache."); + } this._nameRefMap = new Map(); this._imageMap = new Map(); this._imageCache = new RefSetCache(); @@ -36,6 +39,12 @@ class LocalImageCache { return this._imageCache.get(ref) || null; } + set(name, ref, data) { + unreachable("Abstract method `set` called."); + } +} + +class LocalImageCache extends BaseLocalCache { set(name, ref = null, data) { if (!name) { throw new Error('LocalImageCache.set - expected "name" argument.'); @@ -56,6 +65,32 @@ class LocalImageCache { } } +class LocalColorSpaceCache extends BaseLocalCache { + set(name = null, ref = null, data) { + if (!name && !ref) { + throw new Error( + 'LocalColorSpaceCache.set - expected "name" and/or "ref" argument.' + ); + } + if (ref) { + if (this._imageCache.has(ref)) { + return; + } + if (name) { + // Optional when `ref` is defined. + this._nameRefMap.set(name, ref); + } + this._imageCache.put(ref, data); + return; + } + // name + if (this._imageMap.has(name)) { + return; + } + this._imageMap.set(name, data); + } +} + class GlobalImageCache { static get NUM_PAGES_THRESHOLD() { return shadow(this, "NUM_PAGES_THRESHOLD", 2); @@ -149,4 +184,4 @@ class GlobalImageCache { } } -export { LocalImageCache, GlobalImageCache }; +export { LocalImageCache, LocalColorSpaceCache, GlobalImageCache };