name_number_tree.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.NumberTree = exports.NameTree = void 0;
  27. var _primitives = require("./primitives.js");
  28. var _util = require("../shared/util.js");
  29. class NameOrNumberTree {
  30. constructor(root, xref, type) {
  31. if (this.constructor === NameOrNumberTree) {
  32. (0, _util.unreachable)("Cannot initialize NameOrNumberTree.");
  33. }
  34. this.root = root;
  35. this.xref = xref;
  36. this._type = type;
  37. }
  38. getAll() {
  39. const map = new Map();
  40. if (!this.root) {
  41. return map;
  42. }
  43. const xref = this.xref;
  44. const processed = new _primitives.RefSet();
  45. processed.put(this.root);
  46. const queue = [this.root];
  47. while (queue.length > 0) {
  48. const obj = xref.fetchIfRef(queue.shift());
  49. if (!(obj instanceof _primitives.Dict)) {
  50. continue;
  51. }
  52. if (obj.has("Kids")) {
  53. const kids = obj.get("Kids");
  54. if (!Array.isArray(kids)) {
  55. continue;
  56. }
  57. for (const kid of kids) {
  58. if (processed.has(kid)) {
  59. throw new _util.FormatError(`Duplicate entry in "${this._type}" tree.`);
  60. }
  61. queue.push(kid);
  62. processed.put(kid);
  63. }
  64. continue;
  65. }
  66. const entries = obj.get(this._type);
  67. if (!Array.isArray(entries)) {
  68. continue;
  69. }
  70. for (let i = 0, ii = entries.length; i < ii; i += 2) {
  71. map.set(xref.fetchIfRef(entries[i]), xref.fetchIfRef(entries[i + 1]));
  72. }
  73. }
  74. return map;
  75. }
  76. get(key) {
  77. if (!this.root) {
  78. return null;
  79. }
  80. const xref = this.xref;
  81. let kidsOrEntries = xref.fetchIfRef(this.root);
  82. let loopCount = 0;
  83. const MAX_LEVELS = 10;
  84. while (kidsOrEntries.has("Kids")) {
  85. if (++loopCount > MAX_LEVELS) {
  86. (0, _util.warn)(`Search depth limit reached for "${this._type}" tree.`);
  87. return null;
  88. }
  89. const kids = kidsOrEntries.get("Kids");
  90. if (!Array.isArray(kids)) {
  91. return null;
  92. }
  93. let l = 0,
  94. r = kids.length - 1;
  95. while (l <= r) {
  96. const m = l + r >> 1;
  97. const kid = xref.fetchIfRef(kids[m]);
  98. const limits = kid.get("Limits");
  99. if (key < xref.fetchIfRef(limits[0])) {
  100. r = m - 1;
  101. } else if (key > xref.fetchIfRef(limits[1])) {
  102. l = m + 1;
  103. } else {
  104. kidsOrEntries = kid;
  105. break;
  106. }
  107. }
  108. if (l > r) {
  109. return null;
  110. }
  111. }
  112. const entries = kidsOrEntries.get(this._type);
  113. if (Array.isArray(entries)) {
  114. let l = 0,
  115. r = entries.length - 2;
  116. while (l <= r) {
  117. const tmp = l + r >> 1,
  118. m = tmp + (tmp & 1);
  119. const currentKey = xref.fetchIfRef(entries[m]);
  120. if (key < currentKey) {
  121. r = m - 2;
  122. } else if (key > currentKey) {
  123. l = m + 2;
  124. } else {
  125. return xref.fetchIfRef(entries[m + 1]);
  126. }
  127. }
  128. }
  129. return null;
  130. }
  131. }
  132. class NameTree extends NameOrNumberTree {
  133. constructor(root, xref) {
  134. super(root, xref, "Names");
  135. }
  136. }
  137. exports.NameTree = NameTree;
  138. class NumberTree extends NameOrNumberTree {
  139. constructor(root, xref) {
  140. super(root, xref, "Nums");
  141. }
  142. }
  143. exports.NumberTree = NumberTree;