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.
This commit is contained in:
Jonas Jenwald 2020-06-17 18:29:50 +02:00
parent e22bc483a5
commit 51e87b9248
2 changed files with 40 additions and 5 deletions

View File

@ -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;

View File

@ -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 };