mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-25 17:48:07 +02:00
Move worker-thread only functions from src/shared/util.js
and into a new src/core/core_utils.js
file
The `src/shared/util.js` file is being bundled into both the `pdf.js` and `pdf.worker.js` files, meaning that its code is by definition duplicated. Some main-thread only utility functions have already been moved to a separate `src/display/display_utils.js` file, and this patch simply extends that concept to utility functions which are used *only* on the worker-thread. Note in particular the `getInheritableProperty` function, which expects a `Dict` as input and thus *cannot* possibly ever be used on the main-thread.
This commit is contained in:
parent
a1f7517996
commit
db5dc14158
21 changed files with 359 additions and 308 deletions
160
src/core/core_utils.js
Normal file
160
src/core/core_utils.js
Normal file
|
@ -0,0 +1,160 @@
|
|||
/* 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 */
|
||||
|
||||
import { assert, warn } from '../shared/util';
|
||||
|
||||
function getLookupTableFactory(initializer) {
|
||||
let lookup;
|
||||
return function() {
|
||||
if (initializer) {
|
||||
lookup = Object.create(null);
|
||||
initializer(lookup);
|
||||
initializer = null;
|
||||
}
|
||||
return lookup;
|
||||
};
|
||||
}
|
||||
|
||||
const MissingDataException = (function MissingDataExceptionClosure() {
|
||||
function MissingDataException(begin, end) {
|
||||
this.begin = begin;
|
||||
this.end = end;
|
||||
this.message = `Missing data [${begin}, ${end})`;
|
||||
}
|
||||
|
||||
MissingDataException.prototype = new Error();
|
||||
MissingDataException.prototype.name = 'MissingDataException';
|
||||
MissingDataException.constructor = MissingDataException;
|
||||
|
||||
return MissingDataException;
|
||||
})();
|
||||
|
||||
const XRefEntryException = (function XRefEntryExceptionClosure() {
|
||||
function XRefEntryException(msg) {
|
||||
this.message = msg;
|
||||
}
|
||||
|
||||
XRefEntryException.prototype = new Error();
|
||||
XRefEntryException.prototype.name = 'XRefEntryException';
|
||||
XRefEntryException.constructor = XRefEntryException;
|
||||
|
||||
return XRefEntryException;
|
||||
})();
|
||||
|
||||
const XRefParseException = (function XRefParseExceptionClosure() {
|
||||
function XRefParseException(msg) {
|
||||
this.message = msg;
|
||||
}
|
||||
|
||||
XRefParseException.prototype = new Error();
|
||||
XRefParseException.prototype.name = 'XRefParseException';
|
||||
XRefParseException.constructor = XRefParseException;
|
||||
|
||||
return XRefParseException;
|
||||
})();
|
||||
|
||||
/**
|
||||
* 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`.
|
||||
*/
|
||||
function getInheritableProperty({ dict, key, getArray = false,
|
||||
stopWhenFound = true, }) {
|
||||
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;
|
||||
}
|
||||
dict = dict.get('Parent');
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
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'
|
||||
];
|
||||
|
||||
/**
|
||||
* Converts positive integers to (upper case) Roman numerals.
|
||||
* @param {integer} number - The number that should be converted.
|
||||
* @param {boolean} lowerCase - Indicates if the result should be converted
|
||||
* to lower case letters. The default value is `false`.
|
||||
* @return {string} The resulting Roman number.
|
||||
*/
|
||||
function toRomanNumerals(number, lowerCase = false) {
|
||||
assert(Number.isInteger(number) && number > 0,
|
||||
'The number should be a positive integer.');
|
||||
let pos, romanBuf = [];
|
||||
// Thousands
|
||||
while (number >= 1000) {
|
||||
number -= 1000;
|
||||
romanBuf.push('M');
|
||||
}
|
||||
// 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]);
|
||||
|
||||
const romanStr = romanBuf.join('');
|
||||
return (lowerCase ? romanStr.toLowerCase() : romanStr);
|
||||
}
|
||||
|
||||
export {
|
||||
getLookupTableFactory,
|
||||
MissingDataException,
|
||||
XRefEntryException,
|
||||
XRefParseException,
|
||||
getInheritableProperty,
|
||||
toRomanNumerals,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue