core_utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.getLookupTableFactory = getLookupTableFactory;
  27. exports.getInheritableProperty = getInheritableProperty;
  28. exports.toRomanNumerals = toRomanNumerals;
  29. exports.log2 = log2;
  30. exports.readInt8 = readInt8;
  31. exports.readUint16 = readUint16;
  32. exports.readUint32 = readUint32;
  33. exports.isWhiteSpace = isWhiteSpace;
  34. exports.XRefParseException = exports.XRefEntryException = exports.MissingDataException = void 0;
  35. var _util = require("../shared/util.js");
  36. function getLookupTableFactory(initializer) {
  37. let lookup;
  38. return function () {
  39. if (initializer) {
  40. lookup = Object.create(null);
  41. initializer(lookup);
  42. initializer = null;
  43. }
  44. return lookup;
  45. };
  46. }
  47. class MissingDataException extends _util.BaseException {
  48. constructor(begin, end) {
  49. super(`Missing data [${begin}, ${end})`);
  50. this.begin = begin;
  51. this.end = end;
  52. }
  53. }
  54. exports.MissingDataException = MissingDataException;
  55. class XRefEntryException extends _util.BaseException {}
  56. exports.XRefEntryException = XRefEntryException;
  57. class XRefParseException extends _util.BaseException {}
  58. exports.XRefParseException = XRefParseException;
  59. function getInheritableProperty({
  60. dict,
  61. key,
  62. getArray = false,
  63. stopWhenFound = true
  64. }) {
  65. const LOOP_LIMIT = 100;
  66. let loopCount = 0;
  67. let values;
  68. while (dict) {
  69. const value = getArray ? dict.getArray(key) : dict.get(key);
  70. if (value !== undefined) {
  71. if (stopWhenFound) {
  72. return value;
  73. }
  74. if (!values) {
  75. values = [];
  76. }
  77. values.push(value);
  78. }
  79. if (++loopCount > LOOP_LIMIT) {
  80. (0, _util.warn)(`getInheritableProperty: maximum loop count exceeded for "${key}"`);
  81. break;
  82. }
  83. dict = dict.get("Parent");
  84. }
  85. return values;
  86. }
  87. const ROMAN_NUMBER_MAP = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
  88. function toRomanNumerals(number, lowerCase = false) {
  89. (0, _util.assert)(Number.isInteger(number) && number > 0, "The number should be a positive integer.");
  90. const romanBuf = [];
  91. let pos;
  92. while (number >= 1000) {
  93. number -= 1000;
  94. romanBuf.push("M");
  95. }
  96. pos = number / 100 | 0;
  97. number %= 100;
  98. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  99. pos = number / 10 | 0;
  100. number %= 10;
  101. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  102. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  103. const romanStr = romanBuf.join("");
  104. return lowerCase ? romanStr.toLowerCase() : romanStr;
  105. }
  106. function log2(x) {
  107. if (x <= 0) {
  108. return 0;
  109. }
  110. return Math.ceil(Math.log2(x));
  111. }
  112. function readInt8(data, offset) {
  113. return data[offset] << 24 >> 24;
  114. }
  115. function readUint16(data, offset) {
  116. return data[offset] << 8 | data[offset + 1];
  117. }
  118. function readUint32(data, offset) {
  119. return (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
  120. }
  121. function isWhiteSpace(ch) {
  122. return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
  123. }