image_utils.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.GlobalImageCache = exports.LocalGStateCache = exports.LocalFunctionCache = exports.LocalColorSpaceCache = exports.LocalImageCache = 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 (!name) {
  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 (!name && !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) {
  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 (!name) {
  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 GlobalImageCache {
  137. static get NUM_PAGES_THRESHOLD() {
  138. return (0, _util.shadow)(this, "NUM_PAGES_THRESHOLD", 2);
  139. }
  140. static get MAX_IMAGES_TO_CACHE() {
  141. return (0, _util.shadow)(this, "MAX_IMAGES_TO_CACHE", 10);
  142. }
  143. constructor() {
  144. this._refCache = new _primitives.RefSetCache();
  145. this._imageCache = new _primitives.RefSetCache();
  146. }
  147. shouldCache(ref, pageIndex) {
  148. const pageIndexSet = this._refCache.get(ref);
  149. const numPages = pageIndexSet ? pageIndexSet.size + (pageIndexSet.has(pageIndex) ? 0 : 1) : 1;
  150. if (numPages < GlobalImageCache.NUM_PAGES_THRESHOLD) {
  151. return false;
  152. }
  153. if (!this._imageCache.has(ref) && this._imageCache.size >= GlobalImageCache.MAX_IMAGES_TO_CACHE) {
  154. return false;
  155. }
  156. return true;
  157. }
  158. addPageIndex(ref, pageIndex) {
  159. let pageIndexSet = this._refCache.get(ref);
  160. if (!pageIndexSet) {
  161. pageIndexSet = new Set();
  162. this._refCache.put(ref, pageIndexSet);
  163. }
  164. pageIndexSet.add(pageIndex);
  165. }
  166. getData(ref, pageIndex) {
  167. const pageIndexSet = this._refCache.get(ref);
  168. if (!pageIndexSet) {
  169. return null;
  170. }
  171. if (pageIndexSet.size < GlobalImageCache.NUM_PAGES_THRESHOLD) {
  172. return null;
  173. }
  174. if (!this._imageCache.has(ref)) {
  175. return null;
  176. }
  177. pageIndexSet.add(pageIndex);
  178. return this._imageCache.get(ref);
  179. }
  180. setData(ref, data) {
  181. if (!this._refCache.has(ref)) {
  182. throw new Error('GlobalImageCache.setData - expected "addPageIndex" to have been called.');
  183. }
  184. if (this._imageCache.has(ref)) {
  185. return;
  186. }
  187. if (this._imageCache.size >= GlobalImageCache.MAX_IMAGES_TO_CACHE) {
  188. (0, _util.info)("GlobalImageCache.setData - ignoring image above MAX_IMAGES_TO_CACHE.");
  189. return;
  190. }
  191. this._imageCache.put(ref, data);
  192. }
  193. clear(onlyData = false) {
  194. if (!onlyData) {
  195. this._refCache.clear();
  196. }
  197. this._imageCache.clear();
  198. }
  199. }
  200. exports.GlobalImageCache = GlobalImageCache;