2019-02-24 00:14:31 +09:00
|
|
|
/* Copyright 2019 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
/* eslint no-var: error */
|
|
|
|
|
2020-01-02 20:00:16 +09:00
|
|
|
import { assert, BaseException, warn } from "../shared/util.js";
|
2019-02-24 00:14:31 +09:00
|
|
|
|
|
|
|
function getLookupTableFactory(initializer) {
|
|
|
|
let lookup;
|
2020-04-14 19:28:14 +09:00
|
|
|
return function () {
|
2019-02-24 00:14:31 +09:00
|
|
|
if (initializer) {
|
|
|
|
lookup = Object.create(null);
|
|
|
|
initializer(lookup);
|
|
|
|
initializer = null;
|
|
|
|
}
|
|
|
|
return lookup;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-29 08:18:48 +09:00
|
|
|
class MissingDataException extends BaseException {
|
|
|
|
constructor(begin, end) {
|
|
|
|
super(`Missing data [${begin}, ${end})`);
|
2019-02-24 00:14:31 +09:00
|
|
|
this.begin = begin;
|
|
|
|
this.end = end;
|
|
|
|
}
|
2019-09-29 08:18:48 +09:00
|
|
|
}
|
2019-02-24 00:14:31 +09:00
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
class XRefEntryException extends BaseException {}
|
2019-02-24 00:14:31 +09:00
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
class XRefParseException extends BaseException {}
|
2019-02-24 00:14:31 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value of an inheritable property.
|
|
|
|
*
|
|
|
|
* If the PDF specification explicitly lists a property in a dictionary as
|
|
|
|
* inheritable, then the value of the property may be present in the dictionary
|
|
|
|
* itself or in one or more parents of the dictionary.
|
|
|
|
*
|
|
|
|
* If the key is not found in the tree, `undefined` is returned. Otherwise,
|
|
|
|
* the value for the key is returned or, if `stopWhenFound` is `false`, a list
|
|
|
|
* of values is returned. To avoid infinite loops, the traversal is stopped when
|
|
|
|
* the loop limit is reached.
|
|
|
|
*
|
|
|
|
* @param {Dict} dict - Dictionary from where to start the traversal.
|
|
|
|
* @param {string} key - The key of the property to find the value for.
|
|
|
|
* @param {boolean} getArray - Whether or not the value should be fetched as an
|
|
|
|
* array. The default value is `false`.
|
|
|
|
* @param {boolean} stopWhenFound - Whether or not to stop the traversal when
|
|
|
|
* the key is found. If set to `false`, we always walk up the entire parent
|
|
|
|
* chain, for example to be able to find `\Resources` placed on multiple
|
|
|
|
* levels of the tree. The default value is `true`.
|
|
|
|
*/
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
function getInheritableProperty({
|
|
|
|
dict,
|
|
|
|
key,
|
|
|
|
getArray = false,
|
|
|
|
stopWhenFound = true,
|
|
|
|
}) {
|
2019-02-24 00:14:31 +09:00
|
|
|
const LOOP_LIMIT = 100;
|
|
|
|
let loopCount = 0;
|
|
|
|
let values;
|
|
|
|
|
|
|
|
while (dict) {
|
|
|
|
const value = getArray ? dict.getArray(key) : dict.get(key);
|
|
|
|
if (value !== undefined) {
|
|
|
|
if (stopWhenFound) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (!values) {
|
|
|
|
values = [];
|
|
|
|
}
|
|
|
|
values.push(value);
|
|
|
|
}
|
|
|
|
if (++loopCount > LOOP_LIMIT) {
|
|
|
|
warn(`getInheritableProperty: maximum loop count exceeded for "${key}"`);
|
|
|
|
break;
|
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
dict = dict.get("Parent");
|
2019-02-24 00:14:31 +09:00
|
|
|
}
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:54:34 +09:00
|
|
|
// prettier-ignore
|
2019-02-24 00:14:31 +09:00
|
|
|
const ROMAN_NUMBER_MAP = [
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
"", "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"
|
2019-02-24 00:14:31 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts positive integers to (upper case) Roman numerals.
|
2019-10-12 22:54:17 +09:00
|
|
|
* @param {number} number - The number that should be converted.
|
2019-02-24 00:14:31 +09:00
|
|
|
* @param {boolean} lowerCase - Indicates if the result should be converted
|
|
|
|
* to lower case letters. The default value is `false`.
|
2019-10-13 01:14:29 +09:00
|
|
|
* @returns {string} The resulting Roman number.
|
2019-02-24 00:14:31 +09:00
|
|
|
*/
|
|
|
|
function toRomanNumerals(number, lowerCase = false) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
assert(
|
|
|
|
Number.isInteger(number) && number > 0,
|
|
|
|
"The number should be a positive integer."
|
|
|
|
);
|
2020-01-24 21:21:16 +09:00
|
|
|
const romanBuf = [];
|
|
|
|
let pos;
|
2019-02-24 00:14:31 +09:00
|
|
|
// Thousands
|
|
|
|
while (number >= 1000) {
|
|
|
|
number -= 1000;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
romanBuf.push("M");
|
2019-02-24 00:14:31 +09:00
|
|
|
}
|
|
|
|
// Hundreds
|
|
|
|
pos = (number / 100) | 0;
|
|
|
|
number %= 100;
|
|
|
|
romanBuf.push(ROMAN_NUMBER_MAP[pos]);
|
|
|
|
// Tens
|
|
|
|
pos = (number / 10) | 0;
|
|
|
|
number %= 10;
|
|
|
|
romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
|
|
|
|
// Ones
|
|
|
|
romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
|
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
const romanStr = romanBuf.join("");
|
|
|
|
return lowerCase ? romanStr.toLowerCase() : romanStr;
|
2019-02-24 00:14:31 +09:00
|
|
|
}
|
|
|
|
|
2020-01-08 03:59:16 +09:00
|
|
|
// Calculate the base 2 logarithm of the number `x`. This differs from the
|
|
|
|
// native function in the sense that it returns the ceiling value and that it
|
|
|
|
// returns 0 instead of `Infinity`/`NaN` for `x` values smaller than/equal to 0.
|
|
|
|
function log2(x) {
|
|
|
|
if (x <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return Math.ceil(Math.log2(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
function readInt8(data, offset) {
|
|
|
|
return (data[offset] << 24) >> 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readUint16(data, offset) {
|
|
|
|
return (data[offset] << 8) | data[offset + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function readUint32(data, offset) {
|
|
|
|
return (
|
|
|
|
((data[offset] << 24) |
|
|
|
|
(data[offset + 1] << 16) |
|
|
|
|
(data[offset + 2] << 8) |
|
|
|
|
data[offset + 3]) >>>
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if ch is one of the following characters: SPACE, TAB, CR or LF.
|
2020-02-10 17:38:57 +09:00
|
|
|
function isWhiteSpace(ch) {
|
2020-01-08 03:59:16 +09:00
|
|
|
return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
|
|
|
|
}
|
|
|
|
|
2019-02-24 00:14:31 +09:00
|
|
|
export {
|
|
|
|
getLookupTableFactory,
|
|
|
|
MissingDataException,
|
|
|
|
XRefEntryException,
|
|
|
|
XRefParseException,
|
|
|
|
getInheritableProperty,
|
|
|
|
toRomanNumerals,
|
2020-01-08 03:59:16 +09:00
|
|
|
log2,
|
|
|
|
readInt8,
|
|
|
|
readUint16,
|
|
|
|
readUint32,
|
2020-02-10 17:38:57 +09:00
|
|
|
isWhiteSpace,
|
2019-02-24 00:14:31 +09:00
|
|
|
};
|