image_utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.LocalImageCache = void 0;
  27. var _util = require("../shared/util.js");
  28. var _primitives = require("./primitives.js");
  29. class LocalImageCache {
  30. constructor() {
  31. this._nameRefMap = new Map();
  32. this._imageMap = new Map();
  33. this._imageCache = new _primitives.RefSetCache();
  34. }
  35. getByName(name) {
  36. const ref = this._nameRefMap.get(name);
  37. if (ref) {
  38. return this.getByRef(ref);
  39. }
  40. return this._imageMap.get(name) || null;
  41. }
  42. getByRef(ref) {
  43. return this._imageCache.get(ref) || null;
  44. }
  45. set(name, ref = null, data) {
  46. if (!name) {
  47. throw new Error('LocalImageCache.set - expected "name" argument.');
  48. }
  49. if (ref) {
  50. if (this._imageCache.has(ref)) {
  51. return;
  52. }
  53. this._nameRefMap.set(name, ref);
  54. this._imageCache.put(ref, data);
  55. return;
  56. }
  57. if (this._imageMap.has(name)) {
  58. return;
  59. }
  60. this._imageMap.set(name, data);
  61. }
  62. }
  63. exports.LocalImageCache = LocalImageCache;
  64. class GlobalImageCache {
  65. static get NUM_PAGES_THRESHOLD() {
  66. return (0, _util.shadow)(this, "NUM_PAGES_THRESHOLD", 2);
  67. }
  68. static get MAX_IMAGES_TO_CACHE() {
  69. return (0, _util.shadow)(this, "MAX_IMAGES_TO_CACHE", 10);
  70. }
  71. constructor() {
  72. this._refCache = new _primitives.RefSetCache();
  73. this._imageCache = new _primitives.RefSetCache();
  74. }
  75. shouldCache(ref, pageIndex) {
  76. const pageIndexSet = this._refCache.get(ref);
  77. const numPages = pageIndexSet ? pageIndexSet.size + (pageIndexSet.has(pageIndex) ? 0 : 1) : 1;
  78. if (numPages < GlobalImageCache.NUM_PAGES_THRESHOLD) {
  79. return false;
  80. }
  81. if (!this._imageCache.has(ref) && this._imageCache.size >= GlobalImageCache.MAX_IMAGES_TO_CACHE) {
  82. return false;
  83. }
  84. return true;
  85. }
  86. addPageIndex(ref, pageIndex) {
  87. let pageIndexSet = this._refCache.get(ref);
  88. if (!pageIndexSet) {
  89. pageIndexSet = new Set();
  90. this._refCache.put(ref, pageIndexSet);
  91. }
  92. pageIndexSet.add(pageIndex);
  93. }
  94. getData(ref, pageIndex) {
  95. if (!this._refCache.has(ref)) {
  96. return null;
  97. }
  98. const pageIndexSet = this._refCache.get(ref);
  99. if (pageIndexSet.size < GlobalImageCache.NUM_PAGES_THRESHOLD) {
  100. return null;
  101. }
  102. if (!this._imageCache.has(ref)) {
  103. return null;
  104. }
  105. pageIndexSet.add(pageIndex);
  106. return this._imageCache.get(ref);
  107. }
  108. setData(ref, data) {
  109. if (!this._refCache.has(ref)) {
  110. throw new Error('GlobalImageCache.setData - expected "addPageIndex" to have been called.');
  111. }
  112. if (this._imageCache.has(ref)) {
  113. return;
  114. }
  115. if (this._imageCache.size >= GlobalImageCache.MAX_IMAGES_TO_CACHE) {
  116. (0, _util.info)("GlobalImageCache.setData - ignoring image above MAX_IMAGES_TO_CACHE.");
  117. return;
  118. }
  119. this._imageCache.put(ref, data);
  120. }
  121. clear(onlyData = false) {
  122. if (!onlyData) {
  123. this._refCache.clear();
  124. }
  125. this._imageCache.clear();
  126. }
  127. }
  128. exports.GlobalImageCache = GlobalImageCache;