core_utils.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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.XRefParseException = exports.XRefEntryException = exports.MissingDataException = void 0;
  30. var _util = require("../shared/util");
  31. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  32. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  33. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  34. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  35. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  36. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  37. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  38. function getLookupTableFactory(initializer) {
  39. var lookup;
  40. return function () {
  41. if (initializer) {
  42. lookup = Object.create(null);
  43. initializer(lookup);
  44. initializer = null;
  45. }
  46. return lookup;
  47. };
  48. }
  49. var MissingDataException =
  50. /*#__PURE__*/
  51. function (_BaseException) {
  52. _inherits(MissingDataException, _BaseException);
  53. function MissingDataException(begin, end) {
  54. var _this;
  55. _classCallCheck(this, MissingDataException);
  56. _this = _possibleConstructorReturn(this, _getPrototypeOf(MissingDataException).call(this, "Missing data [".concat(begin, ", ").concat(end, ")")));
  57. _this.begin = begin;
  58. _this.end = end;
  59. return _this;
  60. }
  61. return MissingDataException;
  62. }(_util.BaseException);
  63. exports.MissingDataException = MissingDataException;
  64. var XRefEntryException =
  65. /*#__PURE__*/
  66. function (_BaseException2) {
  67. _inherits(XRefEntryException, _BaseException2);
  68. function XRefEntryException() {
  69. _classCallCheck(this, XRefEntryException);
  70. return _possibleConstructorReturn(this, _getPrototypeOf(XRefEntryException).apply(this, arguments));
  71. }
  72. return XRefEntryException;
  73. }(_util.BaseException);
  74. exports.XRefEntryException = XRefEntryException;
  75. var XRefParseException =
  76. /*#__PURE__*/
  77. function (_BaseException3) {
  78. _inherits(XRefParseException, _BaseException3);
  79. function XRefParseException() {
  80. _classCallCheck(this, XRefParseException);
  81. return _possibleConstructorReturn(this, _getPrototypeOf(XRefParseException).apply(this, arguments));
  82. }
  83. return XRefParseException;
  84. }(_util.BaseException);
  85. exports.XRefParseException = XRefParseException;
  86. function getInheritableProperty(_ref) {
  87. var dict = _ref.dict,
  88. key = _ref.key,
  89. _ref$getArray = _ref.getArray,
  90. getArray = _ref$getArray === void 0 ? false : _ref$getArray,
  91. _ref$stopWhenFound = _ref.stopWhenFound,
  92. stopWhenFound = _ref$stopWhenFound === void 0 ? true : _ref$stopWhenFound;
  93. var LOOP_LIMIT = 100;
  94. var loopCount = 0;
  95. var values;
  96. while (dict) {
  97. var value = getArray ? dict.getArray(key) : dict.get(key);
  98. if (value !== undefined) {
  99. if (stopWhenFound) {
  100. return value;
  101. }
  102. if (!values) {
  103. values = [];
  104. }
  105. values.push(value);
  106. }
  107. if (++loopCount > LOOP_LIMIT) {
  108. (0, _util.warn)("getInheritableProperty: maximum loop count exceeded for \"".concat(key, "\""));
  109. break;
  110. }
  111. dict = dict.get('Parent');
  112. }
  113. return values;
  114. }
  115. var 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'];
  116. function toRomanNumerals(number) {
  117. var lowerCase = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  118. (0, _util.assert)(Number.isInteger(number) && number > 0, 'The number should be a positive integer.');
  119. var pos,
  120. romanBuf = [];
  121. while (number >= 1000) {
  122. number -= 1000;
  123. romanBuf.push('M');
  124. }
  125. pos = number / 100 | 0;
  126. number %= 100;
  127. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  128. pos = number / 10 | 0;
  129. number %= 10;
  130. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  131. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  132. var romanStr = romanBuf.join('');
  133. return lowerCase ? romanStr.toLowerCase() : romanStr;
  134. }