image_utils.js 7.0 KB

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