Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move worker-thread only functions from src/shared/util.js and into a new src/core/core_utils.js file #10577

Merged
merged 3 commits into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

import {
AnnotationBorderStyleType, AnnotationFieldFlag, AnnotationFlag,
AnnotationType, getInheritableProperty, OPS, stringToBytes, stringToPDFString,
Util, warn
AnnotationType, OPS, stringToBytes, stringToPDFString, Util, warn
} from '../shared/util';
import { Catalog, FileSpec, ObjectLoader } from './obj';
import { Dict, isDict, isName, isRef, isStream } from './primitives';
import { ColorSpace } from './colorspace';
import { getInheritableProperty } from './core_utils';
import { OperatorList } from './operator_list';
import { Stream } from './stream';

Expand Down
4 changes: 2 additions & 2 deletions src/core/chunked_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
/* eslint no-var: error */

import {
arrayByteLength, arraysToBytes, createPromiseCapability, isEmptyObj,
MissingDataException
arrayByteLength, arraysToBytes, createPromiseCapability, isEmptyObj
} from '../shared/util';
import { MissingDataException } from './core_utils';

class ChunkedStream {
constructor(length, chunkSize, manager) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/cmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
*/

import {
CMapCompressionType, FormatError, isString, MissingDataException, unreachable,
warn
CMapCompressionType, FormatError, isString, unreachable, warn
} from '../shared/util';
import { isCmd, isEOF, isName, isStream } from './primitives';
import { Lexer } from './parser';
import { MissingDataException } from './core_utils';
import { Stream } from './stream';

var BUILT_IN_CMAPS = [
Expand Down
160 changes: 160 additions & 0 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
@@ -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,
};
9 changes: 6 additions & 3 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
/* eslint no-var: error */

import {
assert, FormatError, getInheritableProperty, info, isArrayBuffer, isBool,
isNum, isSpace, isString, MissingDataException, OPS, shadow, stringToBytes,
stringToPDFString, Util, warn, XRefEntryException, XRefParseException
assert, FormatError, info, isArrayBuffer, isBool, isNum, isSpace, isString,
OPS, shadow, stringToBytes, stringToPDFString, Util, warn
} from '../shared/util';
import { Catalog, ObjectLoader, XRef } from './obj';
import { Dict, isDict, isName, isStream, Ref } from './primitives';
import {
getInheritableProperty, MissingDataException, XRefEntryException,
XRefParseException
} from './core_utils';
import { NullStream, Stream, StreamsSequenceStream } from './stream';
import { AnnotationFactory } from './annotation';
import { calculateMD5 } from './crypto';
Expand Down
7 changes: 4 additions & 3 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

import {
AbortException, assert, CMapCompressionType, createPromiseCapability,
FONT_IDENTITY_MATRIX, FormatError, getLookupTableFactory, IDENTITY_MATRIX,
info, isNum, isString, NativeImageDecoding, OPS, stringToPDFString,
TextRenderingMode, UNSUPPORTED_FEATURES, Util, warn
FONT_IDENTITY_MATRIX, FormatError, IDENTITY_MATRIX, info, isNum, isString,
NativeImageDecoding, OPS, stringToPDFString, TextRenderingMode,
UNSUPPORTED_FEATURES, Util, warn
} from '../shared/util';
import { CMapFactory, IdentityCMap } from './cmap';
import { DecodeStream, Stream } from './stream';
Expand All @@ -42,6 +42,7 @@ import { Lexer, Parser } from './parser';
import { bidi } from './bidi';
import { ColorSpace } from './colorspace';
import { getGlyphsUnicode } from './glyphlist';
import { getLookupTableFactory } from './core_utils';
import { getMetrics } from './metrics';
import { isPDFFunction } from './function';
import { JpegStream } from './jpeg_stream';
Expand Down
4 changes: 2 additions & 2 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

import {
assert, bytesToString, FONT_IDENTITY_MATRIX, FontType, FormatError, info,
isNum, isSpace, MissingDataException, readUint32, shadow, string32,
unreachable, warn
isNum, isSpace, readUint32, shadow, string32, unreachable, warn
} from '../shared/util';
import {
CFF, CFFCharset, CFFCompiler, CFFHeader, CFFIndex, CFFParser, CFFPrivateDict,
Expand All @@ -36,6 +35,7 @@ import {
} from './unicode';
import { FontRendererFactory } from './font_renderer';
import { IdentityCMap } from './cmap';
import { MissingDataException } from './core_utils';
import { Stream } from './stream';
import { Type1Parser } from './type1_parser';

Expand Down
2 changes: 1 addition & 1 deletion src/core/glyphlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
/* no-babel-preset */

var getLookupTableFactory = require('../shared/util').getLookupTableFactory;
var getLookupTableFactory = require('./core_utils').getLookupTableFactory;

var getGlyphsUnicode = getLookupTableFactory(function (t) {
t['A'] = 0x0041;
Expand Down
2 changes: 1 addition & 1 deletion src/core/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

import { getLookupTableFactory } from '../shared/util';
import { getLookupTableFactory } from './core_utils';

// The Metrics object contains glyph widths (in glyph space units).
// As per PDF spec, for most fonts (Type 3 being an exception) a glyph
Expand Down
8 changes: 5 additions & 3 deletions src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@

import {
bytesToString, createPromiseCapability, createValidAbsoluteUrl, FormatError,
info, InvalidPDFException, isBool, isNum, isString, MissingDataException,
PermissionFlag, shadow, stringToPDFString, stringToUTF8String,
toRomanNumerals, unreachable, warn, XRefEntryException, XRefParseException
info, InvalidPDFException, isBool, isNum, isString, PermissionFlag, shadow,
stringToPDFString, stringToUTF8String, unreachable, warn
} from '../shared/util';
import {
Dict, isCmd, isDict, isName, isRef, isRefsEqual, isStream, Ref, RefSet,
RefSetCache
} from './primitives';
import { Lexer, Parser } from './parser';
import {
MissingDataException, toRomanNumerals, XRefEntryException, XRefParseException
} from './core_utils';
import { ChunkedStream } from './chunked_stream';
import { CipherTransformFactory } from './crypto';
import { ColorSpace } from './colorspace';
Expand Down
3 changes: 2 additions & 1 deletion src/core/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from './stream';
import {
assert, bytesToString, FormatError, info, isNum, isSpace, isString,
MissingDataException, StreamType, warn
StreamType, warn
} from '../shared/util';
import {
Cmd, Dict, EOF, isCmd, isDict, isEOF, isName, Name, Ref
Expand All @@ -28,6 +28,7 @@ import { CCITTFaxStream } from './ccitt_stream';
import { Jbig2Stream } from './jbig2_stream';
import { JpegStream } from './jpeg_stream';
import { JpxStream } from './jpx_stream';
import { MissingDataException } from './core_utils';

const MAX_LENGTH_TO_CACHE = 1000;
const MAX_ADLER32_LENGTH = 5552;
Expand Down
4 changes: 2 additions & 2 deletions src/core/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
/* eslint-disable no-multi-spaces */

import {
assert, FormatError, info, MissingDataException, unreachable,
UNSUPPORTED_FEATURES, Util, warn
assert, FormatError, info, unreachable, UNSUPPORTED_FEATURES, Util, warn
} from '../shared/util';
import { ColorSpace } from './colorspace';
import { isStream } from './primitives';
import { MissingDataException } from './core_utils';

var ShadingType = {
FUNCTION_BASED: 1,
Expand Down
3 changes: 2 additions & 1 deletion src/core/pdf_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
*/

import {
createValidAbsoluteUrl, MissingDataException, shadow, unreachable, warn
createValidAbsoluteUrl, shadow, unreachable, warn
} from '../shared/util';
import { ChunkedStreamManager } from './chunked_stream';
import { MissingDataException } from './core_utils';
import { PDFDocument } from './document';
import { Stream } from './stream';

Expand Down
2 changes: 1 addition & 1 deletion src/core/standard_fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
/* eslint no-var: error */

import { getLookupTableFactory } from '../shared/util';
import { getLookupTableFactory } from './core_utils';

/**
* Hold a map of decoded fonts and of the standard fourteen Type1
Expand Down
2 changes: 1 addition & 1 deletion src/core/unicode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
/* no-babel-preset */

var getLookupTableFactory = require('../shared/util').getLookupTableFactory;
var getLookupTableFactory = require('./core_utils').getLookupTableFactory;

// Some characters, e.g. copyrightserif, are mapped to the private use area
// and might not be displayed using standard fonts. Mapping/hacking well-known
Expand Down
3 changes: 2 additions & 1 deletion src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import {
arrayByteLength, arraysToBytes, assert, createPromiseCapability, info,
InvalidPDFException, MissingPDFException, PasswordException,
setVerbosityLevel, UnexpectedResponseException, UnknownErrorException,
UNSUPPORTED_FEATURES, warn, XRefParseException
UNSUPPORTED_FEATURES, warn
} from '../shared/util';
import { LocalPdfManager, NetworkPdfManager } from './pdf_manager';
import isNodeJS from '../shared/is_node';
import { MessageHandler } from '../shared/message_handler';
import { Ref } from './primitives';
import { XRefParseException } from './core_utils';

var WorkerTask = (function WorkerTaskClosure() {
function WorkerTask(name) {
Expand Down
2 changes: 1 addition & 1 deletion src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import {
addLinkAttributes, DOMSVGFactory, getFilenameFromUrl, LinkTarget
} from './dom_utils';
} from './display_utils';
import {
AnnotationBorderStyleType, AnnotationType, stringToPDFString, unreachable,
Util, warn
Expand Down
2 changes: 1 addition & 1 deletion src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import {
DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer, loadScript,
PageViewport, RenderingCancelledException, StatTimer
} from './dom_utils';
} from './display_utils';
import { FontFaceObject, FontLoader } from './font_loader';
import { apiCompatibilityParams } from './api_compatibility';
import { CanvasGraphics } from './canvas';
Expand Down
File renamed without changes.
Loading