image_utils.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.LocalTilingPatternCache = exports.LocalImageCache = exports.LocalGStateCache = exports.LocalFunctionCache = exports.LocalColorSpaceCache = exports.GlobalImageCache = void 0;
  27. var _util = require("../shared/util.js");
  28. var _primitives = require("./primitives.js");
  29. class BaseLocalCache {
  30. constructor(options) {
  31. if (this.constructor === BaseLocalCache) {
  32. (0, _util.unreachable)("Cannot initialize BaseLocalCache.");
  33. }
  34. this._onlyRefs = (options && options.onlyRefs) === true;
  35. if (!this._onlyRefs) {
  36. this._nameRefMap = new Map();
  37. this._imageMap = new Map();
  38. }
  39. this._imageCache = new _primitives.RefSetCache();
  40. }
  41. getByName(name) {
  42. if (this._onlyRefs) {
  43. (0, _util.unreachable)("Should not call `getByName` method.");
  44. }
  45. const ref = this._nameRefMap.get(name);
  46. if (ref) {
  47. return this.getByRef(ref);
  48. }
  49. return this._imageMap.get(name) || null;
  50. }
  51. getByRef(ref) {
  52. return this._imageCache.get(ref) || null;
  53. }
  54. set(name, ref, data) {
  55. (0, _util.unreachable)("Abstract method `set` called.");
  56. }
  57. }
  58. class LocalImageCache extends BaseLocalCache {
  59. set(name, ref = null, data) {
  60. if (typeof name !== "string") {
  61. throw new Error('LocalImageCache.set - expected "name" argument.');
  62. }
  63. if (ref) {
  64. if (this._imageCache.has(ref)) {
  65. return;
  66. }
  67. this._nameRefMap.set(name, ref);
  68. this._imageCache.put(ref, data);
  69. return;
  70. }
  71. if (this._imageMap.has(name)) {
  72. return;
  73. }
  74. this._imageMap.set(name, data);
  75. }
  76. }
  77. exports.LocalImageCache = LocalImageCache;
  78. class LocalColorSpaceCache extends BaseLocalCache {
  79. set(name = null, ref = null, data) {
  80. if (typeof name !== "string" && !ref) {
  81. throw new Error('LocalColorSpaceCache.set - expected "name" and/or "ref" argument.');
  82. }
  83. if (ref) {
  84. if (this._imageCache.has(ref)) {
  85. return;
  86. }
  87. if (name !== null) {
  88. this._nameRefMap.set(name, ref);
  89. }
  90. this._imageCache.put(ref, data);
  91. return;
  92. }
  93. if (this._imageMap.has(name)) {
  94. return;
  95. }
  96. this._imageMap.set(name, data);
  97. }
  98. }
  99. exports.LocalColorSpaceCache = LocalColorSpaceCache;
  100. class LocalFunctionCache extends BaseLocalCache {
  101. constructor(options) {
  102. super({
  103. onlyRefs: true
  104. });
  105. }
  106. set(name = null, ref, data) {
  107. if (!ref) {
  108. throw new Error('LocalFunctionCache.set - expected "ref" argument.');
  109. }
  110. if (this._imageCache.has(ref)) {
  111. return;
  112. }
  113. this._imageCache.put(ref, data);
  114. }
  115. }
  116. exports.LocalFunctionCache = LocalFunctionCache;
  117. class LocalGStateCache extends BaseLocalCache {
  118. set(name, ref = null, data) {
  119. if (typeof name !== "string") {
  120. throw new Error('LocalGStateCache.set - expected "name" argument.');
  121. }
  122. if (ref) {
  123. if (this._imageCache.has(ref)) {
  124. return;
  125. }
  126. this._nameRefMap.set(name, ref);
  127. this._imageCache.put(ref, data);
  128. return;
  129. }
  130. if (this._imageMap.has(name)) {
  131. return;
  132. }
  133. this._imageMap.set(name, data);
  134. }
  135. }
  136. exports.LocalGStateCache = LocalGStateCache;
  137. class LocalTilingPatternCache extends BaseLocalCache {
  138. constructor(options) {
  139. super({
  140. onlyRefs: true
  141. });
  142. }
  143. set(name = null, ref, data) {
  144. if (!ref) {
  145. throw new Error('LocalTilingPatternCache.set - expected "ref" argument.');
  146. }
  147. if (this._imageCache.has(ref)) {
  148. return;
  149. }
  150. this._imageCache.put(ref, data);
  151. }
  152. }
  153. exports.LocalTilingPatternCache = LocalTilingPatternCache;
  154. class GlobalImageCache {
  155. static get NUM_PAGES_THRESHOLD() {
  156. return (0, _util.shadow)(this, "NUM_PAGES_THRESHOLD", 2);
  157. }
  158. static get MIN_IMAGES_TO_CACHE() {
  159. return (0, _util.shadow)(this, "MIN_IMAGES_TO_CACHE", 10);
  160. }
  161. static get MAX_BYTE_SIZE() {
  162. return (0, _util.shadow)(this, "MAX_BYTE_SIZE", 40e6);
  163. }
  164. constructor() {
  165. this._refCache = new _primitives.RefSetCache();
  166. this._imageCache = new _primitives.RefSetCache();
  167. }
  168. get _byteSize() {
  169. let byteSize = 0;
  170. for (const imageData of this._imageCache) {
  171. byteSize += imageData.byteSize;
  172. }
  173. return byteSize;
  174. }
  175. get _cacheLimitReached() {
  176. if (this._imageCache.size < GlobalImageCache.MIN_IMAGES_TO_CACHE) {
  177. return false;
  178. }
  179. if (this._byteSize < GlobalImageCache.MAX_BYTE_SIZE) {
  180. return false;
  181. }
  182. return true;
  183. }
  184. shouldCache(ref, pageIndex) {
  185. const pageIndexSet = this._refCache.get(ref);
  186. const numPages = pageIndexSet ? pageIndexSet.size + (pageIndexSet.has(pageIndex) ? 0 : 1) : 1;
  187. if (numPages < GlobalImageCache.NUM_PAGES_THRESHOLD) {
  188. return false;
  189. }
  190. if (!this._imageCache.has(ref) && this._cacheLimitReached) {
  191. return false;
  192. }
  193. return true;
  194. }
  195. addPageIndex(ref, pageIndex) {
  196. let pageIndexSet = this._refCache.get(ref);
  197. if (!pageIndexSet) {
  198. pageIndexSet = new Set();
  199. this._refCache.put(ref, pageIndexSet);
  200. }
  201. pageIndexSet.add(pageIndex);
  202. }
  203. addByteSize(ref, byteSize) {
  204. const imageData = this._imageCache.get(ref);
  205. if (!imageData) {
  206. return;
  207. }
  208. if (imageData.byteSize) {
  209. return;
  210. }
  211. imageData.byteSize = byteSize;
  212. }
  213. getData(ref, pageIndex) {
  214. const pageIndexSet = this._refCache.get(ref);
  215. if (!pageIndexSet) {
  216. return null;
  217. }
  218. if (pageIndexSet.size < GlobalImageCache.NUM_PAGES_THRESHOLD) {
  219. return null;
  220. }
  221. const imageData = this._imageCache.get(ref);
  222. if (!imageData) {
  223. return null;
  224. }
  225. pageIndexSet.add(pageIndex);
  226. return imageData;
  227. }
  228. setData(ref, data) {
  229. if (!this._refCache.has(ref)) {
  230. throw new Error('GlobalImageCache.setData - expected "addPageIndex" to have been called.');
  231. }
  232. if (this._imageCache.has(ref)) {
  233. return;
  234. }
  235. if (this._cacheLimitReached) {
  236. (0, _util.warn)("GlobalImageCache.setData - cache limit reached.");
  237. return;
  238. }
  239. this._imageCache.put(ref, data);
  240. }
  241. clear(onlyData = false) {
  242. if (!onlyData) {
  243. this._refCache.clear();
  244. }
  245. this._imageCache.clear();
  246. }
  247. }
  248. exports.GlobalImageCache = GlobalImageCache;