name_number_tree.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. for (let i = 0, ii = kids.length; i < ii; i++) {
  55. const kid = kids[i];
  56. if (processed.has(kid)) {
  57. throw new _util.FormatError(`Duplicate entry in "${this._type}" tree.`);
  58. }
  59. queue.push(kid);
  60. processed.put(kid);
  61. }
  62. continue;
  63. }
  64. const entries = obj.get(this._type);
  65. if (!Array.isArray(entries)) {
  66. continue;
  67. }
  68. for (let i = 0, ii = entries.length; i < ii; i += 2) {
  69. map.set(xref.fetchIfRef(entries[i]), xref.fetchIfRef(entries[i + 1]));
  70. }
  71. }
  72. return map;
  73. }
  74. get(key) {
  75. if (!this.root) {
  76. return null;
  77. }
  78. const xref = this.xref;
  79. let kidsOrEntries = xref.fetchIfRef(this.root);
  80. let loopCount = 0;
  81. const MAX_LEVELS = 10;
  82. while (kidsOrEntries.has("Kids")) {
  83. if (++loopCount > MAX_LEVELS) {
  84. (0, _util.warn)(`Search depth limit reached for "${this._type}" tree.`);
  85. return null;
  86. }
  87. const kids = kidsOrEntries.get("Kids");
  88. if (!Array.isArray(kids)) {
  89. return null;
  90. }
  91. let l = 0,
  92. r = kids.length - 1;
  93. while (l <= r) {
  94. const m = l + r >> 1;
  95. const kid = xref.fetchIfRef(kids[m]);
  96. const limits = kid.get("Limits");
  97. if (key < xref.fetchIfRef(limits[0])) {
  98. r = m - 1;
  99. } else if (key > xref.fetchIfRef(limits[1])) {
  100. l = m + 1;
  101. } else {
  102. kidsOrEntries = xref.fetchIfRef(kids[m]);
  103. break;
  104. }
  105. }
  106. if (l > r) {
  107. return null;
  108. }
  109. }
  110. const entries = kidsOrEntries.get(this._type);
  111. if (Array.isArray(entries)) {
  112. let l = 0,
  113. r = entries.length - 2;
  114. while (l <= r) {
  115. const tmp = l + r >> 1,
  116. m = tmp + (tmp & 1);
  117. const currentKey = xref.fetchIfRef(entries[m]);
  118. if (key < currentKey) {
  119. r = m - 2;
  120. } else if (key > currentKey) {
  121. l = m + 2;
  122. } else {
  123. return xref.fetchIfRef(entries[m + 1]);
  124. }
  125. }
  126. }
  127. return null;
  128. }
  129. }
  130. class NameTree extends NameOrNumberTree {
  131. constructor(root, xref) {
  132. super(root, xref, "Names");
  133. }
  134. }
  135. exports.NameTree = NameTree;
  136. class NumberTree extends NameOrNumberTree {
  137. constructor(root, xref) {
  138. super(root, xref, "Nums");
  139. }
  140. }
  141. exports.NumberTree = NumberTree;