core_utils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 getLookupTableFactory(initializer) {
  32. var lookup;
  33. return function () {
  34. if (initializer) {
  35. lookup = Object.create(null);
  36. initializer(lookup);
  37. initializer = null;
  38. }
  39. return lookup;
  40. };
  41. }
  42. var MissingDataException = function MissingDataExceptionClosure() {
  43. function MissingDataException(begin, end) {
  44. this.begin = begin;
  45. this.end = end;
  46. this.message = "Missing data [".concat(begin, ", ").concat(end, ")");
  47. }
  48. MissingDataException.prototype = new Error();
  49. MissingDataException.prototype.name = 'MissingDataException';
  50. MissingDataException.constructor = MissingDataException;
  51. return MissingDataException;
  52. }();
  53. exports.MissingDataException = MissingDataException;
  54. var XRefEntryException = function XRefEntryExceptionClosure() {
  55. function XRefEntryException(msg) {
  56. this.message = msg;
  57. }
  58. XRefEntryException.prototype = new Error();
  59. XRefEntryException.prototype.name = 'XRefEntryException';
  60. XRefEntryException.constructor = XRefEntryException;
  61. return XRefEntryException;
  62. }();
  63. exports.XRefEntryException = XRefEntryException;
  64. var XRefParseException = function XRefParseExceptionClosure() {
  65. function XRefParseException(msg) {
  66. this.message = msg;
  67. }
  68. XRefParseException.prototype = new Error();
  69. XRefParseException.prototype.name = 'XRefParseException';
  70. XRefParseException.constructor = XRefParseException;
  71. return XRefParseException;
  72. }();
  73. exports.XRefParseException = XRefParseException;
  74. function getInheritableProperty(_ref) {
  75. var dict = _ref.dict,
  76. key = _ref.key,
  77. _ref$getArray = _ref.getArray,
  78. getArray = _ref$getArray === void 0 ? false : _ref$getArray,
  79. _ref$stopWhenFound = _ref.stopWhenFound,
  80. stopWhenFound = _ref$stopWhenFound === void 0 ? true : _ref$stopWhenFound;
  81. var LOOP_LIMIT = 100;
  82. var loopCount = 0;
  83. var values;
  84. while (dict) {
  85. var value = getArray ? dict.getArray(key) : dict.get(key);
  86. if (value !== undefined) {
  87. if (stopWhenFound) {
  88. return value;
  89. }
  90. if (!values) {
  91. values = [];
  92. }
  93. values.push(value);
  94. }
  95. if (++loopCount > LOOP_LIMIT) {
  96. (0, _util.warn)("getInheritableProperty: maximum loop count exceeded for \"".concat(key, "\""));
  97. break;
  98. }
  99. dict = dict.get('Parent');
  100. }
  101. return values;
  102. }
  103. 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'];
  104. function toRomanNumerals(number) {
  105. var lowerCase = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  106. (0, _util.assert)(Number.isInteger(number) && number > 0, 'The number should be a positive integer.');
  107. var pos,
  108. romanBuf = [];
  109. while (number >= 1000) {
  110. number -= 1000;
  111. romanBuf.push('M');
  112. }
  113. pos = number / 100 | 0;
  114. number %= 100;
  115. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  116. pos = number / 10 | 0;
  117. number %= 10;
  118. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  119. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  120. var romanStr = romanBuf.join('');
  121. return lowerCase ? romanStr.toLowerCase() : romanStr;
  122. }