UNPKG

7.09 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.LocalTilingPatternCache = exports.LocalImageCache = exports.LocalGStateCache = exports.LocalFunctionCache = exports.LocalColorSpaceCache = exports.GlobalImageCache = void 0;
28
29var _util = require("../shared/util.js");
30
31var _primitives = require("./primitives.js");
32
33class BaseLocalCache {
34 constructor(options) {
35 if (this.constructor === BaseLocalCache) {
36 (0, _util.unreachable)("Cannot initialize BaseLocalCache.");
37 }
38
39 this._onlyRefs = (options && options.onlyRefs) === true;
40
41 if (!this._onlyRefs) {
42 this._nameRefMap = new Map();
43 this._imageMap = new Map();
44 }
45
46 this._imageCache = new _primitives.RefSetCache();
47 }
48
49 getByName(name) {
50 if (this._onlyRefs) {
51 (0, _util.unreachable)("Should not call `getByName` method.");
52 }
53
54 const ref = this._nameRefMap.get(name);
55
56 if (ref) {
57 return this.getByRef(ref);
58 }
59
60 return this._imageMap.get(name) || null;
61 }
62
63 getByRef(ref) {
64 return this._imageCache.get(ref) || null;
65 }
66
67 set(name, ref, data) {
68 (0, _util.unreachable)("Abstract method `set` called.");
69 }
70
71}
72
73class LocalImageCache extends BaseLocalCache {
74 set(name, ref = null, data) {
75 if (typeof name !== "string") {
76 throw new Error('LocalImageCache.set - expected "name" argument.');
77 }
78
79 if (ref) {
80 if (this._imageCache.has(ref)) {
81 return;
82 }
83
84 this._nameRefMap.set(name, ref);
85
86 this._imageCache.put(ref, data);
87
88 return;
89 }
90
91 if (this._imageMap.has(name)) {
92 return;
93 }
94
95 this._imageMap.set(name, data);
96 }
97
98}
99
100exports.LocalImageCache = LocalImageCache;
101
102class LocalColorSpaceCache extends BaseLocalCache {
103 set(name = null, ref = null, data) {
104 if (typeof name !== "string" && !ref) {
105 throw new Error('LocalColorSpaceCache.set - expected "name" and/or "ref" argument.');
106 }
107
108 if (ref) {
109 if (this._imageCache.has(ref)) {
110 return;
111 }
112
113 if (name !== null) {
114 this._nameRefMap.set(name, ref);
115 }
116
117 this._imageCache.put(ref, data);
118
119 return;
120 }
121
122 if (this._imageMap.has(name)) {
123 return;
124 }
125
126 this._imageMap.set(name, data);
127 }
128
129}
130
131exports.LocalColorSpaceCache = LocalColorSpaceCache;
132
133class LocalFunctionCache extends BaseLocalCache {
134 constructor(options) {
135 super({
136 onlyRefs: true
137 });
138 }
139
140 set(name = null, ref, data) {
141 if (!ref) {
142 throw new Error('LocalFunctionCache.set - expected "ref" argument.');
143 }
144
145 if (this._imageCache.has(ref)) {
146 return;
147 }
148
149 this._imageCache.put(ref, data);
150 }
151
152}
153
154exports.LocalFunctionCache = LocalFunctionCache;
155
156class LocalGStateCache extends BaseLocalCache {
157 set(name, ref = null, data) {
158 if (typeof name !== "string") {
159 throw new Error('LocalGStateCache.set - expected "name" argument.');
160 }
161
162 if (ref) {
163 if (this._imageCache.has(ref)) {
164 return;
165 }
166
167 this._nameRefMap.set(name, ref);
168
169 this._imageCache.put(ref, data);
170
171 return;
172 }
173
174 if (this._imageMap.has(name)) {
175 return;
176 }
177
178 this._imageMap.set(name, data);
179 }
180
181}
182
183exports.LocalGStateCache = LocalGStateCache;
184
185class LocalTilingPatternCache extends BaseLocalCache {
186 constructor(options) {
187 super({
188 onlyRefs: true
189 });
190 }
191
192 set(name = null, ref, data) {
193 if (!ref) {
194 throw new Error('LocalTilingPatternCache.set - expected "ref" argument.');
195 }
196
197 if (this._imageCache.has(ref)) {
198 return;
199 }
200
201 this._imageCache.put(ref, data);
202 }
203
204}
205
206exports.LocalTilingPatternCache = LocalTilingPatternCache;
207
208class GlobalImageCache {
209 static get NUM_PAGES_THRESHOLD() {
210 return (0, _util.shadow)(this, "NUM_PAGES_THRESHOLD", 2);
211 }
212
213 static get MIN_IMAGES_TO_CACHE() {
214 return (0, _util.shadow)(this, "MIN_IMAGES_TO_CACHE", 10);
215 }
216
217 static get MAX_BYTE_SIZE() {
218 return (0, _util.shadow)(this, "MAX_BYTE_SIZE", 40e6);
219 }
220
221 constructor() {
222 this._refCache = new _primitives.RefSetCache();
223 this._imageCache = new _primitives.RefSetCache();
224 }
225
226 get _byteSize() {
227 let byteSize = 0;
228
229 for (const imageData of this._imageCache) {
230 byteSize += imageData.byteSize;
231 }
232
233 return byteSize;
234 }
235
236 get _cacheLimitReached() {
237 if (this._imageCache.size < GlobalImageCache.MIN_IMAGES_TO_CACHE) {
238 return false;
239 }
240
241 if (this._byteSize < GlobalImageCache.MAX_BYTE_SIZE) {
242 return false;
243 }
244
245 return true;
246 }
247
248 shouldCache(ref, pageIndex) {
249 const pageIndexSet = this._refCache.get(ref);
250
251 const numPages = pageIndexSet ? pageIndexSet.size + (pageIndexSet.has(pageIndex) ? 0 : 1) : 1;
252
253 if (numPages < GlobalImageCache.NUM_PAGES_THRESHOLD) {
254 return false;
255 }
256
257 if (!this._imageCache.has(ref) && this._cacheLimitReached) {
258 return false;
259 }
260
261 return true;
262 }
263
264 addPageIndex(ref, pageIndex) {
265 let pageIndexSet = this._refCache.get(ref);
266
267 if (!pageIndexSet) {
268 pageIndexSet = new Set();
269
270 this._refCache.put(ref, pageIndexSet);
271 }
272
273 pageIndexSet.add(pageIndex);
274 }
275
276 addByteSize(ref, byteSize) {
277 const imageData = this._imageCache.get(ref);
278
279 if (!imageData) {
280 return;
281 }
282
283 if (imageData.byteSize) {
284 return;
285 }
286
287 imageData.byteSize = byteSize;
288 }
289
290 getData(ref, pageIndex) {
291 const pageIndexSet = this._refCache.get(ref);
292
293 if (!pageIndexSet) {
294 return null;
295 }
296
297 if (pageIndexSet.size < GlobalImageCache.NUM_PAGES_THRESHOLD) {
298 return null;
299 }
300
301 const imageData = this._imageCache.get(ref);
302
303 if (!imageData) {
304 return null;
305 }
306
307 pageIndexSet.add(pageIndex);
308 return imageData;
309 }
310
311 setData(ref, data) {
312 if (!this._refCache.has(ref)) {
313 throw new Error('GlobalImageCache.setData - expected "addPageIndex" to have been called.');
314 }
315
316 if (this._imageCache.has(ref)) {
317 return;
318 }
319
320 if (this._cacheLimitReached) {
321 (0, _util.warn)("GlobalImageCache.setData - cache limit reached.");
322 return;
323 }
324
325 this._imageCache.put(ref, data);
326 }
327
328 clear(onlyData = false) {
329 if (!onlyData) {
330 this._refCache.clear();
331 }
332
333 this._imageCache.clear();
334 }
335
336}
337
338exports.GlobalImageCache = GlobalImageCache;
\No newline at end of file