1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02: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.)
This commit is contained in:
Jonas Jenwald 2019-12-25 15:59:37 +01:00
parent 8ec1dfde49
commit de36b2aaba
205 changed files with 40024 additions and 31859 deletions

View file

@ -15,16 +15,25 @@
/* eslint no-var: error */
import {
AnnotationBorderStyleType, AnnotationFieldFlag, AnnotationFlag,
AnnotationReplyType, AnnotationType, assert, isString, 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';
AnnotationBorderStyleType,
AnnotationFieldFlag,
AnnotationFlag,
AnnotationReplyType,
AnnotationType,
assert,
isString,
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";
class AnnotationFactory {
/**
@ -40,8 +49,12 @@ class AnnotationFactory {
* instance.
*/
static create(xref, ref, pdfManager, idFactory) {
return pdfManager.ensure(this, '_create',
[xref, ref, pdfManager, idFactory]);
return pdfManager.ensure(this, "_create", [
xref,
ref,
pdfManager,
idFactory,
]);
}
/**
@ -55,7 +68,7 @@ class AnnotationFactory {
const id = isRef(ref) ? ref.toString() : `annot_${idFactory.createObjId()}`;
// Determine the annotation's subtype.
let subtype = dict.get('Subtype');
let subtype = dict.get("Subtype");
subtype = isName(subtype) ? subtype.name : null;
// Return the right annotation object based on the subtype and field type.
@ -68,79 +81,87 @@ class AnnotationFactory {
};
switch (subtype) {
case 'Link':
case "Link":
return new LinkAnnotation(parameters);
case 'Text':
case "Text":
return new TextAnnotation(parameters);
case 'Widget':
let fieldType = getInheritableProperty({ dict, key: 'FT', });
case "Widget":
let fieldType = getInheritableProperty({ dict, key: "FT" });
fieldType = isName(fieldType) ? fieldType.name : null;
switch (fieldType) {
case 'Tx':
case "Tx":
return new TextWidgetAnnotation(parameters);
case 'Btn':
case "Btn":
return new ButtonWidgetAnnotation(parameters);
case 'Ch':
case "Ch":
return new ChoiceWidgetAnnotation(parameters);
}
warn('Unimplemented widget field type "' + fieldType + '", ' +
'falling back to base field type.');
warn(
'Unimplemented widget field type "' +
fieldType +
'", ' +
"falling back to base field type."
);
return new WidgetAnnotation(parameters);
case 'Popup':
case "Popup":
return new PopupAnnotation(parameters);
case 'FreeText':
case "FreeText":
return new FreeTextAnnotation(parameters);
case 'Line':
case "Line":
return new LineAnnotation(parameters);
case 'Square':
case "Square":
return new SquareAnnotation(parameters);
case 'Circle':
case "Circle":
return new CircleAnnotation(parameters);
case 'PolyLine':
case "PolyLine":
return new PolylineAnnotation(parameters);
case 'Polygon':
case "Polygon":
return new PolygonAnnotation(parameters);
case 'Caret':
case "Caret":
return new CaretAnnotation(parameters);
case 'Ink':
case "Ink":
return new InkAnnotation(parameters);
case 'Highlight':
case "Highlight":
return new HighlightAnnotation(parameters);
case 'Underline':
case "Underline":
return new UnderlineAnnotation(parameters);
case 'Squiggly':
case "Squiggly":
return new SquigglyAnnotation(parameters);
case 'StrikeOut':
case "StrikeOut":
return new StrikeOutAnnotation(parameters);
case 'Stamp':
case "Stamp":
return new StampAnnotation(parameters);
case 'FileAttachment':
case "FileAttachment":
return new FileAttachmentAnnotation(parameters);
default:
if (!subtype) {
warn('Annotation is missing the required /Subtype.');
warn("Annotation is missing the required /Subtype.");
} else {
warn('Unimplemented annotation type "' + subtype + '", ' +
'falling back to base annotation.');
warn(
'Unimplemented annotation type "' +
subtype +
'", ' +
"falling back to base annotation."
);
}
return new Annotation(parameters);
}
@ -148,13 +169,13 @@ class AnnotationFactory {
}
function getQuadPoints(dict, rect) {
if (!dict.has('QuadPoints')) {
if (!dict.has("QuadPoints")) {
return null;
}
// The region is described as a number of quadrilaterals.
// Each quadrilateral must consist of eight coordinates.
const quadPoints = dict.getArray('QuadPoints');
const quadPoints = dict.getArray("QuadPoints");
if (!Array.isArray(quadPoints) || quadPoints.length % 8 > 0) {
return null;
}
@ -165,7 +186,7 @@ function getQuadPoints(dict, rect) {
// quadrilateral in the order [x1, y1, x2, y2, x3, y3, x4, y4].
// Convert this to an array of objects with x and y coordinates.
quadPointsLists.push([]);
for (let j = i * 8, jj = (i * 8) + 8; j < jj; j += 2) {
for (let j = i * 8, jj = i * 8 + 8; j < jj; j += 2) {
const x = quadPoints[j];
const y = quadPoints[j + 1];
@ -174,7 +195,7 @@ function getQuadPoints(dict, rect) {
if (x < rect[0] || x > rect[2] || y < rect[1] || y > rect[3]) {
return null;
}
quadPointsLists[i].push({ x, y, });
quadPointsLists[i].push({ x, y });
}
}
return quadPointsLists;
@ -182,8 +203,10 @@ function getQuadPoints(dict, rect) {
function getTransformMatrix(rect, bbox, matrix) {
// 12.5.5: Algorithm: Appearance streams
const [minX, minY, maxX, maxY] =
Util.getAxialAlignedBoundingBox(bbox, matrix);
const [minX, minY, maxX, maxY] = Util.getAxialAlignedBoundingBox(
bbox,
matrix
);
if (minX === maxX || minY === maxY) {
// From real-life file, bbox was [0, 0, 0, 0]. In this case,
// just apply the transform for rect
@ -198,7 +221,7 @@ function getTransformMatrix(rect, bbox, matrix) {
0,
yRatio,
rect[0] - minX * xRatio,
rect[1] - minY * yRatio
rect[1] - minY * yRatio,
];
}
@ -206,11 +229,11 @@ class Annotation {
constructor(params) {
const dict = params.dict;
this.setContents(dict.get('Contents'));
this.setModificationDate(dict.get('M'));
this.setFlags(dict.get('F'));
this.setRectangle(dict.getArray('Rect'));
this.setColor(dict.getArray('C'));
this.setContents(dict.get("Contents"));
this.setModificationDate(dict.get("M"));
this.setFlags(dict.get("F"));
this.setRectangle(dict.getArray("Rect"));
this.setColor(dict.getArray("C"));
this.setBorderStyle(dict);
this.setAppearance(dict);
@ -239,18 +262,22 @@ class Annotation {
* @private
*/
_isViewable(flags) {
return !this._hasFlag(flags, AnnotationFlag.INVISIBLE) &&
!this._hasFlag(flags, AnnotationFlag.HIDDEN) &&
!this._hasFlag(flags, AnnotationFlag.NOVIEW);
return (
!this._hasFlag(flags, AnnotationFlag.INVISIBLE) &&
!this._hasFlag(flags, AnnotationFlag.HIDDEN) &&
!this._hasFlag(flags, AnnotationFlag.NOVIEW)
);
}
/**
* @private
*/
_isPrintable(flags) {
return this._hasFlag(flags, AnnotationFlag.PRINT) &&
!this._hasFlag(flags, AnnotationFlag.INVISIBLE) &&
!this._hasFlag(flags, AnnotationFlag.HIDDEN);
return (
this._hasFlag(flags, AnnotationFlag.PRINT) &&
!this._hasFlag(flags, AnnotationFlag.INVISIBLE) &&
!this._hasFlag(flags, AnnotationFlag.HIDDEN)
);
}
/**
@ -283,7 +310,7 @@ class Annotation {
* description of the annotation's contents
*/
setContents(contents) {
this.contents = stringToPDFString(contents || '');
this.contents = stringToPDFString(contents || "");
}
/**
@ -295,8 +322,9 @@ class Annotation {
* annotation was last modified
*/
setModificationDate(modificationDate) {
this.modificationDate = isString(modificationDate) ?
modificationDate : null;
this.modificationDate = isString(modificationDate)
? modificationDate
: null;
}
/**
@ -309,7 +337,7 @@ class Annotation {
* @see {@link shared/util.js}
*/
setFlags(flags) {
this.flags = (Number.isInteger(flags) && flags > 0) ? flags : 0;
this.flags = Number.isInteger(flags) && flags > 0 ? flags : 0;
}
/**
@ -392,32 +420,35 @@ class Annotation {
* @param {Dict} borderStyle - The border style dictionary
*/
setBorderStyle(borderStyle) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(this.rectangle, 'setRectangle must have been called previously.');
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(this.rectangle, "setRectangle must have been called previously.");
}
this.borderStyle = new AnnotationBorderStyle();
if (!isDict(borderStyle)) {
return;
}
if (borderStyle.has('BS')) {
const dict = borderStyle.get('BS');
const dictType = dict.get('Type');
if (borderStyle.has("BS")) {
const dict = borderStyle.get("BS");
const dictType = dict.get("Type");
if (!dictType || isName(dictType, 'Border')) {
this.borderStyle.setWidth(dict.get('W'), this.rectangle);
this.borderStyle.setStyle(dict.get('S'));
this.borderStyle.setDashArray(dict.getArray('D'));
if (!dictType || isName(dictType, "Border")) {
this.borderStyle.setWidth(dict.get("W"), this.rectangle);
this.borderStyle.setStyle(dict.get("S"));
this.borderStyle.setDashArray(dict.getArray("D"));
}
} else if (borderStyle.has('Border')) {
const array = borderStyle.getArray('Border');
} else if (borderStyle.has("Border")) {
const array = borderStyle.getArray("Border");
if (Array.isArray(array) && array.length >= 3) {
this.borderStyle.setHorizontalCornerRadius(array[0]);
this.borderStyle.setVerticalCornerRadius(array[1]);
this.borderStyle.setWidth(array[2], this.rectangle);
if (array.length === 4) { // Dash array available
if (array.length === 4) {
// Dash array available
this.borderStyle.setDashArray(array[3]);
}
}
@ -441,13 +472,13 @@ class Annotation {
setAppearance(dict) {
this.appearance = null;
const appearanceStates = dict.get('AP');
const appearanceStates = dict.get("AP");
if (!isDict(appearanceStates)) {
return;
}
// In case the normal appearance is a stream, then it is used directly.
const normalAppearanceState = appearanceStates.get('N');
const normalAppearanceState = appearanceStates.get("N");
if (isStream(normalAppearanceState)) {
this.appearance = normalAppearanceState;
return;
@ -458,7 +489,7 @@ class Annotation {
// In case the normal appearance is a dictionary, the `AS` entry provides
// the key of the stream in this dictionary.
const as = dict.get('AS');
const as = dict.get("AS");
if (!isName(as) || !normalAppearanceState.has(as.name)) {
return;
}
@ -466,7 +497,7 @@ class Annotation {
}
loadResources(keys) {
return this.appearance.dict.getAsync('Resources').then((resources) => {
return this.appearance.dict.getAsync("Resources").then(resources => {
if (!resources) {
return undefined;
}
@ -486,30 +517,32 @@ class Annotation {
const data = this.data;
const appearanceDict = this.appearance.dict;
const resourcesPromise = this.loadResources([
'ExtGState',
'ColorSpace',
'Pattern',
'Shading',
'XObject',
'Font',
"ExtGState",
"ColorSpace",
"Pattern",
"Shading",
"XObject",
"Font",
]);
const bbox = appearanceDict.getArray('BBox') || [0, 0, 1, 1];
const matrix = appearanceDict.getArray('Matrix') || [1, 0, 0, 1, 0, 0];
const bbox = appearanceDict.getArray("BBox") || [0, 0, 1, 1];
const matrix = appearanceDict.getArray("Matrix") || [1, 0, 0, 1, 0, 0];
const transform = getTransformMatrix(data.rect, bbox, matrix);
return resourcesPromise.then((resources) => {
return resourcesPromise.then(resources => {
const opList = new OperatorList();
opList.addOp(OPS.beginAnnotation, [data.rect, transform, matrix]);
return evaluator.getOperatorList({
stream: this.appearance,
task,
resources,
operatorList: opList,
}).then(() => {
opList.addOp(OPS.endAnnotation, []);
this.appearance.reset();
return opList;
});
return evaluator
.getOperatorList({
stream: this.appearance,
task,
resources,
operatorList: opList,
})
.then(() => {
opList.addOp(OPS.endAnnotation, []);
this.appearance.reset();
return opList;
});
});
}
}
@ -535,10 +568,14 @@ class AnnotationBorderStyle {
* @param {Array} rect - The annotation `Rect` entry.
*/
setWidth(width, rect = [0, 0, 0, 0]) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(Array.isArray(rect) && rect.length === 4,
'A valid `rect` parameter must be provided.');
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
Array.isArray(rect) && rect.length === 4,
"A valid `rect` parameter must be provided."
);
}
// Some corrupt PDF generators may provide the width as a `Name`,
@ -555,8 +592,11 @@ class AnnotationBorderStyle {
// Ignore large `width`s, since they lead to the Annotation overflowing
// the size set by the `Rect` entry thus causing the `annotationLayer`
// to render it over the surrounding document (fixes bug1552113.pdf).
if ((maxWidth > 0 && maxHeight > 0) &&
(width > maxWidth || width > maxHeight)) {
if (
maxWidth > 0 &&
maxHeight > 0 &&
(width > maxWidth || width > maxHeight)
) {
warn(`AnnotationBorderStyle.setWidth - ignoring width: ${width}`);
width = 1;
}
@ -578,23 +618,23 @@ class AnnotationBorderStyle {
return;
}
switch (style.name) {
case 'S':
case "S":
this.style = AnnotationBorderStyleType.SOLID;
break;
case 'D':
case "D":
this.style = AnnotationBorderStyleType.DASHED;
break;
case 'B':
case "B":
this.style = AnnotationBorderStyleType.BEVELED;
break;
case 'I':
case "I":
this.style = AnnotationBorderStyleType.INSET;
break;
case 'U':
case "U":
this.style = AnnotationBorderStyleType.UNDERLINE;
break;
@ -620,7 +660,7 @@ class AnnotationBorderStyle {
let isValid = true;
let allZeros = true;
for (const element of dashArray) {
const validNumber = (+element >= 0);
const validNumber = +element >= 0;
if (!validNumber) {
isValid = false;
break;
@ -671,56 +711,56 @@ class MarkupAnnotation extends Annotation {
const dict = parameters.dict;
if (dict.has('IRT')) {
const rawIRT = dict.getRaw('IRT');
if (dict.has("IRT")) {
const rawIRT = dict.getRaw("IRT");
this.data.inReplyTo = isRef(rawIRT) ? rawIRT.toString() : null;
const rt = dict.get('RT');
const rt = dict.get("RT");
this.data.replyType = isName(rt) ? rt.name : AnnotationReplyType.REPLY;
}
if (this.data.replyType === AnnotationReplyType.GROUP) {
// Subordinate annotations in a group should inherit
// the group attributes from the primary annotation.
const parent = dict.get('IRT');
const parent = dict.get("IRT");
this.data.title = stringToPDFString(parent.get('T') || '');
this.data.title = stringToPDFString(parent.get("T") || "");
this.setContents(parent.get('Contents'));
this.setContents(parent.get("Contents"));
this.data.contents = this.contents;
if (!parent.has('CreationDate')) {
if (!parent.has("CreationDate")) {
this.data.creationDate = null;
} else {
this.setCreationDate(parent.get('CreationDate'));
this.setCreationDate(parent.get("CreationDate"));
this.data.creationDate = this.creationDate;
}
if (!parent.has('M')) {
if (!parent.has("M")) {
this.data.modificationDate = null;
} else {
this.setModificationDate(parent.get('M'));
this.setModificationDate(parent.get("M"));
this.data.modificationDate = this.modificationDate;
}
this.data.hasPopup = parent.has('Popup');
this.data.hasPopup = parent.has("Popup");
if (!parent.has('C')) {
if (!parent.has("C")) {
// Fall back to the default background color.
this.data.color = null;
} else {
this.setColor(parent.getArray('C'));
this.setColor(parent.getArray("C"));
this.data.color = this.color;
}
} else {
this.data.title = stringToPDFString(dict.get('T') || '');
this.data.title = stringToPDFString(dict.get("T") || "");
this.setCreationDate(dict.get('CreationDate'));
this.setCreationDate(dict.get("CreationDate"));
this.data.creationDate = this.creationDate;
this.data.hasPopup = dict.has('Popup');
this.data.hasPopup = dict.has("Popup");
if (!dict.has('C')) {
if (!dict.has("C")) {
// Fall back to the default background color.
this.data.color = null;
}
@ -749,16 +789,19 @@ class WidgetAnnotation extends Annotation {
data.annotationType = AnnotationType.WIDGET;
data.fieldName = this._constructFieldName(dict);
data.fieldValue = getInheritableProperty({ dict, key: 'V',
getArray: true, });
data.alternativeText = stringToPDFString(dict.get('TU') || '');
data.defaultAppearance = getInheritableProperty({ dict, key: 'DA', }) || '';
const fieldType = getInheritableProperty({ dict, key: 'FT', });
data.fieldValue = getInheritableProperty({
dict,
key: "V",
getArray: true,
});
data.alternativeText = stringToPDFString(dict.get("TU") || "");
data.defaultAppearance = getInheritableProperty({ dict, key: "DA" }) || "";
const fieldType = getInheritableProperty({ dict, key: "FT" });
data.fieldType = isName(fieldType) ? fieldType.name : null;
this.fieldResources = getInheritableProperty({ dict, key: 'DR', }) ||
Dict.empty;
this.fieldResources =
getInheritableProperty({ dict, key: "DR" }) || Dict.empty;
data.fieldFlags = getInheritableProperty({ dict, key: 'Ff', });
data.fieldFlags = getInheritableProperty({ dict, key: "Ff" });
if (!Number.isInteger(data.fieldFlags) || data.fieldFlags < 0) {
data.fieldFlags = 0;
}
@ -768,7 +811,7 @@ class WidgetAnnotation extends Annotation {
// Hide signatures because we cannot validate them, and unset the fieldValue
// since it's (most likely) a `Dict` which is non-serializable and will thus
// cause errors when sending annotations to the main-thread (issue 10347).
if (data.fieldType === 'Sig') {
if (data.fieldType === "Sig") {
data.fieldValue = null;
this.setFlags(AnnotationFlag.HIDDEN);
}
@ -786,26 +829,26 @@ class WidgetAnnotation extends Annotation {
_constructFieldName(dict) {
// Both the `Parent` and `T` fields are optional. While at least one of
// them should be provided, bad PDF generators may fail to do so.
if (!dict.has('T') && !dict.has('Parent')) {
warn('Unknown field name, falling back to empty field name.');
return '';
if (!dict.has("T") && !dict.has("Parent")) {
warn("Unknown field name, falling back to empty field name.");
return "";
}
// If no parent exists, the partial and fully qualified names are equal.
if (!dict.has('Parent')) {
return stringToPDFString(dict.get('T'));
if (!dict.has("Parent")) {
return stringToPDFString(dict.get("T"));
}
// Form the fully qualified field name by appending the partial name to
// the parent's fully qualified name, separated by a period.
const fieldName = [];
if (dict.has('T')) {
fieldName.unshift(stringToPDFString(dict.get('T')));
if (dict.has("T")) {
fieldName.unshift(stringToPDFString(dict.get("T")));
}
let loopDict = dict;
while (loopDict.has('Parent')) {
loopDict = loopDict.get('Parent');
while (loopDict.has("Parent")) {
loopDict = loopDict.get("Parent");
if (!isDict(loopDict)) {
// Even though it is not allowed according to the PDF specification,
// bad PDF generators may provide a `Parent` entry that is not a
@ -813,11 +856,11 @@ class WidgetAnnotation extends Annotation {
break;
}
if (loopDict.has('T')) {
fieldName.unshift(stringToPDFString(loopDict.get('T')));
if (loopDict.has("T")) {
fieldName.unshift(stringToPDFString(loopDict.get("T")));
}
}
return fieldName.join('.');
return fieldName.join(".");
}
/**
@ -851,17 +894,17 @@ class TextWidgetAnnotation extends WidgetAnnotation {
const dict = params.dict;
// The field value is always a string.
this.data.fieldValue = stringToPDFString(this.data.fieldValue || '');
this.data.fieldValue = stringToPDFString(this.data.fieldValue || "");
// Determine the alignment of text in the field.
let alignment = getInheritableProperty({ dict, key: 'Q', });
let alignment = getInheritableProperty({ dict, key: "Q" });
if (!Number.isInteger(alignment) || alignment < 0 || alignment > 2) {
alignment = null;
}
this.data.textAlignment = alignment;
// Determine the maximum length of text in the field.
let maximumLength = getInheritableProperty({ dict, key: 'MaxLen', });
let maximumLength = getInheritableProperty({ dict, key: "MaxLen" });
if (!Number.isInteger(maximumLength) || maximumLength < 0) {
maximumLength = null;
}
@ -869,11 +912,12 @@ class TextWidgetAnnotation extends WidgetAnnotation {
// Process field flags for the display layer.
this.data.multiLine = this.hasFieldFlag(AnnotationFieldFlag.MULTILINE);
this.data.comb = this.hasFieldFlag(AnnotationFieldFlag.COMB) &&
!this.hasFieldFlag(AnnotationFieldFlag.MULTILINE) &&
!this.hasFieldFlag(AnnotationFieldFlag.PASSWORD) &&
!this.hasFieldFlag(AnnotationFieldFlag.FILESELECT) &&
this.data.maxLen !== null;
this.data.comb =
this.hasFieldFlag(AnnotationFieldFlag.COMB) &&
!this.hasFieldFlag(AnnotationFieldFlag.MULTILINE) &&
!this.hasFieldFlag(AnnotationFieldFlag.PASSWORD) &&
!this.hasFieldFlag(AnnotationFieldFlag.FILESELECT) &&
this.data.maxLen !== null;
}
getOperatorList(evaluator, task, renderForms) {
@ -890,14 +934,16 @@ class TextWidgetAnnotation extends WidgetAnnotation {
}
const stream = new Stream(stringToBytes(this.data.defaultAppearance));
return evaluator.getOperatorList({
stream,
task,
resources: this.fieldResources,
operatorList,
}).then(function () {
return operatorList;
});
return evaluator
.getOperatorList({
stream,
task,
resources: this.fieldResources,
operatorList,
})
.then(function() {
return operatorList;
});
}
}
@ -905,10 +951,12 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
constructor(params) {
super(params);
this.data.checkBox = !this.hasFieldFlag(AnnotationFieldFlag.RADIO) &&
!this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
this.data.radioButton = this.hasFieldFlag(AnnotationFieldFlag.RADIO) &&
!this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
this.data.checkBox =
!this.hasFieldFlag(AnnotationFieldFlag.RADIO) &&
!this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
this.data.radioButton =
this.hasFieldFlag(AnnotationFieldFlag.RADIO) &&
!this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
this.data.pushButton = this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
if (this.data.checkBox) {
@ -918,7 +966,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
} else if (this.data.pushButton) {
this._processPushButton(params);
} else {
warn('Invalid field flags for button widget annotation');
warn("Invalid field flags for button widget annotation");
}
}
@ -927,12 +975,12 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
this.data.fieldValue = this.data.fieldValue.name;
}
const customAppearance = params.dict.get('AP');
const customAppearance = params.dict.get("AP");
if (!isDict(customAppearance)) {
return;
}
const exportValueOptionsDict = customAppearance.get('D');
const exportValueOptionsDict = customAppearance.get("D");
if (!isDict(exportValueOptionsDict)) {
return;
}
@ -943,8 +991,8 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
return;
}
this.data.exportValue = exportValues[0] === 'Off' ?
exportValues[1] : exportValues[0];
this.data.exportValue =
exportValues[0] === "Off" ? exportValues[1] : exportValues[0];
}
_processRadioButton(params) {
@ -952,25 +1000,25 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
// The parent field's `V` entry holds a `Name` object with the appearance
// state of whichever child field is currently in the "on" state.
const fieldParent = params.dict.get('Parent');
if (isDict(fieldParent) && fieldParent.has('V')) {
const fieldParentValue = fieldParent.get('V');
const fieldParent = params.dict.get("Parent");
if (isDict(fieldParent) && fieldParent.has("V")) {
const fieldParentValue = fieldParent.get("V");
if (isName(fieldParentValue)) {
this.data.fieldValue = fieldParentValue.name;
}
}
// The button's value corresponds to its appearance state.
const appearanceStates = params.dict.get('AP');
const appearanceStates = params.dict.get("AP");
if (!isDict(appearanceStates)) {
return;
}
const normalAppearanceState = appearanceStates.get('N');
const normalAppearanceState = appearanceStates.get("N");
if (!isDict(normalAppearanceState)) {
return;
}
for (const key of normalAppearanceState.getKeys()) {
if (key !== 'Off') {
if (key !== "Off") {
this.data.buttonValue = key;
break;
}
@ -978,8 +1026,8 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}
_processPushButton(params) {
if (!params.dict.has('A')) {
warn('Push buttons without action dictionaries are not supported');
if (!params.dict.has("A")) {
warn("Push buttons without action dictionaries are not supported");
return;
}
@ -1006,7 +1054,7 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
// inherit the options from a parent annotation (issue 8094).
this.data.options = [];
const options = getInheritableProperty({ dict: params.dict, key: 'Opt', });
const options = getInheritableProperty({ dict: params.dict, key: "Opt" });
if (Array.isArray(options)) {
const xref = params.xref;
for (let i = 0, ii = options.length; i < ii; i++) {
@ -1015,8 +1063,9 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
this.data.options[i] = {
exportValue: isOptionArray ? xref.fetchIfRef(option[0]) : option,
displayValue: stringToPDFString(isOptionArray ?
xref.fetchIfRef(option[1]) : option),
displayValue: stringToPDFString(
isOptionArray ? xref.fetchIfRef(option[1]) : option
),
};
}
}
@ -1044,17 +1093,16 @@ class TextAnnotation extends MarkupAnnotation {
this.data.annotationType = AnnotationType.TEXT;
if (this.data.hasAppearance) {
this.data.name = 'NoIcon';
this.data.name = "NoIcon";
} else {
this.data.rect[1] = this.data.rect[3] - DEFAULT_ICON_SIZE;
this.data.rect[2] = this.data.rect[0] + DEFAULT_ICON_SIZE;
this.data.name = dict.has('Name') ?
dict.get('Name').name : 'Note';
this.data.name = dict.has("Name") ? dict.get("Name").name : "Note";
}
if (dict.has('State')) {
this.data.state = dict.get('State') || null;
this.data.stateModel = dict.get('StateModel') || null;
if (dict.has("State")) {
this.data.state = dict.get("State") || null;
this.data.stateModel = dict.get("StateModel") || null;
} else {
this.data.state = null;
this.data.stateModel = null;
@ -1087,36 +1135,36 @@ class PopupAnnotation extends Annotation {
this.data.annotationType = AnnotationType.POPUP;
let parentItem = parameters.dict.get('Parent');
let parentItem = parameters.dict.get("Parent");
if (!parentItem) {
warn('Popup annotation has a missing or invalid parent annotation.');
warn("Popup annotation has a missing or invalid parent annotation.");
return;
}
const parentSubtype = parentItem.get('Subtype');
const parentSubtype = parentItem.get("Subtype");
this.data.parentType = isName(parentSubtype) ? parentSubtype.name : null;
const rawParent = parameters.dict.getRaw('Parent');
const rawParent = parameters.dict.getRaw("Parent");
this.data.parentId = isRef(rawParent) ? rawParent.toString() : null;
const rt = parentItem.get('RT');
const rt = parentItem.get("RT");
if (isName(rt, AnnotationReplyType.GROUP)) {
// Subordinate annotations in a group should inherit
// the group attributes from the primary annotation.
parentItem = parentItem.get('IRT');
parentItem = parentItem.get("IRT");
}
if (!parentItem.has('M')) {
if (!parentItem.has("M")) {
this.data.modificationDate = null;
} else {
this.setModificationDate(parentItem.get('M'));
this.setModificationDate(parentItem.get("M"));
this.data.modificationDate = this.modificationDate;
}
if (!parentItem.has('C')) {
if (!parentItem.has("C")) {
// Fall back to the default background color.
this.data.color = null;
} else {
this.setColor(parentItem.getArray('C'));
this.setColor(parentItem.getArray("C"));
this.data.color = this.color;
}
@ -1124,14 +1172,14 @@ class PopupAnnotation extends Annotation {
// that is most likely a bug. Fallback to inherit the flags from the parent
// annotation (this is consistent with the behaviour in Adobe Reader).
if (!this.viewable) {
const parentFlags = parentItem.get('F');
const parentFlags = parentItem.get("F");
if (this._isViewable(parentFlags)) {
this.setFlags(parentFlags);
}
}
this.data.title = stringToPDFString(parentItem.get('T') || '');
this.data.contents = stringToPDFString(parentItem.get('Contents') || '');
this.data.title = stringToPDFString(parentItem.get("T") || "");
this.data.contents = stringToPDFString(parentItem.get("Contents") || "");
}
}
@ -1149,8 +1197,9 @@ class LineAnnotation extends MarkupAnnotation {
this.data.annotationType = AnnotationType.LINE;
this.data.lineCoordinates =
Util.normalizeRect(parameters.dict.getArray('L'));
this.data.lineCoordinates = Util.normalizeRect(
parameters.dict.getArray("L")
);
}
}
@ -1179,7 +1228,7 @@ class PolylineAnnotation extends MarkupAnnotation {
// The vertices array is an array of numbers representing the alternating
// horizontal and vertical coordinates, respectively, of each vertex.
// Convert this to an array of objects with x and y coordinates.
const rawVertices = parameters.dict.getArray('Vertices');
const rawVertices = parameters.dict.getArray("Vertices");
this.data.vertices = [];
for (let i = 0, ii = rawVertices.length; i < ii; i += 2) {
this.data.vertices.push({
@ -1214,7 +1263,7 @@ class InkAnnotation extends MarkupAnnotation {
this.data.annotationType = AnnotationType.INK;
const xref = parameters.xref;
const originalInkLists = parameters.dict.getArray('InkList');
const originalInkLists = parameters.dict.getArray("InkList");
this.data.inkLists = [];
for (let i = 0, ii = originalInkLists.length; i < ii; ++i) {
// The raw ink lists array contains arrays of numbers representing
@ -1296,7 +1345,7 @@ class FileAttachmentAnnotation extends MarkupAnnotation {
constructor(parameters) {
super(parameters);
const file = new FileSpec(parameters.dict.get('FS'), parameters.xref);
const file = new FileSpec(parameters.dict.get("FS"), parameters.xref);
this.data.annotationType = AnnotationType.FILEATTACHMENT;
this.data.file = file.serializable;

View file

@ -16,53 +16,53 @@
// Table C-2
const QeTable = [
{ qe: 0x5601, nmps: 1, nlps: 1, switchFlag: 1, },
{ qe: 0x3401, nmps: 2, nlps: 6, switchFlag: 0, },
{ qe: 0x1801, nmps: 3, nlps: 9, switchFlag: 0, },
{ qe: 0x0AC1, nmps: 4, nlps: 12, switchFlag: 0, },
{ qe: 0x0521, nmps: 5, nlps: 29, switchFlag: 0, },
{ qe: 0x0221, nmps: 38, nlps: 33, switchFlag: 0, },
{ qe: 0x5601, nmps: 7, nlps: 6, switchFlag: 1, },
{ qe: 0x5401, nmps: 8, nlps: 14, switchFlag: 0, },
{ qe: 0x4801, nmps: 9, nlps: 14, switchFlag: 0, },
{ qe: 0x3801, nmps: 10, nlps: 14, switchFlag: 0, },
{ qe: 0x3001, nmps: 11, nlps: 17, switchFlag: 0, },
{ qe: 0x2401, nmps: 12, nlps: 18, switchFlag: 0, },
{ qe: 0x1C01, nmps: 13, nlps: 20, switchFlag: 0, },
{ qe: 0x1601, nmps: 29, nlps: 21, switchFlag: 0, },
{ qe: 0x5601, nmps: 15, nlps: 14, switchFlag: 1, },
{ qe: 0x5401, nmps: 16, nlps: 14, switchFlag: 0, },
{ qe: 0x5101, nmps: 17, nlps: 15, switchFlag: 0, },
{ qe: 0x4801, nmps: 18, nlps: 16, switchFlag: 0, },
{ qe: 0x3801, nmps: 19, nlps: 17, switchFlag: 0, },
{ qe: 0x3401, nmps: 20, nlps: 18, switchFlag: 0, },
{ qe: 0x3001, nmps: 21, nlps: 19, switchFlag: 0, },
{ qe: 0x2801, nmps: 22, nlps: 19, switchFlag: 0, },
{ qe: 0x2401, nmps: 23, nlps: 20, switchFlag: 0, },
{ qe: 0x2201, nmps: 24, nlps: 21, switchFlag: 0, },
{ qe: 0x1C01, nmps: 25, nlps: 22, switchFlag: 0, },
{ qe: 0x1801, nmps: 26, nlps: 23, switchFlag: 0, },
{ qe: 0x1601, nmps: 27, nlps: 24, switchFlag: 0, },
{ qe: 0x1401, nmps: 28, nlps: 25, switchFlag: 0, },
{ qe: 0x1201, nmps: 29, nlps: 26, switchFlag: 0, },
{ qe: 0x1101, nmps: 30, nlps: 27, switchFlag: 0, },
{ qe: 0x0AC1, nmps: 31, nlps: 28, switchFlag: 0, },
{ qe: 0x09C1, nmps: 32, nlps: 29, switchFlag: 0, },
{ qe: 0x08A1, nmps: 33, nlps: 30, switchFlag: 0, },
{ qe: 0x0521, nmps: 34, nlps: 31, switchFlag: 0, },
{ qe: 0x0441, nmps: 35, nlps: 32, switchFlag: 0, },
{ qe: 0x02A1, nmps: 36, nlps: 33, switchFlag: 0, },
{ qe: 0x0221, nmps: 37, nlps: 34, switchFlag: 0, },
{ qe: 0x0141, nmps: 38, nlps: 35, switchFlag: 0, },
{ qe: 0x0111, nmps: 39, nlps: 36, switchFlag: 0, },
{ qe: 0x0085, nmps: 40, nlps: 37, switchFlag: 0, },
{ qe: 0x0049, nmps: 41, nlps: 38, switchFlag: 0, },
{ qe: 0x0025, nmps: 42, nlps: 39, switchFlag: 0, },
{ qe: 0x0015, nmps: 43, nlps: 40, switchFlag: 0, },
{ qe: 0x0009, nmps: 44, nlps: 41, switchFlag: 0, },
{ qe: 0x0005, nmps: 45, nlps: 42, switchFlag: 0, },
{ qe: 0x0001, nmps: 45, nlps: 43, switchFlag: 0, },
{ qe: 0x5601, nmps: 46, nlps: 46, switchFlag: 0, },
{ qe: 0x5601, nmps: 1, nlps: 1, switchFlag: 1 },
{ qe: 0x3401, nmps: 2, nlps: 6, switchFlag: 0 },
{ qe: 0x1801, nmps: 3, nlps: 9, switchFlag: 0 },
{ qe: 0x0ac1, nmps: 4, nlps: 12, switchFlag: 0 },
{ qe: 0x0521, nmps: 5, nlps: 29, switchFlag: 0 },
{ qe: 0x0221, nmps: 38, nlps: 33, switchFlag: 0 },
{ qe: 0x5601, nmps: 7, nlps: 6, switchFlag: 1 },
{ qe: 0x5401, nmps: 8, nlps: 14, switchFlag: 0 },
{ qe: 0x4801, nmps: 9, nlps: 14, switchFlag: 0 },
{ qe: 0x3801, nmps: 10, nlps: 14, switchFlag: 0 },
{ qe: 0x3001, nmps: 11, nlps: 17, switchFlag: 0 },
{ qe: 0x2401, nmps: 12, nlps: 18, switchFlag: 0 },
{ qe: 0x1c01, nmps: 13, nlps: 20, switchFlag: 0 },
{ qe: 0x1601, nmps: 29, nlps: 21, switchFlag: 0 },
{ qe: 0x5601, nmps: 15, nlps: 14, switchFlag: 1 },
{ qe: 0x5401, nmps: 16, nlps: 14, switchFlag: 0 },
{ qe: 0x5101, nmps: 17, nlps: 15, switchFlag: 0 },
{ qe: 0x4801, nmps: 18, nlps: 16, switchFlag: 0 },
{ qe: 0x3801, nmps: 19, nlps: 17, switchFlag: 0 },
{ qe: 0x3401, nmps: 20, nlps: 18, switchFlag: 0 },
{ qe: 0x3001, nmps: 21, nlps: 19, switchFlag: 0 },
{ qe: 0x2801, nmps: 22, nlps: 19, switchFlag: 0 },
{ qe: 0x2401, nmps: 23, nlps: 20, switchFlag: 0 },
{ qe: 0x2201, nmps: 24, nlps: 21, switchFlag: 0 },
{ qe: 0x1c01, nmps: 25, nlps: 22, switchFlag: 0 },
{ qe: 0x1801, nmps: 26, nlps: 23, switchFlag: 0 },
{ qe: 0x1601, nmps: 27, nlps: 24, switchFlag: 0 },
{ qe: 0x1401, nmps: 28, nlps: 25, switchFlag: 0 },
{ qe: 0x1201, nmps: 29, nlps: 26, switchFlag: 0 },
{ qe: 0x1101, nmps: 30, nlps: 27, switchFlag: 0 },
{ qe: 0x0ac1, nmps: 31, nlps: 28, switchFlag: 0 },
{ qe: 0x09c1, nmps: 32, nlps: 29, switchFlag: 0 },
{ qe: 0x08a1, nmps: 33, nlps: 30, switchFlag: 0 },
{ qe: 0x0521, nmps: 34, nlps: 31, switchFlag: 0 },
{ qe: 0x0441, nmps: 35, nlps: 32, switchFlag: 0 },
{ qe: 0x02a1, nmps: 36, nlps: 33, switchFlag: 0 },
{ qe: 0x0221, nmps: 37, nlps: 34, switchFlag: 0 },
{ qe: 0x0141, nmps: 38, nlps: 35, switchFlag: 0 },
{ qe: 0x0111, nmps: 39, nlps: 36, switchFlag: 0 },
{ qe: 0x0085, nmps: 40, nlps: 37, switchFlag: 0 },
{ qe: 0x0049, nmps: 41, nlps: 38, switchFlag: 0 },
{ qe: 0x0025, nmps: 42, nlps: 39, switchFlag: 0 },
{ qe: 0x0015, nmps: 43, nlps: 40, switchFlag: 0 },
{ qe: 0x0009, nmps: 44, nlps: 41, switchFlag: 0 },
{ qe: 0x0005, nmps: 45, nlps: 42, switchFlag: 0 },
{ qe: 0x0001, nmps: 45, nlps: 43, switchFlag: 0 },
{ qe: 0x5601, nmps: 46, nlps: 46, switchFlag: 0 },
];
/**
@ -86,8 +86,8 @@ class ArithmeticDecoder {
this.byteIn();
this.chigh = ((this.chigh << 7) & 0xFFFF) | ((this.clow >> 9) & 0x7F);
this.clow = (this.clow << 7) & 0xFFFF;
this.chigh = ((this.chigh << 7) & 0xffff) | ((this.clow >> 9) & 0x7f);
this.clow = (this.clow << 7) & 0xffff;
this.ct -= 7;
this.a = 0x8000;
}
@ -97,25 +97,25 @@ class ArithmeticDecoder {
const data = this.data;
let bp = this.bp;
if (data[bp] === 0xFF) {
if (data[bp + 1] > 0x8F) {
this.clow += 0xFF00;
if (data[bp] === 0xff) {
if (data[bp + 1] > 0x8f) {
this.clow += 0xff00;
this.ct = 8;
} else {
bp++;
this.clow += (data[bp] << 9);
this.clow += data[bp] << 9;
this.ct = 7;
this.bp = bp;
}
} else {
bp++;
this.clow += bp < this.dataEnd ? (data[bp] << 8) : 0xFF00;
this.clow += bp < this.dataEnd ? data[bp] << 8 : 0xff00;
this.ct = 8;
this.bp = bp;
}
if (this.clow > 0xFFFF) {
this.chigh += (this.clow >> 16);
this.clow &= 0xFFFF;
if (this.clow > 0xffff) {
this.chigh += this.clow >> 16;
this.clow &= 0xffff;
}
}
@ -123,7 +123,8 @@ class ArithmeticDecoder {
readBit(contexts, pos) {
// Contexts are packed into 1 byte:
// highest 7 bits carry cx.index, lowest bit carries cx.mps
let cx_index = contexts[pos] >> 1, cx_mps = contexts[pos] & 1;
let cx_index = contexts[pos] >> 1,
cx_mps = contexts[pos] & 1;
const qeTableIcx = QeTable[cx_index];
const qeIcx = qeTableIcx.qe;
let d;
@ -168,17 +169,15 @@ class ArithmeticDecoder {
}
a <<= 1;
this.chigh = ((this.chigh << 1) & 0xFFFF) | ((this.clow >> 15) & 1);
this.clow = (this.clow << 1) & 0xFFFF;
this.chigh = ((this.chigh << 1) & 0xffff) | ((this.clow >> 15) & 1);
this.clow = (this.clow << 1) & 0xffff;
this.ct--;
} while ((a & 0x8000) === 0);
this.a = a;
contexts[pos] = cx_index << 1 | cx_mps;
contexts[pos] = (cx_index << 1) | cx_mps;
return d;
}
}
export {
ArithmeticDecoder,
};
export { ArithmeticDecoder };

View file

@ -13,32 +13,32 @@
* limitations under the License.
*/
import { warn } from '../shared/util';
import { warn } from "../shared/util";
// Character types for symbols from 0000 to 00FF.
// Source: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
// prettier-ignore
var baseTypes = [
'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'S', 'B', 'S',
'WS', 'B', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN',
'BN', 'BN', 'BN', 'BN', 'B', 'B', 'B', 'S', 'WS', 'ON', 'ON', 'ET',
'ET', 'ET', 'ON', 'ON', 'ON', 'ON', 'ON', 'ES', 'CS', 'ES', 'CS', 'CS',
'EN', 'EN', 'EN', 'EN', 'EN', 'EN', 'EN', 'EN', 'EN', 'EN', 'CS', 'ON',
'ON', 'ON', 'ON', 'ON', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L',
'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L',
'L', 'L', 'L', 'L', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'L', 'L', 'L',
'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L',
'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'ON', 'ON', 'ON', 'ON',
'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'B', 'BN', 'BN', 'BN', 'BN', 'BN',
'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN',
'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'CS', 'ON', 'ET',
'ET', 'ET', 'ET', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'ON', 'BN', 'ON',
'ON', 'ET', 'ET', 'EN', 'EN', 'ON', 'L', 'ON', 'ON', 'ON', 'EN', 'L',
'ON', 'ON', 'ON', 'ON', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L',
'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L',
'L', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L',
'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L',
'L', 'L', 'L', 'L', 'L', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L'
"BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "S", "B", "S",
"WS", "B", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN",
"BN", "BN", "BN", "BN", "B", "B", "B", "S", "WS", "ON", "ON", "ET",
"ET", "ET", "ON", "ON", "ON", "ON", "ON", "ES", "CS", "ES", "CS", "CS",
"EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "CS", "ON",
"ON", "ON", "ON", "ON", "ON", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "ON", "ON", "ON", "ON", "ON", "ON", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "ON", "ON", "ON", "ON",
"BN", "BN", "BN", "BN", "BN", "BN", "B", "BN", "BN", "BN", "BN", "BN",
"BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN",
"BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "CS", "ON", "ET",
"ET", "ET", "ET", "ON", "ON", "ON", "ON", "L", "ON", "ON", "BN", "ON",
"ON", "ET", "ET", "EN", "EN", "ON", "L", "ON", "ON", "ON", "EN", "L",
"ON", "ON", "ON", "ON", "ON", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "ON", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "ON", "L", "L", "L", "L", "L", "L", "L", "L"
];
// Character types for symbols from 0600 to 06FF.
@ -49,28 +49,28 @@ var baseTypes = [
// empty string is required to properly index the items after it.
// prettier-ignore
var arabicTypes = [
'AN', 'AN', 'AN', 'AN', 'AN', 'AN', 'ON', 'ON', 'AL', 'ET', 'ET', 'AL',
'CS', 'AL', 'ON', 'ON', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM',
'NSM', 'NSM', 'NSM', 'NSM', 'AL', 'AL', '', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM',
'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM',
'NSM', 'NSM', 'NSM', 'NSM', 'AN', 'AN', 'AN', 'AN', 'AN', 'AN', 'AN',
'AN', 'AN', 'AN', 'ET', 'AN', 'AN', 'AL', 'AL', 'AL', 'NSM', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL',
'AL', 'AL', 'AL', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'AN',
'ON', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'AL', 'AL', 'NSM', 'NSM',
'ON', 'NSM', 'NSM', 'NSM', 'NSM', 'AL', 'AL', 'EN', 'EN', 'EN', 'EN',
'EN', 'EN', 'EN', 'EN', 'EN', 'EN', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL'
"AN", "AN", "AN", "AN", "AN", "AN", "ON", "ON", "AL", "ET", "ET", "AL",
"CS", "AL", "ON", "ON", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM",
"NSM", "NSM", "NSM", "NSM", "AL", "AL", "", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM",
"NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM",
"NSM", "NSM", "NSM", "NSM", "AN", "AN", "AN", "AN", "AN", "AN", "AN",
"AN", "AN", "AN", "ET", "AN", "AN", "AL", "AL", "AL", "NSM", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "AN",
"ON", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "AL", "AL", "NSM", "NSM",
"ON", "NSM", "NSM", "NSM", "NSM", "AL", "AL", "EN", "EN", "EN", "EN",
"EN", "EN", "EN", "EN", "EN", "EN", "AL", "AL", "AL", "AL", "AL", "AL"
];
function isOdd(i) {
@ -107,7 +107,7 @@ function reverseValues(arr, start, end) {
function createBidiText(str, isLTR, vertical) {
return {
str,
dir: (vertical ? 'ttb' : (isLTR ? 'ltr' : 'rtl')),
dir: vertical ? "ttb" : isLTR ? "ltr" : "rtl",
};
}
@ -133,20 +133,20 @@ function bidi(str, startLevel, vertical) {
chars[i] = str.charAt(i);
var charCode = str.charCodeAt(i);
var charType = 'L';
var charType = "L";
if (charCode <= 0x00ff) {
charType = baseTypes[charCode];
} else if (0x0590 <= charCode && charCode <= 0x05f4) {
charType = 'R';
charType = "R";
} else if (0x0600 <= charCode && charCode <= 0x06ff) {
charType = arabicTypes[charCode & 0xff];
if (!charType) {
warn('Bidi: invalid Unicode character ' + charCode.toString(16));
warn("Bidi: invalid Unicode character " + charCode.toString(16));
}
} else if (0x0700 <= charCode && charCode <= 0x08AC) {
charType = 'AL';
} else if (0x0700 <= charCode && charCode <= 0x08ac) {
charType = "AL";
}
if (charType === 'R' || charType === 'AL' || charType === 'AN') {
if (charType === "R" || charType === "AL" || charType === "AN") {
numBidi++;
}
types[i] = charType;
@ -162,7 +162,7 @@ function bidi(str, startLevel, vertical) {
}
if (startLevel === -1) {
if ((numBidi / strLength) < 0.3) {
if (numBidi / strLength < 0.3) {
isLTR = true;
startLevel = 0;
} else {
@ -179,7 +179,7 @@ function bidi(str, startLevel, vertical) {
/*
X1-X10: skip most of this, since we are NOT doing the embeddings.
*/
var e = (isOdd(startLevel) ? 'R' : 'L');
var e = isOdd(startLevel) ? "R" : "L";
var sor = e;
var eor = sor;
@ -190,7 +190,7 @@ function bidi(str, startLevel, vertical) {
*/
var lastType = sor;
for (i = 0; i < strLength; ++i) {
if (types[i] === 'NSM') {
if (types[i] === "NSM") {
types[i] = lastType;
} else {
lastType = types[i];
@ -206,9 +206,9 @@ function bidi(str, startLevel, vertical) {
var t;
for (i = 0; i < strLength; ++i) {
t = types[i];
if (t === 'EN') {
types[i] = (lastType === 'AL') ? 'AN' : 'EN';
} else if (t === 'R' || t === 'L' || t === 'AL') {
if (t === "EN") {
types[i] = lastType === "AL" ? "AN" : "EN";
} else if (t === "R" || t === "L" || t === "AL") {
lastType = t;
}
}
@ -218,8 +218,8 @@ function bidi(str, startLevel, vertical) {
*/
for (i = 0; i < strLength; ++i) {
t = types[i];
if (t === 'AL') {
types[i] = 'R';
if (t === "AL") {
types[i] = "R";
}
}
@ -229,12 +229,14 @@ function bidi(str, startLevel, vertical) {
type changes to that type:
*/
for (i = 1; i < strLength - 1; ++i) {
if (types[i] === 'ES' && types[i - 1] === 'EN' && types[i + 1] === 'EN') {
types[i] = 'EN';
if (types[i] === "ES" && types[i - 1] === "EN" && types[i + 1] === "EN") {
types[i] = "EN";
}
if (types[i] === 'CS' &&
(types[i - 1] === 'EN' || types[i - 1] === 'AN') &&
types[i + 1] === types[i - 1]) {
if (
types[i] === "CS" &&
(types[i - 1] === "EN" || types[i - 1] === "AN") &&
types[i + 1] === types[i - 1]
) {
types[i] = types[i - 1];
}
}
@ -244,21 +246,21 @@ function bidi(str, startLevel, vertical) {
to all European numbers:
*/
for (i = 0; i < strLength; ++i) {
if (types[i] === 'EN') {
if (types[i] === "EN") {
// do before
var j;
for (j = i - 1; j >= 0; --j) {
if (types[j] !== 'ET') {
if (types[j] !== "ET") {
break;
}
types[j] = 'EN';
types[j] = "EN";
}
// do after
for (j = i + 1; j < strLength; ++j) {
if (types[j] !== 'ET') {
if (types[j] !== "ET") {
break;
}
types[j] = 'EN';
types[j] = "EN";
}
}
}
@ -268,8 +270,8 @@ function bidi(str, startLevel, vertical) {
*/
for (i = 0; i < strLength; ++i) {
t = types[i];
if (t === 'WS' || t === 'ES' || t === 'ET' || t === 'CS') {
types[i] = 'ON';
if (t === "WS" || t === "ES" || t === "ET" || t === "CS") {
types[i] = "ON";
}
}
@ -281,9 +283,9 @@ function bidi(str, startLevel, vertical) {
lastType = sor;
for (i = 0; i < strLength; ++i) {
t = types[i];
if (t === 'EN') {
types[i] = ((lastType === 'L') ? 'L' : 'EN');
} else if (t === 'R' || t === 'L') {
if (t === "EN") {
types[i] = lastType === "L" ? "L" : "EN";
} else if (t === "R" || t === "L") {
lastType = t;
}
}
@ -295,8 +297,8 @@ function bidi(str, startLevel, vertical) {
end-of-level-run (eor) are used at level run boundaries.
*/
for (i = 0; i < strLength; ++i) {
if (types[i] === 'ON') {
var end = findUnequal(types, i + 1, 'ON');
if (types[i] === "ON") {
var end = findUnequal(types, i + 1, "ON");
var before = sor;
if (i > 0) {
before = types[i - 1];
@ -306,11 +308,11 @@ function bidi(str, startLevel, vertical) {
if (end + 1 < strLength) {
after = types[end + 1];
}
if (before !== 'L') {
before = 'R';
if (before !== "L") {
before = "R";
}
if (after !== 'L') {
after = 'R';
if (after !== "L") {
after = "R";
}
if (before === after) {
setValues(types, i, end, before);
@ -323,7 +325,7 @@ function bidi(str, startLevel, vertical) {
N2. Any remaining neutrals take the embedding direction.
*/
for (i = 0; i < strLength; ++i) {
if (types[i] === 'ON') {
if (types[i] === "ON") {
types[i] = e;
}
}
@ -338,13 +340,14 @@ function bidi(str, startLevel, vertical) {
for (i = 0; i < strLength; ++i) {
t = types[i];
if (isEven(levels[i])) {
if (t === 'R') {
if (t === "R") {
levels[i] += 1;
} else if (t === 'AN' || t === 'EN') {
} else if (t === "AN" || t === "EN") {
levels[i] += 2;
}
} else { // isOdd
if (t === 'L' || t === 'AN' || t === 'EN') {
} else {
// isOdd
if (t === "L" || t === "AN" || t === "EN") {
levels[i] += 1;
}
}
@ -422,13 +425,11 @@ function bidi(str, startLevel, vertical) {
// Finally, return string
for (i = 0, ii = chars.length; i < ii; ++i) {
var ch = chars[i];
if (ch === '<' || ch === '>') {
chars[i] = '';
if (ch === "<" || ch === ">") {
chars[i] = "";
}
}
return createBidiText(chars.join(''), isLTR);
return createBidiText(chars.join(""), isLTR);
}
export {
bidi,
};
export { bidi };

View file

@ -25,10 +25,9 @@
* or -1 when EOF is reached.
*/
import { info } from '../shared/util';
import { info } from "../shared/util";
let CCITTFaxDecoder = (function CCITTFaxDecoder() {
const ccittEOL = -2;
const ccittEOF = -1;
const twoDimPass = 0;
@ -467,23 +466,23 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
* @param {Object} [options] - Decoding options.
*/
function CCITTFaxDecoder(source, options = {}) {
if (!source || typeof source.next !== 'function') {
if (!source || typeof source.next !== "function") {
throw new Error('CCITTFaxDecoder - invalid "source" parameter.');
}
this.source = source;
this.eof = false;
this.encoding = options['K'] || 0;
this.eoline = options['EndOfLine'] || false;
this.byteAlign = options['EncodedByteAlign'] || false;
this.columns = options['Columns'] || 1728;
this.rows = options['Rows'] || 0;
let eoblock = options['EndOfBlock'];
this.encoding = options["K"] || 0;
this.eoline = options["EndOfLine"] || false;
this.byteAlign = options["EncodedByteAlign"] || false;
this.columns = options["Columns"] || 1728;
this.rows = options["Rows"] || 0;
let eoblock = options["EndOfBlock"];
if (eoblock === null || eoblock === undefined) {
eoblock = true;
}
this.eoblock = eoblock;
this.black = options['BlackIs1'] || false;
this.black = options["BlackIs1"] || false;
this.codingLine = new Uint32Array(this.columns + 1);
this.refLine = new Uint32Array(this.columns + 2);
@ -556,27 +555,33 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
code1 = code2 = 0;
if (blackPixels) {
do {
code1 += (code3 = this._getBlackCode());
code1 += code3 = this._getBlackCode();
} while (code3 >= 64);
do {
code2 += (code3 = this._getWhiteCode());
code2 += code3 = this._getWhiteCode();
} while (code3 >= 64);
} else {
do {
code1 += (code3 = this._getWhiteCode());
code1 += code3 = this._getWhiteCode();
} while (code3 >= 64);
do {
code2 += (code3 = this._getBlackCode());
code2 += code3 = this._getBlackCode();
} while (code3 >= 64);
}
this._addPixels(codingLine[this.codingPos] +
code1, blackPixels);
this._addPixels(
codingLine[this.codingPos] + code1,
blackPixels
);
if (codingLine[this.codingPos] < columns) {
this._addPixels(codingLine[this.codingPos] + code2,
blackPixels ^ 1);
this._addPixels(
codingLine[this.codingPos] + code2,
blackPixels ^ 1
);
}
while (refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns) {
while (
refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns
) {
refPos += 2;
}
break;
@ -585,8 +590,10 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
blackPixels ^= 1;
if (codingLine[this.codingPos] < columns) {
++refPos;
while (refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns) {
while (
refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns
) {
refPos += 2;
}
}
@ -596,8 +603,10 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
blackPixels ^= 1;
if (codingLine[this.codingPos] < columns) {
++refPos;
while (refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns) {
while (
refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns
) {
refPos += 2;
}
}
@ -607,8 +616,10 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
blackPixels ^= 1;
if (codingLine[this.codingPos] < columns) {
++refPos;
while (refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns) {
while (
refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns
) {
refPos += 2;
}
}
@ -618,8 +629,10 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
blackPixels ^= 1;
if (codingLine[this.codingPos] < columns) {
++refPos;
while (refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns) {
while (
refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns
) {
refPos += 2;
}
}
@ -633,8 +646,10 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
} else {
++refPos;
}
while (refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns) {
while (
refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns
) {
refPos += 2;
}
}
@ -648,8 +663,10 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
} else {
++refPos;
}
while (refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns) {
while (
refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns
) {
refPos += 2;
}
}
@ -663,8 +680,10 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
} else {
++refPos;
}
while (refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns) {
while (
refLine[refPos] <= codingLine[this.codingPos] &&
refLine[refPos] < columns
) {
refPos += 2;
}
}
@ -674,7 +693,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
this.eof = true;
break;
default:
info('bad 2d code');
info("bad 2d code");
this._addPixels(columns, 0);
this.err = true;
}
@ -687,11 +706,11 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
code1 = 0;
if (blackPixels) {
do {
code1 += (code3 = this._getBlackCode());
code1 += code3 = this._getBlackCode();
} while (code3 >= 64);
} else {
do {
code1 += (code3 = this._getWhiteCode());
code1 += code3 = this._getWhiteCode();
} while (code3 >= 64);
}
this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
@ -745,7 +764,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
for (i = 0; i < 4; ++i) {
code1 = this._lookBits(12);
if (code1 !== 1) {
info('bad rtc code: ' + code1);
info("bad rtc code: " + code1);
}
this._eatBits(12);
if (this.encoding > 0) {
@ -763,7 +782,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
this.eof = true;
return -1;
}
if ((code1 >> 1) === 1) {
if (code1 >> 1 === 1) {
break;
}
this._eatBits(1);
@ -776,21 +795,21 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
}
if (codingLine[0] > 0) {
this.outputBits = codingLine[this.codingPos = 0];
this.outputBits = codingLine[(this.codingPos = 0)];
} else {
this.outputBits = codingLine[this.codingPos = 1];
this.outputBits = codingLine[(this.codingPos = 1)];
}
this.row++;
}
let c;
if (this.outputBits >= 8) {
c = (this.codingPos & 1) ? 0 : 0xFF;
c = this.codingPos & 1 ? 0 : 0xff;
this.outputBits -= 8;
if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
this.codingPos++;
this.outputBits = (codingLine[this.codingPos] -
codingLine[this.codingPos - 1]);
this.outputBits =
codingLine[this.codingPos] - codingLine[this.codingPos - 1];
}
} else {
bits = 8;
@ -799,21 +818,21 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
if (this.outputBits > bits) {
c <<= bits;
if (!(this.codingPos & 1)) {
c |= 0xFF >> (8 - bits);
c |= 0xff >> (8 - bits);
}
this.outputBits -= bits;
bits = 0;
} else {
c <<= this.outputBits;
if (!(this.codingPos & 1)) {
c |= 0xFF >> (8 - this.outputBits);
c |= 0xff >> (8 - this.outputBits);
}
bits -= this.outputBits;
this.outputBits = 0;
if (codingLine[this.codingPos] < columns) {
this.codingPos++;
this.outputBits = (codingLine[this.codingPos] -
codingLine[this.codingPos - 1]);
this.outputBits =
codingLine[this.codingPos] - codingLine[this.codingPos - 1];
} else if (bits > 0) {
c <<= bits;
bits = 0;
@ -822,7 +841,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
} while (bits);
}
if (this.black) {
c ^= 0xFF;
c ^= 0xff;
}
return c;
},
@ -836,7 +855,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
if (a1 > codingLine[codingPos]) {
if (a1 > this.columns) {
info('row is wrong length');
info("row is wrong length");
this.err = true;
a1 = this.columns;
}
@ -858,7 +877,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
if (a1 > codingLine[codingPos]) {
if (a1 > this.columns) {
info('row is wrong length');
info("row is wrong length");
this.err = true;
a1 = this.columns;
}
@ -869,7 +888,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
codingLine[codingPos] = a1;
} else if (a1 < codingLine[codingPos]) {
if (a1 < 0) {
info('invalid code');
info("invalid code");
this.err = true;
a1 = 0;
}
@ -931,7 +950,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
return result[1];
}
}
info('Bad two dim code');
info("Bad two dim code");
return ccittEOF;
},
@ -947,7 +966,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
return 1;
}
if ((code >> 5) === 0) {
if (code >> 5 === 0) {
p = whiteTable1[code];
} else {
p = whiteTable2[code >> 3];
@ -968,7 +987,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
return result[1];
}
}
info('bad white code');
info("bad white code");
this._eatBits(1);
return 1;
},
@ -983,9 +1002,9 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
if (code === ccittEOF) {
return 1;
}
if ((code >> 7) === 0) {
if (code >> 7 === 0) {
p = blackTable1[code];
} else if ((code >> 9) === 0 && (code >> 7) !== 0) {
} else if (code >> 9 === 0 && code >> 7 !== 0) {
p = blackTable2[(code >> 1) - 64];
} else {
p = blackTable3[code >> 7];
@ -1011,7 +1030,7 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
return result[1];
}
}
info('bad black code');
info("bad black code");
this._eatBits(1);
return 1;
},
@ -1026,13 +1045,12 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
if (this.inputBits === 0) {
return ccittEOF;
}
return ((this.inputBuf << (n - this.inputBits)) &
(0xFFFF >> (16 - n)));
return (this.inputBuf << (n - this.inputBits)) & (0xffff >> (16 - n));
}
this.inputBuf = (this.inputBuf << 8) | c;
this.inputBits += 8;
}
return (this.inputBuf >> (this.inputBits - n)) & (0xFFFF >> (16 - n));
return (this.inputBuf >> (this.inputBits - n)) & (0xffff >> (16 - n));
},
/**
@ -1048,6 +1066,4 @@ let CCITTFaxDecoder = (function CCITTFaxDecoder() {
return CCITTFaxDecoder;
})();
export {
CCITTFaxDecoder,
};
export { CCITTFaxDecoder };

View file

@ -13,9 +13,9 @@
* limitations under the License.
*/
import { Dict, isDict } from './primitives';
import { CCITTFaxDecoder } from './ccitt';
import { DecodeStream } from './stream';
import { Dict, isDict } from "./primitives";
import { CCITTFaxDecoder } from "./ccitt";
import { DecodeStream } from "./stream";
var CCITTFaxStream = (function CCITTFaxStreamClosure() {
function CCITTFaxStream(str, maybeLength, params) {
@ -32,13 +32,13 @@ var CCITTFaxStream = (function CCITTFaxStreamClosure() {
},
};
this.ccittFaxDecoder = new CCITTFaxDecoder(source, {
K: params.get('K'),
EndOfLine: params.get('EndOfLine'),
EncodedByteAlign: params.get('EncodedByteAlign'),
Columns: params.get('Columns'),
Rows: params.get('Rows'),
EndOfBlock: params.get('EndOfBlock'),
BlackIs1: params.get('BlackIs1'),
K: params.get("K"),
EndOfLine: params.get("EndOfLine"),
EncodedByteAlign: params.get("EncodedByteAlign"),
Columns: params.get("Columns"),
Rows: params.get("Rows"),
EndOfBlock: params.get("EndOfBlock"),
BlackIs1: params.get("BlackIs1"),
});
DecodeStream.call(this, maybeLength);
@ -61,6 +61,4 @@ var CCITTFaxStream = (function CCITTFaxStreamClosure() {
return CCITTFaxStream;
})();
export {
CCITTFaxStream,
};
export { CCITTFaxStream };

File diff suppressed because it is too large Load diff

View file

@ -16,109 +16,105 @@
// prettier-ignore
const ISOAdobeCharset = [
'.notdef', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar',
'percent', 'ampersand', 'quoteright', 'parenleft', 'parenright',
'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash', 'zero',
'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
'nine', 'colon', 'semicolon', 'less', 'equal', 'greater', 'question',
'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'bracketleft', 'backslash', 'bracketright', 'asciicircum', 'underscore',
'quoteleft', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'braceleft', 'bar', 'braceright', 'asciitilde', 'exclamdown', 'cent',
'sterling', 'fraction', 'yen', 'florin', 'section', 'currency',
'quotesingle', 'quotedblleft', 'guillemotleft', 'guilsinglleft',
'guilsinglright', 'fi', 'fl', 'endash', 'dagger', 'daggerdbl',
'periodcentered', 'paragraph', 'bullet', 'quotesinglbase',
'quotedblbase', 'quotedblright', 'guillemotright', 'ellipsis',
'perthousand', 'questiondown', 'grave', 'acute', 'circumflex', 'tilde',
'macron', 'breve', 'dotaccent', 'dieresis', 'ring', 'cedilla',
'hungarumlaut', 'ogonek', 'caron', 'emdash', 'AE', 'ordfeminine',
'Lslash', 'Oslash', 'OE', 'ordmasculine', 'ae', 'dotlessi', 'lslash',
'oslash', 'oe', 'germandbls', 'onesuperior', 'logicalnot', 'mu',
'trademark', 'Eth', 'onehalf', 'plusminus', 'Thorn', 'onequarter',
'divide', 'brokenbar', 'degree', 'thorn', 'threequarters', 'twosuperior',
'registered', 'minus', 'eth', 'multiply', 'threesuperior', 'copyright',
'Aacute', 'Acircumflex', 'Adieresis', 'Agrave', 'Aring', 'Atilde',
'Ccedilla', 'Eacute', 'Ecircumflex', 'Edieresis', 'Egrave', 'Iacute',
'Icircumflex', 'Idieresis', 'Igrave', 'Ntilde', 'Oacute', 'Ocircumflex',
'Odieresis', 'Ograve', 'Otilde', 'Scaron', 'Uacute', 'Ucircumflex',
'Udieresis', 'Ugrave', 'Yacute', 'Ydieresis', 'Zcaron', 'aacute',
'acircumflex', 'adieresis', 'agrave', 'aring', 'atilde', 'ccedilla',
'eacute', 'ecircumflex', 'edieresis', 'egrave', 'iacute', 'icircumflex',
'idieresis', 'igrave', 'ntilde', 'oacute', 'ocircumflex', 'odieresis',
'ograve', 'otilde', 'scaron', 'uacute', 'ucircumflex', 'udieresis',
'ugrave', 'yacute', 'ydieresis', 'zcaron'
".notdef", "space", "exclam", "quotedbl", "numbersign", "dollar",
"percent", "ampersand", "quoteright", "parenleft", "parenright",
"asterisk", "plus", "comma", "hyphen", "period", "slash", "zero",
"one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "colon", "semicolon", "less", "equal", "greater", "question",
"at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"bracketleft", "backslash", "bracketright", "asciicircum", "underscore",
"quoteleft", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"braceleft", "bar", "braceright", "asciitilde", "exclamdown", "cent",
"sterling", "fraction", "yen", "florin", "section", "currency",
"quotesingle", "quotedblleft", "guillemotleft", "guilsinglleft",
"guilsinglright", "fi", "fl", "endash", "dagger", "daggerdbl",
"periodcentered", "paragraph", "bullet", "quotesinglbase",
"quotedblbase", "quotedblright", "guillemotright", "ellipsis",
"perthousand", "questiondown", "grave", "acute", "circumflex", "tilde",
"macron", "breve", "dotaccent", "dieresis", "ring", "cedilla",
"hungarumlaut", "ogonek", "caron", "emdash", "AE", "ordfeminine",
"Lslash", "Oslash", "OE", "ordmasculine", "ae", "dotlessi", "lslash",
"oslash", "oe", "germandbls", "onesuperior", "logicalnot", "mu",
"trademark", "Eth", "onehalf", "plusminus", "Thorn", "onequarter",
"divide", "brokenbar", "degree", "thorn", "threequarters", "twosuperior",
"registered", "minus", "eth", "multiply", "threesuperior", "copyright",
"Aacute", "Acircumflex", "Adieresis", "Agrave", "Aring", "Atilde",
"Ccedilla", "Eacute", "Ecircumflex", "Edieresis", "Egrave", "Iacute",
"Icircumflex", "Idieresis", "Igrave", "Ntilde", "Oacute", "Ocircumflex",
"Odieresis", "Ograve", "Otilde", "Scaron", "Uacute", "Ucircumflex",
"Udieresis", "Ugrave", "Yacute", "Ydieresis", "Zcaron", "aacute",
"acircumflex", "adieresis", "agrave", "aring", "atilde", "ccedilla",
"eacute", "ecircumflex", "edieresis", "egrave", "iacute", "icircumflex",
"idieresis", "igrave", "ntilde", "oacute", "ocircumflex", "odieresis",
"ograve", "otilde", "scaron", "uacute", "ucircumflex", "udieresis",
"ugrave", "yacute", "ydieresis", "zcaron"
];
// prettier-ignore
const ExpertCharset = [
'.notdef', 'space', 'exclamsmall', 'Hungarumlautsmall', 'dollaroldstyle',
'dollarsuperior', 'ampersandsmall', 'Acutesmall', 'parenleftsuperior',
'parenrightsuperior', 'twodotenleader', 'onedotenleader', 'comma',
'hyphen', 'period', 'fraction', 'zerooldstyle', 'oneoldstyle',
'twooldstyle', 'threeoldstyle', 'fouroldstyle', 'fiveoldstyle',
'sixoldstyle', 'sevenoldstyle', 'eightoldstyle', 'nineoldstyle',
'colon', 'semicolon', 'commasuperior', 'threequartersemdash',
'periodsuperior', 'questionsmall', 'asuperior', 'bsuperior',
'centsuperior', 'dsuperior', 'esuperior', 'isuperior', 'lsuperior',
'msuperior', 'nsuperior', 'osuperior', 'rsuperior', 'ssuperior',
'tsuperior', 'ff', 'fi', 'fl', 'ffi', 'ffl', 'parenleftinferior',
'parenrightinferior', 'Circumflexsmall', 'hyphensuperior', 'Gravesmall',
'Asmall', 'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall',
'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall', 'Nsmall',
'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall', 'Tsmall', 'Usmall',
'Vsmall', 'Wsmall', 'Xsmall', 'Ysmall', 'Zsmall', 'colonmonetary',
'onefitted', 'rupiah', 'Tildesmall', 'exclamdownsmall', 'centoldstyle',
'Lslashsmall', 'Scaronsmall', 'Zcaronsmall', 'Dieresissmall',
'Brevesmall', 'Caronsmall', 'Dotaccentsmall', 'Macronsmall',
'figuredash', 'hypheninferior', 'Ogoneksmall', 'Ringsmall',
'Cedillasmall', 'onequarter', 'onehalf', 'threequarters',
'questiondownsmall', 'oneeighth', 'threeeighths', 'fiveeighths',
'seveneighths', 'onethird', 'twothirds', 'zerosuperior', 'onesuperior',
'twosuperior', 'threesuperior', 'foursuperior', 'fivesuperior',
'sixsuperior', 'sevensuperior', 'eightsuperior', 'ninesuperior',
'zeroinferior', 'oneinferior', 'twoinferior', 'threeinferior',
'fourinferior', 'fiveinferior', 'sixinferior', 'seveninferior',
'eightinferior', 'nineinferior', 'centinferior', 'dollarinferior',
'periodinferior', 'commainferior', 'Agravesmall', 'Aacutesmall',
'Acircumflexsmall', 'Atildesmall', 'Adieresissmall', 'Aringsmall',
'AEsmall', 'Ccedillasmall', 'Egravesmall', 'Eacutesmall',
'Ecircumflexsmall', 'Edieresissmall', 'Igravesmall', 'Iacutesmall',
'Icircumflexsmall', 'Idieresissmall', 'Ethsmall', 'Ntildesmall',
'Ogravesmall', 'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall',
'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall', 'Uacutesmall',
'Ucircumflexsmall', 'Udieresissmall', 'Yacutesmall', 'Thornsmall',
'Ydieresissmall'
".notdef", "space", "exclamsmall", "Hungarumlautsmall", "dollaroldstyle",
"dollarsuperior", "ampersandsmall", "Acutesmall", "parenleftsuperior",
"parenrightsuperior", "twodotenleader", "onedotenleader", "comma",
"hyphen", "period", "fraction", "zerooldstyle", "oneoldstyle",
"twooldstyle", "threeoldstyle", "fouroldstyle", "fiveoldstyle",
"sixoldstyle", "sevenoldstyle", "eightoldstyle", "nineoldstyle",
"colon", "semicolon", "commasuperior", "threequartersemdash",
"periodsuperior", "questionsmall", "asuperior", "bsuperior",
"centsuperior", "dsuperior", "esuperior", "isuperior", "lsuperior",
"msuperior", "nsuperior", "osuperior", "rsuperior", "ssuperior",
"tsuperior", "ff", "fi", "fl", "ffi", "ffl", "parenleftinferior",
"parenrightinferior", "Circumflexsmall", "hyphensuperior", "Gravesmall",
"Asmall", "Bsmall", "Csmall", "Dsmall", "Esmall", "Fsmall", "Gsmall",
"Hsmall", "Ismall", "Jsmall", "Ksmall", "Lsmall", "Msmall", "Nsmall",
"Osmall", "Psmall", "Qsmall", "Rsmall", "Ssmall", "Tsmall", "Usmall",
"Vsmall", "Wsmall", "Xsmall", "Ysmall", "Zsmall", "colonmonetary",
"onefitted", "rupiah", "Tildesmall", "exclamdownsmall", "centoldstyle",
"Lslashsmall", "Scaronsmall", "Zcaronsmall", "Dieresissmall",
"Brevesmall", "Caronsmall", "Dotaccentsmall", "Macronsmall",
"figuredash", "hypheninferior", "Ogoneksmall", "Ringsmall",
"Cedillasmall", "onequarter", "onehalf", "threequarters",
"questiondownsmall", "oneeighth", "threeeighths", "fiveeighths",
"seveneighths", "onethird", "twothirds", "zerosuperior", "onesuperior",
"twosuperior", "threesuperior", "foursuperior", "fivesuperior",
"sixsuperior", "sevensuperior", "eightsuperior", "ninesuperior",
"zeroinferior", "oneinferior", "twoinferior", "threeinferior",
"fourinferior", "fiveinferior", "sixinferior", "seveninferior",
"eightinferior", "nineinferior", "centinferior", "dollarinferior",
"periodinferior", "commainferior", "Agravesmall", "Aacutesmall",
"Acircumflexsmall", "Atildesmall", "Adieresissmall", "Aringsmall",
"AEsmall", "Ccedillasmall", "Egravesmall", "Eacutesmall",
"Ecircumflexsmall", "Edieresissmall", "Igravesmall", "Iacutesmall",
"Icircumflexsmall", "Idieresissmall", "Ethsmall", "Ntildesmall",
"Ogravesmall", "Oacutesmall", "Ocircumflexsmall", "Otildesmall",
"Odieresissmall", "OEsmall", "Oslashsmall", "Ugravesmall", "Uacutesmall",
"Ucircumflexsmall", "Udieresissmall", "Yacutesmall", "Thornsmall",
"Ydieresissmall"
];
// prettier-ignore
const ExpertSubsetCharset = [
'.notdef', 'space', 'dollaroldstyle', 'dollarsuperior',
'parenleftsuperior', 'parenrightsuperior', 'twodotenleader',
'onedotenleader', 'comma', 'hyphen', 'period', 'fraction',
'zerooldstyle', 'oneoldstyle', 'twooldstyle', 'threeoldstyle',
'fouroldstyle', 'fiveoldstyle', 'sixoldstyle', 'sevenoldstyle',
'eightoldstyle', 'nineoldstyle', 'colon', 'semicolon', 'commasuperior',
'threequartersemdash', 'periodsuperior', 'asuperior', 'bsuperior',
'centsuperior', 'dsuperior', 'esuperior', 'isuperior', 'lsuperior',
'msuperior', 'nsuperior', 'osuperior', 'rsuperior', 'ssuperior',
'tsuperior', 'ff', 'fi', 'fl', 'ffi', 'ffl', 'parenleftinferior',
'parenrightinferior', 'hyphensuperior', 'colonmonetary', 'onefitted',
'rupiah', 'centoldstyle', 'figuredash', 'hypheninferior', 'onequarter',
'onehalf', 'threequarters', 'oneeighth', 'threeeighths', 'fiveeighths',
'seveneighths', 'onethird', 'twothirds', 'zerosuperior', 'onesuperior',
'twosuperior', 'threesuperior', 'foursuperior', 'fivesuperior',
'sixsuperior', 'sevensuperior', 'eightsuperior', 'ninesuperior',
'zeroinferior', 'oneinferior', 'twoinferior', 'threeinferior',
'fourinferior', 'fiveinferior', 'sixinferior', 'seveninferior',
'eightinferior', 'nineinferior', 'centinferior', 'dollarinferior',
'periodinferior', 'commainferior'
".notdef", "space", "dollaroldstyle", "dollarsuperior",
"parenleftsuperior", "parenrightsuperior", "twodotenleader",
"onedotenleader", "comma", "hyphen", "period", "fraction",
"zerooldstyle", "oneoldstyle", "twooldstyle", "threeoldstyle",
"fouroldstyle", "fiveoldstyle", "sixoldstyle", "sevenoldstyle",
"eightoldstyle", "nineoldstyle", "colon", "semicolon", "commasuperior",
"threequartersemdash", "periodsuperior", "asuperior", "bsuperior",
"centsuperior", "dsuperior", "esuperior", "isuperior", "lsuperior",
"msuperior", "nsuperior", "osuperior", "rsuperior", "ssuperior",
"tsuperior", "ff", "fi", "fl", "ffi", "ffl", "parenleftinferior",
"parenrightinferior", "hyphensuperior", "colonmonetary", "onefitted",
"rupiah", "centoldstyle", "figuredash", "hypheninferior", "onequarter",
"onehalf", "threequarters", "oneeighth", "threeeighths", "fiveeighths",
"seveneighths", "onethird", "twothirds", "zerosuperior", "onesuperior",
"twosuperior", "threesuperior", "foursuperior", "fivesuperior",
"sixsuperior", "sevensuperior", "eightsuperior", "ninesuperior",
"zeroinferior", "oneinferior", "twoinferior", "threeinferior",
"fourinferior", "fiveinferior", "sixinferior", "seveninferior",
"eightinferior", "nineinferior", "centinferior", "dollarinferior",
"periodinferior", "commainferior"
];
export {
ISOAdobeCharset,
ExpertCharset,
ExpertSubsetCharset,
};
export { ISOAdobeCharset, ExpertCharset, ExpertSubsetCharset };

View file

@ -15,9 +15,12 @@
/* eslint no-var: error */
import {
arrayByteLength, arraysToBytes, createPromiseCapability, isEmptyObj
} from '../shared/util';
import { MissingDataException } from './core_utils';
arrayByteLength,
arraysToBytes,
createPromiseCapability,
isEmptyObj,
} from "../shared/util";
import { MissingDataException } from "./core_utils";
class ChunkedStream {
constructor(length, chunkSize, manager) {
@ -86,8 +89,10 @@ class ChunkedStream {
this.bytes.set(new Uint8Array(data), position);
position += data.byteLength;
this.progressiveDataLength = position;
const endChunk = position >= this.end ? this.numChunks :
Math.floor(position / this.chunkSize);
const endChunk =
position >= this.end
? this.numChunks
: Math.floor(position / this.chunkSize);
for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
if (!this.loadedChunks[curChunk]) {
@ -194,7 +199,7 @@ class ChunkedStream {
}
const subarray = bytes.subarray(pos, strEnd);
// `this.bytes` is always a `Uint8Array` here.
return (forceClamped ? new Uint8ClampedArray(subarray) : subarray);
return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
}
let end = pos + length;
@ -208,7 +213,7 @@ class ChunkedStream {
this.pos = end;
const subarray = bytes.subarray(pos, end);
// `this.bytes` is always a `Uint8Array` here.
return (forceClamped ? new Uint8ClampedArray(subarray) : subarray);
return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
}
peekByte() {
@ -332,16 +337,17 @@ class ChunkedStreamManager {
rangeReader.onProgress = this.onProgress.bind(this);
}
let chunks = [], loaded = 0;
let chunks = [],
loaded = 0;
const promise = new Promise((resolve, reject) => {
const readChunk = (chunk) => {
const readChunk = chunk => {
try {
if (!chunk.done) {
const data = chunk.value;
chunks.push(data);
loaded += arrayByteLength(data);
if (rangeReader.isStreamingSupported) {
this.onProgress({ loaded, });
this.onProgress({ loaded });
}
rangeReader.read().then(readChunk, reject);
return;
@ -355,11 +361,11 @@ class ChunkedStreamManager {
};
rangeReader.read().then(readChunk, reject);
});
promise.then((data) => {
promise.then(data => {
if (this.aborted) {
return; // Ignoring any data after abort.
}
this.onReceiveData({ chunk: data, begin, });
this.onReceiveData({ chunk: data, begin });
});
// TODO check errors
}
@ -470,13 +476,11 @@ class ChunkedStreamManager {
}
if (prevChunk >= 0 && prevChunk + 1 !== chunk) {
groupedChunks.push({ beginChunk,
endChunk: prevChunk + 1, });
groupedChunks.push({ beginChunk, endChunk: prevChunk + 1 });
beginChunk = chunk;
}
if (i + 1 === chunks.length) {
groupedChunks.push({ beginChunk,
endChunk: chunk + 1, });
groupedChunks.push({ beginChunk, endChunk: chunk + 1 });
}
prevChunk = chunk;
@ -485,7 +489,7 @@ class ChunkedStreamManager {
}
onProgress(args) {
this.msgHandler.send('DocProgress', {
this.msgHandler.send("DocProgress", {
loaded: this.stream.numChunksLoaded * this.chunkSize + args.loaded,
total: this.length,
});
@ -498,8 +502,10 @@ class ChunkedStreamManager {
const end = begin + chunk.byteLength;
const beginChunk = Math.floor(begin / this.chunkSize);
const endChunk = end < this.length ? Math.floor(end / this.chunkSize) :
Math.ceil(end / this.chunkSize);
const endChunk =
end < this.length
? Math.floor(end / this.chunkSize)
: Math.ceil(end / this.chunkSize);
if (isProgressive) {
this.stream.onReceiveProgressiveData(chunk);
@ -557,7 +563,7 @@ class ChunkedStreamManager {
capability.resolve();
}
this.msgHandler.send('DocProgress', {
this.msgHandler.send("DocProgress", {
loaded: this.stream.numChunksLoaded * this.chunkSize,
total: this.length,
});
@ -586,7 +592,4 @@ class ChunkedStreamManager {
}
}
export {
ChunkedStream,
ChunkedStreamManager,
};
export { ChunkedStream, ChunkedStreamManager };

View file

@ -14,184 +14,189 @@
*/
import {
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';
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 = [
// << Start unicode maps.
'Adobe-GB1-UCS2',
'Adobe-CNS1-UCS2',
'Adobe-Japan1-UCS2',
'Adobe-Korea1-UCS2',
// >> End unicode maps.
'78-EUC-H',
'78-EUC-V',
'78-H',
'78-RKSJ-H',
'78-RKSJ-V',
'78-V',
'78ms-RKSJ-H',
'78ms-RKSJ-V',
'83pv-RKSJ-H',
'90ms-RKSJ-H',
'90ms-RKSJ-V',
'90msp-RKSJ-H',
'90msp-RKSJ-V',
'90pv-RKSJ-H',
'90pv-RKSJ-V',
'Add-H',
'Add-RKSJ-H',
'Add-RKSJ-V',
'Add-V',
'Adobe-CNS1-0',
'Adobe-CNS1-1',
'Adobe-CNS1-2',
'Adobe-CNS1-3',
'Adobe-CNS1-4',
'Adobe-CNS1-5',
'Adobe-CNS1-6',
'Adobe-GB1-0',
'Adobe-GB1-1',
'Adobe-GB1-2',
'Adobe-GB1-3',
'Adobe-GB1-4',
'Adobe-GB1-5',
'Adobe-Japan1-0',
'Adobe-Japan1-1',
'Adobe-Japan1-2',
'Adobe-Japan1-3',
'Adobe-Japan1-4',
'Adobe-Japan1-5',
'Adobe-Japan1-6',
'Adobe-Korea1-0',
'Adobe-Korea1-1',
'Adobe-Korea1-2',
'B5-H',
'B5-V',
'B5pc-H',
'B5pc-V',
'CNS-EUC-H',
'CNS-EUC-V',
'CNS1-H',
'CNS1-V',
'CNS2-H',
'CNS2-V',
'ETHK-B5-H',
'ETHK-B5-V',
'ETen-B5-H',
'ETen-B5-V',
'ETenms-B5-H',
'ETenms-B5-V',
'EUC-H',
'EUC-V',
'Ext-H',
'Ext-RKSJ-H',
'Ext-RKSJ-V',
'Ext-V',
'GB-EUC-H',
'GB-EUC-V',
'GB-H',
'GB-V',
'GBK-EUC-H',
'GBK-EUC-V',
'GBK2K-H',
'GBK2K-V',
'GBKp-EUC-H',
'GBKp-EUC-V',
'GBT-EUC-H',
'GBT-EUC-V',
'GBT-H',
'GBT-V',
'GBTpc-EUC-H',
'GBTpc-EUC-V',
'GBpc-EUC-H',
'GBpc-EUC-V',
'H',
'HKdla-B5-H',
'HKdla-B5-V',
'HKdlb-B5-H',
'HKdlb-B5-V',
'HKgccs-B5-H',
'HKgccs-B5-V',
'HKm314-B5-H',
'HKm314-B5-V',
'HKm471-B5-H',
'HKm471-B5-V',
'HKscs-B5-H',
'HKscs-B5-V',
'Hankaku',
'Hiragana',
'KSC-EUC-H',
'KSC-EUC-V',
'KSC-H',
'KSC-Johab-H',
'KSC-Johab-V',
'KSC-V',
'KSCms-UHC-H',
'KSCms-UHC-HW-H',
'KSCms-UHC-HW-V',
'KSCms-UHC-V',
'KSCpc-EUC-H',
'KSCpc-EUC-V',
'Katakana',
'NWP-H',
'NWP-V',
'RKSJ-H',
'RKSJ-V',
'Roman',
'UniCNS-UCS2-H',
'UniCNS-UCS2-V',
'UniCNS-UTF16-H',
'UniCNS-UTF16-V',
'UniCNS-UTF32-H',
'UniCNS-UTF32-V',
'UniCNS-UTF8-H',
'UniCNS-UTF8-V',
'UniGB-UCS2-H',
'UniGB-UCS2-V',
'UniGB-UTF16-H',
'UniGB-UTF16-V',
'UniGB-UTF32-H',
'UniGB-UTF32-V',
'UniGB-UTF8-H',
'UniGB-UTF8-V',
'UniJIS-UCS2-H',
'UniJIS-UCS2-HW-H',
'UniJIS-UCS2-HW-V',
'UniJIS-UCS2-V',
'UniJIS-UTF16-H',
'UniJIS-UTF16-V',
'UniJIS-UTF32-H',
'UniJIS-UTF32-V',
'UniJIS-UTF8-H',
'UniJIS-UTF8-V',
'UniJIS2004-UTF16-H',
'UniJIS2004-UTF16-V',
'UniJIS2004-UTF32-H',
'UniJIS2004-UTF32-V',
'UniJIS2004-UTF8-H',
'UniJIS2004-UTF8-V',
'UniJISPro-UCS2-HW-V',
'UniJISPro-UCS2-V',
'UniJISPro-UTF8-V',
'UniJISX0213-UTF32-H',
'UniJISX0213-UTF32-V',
'UniJISX02132004-UTF32-H',
'UniJISX02132004-UTF32-V',
'UniKS-UCS2-H',
'UniKS-UCS2-V',
'UniKS-UTF16-H',
'UniKS-UTF16-V',
'UniKS-UTF32-H',
'UniKS-UTF32-V',
'UniKS-UTF8-H',
'UniKS-UTF8-V',
'V',
'WP-Symbol'];
// << Start unicode maps.
"Adobe-GB1-UCS2",
"Adobe-CNS1-UCS2",
"Adobe-Japan1-UCS2",
"Adobe-Korea1-UCS2",
// >> End unicode maps.
"78-EUC-H",
"78-EUC-V",
"78-H",
"78-RKSJ-H",
"78-RKSJ-V",
"78-V",
"78ms-RKSJ-H",
"78ms-RKSJ-V",
"83pv-RKSJ-H",
"90ms-RKSJ-H",
"90ms-RKSJ-V",
"90msp-RKSJ-H",
"90msp-RKSJ-V",
"90pv-RKSJ-H",
"90pv-RKSJ-V",
"Add-H",
"Add-RKSJ-H",
"Add-RKSJ-V",
"Add-V",
"Adobe-CNS1-0",
"Adobe-CNS1-1",
"Adobe-CNS1-2",
"Adobe-CNS1-3",
"Adobe-CNS1-4",
"Adobe-CNS1-5",
"Adobe-CNS1-6",
"Adobe-GB1-0",
"Adobe-GB1-1",
"Adobe-GB1-2",
"Adobe-GB1-3",
"Adobe-GB1-4",
"Adobe-GB1-5",
"Adobe-Japan1-0",
"Adobe-Japan1-1",
"Adobe-Japan1-2",
"Adobe-Japan1-3",
"Adobe-Japan1-4",
"Adobe-Japan1-5",
"Adobe-Japan1-6",
"Adobe-Korea1-0",
"Adobe-Korea1-1",
"Adobe-Korea1-2",
"B5-H",
"B5-V",
"B5pc-H",
"B5pc-V",
"CNS-EUC-H",
"CNS-EUC-V",
"CNS1-H",
"CNS1-V",
"CNS2-H",
"CNS2-V",
"ETHK-B5-H",
"ETHK-B5-V",
"ETen-B5-H",
"ETen-B5-V",
"ETenms-B5-H",
"ETenms-B5-V",
"EUC-H",
"EUC-V",
"Ext-H",
"Ext-RKSJ-H",
"Ext-RKSJ-V",
"Ext-V",
"GB-EUC-H",
"GB-EUC-V",
"GB-H",
"GB-V",
"GBK-EUC-H",
"GBK-EUC-V",
"GBK2K-H",
"GBK2K-V",
"GBKp-EUC-H",
"GBKp-EUC-V",
"GBT-EUC-H",
"GBT-EUC-V",
"GBT-H",
"GBT-V",
"GBTpc-EUC-H",
"GBTpc-EUC-V",
"GBpc-EUC-H",
"GBpc-EUC-V",
"H",
"HKdla-B5-H",
"HKdla-B5-V",
"HKdlb-B5-H",
"HKdlb-B5-V",
"HKgccs-B5-H",
"HKgccs-B5-V",
"HKm314-B5-H",
"HKm314-B5-V",
"HKm471-B5-H",
"HKm471-B5-V",
"HKscs-B5-H",
"HKscs-B5-V",
"Hankaku",
"Hiragana",
"KSC-EUC-H",
"KSC-EUC-V",
"KSC-H",
"KSC-Johab-H",
"KSC-Johab-V",
"KSC-V",
"KSCms-UHC-H",
"KSCms-UHC-HW-H",
"KSCms-UHC-HW-V",
"KSCms-UHC-V",
"KSCpc-EUC-H",
"KSCpc-EUC-V",
"Katakana",
"NWP-H",
"NWP-V",
"RKSJ-H",
"RKSJ-V",
"Roman",
"UniCNS-UCS2-H",
"UniCNS-UCS2-V",
"UniCNS-UTF16-H",
"UniCNS-UTF16-V",
"UniCNS-UTF32-H",
"UniCNS-UTF32-V",
"UniCNS-UTF8-H",
"UniCNS-UTF8-V",
"UniGB-UCS2-H",
"UniGB-UCS2-V",
"UniGB-UTF16-H",
"UniGB-UTF16-V",
"UniGB-UTF32-H",
"UniGB-UTF32-V",
"UniGB-UTF8-H",
"UniGB-UTF8-V",
"UniJIS-UCS2-H",
"UniJIS-UCS2-HW-H",
"UniJIS-UCS2-HW-V",
"UniJIS-UCS2-V",
"UniJIS-UTF16-H",
"UniJIS-UTF16-V",
"UniJIS-UTF32-H",
"UniJIS-UTF32-V",
"UniJIS-UTF8-H",
"UniJIS-UTF8-V",
"UniJIS2004-UTF16-H",
"UniJIS2004-UTF16-V",
"UniJIS2004-UTF32-H",
"UniJIS2004-UTF32-V",
"UniJIS2004-UTF8-H",
"UniJIS2004-UTF8-V",
"UniJISPro-UCS2-HW-V",
"UniJISPro-UCS2-V",
"UniJISPro-UTF8-V",
"UniJISX0213-UTF32-H",
"UniJISX0213-UTF32-V",
"UniJISX02132004-UTF32-H",
"UniJISX02132004-UTF32-V",
"UniKS-UCS2-H",
"UniKS-UCS2-V",
"UniKS-UTF16-H",
"UniKS-UTF16-V",
"UniKS-UTF32-H",
"UniKS-UTF32-V",
"UniKS-UTF8-H",
"UniKS-UTF8-V",
"V",
"WP-Symbol",
];
// CMap, not to be confused with TrueType's cmap.
class CMap {
@ -206,7 +211,7 @@ class CMap {
// - bf chars are variable-length byte sequences, stored as strings, with
// one byte per character.
this._map = [];
this.name = '';
this.name = "";
this.vertical = false;
this.useCMap = null;
this.builtInCMap = builtInCMap;
@ -228,13 +233,15 @@ class CMap {
while (low <= high) {
this._map[low++] = dstLow;
// Only the last byte has to be incremented.
dstLow = dstLow.substring(0, lastByte) +
String.fromCharCode(dstLow.charCodeAt(lastByte) + 1);
dstLow =
dstLow.substring(0, lastByte) +
String.fromCharCode(dstLow.charCodeAt(lastByte) + 1);
}
}
mapBfRangeToArray(low, high, array) {
let i = 0, ii = array.length;
let i = 0,
ii = array.length;
while (low <= high && i < ii) {
this._map[low] = array[i++];
++low;
@ -284,7 +291,7 @@ class CMap {
}
for (let charCode in map) {
if (map[charCode] === value) {
return (charCode | 0);
return charCode | 0;
}
}
return -1;
@ -303,7 +310,7 @@ class CMap {
c = ((c << 8) | str.charCodeAt(offset + n)) >>> 0;
// Check each codespace range to see if it falls within.
const codespaceRange = codespaceRanges[n];
for (let k = 0, kk = codespaceRange.length; k < kk;) {
for (let k = 0, kk = codespaceRange.length; k < kk; ) {
const low = codespaceRange[k++];
const high = codespaceRange[k++];
if (c >= low && c <= high) {
@ -322,7 +329,7 @@ class CMap {
}
get isIdentityCMap() {
if (!(this.name === 'Identity-H' || this.name === 'Identity-V')) {
if (!(this.name === "Identity-H" || this.name === "Identity-V")) {
return false;
}
if (this._map.length !== 0x10000) {
@ -348,23 +355,23 @@ class IdentityCMap extends CMap {
}
mapCidRange(low, high, dstLow) {
unreachable('should not call mapCidRange');
unreachable("should not call mapCidRange");
}
mapBfRange(low, high, dstLow) {
unreachable('should not call mapBfRange');
unreachable("should not call mapBfRange");
}
mapBfRangeToArray(low, high, array) {
unreachable('should not call mapBfRangeToArray');
unreachable("should not call mapBfRangeToArray");
}
mapOne(src, dst) {
unreachable('should not call mapCidOne');
unreachable("should not call mapCidOne");
}
lookup(code) {
return (Number.isInteger(code) && code <= 0xffff) ? code : undefined;
return Number.isInteger(code) && code <= 0xffff ? code : undefined;
}
contains(code) {
@ -378,7 +385,7 @@ class IdentityCMap extends CMap {
}
charCodeOf(value) {
return (Number.isInteger(value) && value <= 0xffff) ? value : -1;
return Number.isInteger(value) && value <= 0xffff ? value : -1;
}
getMap() {
@ -394,8 +401,9 @@ class IdentityCMap extends CMap {
return 0x10000;
}
get isIdentityCMap() { // eslint-disable-line getter-return
unreachable('should not access .isIdentityCMap');
get isIdentityCMap() {
// eslint-disable-line getter-return
unreachable("should not access .isIdentityCMap");
}
}
@ -461,34 +469,36 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
do {
var b = this.readByte();
if (b < 0) {
throw new FormatError('unexpected EOF in bcmap');
throw new FormatError("unexpected EOF in bcmap");
}
last = !(b & 0x80);
n = (n << 7) | (b & 0x7F);
n = (n << 7) | (b & 0x7f);
} while (!last);
return n;
},
readSigned() {
var n = this.readNumber();
return (n & 1) ? ~(n >>> 1) : n >>> 1;
return n & 1 ? ~(n >>> 1) : n >>> 1;
},
readHex(num, size) {
num.set(this.buffer.subarray(this.pos,
this.pos + size + 1));
num.set(this.buffer.subarray(this.pos, this.pos + size + 1));
this.pos += size + 1;
},
readHexNumber(num, size) {
var last;
var stack = this.tmpBuf, sp = 0;
var stack = this.tmpBuf,
sp = 0;
do {
var b = this.readByte();
if (b < 0) {
throw new FormatError('unexpected EOF in bcmap');
throw new FormatError("unexpected EOF in bcmap");
}
last = !(b & 0x80);
stack[sp++] = b & 0x7F;
stack[sp++] = b & 0x7f;
} while (!last);
var i = size, buffer = 0, bufferSize = 0;
var i = size,
buffer = 0,
bufferSize = 0;
while (i >= 0) {
while (bufferSize < 8 && stack.length > 0) {
buffer = (stack[--sp] << bufferSize) | buffer;
@ -511,7 +521,7 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
},
readString() {
var len = this.readNumber();
var s = '';
var s = "";
for (var i = 0; i < len; i++) {
s += String.fromCharCode(this.readNumber());
}
@ -520,7 +530,7 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
};
function processBinaryCMap(data, cMap, extend) {
return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
var stream = new BinaryCMapStream(data);
var header = stream.readByte();
cMap.vertical = !!(header & 1);
@ -536,8 +546,9 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
var b;
while ((b = stream.readByte()) >= 0) {
var type = b >> 5;
if (type === 7) { // metadata, e.g. comment or usecmap
switch (b & 0x1F) {
if (type === 7) {
// metadata, e.g. comment or usecmap
switch (b & 0x1f) {
case 0:
stream.readString(); // skipping comment
break;
@ -551,7 +562,7 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
var dataSize = b & 15;
if (dataSize + 1 > MAX_NUM_SIZE) {
throw new Error('processBinaryCMap: Invalid dataSize.');
throw new Error("processBinaryCMap: Invalid dataSize.");
}
var ucs2DataSize = 1;
@ -562,16 +573,22 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
stream.readHex(start, dataSize);
stream.readHexNumber(end, dataSize);
addHex(end, start, dataSize);
cMap.addCodespaceRange(dataSize + 1, hexToInt(start, dataSize),
hexToInt(end, dataSize));
cMap.addCodespaceRange(
dataSize + 1,
hexToInt(start, dataSize),
hexToInt(end, dataSize)
);
for (i = 1; i < subitemsCount; i++) {
incHex(end, dataSize);
stream.readHexNumber(start, dataSize);
addHex(start, end, dataSize);
stream.readHexNumber(end, dataSize);
addHex(end, start, dataSize);
cMap.addCodespaceRange(dataSize + 1, hexToInt(start, dataSize),
hexToInt(end, dataSize));
cMap.addCodespaceRange(
dataSize + 1,
hexToInt(start, dataSize),
hexToInt(end, dataSize)
);
}
break;
case 1: // notdefrange
@ -609,8 +626,11 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
stream.readHexNumber(end, dataSize);
addHex(end, start, dataSize);
code = stream.readNumber();
cMap.mapCidRange(hexToInt(start, dataSize), hexToInt(end, dataSize),
code);
cMap.mapCidRange(
hexToInt(start, dataSize),
hexToInt(end, dataSize),
code
);
for (i = 1; i < subitemsCount; i++) {
incHex(end, dataSize);
if (!sequence) {
@ -622,15 +642,20 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
stream.readHexNumber(end, dataSize);
addHex(end, start, dataSize);
code = stream.readNumber();
cMap.mapCidRange(hexToInt(start, dataSize),
hexToInt(end, dataSize), code);
cMap.mapCidRange(
hexToInt(start, dataSize),
hexToInt(end, dataSize),
code
);
}
break;
case 4: // bfchar
stream.readHex(char, ucs2DataSize);
stream.readHex(charCode, dataSize);
cMap.mapOne(hexToInt(char, ucs2DataSize),
hexToStr(charCode, dataSize));
cMap.mapOne(
hexToInt(char, ucs2DataSize),
hexToStr(charCode, dataSize)
);
for (i = 1; i < subitemsCount; i++) {
incHex(char, ucs2DataSize);
if (!sequence) {
@ -640,8 +665,10 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
incHex(charCode, dataSize);
stream.readHexSigned(tmp, dataSize);
addHex(charCode, tmp, dataSize);
cMap.mapOne(hexToInt(char, ucs2DataSize),
hexToStr(charCode, dataSize));
cMap.mapOne(
hexToInt(char, ucs2DataSize),
hexToStr(charCode, dataSize)
);
}
break;
case 5: // bfrange
@ -649,9 +676,11 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
stream.readHexNumber(end, ucs2DataSize);
addHex(end, start, ucs2DataSize);
stream.readHex(charCode, dataSize);
cMap.mapBfRange(hexToInt(start, ucs2DataSize),
hexToInt(end, ucs2DataSize),
hexToStr(charCode, dataSize));
cMap.mapBfRange(
hexToInt(start, ucs2DataSize),
hexToInt(end, ucs2DataSize),
hexToStr(charCode, dataSize)
);
for (i = 1; i < subitemsCount; i++) {
incHex(end, ucs2DataSize);
if (!sequence) {
@ -663,13 +692,15 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
stream.readHexNumber(end, ucs2DataSize);
addHex(end, start, ucs2DataSize);
stream.readHex(charCode, dataSize);
cMap.mapBfRange(hexToInt(start, ucs2DataSize),
hexToInt(end, ucs2DataSize),
hexToStr(charCode, dataSize));
cMap.mapBfRange(
hexToInt(start, ucs2DataSize),
hexToInt(end, ucs2DataSize),
hexToStr(charCode, dataSize)
);
}
break;
default:
reject(new Error('processBinaryCMap: Unknown type: ' + type));
reject(new Error("processBinaryCMap: Unknown type: " + type));
return;
}
}
@ -702,13 +733,13 @@ var CMapFactory = (function CMapFactoryClosure() {
function expectString(obj) {
if (!isString(obj)) {
throw new FormatError('Malformed CMap: expected string.');
throw new FormatError("Malformed CMap: expected string.");
}
}
function expectInt(obj) {
if (!Number.isInteger(obj)) {
throw new FormatError('Malformed CMap: expected int.');
throw new FormatError("Malformed CMap: expected int.");
}
}
@ -718,7 +749,7 @@ var CMapFactory = (function CMapFactoryClosure() {
if (isEOF(obj)) {
break;
}
if (isCmd(obj, 'endbfchar')) {
if (isCmd(obj, "endbfchar")) {
return;
}
expectString(obj);
@ -737,7 +768,7 @@ var CMapFactory = (function CMapFactoryClosure() {
if (isEOF(obj)) {
break;
}
if (isCmd(obj, 'endbfrange')) {
if (isCmd(obj, "endbfrange")) {
return;
}
expectString(obj);
@ -749,10 +780,10 @@ var CMapFactory = (function CMapFactoryClosure() {
if (Number.isInteger(obj) || isString(obj)) {
var dstLow = Number.isInteger(obj) ? String.fromCharCode(obj) : obj;
cMap.mapBfRange(low, high, dstLow);
} else if (isCmd(obj, '[')) {
} else if (isCmd(obj, "[")) {
obj = lexer.getObj();
var array = [];
while (!isCmd(obj, ']') && !isEOF(obj)) {
while (!isCmd(obj, "]") && !isEOF(obj)) {
array.push(obj);
obj = lexer.getObj();
}
@ -761,7 +792,7 @@ var CMapFactory = (function CMapFactoryClosure() {
break;
}
}
throw new FormatError('Invalid bf range.');
throw new FormatError("Invalid bf range.");
}
function parseCidChar(cMap, lexer) {
@ -770,7 +801,7 @@ var CMapFactory = (function CMapFactoryClosure() {
if (isEOF(obj)) {
break;
}
if (isCmd(obj, 'endcidchar')) {
if (isCmd(obj, "endcidchar")) {
return;
}
expectString(obj);
@ -788,7 +819,7 @@ var CMapFactory = (function CMapFactoryClosure() {
if (isEOF(obj)) {
break;
}
if (isCmd(obj, 'endcidrange')) {
if (isCmd(obj, "endcidrange")) {
return;
}
expectString(obj);
@ -809,7 +840,7 @@ var CMapFactory = (function CMapFactoryClosure() {
if (isEOF(obj)) {
break;
}
if (isCmd(obj, 'endcodespacerange')) {
if (isCmd(obj, "endcodespacerange")) {
return;
}
if (!isString(obj)) {
@ -823,7 +854,7 @@ var CMapFactory = (function CMapFactoryClosure() {
var high = strToInt(obj);
cMap.addCodespaceRange(obj.length, low, high);
}
throw new FormatError('Invalid codespace range.');
throw new FormatError("Invalid codespace range.");
}
function parseWMode(cMap, lexer) {
@ -849,34 +880,34 @@ var CMapFactory = (function CMapFactoryClosure() {
if (isEOF(obj)) {
break;
} else if (isName(obj)) {
if (obj.name === 'WMode') {
if (obj.name === "WMode") {
parseWMode(cMap, lexer);
} else if (obj.name === 'CMapName') {
} else if (obj.name === "CMapName") {
parseCMapName(cMap, lexer);
}
previous = obj;
} else if (isCmd(obj)) {
switch (obj.cmd) {
case 'endcmap':
case "endcmap":
break objLoop;
case 'usecmap':
case "usecmap":
if (isName(previous)) {
embeddedUseCMap = previous.name;
}
break;
case 'begincodespacerange':
case "begincodespacerange":
parseCodespaceRange(cMap, lexer);
break;
case 'beginbfchar':
case "beginbfchar":
parseBfChar(cMap, lexer);
break;
case 'begincidchar':
case "begincidchar":
parseCidChar(cMap, lexer);
break;
case 'beginbfrange':
case "beginbfrange":
parseBfRange(cMap, lexer);
break;
case 'begincidrange':
case "begincidrange":
parseCidRange(cMap, lexer);
break;
}
@ -885,7 +916,7 @@ var CMapFactory = (function CMapFactoryClosure() {
if (ex instanceof MissingDataException) {
throw ex;
}
warn('Invalid cMap data: ' + ex);
warn("Invalid cMap data: " + ex);
continue;
}
}
@ -926,26 +957,29 @@ var CMapFactory = (function CMapFactoryClosure() {
}
function createBuiltInCMap(name, fetchBuiltInCMap) {
if (name === 'Identity-H') {
if (name === "Identity-H") {
return Promise.resolve(new IdentityCMap(false, 2));
} else if (name === 'Identity-V') {
} else if (name === "Identity-V") {
return Promise.resolve(new IdentityCMap(true, 2));
}
if (!BUILT_IN_CMAPS.includes(name)) {
return Promise.reject(new Error('Unknown CMap name: ' + name));
return Promise.reject(new Error("Unknown CMap name: " + name));
}
if (!fetchBuiltInCMap) {
return Promise.reject(new Error(
'Built-in CMap parameters are not provided.'));
return Promise.reject(
new Error("Built-in CMap parameters are not provided.")
);
}
return fetchBuiltInCMap(name).then(function (data) {
var cMapData = data.cMapData, compressionType = data.compressionType;
return fetchBuiltInCMap(name).then(function(data) {
var cMapData = data.cMapData,
compressionType = data.compressionType;
var cMap = new CMap(true);
if (compressionType === CMapCompressionType.BINARY) {
return new BinaryCMapReader().process(cMapData, cMap,
function (useCMap) {
return new BinaryCMapReader().process(cMapData, cMap, function(
useCMap
) {
return extendCMap(cMap, fetchBuiltInCMap, useCMap);
});
}
@ -953,8 +987,11 @@ var CMapFactory = (function CMapFactoryClosure() {
var lexer = new Lexer(new Stream(cMapData));
return parseCMap(cMap, lexer, fetchBuiltInCMap, null);
}
return Promise.reject(new Error(
'TODO: Only BINARY/NONE CMap compression is currently supported.'));
return Promise.reject(
new Error(
"TODO: Only BINARY/NONE CMap compression is currently supported."
)
);
});
}
@ -969,21 +1006,18 @@ var CMapFactory = (function CMapFactoryClosure() {
} else if (isStream(encoding)) {
var cMap = new CMap();
var lexer = new Lexer(encoding);
return parseCMap(cMap, lexer, fetchBuiltInCMap, useCMap).then(
function (parsedCMap) {
return parseCMap(cMap, lexer, fetchBuiltInCMap, useCMap).then(function(
parsedCMap
) {
if (parsedCMap.isIdentityCMap) {
return createBuiltInCMap(parsedCMap.name, fetchBuiltInCMap);
}
return parsedCMap;
});
}
return Promise.reject(new Error('Encoding required.'));
return Promise.reject(new Error("Encoding required."));
},
};
})();
export {
CMap,
IdentityCMap,
CMapFactory,
};
export { CMap, IdentityCMap, CMapFactory };

File diff suppressed because it is too large Load diff

View file

@ -14,7 +14,7 @@
*/
/* eslint no-var: error */
import { assert, BaseException, warn } from '../shared/util';
import { assert, BaseException, warn } from "../shared/util";
function getLookupTableFactory(initializer) {
let lookup;
@ -36,9 +36,9 @@ class MissingDataException extends BaseException {
}
}
class XRefEntryException extends BaseException { }
class XRefEntryException extends BaseException {}
class XRefParseException extends BaseException { }
class XRefParseException extends BaseException {}
/**
* Get the value of an inheritable property.
@ -61,8 +61,12 @@ class XRefParseException extends BaseException { }
* 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, }) {
function getInheritableProperty({
dict,
key,
getArray = false,
stopWhenFound = true,
}) {
const LOOP_LIMIT = 100;
let loopCount = 0;
let values;
@ -82,16 +86,16 @@ function getInheritableProperty({ dict, key, getArray = false,
warn(`getInheritableProperty: maximum loop count exceeded for "${key}"`);
break;
}
dict = dict.get('Parent');
dict = dict.get("Parent");
}
return values;
}
// prettier-ignore
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'
"", "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"
];
/**
@ -102,13 +106,16 @@ const ROMAN_NUMBER_MAP = [
* @returns {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 = [];
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');
romanBuf.push("M");
}
// Hundreds
pos = (number / 100) | 0;
@ -121,8 +128,8 @@ function toRomanNumerals(number, lowerCase = false) {
// Ones
romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
const romanStr = romanBuf.join('');
return (lowerCase ? romanStr.toLowerCase() : romanStr);
const romanStr = romanBuf.join("");
return lowerCase ? romanStr.toLowerCase() : romanStr;
}
export {

File diff suppressed because it is too large Load diff

View file

@ -15,35 +15,60 @@
/* eslint no-var: error */
import {
assert, FormatError, info, InvalidPDFException, isArrayBuffer, isArrayEqual,
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';
assert,
FormatError,
info,
InvalidPDFException,
isArrayBuffer,
isArrayEqual,
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';
import { Linearization } from './parser';
import { OperatorList } from './operator_list';
import { PartialEvaluator } from './evaluator';
import { PDFFunctionFactory } from './function';
getInheritableProperty,
MissingDataException,
XRefEntryException,
XRefParseException,
} from "./core_utils";
import { NullStream, Stream, StreamsSequenceStream } from "./stream";
import { AnnotationFactory } from "./annotation";
import { calculateMD5 } from "./crypto";
import { Linearization } from "./parser";
import { OperatorList } from "./operator_list";
import { PartialEvaluator } from "./evaluator";
import { PDFFunctionFactory } from "./function";
const DEFAULT_USER_UNIT = 1.0;
const LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
function isAnnotationRenderable(annotation, intent) {
return (intent === 'display' && annotation.viewable) ||
(intent === 'print' && annotation.printable);
return (
(intent === "display" && annotation.viewable) ||
(intent === "print" && annotation.printable)
);
}
class Page {
constructor({ pdfManager, xref, pageIndex, pageDict, ref, fontCache,
builtInCMapCache, pdfFunctionFactory, }) {
constructor({
pdfManager,
xref,
pageIndex,
pageDict,
ref,
fontCache,
builtInCMapCache,
pdfFunctionFactory,
}) {
this.pdfManager = pdfManager;
this.pageIndex = pageIndex;
this.pageDict = pageDict;
@ -72,8 +97,12 @@ class Page {
* @private
*/
_getInheritableProperty(key, getArray = false) {
const value = getInheritableProperty({ dict: this.pageDict, key, getArray,
stopWhenFound: false, });
const value = getInheritableProperty({
dict: this.pageDict,
key,
getArray,
stopWhenFound: false,
});
if (!Array.isArray(value)) {
return value;
}
@ -84,22 +113,25 @@ class Page {
}
get content() {
return this.pageDict.get('Contents');
return this.pageDict.get("Contents");
}
get resources() {
// For robustness: The spec states that a \Resources entry has to be
// present, but can be empty. Some documents still omit it; in this case
// we return an empty dictionary.
return shadow(this, 'resources',
this._getInheritableProperty('Resources') || Dict.empty);
return shadow(
this,
"resources",
this._getInheritableProperty("Resources") || Dict.empty
);
}
_getBoundingBox(name) {
const box = this._getInheritableProperty(name, /* getArray = */ true);
if (Array.isArray(box) && box.length === 4) {
if ((box[2] - box[0]) !== 0 && (box[3] - box[1]) !== 0) {
if (box[2] - box[0] !== 0 && box[3] - box[1] !== 0) {
return box;
}
warn(`Empty /${name} entry.`);
@ -109,22 +141,28 @@ class Page {
get mediaBox() {
// Reset invalid media box to letter size.
return shadow(this, 'mediaBox',
this._getBoundingBox('MediaBox') || LETTER_SIZE_MEDIABOX);
return shadow(
this,
"mediaBox",
this._getBoundingBox("MediaBox") || LETTER_SIZE_MEDIABOX
);
}
get cropBox() {
// Reset invalid crop box to media box.
return shadow(this, 'cropBox',
this._getBoundingBox('CropBox') || this.mediaBox);
return shadow(
this,
"cropBox",
this._getBoundingBox("CropBox") || this.mediaBox
);
}
get userUnit() {
let obj = this.pageDict.get('UserUnit');
let obj = this.pageDict.get("UserUnit");
if (!isNum(obj) || obj <= 0) {
obj = DEFAULT_USER_UNIT;
}
return shadow(this, 'userUnit', obj);
return shadow(this, "userUnit", obj);
}
get view() {
@ -132,23 +170,23 @@ class Page {
// "The crop, bleed, trim, and art boxes should not ordinarily
// extend beyond the boundaries of the media box. If they do, they are
// effectively reduced to their intersection with the media box."
const { cropBox, mediaBox, } = this;
const { cropBox, mediaBox } = this;
let view;
if (cropBox === mediaBox || isArrayEqual(cropBox, mediaBox)) {
view = mediaBox;
} else {
const box = Util.intersect(cropBox, mediaBox);
if (box && ((box[2] - box[0]) !== 0 && (box[3] - box[1]) !== 0)) {
if (box && box[2] - box[0] !== 0 && box[3] - box[1] !== 0) {
view = box;
} else {
warn('Empty /CropBox and /MediaBox intersection.');
warn("Empty /CropBox and /MediaBox intersection.");
}
}
return shadow(this, 'view', view || mediaBox);
return shadow(this, "view", view || mediaBox);
}
get rotate() {
let rotate = this._getInheritableProperty('Rotate') || 0;
let rotate = this._getInheritableProperty("Rotate") || 0;
// Normalize rotation so it's a multiple of 90 and between 0 and 270.
if (rotate % 90 !== 0) {
@ -160,7 +198,7 @@ class Page {
// rotation. The following is the other implementation of modulo.
rotate = ((rotate % 360) + 360) % 360;
}
return shadow(this, 'rotate', rotate);
return shadow(this, "rotate", rotate);
}
getContentStream() {
@ -187,7 +225,7 @@ class Page {
loadResources(keys) {
if (!this.resourcesPromise) {
// TODO: add async `_getInheritableProperty` and remove this.
this.resourcesPromise = this.pdfManager.ensure(this, 'resources');
this.resourcesPromise = this.pdfManager.ensure(this, "resources");
}
return this.resourcesPromise.then(() => {
const objectLoader = new ObjectLoader(this.resources, keys, this.xref);
@ -195,16 +233,18 @@ class Page {
});
}
getOperatorList({ handler, sink, task, intent, renderInteractiveForms, }) {
const contentStreamPromise = this.pdfManager.ensure(this,
'getContentStream');
getOperatorList({ handler, sink, task, intent, renderInteractiveForms }) {
const contentStreamPromise = this.pdfManager.ensure(
this,
"getContentStream"
);
const resourcesPromise = this.loadResources([
'ExtGState',
'ColorSpace',
'Pattern',
'Shading',
'XObject',
'Font',
"ExtGState",
"ColorSpace",
"Pattern",
"Shading",
"XObject",
"Font",
]);
const partialEvaluator = new PartialEvaluator({
@ -222,61 +262,76 @@ class Page {
const pageListPromise = dataPromises.then(([contentStream]) => {
const opList = new OperatorList(intent, sink, this.pageIndex);
handler.send('StartRenderPage', {
handler.send("StartRenderPage", {
transparency: partialEvaluator.hasBlendModes(this.resources),
pageIndex: this.pageIndex,
intent,
});
return partialEvaluator.getOperatorList({
stream: contentStream,
task,
resources: this.resources,
operatorList: opList,
}).then(function() {
return opList;
});
return partialEvaluator
.getOperatorList({
stream: contentStream,
task,
resources: this.resources,
operatorList: opList,
})
.then(function() {
return opList;
});
});
// Fetch the page's annotations and add their operator lists to the
// page's operator list to render them.
return Promise.all([pageListPromise, this._parsedAnnotations]).then(
function([pageOpList, annotations]) {
if (annotations.length === 0) {
pageOpList.flush(true);
return { length: pageOpList.totalLength, };
}
// Collect the operator list promises for the annotations. Each promise
// is resolved with the complete operator list for a single annotation.
const opListPromises = [];
for (const annotation of annotations) {
if (isAnnotationRenderable(annotation, intent)) {
opListPromises.push(annotation.getOperatorList(
partialEvaluator, task, renderInteractiveForms));
function([pageOpList, annotations]) {
if (annotations.length === 0) {
pageOpList.flush(true);
return { length: pageOpList.totalLength };
}
}
return Promise.all(opListPromises).then(function(opLists) {
pageOpList.addOp(OPS.beginAnnotations, []);
for (const opList of opLists) {
pageOpList.addOpList(opList);
// Collect the operator list promises for the annotations. Each promise
// is resolved with the complete operator list for a single annotation.
const opListPromises = [];
for (const annotation of annotations) {
if (isAnnotationRenderable(annotation, intent)) {
opListPromises.push(
annotation.getOperatorList(
partialEvaluator,
task,
renderInteractiveForms
)
);
}
}
pageOpList.addOp(OPS.endAnnotations, []);
pageOpList.flush(true);
return { length: pageOpList.totalLength, };
});
});
return Promise.all(opListPromises).then(function(opLists) {
pageOpList.addOp(OPS.beginAnnotations, []);
for (const opList of opLists) {
pageOpList.addOpList(opList);
}
pageOpList.addOp(OPS.endAnnotations, []);
pageOpList.flush(true);
return { length: pageOpList.totalLength };
});
}
);
}
extractTextContent({ handler, task, normalizeWhitespace, sink,
combineTextItems, }) {
const contentStreamPromise = this.pdfManager.ensure(this,
'getContentStream');
extractTextContent({
handler,
task,
normalizeWhitespace,
sink,
combineTextItems,
}) {
const contentStreamPromise = this.pdfManager.ensure(
this,
"getContentStream"
);
const resourcesPromise = this.loadResources([
'ExtGState',
'XObject',
'Font',
"ExtGState",
"XObject",
"Font",
]);
const dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
@ -316,47 +371,62 @@ class Page {
}
get annotations() {
return shadow(this, 'annotations',
this._getInheritableProperty('Annots') || []);
return shadow(
this,
"annotations",
this._getInheritableProperty("Annots") || []
);
}
get _parsedAnnotations() {
const parsedAnnotations =
this.pdfManager.ensure(this, 'annotations').then(() => {
const parsedAnnotations = this.pdfManager
.ensure(this, "annotations")
.then(() => {
const annotationRefs = this.annotations;
const annotationPromises = [];
for (let i = 0, ii = annotationRefs.length; i < ii; i++) {
annotationPromises.push(AnnotationFactory.create(
this.xref, annotationRefs[i], this.pdfManager, this.idFactory));
annotationPromises.push(
AnnotationFactory.create(
this.xref,
annotationRefs[i],
this.pdfManager,
this.idFactory
)
);
}
return Promise.all(annotationPromises).then(function(annotations) {
return annotations.filter(function isDefined(annotation) {
return !!annotation;
});
}, function(reason) {
warn(`_parsedAnnotations: "${reason}".`);
return [];
});
return Promise.all(annotationPromises).then(
function(annotations) {
return annotations.filter(function isDefined(annotation) {
return !!annotation;
});
},
function(reason) {
warn(`_parsedAnnotations: "${reason}".`);
return [];
}
);
});
return shadow(this, '_parsedAnnotations', parsedAnnotations);
return shadow(this, "_parsedAnnotations", parsedAnnotations);
}
}
const PDF_HEADER_SIGNATURE = new Uint8Array([0x25, 0x50, 0x44, 0x46, 0x2D]);
const PDF_HEADER_SIGNATURE = new Uint8Array([0x25, 0x50, 0x44, 0x46, 0x2d]);
// prettier-ignore
const STARTXREF_SIGNATURE = new Uint8Array([
0x73, 0x74, 0x61, 0x72, 0x74, 0x78, 0x72, 0x65, 0x66]);
const ENDOBJ_SIGNATURE = new Uint8Array([0x65, 0x6E, 0x64, 0x6F, 0x62, 0x6A]);
const ENDOBJ_SIGNATURE = new Uint8Array([0x65, 0x6e, 0x64, 0x6f, 0x62, 0x6a]);
const FINGERPRINT_FIRST_BYTES = 1024;
const EMPTY_FINGERPRINT =
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00';
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
function find(stream, signature, limit = 1024, backwards = false) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(limit > 0, 'The "limit" must be a positive integer.');
}
const signatureLength = signature.length;
@ -373,24 +443,29 @@ function find(stream, signature, limit = 1024, backwards = false) {
let pos = scanBytes.length - 1;
while (pos >= signatureEnd) {
let j = 0;
while (j < signatureLength &&
scanBytes[pos - j] === signature[signatureEnd - j]) {
while (
j < signatureLength &&
scanBytes[pos - j] === signature[signatureEnd - j]
) {
j++;
}
if (j >= signatureLength) { // `signature` found.
stream.pos += (pos - signatureEnd);
if (j >= signatureLength) {
// `signature` found.
stream.pos += pos - signatureEnd;
return true;
}
pos--;
}
} else { // forwards
} else {
// forwards
let pos = 0;
while (pos <= scanLength) {
let j = 0;
while (j < signatureLength && scanBytes[pos + j] === signature[j]) {
j++;
}
if (j >= signatureLength) { // `signature` found.
if (j >= signatureLength) {
// `signature` found.
stream.pos += pos;
return true;
}
@ -411,11 +486,12 @@ class PDFDocument {
} else if (isArrayBuffer(arg)) {
stream = new Stream(arg);
} else {
throw new Error('PDFDocument: Unknown argument type');
throw new Error("PDFDocument: Unknown argument type");
}
if (stream.length <= 0) {
throw new InvalidPDFException(
'The PDF file is empty, i.e. its size is zero bytes.');
"The PDF file is empty, i.e. its size is zero bytes."
);
}
this.pdfManager = pdfManager;
@ -432,17 +508,17 @@ class PDFDocument {
parse(recoveryMode) {
this.setup(recoveryMode);
const version = this.catalog.catDict.get('Version');
const version = this.catalog.catDict.get("Version");
if (isName(version)) {
this.pdfFormatVersion = version.name;
}
// Check if AcroForms are present in the document.
try {
this.acroForm = this.catalog.catDict.get('AcroForm');
this.acroForm = this.catalog.catDict.get("AcroForm");
if (this.acroForm) {
this.xfa = this.acroForm.get('XFA');
const fields = this.acroForm.get('Fields');
this.xfa = this.acroForm.get("XFA");
const fields = this.acroForm.get("Fields");
if ((!Array.isArray(fields) || fields.length === 0) && !this.xfa) {
this.acroForm = null; // No fields and no XFA, so it's not a form.
}
@ -451,13 +527,13 @@ class PDFDocument {
if (ex instanceof MissingDataException) {
throw ex;
}
info('Cannot fetch AcroForm entry; assuming no AcroForms are present');
info("Cannot fetch AcroForm entry; assuming no AcroForms are present");
this.acroForm = null;
}
// Check if a Collection dictionary is present in the document.
try {
const collection = this.catalog.catDict.get('Collection');
const collection = this.catalog.catDict.get("Collection");
if (isDict(collection) && collection.getKeys().length > 0) {
this.collection = collection;
}
@ -465,7 +541,7 @@ class PDFDocument {
if (ex instanceof MissingDataException) {
throw ex;
}
info('Cannot fetch Collection dictionary.');
info("Cannot fetch Collection dictionary.");
}
}
@ -479,7 +555,7 @@ class PDFDocument {
}
info(err);
}
return shadow(this, 'linearization', linearization);
return shadow(this, "linearization", linearization);
}
get startXRef() {
@ -490,13 +566,14 @@ class PDFDocument {
// Find the end of the first object.
stream.reset();
if (find(stream, ENDOBJ_SIGNATURE)) {
startXRef = (stream.pos + 6) - stream.start;
startXRef = stream.pos + 6 - stream.start;
}
} else {
// Find `startxref` by checking backwards from the end of the file.
const step = 1024;
const startXRefLength = STARTXREF_SIGNATURE.length;
let found = false, pos = stream.end;
let found = false,
pos = stream.end;
while (!found && pos > 0) {
pos -= step - startXRefLength;
@ -513,8 +590,9 @@ class PDFDocument {
do {
ch = stream.getByte();
} while (isSpace(ch));
let str = '';
while (ch >= 0x20 && ch <= 0x39) { // < '9'
let str = "";
while (ch >= 0x20 && ch <= 0x39) {
// < '9'
str += String.fromCharCode(ch);
ch = stream.getByte();
}
@ -524,7 +602,7 @@ class PDFDocument {
}
}
}
return shadow(this, 'startXRef', startXRef);
return shadow(this, "startXRef", startXRef);
}
// Find the header, get the PDF format version and setup the
@ -542,8 +620,10 @@ class PDFDocument {
// Read the PDF format version.
const MAX_PDF_VERSION_LENGTH = 12;
let version = '', ch;
while ((ch = stream.getByte()) > 0x20) { // Space
let version = "",
ch;
while ((ch = stream.getByte()) > 0x20) {
// Space
if (version.length >= MAX_PDF_VERSION_LENGTH) {
break;
}
@ -567,7 +647,7 @@ class PDFDocument {
get numPages() {
const linearization = this.linearization;
const num = linearization ? linearization.numPages : this.catalog.numPages;
return shadow(this, 'numPages', num);
return shadow(this, "numPages", num);
}
get documentInfo() {
@ -593,12 +673,12 @@ class PDFDocument {
let infoDict;
try {
infoDict = this.xref.trailer.get('Info');
infoDict = this.xref.trailer.get("Info");
} catch (err) {
if (err instanceof MissingDataException) {
throw err;
}
info('The document information dictionary is invalid.');
info("The document information dictionary is invalid.");
}
if (isDict(infoDict)) {
@ -610,12 +690,12 @@ class PDFDocument {
if (DocumentInfoValidators[key]) {
// Make sure the (standard) value conforms to the specification.
if (DocumentInfoValidators[key](value)) {
docInfo[key] = (typeof value !== 'string' ?
value : stringToPDFString(value));
docInfo[key] =
typeof value !== "string" ? value : stringToPDFString(value);
} else {
info(`Bad value in document info for "${key}".`);
}
} else if (typeof key === 'string') {
} else if (typeof key === "string") {
// For custom values, only accept white-listed types to prevent
// errors that would occur when trying to send non-serializable
// objects to the main-thread (for example `Dict` or `Stream`).
@ -629,67 +709,83 @@ class PDFDocument {
continue;
}
if (!docInfo['Custom']) {
docInfo['Custom'] = Object.create(null);
if (!docInfo["Custom"]) {
docInfo["Custom"] = Object.create(null);
}
docInfo['Custom'][key] = customValue;
docInfo["Custom"][key] = customValue;
}
}
}
return shadow(this, 'documentInfo', docInfo);
return shadow(this, "documentInfo", docInfo);
}
get fingerprint() {
let hash;
const idArray = this.xref.trailer.get('ID');
if (Array.isArray(idArray) && idArray[0] && isString(idArray[0]) &&
idArray[0] !== EMPTY_FINGERPRINT) {
const idArray = this.xref.trailer.get("ID");
if (
Array.isArray(idArray) &&
idArray[0] &&
isString(idArray[0]) &&
idArray[0] !== EMPTY_FINGERPRINT
) {
hash = stringToBytes(idArray[0]);
} else {
hash = calculateMD5(this.stream.getByteRange(0, FINGERPRINT_FIRST_BYTES),
0, FINGERPRINT_FIRST_BYTES);
hash = calculateMD5(
this.stream.getByteRange(0, FINGERPRINT_FIRST_BYTES),
0,
FINGERPRINT_FIRST_BYTES
);
}
const fingerprintBuf = [];
for (let i = 0, ii = hash.length; i < ii; i++) {
const hex = hash[i].toString(16);
fingerprintBuf.push(hex.padStart(2, '0'));
fingerprintBuf.push(hex.padStart(2, "0"));
}
return shadow(this, 'fingerprint', fingerprintBuf.join(''));
return shadow(this, "fingerprint", fingerprintBuf.join(""));
}
_getLinearizationPage(pageIndex) {
const { catalog, linearization, } = this;
const { catalog, linearization } = this;
assert(linearization && linearization.pageFirst === pageIndex);
const ref = Ref.get(linearization.objectNumberFirst, 0);
return this.xref.fetchAsync(ref).then((obj) => {
// Ensure that the object that was found is actually a Page dictionary.
if (isDict(obj, 'Page') ||
(isDict(obj) && !obj.has('Type') && obj.has('Contents'))) {
if (ref && !catalog.pageKidsCountCache.has(ref)) {
catalog.pageKidsCountCache.put(ref, 1); // Cache the Page reference.
return this.xref
.fetchAsync(ref)
.then(obj => {
// Ensure that the object that was found is actually a Page dictionary.
if (
isDict(obj, "Page") ||
(isDict(obj) && !obj.has("Type") && obj.has("Contents"))
) {
if (ref && !catalog.pageKidsCountCache.has(ref)) {
catalog.pageKidsCountCache.put(ref, 1); // Cache the Page reference.
}
return [obj, ref];
}
return [obj, ref];
}
throw new FormatError('The Linearization dictionary doesn\'t point ' +
'to a valid Page dictionary.');
}).catch((reason) => {
info(reason);
return catalog.getPageDict(pageIndex);
});
throw new FormatError(
"The Linearization dictionary doesn't point " +
"to a valid Page dictionary."
);
})
.catch(reason => {
info(reason);
return catalog.getPageDict(pageIndex);
});
}
getPage(pageIndex) {
if (this._pagePromises[pageIndex] !== undefined) {
return this._pagePromises[pageIndex];
}
const { catalog, linearization, } = this;
const { catalog, linearization } = this;
const promise = (linearization && linearization.pageFirst === pageIndex) ?
this._getLinearizationPage(pageIndex) : catalog.getPageDict(pageIndex);
const promise =
linearization && linearization.pageFirst === pageIndex
? this._getLinearizationPage(pageIndex)
: catalog.getPageDict(pageIndex);
return this._pagePromises[pageIndex] = promise.then(([pageDict, ref]) => {
return (this._pagePromises[pageIndex] = promise.then(([pageDict, ref]) => {
return new Page({
pdfManager: this.pdfManager,
xref: this.xref,
@ -700,11 +796,11 @@ class PDFDocument {
builtInCMapCache: catalog.builtInCMapCache,
pdfFunctionFactory: this.pdfFunctionFactory,
});
});
}));
}
checkFirstPage() {
return this.getPage(0).catch(async (reason) => {
return this.getPage(0).catch(async reason => {
if (reason instanceof XRefEntryException) {
// Clear out the various caches to ensure that we haven't stored any
// inconsistent and/or incorrect state, since that could easily break
@ -726,7 +822,4 @@ class PDFDocument {
}
}
export {
Page,
PDFDocument,
};
export { Page, PDFDocument };

View file

@ -15,275 +15,275 @@
// prettier-ignore
const ExpertEncoding = [
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'space', 'exclamsmall', 'Hungarumlautsmall', '', 'dollaroldstyle',
'dollarsuperior', 'ampersandsmall', 'Acutesmall', 'parenleftsuperior',
'parenrightsuperior', 'twodotenleader', 'onedotenleader', 'comma',
'hyphen', 'period', 'fraction', 'zerooldstyle', 'oneoldstyle',
'twooldstyle', 'threeoldstyle', 'fouroldstyle', 'fiveoldstyle',
'sixoldstyle', 'sevenoldstyle', 'eightoldstyle', 'nineoldstyle', 'colon',
'semicolon', 'commasuperior', 'threequartersemdash', 'periodsuperior',
'questionsmall', '', 'asuperior', 'bsuperior', 'centsuperior', 'dsuperior',
'esuperior', '', '', '', 'isuperior', '', '', 'lsuperior', 'msuperior',
'nsuperior', 'osuperior', '', '', 'rsuperior', 'ssuperior', 'tsuperior',
'', 'ff', 'fi', 'fl', 'ffi', 'ffl', 'parenleftinferior', '',
'parenrightinferior', 'Circumflexsmall', 'hyphensuperior', 'Gravesmall',
'Asmall', 'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall',
'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall', 'Nsmall',
'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall', 'Tsmall', 'Usmall',
'Vsmall', 'Wsmall', 'Xsmall', 'Ysmall', 'Zsmall', 'colonmonetary',
'onefitted', 'rupiah', 'Tildesmall', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', 'exclamdownsmall', 'centoldstyle', 'Lslashsmall',
'', '', 'Scaronsmall', 'Zcaronsmall', 'Dieresissmall', 'Brevesmall',
'Caronsmall', '', 'Dotaccentsmall', '', '', 'Macronsmall', '', '',
'figuredash', 'hypheninferior', '', '', 'Ogoneksmall', 'Ringsmall',
'Cedillasmall', '', '', '', 'onequarter', 'onehalf', 'threequarters',
'questiondownsmall', 'oneeighth', 'threeeighths', 'fiveeighths',
'seveneighths', 'onethird', 'twothirds', '', '', 'zerosuperior',
'onesuperior', 'twosuperior', 'threesuperior', 'foursuperior',
'fivesuperior', 'sixsuperior', 'sevensuperior', 'eightsuperior',
'ninesuperior', 'zeroinferior', 'oneinferior', 'twoinferior',
'threeinferior', 'fourinferior', 'fiveinferior', 'sixinferior',
'seveninferior', 'eightinferior', 'nineinferior', 'centinferior',
'dollarinferior', 'periodinferior', 'commainferior', 'Agravesmall',
'Aacutesmall', 'Acircumflexsmall', 'Atildesmall', 'Adieresissmall',
'Aringsmall', 'AEsmall', 'Ccedillasmall', 'Egravesmall', 'Eacutesmall',
'Ecircumflexsmall', 'Edieresissmall', 'Igravesmall', 'Iacutesmall',
'Icircumflexsmall', 'Idieresissmall', 'Ethsmall', 'Ntildesmall',
'Ogravesmall', 'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall',
'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall', 'Uacutesmall',
'Ucircumflexsmall', 'Udieresissmall', 'Yacutesmall', 'Thornsmall',
'Ydieresissmall'];
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"space", "exclamsmall", "Hungarumlautsmall", "", "dollaroldstyle",
"dollarsuperior", "ampersandsmall", "Acutesmall", "parenleftsuperior",
"parenrightsuperior", "twodotenleader", "onedotenleader", "comma",
"hyphen", "period", "fraction", "zerooldstyle", "oneoldstyle",
"twooldstyle", "threeoldstyle", "fouroldstyle", "fiveoldstyle",
"sixoldstyle", "sevenoldstyle", "eightoldstyle", "nineoldstyle", "colon",
"semicolon", "commasuperior", "threequartersemdash", "periodsuperior",
"questionsmall", "", "asuperior", "bsuperior", "centsuperior", "dsuperior",
"esuperior", "", "", "", "isuperior", "", "", "lsuperior", "msuperior",
"nsuperior", "osuperior", "", "", "rsuperior", "ssuperior", "tsuperior",
"", "ff", "fi", "fl", "ffi", "ffl", "parenleftinferior", "",
"parenrightinferior", "Circumflexsmall", "hyphensuperior", "Gravesmall",
"Asmall", "Bsmall", "Csmall", "Dsmall", "Esmall", "Fsmall", "Gsmall",
"Hsmall", "Ismall", "Jsmall", "Ksmall", "Lsmall", "Msmall", "Nsmall",
"Osmall", "Psmall", "Qsmall", "Rsmall", "Ssmall", "Tsmall", "Usmall",
"Vsmall", "Wsmall", "Xsmall", "Ysmall", "Zsmall", "colonmonetary",
"onefitted", "rupiah", "Tildesmall", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "exclamdownsmall", "centoldstyle", "Lslashsmall",
"", "", "Scaronsmall", "Zcaronsmall", "Dieresissmall", "Brevesmall",
"Caronsmall", "", "Dotaccentsmall", "", "", "Macronsmall", "", "",
"figuredash", "hypheninferior", "", "", "Ogoneksmall", "Ringsmall",
"Cedillasmall", "", "", "", "onequarter", "onehalf", "threequarters",
"questiondownsmall", "oneeighth", "threeeighths", "fiveeighths",
"seveneighths", "onethird", "twothirds", "", "", "zerosuperior",
"onesuperior", "twosuperior", "threesuperior", "foursuperior",
"fivesuperior", "sixsuperior", "sevensuperior", "eightsuperior",
"ninesuperior", "zeroinferior", "oneinferior", "twoinferior",
"threeinferior", "fourinferior", "fiveinferior", "sixinferior",
"seveninferior", "eightinferior", "nineinferior", "centinferior",
"dollarinferior", "periodinferior", "commainferior", "Agravesmall",
"Aacutesmall", "Acircumflexsmall", "Atildesmall", "Adieresissmall",
"Aringsmall", "AEsmall", "Ccedillasmall", "Egravesmall", "Eacutesmall",
"Ecircumflexsmall", "Edieresissmall", "Igravesmall", "Iacutesmall",
"Icircumflexsmall", "Idieresissmall", "Ethsmall", "Ntildesmall",
"Ogravesmall", "Oacutesmall", "Ocircumflexsmall", "Otildesmall",
"Odieresissmall", "OEsmall", "Oslashsmall", "Ugravesmall", "Uacutesmall",
"Ucircumflexsmall", "Udieresissmall", "Yacutesmall", "Thornsmall",
"Ydieresissmall"];
// prettier-ignore
const MacExpertEncoding = [
'', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'space', 'exclamsmall', 'Hungarumlautsmall', 'centoldstyle',
'dollaroldstyle', 'dollarsuperior', 'ampersandsmall', 'Acutesmall',
'parenleftsuperior', 'parenrightsuperior', 'twodotenleader',
'onedotenleader', 'comma', 'hyphen', 'period', 'fraction', 'zerooldstyle',
'oneoldstyle', 'twooldstyle', 'threeoldstyle', 'fouroldstyle',
'fiveoldstyle', 'sixoldstyle', 'sevenoldstyle', 'eightoldstyle',
'nineoldstyle', 'colon', 'semicolon', '', 'threequartersemdash', '',
'questionsmall', '', '', '', '', 'Ethsmall', '', '', 'onequarter',
'onehalf', 'threequarters', 'oneeighth', 'threeeighths', 'fiveeighths',
'seveneighths', 'onethird', 'twothirds', '', '', '', '', '', '', 'ff',
'fi', 'fl', 'ffi', 'ffl', 'parenleftinferior', '', 'parenrightinferior',
'Circumflexsmall', 'hypheninferior', 'Gravesmall', 'Asmall', 'Bsmall',
'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall', 'Hsmall', 'Ismall',
'Jsmall', 'Ksmall', 'Lsmall', 'Msmall', 'Nsmall', 'Osmall', 'Psmall',
'Qsmall', 'Rsmall', 'Ssmall', 'Tsmall', 'Usmall', 'Vsmall', 'Wsmall',
'Xsmall', 'Ysmall', 'Zsmall', 'colonmonetary', 'onefitted', 'rupiah',
'Tildesmall', '', '', 'asuperior', 'centsuperior', '', '', '', '',
'Aacutesmall', 'Agravesmall', 'Acircumflexsmall', 'Adieresissmall',
'Atildesmall', 'Aringsmall', 'Ccedillasmall', 'Eacutesmall', 'Egravesmall',
'Ecircumflexsmall', 'Edieresissmall', 'Iacutesmall', 'Igravesmall',
'Icircumflexsmall', 'Idieresissmall', 'Ntildesmall', 'Oacutesmall',
'Ogravesmall', 'Ocircumflexsmall', 'Odieresissmall', 'Otildesmall',
'Uacutesmall', 'Ugravesmall', 'Ucircumflexsmall', 'Udieresissmall', '',
'eightsuperior', 'fourinferior', 'threeinferior', 'sixinferior',
'eightinferior', 'seveninferior', 'Scaronsmall', '', 'centinferior',
'twoinferior', '', 'Dieresissmall', '', 'Caronsmall', 'osuperior',
'fiveinferior', '', 'commainferior', 'periodinferior', 'Yacutesmall', '',
'dollarinferior', '', '', 'Thornsmall', '', 'nineinferior', 'zeroinferior',
'Zcaronsmall', 'AEsmall', 'Oslashsmall', 'questiondownsmall',
'oneinferior', 'Lslashsmall', '', '', '', '', '', '', 'Cedillasmall', '',
'', '', '', '', 'OEsmall', 'figuredash', 'hyphensuperior', '', '', '', '',
'exclamdownsmall', '', 'Ydieresissmall', '', 'onesuperior', 'twosuperior',
'threesuperior', 'foursuperior', 'fivesuperior', 'sixsuperior',
'sevensuperior', 'ninesuperior', 'zerosuperior', '', 'esuperior',
'rsuperior', 'tsuperior', '', '', 'isuperior', 'ssuperior', 'dsuperior',
'', '', '', '', '', 'lsuperior', 'Ogoneksmall', 'Brevesmall',
'Macronsmall', 'bsuperior', 'nsuperior', 'msuperior', 'commasuperior',
'periodsuperior', 'Dotaccentsmall', 'Ringsmall', '', '', '', ''];
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"space", "exclamsmall", "Hungarumlautsmall", "centoldstyle",
"dollaroldstyle", "dollarsuperior", "ampersandsmall", "Acutesmall",
"parenleftsuperior", "parenrightsuperior", "twodotenleader",
"onedotenleader", "comma", "hyphen", "period", "fraction", "zerooldstyle",
"oneoldstyle", "twooldstyle", "threeoldstyle", "fouroldstyle",
"fiveoldstyle", "sixoldstyle", "sevenoldstyle", "eightoldstyle",
"nineoldstyle", "colon", "semicolon", "", "threequartersemdash", "",
"questionsmall", "", "", "", "", "Ethsmall", "", "", "onequarter",
"onehalf", "threequarters", "oneeighth", "threeeighths", "fiveeighths",
"seveneighths", "onethird", "twothirds", "", "", "", "", "", "", "ff",
"fi", "fl", "ffi", "ffl", "parenleftinferior", "", "parenrightinferior",
"Circumflexsmall", "hypheninferior", "Gravesmall", "Asmall", "Bsmall",
"Csmall", "Dsmall", "Esmall", "Fsmall", "Gsmall", "Hsmall", "Ismall",
"Jsmall", "Ksmall", "Lsmall", "Msmall", "Nsmall", "Osmall", "Psmall",
"Qsmall", "Rsmall", "Ssmall", "Tsmall", "Usmall", "Vsmall", "Wsmall",
"Xsmall", "Ysmall", "Zsmall", "colonmonetary", "onefitted", "rupiah",
"Tildesmall", "", "", "asuperior", "centsuperior", "", "", "", "",
"Aacutesmall", "Agravesmall", "Acircumflexsmall", "Adieresissmall",
"Atildesmall", "Aringsmall", "Ccedillasmall", "Eacutesmall", "Egravesmall",
"Ecircumflexsmall", "Edieresissmall", "Iacutesmall", "Igravesmall",
"Icircumflexsmall", "Idieresissmall", "Ntildesmall", "Oacutesmall",
"Ogravesmall", "Ocircumflexsmall", "Odieresissmall", "Otildesmall",
"Uacutesmall", "Ugravesmall", "Ucircumflexsmall", "Udieresissmall", "",
"eightsuperior", "fourinferior", "threeinferior", "sixinferior",
"eightinferior", "seveninferior", "Scaronsmall", "", "centinferior",
"twoinferior", "", "Dieresissmall", "", "Caronsmall", "osuperior",
"fiveinferior", "", "commainferior", "periodinferior", "Yacutesmall", "",
"dollarinferior", "", "", "Thornsmall", "", "nineinferior", "zeroinferior",
"Zcaronsmall", "AEsmall", "Oslashsmall", "questiondownsmall",
"oneinferior", "Lslashsmall", "", "", "", "", "", "", "Cedillasmall", "",
"", "", "", "", "OEsmall", "figuredash", "hyphensuperior", "", "", "", "",
"exclamdownsmall", "", "Ydieresissmall", "", "onesuperior", "twosuperior",
"threesuperior", "foursuperior", "fivesuperior", "sixsuperior",
"sevensuperior", "ninesuperior", "zerosuperior", "", "esuperior",
"rsuperior", "tsuperior", "", "", "isuperior", "ssuperior", "dsuperior",
"", "", "", "", "", "lsuperior", "Ogoneksmall", "Brevesmall",
"Macronsmall", "bsuperior", "nsuperior", "msuperior", "commasuperior",
"periodsuperior", "Dotaccentsmall", "Ringsmall", "", "", "", ""];
// prettier-ignore
const MacRomanEncoding = [
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent',
'ampersand', 'quotesingle', 'parenleft', 'parenright', 'asterisk', 'plus',
'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two', 'three',
'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon',
'less', 'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright',
'asciicircum', 'underscore', 'grave', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde', '',
'Adieresis', 'Aring', 'Ccedilla', 'Eacute', 'Ntilde', 'Odieresis',
'Udieresis', 'aacute', 'agrave', 'acircumflex', 'adieresis', 'atilde',
'aring', 'ccedilla', 'eacute', 'egrave', 'ecircumflex', 'edieresis',
'iacute', 'igrave', 'icircumflex', 'idieresis', 'ntilde', 'oacute',
'ograve', 'ocircumflex', 'odieresis', 'otilde', 'uacute', 'ugrave',
'ucircumflex', 'udieresis', 'dagger', 'degree', 'cent', 'sterling',
'section', 'bullet', 'paragraph', 'germandbls', 'registered', 'copyright',
'trademark', 'acute', 'dieresis', 'notequal', 'AE', 'Oslash', 'infinity',
'plusminus', 'lessequal', 'greaterequal', 'yen', 'mu', 'partialdiff',
'summation', 'product', 'pi', 'integral', 'ordfeminine', 'ordmasculine',
'Omega', 'ae', 'oslash', 'questiondown', 'exclamdown', 'logicalnot',
'radical', 'florin', 'approxequal', 'Delta', 'guillemotleft',
'guillemotright', 'ellipsis', 'space', 'Agrave', 'Atilde', 'Otilde', 'OE',
'oe', 'endash', 'emdash', 'quotedblleft', 'quotedblright', 'quoteleft',
'quoteright', 'divide', 'lozenge', 'ydieresis', 'Ydieresis', 'fraction',
'currency', 'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'daggerdbl',
'periodcentered', 'quotesinglbase', 'quotedblbase', 'perthousand',
'Acircumflex', 'Ecircumflex', 'Aacute', 'Edieresis', 'Egrave', 'Iacute',
'Icircumflex', 'Idieresis', 'Igrave', 'Oacute', 'Ocircumflex', 'apple',
'Ograve', 'Uacute', 'Ucircumflex', 'Ugrave', 'dotlessi', 'circumflex',
'tilde', 'macron', 'breve', 'dotaccent', 'ring', 'cedilla', 'hungarumlaut',
'ogonek', 'caron'];
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"space", "exclam", "quotedbl", "numbersign", "dollar", "percent",
"ampersand", "quotesingle", "parenleft", "parenright", "asterisk", "plus",
"comma", "hyphen", "period", "slash", "zero", "one", "two", "three",
"four", "five", "six", "seven", "eight", "nine", "colon", "semicolon",
"less", "equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "bracketleft", "backslash", "bracketright",
"asciicircum", "underscore", "grave", "a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", "",
"Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde", "Odieresis",
"Udieresis", "aacute", "agrave", "acircumflex", "adieresis", "atilde",
"aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis",
"iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute",
"ograve", "ocircumflex", "odieresis", "otilde", "uacute", "ugrave",
"ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling",
"section", "bullet", "paragraph", "germandbls", "registered", "copyright",
"trademark", "acute", "dieresis", "notequal", "AE", "Oslash", "infinity",
"plusminus", "lessequal", "greaterequal", "yen", "mu", "partialdiff",
"summation", "product", "pi", "integral", "ordfeminine", "ordmasculine",
"Omega", "ae", "oslash", "questiondown", "exclamdown", "logicalnot",
"radical", "florin", "approxequal", "Delta", "guillemotleft",
"guillemotright", "ellipsis", "space", "Agrave", "Atilde", "Otilde", "OE",
"oe", "endash", "emdash", "quotedblleft", "quotedblright", "quoteleft",
"quoteright", "divide", "lozenge", "ydieresis", "Ydieresis", "fraction",
"currency", "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl",
"periodcentered", "quotesinglbase", "quotedblbase", "perthousand",
"Acircumflex", "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute",
"Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", "apple",
"Ograve", "Uacute", "Ucircumflex", "Ugrave", "dotlessi", "circumflex",
"tilde", "macron", "breve", "dotaccent", "ring", "cedilla", "hungarumlaut",
"ogonek", "caron"];
// prettier-ignore
const StandardEncoding = [
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent',
'ampersand', 'quoteright', 'parenleft', 'parenright', 'asterisk', 'plus',
'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two', 'three',
'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon',
'less', 'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright',
'asciicircum', 'underscore', 'quoteleft', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'exclamdown',
'cent', 'sterling', 'fraction', 'yen', 'florin', 'section', 'currency',
'quotesingle', 'quotedblleft', 'guillemotleft', 'guilsinglleft',
'guilsinglright', 'fi', 'fl', '', 'endash', 'dagger', 'daggerdbl',
'periodcentered', '', 'paragraph', 'bullet', 'quotesinglbase',
'quotedblbase', 'quotedblright', 'guillemotright', 'ellipsis',
'perthousand', '', 'questiondown', '', 'grave', 'acute', 'circumflex',
'tilde', 'macron', 'breve', 'dotaccent', 'dieresis', '', 'ring', 'cedilla',
'', 'hungarumlaut', 'ogonek', 'caron', 'emdash', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', 'AE', '', 'ordfeminine', '', '',
'', '', 'Lslash', 'Oslash', 'OE', 'ordmasculine', '', '', '', '', '', 'ae',
'', '', '', 'dotlessi', '', '', 'lslash', 'oslash', 'oe', 'germandbls', '',
'', '', ''];
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"space", "exclam", "quotedbl", "numbersign", "dollar", "percent",
"ampersand", "quoteright", "parenleft", "parenright", "asterisk", "plus",
"comma", "hyphen", "period", "slash", "zero", "one", "two", "three",
"four", "five", "six", "seven", "eight", "nine", "colon", "semicolon",
"less", "equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "bracketleft", "backslash", "bracketright",
"asciicircum", "underscore", "quoteleft", "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "exclamdown",
"cent", "sterling", "fraction", "yen", "florin", "section", "currency",
"quotesingle", "quotedblleft", "guillemotleft", "guilsinglleft",
"guilsinglright", "fi", "fl", "", "endash", "dagger", "daggerdbl",
"periodcentered", "", "paragraph", "bullet", "quotesinglbase",
"quotedblbase", "quotedblright", "guillemotright", "ellipsis",
"perthousand", "", "questiondown", "", "grave", "acute", "circumflex",
"tilde", "macron", "breve", "dotaccent", "dieresis", "", "ring", "cedilla",
"", "hungarumlaut", "ogonek", "caron", "emdash", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "AE", "", "ordfeminine", "", "",
"", "", "Lslash", "Oslash", "OE", "ordmasculine", "", "", "", "", "", "ae",
"", "", "", "dotlessi", "", "", "lslash", "oslash", "oe", "germandbls", "",
"", "", ""];
// prettier-ignore
const WinAnsiEncoding = [
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent',
'ampersand', 'quotesingle', 'parenleft', 'parenright', 'asterisk', 'plus',
'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two', 'three',
'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon',
'less', 'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright',
'asciicircum', 'underscore', 'grave', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde',
'bullet', 'Euro', 'bullet', 'quotesinglbase', 'florin', 'quotedblbase',
'ellipsis', 'dagger', 'daggerdbl', 'circumflex', 'perthousand', 'Scaron',
'guilsinglleft', 'OE', 'bullet', 'Zcaron', 'bullet', 'bullet', 'quoteleft',
'quoteright', 'quotedblleft', 'quotedblright', 'bullet', 'endash',
'emdash', 'tilde', 'trademark', 'scaron', 'guilsinglright', 'oe', 'bullet',
'zcaron', 'Ydieresis', 'space', 'exclamdown', 'cent', 'sterling',
'currency', 'yen', 'brokenbar', 'section', 'dieresis', 'copyright',
'ordfeminine', 'guillemotleft', 'logicalnot', 'hyphen', 'registered',
'macron', 'degree', 'plusminus', 'twosuperior', 'threesuperior', 'acute',
'mu', 'paragraph', 'periodcentered', 'cedilla', 'onesuperior',
'ordmasculine', 'guillemotright', 'onequarter', 'onehalf', 'threequarters',
'questiondown', 'Agrave', 'Aacute', 'Acircumflex', 'Atilde', 'Adieresis',
'Aring', 'AE', 'Ccedilla', 'Egrave', 'Eacute', 'Ecircumflex', 'Edieresis',
'Igrave', 'Iacute', 'Icircumflex', 'Idieresis', 'Eth', 'Ntilde', 'Ograve',
'Oacute', 'Ocircumflex', 'Otilde', 'Odieresis', 'multiply', 'Oslash',
'Ugrave', 'Uacute', 'Ucircumflex', 'Udieresis', 'Yacute', 'Thorn',
'germandbls', 'agrave', 'aacute', 'acircumflex', 'atilde', 'adieresis',
'aring', 'ae', 'ccedilla', 'egrave', 'eacute', 'ecircumflex', 'edieresis',
'igrave', 'iacute', 'icircumflex', 'idieresis', 'eth', 'ntilde', 'ograve',
'oacute', 'ocircumflex', 'otilde', 'odieresis', 'divide', 'oslash',
'ugrave', 'uacute', 'ucircumflex', 'udieresis', 'yacute', 'thorn',
'ydieresis'];
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"space", "exclam", "quotedbl", "numbersign", "dollar", "percent",
"ampersand", "quotesingle", "parenleft", "parenright", "asterisk", "plus",
"comma", "hyphen", "period", "slash", "zero", "one", "two", "three",
"four", "five", "six", "seven", "eight", "nine", "colon", "semicolon",
"less", "equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "bracketleft", "backslash", "bracketright",
"asciicircum", "underscore", "grave", "a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde",
"bullet", "Euro", "bullet", "quotesinglbase", "florin", "quotedblbase",
"ellipsis", "dagger", "daggerdbl", "circumflex", "perthousand", "Scaron",
"guilsinglleft", "OE", "bullet", "Zcaron", "bullet", "bullet", "quoteleft",
"quoteright", "quotedblleft", "quotedblright", "bullet", "endash",
"emdash", "tilde", "trademark", "scaron", "guilsinglright", "oe", "bullet",
"zcaron", "Ydieresis", "space", "exclamdown", "cent", "sterling",
"currency", "yen", "brokenbar", "section", "dieresis", "copyright",
"ordfeminine", "guillemotleft", "logicalnot", "hyphen", "registered",
"macron", "degree", "plusminus", "twosuperior", "threesuperior", "acute",
"mu", "paragraph", "periodcentered", "cedilla", "onesuperior",
"ordmasculine", "guillemotright", "onequarter", "onehalf", "threequarters",
"questiondown", "Agrave", "Aacute", "Acircumflex", "Atilde", "Adieresis",
"Aring", "AE", "Ccedilla", "Egrave", "Eacute", "Ecircumflex", "Edieresis",
"Igrave", "Iacute", "Icircumflex", "Idieresis", "Eth", "Ntilde", "Ograve",
"Oacute", "Ocircumflex", "Otilde", "Odieresis", "multiply", "Oslash",
"Ugrave", "Uacute", "Ucircumflex", "Udieresis", "Yacute", "Thorn",
"germandbls", "agrave", "aacute", "acircumflex", "atilde", "adieresis",
"aring", "ae", "ccedilla", "egrave", "eacute", "ecircumflex", "edieresis",
"igrave", "iacute", "icircumflex", "idieresis", "eth", "ntilde", "ograve",
"oacute", "ocircumflex", "otilde", "odieresis", "divide", "oslash",
"ugrave", "uacute", "ucircumflex", "udieresis", "yacute", "thorn",
"ydieresis"];
// prettier-ignore
const SymbolSetEncoding = [
'', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'space', 'exclam', 'universal', 'numbersign', 'existential', 'percent',
'ampersand', 'suchthat', 'parenleft', 'parenright', 'asteriskmath', 'plus',
'comma', 'minus', 'period', 'slash', 'zero', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon', 'less',
'equal', 'greater', 'question', 'congruent', 'Alpha', 'Beta', 'Chi',
'Delta', 'Epsilon', 'Phi', 'Gamma', 'Eta', 'Iota', 'theta1', 'Kappa',
'Lambda', 'Mu', 'Nu', 'Omicron', 'Pi', 'Theta', 'Rho', 'Sigma', 'Tau',
'Upsilon', 'sigma1', 'Omega', 'Xi', 'Psi', 'Zeta', 'bracketleft',
'therefore', 'bracketright', 'perpendicular', 'underscore', 'radicalex',
'alpha', 'beta', 'chi', 'delta', 'epsilon', 'phi', 'gamma', 'eta', 'iota',
'phi1', 'kappa', 'lambda', 'mu', 'nu', 'omicron', 'pi', 'theta', 'rho',
'sigma', 'tau', 'upsilon', 'omega1', 'omega', 'xi', 'psi', 'zeta',
'braceleft', 'bar', 'braceright', 'similar', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', 'Euro', 'Upsilon1', 'minute', 'lessequal',
'fraction', 'infinity', 'florin', 'club', 'diamond', 'heart', 'spade',
'arrowboth', 'arrowleft', 'arrowup', 'arrowright', 'arrowdown', 'degree',
'plusminus', 'second', 'greaterequal', 'multiply', 'proportional',
'partialdiff', 'bullet', 'divide', 'notequal', 'equivalence',
'approxequal', 'ellipsis', 'arrowvertex', 'arrowhorizex', 'carriagereturn',
'aleph', 'Ifraktur', 'Rfraktur', 'weierstrass', 'circlemultiply',
'circleplus', 'emptyset', 'intersection', 'union', 'propersuperset',
'reflexsuperset', 'notsubset', 'propersubset', 'reflexsubset', 'element',
'notelement', 'angle', 'gradient', 'registerserif', 'copyrightserif',
'trademarkserif', 'product', 'radical', 'dotmath', 'logicalnot',
'logicaland', 'logicalor', 'arrowdblboth', 'arrowdblleft', 'arrowdblup',
'arrowdblright', 'arrowdbldown', 'lozenge', 'angleleft', 'registersans',
'copyrightsans', 'trademarksans', 'summation', 'parenlefttp',
'parenleftex', 'parenleftbt', 'bracketlefttp', 'bracketleftex',
'bracketleftbt', 'bracelefttp', 'braceleftmid', 'braceleftbt', 'braceex',
'', 'angleright', 'integral', 'integraltp', 'integralex', 'integralbt',
'parenrighttp', 'parenrightex', 'parenrightbt', 'bracketrighttp',
'bracketrightex', 'bracketrightbt', 'bracerighttp', 'bracerightmid',
'bracerightbt', ''];
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"space", "exclam", "universal", "numbersign", "existential", "percent",
"ampersand", "suchthat", "parenleft", "parenright", "asteriskmath", "plus",
"comma", "minus", "period", "slash", "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "colon", "semicolon", "less",
"equal", "greater", "question", "congruent", "Alpha", "Beta", "Chi",
"Delta", "Epsilon", "Phi", "Gamma", "Eta", "Iota", "theta1", "Kappa",
"Lambda", "Mu", "Nu", "Omicron", "Pi", "Theta", "Rho", "Sigma", "Tau",
"Upsilon", "sigma1", "Omega", "Xi", "Psi", "Zeta", "bracketleft",
"therefore", "bracketright", "perpendicular", "underscore", "radicalex",
"alpha", "beta", "chi", "delta", "epsilon", "phi", "gamma", "eta", "iota",
"phi1", "kappa", "lambda", "mu", "nu", "omicron", "pi", "theta", "rho",
"sigma", "tau", "upsilon", "omega1", "omega", "xi", "psi", "zeta",
"braceleft", "bar", "braceright", "similar", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "Euro", "Upsilon1", "minute", "lessequal",
"fraction", "infinity", "florin", "club", "diamond", "heart", "spade",
"arrowboth", "arrowleft", "arrowup", "arrowright", "arrowdown", "degree",
"plusminus", "second", "greaterequal", "multiply", "proportional",
"partialdiff", "bullet", "divide", "notequal", "equivalence",
"approxequal", "ellipsis", "arrowvertex", "arrowhorizex", "carriagereturn",
"aleph", "Ifraktur", "Rfraktur", "weierstrass", "circlemultiply",
"circleplus", "emptyset", "intersection", "union", "propersuperset",
"reflexsuperset", "notsubset", "propersubset", "reflexsubset", "element",
"notelement", "angle", "gradient", "registerserif", "copyrightserif",
"trademarkserif", "product", "radical", "dotmath", "logicalnot",
"logicaland", "logicalor", "arrowdblboth", "arrowdblleft", "arrowdblup",
"arrowdblright", "arrowdbldown", "lozenge", "angleleft", "registersans",
"copyrightsans", "trademarksans", "summation", "parenlefttp",
"parenleftex", "parenleftbt", "bracketlefttp", "bracketleftex",
"bracketleftbt", "bracelefttp", "braceleftmid", "braceleftbt", "braceex",
"", "angleright", "integral", "integraltp", "integralex", "integralbt",
"parenrighttp", "parenrightex", "parenrightbt", "bracketrighttp",
"bracketrightex", "bracketrightbt", "bracerighttp", "bracerightmid",
"bracerightbt", ""];
// prettier-ignore
const ZapfDingbatsEncoding = [
'', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'space', 'a1', 'a2', 'a202', 'a3', 'a4', 'a5', 'a119', 'a118', 'a117',
'a11', 'a12', 'a13', 'a14', 'a15', 'a16', 'a105', 'a17', 'a18', 'a19',
'a20', 'a21', 'a22', 'a23', 'a24', 'a25', 'a26', 'a27', 'a28', 'a6', 'a7',
'a8', 'a9', 'a10', 'a29', 'a30', 'a31', 'a32', 'a33', 'a34', 'a35', 'a36',
'a37', 'a38', 'a39', 'a40', 'a41', 'a42', 'a43', 'a44', 'a45', 'a46',
'a47', 'a48', 'a49', 'a50', 'a51', 'a52', 'a53', 'a54', 'a55', 'a56',
'a57', 'a58', 'a59', 'a60', 'a61', 'a62', 'a63', 'a64', 'a65', 'a66',
'a67', 'a68', 'a69', 'a70', 'a71', 'a72', 'a73', 'a74', 'a203', 'a75',
'a204', 'a76', 'a77', 'a78', 'a79', 'a81', 'a82', 'a83', 'a84', 'a97',
'a98', 'a99', 'a100', '', 'a89', 'a90', 'a93', 'a94', 'a91', 'a92', 'a205',
'a85', 'a206', 'a86', 'a87', 'a88', 'a95', 'a96', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', 'a101', 'a102', 'a103',
'a104', 'a106', 'a107', 'a108', 'a112', 'a111', 'a110', 'a109', 'a120',
'a121', 'a122', 'a123', 'a124', 'a125', 'a126', 'a127', 'a128', 'a129',
'a130', 'a131', 'a132', 'a133', 'a134', 'a135', 'a136', 'a137', 'a138',
'a139', 'a140', 'a141', 'a142', 'a143', 'a144', 'a145', 'a146', 'a147',
'a148', 'a149', 'a150', 'a151', 'a152', 'a153', 'a154', 'a155', 'a156',
'a157', 'a158', 'a159', 'a160', 'a161', 'a163', 'a164', 'a196', 'a165',
'a192', 'a166', 'a167', 'a168', 'a169', 'a170', 'a171', 'a172', 'a173',
'a162', 'a174', 'a175', 'a176', 'a177', 'a178', 'a179', 'a193', 'a180',
'a199', 'a181', 'a200', 'a182', '', 'a201', 'a183', 'a184', 'a197', 'a185',
'a194', 'a198', 'a186', 'a195', 'a187', 'a188', 'a189', 'a190', 'a191', ''];
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"space", "a1", "a2", "a202", "a3", "a4", "a5", "a119", "a118", "a117",
"a11", "a12", "a13", "a14", "a15", "a16", "a105", "a17", "a18", "a19",
"a20", "a21", "a22", "a23", "a24", "a25", "a26", "a27", "a28", "a6", "a7",
"a8", "a9", "a10", "a29", "a30", "a31", "a32", "a33", "a34", "a35", "a36",
"a37", "a38", "a39", "a40", "a41", "a42", "a43", "a44", "a45", "a46",
"a47", "a48", "a49", "a50", "a51", "a52", "a53", "a54", "a55", "a56",
"a57", "a58", "a59", "a60", "a61", "a62", "a63", "a64", "a65", "a66",
"a67", "a68", "a69", "a70", "a71", "a72", "a73", "a74", "a203", "a75",
"a204", "a76", "a77", "a78", "a79", "a81", "a82", "a83", "a84", "a97",
"a98", "a99", "a100", "", "a89", "a90", "a93", "a94", "a91", "a92", "a205",
"a85", "a206", "a86", "a87", "a88", "a95", "a96", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "a101", "a102", "a103",
"a104", "a106", "a107", "a108", "a112", "a111", "a110", "a109", "a120",
"a121", "a122", "a123", "a124", "a125", "a126", "a127", "a128", "a129",
"a130", "a131", "a132", "a133", "a134", "a135", "a136", "a137", "a138",
"a139", "a140", "a141", "a142", "a143", "a144", "a145", "a146", "a147",
"a148", "a149", "a150", "a151", "a152", "a153", "a154", "a155", "a156",
"a157", "a158", "a159", "a160", "a161", "a163", "a164", "a196", "a165",
"a192", "a166", "a167", "a168", "a169", "a170", "a171", "a172", "a173",
"a162", "a174", "a175", "a176", "a177", "a178", "a179", "a193", "a180",
"a199", "a181", "a200", "a182", "", "a201", "a183", "a184", "a197", "a185",
"a194", "a198", "a186", "a195", "a187", "a188", "a189", "a190", "a191", ""];
function getEncoding(encodingName) {
switch (encodingName) {
case 'WinAnsiEncoding':
case "WinAnsiEncoding":
return WinAnsiEncoding;
case 'StandardEncoding':
case "StandardEncoding":
return StandardEncoding;
case 'MacRomanEncoding':
case "MacRomanEncoding":
return MacRomanEncoding;
case 'SymbolSetEncoding':
case "SymbolSetEncoding":
return SymbolSetEncoding;
case 'ZapfDingbatsEncoding':
case "ZapfDingbatsEncoding":
return ZapfDingbatsEncoding;
case 'ExpertEncoding':
case "ExpertEncoding":
return ExpertEncoding;
case 'MacExpertEncoding':
case "MacExpertEncoding":
return MacExpertEncoding;
default:
return null;

File diff suppressed because it is too large Load diff

View file

@ -14,17 +14,25 @@
*/
import {
bytesToString, FONT_IDENTITY_MATRIX, FormatError, unreachable, warn
} from '../shared/util';
import { CFFParser } from './cff_parser';
import { getGlyphsUnicode } from './glyphlist';
import { StandardEncoding } from './encodings';
import { Stream } from './stream';
bytesToString,
FONT_IDENTITY_MATRIX,
FormatError,
unreachable,
warn,
} from "../shared/util";
import { CFFParser } from "./cff_parser";
import { getGlyphsUnicode } from "./glyphlist";
import { StandardEncoding } from "./encodings";
import { Stream } from "./stream";
var FontRendererFactory = (function FontRendererFactoryClosure() {
function getLong(data, offset) {
return (data[offset] << 24) | (data[offset + 1] << 16) |
(data[offset + 2] << 8) | data[offset + 3];
return (
(data[offset] << 24) |
(data[offset + 1] << 16) |
(data[offset + 2] << 8) |
data[offset + 3]
);
}
function getUshort(data, offset) {
@ -32,8 +40,10 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
}
function parseCmap(data, start, end) {
var offset = (getUshort(data, start + 2) === 1 ?
getLong(data, start + 8) : getLong(data, start + 16));
var offset =
getUshort(data, start + 2) === 1
? getLong(data, start + 8)
: getLong(data, start + 16);
var format = getUshort(data, start + offset);
var ranges, p, i;
if (format === 4) {
@ -42,7 +52,7 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
p = start + offset + 14;
ranges = [];
for (i = 0; i < segCount; i++, p += 2) {
ranges[i] = { end: getUshort(data, p), };
ranges[i] = { end: getUshort(data, p) };
}
p += 2;
for (i = 0; i < segCount; i++, p += 2) {
@ -83,13 +93,18 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
function parseCff(data, start, end, seacAnalysisEnabled) {
var properties = {};
var parser = new CFFParser(new Stream(data, start, end - start),
properties, seacAnalysisEnabled);
var parser = new CFFParser(
new Stream(data, start, end - start),
properties,
seacAnalysisEnabled
);
var cff = parser.parse();
return {
glyphs: cff.charStrings.objects,
subrs: (cff.topDict.privateDict && cff.topDict.privateDict.subrsIndex &&
cff.topDict.privateDict.subrsIndex.objects),
subrs:
cff.topDict.privateDict &&
cff.topDict.privateDict.subrsIndex &&
cff.topDict.privateDict.subrsIndex.objects,
gsubrs: cff.globalSubrIndex && cff.globalSubrIndex.objects,
isCFFCIDFont: cff.isCIDFont,
fdSelect: cff.fdSelect,
@ -102,8 +117,12 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
if (isGlyphLocationsLong) {
itemSize = 4;
itemDecode = function fontItemDecodeLong(data, offset) {
return (data[offset] << 24) | (data[offset + 1] << 16) |
(data[offset + 2] << 8) | data[offset + 3];
return (
(data[offset] << 24) |
(data[offset + 1] << 16) |
(data[offset + 2] << 8) |
data[offset + 3]
);
};
} else {
itemSize = 2;
@ -122,8 +141,10 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
}
function lookupCmap(ranges, unicode) {
var code = unicode.codePointAt(0), gid = 0;
var l = 0, r = ranges.length - 1;
var code = unicode.codePointAt(0),
gid = 0;
var l = 0,
r = ranges.length - 1;
while (l < r) {
var c = (l + r + 1) >> 1;
if (code < ranges[c].start) {
@ -133,8 +154,10 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
}
}
if (ranges[l].start <= code && code <= ranges[l].end) {
gid = (ranges[l].idDelta + (ranges[l].ids ?
ranges[l].ids[code - ranges[l].start] : code)) & 0xFFFF;
gid =
(ranges[l].idDelta +
(ranges[l].ids ? ranges[l].ids[code - ranges[l].start] : code)) &
0xffff;
}
return {
charCode: code,
@ -144,19 +167,20 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
function compileGlyf(code, cmds, font) {
function moveTo(x, y) {
cmds.push({ cmd: 'moveTo', args: [x, y], });
cmds.push({ cmd: "moveTo", args: [x, y] });
}
function lineTo(x, y) {
cmds.push({ cmd: 'lineTo', args: [x, y], });
cmds.push({ cmd: "lineTo", args: [x, y] });
}
function quadraticCurveTo(xa, ya, x, y) {
cmds.push({ cmd: 'quadraticCurveTo', args: [xa, ya, x, y], });
cmds.push({ cmd: "quadraticCurveTo", args: [xa, ya, x, y] });
}
var i = 0;
var numberOfContours = ((code[i] << 24) | (code[i + 1] << 16)) >> 16;
var flags;
var x = 0, y = 0;
var x = 0,
y = 0;
i += 10;
if (numberOfContours < 0) {
// composite glyph
@ -165,29 +189,34 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
var glyphIndex = (code[i + 2] << 8) | code[i + 3];
i += 4;
var arg1, arg2;
if ((flags & 0x01)) {
if (flags & 0x01) {
arg1 = ((code[i] << 24) | (code[i + 1] << 16)) >> 16;
arg2 = ((code[i + 2] << 24) | (code[i + 3] << 16)) >> 16;
i += 4;
} else {
arg1 = code[i++]; arg2 = code[i++];
arg1 = code[i++];
arg2 = code[i++];
}
if ((flags & 0x02)) {
x = arg1;
y = arg2;
if (flags & 0x02) {
x = arg1;
y = arg2;
} else {
x = 0; y = 0; // TODO "they are points" ?
x = 0;
y = 0; // TODO "they are points" ?
}
var scaleX = 1, scaleY = 1, scale01 = 0, scale10 = 0;
if ((flags & 0x08)) {
scaleX =
scaleY = ((code[i] << 24) | (code[i + 1] << 16)) / 1073741824;
var scaleX = 1,
scaleY = 1,
scale01 = 0,
scale10 = 0;
if (flags & 0x08) {
scaleX = scaleY =
((code[i] << 24) | (code[i + 1] << 16)) / 1073741824;
i += 2;
} else if ((flags & 0x40)) {
} else if (flags & 0x40) {
scaleX = ((code[i] << 24) | (code[i + 1] << 16)) / 1073741824;
scaleY = ((code[i + 2] << 24) | (code[i + 3] << 16)) / 1073741824;
i += 4;
} else if ((flags & 0x80)) {
} else if (flags & 0x80) {
scaleX = ((code[i] << 24) | (code[i + 1] << 16)) / 1073741824;
scale01 = ((code[i + 2] << 24) | (code[i + 3] << 16)) / 1073741824;
scale10 = ((code[i + 4] << 24) | (code[i + 5] << 16)) / 1073741824;
@ -196,13 +225,15 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
}
var subglyph = font.glyphs[glyphIndex];
if (subglyph) {
cmds.push({ cmd: 'save', });
cmds.push({ cmd: 'transform',
args: [scaleX, scale01, scale10, scaleY, x, y], });
cmds.push({ cmd: "save" });
cmds.push({
cmd: "transform",
args: [scaleX, scale01, scale10, scaleY, x, y],
});
compileGlyf(subglyph, cmds, font);
cmds.push({ cmd: 'restore', });
cmds.push({ cmd: "restore" });
}
} while ((flags & 0x20));
} while (flags & 0x20);
} else {
// simple glyph
var endPtsOfContours = [];
@ -218,11 +249,11 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
while (points.length < numberOfPoints) {
flags = code[i++];
var repeat = 1;
if ((flags & 0x08)) {
if (flags & 0x08) {
repeat += code[i++];
}
while (repeat-- > 0) {
points.push({ flags, });
points.push({ flags });
}
}
for (j = 0; j < numberOfPoints; j++) {
@ -262,9 +293,9 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
// contours might have implicit points, which is located in the middle
// between two neighboring off-curve points
var contour = points.slice(startPoint, endPoint + 1);
if ((contour[0].flags & 1)) {
if (contour[0].flags & 1) {
contour.push(contour[0]); // using start point at the contour end
} else if ((contour[contour.length - 1].flags & 1)) {
} else if (contour[contour.length - 1].flags & 1) {
// first is off-curve point, trying to use one from the end
contour.unshift(contour[contour.length - 1]);
} else {
@ -279,16 +310,23 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
}
moveTo(contour[0].x, contour[0].y);
for (j = 1, jj = contour.length; j < jj; j++) {
if ((contour[j].flags & 1)) {
if (contour[j].flags & 1) {
lineTo(contour[j].x, contour[j].y);
} else if ((contour[j + 1].flags & 1)) {
quadraticCurveTo(contour[j].x, contour[j].y,
contour[j + 1].x, contour[j + 1].y);
} else if (contour[j + 1].flags & 1) {
quadraticCurveTo(
contour[j].x,
contour[j].y,
contour[j + 1].x,
contour[j + 1].y
);
j++;
} else {
quadraticCurveTo(contour[j].x, contour[j].y,
quadraticCurveTo(
contour[j].x,
contour[j].y,
(contour[j].x + contour[j + 1].x) / 2,
(contour[j].y + contour[j + 1].y) / 2);
(contour[j].y + contour[j + 1].y) / 2
);
}
}
startPoint = endPoint + 1;
@ -298,17 +336,18 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
function compileCharString(code, cmds, font, glyphId) {
var stack = [];
var x = 0, y = 0;
var x = 0,
y = 0;
var stems = 0;
function moveTo(x, y) {
cmds.push({ cmd: 'moveTo', args: [x, y], });
cmds.push({ cmd: "moveTo", args: [x, y] });
}
function lineTo(x, y) {
cmds.push({ cmd: 'lineTo', args: [x, y], });
cmds.push({ cmd: "lineTo", args: [x, y] });
}
function bezierCurveTo(x1, y1, x2, y2, x, y) {
cmds.push({ cmd: 'bezierCurveTo', args: [x1, y1, x2, y2, x, y], });
cmds.push({ cmd: "bezierCurveTo", args: [x1, y1, x2, y2, x, y] });
}
function parse(code) {
@ -362,9 +401,12 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
break;
case 8: // rrcurveto
while (stack.length > 0) {
xa = x + stack.shift(); ya = y + stack.shift();
xb = xa + stack.shift(); yb = ya + stack.shift();
x = xb + stack.shift(); y = yb + stack.shift();
xa = x + stack.shift();
ya = y + stack.shift();
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb + stack.shift();
y = yb + stack.shift();
bezierCurveTo(xa, ya, xb, yb, x, y);
}
break;
@ -374,19 +416,19 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
if (font.isCFFCIDFont) {
let fdIndex = font.fdSelect.getFDIndex(glyphId);
if (fdIndex >= 0 && fdIndex < font.fdArray.length) {
let fontDict = font.fdArray[fdIndex], subrs;
let fontDict = font.fdArray[fdIndex],
subrs;
if (fontDict.privateDict && fontDict.privateDict.subrsIndex) {
subrs = fontDict.privateDict.subrsIndex.objects;
}
if (subrs) {
let numSubrs = subrs.length;
// Add subroutine bias.
n += numSubrs < 1240 ? 107 :
(numSubrs < 33900 ? 1131 : 32768);
n += numSubrs < 1240 ? 107 : numSubrs < 33900 ? 1131 : 32768;
subrCode = subrs[n];
}
} else {
warn('Invalid fd index for glyph index.');
warn("Invalid fd index for glyph index.");
}
} else {
subrCode = font.subrs[n + font.subrsBias];
@ -402,7 +444,8 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
switch (v) {
case 34: // flex
xa = x + stack.shift();
xb = xa + stack.shift(); y1 = y + stack.shift();
xb = xa + stack.shift();
y1 = y + stack.shift();
x = xb + stack.shift();
bezierCurveTo(xa, y, xb, y1, x, y1);
xa = x + stack.shift();
@ -411,35 +454,51 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
bezierCurveTo(xa, y1, xb, y, x, y);
break;
case 35: // flex
xa = x + stack.shift(); ya = y + stack.shift();
xb = xa + stack.shift(); yb = ya + stack.shift();
x = xb + stack.shift(); y = yb + stack.shift();
xa = x + stack.shift();
ya = y + stack.shift();
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb + stack.shift();
y = yb + stack.shift();
bezierCurveTo(xa, ya, xb, yb, x, y);
xa = x + stack.shift(); ya = y + stack.shift();
xb = xa + stack.shift(); yb = ya + stack.shift();
x = xb + stack.shift(); y = yb + stack.shift();
xa = x + stack.shift();
ya = y + stack.shift();
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb + stack.shift();
y = yb + stack.shift();
bezierCurveTo(xa, ya, xb, yb, x, y);
stack.pop(); // fd
break;
case 36: // hflex1
xa = x + stack.shift(); y1 = y + stack.shift();
xb = xa + stack.shift(); y2 = y1 + stack.shift();
xa = x + stack.shift();
y1 = y + stack.shift();
xb = xa + stack.shift();
y2 = y1 + stack.shift();
x = xb + stack.shift();
bezierCurveTo(xa, y1, xb, y2, x, y2);
xa = x + stack.shift();
xb = xa + stack.shift(); y3 = y2 + stack.shift();
xb = xa + stack.shift();
y3 = y2 + stack.shift();
x = xb + stack.shift();
bezierCurveTo(xa, y2, xb, y3, x, y);
break;
case 37: // flex1
var x0 = x, y0 = y;
xa = x + stack.shift(); ya = y + stack.shift();
xb = xa + stack.shift(); yb = ya + stack.shift();
x = xb + stack.shift(); y = yb + stack.shift();
var x0 = x,
y0 = y;
xa = x + stack.shift();
ya = y + stack.shift();
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb + stack.shift();
y = yb + stack.shift();
bezierCurveTo(xa, ya, xb, yb, x, y);
xa = x + stack.shift(); ya = y + stack.shift();
xb = xa + stack.shift(); yb = ya + stack.shift();
x = xb; y = yb;
xa = x + stack.shift();
ya = y + stack.shift();
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb;
y = yb;
if (Math.abs(x - x0) > Math.abs(y - y0)) {
x += stack.shift();
} else {
@ -457,18 +516,30 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
var bchar = stack.pop();
y = stack.pop();
x = stack.pop();
cmds.push({ cmd: 'save', });
cmds.push({ cmd: 'translate', args: [x, y], });
var cmap = lookupCmap(font.cmap, String.fromCharCode(
font.glyphNameMap[StandardEncoding[achar]]));
compileCharString(font.glyphs[cmap.glyphId], cmds, font,
cmap.glyphId);
cmds.push({ cmd: 'restore', });
cmds.push({ cmd: "save" });
cmds.push({ cmd: "translate", args: [x, y] });
var cmap = lookupCmap(
font.cmap,
String.fromCharCode(font.glyphNameMap[StandardEncoding[achar]])
);
compileCharString(
font.glyphs[cmap.glyphId],
cmds,
font,
cmap.glyphId
);
cmds.push({ cmd: "restore" });
cmap = lookupCmap(font.cmap, String.fromCharCode(
font.glyphNameMap[StandardEncoding[bchar]]));
compileCharString(font.glyphs[cmap.glyphId], cmds, font,
cmap.glyphId);
cmap = lookupCmap(
font.cmap,
String.fromCharCode(font.glyphNameMap[StandardEncoding[bchar]])
);
compileCharString(
font.glyphs[cmap.glyphId],
cmds,
font,
cmap.glyphId
);
}
return;
case 18: // hstemhm
@ -502,9 +573,12 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
break;
case 24: // rcurveline
while (stack.length > 2) {
xa = x + stack.shift(); ya = y + stack.shift();
xb = xa + stack.shift(); yb = ya + stack.shift();
x = xb + stack.shift(); y = yb + stack.shift();
xa = x + stack.shift();
ya = y + stack.shift();
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb + stack.shift();
y = yb + stack.shift();
bezierCurveTo(xa, ya, xb, yb, x, y);
}
x += stack.shift();
@ -517,9 +591,12 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
y += stack.shift();
lineTo(x, y);
}
xa = x + stack.shift(); ya = y + stack.shift();
xb = xa + stack.shift(); yb = ya + stack.shift();
x = xb + stack.shift(); y = yb + stack.shift();
xa = x + stack.shift();
ya = y + stack.shift();
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb + stack.shift();
y = yb + stack.shift();
bezierCurveTo(xa, ya, xb, yb, x, y);
break;
case 26: // vvcurveto
@ -527,9 +604,12 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
x += stack.shift();
}
while (stack.length > 0) {
xa = x; ya = y + stack.shift();
xb = xa + stack.shift(); yb = ya + stack.shift();
x = xb; y = yb + stack.shift();
xa = x;
ya = y + stack.shift();
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb;
y = yb + stack.shift();
bezierCurveTo(xa, ya, xb, yb, x, y);
}
break;
@ -538,9 +618,12 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
y += stack.shift();
}
while (stack.length > 0) {
xa = x + stack.shift(); ya = y;
xb = xa + stack.shift(); yb = ya + stack.shift();
x = xb + stack.shift(); y = yb;
xa = x + stack.shift();
ya = y;
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb + stack.shift();
y = yb;
bezierCurveTo(xa, ya, xb, yb, x, y);
}
break;
@ -557,8 +640,10 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
break;
case 30: // vhcurveto
while (stack.length > 0) {
xa = x; ya = y + stack.shift();
xb = xa + stack.shift(); yb = ya + stack.shift();
xa = x;
ya = y + stack.shift();
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb + stack.shift();
y = yb + (stack.length === 1 ? stack.shift() : 0);
bezierCurveTo(xa, ya, xb, yb, x, y);
@ -566,8 +651,10 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
break;
}
xa = x + stack.shift(); ya = y;
xb = xa + stack.shift(); yb = ya + stack.shift();
xa = x + stack.shift();
ya = y;
xb = xa + stack.shift();
yb = ya + stack.shift();
y = yb + stack.shift();
x = xb + (stack.length === 1 ? stack.shift() : 0);
bezierCurveTo(xa, ya, xb, yb, x, y);
@ -575,8 +662,10 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
break;
case 31: // hvcurveto
while (stack.length > 0) {
xa = x + stack.shift(); ya = y;
xb = xa + stack.shift(); yb = ya + stack.shift();
xa = x + stack.shift();
ya = y;
xb = xa + stack.shift();
yb = ya + stack.shift();
y = yb + stack.shift();
x = xb + (stack.length === 1 ? stack.shift() : 0);
bezierCurveTo(xa, ya, xb, yb, x, y);
@ -584,8 +673,10 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
break;
}
xa = x; ya = y + stack.shift();
xb = xa + stack.shift(); yb = ya + stack.shift();
xa = x;
ya = y + stack.shift();
xb = xa + stack.shift();
yb = ya + stack.shift();
x = xb + stack.shift();
y = yb + (stack.length === 1 ? stack.shift() : 0);
bezierCurveTo(xa, ya, xb, yb, x, y);
@ -602,8 +693,13 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
} else if (v < 255) {
stack.push(-(v - 251) * 256 - code[i++] - 108);
} else {
stack.push(((code[i] << 24) | (code[i + 1] << 16) |
(code[i + 2] << 8) | code[i + 3]) / 65536);
stack.push(
((code[i] << 24) |
(code[i + 1] << 16) |
(code[i + 2] << 8) |
code[i + 3]) /
65536
);
i += 4;
}
break;
@ -621,7 +717,7 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
class CompiledFont {
constructor(fontMatrix) {
if (this.constructor === CompiledFont) {
unreachable('Cannot initialize CompiledFont.');
unreachable("Cannot initialize CompiledFont.");
}
this.fontMatrix = fontMatrix;
@ -654,32 +750,34 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
let fdIndex = this.fdSelect.getFDIndex(glyphId);
if (fdIndex >= 0 && fdIndex < this.fdArray.length) {
let fontDict = this.fdArray[fdIndex];
fontMatrix = fontDict.getByName('FontMatrix') || FONT_IDENTITY_MATRIX;
fontMatrix = fontDict.getByName("FontMatrix") || FONT_IDENTITY_MATRIX;
} else {
warn('Invalid fd index for glyph index.');
warn("Invalid fd index for glyph index.");
}
}
const cmds = [];
cmds.push({ cmd: 'save', });
cmds.push({ cmd: 'transform', args: fontMatrix.slice(), });
cmds.push({ cmd: 'scale', args: ['size', '-size'], });
cmds.push({ cmd: "save" });
cmds.push({ cmd: "transform", args: fontMatrix.slice() });
cmds.push({ cmd: "scale", args: ["size", "-size"] });
this.compileGlyphImpl(code, cmds, glyphId);
cmds.push({ cmd: 'restore', });
cmds.push({ cmd: "restore" });
return cmds;
}
compileGlyphImpl() {
unreachable('Children classes should implement this.');
unreachable("Children classes should implement this.");
}
hasBuiltPath(unicode) {
const cmap = lookupCmap(this.cmap, unicode);
return (this.compiledGlyphs[cmap.glyphId] !== undefined &&
this.compiledCharCodeToGlyphId[cmap.charCode] !== undefined);
return (
this.compiledGlyphs[cmap.glyphId] !== undefined &&
this.compiledCharCodeToGlyphId[cmap.charCode] !== undefined
);
}
}
@ -706,10 +804,18 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
this.cmap = cmap;
this.glyphNameMap = glyphNameMap || getGlyphsUnicode();
this.gsubrsBias = (this.gsubrs.length < 1240 ?
107 : (this.gsubrs.length < 33900 ? 1131 : 32768));
this.subrsBias = (this.subrs.length < 1240 ?
107 : (this.subrs.length < 33900 ? 1131 : 32768));
this.gsubrsBias =
this.gsubrs.length < 1240
? 107
: this.gsubrs.length < 33900
? 1131
: 32768;
this.subrsBias =
this.subrs.length < 1240
? 107
: this.subrs.length < 33900
? 1131
: 32768;
this.isCFFCIDFont = cffInfo.isCFFCIDFont;
this.fdSelect = cffInfo.fdSelect;
@ -731,36 +837,38 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
var offset = getLong(data, p + 8);
var length = getLong(data, p + 12);
switch (tag) {
case 'cmap':
case "cmap":
cmap = parseCmap(data, offset, offset + length);
break;
case 'glyf':
case "glyf":
glyf = data.subarray(offset, offset + length);
break;
case 'loca':
case "loca":
loca = data.subarray(offset, offset + length);
break;
case 'head':
case "head":
unitsPerEm = getUshort(data, offset + 18);
indexToLocFormat = getUshort(data, offset + 50);
break;
case 'CFF ':
case "CFF ":
cff = parseCff(data, offset, offset + length, seacAnalysisEnabled);
break;
}
}
if (glyf) {
var fontMatrix = (!unitsPerEm ? font.fontMatrix :
[1 / unitsPerEm, 0, 0, 1 / unitsPerEm, 0, 0]);
var fontMatrix = !unitsPerEm
? font.fontMatrix
: [1 / unitsPerEm, 0, 0, 1 / unitsPerEm, 0, 0];
return new TrueTypeCompiled(
parseGlyfTable(glyf, loca, indexToLocFormat), cmap, fontMatrix);
parseGlyfTable(glyf, loca, indexToLocFormat),
cmap,
fontMatrix
);
}
return new Type2Compiled(cff, cmap, font.fontMatrix, font.glyphNameMap);
},
};
})();
export {
FontRendererFactory,
};
export { FontRendererFactory };

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -13,12 +13,12 @@
* limitations under the License.
*/
import { assert, FormatError, ImageKind, info, warn } from '../shared/util';
import { isName, isStream, Name } from './primitives';
import { ColorSpace } from './colorspace';
import { DecodeStream } from './stream';
import { JpegStream } from './jpeg_stream';
import { JpxImage } from './jpx';
import { assert, FormatError, ImageKind, info, warn } from "../shared/util";
import { isName, isStream, Name } from "./primitives";
import { ColorSpace } from "./colorspace";
import { DecodeStream } from "./stream";
import { JpegStream } from "./jpeg_stream";
import { JpxImage } from "./jpx";
var PDFImage = (function PDFImageClosure() {
/**
@ -27,9 +27,11 @@ var PDFImage = (function PDFImageClosure() {
*/
function handleImageData(image, nativeDecoder) {
if (nativeDecoder && nativeDecoder.canDecode(image)) {
return nativeDecoder.decode(image).catch((reason) => {
warn('Native image decoding failed -- trying to recover: ' +
(reason && reason.message));
return nativeDecoder.decode(image).catch(reason => {
warn(
"Native image decoding failed -- trying to recover: " +
(reason && reason.message)
);
return image;
});
}
@ -43,7 +45,7 @@ var PDFImage = (function PDFImageClosure() {
function decodeAndClamp(value, addend, coefficient, max) {
value = addend + value * coefficient;
// Clamp the value to the range
return (value < 0 ? 0 : (value > max ? max : value));
return value < 0 ? 0 : value > max ? max : value;
}
/**
@ -58,11 +60,19 @@ var PDFImage = (function PDFImageClosure() {
*/
function resizeImageMask(src, bpc, w1, h1, w2, h2) {
var length = w2 * h2;
var dest = (bpc <= 8 ? new Uint8Array(length) :
(bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length)));
var dest =
bpc <= 8
? new Uint8Array(length)
: bpc <= 16
? new Uint16Array(length)
: new Uint32Array(length);
var xRatio = w1 / w2;
var yRatio = h1 / h2;
var i, j, py, newIndex = 0, oldIndex;
var i,
j,
py,
newIndex = 0,
oldIndex;
var xScaled = new Uint16Array(w2);
var w1Scanline = w1;
@ -79,15 +89,23 @@ var PDFImage = (function PDFImageClosure() {
return dest;
}
function PDFImage({ xref, res, image, isInline = false, smask = null,
mask = null, isMask = false, pdfFunctionFactory, }) {
function PDFImage({
xref,
res,
image,
isInline = false,
smask = null,
mask = null,
isMask = false,
pdfFunctionFactory,
}) {
this.image = image;
var dict = image.dict;
const filter = dict.get('Filter');
const filter = dict.get("Filter");
if (isName(filter)) {
switch (filter.name) {
case 'JPXDecode':
case "JPXDecode":
var jpxImage = new JpxImage();
jpxImage.parseImageProperties(image.stream);
image.stream.reset();
@ -97,7 +115,7 @@ var PDFImage = (function PDFImageClosure() {
image.bitsPerComponent = jpxImage.bitsPerComponent;
image.numComps = jpxImage.componentsCount;
break;
case 'JBIG2Decode':
case "JBIG2Decode":
image.bitsPerComponent = 1;
image.numComps = 1;
break;
@ -105,86 +123,103 @@ var PDFImage = (function PDFImageClosure() {
}
// TODO cache rendered images?
let width = dict.get('Width', 'W');
let height = dict.get('Height', 'H');
let width = dict.get("Width", "W");
let height = dict.get("Height", "H");
if ((Number.isInteger(image.width) && image.width > 0) &&
(Number.isInteger(image.height) && image.height > 0) &&
(image.width !== width || image.height !== height)) {
warn('PDFImage - using the Width/Height of the image data, ' +
'rather than the image dictionary.');
if (
Number.isInteger(image.width) &&
image.width > 0 &&
Number.isInteger(image.height) &&
image.height > 0 &&
(image.width !== width || image.height !== height)
) {
warn(
"PDFImage - using the Width/Height of the image data, " +
"rather than the image dictionary."
);
width = image.width;
height = image.height;
}
if (width < 1 || height < 1) {
throw new FormatError(`Invalid image width: ${width} or ` +
`height: ${height}`);
throw new FormatError(
`Invalid image width: ${width} or ` + `height: ${height}`
);
}
this.width = width;
this.height = height;
this.interpolate = dict.get('Interpolate', 'I') || false;
this.imageMask = dict.get('ImageMask', 'IM') || false;
this.matte = dict.get('Matte') || false;
this.interpolate = dict.get("Interpolate", "I") || false;
this.imageMask = dict.get("ImageMask", "IM") || false;
this.matte = dict.get("Matte") || false;
var bitsPerComponent = image.bitsPerComponent;
if (!bitsPerComponent) {
bitsPerComponent = dict.get('BitsPerComponent', 'BPC');
bitsPerComponent = dict.get("BitsPerComponent", "BPC");
if (!bitsPerComponent) {
if (this.imageMask) {
bitsPerComponent = 1;
} else {
throw new FormatError(
`Bits per component missing in image: ${this.imageMask}`);
`Bits per component missing in image: ${this.imageMask}`
);
}
}
}
this.bpc = bitsPerComponent;
if (!this.imageMask) {
var colorSpace = dict.get('ColorSpace', 'CS');
var colorSpace = dict.get("ColorSpace", "CS");
if (!colorSpace) {
info('JPX images (which do not require color spaces)');
info("JPX images (which do not require color spaces)");
switch (image.numComps) {
case 1:
colorSpace = Name.get('DeviceGray');
colorSpace = Name.get("DeviceGray");
break;
case 3:
colorSpace = Name.get('DeviceRGB');
colorSpace = Name.get("DeviceRGB");
break;
case 4:
colorSpace = Name.get('DeviceCMYK');
colorSpace = Name.get("DeviceCMYK");
break;
default:
throw new Error(`JPX images with ${image.numComps} ` +
'color components not supported.');
throw new Error(
`JPX images with ${image.numComps} ` +
"color components not supported."
);
}
}
let resources = isInline ? res : null;
this.colorSpace = ColorSpace.parse(colorSpace, xref, resources,
pdfFunctionFactory);
this.colorSpace = ColorSpace.parse(
colorSpace,
xref,
resources,
pdfFunctionFactory
);
this.numComps = this.colorSpace.numComps;
}
this.decode = dict.getArray('Decode', 'D');
this.decode = dict.getArray("Decode", "D");
this.needsDecode = false;
if (this.decode &&
((this.colorSpace &&
!this.colorSpace.isDefaultDecode(this.decode, bitsPerComponent)) ||
(isMask &&
!ColorSpace.isDefaultDecode(this.decode, /* numComps = */ 1)))) {
if (
this.decode &&
((this.colorSpace &&
!this.colorSpace.isDefaultDecode(this.decode, bitsPerComponent)) ||
(isMask &&
!ColorSpace.isDefaultDecode(this.decode, /* numComps = */ 1)))
) {
this.needsDecode = true;
// Do some preprocessing to avoid more math.
var max = (1 << bitsPerComponent) - 1;
this.decodeCoefficients = [];
this.decodeAddends = [];
const isIndexed = this.colorSpace && this.colorSpace.name === 'Indexed';
const isIndexed = this.colorSpace && this.colorSpace.name === "Indexed";
for (var i = 0, j = 0; i < this.decode.length; i += 2, ++j) {
var dmin = this.decode[i];
var dmax = this.decode[i + 1];
this.decodeCoefficients[j] = isIndexed ? ((dmax - dmin) / max) :
(dmax - dmin);
this.decodeAddends[j] = isIndexed ? dmin : (max * dmin);
this.decodeCoefficients[j] = isIndexed
? (dmax - dmin) / max
: dmax - dmin;
this.decodeAddends[j] = isIndexed ? dmin : max * dmin;
}
}
@ -198,9 +233,10 @@ var PDFImage = (function PDFImageClosure() {
});
} else if (mask) {
if (isStream(mask)) {
var maskDict = mask.dict, imageMask = maskDict.get('ImageMask', 'IM');
var maskDict = mask.dict,
imageMask = maskDict.get("ImageMask", "IM");
if (!imageMask) {
warn('Ignoring /Mask in image without /ImageMask.');
warn("Ignoring /Mask in image without /ImageMask.");
} else {
this.mask = new PDFImage({
xref,
@ -221,15 +257,21 @@ var PDFImage = (function PDFImageClosure() {
* Handles processing of image data and returns the Promise that is resolved
* with a PDFImage when the image is ready to be used.
*/
PDFImage.buildImage = function({ handler, xref, res, image, isInline = false,
nativeDecoder = null,
pdfFunctionFactory, }) {
PDFImage.buildImage = function({
handler,
xref,
res,
image,
isInline = false,
nativeDecoder = null,
pdfFunctionFactory,
}) {
var imagePromise = handleImageData(image, nativeDecoder);
var smaskPromise;
var maskPromise;
var smask = image.dict.get('SMask');
var mask = image.dict.get('Mask');
var smask = image.dict.get("SMask");
var mask = image.dict.get("Mask");
if (smask) {
smaskPromise = handleImageData(smask, nativeDecoder);
@ -242,7 +284,7 @@ var PDFImage = (function PDFImageClosure() {
} else if (Array.isArray(mask)) {
maskPromise = Promise.resolve(mask);
} else {
warn('Unsupported mask format.');
warn("Unsupported mask format.");
maskPromise = Promise.resolve(null);
}
} else {
@ -260,15 +302,25 @@ var PDFImage = (function PDFImageClosure() {
mask: maskData,
pdfFunctionFactory,
});
});
}
);
};
PDFImage.createMask = function({ imgArray, width, height,
imageIsFromDecodeStream, inverseDecode, }) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(imgArray instanceof Uint8ClampedArray,
'PDFImage.createMask: Unsupported "imgArray" type.');
PDFImage.createMask = function({
imgArray,
width,
height,
imageIsFromDecodeStream,
inverseDecode,
}) {
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
imgArray instanceof Uint8ClampedArray,
'PDFImage.createMask: Unsupported "imgArray" type.'
);
}
// |imgArray| might not contain full data for every pixel of the mask, so
// we need to distinguish between |computedLength| and |actualLength|.
@ -301,24 +353,28 @@ var PDFImage = (function PDFImageClosure() {
// in this thread can be relying on its contents.
if (inverseDecode) {
for (i = 0; i < actualLength; i++) {
data[i] ^= 0xFF;
data[i] ^= 0xff;
}
}
return { data, width, height, };
return { data, width, height };
};
PDFImage.prototype = {
get drawWidth() {
return Math.max(this.width,
this.smask && this.smask.width || 0,
this.mask && this.mask.width || 0);
return Math.max(
this.width,
(this.smask && this.smask.width) || 0,
(this.mask && this.mask.width) || 0
);
},
get drawHeight() {
return Math.max(this.height,
this.smask && this.smask.height || 0,
this.mask && this.mask.height || 0);
return Math.max(
this.height,
(this.smask && this.smask.height) || 0,
(this.mask && this.mask.height) || 0
);
},
decodeBuffer(buffer) {
@ -333,15 +389,19 @@ var PDFImage = (function PDFImageClosure() {
if (bpc === 1) {
// If the buffer needed decode that means it just needs to be inverted.
for (i = 0, ii = buffer.length; i < ii; i++) {
buffer[i] = +!(buffer[i]);
buffer[i] = +!buffer[i];
}
return;
}
var index = 0;
for (i = 0, ii = this.width * this.height; i < ii; i++) {
for (var j = 0; j < numComps; j++) {
buffer[index] = decodeAndClamp(buffer[index], decodeAddends[j],
decodeCoefficients[j], max);
buffer[index] = decodeAndClamp(
buffer[index],
decodeAddends[j],
decodeCoefficients[j],
max
);
index++;
}
}
@ -361,12 +421,18 @@ var PDFImage = (function PDFImageClosure() {
var length = width * height * numComps;
var bufferPos = 0;
var output = (bpc <= 8 ? new Uint8Array(length) :
(bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length)));
var output =
bpc <= 8
? new Uint8Array(length)
: bpc <= 16
? new Uint16Array(length)
: new Uint32Array(length);
var rowComps = width * numComps;
var max = (1 << bpc) - 1;
var i = 0, ii, buf;
var i = 0,
ii,
buf;
if (bpc === 1) {
// Optimization for reading 1 bpc images.
@ -416,7 +482,7 @@ var PDFImage = (function PDFImageClosure() {
var remainingBits = bits - bpc;
var value = buf >> remainingBits;
output[i] = (value < 0 ? 0 : (value > max ? max : value));
output[i] = value < 0 ? 0 : value > max ? max : value;
buf = buf & ((1 << remainingBits) - 1);
bits = remainingBits;
}
@ -425,10 +491,14 @@ var PDFImage = (function PDFImageClosure() {
},
fillOpacity(rgbaBuf, width, height, actualHeight, image) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(rgbaBuf instanceof Uint8ClampedArray,
'PDFImage.fillOpacity: Unsupported "rgbaBuf" type.');
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
rgbaBuf instanceof Uint8ClampedArray,
'PDFImage.fillOpacity: Unsupported "rgbaBuf" type.'
);
}
var smask = this.smask;
var mask = this.mask;
@ -440,8 +510,14 @@ var PDFImage = (function PDFImageClosure() {
alphaBuf = new Uint8ClampedArray(sw * sh);
smask.fillGrayBuffer(alphaBuf);
if (sw !== width || sh !== height) {
alphaBuf = resizeImageMask(alphaBuf, smask.bpc, sw, sh,
width, height);
alphaBuf = resizeImageMask(
alphaBuf,
smask.bpc,
sw,
sh,
width,
height
);
}
} else if (mask) {
if (mask instanceof PDFImage) {
@ -457,8 +533,14 @@ var PDFImage = (function PDFImageClosure() {
}
if (sw !== width || sh !== height) {
alphaBuf = resizeImageMask(alphaBuf, mask.bpc, sw, sh,
width, height);
alphaBuf = resizeImageMask(
alphaBuf,
mask.bpc,
sw,
sh,
width,
height
);
}
} else if (Array.isArray(mask)) {
// Color key mask: if any of the components are outside the range
@ -479,7 +561,7 @@ var PDFImage = (function PDFImageClosure() {
alphaBuf[i] = opacity;
}
} else {
throw new FormatError('Unknown mask format.');
throw new FormatError("Unknown mask format.");
}
}
@ -496,10 +578,14 @@ var PDFImage = (function PDFImageClosure() {
},
undoPreblend(buffer, width, height) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(buffer instanceof Uint8ClampedArray,
'PDFImage.undoPreblend: Unsupported "buffer" type.');
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
buffer instanceof Uint8ClampedArray,
'PDFImage.undoPreblend: Unsupported "buffer" type.'
);
}
var matte = this.smask && this.smask.matte;
if (!matte) {
@ -530,7 +616,8 @@ var PDFImage = (function PDFImageClosure() {
createImageData(forceRGBA = false) {
var drawWidth = this.drawWidth;
var drawHeight = this.drawHeight;
var imgData = { // other fields are filled in below
var imgData = {
// other fields are filled in below
width: drawWidth,
height: drawHeight,
kind: 0,
@ -555,14 +642,22 @@ var PDFImage = (function PDFImageClosure() {
// Similarly, if it is a 24-bit-per pixel RGB image without any
// complications, we avoid expanding by 1.333x to RGBA form.
var kind;
if (this.colorSpace.name === 'DeviceGray' && bpc === 1) {
if (this.colorSpace.name === "DeviceGray" && bpc === 1) {
kind = ImageKind.GRAYSCALE_1BPP;
} else if (this.colorSpace.name === 'DeviceRGB' && bpc === 8 &&
!this.needsDecode) {
} else if (
this.colorSpace.name === "DeviceRGB" &&
bpc === 8 &&
!this.needsDecode
) {
kind = ImageKind.RGB_24BPP;
}
if (kind && !this.smask && !this.mask &&
drawWidth === originalWidth && drawHeight === originalHeight) {
if (
kind &&
!this.smask &&
!this.mask &&
drawWidth === originalWidth &&
drawHeight === originalHeight
) {
imgData.kind = kind;
imgArray = this.getImageBytes(originalHeight * rowBytes);
@ -580,8 +675,10 @@ var PDFImage = (function PDFImageClosure() {
}
if (this.needsDecode) {
// Invert the buffer (which must be grayscale if we reached here).
assert(kind === ImageKind.GRAYSCALE_1BPP,
'PDFImage.createImageData: The image must be grayscale.');
assert(
kind === ImageKind.GRAYSCALE_1BPP,
"PDFImage.createImageData: The image must be grayscale."
);
var buffer = imgData.data;
for (var i = 0, ii = buffer.length; i < ii; i++) {
buffer[i] ^= 0xff;
@ -592,16 +689,20 @@ var PDFImage = (function PDFImageClosure() {
if (this.image instanceof JpegStream && !this.smask && !this.mask) {
let imageLength = originalHeight * rowBytes;
switch (this.colorSpace.name) {
case 'DeviceGray':
case "DeviceGray":
// Avoid truncating the image, since `JpegImage.getData`
// will expand the image data when `forceRGB === true`.
imageLength *= 3;
/* falls through */
case 'DeviceRGB':
case 'DeviceCMYK':
/* falls through */
case "DeviceRGB":
case "DeviceCMYK":
imgData.kind = ImageKind.RGB_24BPP;
imgData.data = this.getImageBytes(imageLength,
drawWidth, drawHeight, /* forceRGB = */ true);
imgData.data = this.getImageBytes(
imageLength,
drawWidth,
drawHeight,
/* forceRGB = */ true
);
return imgData;
}
}
@ -609,8 +710,8 @@ var PDFImage = (function PDFImageClosure() {
imgArray = this.getImageBytes(originalHeight * rowBytes);
// imgArray can be incomplete (e.g. after CCITT fax encoding).
var actualHeight = 0 | (imgArray.length / rowBytes *
drawHeight / originalHeight);
var actualHeight =
0 | (((imgArray.length / rowBytes) * drawHeight) / originalHeight);
var comps = this.getComponents(imgArray);
@ -629,16 +730,29 @@ var PDFImage = (function PDFImageClosure() {
maybeUndoPreblend = true;
// Color key masking (opacity) must be performed before decoding.
this.fillOpacity(imgData.data, drawWidth, drawHeight, actualHeight,
comps);
this.fillOpacity(
imgData.data,
drawWidth,
drawHeight,
actualHeight,
comps
);
}
if (this.needsDecode) {
this.decodeBuffer(comps);
}
this.colorSpace.fillRgb(imgData.data, originalWidth, originalHeight,
drawWidth, drawHeight, actualHeight, bpc, comps,
alpha01);
this.colorSpace.fillRgb(
imgData.data,
originalWidth,
originalHeight,
drawWidth,
drawHeight,
actualHeight,
bpc,
comps,
alpha01
);
if (maybeUndoPreblend) {
this.undoPreblend(imgData.data, drawWidth, actualHeight);
}
@ -647,15 +761,20 @@ var PDFImage = (function PDFImageClosure() {
},
fillGrayBuffer(buffer) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(buffer instanceof Uint8ClampedArray,
'PDFImage.fillGrayBuffer: Unsupported "buffer" type.');
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
buffer instanceof Uint8ClampedArray,
'PDFImage.fillGrayBuffer: Unsupported "buffer" type.'
);
}
var numComps = this.numComps;
if (numComps !== 1) {
throw new FormatError(
`Reading gray scale from a color image: ${numComps}`);
`Reading gray scale from a color image: ${numComps}`
);
}
var width = this.width;
@ -680,7 +799,7 @@ var PDFImage = (function PDFImageClosure() {
} else {
// scale to {0, 255}
for (i = 0; i < length; ++i) {
buffer[i] = (-comps[i]) & 255;
buffer[i] = -comps[i] & 255;
}
}
return;
@ -708,6 +827,4 @@ var PDFImage = (function PDFImageClosure() {
return PDFImage;
})();
export {
PDFImage,
};
export { PDFImage };

View file

@ -14,13 +14,18 @@
*/
/* eslint no-var: error */
import { ColorSpace } from './colorspace';
import { JpegStream } from './jpeg_stream';
import { Stream } from './stream';
import { ColorSpace } from "./colorspace";
import { JpegStream } from "./jpeg_stream";
import { Stream } from "./stream";
class NativeImageDecoder {
constructor({ xref, resources, handler, forceDataSchema = false,
pdfFunctionFactory, }) {
constructor({
xref,
resources,
handler,
forceDataSchema = false,
pdfFunctionFactory,
}) {
this.xref = xref;
this.resources = resources;
this.handler = handler;
@ -29,23 +34,36 @@ class NativeImageDecoder {
}
canDecode(image) {
return image instanceof JpegStream &&
NativeImageDecoder.isDecodable(image, this.xref, this.resources,
this.pdfFunctionFactory);
return (
image instanceof JpegStream &&
NativeImageDecoder.isDecodable(
image,
this.xref,
this.resources,
this.pdfFunctionFactory
)
);
}
decode(image) {
// For natively supported JPEGs send them to the main thread for decoding.
const dict = image.dict;
let colorSpace = dict.get('ColorSpace', 'CS');
colorSpace = ColorSpace.parse(colorSpace, this.xref, this.resources,
this.pdfFunctionFactory);
let colorSpace = dict.get("ColorSpace", "CS");
colorSpace = ColorSpace.parse(
colorSpace,
this.xref,
this.resources,
this.pdfFunctionFactory
);
return this.handler.sendWithPromise('JpegDecode', [
image.getIR(this.forceDataSchema), colorSpace.numComps
]).then(function({ data, width, height, }) {
return new Stream(data, 0, data.length, dict);
});
return this.handler
.sendWithPromise("JpegDecode", [
image.getIR(this.forceDataSchema),
colorSpace.numComps,
])
.then(function({ data, width, height }) {
return new Stream(data, 0, data.length, dict);
});
}
/**
@ -54,14 +72,20 @@ class NativeImageDecoder {
*/
static isSupported(image, xref, res, pdfFunctionFactory) {
const dict = image.dict;
if (dict.has('DecodeParms') || dict.has('DP')) {
if (dict.has("DecodeParms") || dict.has("DP")) {
return false;
}
const cs = ColorSpace.parse(dict.get('ColorSpace', 'CS'), xref, res,
pdfFunctionFactory);
const cs = ColorSpace.parse(
dict.get("ColorSpace", "CS"),
xref,
res,
pdfFunctionFactory
);
// isDefaultDecode() of DeviceGray and DeviceRGB needs no `bpc` argument.
return (cs.name === 'DeviceGray' || cs.name === 'DeviceRGB') &&
cs.isDefaultDecode(dict.getArray('Decode', 'D'));
return (
(cs.name === "DeviceGray" || cs.name === "DeviceRGB") &&
cs.isDefaultDecode(dict.getArray("Decode", "D"))
);
}
/**
@ -69,17 +93,21 @@ class NativeImageDecoder {
*/
static isDecodable(image, xref, res, pdfFunctionFactory) {
const dict = image.dict;
if (dict.has('DecodeParms') || dict.has('DP')) {
if (dict.has("DecodeParms") || dict.has("DP")) {
return false;
}
const cs = ColorSpace.parse(dict.get('ColorSpace', 'CS'), xref, res,
pdfFunctionFactory);
const bpc = dict.get('BitsPerComponent', 'BPC') || 1;
return (cs.numComps === 1 || cs.numComps === 3) &&
cs.isDefaultDecode(dict.getArray('Decode', 'D'), bpc);
const cs = ColorSpace.parse(
dict.get("ColorSpace", "CS"),
xref,
res,
pdfFunctionFactory
);
const bpc = dict.get("BitsPerComponent", "BPC") || 1;
return (
(cs.numComps === 1 || cs.numComps === 3) &&
cs.isDefaultDecode(dict.getArray("Decode", "D"), bpc)
);
}
}
export {
NativeImageDecoder,
};
export { NativeImageDecoder };

File diff suppressed because it is too large Load diff

View file

@ -13,10 +13,10 @@
* limitations under the License.
*/
import { isDict, isStream } from './primitives';
import { DecodeStream } from './stream';
import { Jbig2Image } from './jbig2';
import { shadow } from '../shared/util';
import { isDict, isStream } from "./primitives";
import { DecodeStream } from "./stream";
import { Jbig2Image } from "./jbig2";
import { shadow } from "../shared/util";
/**
* For JBIG2's we use a library to decode these images and
@ -34,10 +34,10 @@ let Jbig2Stream = (function Jbig2StreamClosure() {
Jbig2Stream.prototype = Object.create(DecodeStream.prototype);
Object.defineProperty(Jbig2Stream.prototype, 'bytes', {
Object.defineProperty(Jbig2Stream.prototype, "bytes", {
get() {
// If `this.maybeLength` is null, we'll get the entire stream.
return shadow(this, 'bytes', this.stream.getBytes(this.maybeLength));
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
},
configurable: true,
});
@ -55,19 +55,19 @@ let Jbig2Stream = (function Jbig2StreamClosure() {
let chunks = [];
if (isDict(this.params)) {
let globalsStream = this.params.get('JBIG2Globals');
let globalsStream = this.params.get("JBIG2Globals");
if (isStream(globalsStream)) {
let globals = globalsStream.getBytes();
chunks.push({ data: globals, start: 0, end: globals.length, });
chunks.push({ data: globals, start: 0, end: globals.length });
}
}
chunks.push({ data: this.bytes, start: 0, end: this.bytes.length, });
chunks.push({ data: this.bytes, start: 0, end: this.bytes.length });
let data = jbig2Image.parseChunks(chunks);
let dataLength = data.length;
// JBIG2 had black as 1 and white as 0, inverting the colors
for (let i = 0; i < dataLength; i++) {
data[i] ^= 0xFF;
data[i] ^= 0xff;
}
this.buffer = data;
this.bufferLength = dataLength;
@ -77,6 +77,4 @@ let Jbig2Stream = (function Jbig2StreamClosure() {
return Jbig2Stream;
})();
export {
Jbig2Stream,
};
export { Jbig2Stream };

View file

@ -13,10 +13,10 @@
* limitations under the License.
*/
import { createObjectURL, shadow } from '../shared/util';
import { DecodeStream } from './stream';
import { isDict } from './primitives';
import { JpegImage } from './jpg';
import { createObjectURL, shadow } from "../shared/util";
import { DecodeStream } from "./stream";
import { isDict } from "./primitives";
import { JpegImage } from "./jpg";
/**
* Depending on the type of JPEG a JpegStream is handled in different ways. For
@ -31,7 +31,8 @@ let JpegStream = (function JpegStreamClosure() {
// Note: this seems to mainly affect inline images.
let ch;
while ((ch = stream.getByte()) !== -1) {
if (ch === 0xFF) { // Find the first byte of the SOI marker (0xFFD8).
if (ch === 0xff) {
// Find the first byte of the SOI marker (0xFFD8).
stream.skip(-1); // Reset the stream position to the SOI.
break;
}
@ -46,10 +47,10 @@ let JpegStream = (function JpegStreamClosure() {
JpegStream.prototype = Object.create(DecodeStream.prototype);
Object.defineProperty(JpegStream.prototype, 'bytes', {
Object.defineProperty(JpegStream.prototype, "bytes", {
get: function JpegStream_bytes() {
// If `this.maybeLength` is null, we'll get the entire stream.
return shadow(this, 'bytes', this.stream.getBytes(this.maybeLength));
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
},
configurable: true,
});
@ -69,9 +70,9 @@ let JpegStream = (function JpegStreamClosure() {
};
// Checking if values need to be transformed before conversion.
let decodeArr = this.dict.getArray('Decode', 'D');
let decodeArr = this.dict.getArray("Decode", "D");
if (this.forceRGB && Array.isArray(decodeArr)) {
let bitsPerComponent = this.dict.get('BitsPerComponent') || 8;
let bitsPerComponent = this.dict.get("BitsPerComponent") || 8;
let decodeArrLength = decodeArr.length;
let transform = new Int32Array(decodeArrLength);
let transformNeeded = false;
@ -89,7 +90,7 @@ let JpegStream = (function JpegStreamClosure() {
}
// Fetching the 'ColorTransform' entry, if it exists.
if (isDict(this.params)) {
let colorTransform = this.params.get('ColorTransform');
let colorTransform = this.params.get("ColorTransform");
if (Number.isInteger(colorTransform)) {
jpegOptions.colorTransform = colorTransform;
}
@ -109,12 +110,10 @@ let JpegStream = (function JpegStreamClosure() {
};
JpegStream.prototype.getIR = function(forceDataSchema = false) {
return createObjectURL(this.bytes, 'image/jpeg', forceDataSchema);
return createObjectURL(this.bytes, "image/jpeg", forceDataSchema);
};
return JpegStream;
})();
export {
JpegStream,
};
export { JpegStream };

View file

@ -14,7 +14,7 @@
*/
/* eslint-disable no-multi-spaces */
import { assert, BaseException, warn } from '../shared/util';
import { assert, BaseException, warn } from "../shared/util";
class JpegError extends BaseException {
constructor(msg) {
@ -29,7 +29,7 @@ class DNLMarkerError extends BaseException {
}
}
class EOIMarkerError extends BaseException { }
class EOIMarkerError extends BaseException {}
/**
* This code was forked from https://github.com/notmasteryet/jpgjs.
@ -64,27 +64,32 @@ var JpegImage = (function JpegImageClosure() {
63
]);
var dctCos1 = 4017; // cos(pi/16)
var dctSin1 = 799; // sin(pi/16)
var dctCos3 = 3406; // cos(3*pi/16)
var dctSin3 = 2276; // sin(3*pi/16)
var dctCos6 = 1567; // cos(6*pi/16)
var dctSin6 = 3784; // sin(6*pi/16)
var dctSqrt2 = 5793; // sqrt(2)
var dctSqrt1d2 = 2896; // sqrt(2) / 2
var dctCos1 = 4017; // cos(pi/16)
var dctSin1 = 799; // sin(pi/16)
var dctCos3 = 3406; // cos(3*pi/16)
var dctSin3 = 2276; // sin(3*pi/16)
var dctCos6 = 1567; // cos(6*pi/16)
var dctSin6 = 3784; // sin(6*pi/16)
var dctSqrt2 = 5793; // sqrt(2)
var dctSqrt1d2 = 2896; // sqrt(2) / 2
function JpegImage({ decodeTransform = null, colorTransform = -1, } = {}) {
function JpegImage({ decodeTransform = null, colorTransform = -1 } = {}) {
this._decodeTransform = decodeTransform;
this._colorTransform = colorTransform;
}
function buildHuffmanTable(codeLengths, values) {
var k = 0, code = [], i, j, length = 16;
var k = 0,
code = [],
i,
j,
length = 16;
while (length > 0 && !codeLengths[length - 1]) {
length--;
}
code.push({ children: [], index: 0, });
var p = code[0], q;
code.push({ children: [], index: 0 });
var p = code[0],
q;
for (i = 0; i < length; i++) {
for (j = 0; j < codeLengths[i]; j++) {
p = code.pop();
@ -95,7 +100,7 @@ var JpegImage = (function JpegImageClosure() {
p.index++;
code.push(p);
while (code.length <= i) {
code.push(q = { children: [], index: 0, });
code.push((q = { children: [], index: 0 }));
p.children[p.index] = q.children;
p = q;
}
@ -103,7 +108,7 @@ var JpegImage = (function JpegImageClosure() {
}
if (i + 1 < length) {
// p here points to last code
code.push(q = { children: [], index: 0, });
code.push((q = { children: [], index: 0 }));
p.children[p.index] = q.children;
p = q;
}
@ -115,13 +120,24 @@ var JpegImage = (function JpegImageClosure() {
return 64 * ((component.blocksPerLine + 1) * row + col);
}
function decodeScan(data, offset, frame, components, resetInterval,
spectralStart, spectralEnd, successivePrev, successive,
parseDNLMarker = false) {
function decodeScan(
data,
offset,
frame,
components,
resetInterval,
spectralStart,
spectralEnd,
successivePrev,
successive,
parseDNLMarker = false
) {
var mcusPerLine = frame.mcusPerLine;
var progressive = frame.progressive;
var startOffset = offset, bitsData = 0, bitsCount = 0;
var startOffset = offset,
bitsData = 0,
bitsCount = 0;
function readBit() {
if (bitsCount > 0) {
@ -129,22 +145,28 @@ var JpegImage = (function JpegImageClosure() {
return (bitsData >> bitsCount) & 1;
}
bitsData = data[offset++];
if (bitsData === 0xFF) {
if (bitsData === 0xff) {
var nextByte = data[offset++];
if (nextByte) {
if (nextByte === 0xDC && parseDNLMarker) { // DNL == 0xFFDC
if (nextByte === 0xdc && parseDNLMarker) {
// DNL == 0xFFDC
offset += 2; // Skip data length.
const scanLines = (data[offset++] << 8) | data[offset++];
if (scanLines > 0 && scanLines !== frame.scanLines) {
throw new DNLMarkerError(
'Found DNL marker (0xFFDC) while parsing scan data', scanLines);
"Found DNL marker (0xFFDC) while parsing scan data",
scanLines
);
}
} else if (nextByte === 0xD9) { // EOI == 0xFFD9
} else if (nextByte === 0xd9) {
// EOI == 0xFFD9
throw new EOIMarkerError(
'Found EOI marker (0xFFD9) while parsing scan data');
"Found EOI marker (0xFFD9) while parsing scan data"
);
}
throw new JpegError(
`unexpected marker ${((bitsData << 8) | nextByte).toString(16)}`);
`unexpected marker ${((bitsData << 8) | nextByte).toString(16)}`
);
}
// unstuff 0
}
@ -156,11 +178,11 @@ var JpegImage = (function JpegImageClosure() {
var node = tree;
while (true) {
node = node[readBit()];
if (typeof node === 'number') {
if (typeof node === "number") {
return node;
}
if (typeof node !== 'object') {
throw new JpegError('invalid huffman sequence');
if (typeof node !== "object") {
throw new JpegError("invalid huffman sequence");
}
}
}
@ -188,11 +210,12 @@ var JpegImage = (function JpegImageClosure() {
function decodeBaseline(component, offset) {
var t = decodeHuffman(component.huffmanTableDC);
var diff = t === 0 ? 0 : receiveAndExtend(t);
component.blockData[offset] = (component.pred += diff);
component.blockData[offset] = component.pred += diff;
var k = 1;
while (k < 64) {
var rs = decodeHuffman(component.huffmanTableAC);
var s = rs & 15, r = rs >> 4;
var s = rs & 15,
r = rs >> 4;
if (s === 0) {
if (r < 15) {
break;
@ -209,8 +232,8 @@ var JpegImage = (function JpegImageClosure() {
function decodeDCFirst(component, offset) {
var t = decodeHuffman(component.huffmanTableDC);
var diff = t === 0 ? 0 : (receiveAndExtend(t) << successive);
component.blockData[offset] = (component.pred += diff);
var diff = t === 0 ? 0 : receiveAndExtend(t) << successive;
component.blockData[offset] = component.pred += diff;
}
function decodeDCSuccessive(component, offset) {
@ -223,10 +246,12 @@ var JpegImage = (function JpegImageClosure() {
eobrun--;
return;
}
var k = spectralStart, e = spectralEnd;
var k = spectralStart,
e = spectralEnd;
while (k <= e) {
var rs = decodeHuffman(component.huffmanTableAC);
var s = rs & 15, r = rs >> 4;
var s = rs & 15,
r = rs >> 4;
if (s === 0) {
if (r < 15) {
eobrun = receive(r) + (1 << r) - 1;
@ -243,7 +268,8 @@ var JpegImage = (function JpegImageClosure() {
}
}
var successiveACState = 0, successiveACNextValue;
var successiveACState = 0,
successiveACNextValue;
function decodeACSuccessive(component, offset) {
var k = spectralStart;
var e = spectralEnd;
@ -268,7 +294,7 @@ var JpegImage = (function JpegImageClosure() {
}
} else {
if (s !== 1) {
throw new JpegError('invalid ACn encoding');
throw new JpegError("invalid ACn encoding");
}
successiveACNextValue = receiveAndExtend(s);
successiveACState = r ? 2 : 3;
@ -339,7 +365,8 @@ var JpegImage = (function JpegImageClosure() {
decodeFn = decodeBaseline;
}
var mcu = 0, fileMarker;
var mcu = 0,
fileMarker;
var mcuExpected;
if (componentsLength === 1) {
mcuExpected = components[0].blocksPerLine * components[0].blocksPerColumn;
@ -350,8 +377,9 @@ var JpegImage = (function JpegImageClosure() {
var h, v;
while (mcu < mcuExpected) {
// reset interval stuff
var mcuToRead = resetInterval ?
Math.min(mcuExpected - mcu, resetInterval) : mcuExpected;
var mcuToRead = resetInterval
? Math.min(mcuExpected - mcu, resetInterval)
: mcuExpected;
for (i = 0; i < componentsLength; i++) {
components[i].pred = 0;
}
@ -388,16 +416,19 @@ var JpegImage = (function JpegImageClosure() {
} else if (fileMarker.invalid) {
// Some bad images seem to pad Scan blocks with e.g. zero bytes, skip
// past those to attempt to find a valid marker (fixes issue4090.pdf).
warn('decodeScan - unexpected MCU data, current marker is: ' +
fileMarker.invalid);
warn(
"decodeScan - unexpected MCU data, current marker is: " +
fileMarker.invalid
);
offset = fileMarker.offset;
}
var marker = fileMarker && fileMarker.marker;
if (!marker || marker <= 0xFF00) {
throw new JpegError('decodeScan - a valid marker was not found.');
if (!marker || marker <= 0xff00) {
throw new JpegError("decodeScan - a valid marker was not found.");
}
if (marker >= 0xFFD0 && marker <= 0xFFD7) { // RSTx
if (marker >= 0xffd0 && marker <= 0xffd7) {
// RSTx
offset += 2;
} else {
break;
@ -408,8 +439,10 @@ var JpegImage = (function JpegImageClosure() {
// Some images include more Scan blocks than expected, skip past those and
// attempt to find the next valid marker (fixes issue8182.pdf).
if (fileMarker && fileMarker.invalid) {
warn('decodeScan - unexpected Scan data, current marker is: ' +
fileMarker.invalid);
warn(
"decodeScan - unexpected Scan data, current marker is: " +
fileMarker.invalid
);
offset = fileMarker.offset;
}
@ -422,13 +455,14 @@ var JpegImage = (function JpegImageClosure() {
// IEEE Intl. Conf. on Acoustics, Speech & Signal Processing, 1989,
// 988-991.
function quantizeAndInverse(component, blockBufferOffset, p) {
var qt = component.quantizationTable, blockData = component.blockData;
var qt = component.quantizationTable,
blockData = component.blockData;
var v0, v1, v2, v3, v4, v5, v6, v7;
var p0, p1, p2, p3, p4, p5, p6, p7;
var t;
if (!qt) {
throw new JpegError('missing required Quantization Table.');
throw new JpegError("missing required Quantization Table.");
}
// inverse DCT on rows
@ -481,7 +515,7 @@ var JpegImage = (function JpegImageClosure() {
// stage 3
v0 = (v0 + v1 + 1) >> 1;
v1 = v0 - v1;
t = (v2 * dctSin6 + v3 * dctCos6 + 128) >> 8;
t = (v2 * dctSin6 + v3 * dctCos6 + 128) >> 8;
v2 = (v2 * dctCos6 - v3 * dctSin6 + 128) >> 8;
v3 = t;
v4 = (v4 + v6 + 1) >> 1;
@ -494,10 +528,10 @@ var JpegImage = (function JpegImageClosure() {
v3 = v0 - v3;
v1 = (v1 + v2 + 1) >> 1;
v2 = v1 - v2;
t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
v7 = t;
t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
v6 = t;
@ -515,7 +549,7 @@ var JpegImage = (function JpegImageClosure() {
// inverse DCT on columns
for (var col = 0; col < 8; ++col) {
p0 = p[col];
p1 = p[col + 8];
p1 = p[col + 8];
p2 = p[col + 16];
p3 = p[col + 24];
p4 = p[col + 32];
@ -527,9 +561,9 @@ var JpegImage = (function JpegImageClosure() {
if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {
t = (dctSqrt2 * p0 + 8192) >> 14;
// convert to 8 bit
t = (t < -2040) ? 0 : (t >= 2024) ? 255 : (t + 2056) >> 4;
t = t < -2040 ? 0 : t >= 2024 ? 255 : (t + 2056) >> 4;
blockData[blockBufferOffset + col] = t;
blockData[blockBufferOffset + col + 8] = t;
blockData[blockBufferOffset + col + 8] = t;
blockData[blockBufferOffset + col + 16] = t;
blockData[blockBufferOffset + col + 24] = t;
blockData[blockBufferOffset + col + 32] = t;
@ -554,7 +588,7 @@ var JpegImage = (function JpegImageClosure() {
// converting to UInt8 range later.
v0 = ((v0 + v1 + 1) >> 1) + 4112;
v1 = v0 - v1;
t = (v2 * dctSin6 + v3 * dctCos6 + 2048) >> 12;
t = (v2 * dctSin6 + v3 * dctCos6 + 2048) >> 12;
v2 = (v2 * dctCos6 - v3 * dctSin6 + 2048) >> 12;
v3 = t;
v4 = (v4 + v6 + 1) >> 1;
@ -567,10 +601,10 @@ var JpegImage = (function JpegImageClosure() {
v3 = v0 - v3;
v1 = (v1 + v2 + 1) >> 1;
v2 = v1 - v2;
t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
v7 = t;
t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
v6 = t;
@ -585,18 +619,18 @@ var JpegImage = (function JpegImageClosure() {
p4 = v3 - v4;
// convert to 8-bit integers
p0 = (p0 < 16) ? 0 : (p0 >= 4080) ? 255 : p0 >> 4;
p1 = (p1 < 16) ? 0 : (p1 >= 4080) ? 255 : p1 >> 4;
p2 = (p2 < 16) ? 0 : (p2 >= 4080) ? 255 : p2 >> 4;
p3 = (p3 < 16) ? 0 : (p3 >= 4080) ? 255 : p3 >> 4;
p4 = (p4 < 16) ? 0 : (p4 >= 4080) ? 255 : p4 >> 4;
p5 = (p5 < 16) ? 0 : (p5 >= 4080) ? 255 : p5 >> 4;
p6 = (p6 < 16) ? 0 : (p6 >= 4080) ? 255 : p6 >> 4;
p7 = (p7 < 16) ? 0 : (p7 >= 4080) ? 255 : p7 >> 4;
p0 = p0 < 16 ? 0 : p0 >= 4080 ? 255 : p0 >> 4;
p1 = p1 < 16 ? 0 : p1 >= 4080 ? 255 : p1 >> 4;
p2 = p2 < 16 ? 0 : p2 >= 4080 ? 255 : p2 >> 4;
p3 = p3 < 16 ? 0 : p3 >= 4080 ? 255 : p3 >> 4;
p4 = p4 < 16 ? 0 : p4 >= 4080 ? 255 : p4 >> 4;
p5 = p5 < 16 ? 0 : p5 >= 4080 ? 255 : p5 >> 4;
p6 = p6 < 16 ? 0 : p6 >= 4080 ? 255 : p6 >> 4;
p7 = p7 < 16 ? 0 : p7 >= 4080 ? 255 : p7 >> 4;
// store block data
blockData[blockBufferOffset + col] = p0;
blockData[blockBufferOffset + col + 8] = p1;
blockData[blockBufferOffset + col + 8] = p1;
blockData[blockBufferOffset + col + 16] = p2;
blockData[blockBufferOffset + col + 24] = p3;
blockData[blockBufferOffset + col + 32] = p4;
@ -632,7 +666,7 @@ var JpegImage = (function JpegImageClosure() {
return null; // Don't attempt to read non-existent data and just return.
}
var currentMarker = peekUint16(currentPos);
if (currentMarker >= 0xFFC0 && currentMarker <= 0xFFFE) {
if (currentMarker >= 0xffc0 && currentMarker <= 0xfffe) {
return {
invalid: null,
marker: currentMarker,
@ -640,7 +674,7 @@ var JpegImage = (function JpegImageClosure() {
};
}
var newMarker = peekUint16(newPos);
while (!(newMarker >= 0xFFC0 && newMarker <= 0xFFFE)) {
while (!(newMarker >= 0xffc0 && newMarker <= 0xfffe)) {
if (++newPos >= maxPos) {
return null; // Don't attempt to read non-existent data and just return.
}
@ -654,8 +688,7 @@ var JpegImage = (function JpegImageClosure() {
}
JpegImage.prototype = {
parse(data, { dnlScanLines = null, } = {}) {
parse(data, { dnlScanLines = null } = {}) {
function readUint16() {
var value = (data[offset] << 8) | data[offset + 1];
offset += 2;
@ -668,8 +701,10 @@ var JpegImage = (function JpegImageClosure() {
var fileMarker = findNextFileMarker(data, endOffset, offset);
if (fileMarker && fileMarker.invalid) {
warn('readDataBlock - incorrect length, current marker is: ' +
fileMarker.invalid);
warn(
"readDataBlock - incorrect length, current marker is: " +
fileMarker.invalid
);
endOffset = fileMarker.offset;
}
@ -683,15 +718,17 @@ var JpegImage = (function JpegImageClosure() {
var mcusPerColumn = Math.ceil(frame.scanLines / 8 / frame.maxV);
for (var i = 0; i < frame.components.length; i++) {
component = frame.components[i];
var blocksPerLine = Math.ceil(Math.ceil(frame.samplesPerLine / 8) *
component.h / frame.maxH);
var blocksPerColumn = Math.ceil(Math.ceil(frame.scanLines / 8) *
component.v / frame.maxV);
var blocksPerLine = Math.ceil(
(Math.ceil(frame.samplesPerLine / 8) * component.h) / frame.maxH
);
var blocksPerColumn = Math.ceil(
(Math.ceil(frame.scanLines / 8) * component.v) / frame.maxV
);
var blocksPerLineForMcu = mcusPerLine * component.h;
var blocksPerColumnForMcu = mcusPerColumn * component.v;
var blocksBufferSize = 64 * blocksPerColumnForMcu *
(blocksPerLineForMcu + 1);
var blocksBufferSize =
64 * blocksPerColumnForMcu * (blocksPerLineForMcu + 1);
component.blockData = new Int16Array(blocksBufferSize);
component.blocksPerLine = blocksPerLine;
component.blocksPerColumn = blocksPerColumn;
@ -706,56 +743,71 @@ var JpegImage = (function JpegImageClosure() {
var frame, resetInterval;
let numSOSMarkers = 0;
var quantizationTables = [];
var huffmanTablesAC = [], huffmanTablesDC = [];
var huffmanTablesAC = [],
huffmanTablesDC = [];
var fileMarker = readUint16();
if (fileMarker !== 0xFFD8) { // SOI (Start of Image)
throw new JpegError('SOI not found');
if (fileMarker !== 0xffd8) {
// SOI (Start of Image)
throw new JpegError("SOI not found");
}
fileMarker = readUint16();
markerLoop: while (fileMarker !== 0xFFD9) { // EOI (End of image)
markerLoop: while (fileMarker !== 0xffd9) {
// EOI (End of image)
var i, j, l;
switch (fileMarker) {
case 0xFFE0: // APP0 (Application Specific)
case 0xFFE1: // APP1
case 0xFFE2: // APP2
case 0xFFE3: // APP3
case 0xFFE4: // APP4
case 0xFFE5: // APP5
case 0xFFE6: // APP6
case 0xFFE7: // APP7
case 0xFFE8: // APP8
case 0xFFE9: // APP9
case 0xFFEA: // APP10
case 0xFFEB: // APP11
case 0xFFEC: // APP12
case 0xFFED: // APP13
case 0xFFEE: // APP14
case 0xFFEF: // APP15
case 0xFFFE: // COM (Comment)
case 0xffe0: // APP0 (Application Specific)
case 0xffe1: // APP1
case 0xffe2: // APP2
case 0xffe3: // APP3
case 0xffe4: // APP4
case 0xffe5: // APP5
case 0xffe6: // APP6
case 0xffe7: // APP7
case 0xffe8: // APP8
case 0xffe9: // APP9
case 0xffea: // APP10
case 0xffeb: // APP11
case 0xffec: // APP12
case 0xffed: // APP13
case 0xffee: // APP14
case 0xffef: // APP15
case 0xfffe: // COM (Comment)
var appData = readDataBlock();
if (fileMarker === 0xFFE0) {
if (appData[0] === 0x4A && appData[1] === 0x46 &&
appData[2] === 0x49 && appData[3] === 0x46 &&
appData[4] === 0) { // 'JFIF\x00'
if (fileMarker === 0xffe0) {
if (
appData[0] === 0x4a &&
appData[1] === 0x46 &&
appData[2] === 0x49 &&
appData[3] === 0x46 &&
appData[4] === 0
) {
// 'JFIF\x00'
jfif = {
version: { major: appData[5], minor: appData[6], },
version: { major: appData[5], minor: appData[6] },
densityUnits: appData[7],
xDensity: (appData[8] << 8) | appData[9],
yDensity: (appData[10] << 8) | appData[11],
thumbWidth: appData[12],
thumbHeight: appData[13],
thumbData: appData.subarray(14, 14 +
3 * appData[12] * appData[13]),
thumbData: appData.subarray(
14,
14 + 3 * appData[12] * appData[13]
),
};
}
}
// TODO APP1 - Exif
if (fileMarker === 0xFFEE) {
if (appData[0] === 0x41 && appData[1] === 0x64 &&
appData[2] === 0x6F && appData[3] === 0x62 &&
appData[4] === 0x65) { // 'Adobe'
if (fileMarker === 0xffee) {
if (
appData[0] === 0x41 &&
appData[1] === 0x64 &&
appData[2] === 0x6f &&
appData[3] === 0x62 &&
appData[4] === 0x65
) {
// 'Adobe'
adobe = {
version: (appData[5] << 8) | appData[6],
flags0: (appData[7] << 8) | appData[8],
@ -766,48 +818,52 @@ var JpegImage = (function JpegImageClosure() {
}
break;
case 0xFFDB: // DQT (Define Quantization Tables)
case 0xffdb: // DQT (Define Quantization Tables)
var quantizationTablesLength = readUint16();
var quantizationTablesEnd = quantizationTablesLength + offset - 2;
var z;
while (offset < quantizationTablesEnd) {
var quantizationTableSpec = data[offset++];
var tableData = new Uint16Array(64);
if ((quantizationTableSpec >> 4) === 0) { // 8 bit values
if (quantizationTableSpec >> 4 === 0) {
// 8 bit values
for (j = 0; j < 64; j++) {
z = dctZigZag[j];
tableData[z] = data[offset++];
}
} else if ((quantizationTableSpec >> 4) === 1) { // 16 bit values
} else if (quantizationTableSpec >> 4 === 1) {
// 16 bit values
for (j = 0; j < 64; j++) {
z = dctZigZag[j];
tableData[z] = readUint16();
}
} else {
throw new JpegError('DQT - invalid table spec');
throw new JpegError("DQT - invalid table spec");
}
quantizationTables[quantizationTableSpec & 15] = tableData;
}
break;
case 0xFFC0: // SOF0 (Start of Frame, Baseline DCT)
case 0xFFC1: // SOF1 (Start of Frame, Extended DCT)
case 0xFFC2: // SOF2 (Start of Frame, Progressive DCT)
case 0xffc0: // SOF0 (Start of Frame, Baseline DCT)
case 0xffc1: // SOF1 (Start of Frame, Extended DCT)
case 0xffc2: // SOF2 (Start of Frame, Progressive DCT)
if (frame) {
throw new JpegError('Only single frame JPEGs supported');
throw new JpegError("Only single frame JPEGs supported");
}
readUint16(); // skip data length
frame = {};
frame.extended = (fileMarker === 0xFFC1);
frame.progressive = (fileMarker === 0xFFC2);
frame.extended = fileMarker === 0xffc1;
frame.progressive = fileMarker === 0xffc2;
frame.precision = data[offset++];
const sofScanLines = readUint16();
frame.scanLines = dnlScanLines || sofScanLines;
frame.samplesPerLine = readUint16();
frame.components = [];
frame.componentIds = {};
var componentsCount = data[offset++], componentId;
var maxH = 0, maxV = 0;
var componentsCount = data[offset++],
componentId;
var maxH = 0,
maxV = 0;
for (i = 0; i < componentsCount; i++) {
componentId = data[offset];
var h = data[offset + 1] >> 4;
@ -833,14 +889,14 @@ var JpegImage = (function JpegImageClosure() {
prepareComponents(frame);
break;
case 0xFFC4: // DHT (Define Huffman Tables)
case 0xffc4: // DHT (Define Huffman Tables)
var huffmanLength = readUint16();
for (i = 2; i < huffmanLength;) {
for (i = 2; i < huffmanLength; ) {
var huffmanTableSpec = data[offset++];
var codeLengths = new Uint8Array(16);
var codeLengthSum = 0;
for (j = 0; j < 16; j++, offset++) {
codeLengthSum += (codeLengths[j] = data[offset]);
codeLengthSum += codeLengths[j] = data[offset];
}
var huffmanValues = new Uint8Array(codeLengthSum);
for (j = 0; j < codeLengthSum; j++, offset++) {
@ -848,27 +904,28 @@ var JpegImage = (function JpegImageClosure() {
}
i += 17 + codeLengthSum;
((huffmanTableSpec >> 4) === 0 ?
huffmanTablesDC : huffmanTablesAC)[huffmanTableSpec & 15] =
buildHuffmanTable(codeLengths, huffmanValues);
(huffmanTableSpec >> 4 === 0 ? huffmanTablesDC : huffmanTablesAC)[
huffmanTableSpec & 15
] = buildHuffmanTable(codeLengths, huffmanValues);
}
break;
case 0xFFDD: // DRI (Define Restart Interval)
case 0xffdd: // DRI (Define Restart Interval)
readUint16(); // skip data length
resetInterval = readUint16();
break;
case 0xFFDA: // SOS (Start of Scan)
case 0xffda: // SOS (Start of Scan)
// A DNL marker (0xFFDC), if it exists, is only allowed at the end
// of the first scan segment and may only occur once in an image.
// Furthermore, to prevent an infinite loop, do *not* attempt to
// parse DNL markers during re-parsing of the JPEG scan data.
const parseDNLMarker = (++numSOSMarkers) === 1 && !dnlScanLines;
const parseDNLMarker = ++numSOSMarkers === 1 && !dnlScanLines;
readUint16(); // scanLength
var selectorsCount = data[offset++];
var components = [], component;
var components = [],
component;
for (i = 0; i < selectorsCount; i++) {
var componentIndex = frame.componentIds[data[offset++]];
component = frame.components[componentIndex];
@ -881,16 +938,23 @@ var JpegImage = (function JpegImageClosure() {
var spectralEnd = data[offset++];
var successiveApproximation = data[offset++];
try {
var processed = decodeScan(data, offset,
frame, components, resetInterval,
spectralStart, spectralEnd,
successiveApproximation >> 4, successiveApproximation & 15,
parseDNLMarker);
var processed = decodeScan(
data,
offset,
frame,
components,
resetInterval,
spectralStart,
spectralEnd,
successiveApproximation >> 4,
successiveApproximation & 15,
parseDNLMarker
);
offset += processed;
} catch (ex) {
if (ex instanceof DNLMarkerError) {
warn(`${ex.message} -- attempting to re-parse the JPEG image.`);
return this.parse(data, { dnlScanLines: ex.scanLines, });
return this.parse(data, { dnlScanLines: ex.scanLines });
} else if (ex instanceof EOIMarkerError) {
warn(`${ex.message} -- ignoring the rest of the image data.`);
break markerLoop;
@ -899,20 +963,24 @@ var JpegImage = (function JpegImageClosure() {
}
break;
case 0xFFDC: // DNL (Define Number of Lines)
case 0xffdc: // DNL (Define Number of Lines)
// Ignore the marker, since it's being handled in `decodeScan`.
offset += 4;
break;
case 0xFFFF: // Fill bytes
if (data[offset] !== 0xFF) { // Avoid skipping a valid marker.
case 0xffff: // Fill bytes
if (data[offset] !== 0xff) {
// Avoid skipping a valid marker.
offset--;
}
break;
default:
if (data[offset - 3] === 0xFF &&
data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {
if (
data[offset - 3] === 0xff &&
data[offset - 2] >= 0xc0 &&
data[offset - 2] <= 0xfe
) {
// could be incorrect encoding -- last 0xFF byte of the previous
// block was eaten by the encoder
offset -= 3;
@ -920,18 +988,23 @@ var JpegImage = (function JpegImageClosure() {
}
let nextFileMarker = findNextFileMarker(data, offset - 2);
if (nextFileMarker && nextFileMarker.invalid) {
warn('JpegImage.parse - unexpected data, current marker is: ' +
nextFileMarker.invalid);
warn(
"JpegImage.parse - unexpected data, current marker is: " +
nextFileMarker.invalid
);
offset = nextFileMarker.offset;
break;
}
if (offset > (data.length - 2)) {
warn('JpegImage.parse - reached the end of the image data ' +
'without finding an EOI marker (0xFFD9).');
if (offset > data.length - 2) {
warn(
"JpegImage.parse - reached the end of the image data " +
"without finding an EOI marker (0xFFD9)."
);
break markerLoop;
}
throw new JpegError('JpegImage.parse - unknown marker: ' +
fileMarker.toString(16));
throw new JpegError(
"JpegImage.parse - unknown marker: " + fileMarker.toString(16)
);
}
fileMarker = readUint16();
}
@ -965,7 +1038,8 @@ var JpegImage = (function JpegImageClosure() {
},
_getLinearizedBlockData(width, height, isSourcePDF = false) {
var scaleX = this.width / width, scaleY = this.height / height;
var scaleX = this.width / width,
scaleY = this.height / height;
var component, componentScaleX, componentScaleY, blocksPerScanline;
var x, y, i, j, k;
@ -993,7 +1067,7 @@ var JpegImage = (function JpegImageClosure() {
// linearize the blocks of the component
for (y = 0; y < height; y++) {
j = 0 | (y * componentScaleY);
index = blocksPerScanline * (j & mask3LSB) | ((j & 7) << 3);
index = (blocksPerScanline * (j & mask3LSB)) | ((j & 7) << 3);
for (x = 0; x < width; x++) {
data[offset] = output[index + xScaleBlockOffset[x]];
offset += numComponents;
@ -1022,7 +1096,7 @@ var JpegImage = (function JpegImageClosure() {
}
if (transform) {
for (i = 0; i < dataLength;) {
for (i = 0; i < dataLength; ) {
for (j = 0, k = 0; j < numComponents; j++, i++, k += 2) {
data[i] = ((data[i] * transform[k]) >> 8) + transform[k + 1];
}
@ -1077,34 +1151,61 @@ var JpegImage = (function JpegImageClosure() {
Cr = data[i + 2];
k = data[i + 3];
data[offset++] = -122.67195406894 +
Cb * (-6.60635669420364e-5 * Cb + 0.000437130475926232 * Cr -
5.4080610064599e-5 * Y + 0.00048449797120281 * k -
0.154362151871126) +
Cr * (-0.000957964378445773 * Cr + 0.000817076911346625 * Y -
0.00477271405408747 * k + 1.53380253221734) +
Y * (0.000961250184130688 * Y - 0.00266257332283933 * k +
0.48357088451265) +
data[offset++] =
-122.67195406894 +
Cb *
(-6.60635669420364e-5 * Cb +
0.000437130475926232 * Cr -
5.4080610064599e-5 * Y +
0.00048449797120281 * k -
0.154362151871126) +
Cr *
(-0.000957964378445773 * Cr +
0.000817076911346625 * Y -
0.00477271405408747 * k +
1.53380253221734) +
Y *
(0.000961250184130688 * Y -
0.00266257332283933 * k +
0.48357088451265) +
k * (-0.000336197177618394 * k + 0.484791561490776);
data[offset++] = 107.268039397724 +
Cb * (2.19927104525741e-5 * Cb - 0.000640992018297945 * Cr +
0.000659397001245577 * Y + 0.000426105652938837 * k -
0.176491792462875) +
Cr * (-0.000778269941513683 * Cr + 0.00130872261408275 * Y +
0.000770482631801132 * k - 0.151051492775562) +
Y * (0.00126935368114843 * Y - 0.00265090189010898 * k +
0.25802910206845) +
data[offset++] =
107.268039397724 +
Cb *
(2.19927104525741e-5 * Cb -
0.000640992018297945 * Cr +
0.000659397001245577 * Y +
0.000426105652938837 * k -
0.176491792462875) +
Cr *
(-0.000778269941513683 * Cr +
0.00130872261408275 * Y +
0.000770482631801132 * k -
0.151051492775562) +
Y *
(0.00126935368114843 * Y -
0.00265090189010898 * k +
0.25802910206845) +
k * (-0.000318913117588328 * k - 0.213742400323665);
data[offset++] = -20.810012546947 +
Cb * (-0.000570115196973677 * Cb - 2.63409051004589e-5 * Cr +
0.0020741088115012 * Y - 0.00288260236853442 * k +
0.814272968359295) +
Cr * (-1.53496057440975e-5 * Cr - 0.000132689043961446 * Y +
0.000560833691242812 * k - 0.195152027534049) +
Y * (0.00174418132927582 * Y - 0.00255243321439347 * k +
0.116935020465145) +
data[offset++] =
-20.810012546947 +
Cb *
(-0.000570115196973677 * Cb -
2.63409051004589e-5 * Cr +
0.0020741088115012 * Y -
0.00288260236853442 * k +
0.814272968359295) +
Cr *
(-1.53496057440975e-5 * Cr -
0.000132689043961446 * Y +
0.000560833691242812 * k -
0.195152027534049) +
Y *
(0.00174418132927582 * Y -
0.00255243321439347 * k +
0.116935020465145) +
k * (-0.000343531996510555 * k + 0.24165260232407);
}
// Ensure that only the converted RGB data is returned.
@ -1135,47 +1236,74 @@ var JpegImage = (function JpegImageClosure() {
y = data[i + 2] * scale;
k = data[i + 3] * scale;
data[offset++] = 255 +
c * (-4.387332384609988 * c + 54.48615194189176 * m +
18.82290502165302 * y + 212.25662451639585 * k -
285.2331026137004) +
m * (1.7149763477362134 * m - 5.6096736904047315 * y -
17.873870861415444 * k - 5.497006427196366) +
y * (-2.5217340131683033 * y - 21.248923337353073 * k +
17.5119270841813) -
data[offset++] =
255 +
c *
(-4.387332384609988 * c +
54.48615194189176 * m +
18.82290502165302 * y +
212.25662451639585 * k -
285.2331026137004) +
m *
(1.7149763477362134 * m -
5.6096736904047315 * y -
17.873870861415444 * k -
5.497006427196366) +
y *
(-2.5217340131683033 * y -
21.248923337353073 * k +
17.5119270841813) -
k * (21.86122147463605 * k + 189.48180835922747);
data[offset++] = 255 +
c * (8.841041422036149 * c + 60.118027045597366 * m +
6.871425592049007 * y + 31.159100130055922 * k -
79.2970844816548) +
m * (-15.310361306967817 * m + 17.575251261109482 * y +
131.35250912493976 * k - 190.9453302588951) +
y * (4.444339102852739 * y + 9.8632861493405 * k -
24.86741582555878) -
data[offset++] =
255 +
c *
(8.841041422036149 * c +
60.118027045597366 * m +
6.871425592049007 * y +
31.159100130055922 * k -
79.2970844816548) +
m *
(-15.310361306967817 * m +
17.575251261109482 * y +
131.35250912493976 * k -
190.9453302588951) +
y *
(4.444339102852739 * y + 9.8632861493405 * k - 24.86741582555878) -
k * (20.737325471181034 * k + 187.80453709719578);
data[offset++] = 255 +
c * (0.8842522430003296 * c + 8.078677503112928 * m +
30.89978309703729 * y - 0.23883238689178934 * k -
14.183576799673286) +
m * (10.49593273432072 * m + 63.02378494754052 * y +
50.606957656360734 * k - 112.23884253719248) +
y * (0.03296041114873217 * y + 115.60384449646641 * k -
193.58209356861505) -
data[offset++] =
255 +
c *
(0.8842522430003296 * c +
8.078677503112928 * m +
30.89978309703729 * y -
0.23883238689178934 * k -
14.183576799673286) +
m *
(10.49593273432072 * m +
63.02378494754052 * y +
50.606957656360734 * k -
112.23884253719248) +
y *
(0.03296041114873217 * y +
115.60384449646641 * k -
193.58209356861505) -
k * (22.33816807309886 * k + 180.12613974708367);
}
// Ensure that only the converted RGB data is returned.
return data.subarray(0, offset);
},
getData({ width, height, forceRGB = false, isSourcePDF = false, }) {
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('TESTING && !LIB')) {
assert(isSourcePDF === true,
'JpegImage.getData: Unexpected "isSourcePDF" value for PDF files.');
getData({ width, height, forceRGB = false, isSourcePDF = false }) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING && !LIB")) {
assert(
isSourcePDF === true,
'JpegImage.getData: Unexpected "isSourcePDF" value for PDF files.'
);
}
if (this.numComponents > 4) {
throw new JpegError('Unsupported color mode');
throw new JpegError("Unsupported color mode");
}
// Type of data: Uint8ClampedArray(width * height * numComponents)
var data = this._getLinearizedBlockData(width, height, isSourcePDF);
@ -1210,6 +1338,4 @@ var JpegImage = (function JpegImageClosure() {
return JpegImage;
})();
export {
JpegImage,
};
export { JpegImage };

File diff suppressed because it is too large Load diff

View file

@ -13,9 +13,9 @@
* limitations under the License.
*/
import { DecodeStream } from './stream';
import { JpxImage } from './jpx';
import { shadow } from '../shared/util';
import { DecodeStream } from "./stream";
import { JpxImage } from "./jpx";
import { shadow } from "../shared/util";
/**
* For JPEG 2000's we use a library to decode these images and
@ -33,10 +33,10 @@ let JpxStream = (function JpxStreamClosure() {
JpxStream.prototype = Object.create(DecodeStream.prototype);
Object.defineProperty(JpxStream.prototype, 'bytes', {
Object.defineProperty(JpxStream.prototype, "bytes", {
get: function JpxStream_bytes() {
// If `this.maybeLength` is null, we'll get the entire stream.
return shadow(this, 'bytes', this.stream.getBytes(this.maybeLength));
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
},
configurable: true,
});
@ -91,6 +91,4 @@ let JpxStream = (function JpxStreamClosure() {
return JpxStream;
})();
export {
JpxStream,
};
export { JpxStream };

File diff suppressed because it is too large Load diff

View file

@ -18,7 +18,7 @@
*/
/* eslint no-var: error */
import { isArrayBuffer, isString } from '../shared/util';
import { isArrayBuffer, isString } from "../shared/util";
const SEED = 0xc3d2e1f0;
// Workaround for missing math precision in JS.
@ -49,35 +49,41 @@ class MurmurHash3_64 {
data = input;
length = data.byteLength;
} else {
throw new Error('Wrong data format in MurmurHash3_64_update. ' +
'Input must be a string or array.');
throw new Error(
"Wrong data format in MurmurHash3_64_update. " +
"Input must be a string or array."
);
}
const blockCounts = length >> 2;
const tailLength = length - blockCounts * 4;
// We don't care about endianness here.
const dataUint32 = new Uint32Array(data.buffer, 0, blockCounts);
let k1 = 0, k2 = 0;
let h1 = this.h1, h2 = this.h2;
const C1 = 0xcc9e2d51, C2 = 0x1b873593;
const C1_LOW = C1 & MASK_LOW, C2_LOW = C2 & MASK_LOW;
let k1 = 0,
k2 = 0;
let h1 = this.h1,
h2 = this.h2;
const C1 = 0xcc9e2d51,
C2 = 0x1b873593;
const C1_LOW = C1 & MASK_LOW,
C2_LOW = C2 & MASK_LOW;
for (let i = 0; i < blockCounts; i++) {
if (i & 1) {
k1 = dataUint32[i];
k1 = (k1 * C1 & MASK_HIGH) | (k1 * C1_LOW & MASK_LOW);
k1 = k1 << 15 | k1 >>> 17;
k1 = (k1 * C2 & MASK_HIGH) | (k1 * C2_LOW & MASK_LOW);
k1 = ((k1 * C1) & MASK_HIGH) | ((k1 * C1_LOW) & MASK_LOW);
k1 = (k1 << 15) | (k1 >>> 17);
k1 = ((k1 * C2) & MASK_HIGH) | ((k1 * C2_LOW) & MASK_LOW);
h1 ^= k1;
h1 = h1 << 13 | h1 >>> 19;
h1 = (h1 << 13) | (h1 >>> 19);
h1 = h1 * 5 + 0xe6546b64;
} else {
k2 = dataUint32[i];
k2 = (k2 * C1 & MASK_HIGH) | (k2 * C1_LOW & MASK_LOW);
k2 = k2 << 15 | k2 >>> 17;
k2 = (k2 * C2 & MASK_HIGH) | (k2 * C2_LOW & MASK_LOW);
k2 = ((k2 * C1) & MASK_HIGH) | ((k2 * C1_LOW) & MASK_LOW);
k2 = (k2 << 15) | (k2 >>> 17);
k2 = ((k2 * C2) & MASK_HIGH) | ((k2 * C2_LOW) & MASK_LOW);
h2 ^= k2;
h2 = h2 << 13 | h2 >>> 19;
h2 = (h2 << 13) | (h2 >>> 19);
h2 = h2 * 5 + 0xe6546b64;
}
}
@ -87,17 +93,17 @@ class MurmurHash3_64 {
switch (tailLength) {
case 3:
k1 ^= data[blockCounts * 4 + 2] << 16;
/* falls through */
/* falls through */
case 2:
k1 ^= data[blockCounts * 4 + 1] << 8;
/* falls through */
/* falls through */
case 1:
k1 ^= data[blockCounts * 4];
/* falls through */
k1 = (k1 * C1 & MASK_HIGH) | (k1 * C1_LOW & MASK_LOW);
k1 = k1 << 15 | k1 >>> 17;
k1 = (k1 * C2 & MASK_HIGH) | (k1 * C2_LOW & MASK_LOW);
k1 = ((k1 * C1) & MASK_HIGH) | ((k1 * C1_LOW) & MASK_LOW);
k1 = (k1 << 15) | (k1 >>> 17);
k1 = ((k1 * C2) & MASK_HIGH) | ((k1 * C2_LOW) & MASK_LOW);
if (blockCounts & 1) {
h1 ^= k1;
} else {
@ -110,23 +116,25 @@ class MurmurHash3_64 {
}
hexdigest() {
let h1 = this.h1, h2 = this.h2;
let h1 = this.h1,
h2 = this.h2;
h1 ^= h2 >>> 1;
h1 = (h1 * 0xed558ccd & MASK_HIGH) | (h1 * 0x8ccd & MASK_LOW);
h2 = (h2 * 0xff51afd7 & MASK_HIGH) |
(((h2 << 16 | h1 >>> 16) * 0xafd7ed55 & MASK_HIGH) >>> 16);
h1 = ((h1 * 0xed558ccd) & MASK_HIGH) | ((h1 * 0x8ccd) & MASK_LOW);
h2 =
((h2 * 0xff51afd7) & MASK_HIGH) |
(((((h2 << 16) | (h1 >>> 16)) * 0xafd7ed55) & MASK_HIGH) >>> 16);
h1 ^= h2 >>> 1;
h1 = (h1 * 0x1a85ec53 & MASK_HIGH) | (h1 * 0xec53 & MASK_LOW);
h2 = (h2 * 0xc4ceb9fe & MASK_HIGH) |
(((h2 << 16 | h1 >>> 16) * 0xb9fe1a85 & MASK_HIGH) >>> 16);
h1 = ((h1 * 0x1a85ec53) & MASK_HIGH) | ((h1 * 0xec53) & MASK_LOW);
h2 =
((h2 * 0xc4ceb9fe) & MASK_HIGH) |
(((((h2 << 16) | (h1 >>> 16)) * 0xb9fe1a85) & MASK_HIGH) >>> 16);
h1 ^= h2 >>> 1;
const hex1 = (h1 >>> 0).toString(16), hex2 = (h2 >>> 0).toString(16);
return hex1.padStart(8, '0') + hex2.padStart(8, '0');
const hex1 = (h1 >>> 0).toString(16),
hex2 = (h2 >>> 0).toString(16);
return hex1.padStart(8, "0") + hex2.padStart(8, "0");
}
}
export {
MurmurHash3_64,
};
export { MurmurHash3_64 };

File diff suppressed because it is too large Load diff

View file

@ -14,14 +14,14 @@
*/
/* eslint-disable no-unsanitized/method */
import { assert, ImageKind, OPS } from '../shared/util';
import { assert, ImageKind, OPS } from "../shared/util";
var QueueOptimizer = (function QueueOptimizerClosure() {
function addState(parentState, pattern, checkFn, iterateFn, processFn) {
var state = parentState;
for (var i = 0, ii = pattern.length - 1; i < ii; i++) {
var item = pattern[i];
state = (state[item] || (state[item] = []));
state = state[item] || (state[item] = []);
}
state[pattern[pattern.length - 1]] = {
checkFn,
@ -30,8 +30,12 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
};
}
function handlePaintSolidColorImageMask(iFirstSave, count, fnArray,
argsArray) {
function handlePaintSolidColorImageMask(
iFirstSave,
count,
fnArray,
argsArray
) {
// Handles special case of mainly LaTeX documents which use image masks to
// draw lines with the current fill style.
// 'count' groups of (save, transform, paintImageMaskXObject, restore)+
@ -40,9 +44,13 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
for (var i = 0; i < count; i++) {
var arg = argsArray[iFirstPIMXO + 4 * i];
var imageMask = arg.length === 1 && arg[0];
if (imageMask && imageMask.width === 1 && imageMask.height === 1 &&
(!imageMask.data.length ||
(imageMask.data.length === 1 && imageMask.data[0] === 0))) {
if (
imageMask &&
imageMask.width === 1 &&
imageMask.height === 1 &&
(!imageMask.data.length ||
(imageMask.data.length === 1 && imageMask.data[0] === 0))
) {
fnArray[iFirstPIMXO + 4 * i] = OPS.paintSolidColorImageMask;
continue;
}
@ -55,7 +63,8 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
// This replaces (save, transform, paintInlineImageXObject, restore)+
// sequences with one |paintInlineImageXObjectGroup| operation.
addState(InitialState,
addState(
InitialState,
[OPS.save, OPS.transform, OPS.paintInlineImageXObject, OPS.restore],
null,
function iterateInlineImageGroup(context, i) {
@ -80,23 +89,28 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var MAX_WIDTH = 1000;
var IMAGE_PADDING = 1;
var fnArray = context.fnArray, argsArray = context.argsArray;
var fnArray = context.fnArray,
argsArray = context.argsArray;
var curr = context.iCurr;
var iFirstSave = curr - 3;
var iFirstTransform = curr - 2;
var iFirstPIIXO = curr - 1;
var count = Math.min(Math.floor((i - iFirstSave) / 4),
MAX_IMAGES_IN_INLINE_IMAGES_BLOCK);
var count = Math.min(
Math.floor((i - iFirstSave) / 4),
MAX_IMAGES_IN_INLINE_IMAGES_BLOCK
);
if (count < MIN_IMAGES_IN_INLINE_IMAGES_BLOCK) {
return i - (i - iFirstSave) % 4;
return i - ((i - iFirstSave) % 4);
}
// assuming that heights of those image is too small (~1 pixel)
// packing as much as possible by lines
var maxX = 0;
var map = [], maxLineHeight = 0;
var currentX = IMAGE_PADDING, currentY = IMAGE_PADDING;
var map = [],
maxLineHeight = 0;
var currentX = IMAGE_PADDING,
currentY = IMAGE_PADDING;
var q;
for (q = 0; q < count; q++) {
var transform = argsArray[iFirstTransform + (q << 2)];
@ -110,8 +124,10 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
}
map.push({
transform,
x: currentX, y: currentY,
w: img.width, h: img.height,
x: currentX,
y: currentY,
w: img.width,
h: img.height,
});
currentX += img.width + 2 * IMAGE_PADDING;
maxLineHeight = Math.max(maxLineHeight, img.height);
@ -148,17 +164,25 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
// Replace queue items.
fnArray.splice(iFirstSave, count * 4, OPS.paintInlineImageXObjectGroup);
argsArray.splice(iFirstSave, count * 4,
[{ width: imgWidth, height: imgHeight, kind: ImageKind.RGBA_32BPP,
data: imgData, }, map]);
argsArray.splice(iFirstSave, count * 4, [
{
width: imgWidth,
height: imgHeight,
kind: ImageKind.RGBA_32BPP,
data: imgData,
},
map,
]);
return iFirstSave + 1;
});
}
);
// This replaces (save, transform, paintImageMaskXObject, restore)+
// sequences with one |paintImageMaskXObjectGroup| or one
// |paintImageMaskXObjectRepeat| operation.
addState(InitialState,
addState(
InitialState,
[OPS.save, OPS.transform, OPS.paintImageMaskXObject, OPS.restore],
null,
function iterateImageMaskGroup(context, i) {
@ -182,7 +206,8 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var MAX_IMAGES_IN_MASKS_BLOCK = 100;
var MAX_SAME_IMAGES_IN_MASKS_BLOCK = 1000;
var fnArray = context.fnArray, argsArray = context.argsArray;
var fnArray = context.fnArray,
argsArray = context.argsArray;
var curr = context.iCurr;
var iFirstSave = curr - 3;
var iFirstTransform = curr - 2;
@ -191,18 +216,24 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
// At this point, i is the index of the first op past the last valid
// quartet.
var count = Math.floor((i - iFirstSave) / 4);
count = handlePaintSolidColorImageMask(iFirstSave, count, fnArray,
argsArray);
count = handlePaintSolidColorImageMask(
iFirstSave,
count,
fnArray,
argsArray
);
if (count < MIN_IMAGES_IN_MASKS_BLOCK) {
return i - (i - iFirstSave) % 4;
return i - ((i - iFirstSave) % 4);
}
var q;
var isSameImage = false;
var iTransform, transformArgs;
var firstPIMXOArg0 = argsArray[iFirstPIMXO][0];
if (argsArray[iFirstTransform][1] === 0 &&
argsArray[iFirstTransform][2] === 0) {
if (
argsArray[iFirstTransform][1] === 0 &&
argsArray[iFirstTransform][2] === 0
) {
isSameImage = true;
var firstTransformArg0 = argsArray[iFirstTransform][0];
var firstTransformArg3 = argsArray[iFirstTransform][3];
@ -210,11 +241,13 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var iPIMXO = iFirstPIMXO + 4;
for (q = 1; q < count; q++, iTransform += 4, iPIMXO += 4) {
transformArgs = argsArray[iTransform];
if (argsArray[iPIMXO][0] !== firstPIMXOArg0 ||
transformArgs[0] !== firstTransformArg0 ||
transformArgs[1] !== 0 ||
transformArgs[2] !== 0 ||
transformArgs[3] !== firstTransformArg3) {
if (
argsArray[iPIMXO][0] !== firstPIMXOArg0 ||
transformArgs[0] !== firstTransformArg0 ||
transformArgs[1] !== 0 ||
transformArgs[2] !== 0 ||
transformArgs[3] !== firstTransformArg3
) {
if (q < MIN_IMAGES_IN_MASKS_BLOCK) {
isSameImage = false;
} else {
@ -231,23 +264,30 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
iTransform = iFirstTransform;
for (q = 0; q < count; q++, iTransform += 4) {
transformArgs = argsArray[iTransform];
positions[(q << 1)] = transformArgs[4];
positions[q << 1] = transformArgs[4];
positions[(q << 1) + 1] = transformArgs[5];
}
// Replace queue items.
fnArray.splice(iFirstSave, count * 4, OPS.paintImageMaskXObjectRepeat);
argsArray.splice(iFirstSave, count * 4,
[firstPIMXOArg0, firstTransformArg0, firstTransformArg3, positions]);
argsArray.splice(iFirstSave, count * 4, [
firstPIMXOArg0,
firstTransformArg0,
firstTransformArg3,
positions,
]);
} else {
count = Math.min(count, MAX_IMAGES_IN_MASKS_BLOCK);
var images = [];
for (q = 0; q < count; q++) {
transformArgs = argsArray[iFirstTransform + (q << 2)];
var maskParams = argsArray[iFirstPIMXO + (q << 2)][0];
images.push({ data: maskParams.data, width: maskParams.width,
height: maskParams.height,
transform: transformArgs, });
images.push({
data: maskParams.data,
width: maskParams.width,
height: maskParams.height,
transform: transformArgs,
});
}
// Replace queue items.
@ -256,21 +296,26 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
}
return iFirstSave + 1;
});
}
);
// This replaces (save, transform, paintImageXObject, restore)+ sequences
// with one paintImageXObjectRepeat operation, if the |transform| and
// |paintImageXObjectRepeat| ops are appropriate.
addState(InitialState,
addState(
InitialState,
[OPS.save, OPS.transform, OPS.paintImageXObject, OPS.restore],
function (context) {
function(context) {
var argsArray = context.argsArray;
var iFirstTransform = context.iCurr - 2;
return argsArray[iFirstTransform][1] === 0 &&
argsArray[iFirstTransform][2] === 0;
return (
argsArray[iFirstTransform][1] === 0 &&
argsArray[iFirstTransform][2] === 0
);
},
function iterateImageGroup(context, i) {
var fnArray = context.fnArray, argsArray = context.argsArray;
var fnArray = context.fnArray,
argsArray = context.argsArray;
var iFirstSave = context.iCurr - 3;
var pos = (i - iFirstSave) % 4;
switch (pos) {
@ -283,10 +328,12 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var iFirstTransform = context.iCurr - 2;
var firstTransformArg0 = argsArray[iFirstTransform][0];
var firstTransformArg3 = argsArray[iFirstTransform][3];
if (argsArray[i][0] !== firstTransformArg0 ||
argsArray[i][1] !== 0 ||
argsArray[i][2] !== 0 ||
argsArray[i][3] !== firstTransformArg3) {
if (
argsArray[i][0] !== firstTransformArg0 ||
argsArray[i][1] !== 0 ||
argsArray[i][2] !== 0 ||
argsArray[i][3] !== firstTransformArg3
) {
return false; // transforms don't match
}
return true;
@ -305,11 +352,12 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
}
throw new Error(`iterateImageGroup - invalid pos: ${pos}`);
},
function (context, i) {
function(context, i) {
var MIN_IMAGES_IN_BLOCK = 3;
var MAX_IMAGES_IN_BLOCK = 1000;
var fnArray = context.fnArray, argsArray = context.argsArray;
var fnArray = context.fnArray,
argsArray = context.argsArray;
var curr = context.iCurr;
var iFirstSave = curr - 3;
var iFirstTransform = curr - 2;
@ -320,10 +368,12 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
// At this point, i is the index of the first op past the last valid
// quartet.
var count = Math.min(Math.floor((i - iFirstSave) / 4),
MAX_IMAGES_IN_BLOCK);
var count = Math.min(
Math.floor((i - iFirstSave) / 4),
MAX_IMAGES_IN_BLOCK
);
if (count < MIN_IMAGES_IN_BLOCK) {
return i - (i - iFirstSave) % 4;
return i - ((i - iFirstSave) % 4);
}
// Extract the (x,y) positions from all of the matching transforms.
@ -331,27 +381,34 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var iTransform = iFirstTransform;
for (var q = 0; q < count; q++, iTransform += 4) {
var transformArgs = argsArray[iTransform];
positions[(q << 1)] = transformArgs[4];
positions[q << 1] = transformArgs[4];
positions[(q << 1) + 1] = transformArgs[5];
}
// Replace queue items.
var args = [firstPIXOArg0, firstTransformArg0, firstTransformArg3,
positions];
var args = [
firstPIXOArg0,
firstTransformArg0,
firstTransformArg3,
positions,
];
fnArray.splice(iFirstSave, count * 4, OPS.paintImageXObjectRepeat);
argsArray.splice(iFirstSave, count * 4, args);
return iFirstSave + 1;
});
}
);
// This replaces (beginText, setFont, setTextMatrix, showText, endText)+
// sequences with (beginText, setFont, (setTextMatrix, showText)+, endText)+
// sequences, if the font for each one is the same.
addState(InitialState,
addState(
InitialState,
[OPS.beginText, OPS.setFont, OPS.setTextMatrix, OPS.showText, OPS.endText],
null,
function iterateShowTextGroup(context, i) {
var fnArray = context.fnArray, argsArray = context.argsArray;
var fnArray = context.fnArray,
argsArray = context.argsArray;
var iFirstSave = context.iCurr - 4;
var pos = (i - iFirstSave) % 5;
switch (pos) {
@ -368,8 +425,10 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var iFirstSetFont = context.iCurr - 3;
var firstSetFontArg0 = argsArray[iFirstSetFont][0];
var firstSetFontArg1 = argsArray[iFirstSetFont][1];
if (argsArray[i][0] !== firstSetFontArg0 ||
argsArray[i][1] !== firstSetFontArg1) {
if (
argsArray[i][0] !== firstSetFontArg0 ||
argsArray[i][1] !== firstSetFontArg1
) {
return false; // fonts don't match
}
return true;
@ -378,11 +437,12 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
}
throw new Error(`iterateShowTextGroup - invalid pos: ${pos}`);
},
function (context, i) {
function(context, i) {
var MIN_CHARS_IN_BLOCK = 3;
var MAX_CHARS_IN_BLOCK = 1000;
var fnArray = context.fnArray, argsArray = context.argsArray;
var fnArray = context.fnArray,
argsArray = context.argsArray;
var curr = context.iCurr;
var iFirstBeginText = curr - 4;
var iFirstSetFont = curr - 3;
@ -394,23 +454,27 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
// At this point, i is the index of the first op past the last valid
// quintet.
var count = Math.min(Math.floor((i - iFirstBeginText) / 5),
MAX_CHARS_IN_BLOCK);
var count = Math.min(
Math.floor((i - iFirstBeginText) / 5),
MAX_CHARS_IN_BLOCK
);
if (count < MIN_CHARS_IN_BLOCK) {
return i - (i - iFirstBeginText) % 5;
return i - ((i - iFirstBeginText) % 5);
}
// If the preceding quintet is (<something>, setFont, setTextMatrix,
// showText, endText), include that as well. (E.g. <something> might be
// |dependency|.)
var iFirst = iFirstBeginText;
if (iFirstBeginText >= 4 &&
fnArray[iFirstBeginText - 4] === fnArray[iFirstSetFont] &&
fnArray[iFirstBeginText - 3] === fnArray[iFirstSetTextMatrix] &&
fnArray[iFirstBeginText - 2] === fnArray[iFirstShowText] &&
fnArray[iFirstBeginText - 1] === fnArray[iFirstEndText] &&
argsArray[iFirstBeginText - 4][0] === firstSetFontArg0 &&
argsArray[iFirstBeginText - 4][1] === firstSetFontArg1) {
if (
iFirstBeginText >= 4 &&
fnArray[iFirstBeginText - 4] === fnArray[iFirstSetFont] &&
fnArray[iFirstBeginText - 3] === fnArray[iFirstSetTextMatrix] &&
fnArray[iFirstBeginText - 2] === fnArray[iFirstShowText] &&
fnArray[iFirstBeginText - 1] === fnArray[iFirstEndText] &&
argsArray[iFirstBeginText - 4][0] === firstSetFontArg0 &&
argsArray[iFirstBeginText - 4][1] === firstSetFontArg1
) {
count++;
iFirst -= 5;
}
@ -424,7 +488,8 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
}
return iEndText + 1;
});
}
);
function QueueOptimizer(queue) {
this.queue = queue;
@ -442,10 +507,11 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
_optimize() {
// Process new fnArray item(s) chunk.
const fnArray = this.queue.fnArray;
let i = this.lastProcessed, ii = fnArray.length;
let i = this.lastProcessed,
ii = fnArray.length;
let state = this.state;
let match = this.match;
if (!state && !match && (i + 1 === ii) && !InitialState[fnArray[i]]) {
if (!state && !match && i + 1 === ii && !InitialState[fnArray[i]]) {
// Micro-optimization for the common case: last item is not
// optimizable, just skipping it.
this.lastProcessed = ii;
@ -529,9 +595,9 @@ var NullOptimizer = (function NullOptimizerClosure() {
this.queue.argsArray.push(args);
},
flush() { },
flush() {},
reset() { },
reset() {},
};
return NullOptimizer;
@ -545,7 +611,7 @@ var OperatorList = (function OperatorListClosure() {
this._streamSink = streamSink;
this.fnArray = [];
this.argsArray = [];
if (streamSink && intent !== 'oplist') {
if (streamSink && intent !== "oplist") {
this.optimizer = new QueueOptimizer(this);
} else {
this.optimizer = new NullOptimizer(this);
@ -572,7 +638,7 @@ var OperatorList = (function OperatorListClosure() {
* `this.length === 0` after flushing.
*/
get totalLength() {
return (this._totalLength + this.length);
return this._totalLength + this.length;
},
addOp(fn, args) {
@ -581,8 +647,10 @@ var OperatorList = (function OperatorListClosure() {
if (this._streamSink) {
if (this.weight >= CHUNK_SIZE) {
this.flush();
} else if (this.weight >= CHUNK_SIZE_ABOUT &&
(fn === OPS.restore || fn === OPS.endText)) {
} else if (
this.weight >= CHUNK_SIZE_ABOUT &&
(fn === OPS.restore || fn === OPS.endText)
) {
// heuristic to flush on boundary of restore or endText
this.flush();
}
@ -620,7 +688,7 @@ var OperatorList = (function OperatorListClosure() {
get _transfers() {
const transfers = [];
const { fnArray, argsArray, length, } = this;
const { fnArray, argsArray, length } = this;
for (let i = 0; i < length; i++) {
switch (fnArray[i]) {
case OPS.paintInlineImageXObject:
@ -628,10 +696,14 @@ var OperatorList = (function OperatorListClosure() {
case OPS.paintImageMaskXObject:
const arg = argsArray[i][0]; // first param in imgData
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(arg.data instanceof Uint8ClampedArray,
'OperatorList._transfers: Unsupported "arg.data" type.');
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
arg.data instanceof Uint8ClampedArray,
'OperatorList._transfers: Unsupported "arg.data" type.'
);
}
if (!arg.cached) {
transfers.push(arg.data.buffer);
@ -647,12 +719,16 @@ var OperatorList = (function OperatorListClosure() {
const length = this.length;
this._totalLength += length;
this._streamSink.enqueue({
fnArray: this.fnArray,
argsArray: this.argsArray,
lastChunk,
length,
}, 1, this._transfers);
this._streamSink.enqueue(
{
fnArray: this.fnArray,
argsArray: this.argsArray,
lastChunk,
length,
},
1,
this._transfers
);
this.dependencies = Object.create(null);
this.fnArray.length = 0;
@ -665,6 +741,4 @@ var OperatorList = (function OperatorListClosure() {
return OperatorList;
})();
export {
OperatorList,
};
export { OperatorList };

File diff suppressed because it is too large Load diff

View file

@ -15,11 +15,17 @@
/* eslint-disable no-multi-spaces */
import {
assert, FormatError, info, unreachable, UNSUPPORTED_FEATURES, Util, warn
} from '../shared/util';
import { ColorSpace } from './colorspace';
import { isStream } from './primitives';
import { MissingDataException } from './core_utils';
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,
@ -34,7 +40,7 @@ var ShadingType = {
var Pattern = (function PatternClosure() {
// Constructor should define this.getPattern
function Pattern() {
unreachable('should not call Pattern constructor');
unreachable("should not call Pattern constructor");
}
Pattern.prototype = {
@ -45,33 +51,50 @@ var Pattern = (function PatternClosure() {
},
};
Pattern.parseShading = function(shading, matrix, xref, res, handler,
pdfFunctionFactory) {
Pattern.parseShading = function(
shading,
matrix,
xref,
res,
handler,
pdfFunctionFactory
) {
var dict = isStream(shading) ? shading.dict : shading;
var type = dict.get('ShadingType');
var type = dict.get("ShadingType");
try {
switch (type) {
case ShadingType.AXIAL:
case ShadingType.RADIAL:
// Both radial and axial shadings are handled by RadialAxial shading.
return new Shadings.RadialAxial(dict, matrix, xref, res,
pdfFunctionFactory);
return new Shadings.RadialAxial(
dict,
matrix,
xref,
res,
pdfFunctionFactory
);
case ShadingType.FREE_FORM_MESH:
case ShadingType.LATTICE_FORM_MESH:
case ShadingType.COONS_PATCH_MESH:
case ShadingType.TENSOR_PATCH_MESH:
return new Shadings.Mesh(shading, matrix, xref, res,
pdfFunctionFactory);
return new Shadings.Mesh(
shading,
matrix,
xref,
res,
pdfFunctionFactory
);
default:
throw new FormatError('Unsupported ShadingType: ' + type);
throw new FormatError("Unsupported ShadingType: " + type);
}
} catch (ex) {
if (ex instanceof MissingDataException) {
throw ex;
}
handler.send('UnsupportedFeature',
{ featureId: UNSUPPORTED_FEATURES.shadingPattern, });
handler.send("UnsupportedFeature", {
featureId: UNSUPPORTED_FEATURES.shadingPattern,
});
warn(ex);
return new Shadings.Dummy();
}
@ -90,35 +113,39 @@ Shadings.SMALL_NUMBER = 1e-6;
Shadings.RadialAxial = (function RadialAxialClosure() {
function RadialAxial(dict, matrix, xref, res, pdfFunctionFactory) {
this.matrix = matrix;
this.coordsArr = dict.getArray('Coords');
this.shadingType = dict.get('ShadingType');
this.type = 'Pattern';
var cs = dict.get('ColorSpace', 'CS');
this.coordsArr = dict.getArray("Coords");
this.shadingType = dict.get("ShadingType");
this.type = "Pattern";
var cs = dict.get("ColorSpace", "CS");
cs = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
this.cs = cs;
const bbox = dict.getArray('BBox');
const bbox = dict.getArray("BBox");
if (Array.isArray(bbox) && bbox.length === 4) {
this.bbox = Util.normalizeRect(bbox);
} else {
this.bbox = null;
}
var t0 = 0.0, t1 = 1.0;
if (dict.has('Domain')) {
var domainArr = dict.getArray('Domain');
var t0 = 0.0,
t1 = 1.0;
if (dict.has("Domain")) {
var domainArr = dict.getArray("Domain");
t0 = domainArr[0];
t1 = domainArr[1];
}
var extendStart = false, extendEnd = false;
if (dict.has('Extend')) {
var extendArr = dict.getArray('Extend');
var extendStart = false,
extendEnd = false;
if (dict.has("Extend")) {
var extendArr = dict.getArray("Extend");
extendStart = extendArr[0];
extendEnd = extendArr[1];
}
if (this.shadingType === ShadingType.RADIAL &&
(!extendStart || !extendEnd)) {
if (
this.shadingType === ShadingType.RADIAL &&
(!extendStart || !extendEnd)
) {
// Radial gradient only currently works if either circle is fully within
// the other circle.
var x1 = this.coordsArr[0];
@ -128,16 +155,15 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
var y2 = this.coordsArr[4];
var r2 = this.coordsArr[5];
var distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if (r1 <= r2 + distance &&
r2 <= r1 + distance) {
warn('Unsupported radial gradient.');
if (r1 <= r2 + distance && r2 <= r1 + distance) {
warn("Unsupported radial gradient.");
}
}
this.extendStart = extendStart;
this.extendEnd = extendEnd;
var fnObj = dict.get('Function');
var fnObj = dict.get("Function");
var fn = pdfFunctionFactory.createFromArray(fnObj);
// 10 samples seems good enough for now, but probably won't work
@ -146,17 +172,18 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
const NUMBER_OF_SAMPLES = 10;
const step = (t1 - t0) / NUMBER_OF_SAMPLES;
var colorStops = this.colorStops = [];
var colorStops = (this.colorStops = []);
// Protect against bad domains.
if (t0 >= t1 || step <= 0) {
// Acrobat doesn't seem to handle these cases so we'll ignore for
// now.
info('Bad shading domain.');
info("Bad shading domain.");
return;
}
var color = new Float32Array(cs.numComps), ratio = new Float32Array(1);
var color = new Float32Array(cs.numComps),
ratio = new Float32Array(1);
var rgbColor;
for (let i = 0; i <= NUMBER_OF_SAMPLES; i++) {
ratio[0] = t0 + i * step;
@ -166,9 +193,9 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
colorStops.push([i / NUMBER_OF_SAMPLES, cssColor]);
}
var background = 'transparent';
if (dict.has('Background')) {
rgbColor = cs.getRgb(dict.get('Background'), 0);
var background = "transparent";
if (dict.has("Background")) {
rgbColor = cs.getRgb(dict.get("Background"), 0);
background = Util.makeCssRgb(rgbColor[0], rgbColor[1], rgbColor[2]);
}
@ -197,13 +224,13 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
p1 = [coordsArr[2], coordsArr[3]];
r0 = null;
r1 = null;
type = 'axial';
type = "axial";
} else if (shadingType === ShadingType.RADIAL) {
p0 = [coordsArr[0], coordsArr[1]];
p1 = [coordsArr[3], coordsArr[4]];
r0 = coordsArr[2];
r1 = coordsArr[5];
type = 'radial';
type = "radial";
} else {
unreachable(`getPattern type unknown: ${shadingType}`);
}
@ -219,7 +246,7 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
}
}
return ['RadialAxial', type, this.bbox, this.colorStops, p0, p1, r0, r1];
return ["RadialAxial", type, this.bbox, this.colorStops, p0, p1, r0, r1];
},
};
@ -238,8 +265,9 @@ Shadings.Mesh = (function MeshClosure() {
var numComps = context.numComps;
this.tmpCompsBuf = new Float32Array(numComps);
var csNumComps = context.colorSpace.numComps;
this.tmpCsCompsBuf = context.colorFn ? new Float32Array(csNumComps) :
this.tmpCompsBuf;
this.tmpCsCompsBuf = context.colorFn
? new Float32Array(csNumComps)
: this.tmpCompsBuf;
}
MeshStreamReader.prototype = {
get hasData() {
@ -262,16 +290,26 @@ Shadings.Mesh = (function MeshClosure() {
var bufferLength = this.bufferLength;
if (n === 32) {
if (bufferLength === 0) {
return ((this.stream.getByte() << 24) |
(this.stream.getByte() << 16) | (this.stream.getByte() << 8) |
this.stream.getByte()) >>> 0;
return (
((this.stream.getByte() << 24) |
(this.stream.getByte() << 16) |
(this.stream.getByte() << 8) |
this.stream.getByte()) >>>
0
);
}
buffer = (buffer << 24) | (this.stream.getByte() << 16) |
(this.stream.getByte() << 8) | this.stream.getByte();
buffer =
(buffer << 24) |
(this.stream.getByte() << 16) |
(this.stream.getByte() << 8) |
this.stream.getByte();
var nextByte = this.stream.getByte();
this.buffer = nextByte & ((1 << bufferLength) - 1);
return ((buffer << (8 - bufferLength)) |
((nextByte & 0xFF) >> bufferLength)) >>> 0;
return (
((buffer << (8 - bufferLength)) |
((nextByte & 0xff) >> bufferLength)) >>>
0
);
}
if (n === 8 && bufferLength === 0) {
return this.stream.getByte();
@ -297,18 +335,22 @@ Shadings.Mesh = (function MeshClosure() {
var xi = this.readBits(bitsPerCoordinate);
var yi = this.readBits(bitsPerCoordinate);
var decode = this.context.decode;
var scale = bitsPerCoordinate < 32 ? 1 / ((1 << bitsPerCoordinate) - 1) :
2.3283064365386963e-10; // 2 ^ -32
var scale =
bitsPerCoordinate < 32
? 1 / ((1 << bitsPerCoordinate) - 1)
: 2.3283064365386963e-10; // 2 ^ -32
return [
xi * scale * (decode[1] - decode[0]) + decode[0],
yi * scale * (decode[3] - decode[2]) + decode[2]
yi * scale * (decode[3] - decode[2]) + decode[2],
];
},
readComponents: function MeshStreamReader_readComponents() {
var numComps = this.context.numComps;
var bitsPerComponent = this.context.bitsPerComponent;
var scale = bitsPerComponent < 32 ? 1 / ((1 << bitsPerComponent) - 1) :
2.3283064365386963e-10; // 2 ^ -32
var scale =
bitsPerComponent < 32
? 1 / ((1 << bitsPerComponent) - 1)
: 2.3283064365386963e-10; // 2 ^ -32
var decode = this.context.decode;
var components = this.tmpCompsBuf;
for (var i = 0, j = 4; i < numComps; i++, j += 2) {
@ -333,9 +375,10 @@ Shadings.Mesh = (function MeshClosure() {
var f = reader.readFlag();
var coord = reader.readCoordinate();
var color = reader.readComponents();
if (verticesLeft === 0) { // ignoring flags if we started a triangle
if (verticesLeft === 0) {
// ignoring flags if we started a triangle
if (!(0 <= f && f <= 2)) {
throw new FormatError('Unknown type4 flag');
throw new FormatError("Unknown type4 flag");
}
switch (f) {
case 0:
@ -360,7 +403,7 @@ Shadings.Mesh = (function MeshClosure() {
reader.align();
}
mesh.figures.push({
type: 'triangles',
type: "triangles",
coords: new Int32Array(ps),
colors: new Int32Array(ps),
});
@ -378,7 +421,7 @@ Shadings.Mesh = (function MeshClosure() {
colors.push(color);
}
mesh.figures.push({
type: 'lattice',
type: "lattice",
coords: new Int32Array(ps),
colors: new Int32Array(ps),
verticesPerRow,
@ -394,9 +437,16 @@ Shadings.Mesh = (function MeshClosure() {
function buildB(count) {
var lut = [];
for (var i = 0; i <= count; i++) {
var t = i / count, t_ = 1 - t;
lut.push(new Float32Array([t_ * t_ * t_, 3 * t * t_ * t_,
3 * t * t * t_, t * t * t]));
var t = i / count,
t_ = 1 - t;
lut.push(
new Float32Array([
t_ * t_ * t_,
3 * t * t_ * t_,
3 * t * t * t_,
t * t * t,
])
);
}
return lut;
}
@ -411,37 +461,66 @@ Shadings.Mesh = (function MeshClosure() {
function buildFigureFromPatch(mesh, index) {
var figure = mesh.figures[index];
assert(figure.type === 'patch', 'Unexpected patch mesh figure');
assert(figure.type === "patch", "Unexpected patch mesh figure");
var coords = mesh.coords, colors = mesh.colors;
var coords = mesh.coords,
colors = mesh.colors;
var pi = figure.coords;
var ci = figure.colors;
var figureMinX = Math.min(coords[pi[0]][0], coords[pi[3]][0],
coords[pi[12]][0], coords[pi[15]][0]);
var figureMinY = Math.min(coords[pi[0]][1], coords[pi[3]][1],
coords[pi[12]][1], coords[pi[15]][1]);
var figureMaxX = Math.max(coords[pi[0]][0], coords[pi[3]][0],
coords[pi[12]][0], coords[pi[15]][0]);
var figureMaxY = Math.max(coords[pi[0]][1], coords[pi[3]][1],
coords[pi[12]][1], coords[pi[15]][1]);
var splitXBy = Math.ceil((figureMaxX - figureMinX) * TRIANGLE_DENSITY /
(mesh.bounds[2] - mesh.bounds[0]));
splitXBy = Math.max(MIN_SPLIT_PATCH_CHUNKS_AMOUNT,
Math.min(MAX_SPLIT_PATCH_CHUNKS_AMOUNT, splitXBy));
var splitYBy = Math.ceil((figureMaxY - figureMinY) * TRIANGLE_DENSITY /
(mesh.bounds[3] - mesh.bounds[1]));
splitYBy = Math.max(MIN_SPLIT_PATCH_CHUNKS_AMOUNT,
Math.min(MAX_SPLIT_PATCH_CHUNKS_AMOUNT, splitYBy));
var figureMinX = Math.min(
coords[pi[0]][0],
coords[pi[3]][0],
coords[pi[12]][0],
coords[pi[15]][0]
);
var figureMinY = Math.min(
coords[pi[0]][1],
coords[pi[3]][1],
coords[pi[12]][1],
coords[pi[15]][1]
);
var figureMaxX = Math.max(
coords[pi[0]][0],
coords[pi[3]][0],
coords[pi[12]][0],
coords[pi[15]][0]
);
var figureMaxY = Math.max(
coords[pi[0]][1],
coords[pi[3]][1],
coords[pi[12]][1],
coords[pi[15]][1]
);
var splitXBy = Math.ceil(
((figureMaxX - figureMinX) * TRIANGLE_DENSITY) /
(mesh.bounds[2] - mesh.bounds[0])
);
splitXBy = Math.max(
MIN_SPLIT_PATCH_CHUNKS_AMOUNT,
Math.min(MAX_SPLIT_PATCH_CHUNKS_AMOUNT, splitXBy)
);
var splitYBy = Math.ceil(
((figureMaxY - figureMinY) * TRIANGLE_DENSITY) /
(mesh.bounds[3] - mesh.bounds[1])
);
splitYBy = Math.max(
MIN_SPLIT_PATCH_CHUNKS_AMOUNT,
Math.min(MAX_SPLIT_PATCH_CHUNKS_AMOUNT, splitYBy)
);
var verticesPerRow = splitXBy + 1;
var figureCoords = new Int32Array((splitYBy + 1) * verticesPerRow);
var figureColors = new Int32Array((splitYBy + 1) * verticesPerRow);
var k = 0;
var cl = new Uint8Array(3), cr = new Uint8Array(3);
var c0 = colors[ci[0]], c1 = colors[ci[1]],
c2 = colors[ci[2]], c3 = colors[ci[3]];
var bRow = getB(splitYBy), bCol = getB(splitXBy);
var cl = new Uint8Array(3),
cr = new Uint8Array(3);
var c0 = colors[ci[0]],
c1 = colors[ci[1]],
c2 = colors[ci[2]],
c3 = colors[ci[3]];
var bRow = getB(splitYBy),
bCol = getB(splitXBy);
for (var row = 0; row <= splitYBy; row++) {
cl[0] = ((c0[0] * (splitYBy - row) + c2[0] * row) / splitYBy) | 0;
cl[1] = ((c0[1] * (splitYBy - row) + c2[1] * row) / splitYBy) | 0;
@ -452,11 +531,14 @@ Shadings.Mesh = (function MeshClosure() {
cr[2] = ((c1[2] * (splitYBy - row) + c3[2] * row) / splitYBy) | 0;
for (var col = 0; col <= splitXBy; col++, k++) {
if ((row === 0 || row === splitYBy) &&
(col === 0 || col === splitXBy)) {
if (
(row === 0 || row === splitYBy) &&
(col === 0 || col === splitXBy)
) {
continue;
}
var x = 0, y = 0;
var x = 0,
y = 0;
var q = 0;
for (var i = 0; i <= 3; i++) {
for (var j = 0; j <= 3; j++, q++) {
@ -485,7 +567,7 @@ Shadings.Mesh = (function MeshClosure() {
figureColors[verticesPerRow * splitYBy + splitXBy] = ci[3];
mesh.figures[index] = {
type: 'lattice',
type: "lattice",
coords: figureCoords,
colors: figureColors,
verticesPerRow,
@ -501,15 +583,15 @@ Shadings.Mesh = (function MeshClosure() {
while (reader.hasData) {
var f = reader.readFlag();
if (!(0 <= f && f <= 3)) {
throw new FormatError('Unknown type6 flag');
throw new FormatError("Unknown type6 flag");
}
var i, ii;
var pi = coords.length;
for (i = 0, ii = (f !== 0 ? 8 : 12); i < ii; i++) {
for (i = 0, ii = f !== 0 ? 8 : 12; i < ii; i++) {
coords.push(reader.readCoordinate());
}
var ci = colors.length;
for (i = 0, ii = (f !== 0 ? 2 : 4); i < ii; i++) {
for (i = 0, ii = f !== 0 ? 2 : 4; i < ii; i++) {
colors.push(reader.readComponents());
}
var tmp1, tmp2, tmp3, tmp4;
@ -559,50 +641,66 @@ Shadings.Mesh = (function MeshClosure() {
// set p11, p12, p21, p22
ps[5] = coords.length;
coords.push([
(-4 * coords[ps[0]][0] - coords[ps[15]][0] +
(-4 * coords[ps[0]][0] -
coords[ps[15]][0] +
6 * (coords[ps[4]][0] + coords[ps[1]][0]) -
2 * (coords[ps[12]][0] + coords[ps[3]][0]) +
3 * (coords[ps[13]][0] + coords[ps[7]][0])) / 9,
(-4 * coords[ps[0]][1] - coords[ps[15]][1] +
3 * (coords[ps[13]][0] + coords[ps[7]][0])) /
9,
(-4 * coords[ps[0]][1] -
coords[ps[15]][1] +
6 * (coords[ps[4]][1] + coords[ps[1]][1]) -
2 * (coords[ps[12]][1] + coords[ps[3]][1]) +
3 * (coords[ps[13]][1] + coords[ps[7]][1])) / 9
3 * (coords[ps[13]][1] + coords[ps[7]][1])) /
9,
]);
ps[6] = coords.length;
coords.push([
(-4 * coords[ps[3]][0] - coords[ps[12]][0] +
(-4 * coords[ps[3]][0] -
coords[ps[12]][0] +
6 * (coords[ps[2]][0] + coords[ps[7]][0]) -
2 * (coords[ps[0]][0] + coords[ps[15]][0]) +
3 * (coords[ps[4]][0] + coords[ps[14]][0])) / 9,
(-4 * coords[ps[3]][1] - coords[ps[12]][1] +
3 * (coords[ps[4]][0] + coords[ps[14]][0])) /
9,
(-4 * coords[ps[3]][1] -
coords[ps[12]][1] +
6 * (coords[ps[2]][1] + coords[ps[7]][1]) -
2 * (coords[ps[0]][1] + coords[ps[15]][1]) +
3 * (coords[ps[4]][1] + coords[ps[14]][1])) / 9
3 * (coords[ps[4]][1] + coords[ps[14]][1])) /
9,
]);
ps[9] = coords.length;
coords.push([
(-4 * coords[ps[12]][0] - coords[ps[3]][0] +
(-4 * coords[ps[12]][0] -
coords[ps[3]][0] +
6 * (coords[ps[8]][0] + coords[ps[13]][0]) -
2 * (coords[ps[0]][0] + coords[ps[15]][0]) +
3 * (coords[ps[11]][0] + coords[ps[1]][0])) / 9,
(-4 * coords[ps[12]][1] - coords[ps[3]][1] +
3 * (coords[ps[11]][0] + coords[ps[1]][0])) /
9,
(-4 * coords[ps[12]][1] -
coords[ps[3]][1] +
6 * (coords[ps[8]][1] + coords[ps[13]][1]) -
2 * (coords[ps[0]][1] + coords[ps[15]][1]) +
3 * (coords[ps[11]][1] + coords[ps[1]][1])) / 9
3 * (coords[ps[11]][1] + coords[ps[1]][1])) /
9,
]);
ps[10] = coords.length;
coords.push([
(-4 * coords[ps[15]][0] - coords[ps[0]][0] +
(-4 * coords[ps[15]][0] -
coords[ps[0]][0] +
6 * (coords[ps[11]][0] + coords[ps[14]][0]) -
2 * (coords[ps[12]][0] + coords[ps[3]][0]) +
3 * (coords[ps[2]][0] + coords[ps[8]][0])) / 9,
(-4 * coords[ps[15]][1] - coords[ps[0]][1] +
3 * (coords[ps[2]][0] + coords[ps[8]][0])) /
9,
(-4 * coords[ps[15]][1] -
coords[ps[0]][1] +
6 * (coords[ps[11]][1] + coords[ps[14]][1]) -
2 * (coords[ps[12]][1] + coords[ps[3]][1]) +
3 * (coords[ps[2]][1] + coords[ps[8]][1])) / 9
3 * (coords[ps[2]][1] + coords[ps[8]][1])) /
9,
]);
mesh.figures.push({
type: 'patch',
type: "patch",
coords: new Int32Array(ps), // making copies of ps and cs
colors: new Int32Array(cs),
});
@ -617,15 +715,15 @@ Shadings.Mesh = (function MeshClosure() {
while (reader.hasData) {
var f = reader.readFlag();
if (!(0 <= f && f <= 3)) {
throw new FormatError('Unknown type7 flag');
throw new FormatError("Unknown type7 flag");
}
var i, ii;
var pi = coords.length;
for (i = 0, ii = (f !== 0 ? 12 : 16); i < ii; i++) {
for (i = 0, ii = f !== 0 ? 12 : 16; i < ii; i++) {
coords.push(reader.readCoordinate());
}
var ci = colors.length;
for (i = 0, ii = (f !== 0 ? 2 : 4); i < ii; i++) {
for (i = 0, ii = f !== 0 ? 2 : 4; i < ii; i++) {
colors.push(reader.readComponents());
}
var tmp1, tmp2, tmp3, tmp4;
@ -673,7 +771,7 @@ Shadings.Mesh = (function MeshClosure() {
break;
}
mesh.figures.push({
type: 'patch',
type: "patch",
coords: new Int32Array(ps), // making copies of ps and cs
colors: new Int32Array(cs),
});
@ -681,10 +779,13 @@ Shadings.Mesh = (function MeshClosure() {
}
function updateBounds(mesh) {
var minX = mesh.coords[0][0], minY = mesh.coords[0][1],
maxX = minX, maxY = minY;
var minX = mesh.coords[0][0],
minY = mesh.coords[0][1],
maxX = minX,
maxY = minY;
for (var i = 1, ii = mesh.coords.length; i < ii; i++) {
var x = mesh.coords[i][0], y = mesh.coords[i][1];
var x = mesh.coords[i][0],
y = mesh.coords[i][1];
minX = minX > x ? x : minX;
minY = minY > y ? y : minY;
maxX = maxX < x ? x : maxX;
@ -717,7 +818,9 @@ Shadings.Mesh = (function MeshClosure() {
var figures = mesh.figures;
for (i = 0, ii = figures.length; i < ii; i++) {
var figure = figures[i], ps = figure.coords, cs = figure.colors;
var figure = figures[i],
ps = figure.coords,
cs = figure.colors;
for (j = 0, jj = ps.length; j < jj; j++) {
ps[j] *= 2;
cs[j] *= 3;
@ -727,25 +830,26 @@ Shadings.Mesh = (function MeshClosure() {
function Mesh(stream, matrix, xref, res, pdfFunctionFactory) {
if (!isStream(stream)) {
throw new FormatError('Mesh data is not a stream');
throw new FormatError("Mesh data is not a stream");
}
var dict = stream.dict;
this.matrix = matrix;
this.shadingType = dict.get('ShadingType');
this.type = 'Pattern';
const bbox = dict.getArray('BBox');
this.shadingType = dict.get("ShadingType");
this.type = "Pattern";
const bbox = dict.getArray("BBox");
if (Array.isArray(bbox) && bbox.length === 4) {
this.bbox = Util.normalizeRect(bbox);
} else {
this.bbox = null;
}
var cs = dict.get('ColorSpace', 'CS');
var cs = dict.get("ColorSpace", "CS");
cs = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
this.cs = cs;
this.background = dict.has('Background') ?
cs.getRgb(dict.get('Background'), 0) : null;
this.background = dict.has("Background")
? cs.getRgb(dict.get("Background"), 0)
: null;
var fnObj = dict.get('Function');
var fnObj = dict.get("Function");
var fn = fnObj ? pdfFunctionFactory.createFromArray(fnObj) : null;
this.coords = [];
@ -753,10 +857,10 @@ Shadings.Mesh = (function MeshClosure() {
this.figures = [];
var decodeContext = {
bitsPerCoordinate: dict.get('BitsPerCoordinate'),
bitsPerComponent: dict.get('BitsPerComponent'),
bitsPerFlag: dict.get('BitsPerFlag'),
decode: dict.getArray('Decode'),
bitsPerCoordinate: dict.get("BitsPerCoordinate"),
bitsPerComponent: dict.get("BitsPerComponent"),
bitsPerFlag: dict.get("BitsPerFlag"),
decode: dict.getArray("Decode"),
colorFn: fn,
colorSpace: cs,
numComps: fn ? 1 : cs.numComps,
@ -769,9 +873,9 @@ Shadings.Mesh = (function MeshClosure() {
decodeType4Shading(this, reader);
break;
case ShadingType.LATTICE_FORM_MESH:
var verticesPerRow = dict.get('VerticesPerRow') | 0;
var verticesPerRow = dict.get("VerticesPerRow") | 0;
if (verticesPerRow < 2) {
throw new FormatError('Invalid VerticesPerRow');
throw new FormatError("Invalid VerticesPerRow");
}
decodeType5Shading(this, reader, verticesPerRow);
break;
@ -784,7 +888,7 @@ Shadings.Mesh = (function MeshClosure() {
patchMesh = true;
break;
default:
unreachable('Unsupported mesh type.');
unreachable("Unsupported mesh type.");
break;
}
@ -803,8 +907,17 @@ Shadings.Mesh = (function MeshClosure() {
Mesh.prototype = {
getIR: function Mesh_getIR() {
return ['Mesh', this.shadingType, this.coords, this.colors, this.figures,
this.bounds, this.matrix, this.bbox, this.background];
return [
"Mesh",
this.shadingType,
this.coords,
this.colors,
this.figures,
this.bounds,
this.matrix,
this.bbox,
this.background,
];
},
};
@ -813,38 +926,42 @@ Shadings.Mesh = (function MeshClosure() {
Shadings.Dummy = (function DummyClosure() {
function Dummy() {
this.type = 'Pattern';
this.type = "Pattern";
}
Dummy.prototype = {
getIR: function Dummy_getIR() {
return ['Dummy'];
return ["Dummy"];
},
};
return Dummy;
})();
function getTilingPatternIR(operatorList, dict, args) {
let matrix = dict.getArray('Matrix');
let bbox = Util.normalizeRect(dict.getArray('BBox'));
let xstep = dict.get('XStep');
let ystep = dict.get('YStep');
let paintType = dict.get('PaintType');
let tilingType = dict.get('TilingType');
let matrix = dict.getArray("Matrix");
let bbox = Util.normalizeRect(dict.getArray("BBox"));
let xstep = dict.get("XStep");
let ystep = dict.get("YStep");
let paintType = dict.get("PaintType");
let tilingType = dict.get("TilingType");
// Ensure that the pattern has a non-zero width and height, to prevent errors
// in `pattern_helper.js` (fixes issue8330.pdf).
if ((bbox[2] - bbox[0]) === 0 || (bbox[3] - bbox[1]) === 0) {
if (bbox[2] - bbox[0] === 0 || bbox[3] - bbox[1] === 0) {
throw new FormatError(`Invalid getTilingPatternIR /BBox array: [${bbox}].`);
}
return [
'TilingPattern', args, operatorList, matrix, bbox, xstep, ystep,
paintType, tilingType
"TilingPattern",
args,
operatorList,
matrix,
bbox,
xstep,
ystep,
paintType,
tilingType,
];
}
export {
Pattern,
getTilingPatternIR,
};
export { Pattern, getTilingPatternIR };

View file

@ -14,17 +14,20 @@
*/
import {
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';
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";
class BasePdfManager {
constructor() {
if (this.constructor === BasePdfManager) {
unreachable('Cannot initialize BasePdfManager.');
unreachable("Cannot initialize BasePdfManager.");
}
}
@ -46,11 +49,11 @@ class BasePdfManager {
warn(`Invalid absolute docBaseUrl: "${this._docBaseUrl}".`);
}
}
return shadow(this, 'docBaseUrl', docBaseUrl);
return shadow(this, "docBaseUrl", docBaseUrl);
}
onLoadedStream() {
unreachable('Abstract method `onLoadedStream` called');
unreachable("Abstract method `onLoadedStream` called");
}
ensureDoc(prop, args) {
@ -78,19 +81,19 @@ class BasePdfManager {
}
async ensure(obj, prop, args) {
unreachable('Abstract method `ensure` called');
unreachable("Abstract method `ensure` called");
}
requestRange(begin, end) {
unreachable('Abstract method `requestRange` called');
unreachable("Abstract method `requestRange` called");
}
requestLoadedStream() {
unreachable('Abstract method `requestLoadedStream` called');
unreachable("Abstract method `requestLoadedStream` called");
}
sendProgressiveData(chunk) {
unreachable('Abstract method `sendProgressiveData` called');
unreachable("Abstract method `sendProgressiveData` called");
}
updatePassword(password) {
@ -98,7 +101,7 @@ class BasePdfManager {
}
terminate(reason) {
unreachable('Abstract method `terminate` called');
unreachable("Abstract method `terminate` called");
}
}
@ -118,7 +121,7 @@ class LocalPdfManager extends BasePdfManager {
async ensure(obj, prop, args) {
const value = obj[prop];
if (typeof value === 'function') {
if (typeof value === "function") {
return value.apply(obj, args);
}
return value;
@ -159,7 +162,7 @@ class NetworkPdfManager extends BasePdfManager {
async ensure(obj, prop, args) {
try {
const value = obj[prop];
if (typeof value === 'function') {
if (typeof value === "function") {
return value.apply(obj, args);
}
return value;
@ -181,7 +184,7 @@ class NetworkPdfManager extends BasePdfManager {
}
sendProgressiveData(chunk) {
this.streamManager.onReceiveData({ chunk, });
this.streamManager.onReceiveData({ chunk });
}
onLoadedStream() {
@ -193,7 +196,4 @@ class NetworkPdfManager extends BasePdfManager {
}
}
export {
LocalPdfManager,
NetworkPdfManager,
};
export { LocalPdfManager, NetworkPdfManager };

View file

@ -14,7 +14,7 @@
*/
/* uses XRef */
import { assert } from '../shared/util';
import { assert } from "../shared/util";
var EOF = {};
@ -29,7 +29,7 @@ var Name = (function NameClosure() {
Name.get = function Name_get(name) {
var nameValue = nameCache[name];
return (nameValue ? nameValue : (nameCache[name] = new Name(name)));
return nameValue ? nameValue : (nameCache[name] = new Name(name));
};
Name._clearCache = function() {
@ -50,7 +50,7 @@ var Cmd = (function CmdClosure() {
Cmd.get = function Cmd_get(cmd) {
var cmdValue = cmdCache[cmd];
return (cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd)));
return cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd));
};
Cmd._clearCache = function() {
@ -193,9 +193,9 @@ var Ref = (function RefClosure() {
};
Ref.get = function(num, gen) {
const key = (gen === 0 ? `${num}R` : `${num}R${gen}`);
const key = gen === 0 ? `${num}R` : `${num}R${gen}`;
const refValue = refCache[key];
return (refValue ? refValue : (refCache[key] = new Ref(num, gen)));
return refValue ? refValue : (refCache[key] = new Ref(num, gen));
};
Ref._clearCache = function() {
@ -266,7 +266,7 @@ var RefSetCache = (function RefSetCacheClosure() {
})();
function isEOF(v) {
return (v === EOF);
return v === EOF;
}
function isName(v, name) {
@ -278,8 +278,9 @@ function isCmd(v, cmd) {
}
function isDict(v, type) {
return v instanceof Dict &&
(type === undefined || isName(v.get('Type'), type));
return (
v instanceof Dict && (type === undefined || isName(v.get("Type"), type))
);
}
function isRef(v) {
@ -287,16 +288,20 @@ function isRef(v) {
}
function isRefsEqual(v1, v2) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(v1 instanceof Ref && v2 instanceof Ref,
'isRefsEqual: Both parameters should be `Ref`s.');
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
v1 instanceof Ref && v2 instanceof Ref,
"isRefsEqual: Both parameters should be `Ref`s."
);
}
return v1.num === v2.num && v1.gen === v2.gen;
}
function isStream(v) {
return typeof v === 'object' && v !== null && v.getBytes !== undefined;
return typeof v === "object" && v !== null && v.getBytes !== undefined;
}
function clearPrimitiveCaches() {

View file

@ -14,8 +14,8 @@
*/
/* eslint no-var: error */
import { FormatError, isSpace, shadow } from '../shared/util';
import { EOF } from './primitives';
import { FormatError, isSpace, shadow } from "../shared/util";
import { EOF } from "./primitives";
class PostScriptParser {
constructor(lexer) {
@ -43,7 +43,8 @@ class PostScriptParser {
return true;
}
throw new FormatError(
`Unexpected symbol: found ${this.token.type} expected ${type}.`);
`Unexpected symbol: found ${this.token.type} expected ${type}.`
);
}
parse() {
@ -79,7 +80,7 @@ class PostScriptParser {
// The true block is right after the 'if' so it just falls through on true
// else it jumps and skips the true block.
this.operators[conditionLocation] = this.operators.length;
this.operators[conditionLocation + 1] = 'jz';
this.operators[conditionLocation + 1] = "jz";
} else if (this.accept(PostScriptTokenTypes.LBRACE)) {
const jumpLocation = this.operators.length;
this.operators.push(null, null);
@ -89,12 +90,12 @@ class PostScriptParser {
this.expect(PostScriptTokenTypes.IFELSE);
// The jump is added at the end of the true block to skip the false block.
this.operators[jumpLocation] = this.operators.length;
this.operators[jumpLocation + 1] = 'j';
this.operators[jumpLocation + 1] = "j";
this.operators[conditionLocation] = endOfTrue;
this.operators[conditionLocation + 1] = 'jz';
this.operators[conditionLocation + 1] = "jz";
} else {
throw new FormatError('PS Function: error parsing conditional.');
throw new FormatError("PS Function: error parsing conditional.");
}
}
}
@ -122,28 +123,42 @@ const PostScriptToken = (function PostScriptTokenClosure() {
if (opValue) {
return opValue;
}
return opCache[op] = new PostScriptToken(PostScriptTokenTypes.OPERATOR,
op);
return (opCache[op] = new PostScriptToken(
PostScriptTokenTypes.OPERATOR,
op
));
}
static get LBRACE() {
return shadow(this, 'LBRACE',
new PostScriptToken(PostScriptTokenTypes.LBRACE, '{'));
return shadow(
this,
"LBRACE",
new PostScriptToken(PostScriptTokenTypes.LBRACE, "{")
);
}
static get RBRACE() {
return shadow(this, 'RBRACE',
new PostScriptToken(PostScriptTokenTypes.RBRACE, '}'));
return shadow(
this,
"RBRACE",
new PostScriptToken(PostScriptTokenTypes.RBRACE, "}")
);
}
static get IF() {
return shadow(this, 'IF',
new PostScriptToken(PostScriptTokenTypes.IF, 'IF'));
return shadow(
this,
"IF",
new PostScriptToken(PostScriptTokenTypes.IF, "IF")
);
}
static get IFELSE() {
return shadow(this, 'IFELSE',
new PostScriptToken(PostScriptTokenTypes.IFELSE, 'IFELSE'));
return shadow(
this,
"IFELSE",
new PostScriptToken(PostScriptTokenTypes.IFELSE, "IFELSE")
);
}
}
return PostScriptToken;
@ -172,10 +187,11 @@ class PostScriptLexer {
}
if (comment) {
if (ch === 0x0A || ch === 0x0D) {
if (ch === 0x0a || ch === 0x0d) {
comment = false;
}
} else if (ch === 0x25) { // '%'
} else if (ch === 0x25) {
// '%'
comment = true;
} else if (!isSpace(ch)) {
break;
@ -183,15 +199,27 @@ class PostScriptLexer {
ch = this.nextChar();
}
switch (ch | 0) {
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: // '0'-'4'
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: // '5'-'9'
case 0x2B: case 0x2D: case 0x2E: // '+', '-', '.'
return new PostScriptToken(PostScriptTokenTypes.NUMBER,
this.getNumber());
case 0x7B: // '{'
case 0x30:
case 0x31:
case 0x32:
case 0x33:
case 0x34: // '0'-'4'
case 0x35:
case 0x36:
case 0x37:
case 0x38:
case 0x39: // '5'-'9'
case 0x2b:
case 0x2d:
case 0x2e: // '+', '-', '.'
return new PostScriptToken(
PostScriptTokenTypes.NUMBER,
this.getNumber()
);
case 0x7b: // '{'
this.nextChar();
return PostScriptToken.LBRACE;
case 0x7D: // '}'
case 0x7d: // '}'
this.nextChar();
return PostScriptToken.RBRACE;
}
@ -200,15 +228,17 @@ class PostScriptLexer {
strBuf.length = 0;
strBuf[0] = String.fromCharCode(ch);
while ((ch = this.nextChar()) >= 0 && // and 'A'-'Z', 'a'-'z'
((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A))) {
while (
(ch = this.nextChar()) >= 0 && // and 'A'-'Z', 'a'-'z'
((ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a))
) {
strBuf.push(String.fromCharCode(ch));
}
const str = strBuf.join('');
const str = strBuf.join("");
switch (str.toLowerCase()) {
case 'if':
case "if":
return PostScriptToken.IF;
case 'ifelse':
case "ifelse":
return PostScriptToken.IFELSE;
default:
return PostScriptToken.getOperator(str);
@ -222,14 +252,18 @@ class PostScriptLexer {
strBuf[0] = String.fromCharCode(ch);
while ((ch = this.nextChar()) >= 0) {
if ((ch >= 0x30 && ch <= 0x39) || // '0'-'9'
ch === 0x2D || ch === 0x2E) { // '-', '.'
if (
(ch >= 0x30 && ch <= 0x39) || // '0'-'9'
ch === 0x2d ||
ch === 0x2e
) {
// '-', '.'
strBuf.push(String.fromCharCode(ch));
} else {
break;
}
}
const value = parseFloat(strBuf.join(''));
const value = parseFloat(strBuf.join(""));
if (isNaN(value)) {
throw new FormatError(`Invalid floating point number: ${value}`);
}
@ -237,7 +271,4 @@ class PostScriptLexer {
}
}
export {
PostScriptLexer,
PostScriptParser,
};
export { PostScriptLexer, PostScriptParser };

File diff suppressed because it is too large Load diff

View file

@ -20,17 +20,22 @@
*/
import {
FormatError, isSpace, stringToBytes, unreachable
} from '../shared/util';
import { isDict } from './primitives';
FormatError,
isSpace,
stringToBytes,
unreachable,
} from "../shared/util";
import { isDict } from "./primitives";
var Stream = (function StreamClosure() {
function Stream(arrayBuffer, start, length, dict) {
this.bytes = (arrayBuffer instanceof Uint8Array ?
arrayBuffer : new Uint8Array(arrayBuffer));
this.bytes =
arrayBuffer instanceof Uint8Array
? arrayBuffer
: new Uint8Array(arrayBuffer);
this.start = start || 0;
this.pos = this.start;
this.end = (start + length) || this.bytes.length;
this.end = start + length || this.bytes.length;
this.dict = dict;
}
@ -73,7 +78,7 @@ var Stream = (function StreamClosure() {
if (!length) {
let subarray = bytes.subarray(pos, strEnd);
// `this.bytes` is always a `Uint8Array` here.
return (forceClamped ? new Uint8ClampedArray(subarray) : subarray);
return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
}
var end = pos + length;
if (end > strEnd) {
@ -82,7 +87,7 @@ var Stream = (function StreamClosure() {
this.pos = end;
let subarray = bytes.subarray(pos, end);
// `this.bytes` is always a `Uint8Array` here.
return (forceClamped ? new Uint8ClampedArray(subarray) : subarray);
return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
},
peekByte: function Stream_peekByte() {
var peekedByte = this.getByte();
@ -208,7 +213,8 @@ var DecodeStream = (function DecodeStreamClosure() {
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
},
getBytes(length, forceClamped = false) {
var end, pos = this.pos;
var end,
pos = this.pos;
if (length) {
this.ensureBuffer(pos + length);
@ -231,8 +237,9 @@ var DecodeStream = (function DecodeStreamClosure() {
this.pos = end;
let subarray = this.buffer.subarray(pos, end);
// `this.buffer` is either a `Uint8Array` or `Uint8ClampedArray` here.
return (forceClamped && !(subarray instanceof Uint8ClampedArray) ?
new Uint8ClampedArray(subarray) : subarray);
return forceClamped && !(subarray instanceof Uint8ClampedArray)
? new Uint8ClampedArray(subarray)
: subarray;
},
peekByte: function DecodeStream_peekByte() {
var peekedByte = this.getByte();
@ -255,7 +262,7 @@ var DecodeStream = (function DecodeStreamClosure() {
},
getByteRange(begin, end) {
unreachable('Should not call DecodeStream.getByteRange');
unreachable("Should not call DecodeStream.getByteRange");
},
skip: function DecodeStream_skip(n) {
@ -296,9 +303,7 @@ var StreamsSequenceStream = (function StreamsSequenceStreamClosure() {
StreamsSequenceStream.prototype = Object.create(DecodeStream.prototype);
StreamsSequenceStream.prototype.readBlock =
function streamSequenceStreamReadBlock() {
StreamsSequenceStream.prototype.readBlock = function streamSequenceStreamReadBlock() {
var streams = this.streams;
if (streams.length === 0) {
this.eof = true;
@ -313,9 +318,7 @@ var StreamsSequenceStream = (function StreamsSequenceStreamClosure() {
this.bufferLength = newLength;
};
StreamsSequenceStream.prototype.getBaseStreams =
function StreamsSequenceStream_getBaseStreams() {
StreamsSequenceStream.prototype.getBaseStreams = function StreamsSequenceStream_getBaseStreams() {
var baseStreams = [];
for (var i = 0, ii = this.streams.length; i < ii; i++) {
var stream = this.streams[i];
@ -434,19 +437,18 @@ var FlateStream = (function FlateStreamClosure() {
var cmf = str.getByte();
var flg = str.getByte();
if (cmf === -1 || flg === -1) {
throw new FormatError(
`Invalid header in flate stream: ${cmf}, ${flg}`);
throw new FormatError(`Invalid header in flate stream: ${cmf}, ${flg}`);
}
if ((cmf & 0x0f) !== 0x08) {
throw new FormatError(
`Unknown compression method in flate stream: ${cmf}, ${flg}`);
`Unknown compression method in flate stream: ${cmf}, ${flg}`
);
}
if ((((cmf << 8) + flg) % 31) !== 0) {
if (((cmf << 8) + flg) % 31 !== 0) {
throw new FormatError(`Bad FCHECK in flate stream: ${cmf}, ${flg}`);
}
if (flg & 0x20) {
throw new FormatError(
`FDICT bit set in flate stream: ${cmf}, ${flg}`);
throw new FormatError(`FDICT bit set in flate stream: ${cmf}, ${flg}`);
}
this.codeSize = 0;
@ -465,7 +467,7 @@ var FlateStream = (function FlateStreamClosure() {
var b;
while (codeSize < bits) {
if ((b = str.getByte()) === -1) {
throw new FormatError('Bad encoding in flate stream');
throw new FormatError("Bad encoding in flate stream");
}
codeBuf |= b << codeSize;
codeSize += 8;
@ -491,22 +493,23 @@ var FlateStream = (function FlateStreamClosure() {
// codeSize < codeLen check below guards against incomplete codeVal.
break;
}
codeBuf |= (b << codeSize);
codeBuf |= b << codeSize;
codeSize += 8;
}
var code = codes[codeBuf & ((1 << maxLen) - 1)];
var codeLen = code >> 16;
var codeVal = code & 0xffff;
if (codeLen < 1 || codeSize < codeLen) {
throw new FormatError('Bad encoding in flate stream');
throw new FormatError("Bad encoding in flate stream");
}
this.codeBuf = (codeBuf >> codeLen);
this.codeSize = (codeSize - codeLen);
this.codeBuf = codeBuf >> codeLen;
this.codeSize = codeSize - codeLen;
return codeVal;
};
FlateStream.prototype.generateHuffmanTable =
function flateStreamGenerateHuffmanTable(lengths) {
FlateStream.prototype.generateHuffmanTable = function flateStreamGenerateHuffmanTable(
lengths
) {
var n = lengths.length;
// find max code length
@ -521,9 +524,11 @@ var FlateStream = (function FlateStreamClosure() {
// build the table
var size = 1 << maxLen;
var codes = new Int32Array(size);
for (var len = 1, code = 0, skip = 2;
len <= maxLen;
++len, code <<= 1, skip <<= 1) {
for (
var len = 1, code = 0, skip = 2;
len <= maxLen;
++len, code <<= 1, skip <<= 1
) {
for (var val = 0; val < n; ++val) {
if (lengths[val] === len) {
// bit-reverse the code
@ -556,36 +561,36 @@ var FlateStream = (function FlateStreamClosure() {
}
hdr >>= 1;
if (hdr === 0) { // uncompressed block
if (hdr === 0) {
// uncompressed block
var b;
if ((b = str.getByte()) === -1) {
throw new FormatError('Bad block header in flate stream');
throw new FormatError("Bad block header in flate stream");
}
var blockLen = b;
if ((b = str.getByte()) === -1) {
throw new FormatError('Bad block header in flate stream');
throw new FormatError("Bad block header in flate stream");
}
blockLen |= (b << 8);
blockLen |= b << 8;
if ((b = str.getByte()) === -1) {
throw new FormatError('Bad block header in flate stream');
throw new FormatError("Bad block header in flate stream");
}
var check = b;
if ((b = str.getByte()) === -1) {
throw new FormatError('Bad block header in flate stream');
throw new FormatError("Bad block header in flate stream");
}
check |= (b << 8);
if (check !== (~blockLen & 0xffff) &&
(blockLen !== 0 || check !== 0)) {
check |= b << 8;
if (check !== (~blockLen & 0xffff) && (blockLen !== 0 || check !== 0)) {
// Ignoring error for bad "empty" block (see issue 1277)
throw new FormatError(
'Bad uncompressed block length in flate stream');
throw new FormatError("Bad uncompressed block length in flate stream");
}
this.codeBuf = 0;
this.codeSize = 0;
const bufferLength = this.bufferLength, end = bufferLength + blockLen;
const bufferLength = this.bufferLength,
end = bufferLength + blockLen;
buffer = this.ensureBuffer(end);
this.bufferLength = end;
@ -605,10 +610,12 @@ var FlateStream = (function FlateStreamClosure() {
var litCodeTable;
var distCodeTable;
if (hdr === 1) { // compressed block, fixed codes
if (hdr === 1) {
// compressed block, fixed codes
litCodeTable = fixedLitCodeTab;
distCodeTable = fixedDistCodeTab;
} else if (hdr === 2) { // compressed block, dynamic codes
} else if (hdr === 2) {
// compressed block, dynamic codes
var numLitCodes = this.getBits(5) + 257;
var numDistCodes = this.getBits(5) + 1;
var numCodeLenCodes = this.getBits(4) + 4;
@ -631,11 +638,17 @@ var FlateStream = (function FlateStreamClosure() {
while (i < codes) {
var code = this.getCode(codeLenCodeTab);
if (code === 16) {
bitsLength = 2; bitsOffset = 3; what = len;
bitsLength = 2;
bitsOffset = 3;
what = len;
} else if (code === 17) {
bitsLength = 3; bitsOffset = 3; what = (len = 0);
bitsLength = 3;
bitsOffset = 3;
what = len = 0;
} else if (code === 18) {
bitsLength = 7; bitsOffset = 11; what = (len = 0);
bitsLength = 7;
bitsOffset = 11;
what = len = 0;
} else {
codeLengths[i++] = len = code;
continue;
@ -647,12 +660,14 @@ var FlateStream = (function FlateStreamClosure() {
}
}
litCodeTable =
this.generateHuffmanTable(codeLengths.subarray(0, numLitCodes));
distCodeTable =
this.generateHuffmanTable(codeLengths.subarray(numLitCodes, codes));
litCodeTable = this.generateHuffmanTable(
codeLengths.subarray(0, numLitCodes)
);
distCodeTable = this.generateHuffmanTable(
codeLengths.subarray(numLitCodes, codes)
);
} else {
throw new FormatError('Unknown block type in flate stream');
throw new FormatError("Unknown block type in flate stream");
}
buffer = this.buffer;
@ -704,7 +719,7 @@ var PredictorStream = (function PredictorStreamClosure() {
if (!isDict(params)) {
return str; // no prediction
}
var predictor = this.predictor = params.get('Predictor') || 1;
var predictor = (this.predictor = params.get("Predictor") || 1);
if (predictor <= 1) {
return str; // no prediction
@ -722,9 +737,9 @@ var PredictorStream = (function PredictorStreamClosure() {
this.str = str;
this.dict = str.dict;
var colors = this.colors = params.get('Colors') || 1;
var bits = this.bits = params.get('BitsPerComponent') || 8;
var columns = this.columns = params.get('Columns') || 1;
var colors = (this.colors = params.get("Colors") || 1);
var bits = (this.bits = params.get("BitsPerComponent") || 8);
var columns = (this.columns = params.get("Columns") || 1);
this.pixBytes = (colors * bits + 7) >> 3;
this.rowBytes = (columns * colors * bits + 7) >> 3;
@ -735,8 +750,7 @@ var PredictorStream = (function PredictorStreamClosure() {
PredictorStream.prototype = Object.create(DecodeStream.prototype);
PredictorStream.prototype.readBlockTiff =
function predictorStreamReadBlockTiff() {
PredictorStream.prototype.readBlockTiff = function predictorStreamReadBlockTiff() {
var rowBytes = this.rowBytes;
var bufferLength = this.bufferLength;
@ -751,8 +765,10 @@ var PredictorStream = (function PredictorStreamClosure() {
return;
}
var inbuf = 0, outbuf = 0;
var inbits = 0, outbits = 0;
var inbuf = 0,
outbuf = 0;
var inbits = 0,
outbits = 0;
var pos = bufferLength;
var i;
@ -781,46 +797,46 @@ var PredictorStream = (function PredictorStreamClosure() {
buffer[pos++] = rawBytes[i];
}
for (; i < rowBytes; i += 2) {
var sum = ((rawBytes[i] & 0xFF) << 8) +
(rawBytes[i + 1] & 0xFF) +
((buffer[pos - bytesPerPixel] & 0xFF) << 8) +
(buffer[pos - bytesPerPixel + 1] & 0xFF);
buffer[pos++] = ((sum >> 8) & 0xFF);
buffer[pos++] = (sum & 0xFF);
var sum =
((rawBytes[i] & 0xff) << 8) +
(rawBytes[i + 1] & 0xff) +
((buffer[pos - bytesPerPixel] & 0xff) << 8) +
(buffer[pos - bytesPerPixel + 1] & 0xff);
buffer[pos++] = (sum >> 8) & 0xff;
buffer[pos++] = sum & 0xff;
}
} else {
var compArray = new Uint8Array(colors + 1);
var bitMask = (1 << bits) - 1;
var j = 0, k = bufferLength;
var j = 0,
k = bufferLength;
var columns = this.columns;
for (i = 0; i < columns; ++i) {
for (var kk = 0; kk < colors; ++kk) {
if (inbits < bits) {
inbuf = (inbuf << 8) | (rawBytes[j++] & 0xFF);
inbuf = (inbuf << 8) | (rawBytes[j++] & 0xff);
inbits += 8;
}
compArray[kk] = (compArray[kk] +
(inbuf >> (inbits - bits))) & bitMask;
compArray[kk] =
(compArray[kk] + (inbuf >> (inbits - bits))) & bitMask;
inbits -= bits;
outbuf = (outbuf << bits) | compArray[kk];
outbits += bits;
if (outbits >= 8) {
buffer[k++] = (outbuf >> (outbits - 8)) & 0xFF;
buffer[k++] = (outbuf >> (outbits - 8)) & 0xff;
outbits -= 8;
}
}
}
if (outbits > 0) {
buffer[k++] = (outbuf << (8 - outbits)) +
(inbuf & ((1 << (8 - outbits)) - 1));
buffer[k++] =
(outbuf << (8 - outbits)) + (inbuf & ((1 << (8 - outbits)) - 1));
}
}
this.bufferLength += rowBytes;
};
PredictorStream.prototype.readBlockPng =
function predictorStreamReadBlockPng() {
PredictorStream.prototype.readBlockPng = function predictorStreamReadBlockPng() {
var rowBytes = this.rowBytes;
var pixBytes = this.pixBytes;
@ -839,7 +855,10 @@ var PredictorStream = (function PredictorStreamClosure() {
prevRow = new Uint8Array(rowBytes);
}
var i, j = bufferLength, up, c;
var i,
j = bufferLength,
up,
c;
switch (predictor) {
case 0:
for (i = 0; i < rowBytes; ++i) {
@ -851,13 +870,13 @@ var PredictorStream = (function PredictorStreamClosure() {
buffer[j++] = rawBytes[i];
}
for (; i < rowBytes; ++i) {
buffer[j] = (buffer[j - pixBytes] + rawBytes[i]) & 0xFF;
buffer[j] = (buffer[j - pixBytes] + rawBytes[i]) & 0xff;
j++;
}
break;
case 2:
for (i = 0; i < rowBytes; ++i) {
buffer[j++] = (prevRow[i] + rawBytes[i]) & 0xFF;
buffer[j++] = (prevRow[i] + rawBytes[i]) & 0xff;
}
break;
case 3:
@ -865,8 +884,8 @@ var PredictorStream = (function PredictorStreamClosure() {
buffer[j++] = (prevRow[i] >> 1) + rawBytes[i];
}
for (; i < rowBytes; ++i) {
buffer[j] = (((prevRow[i] + buffer[j - pixBytes]) >> 1) +
rawBytes[i]) & 0xFF;
buffer[j] =
(((prevRow[i] + buffer[j - pixBytes]) >> 1) + rawBytes[i]) & 0xff;
j++;
}
break;
@ -950,7 +969,8 @@ var DecryptStream = (function DecryptStreamClosure() {
chunk = decrypt(chunk, !hasMoreData);
var bufferLength = this.bufferLength;
var i, n = chunk.length;
var i,
n = chunk.length;
var buffer = this.ensureBuffer(bufferLength + n);
for (i = 0; i < n; i++) {
buffer[bufferLength++] = chunk[i];
@ -978,8 +998,8 @@ var Ascii85Stream = (function Ascii85StreamClosure() {
Ascii85Stream.prototype = Object.create(DecodeStream.prototype);
Ascii85Stream.prototype.readBlock = function Ascii85Stream_readBlock() {
var TILDA_CHAR = 0x7E; // '~'
var Z_LOWER_CHAR = 0x7A; // 'z'
var TILDA_CHAR = 0x7e; // '~'
var Z_LOWER_CHAR = 0x7a; // 'z'
var EOF = -1;
var str = this.str;
@ -994,7 +1014,8 @@ var Ascii85Stream = (function Ascii85StreamClosure() {
return;
}
var bufferLength = this.bufferLength, buffer;
var bufferLength = this.bufferLength,
buffer;
var i;
// special code for z
@ -1035,7 +1056,7 @@ var Ascii85Stream = (function Ascii85StreamClosure() {
}
for (i = 3; i >= 0; --i) {
buffer[bufferLength + i] = t & 0xFF;
buffer[bufferLength + i] = t & 0xff;
t >>= 8;
}
}
@ -1075,16 +1096,20 @@ var AsciiHexStream = (function AsciiHexStreamClosure() {
var firstDigit = this.firstDigit;
for (var i = 0, ii = bytes.length; i < ii; i++) {
var ch = bytes[i], digit;
if (ch >= 0x30 && ch <= 0x39) { // '0'-'9'
digit = ch & 0x0F;
var ch = bytes[i],
digit;
if (ch >= 0x30 && ch <= 0x39) {
// '0'-'9'
digit = ch & 0x0f;
} else if ((ch >= 0x41 && ch <= 0x46) || (ch >= 0x61 && ch <= 0x66)) {
// 'A'-'Z', 'a'-'z'
digit = (ch & 0x0F) + 9;
} else if (ch === 0x3E) { // '>'
digit = (ch & 0x0f) + 9;
} else if (ch === 0x3e) {
// '>'
this.eof = true;
break;
} else { // probably whitespace
} else {
// probably whitespace
continue; // ignoring
}
if (firstDigit < 0) {
@ -1096,7 +1121,7 @@ var AsciiHexStream = (function AsciiHexStreamClosure() {
}
if (firstDigit >= 0 && this.eof) {
// incomplete byte
buffer[bufferLength++] = (firstDigit << 4);
buffer[bufferLength++] = firstDigit << 4;
firstDigit = -1;
}
this.firstDigit = firstDigit;
@ -1194,7 +1219,7 @@ var LZWStream = (function LZWStreamClosure() {
cachedData = (cachedData << 8) | c;
bitsCached += 8;
}
this.bitsCached = (bitsCached -= n);
this.bitsCached = bitsCached -= n;
this.cachedData = cachedData;
this.lastCode = null;
return (cachedData >>> bitsCached) & ((1 << n) - 1);
@ -1202,7 +1227,8 @@ var LZWStream = (function LZWStreamClosure() {
LZWStream.prototype.readBlock = function LZWStream_readBlock() {
var blockSize = 512;
var estimatedDecodedSize = blockSize * 2, decodedSizeDelta = blockSize;
var estimatedDecodedSize = blockSize * 2,
decodedSizeDelta = blockSize;
var i, j, q;
var lzwState = this.lzwState;
@ -1256,9 +1282,13 @@ var LZWStream = (function LZWStreamClosure() {
dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
dictionaryValues[nextCode] = currentSequence[0];
nextCode++;
codeLength = (nextCode + earlyChange) & (nextCode + earlyChange - 1) ?
codeLength : Math.min(Math.log(nextCode + earlyChange) /
0.6931471805599453 + 1, 12) | 0;
codeLength =
(nextCode + earlyChange) & (nextCode + earlyChange - 1)
? codeLength
: Math.min(
Math.log(nextCode + earlyChange) / 0.6931471805599453 + 1,
12
) | 0;
}
prevCode = code;

View file

@ -13,9 +13,9 @@
* limitations under the License.
*/
import { isSpace, warn } from '../shared/util';
import { getEncoding } from './encodings';
import { Stream } from './stream';
import { isSpace, warn } from "../shared/util";
import { getEncoding } from "./encodings";
import { Stream } from "./stream";
// Hinting is currently disabled due to unknown problems on windows
// in tracemonkey and various other pdfs with type1 fonts.
@ -61,21 +61,21 @@ var HINTING_ENABLED = false;
*/
var Type1CharString = (function Type1CharStringClosure() {
var COMMAND_MAP = {
'hstem': [1],
'vstem': [3],
'vmoveto': [4],
'rlineto': [5],
'hlineto': [6],
'vlineto': [7],
'rrcurveto': [8],
'callsubr': [10],
'flex': [12, 35],
'drop': [12, 18],
'endchar': [14],
'rmoveto': [21],
'hmoveto': [22],
'vhcurveto': [30],
'hvcurveto': [31],
hstem: [1],
vstem: [3],
vmoveto: [4],
rlineto: [5],
hlineto: [6],
vlineto: [7],
rrcurveto: [8],
callsubr: [10],
flex: [12, 35],
drop: [12, 18],
endchar: [14],
rmoveto: [21],
hmoveto: [22],
vhcurveto: [30],
hvcurveto: [31],
};
function Type1CharString() {
@ -87,8 +87,11 @@ var Type1CharString = (function Type1CharStringClosure() {
}
Type1CharString.prototype = {
convert: function Type1CharString_convert(encoded, subrs,
seacAnalysisEnabled) {
convert: function Type1CharString_convert(
encoded,
subrs,
seacAnalysisEnabled
) {
var count = encoded.length;
var error = false;
var wx, sbx, subrNumber;
@ -154,8 +157,11 @@ var Type1CharString = (function Type1CharStringClosure() {
error = true;
break;
}
error = this.convert(subrs[subrNumber], subrs,
seacAnalysisEnabled);
error = this.convert(
subrs[subrNumber],
subrs,
seacAnalysisEnabled
);
break;
case 11: // return
return error;
@ -214,7 +220,7 @@ var Type1CharString = (function Type1CharStringClosure() {
break;
case (12 << 8) + 2: // hstem3
if (!HINTING_ENABLED) {
this.stack = [];
this.stack = [];
break;
}
// See vstem3.
@ -308,12 +314,15 @@ var Type1CharString = (function Type1CharStringClosure() {
} else if (value <= 246) {
value = value - 139;
} else if (value <= 250) {
value = ((value - 247) * 256) + encoded[++i] + 108;
value = (value - 247) * 256 + encoded[++i] + 108;
} else if (value <= 254) {
value = -((value - 251) * 256) - encoded[++i] - 108;
} else {
value = (encoded[++i] & 0xff) << 24 | (encoded[++i] & 0xff) << 16 |
(encoded[++i] & 0xff) << 8 | (encoded[++i] & 0xff) << 0;
value =
((encoded[++i] & 0xff) << 24) |
((encoded[++i] & 0xff) << 16) |
((encoded[++i] & 0xff) << 8) |
((encoded[++i] & 0xff) << 0);
}
this.stack.push(value);
}
@ -330,13 +339,16 @@ var Type1CharString = (function Type1CharStringClosure() {
var value = this.stack[i];
if (Number.isInteger(value)) {
this.output.push(28, (value >> 8) & 0xff, value & 0xff);
} else { // fixed point
} else {
// fixed point
value = (65536 * value) | 0;
this.output.push(255,
(value >> 24) & 0xFF,
(value >> 16) & 0xFF,
(value >> 8) & 0xFF,
value & 0xFF);
this.output.push(
255,
(value >> 24) & 0xff,
(value >> 16) & 0xff,
(value >> 8) & 0xff,
value & 0xff
);
}
}
this.output.push.apply(this.output, command);
@ -370,16 +382,22 @@ var Type1Parser = (function Type1ParserClosure() {
var CHAR_STRS_ENCRYPT_KEY = 4330;
function isHexDigit(code) {
return code >= 48 && code <= 57 || // '0'-'9'
code >= 65 && code <= 70 || // 'A'-'F'
code >= 97 && code <= 102; // 'a'-'f'
return (
(code >= 48 && code <= 57) || // '0'-'9'
(code >= 65 && code <= 70) || // 'A'-'F'
(code >= 97 && code <= 102)
); // 'a'-'f'
}
function decrypt(data, key, discardNumber) {
if (discardNumber >= data.length) {
return new Uint8Array(0);
}
var r = key | 0, c1 = 52845, c2 = 22719, i, j;
var r = key | 0,
c1 = 52845,
c2 = 22719,
i,
j;
for (i = 0; i < discardNumber; i++) {
r = ((data[i] + r) * c1 + c2) & ((1 << 16) - 1);
}
@ -394,8 +412,11 @@ var Type1Parser = (function Type1ParserClosure() {
}
function decryptAscii(data, key, discardNumber) {
var r = key | 0, c1 = 52845, c2 = 22719;
var count = data.length, maybeLength = count >>> 1;
var r = key | 0,
c1 = 52845,
c2 = 22719;
var count = data.length,
maybeLength = count >>> 1;
var decrypted = new Uint8Array(maybeLength);
var i, j;
for (i = 0, j = 0; i < count; i++) {
@ -405,7 +426,7 @@ var Type1Parser = (function Type1ParserClosure() {
}
i++;
var digit2;
while (i < count && !isHexDigit(digit2 = data[i])) {
while (i < count && !isHexDigit((digit2 = data[i]))) {
i++;
}
if (i < count) {
@ -418,19 +439,31 @@ var Type1Parser = (function Type1ParserClosure() {
}
function isSpecial(c) {
return c === 0x2F || // '/'
c === 0x5B || c === 0x5D || // '[', ']'
c === 0x7B || c === 0x7D || // '{', '}'
c === 0x28 || c === 0x29; // '(', ')'
return (
c === 0x2f || // '/'
c === 0x5b ||
c === 0x5d || // '[', ']'
c === 0x7b ||
c === 0x7d || // '{', '}'
c === 0x28 ||
c === 0x29
); // '(', ')'
}
function Type1Parser(stream, encrypted, seacAnalysisEnabled) {
if (encrypted) {
var data = stream.getBytes();
var isBinary = !(isHexDigit(data[0]) && isHexDigit(data[1]) &&
isHexDigit(data[2]) && isHexDigit(data[3]));
stream = new Stream(isBinary ? decrypt(data, EEXEC_ENCRYPT_KEY, 4) :
decryptAscii(data, EEXEC_ENCRYPT_KEY, 4));
var isBinary = !(
isHexDigit(data[0]) &&
isHexDigit(data[1]) &&
isHexDigit(data[2]) &&
isHexDigit(data[3])
);
stream = new Stream(
isBinary
? decrypt(data, EEXEC_ENCRYPT_KEY, 4)
: decryptAscii(data, EEXEC_ENCRYPT_KEY, 4)
);
}
this.seacAnalysisEnabled = !!seacAnalysisEnabled;
@ -444,7 +477,7 @@ var Type1Parser = (function Type1ParserClosure() {
var array = [];
while (true) {
var token = this.getToken();
if (token === null || token === ']' || token === '}') {
if (token === null || token === "]" || token === "}") {
break;
}
array.push(parseFloat(token || 0));
@ -468,7 +501,7 @@ var Type1Parser = (function Type1ParserClosure() {
var token = this.getToken();
// Use 1 and 0 since that's what type2 charstrings use.
return token === 'true' ? 1 : 0;
return token === "true" ? 1 : 0;
},
nextChar: function Type1_nextChar() {
@ -485,10 +518,11 @@ var Type1Parser = (function Type1ParserClosure() {
}
if (comment) {
if (ch === 0x0A || ch === 0x0D) {
if (ch === 0x0a || ch === 0x0d) {
comment = false;
}
} else if (ch === 0x25) { // '%'
} else if (ch === 0x25) {
// '%'
comment = true;
} else if (!isSpace(ch)) {
break;
@ -499,7 +533,7 @@ var Type1Parser = (function Type1ParserClosure() {
this.nextChar();
return String.fromCharCode(ch);
}
var token = '';
var token = "";
do {
token += String.fromCharCode(ch);
ch = this.nextChar();
@ -523,24 +557,25 @@ var Type1Parser = (function Type1ParserClosure() {
extractFontProgram: function Type1Parser_extractFontProgram(properties) {
var stream = this.stream;
var subrs = [], charstrings = [];
var subrs = [],
charstrings = [];
var privateData = Object.create(null);
privateData['lenIV'] = 4;
privateData["lenIV"] = 4;
var program = {
subrs: [],
charstrings: [],
properties: {
'privateData': privateData,
privateData,
},
};
var token, length, data, lenIV, encoded;
while ((token = this.getToken()) !== null) {
if (token !== '/') {
if (token !== "/") {
continue;
}
token = this.getToken();
switch (token) {
case 'CharStrings':
case "CharStrings":
// The number immediately following CharStrings must be greater or
// equal to the number of CharStrings.
this.getToken();
@ -549,22 +584,22 @@ var Type1Parser = (function Type1ParserClosure() {
this.getToken(); // read in 'begin'
while (true) {
token = this.getToken();
if (token === null || token === 'end') {
if (token === null || token === "end") {
break;
}
if (token !== '/') {
if (token !== "/") {
continue;
}
var glyph = this.getToken();
length = this.readInt();
this.getToken(); // read in 'RD' or '-|'
data = (length > 0 ? stream.getBytes(length) : new Uint8Array(0));
lenIV = program.properties.privateData['lenIV'];
data = length > 0 ? stream.getBytes(length) : new Uint8Array(0);
lenIV = program.properties.privateData["lenIV"];
encoded = this.readCharStrings(data, lenIV);
this.nextChar();
token = this.getToken(); // read in 'ND' or '|-'
if (token === 'noaccess') {
if (token === "noaccess") {
this.getToken(); // read in 'def'
}
charstrings.push({
@ -573,54 +608,56 @@ var Type1Parser = (function Type1ParserClosure() {
});
}
break;
case 'Subrs':
case "Subrs":
this.readInt(); // num
this.getToken(); // read in 'array'
while (this.getToken() === 'dup') {
while (this.getToken() === "dup") {
var index = this.readInt();
length = this.readInt();
this.getToken(); // read in 'RD' or '-|'
data = (length > 0 ? stream.getBytes(length) : new Uint8Array(0));
lenIV = program.properties.privateData['lenIV'];
data = length > 0 ? stream.getBytes(length) : new Uint8Array(0);
lenIV = program.properties.privateData["lenIV"];
encoded = this.readCharStrings(data, lenIV);
this.nextChar();
token = this.getToken(); // read in 'NP' or '|'
if (token === 'noaccess') {
if (token === "noaccess") {
this.getToken(); // read in 'put'
}
subrs[index] = encoded;
}
break;
case 'BlueValues':
case 'OtherBlues':
case 'FamilyBlues':
case 'FamilyOtherBlues':
case "BlueValues":
case "OtherBlues":
case "FamilyBlues":
case "FamilyOtherBlues":
var blueArray = this.readNumberArray();
// *Blue* values may contain invalid data: disables reading of
// those values when hinting is disabled.
if (blueArray.length > 0 && (blueArray.length % 2) === 0 &&
HINTING_ENABLED) {
if (
blueArray.length > 0 &&
blueArray.length % 2 === 0 &&
HINTING_ENABLED
) {
program.properties.privateData[token] = blueArray;
}
break;
case 'StemSnapH':
case 'StemSnapV':
case "StemSnapH":
case "StemSnapV":
program.properties.privateData[token] = this.readNumberArray();
break;
case 'StdHW':
case 'StdVW':
program.properties.privateData[token] =
this.readNumberArray()[0];
case "StdHW":
case "StdVW":
program.properties.privateData[token] = this.readNumberArray()[0];
break;
case 'BlueShift':
case 'lenIV':
case 'BlueFuzz':
case 'BlueScale':
case 'LanguageGroup':
case 'ExpansionFactor':
case "BlueShift":
case "lenIV":
case "BlueFuzz":
case "BlueScale":
case "LanguageGroup":
case "ExpansionFactor":
program.properties.privateData[token] = this.readNumber();
break;
case 'ForceBold':
case "ForceBold":
program.properties.privateData[token] = this.readBoolean();
break;
}
@ -630,8 +667,11 @@ var Type1Parser = (function Type1ParserClosure() {
glyph = charstrings[i].glyph;
encoded = charstrings[i].encoded;
var charString = new Type1CharString();
var error = charString.convert(encoded, subrs,
this.seacAnalysisEnabled);
var error = charString.convert(
encoded,
subrs,
this.seacAnalysisEnabled
);
var output = charString.output;
if (error) {
// It seems when FreeType encounters an error while evaluating a glyph
@ -651,8 +691,12 @@ var Type1Parser = (function Type1ParserClosure() {
// entry, with ones from the font data (fixes issue11150_reduced.pdf).
if (properties.builtInEncoding) {
const index = properties.builtInEncoding.indexOf(glyph);
if (index > -1 && properties.widths[index] === undefined &&
index >= properties.firstChar && index <= properties.lastChar) {
if (
index > -1 &&
properties.widths[index] === undefined &&
index >= properties.firstChar &&
index <= properties.lastChar
) {
properties.widths[index] = charString.width;
}
}
@ -664,16 +708,16 @@ var Type1Parser = (function Type1ParserClosure() {
extractFontHeader: function Type1Parser_extractFontHeader(properties) {
var token;
while ((token = this.getToken()) !== null) {
if (token !== '/') {
if (token !== "/") {
continue;
}
token = this.getToken();
switch (token) {
case 'FontMatrix':
case "FontMatrix":
var matrix = this.readNumberArray();
properties.fontMatrix = matrix;
break;
case 'Encoding':
case "Encoding":
var encodingArg = this.getToken();
var encoding;
if (!/^\d+$/.test(encodingArg)) {
@ -687,13 +731,13 @@ var Type1Parser = (function Type1ParserClosure() {
for (var j = 0; j < size; j++) {
token = this.getToken();
// skipping till first dup or def (e.g. ignoring for statement)
while (token !== 'dup' && token !== 'def') {
while (token !== "dup" && token !== "def") {
token = this.getToken();
if (token === null) {
return; // invalid header
}
}
if (token === 'def') {
if (token === "def") {
break; // read all array data
}
var index = this.readInt();
@ -705,7 +749,7 @@ var Type1Parser = (function Type1ParserClosure() {
}
properties.builtInEncoding = encoding;
break;
case 'FontBBox':
case "FontBBox":
var fontBBox = this.readNumberArray();
// adjusting ascent/descent
properties.ascent = Math.max(fontBBox[3], fontBBox[1]);
@ -720,6 +764,4 @@ var Type1Parser = (function Type1ParserClosure() {
return Type1Parser;
})();
export {
Type1Parser,
};
export { Type1Parser };

File diff suppressed because it is too large Load diff

View file

@ -14,17 +14,28 @@
*/
import {
AbortException, arrayByteLength, arraysToBytes, createPromiseCapability,
getVerbosityLevel, info, InvalidPDFException, MissingPDFException,
PasswordException, setVerbosityLevel, UnexpectedResponseException,
UnknownErrorException, UNSUPPORTED_FEATURES, VerbosityLevel, warn
} from '../shared/util';
import { clearPrimitiveCaches, Ref } from './primitives';
import { LocalPdfManager, NetworkPdfManager } from './pdf_manager';
import { isNodeJS } from '../shared/is_node';
import { MessageHandler } from '../shared/message_handler';
import { PDFWorkerStream } from './worker_stream';
import { XRefParseException } from './core_utils';
AbortException,
arrayByteLength,
arraysToBytes,
createPromiseCapability,
getVerbosityLevel,
info,
InvalidPDFException,
MissingPDFException,
PasswordException,
setVerbosityLevel,
UnexpectedResponseException,
UnknownErrorException,
UNSUPPORTED_FEATURES,
VerbosityLevel,
warn,
} from "../shared/util";
import { clearPrimitiveCaches, Ref } from "./primitives";
import { LocalPdfManager, NetworkPdfManager } from "./pdf_manager";
import { isNodeJS } from "../shared/is_node";
import { MessageHandler } from "../shared/message_handler";
import { PDFWorkerStream } from "./worker_stream";
import { XRefParseException } from "./core_utils";
var WorkerTask = (function WorkerTaskClosure() {
function WorkerTask(name) {
@ -48,7 +59,7 @@ var WorkerTask = (function WorkerTaskClosure() {
ensureNotTerminated() {
if (this.terminated) {
throw new Error('Worker task was terminated');
throw new Error("Worker task was terminated");
}
},
};
@ -59,7 +70,7 @@ var WorkerTask = (function WorkerTaskClosure() {
var WorkerMessageHandler = {
setup(handler, port) {
var testMessageProcessed = false;
handler.on('test', function wphSetupTest(data) {
handler.on("test", function wphSetupTest(data) {
if (testMessageProcessed) {
return; // we already processed 'test' message once
}
@ -67,21 +78,21 @@ var WorkerMessageHandler = {
// check if Uint8Array can be sent to worker
if (!(data instanceof Uint8Array)) {
handler.send('test', null);
handler.send("test", null);
return;
}
// making sure postMessage transfers are working
const supportTransfers = data[0] === 255;
handler.postMessageTransfers = supportTransfers;
handler.send('test', { supportTransfers, });
handler.send("test", { supportTransfers });
});
handler.on('configure', function wphConfigure(data) {
handler.on("configure", function wphConfigure(data) {
setVerbosityLevel(data.verbosity);
});
handler.on('GetDocRequest', function wphSetupDoc(data) {
handler.on("GetDocRequest", function wphSetupDoc(data) {
return WorkerMessageHandler.createDocumentHandler(data, port);
});
},
@ -96,16 +107,19 @@ var WorkerMessageHandler = {
const apiVersion = docParams.apiVersion;
const workerVersion =
typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('TESTING') ?
PDFJSDev.eval('BUNDLE_VERSION') : null;
typeof PDFJSDev !== "undefined" && !PDFJSDev.test("TESTING")
? PDFJSDev.eval("BUNDLE_VERSION")
: null;
if (apiVersion !== workerVersion) {
throw new Error(`The API version "${apiVersion}" does not match ` +
`the Worker version "${workerVersion}".`);
throw new Error(
`The API version "${apiVersion}" does not match ` +
`the Worker version "${workerVersion}".`
);
}
var docId = docParams.docId;
var docBaseUrl = docParams.docBaseUrl;
var workerHandlerName = docParams.docId + '_worker';
var workerHandlerName = docParams.docId + "_worker";
var handler = new MessageHandler(workerHandlerName, docId, port);
// Ensure that postMessage transfers are always correctly enabled/disabled,
@ -114,7 +128,7 @@ var WorkerMessageHandler = {
function ensureNotTerminated() {
if (terminated) {
throw new Error('Worker was terminated');
throw new Error("Worker was terminated");
}
}
@ -129,21 +143,21 @@ var WorkerMessageHandler = {
}
async function loadDocument(recoveryMode) {
await pdfManager.ensureDoc('checkHeader');
await pdfManager.ensureDoc('parseStartXRef');
await pdfManager.ensureDoc('parse', [recoveryMode]);
await pdfManager.ensureDoc("checkHeader");
await pdfManager.ensureDoc("parseStartXRef");
await pdfManager.ensureDoc("parse", [recoveryMode]);
if (!recoveryMode) {
// Check that at least the first page can be successfully loaded,
// since otherwise the XRef table is definitely not valid.
await pdfManager.ensureDoc('checkFirstPage');
await pdfManager.ensureDoc("checkFirstPage");
}
const [numPages, fingerprint] = await Promise.all([
pdfManager.ensureDoc('numPages'),
pdfManager.ensureDoc('fingerprint'),
pdfManager.ensureDoc("numPages"),
pdfManager.ensureDoc("fingerprint"),
]);
return { numPages, fingerprint, };
return { numPages, fingerprint };
}
function getPdfManager(data, evaluatorOptions) {
@ -153,8 +167,13 @@ var WorkerMessageHandler = {
var source = data.source;
if (source.data) {
try {
pdfManager = new LocalPdfManager(docId, source.data, source.password,
evaluatorOptions, docBaseUrl);
pdfManager = new LocalPdfManager(
docId,
source.data,
source.password,
evaluatorOptions,
docBaseUrl
);
pdfManagerCapability.resolve(pdfManager);
} catch (ex) {
pdfManagerCapability.reject(ex);
@ -162,7 +181,8 @@ var WorkerMessageHandler = {
return pdfManagerCapability.promise;
}
var pdfStream, cachedChunks = [];
var pdfStream,
cachedChunks = [];
try {
pdfStream = new PDFWorkerStream(handler);
} catch (ex) {
@ -171,55 +191,68 @@ var WorkerMessageHandler = {
}
var fullRequest = pdfStream.getFullReader();
fullRequest.headersReady.then(function () {
if (!fullRequest.isRangeSupported) {
return;
}
fullRequest.headersReady
.then(function() {
if (!fullRequest.isRangeSupported) {
return;
}
// We don't need auto-fetch when streaming is enabled.
var disableAutoFetch = source.disableAutoFetch ||
fullRequest.isStreamingSupported;
pdfManager = new NetworkPdfManager(docId, pdfStream, {
msgHandler: handler,
password: source.password,
length: fullRequest.contentLength,
disableAutoFetch,
rangeChunkSize: source.rangeChunkSize,
}, evaluatorOptions, docBaseUrl);
// There may be a chance that `pdfManager` is not initialized
// for first few runs of `readchunk` block of code. Be sure
// to send all cached chunks, if any, to chunked_stream via
// pdf_manager.
for (let i = 0; i < cachedChunks.length; i++) {
pdfManager.sendProgressiveData(cachedChunks[i]);
}
// We don't need auto-fetch when streaming is enabled.
var disableAutoFetch =
source.disableAutoFetch || fullRequest.isStreamingSupported;
pdfManager = new NetworkPdfManager(
docId,
pdfStream,
{
msgHandler: handler,
password: source.password,
length: fullRequest.contentLength,
disableAutoFetch,
rangeChunkSize: source.rangeChunkSize,
},
evaluatorOptions,
docBaseUrl
);
// There may be a chance that `pdfManager` is not initialized
// for first few runs of `readchunk` block of code. Be sure
// to send all cached chunks, if any, to chunked_stream via
// pdf_manager.
for (let i = 0; i < cachedChunks.length; i++) {
pdfManager.sendProgressiveData(cachedChunks[i]);
}
cachedChunks = [];
pdfManagerCapability.resolve(pdfManager);
cancelXHRs = null;
}).catch(function (reason) {
pdfManagerCapability.reject(reason);
cancelXHRs = null;
});
cachedChunks = [];
pdfManagerCapability.resolve(pdfManager);
cancelXHRs = null;
})
.catch(function(reason) {
pdfManagerCapability.reject(reason);
cancelXHRs = null;
});
var loaded = 0;
var flushChunks = function () {
var flushChunks = function() {
var pdfFile = arraysToBytes(cachedChunks);
if (source.length && pdfFile.length !== source.length) {
warn('reported HTTP length is different from actual');
warn("reported HTTP length is different from actual");
}
// the data is array, instantiating directly from it
try {
pdfManager = new LocalPdfManager(docId, pdfFile, source.password,
evaluatorOptions, docBaseUrl);
pdfManager = new LocalPdfManager(
docId,
pdfFile,
source.password,
evaluatorOptions,
docBaseUrl
);
pdfManagerCapability.resolve(pdfManager);
} catch (ex) {
pdfManagerCapability.reject(ex);
}
cachedChunks = [];
};
var readPromise = new Promise(function (resolve, reject) {
var readChunk = function (chunk) {
var readPromise = new Promise(function(resolve, reject) {
var readChunk = function(chunk) {
try {
ensureNotTerminated();
if (chunk.done) {
@ -233,7 +266,7 @@ var WorkerMessageHandler = {
var data = chunk.value;
loaded += arrayByteLength(data);
if (!fullRequest.isStreamingSupported) {
handler.send('DocProgress', {
handler.send("DocProgress", {
loaded,
total: Math.max(loaded, fullRequest.contentLength || 0),
});
@ -252,7 +285,7 @@ var WorkerMessageHandler = {
};
fullRequest.read().then(readChunk, reject);
});
readPromise.catch(function (e) {
readPromise.catch(function(e) {
pdfManagerCapability.reject(e);
cancelXHRs = null;
});
@ -267,7 +300,7 @@ var WorkerMessageHandler = {
function setupDoc(data) {
function onSuccess(doc) {
ensureNotTerminated();
handler.send('GetDoc', { pdfInfo: doc, });
handler.send("GetDoc", { pdfInfo: doc });
}
function onFailure(ex) {
@ -277,43 +310,54 @@ var WorkerMessageHandler = {
var task = new WorkerTask(`PasswordException: response ${ex.code}`);
startWorkerTask(task);
handler.sendWithPromise('PasswordRequest', ex).then(function(data) {
finishWorkerTask(task);
pdfManager.updatePassword(data.password);
pdfManagerReady();
}).catch(function() {
finishWorkerTask(task);
handler.send('DocException', ex);
});
} else if (ex instanceof InvalidPDFException ||
ex instanceof MissingPDFException ||
ex instanceof UnexpectedResponseException ||
ex instanceof UnknownErrorException) {
handler.send('DocException', ex);
handler
.sendWithPromise("PasswordRequest", ex)
.then(function(data) {
finishWorkerTask(task);
pdfManager.updatePassword(data.password);
pdfManagerReady();
})
.catch(function() {
finishWorkerTask(task);
handler.send("DocException", ex);
});
} else if (
ex instanceof InvalidPDFException ||
ex instanceof MissingPDFException ||
ex instanceof UnexpectedResponseException ||
ex instanceof UnknownErrorException
) {
handler.send("DocException", ex);
} else {
handler.send('DocException',
new UnknownErrorException(ex.message, ex.toString()));
handler.send(
"DocException",
new UnknownErrorException(ex.message, ex.toString())
);
}
}
function pdfManagerReady() {
ensureNotTerminated();
loadDocument(false).then(onSuccess, function loadFailure(ex) {
ensureNotTerminated();
// Try again with recoveryMode == true
if (!(ex instanceof XRefParseException)) {
onFailure(ex);
return;
}
pdfManager.requestLoadedStream();
pdfManager.onLoadedStream().then(function() {
loadDocument(false).then(
onSuccess,
function loadFailure(ex) {
ensureNotTerminated();
loadDocument(true).then(onSuccess, onFailure);
});
}, onFailure);
// Try again with recoveryMode == true
if (!(ex instanceof XRefParseException)) {
onFailure(ex);
return;
}
pdfManager.requestLoadedStream();
pdfManager.onLoadedStream().then(function() {
ensureNotTerminated();
loadDocument(true).then(onSuccess, onFailure);
});
},
onFailure
);
}
ensureNotTerminated();
@ -327,28 +371,32 @@ var WorkerMessageHandler = {
isEvalSupported: data.isEvalSupported,
};
getPdfManager(data, evaluatorOptions).then(function (newPdfManager) {
if (terminated) {
// We were in a process of setting up the manager, but it got
// terminated in the middle.
newPdfManager.terminate(new AbortException('Worker was terminated.'));
throw new Error('Worker was terminated');
}
pdfManager = newPdfManager;
getPdfManager(data, evaluatorOptions)
.then(function(newPdfManager) {
if (terminated) {
// We were in a process of setting up the manager, but it got
// terminated in the middle.
newPdfManager.terminate(
new AbortException("Worker was terminated.")
);
throw new Error("Worker was terminated");
}
pdfManager = newPdfManager;
pdfManager.onLoadedStream().then(function(stream) {
handler.send('DataLoaded', { length: stream.bytes.byteLength, });
});
}).then(pdfManagerReady, onFailure);
pdfManager.onLoadedStream().then(function(stream) {
handler.send("DataLoaded", { length: stream.bytes.byteLength });
});
})
.then(pdfManagerReady, onFailure);
}
handler.on('GetPage', function wphSetupGetPage(data) {
handler.on("GetPage", function wphSetupGetPage(data) {
return pdfManager.getPage(data.pageIndex).then(function(page) {
return Promise.all([
pdfManager.ensure(page, 'rotate'),
pdfManager.ensure(page, 'ref'),
pdfManager.ensure(page, 'userUnit'),
pdfManager.ensure(page, 'view'),
pdfManager.ensure(page, "rotate"),
pdfManager.ensure(page, "ref"),
pdfManager.ensure(page, "userUnit"),
pdfManager.ensure(page, "view"),
]).then(function([rotate, ref, userUnit, view]) {
return {
rotate,
@ -360,201 +408,206 @@ var WorkerMessageHandler = {
});
});
handler.on('GetPageIndex', function wphSetupGetPageIndex(data) {
handler.on("GetPageIndex", function wphSetupGetPageIndex(data) {
var ref = Ref.get(data.ref.num, data.ref.gen);
var catalog = pdfManager.pdfDocument.catalog;
return catalog.getPageIndex(ref);
});
handler.on('GetDestinations',
function wphSetupGetDestinations(data) {
return pdfManager.ensureCatalog('destinations');
}
);
handler.on('GetDestination',
function wphSetupGetDestination(data) {
return pdfManager.ensureCatalog('getDestination', [data.id]);
}
);
handler.on('GetPageLabels',
function wphSetupGetPageLabels(data) {
return pdfManager.ensureCatalog('pageLabels');
}
);
handler.on('GetPageLayout', function wphSetupGetPageLayout(data) {
return pdfManager.ensureCatalog('pageLayout');
handler.on("GetDestinations", function wphSetupGetDestinations(data) {
return pdfManager.ensureCatalog("destinations");
});
handler.on('GetPageMode', function wphSetupGetPageMode(data) {
return pdfManager.ensureCatalog('pageMode');
handler.on("GetDestination", function wphSetupGetDestination(data) {
return pdfManager.ensureCatalog("getDestination", [data.id]);
});
handler.on('GetViewerPreferences', function(data) {
return pdfManager.ensureCatalog('viewerPreferences');
handler.on("GetPageLabels", function wphSetupGetPageLabels(data) {
return pdfManager.ensureCatalog("pageLabels");
});
handler.on('GetOpenActionDestination', function(data) {
return pdfManager.ensureCatalog('openActionDestination');
handler.on("GetPageLayout", function wphSetupGetPageLayout(data) {
return pdfManager.ensureCatalog("pageLayout");
});
handler.on('GetAttachments',
function wphSetupGetAttachments(data) {
return pdfManager.ensureCatalog('attachments');
}
);
handler.on('GetJavaScript',
function wphSetupGetJavaScript(data) {
return pdfManager.ensureCatalog('javaScript');
}
);
handler.on('GetOutline',
function wphSetupGetOutline(data) {
return pdfManager.ensureCatalog('documentOutline');
}
);
handler.on('GetPermissions', function(data) {
return pdfManager.ensureCatalog('permissions');
handler.on("GetPageMode", function wphSetupGetPageMode(data) {
return pdfManager.ensureCatalog("pageMode");
});
handler.on('GetMetadata',
function wphSetupGetMetadata(data) {
return Promise.all([pdfManager.ensureDoc('documentInfo'),
pdfManager.ensureCatalog('metadata')]);
}
);
handler.on("GetViewerPreferences", function(data) {
return pdfManager.ensureCatalog("viewerPreferences");
});
handler.on('GetData', function wphSetupGetData(data) {
handler.on("GetOpenActionDestination", function(data) {
return pdfManager.ensureCatalog("openActionDestination");
});
handler.on("GetAttachments", function wphSetupGetAttachments(data) {
return pdfManager.ensureCatalog("attachments");
});
handler.on("GetJavaScript", function wphSetupGetJavaScript(data) {
return pdfManager.ensureCatalog("javaScript");
});
handler.on("GetOutline", function wphSetupGetOutline(data) {
return pdfManager.ensureCatalog("documentOutline");
});
handler.on("GetPermissions", function(data) {
return pdfManager.ensureCatalog("permissions");
});
handler.on("GetMetadata", function wphSetupGetMetadata(data) {
return Promise.all([
pdfManager.ensureDoc("documentInfo"),
pdfManager.ensureCatalog("metadata"),
]);
});
handler.on("GetData", function wphSetupGetData(data) {
pdfManager.requestLoadedStream();
return pdfManager.onLoadedStream().then(function(stream) {
return stream.bytes;
});
});
handler.on('GetStats',
function wphSetupGetStats(data) {
return pdfManager.pdfDocument.xref.stats;
}
);
handler.on("GetStats", function wphSetupGetStats(data) {
return pdfManager.pdfDocument.xref.stats;
});
handler.on('GetAnnotations', function({ pageIndex, intent, }) {
handler.on("GetAnnotations", function({ pageIndex, intent }) {
return pdfManager.getPage(pageIndex).then(function(page) {
return page.getAnnotationsData(intent);
});
});
handler.on('GetOperatorList', function wphSetupRenderPage(data, sink) {
handler.on(
"GetOperatorList",
function wphSetupRenderPage(data, sink) {
var pageIndex = data.pageIndex;
pdfManager.getPage(pageIndex).then(function(page) {
var task = new WorkerTask(`GetOperatorList: page ${pageIndex}`);
startWorkerTask(task);
// NOTE: Keep this condition in sync with the `info` helper function.
const start = verbosity >= VerbosityLevel.INFOS ? Date.now() : 0;
// Pre compile the pdf page and fetch the fonts/images.
page
.getOperatorList({
handler,
sink,
task,
intent: data.intent,
renderInteractiveForms: data.renderInteractiveForms,
})
.then(
function(operatorListInfo) {
finishWorkerTask(task);
if (start) {
info(
`page=${pageIndex + 1} - getOperatorList: time=` +
`${Date.now() - start}ms, len=${operatorListInfo.length}`
);
}
sink.close();
},
function(reason) {
finishWorkerTask(task);
if (task.terminated) {
return; // ignoring errors from the terminated thread
}
// For compatibility with older behavior, generating unknown
// unsupported feature notification on errors.
handler.send("UnsupportedFeature", {
featureId: UNSUPPORTED_FEATURES.unknown,
});
sink.error(reason);
// TODO: Should `reason` be re-thrown here (currently that casues
// "Uncaught exception: ..." messages in the console)?
}
);
});
},
this
);
handler.on("GetTextContent", function wphExtractText(data, sink) {
var pageIndex = data.pageIndex;
sink.onPull = function(desiredSize) {};
sink.onCancel = function(reason) {};
pdfManager.getPage(pageIndex).then(function(page) {
var task = new WorkerTask(`GetOperatorList: page ${pageIndex}`);
var task = new WorkerTask("GetTextContent: page " + pageIndex);
startWorkerTask(task);
// NOTE: Keep this condition in sync with the `info` helper function.
const start = (verbosity >= VerbosityLevel.INFOS ? Date.now() : 0);
const start = verbosity >= VerbosityLevel.INFOS ? Date.now() : 0;
// Pre compile the pdf page and fetch the fonts/images.
page.getOperatorList({
handler,
sink,
task,
intent: data.intent,
renderInteractiveForms: data.renderInteractiveForms,
}).then(function(operatorListInfo) {
finishWorkerTask(task);
page
.extractTextContent({
handler,
task,
sink,
normalizeWhitespace: data.normalizeWhitespace,
combineTextItems: data.combineTextItems,
})
.then(
function() {
finishWorkerTask(task);
if (start) {
info(`page=${pageIndex + 1} - getOperatorList: time=` +
`${Date.now() - start}ms, len=${operatorListInfo.length}`);
}
sink.close();
}, function(reason) {
finishWorkerTask(task);
if (task.terminated) {
return; // ignoring errors from the terminated thread
}
// For compatibility with older behavior, generating unknown
// unsupported feature notification on errors.
handler.send('UnsupportedFeature',
{ featureId: UNSUPPORTED_FEATURES.unknown, });
if (start) {
info(
`page=${pageIndex + 1} - getTextContent: time=` +
`${Date.now() - start}ms`
);
}
sink.close();
},
function(reason) {
finishWorkerTask(task);
if (task.terminated) {
return; // ignoring errors from the terminated thread
}
sink.error(reason);
sink.error(reason);
// TODO: Should `reason` be re-thrown here (currently that casues
// "Uncaught exception: ..." messages in the console)?
});
});
}, this);
handler.on('GetTextContent', function wphExtractText(data, sink) {
var pageIndex = data.pageIndex;
sink.onPull = function (desiredSize) { };
sink.onCancel = function (reason) { };
pdfManager.getPage(pageIndex).then(function(page) {
var task = new WorkerTask('GetTextContent: page ' + pageIndex);
startWorkerTask(task);
// NOTE: Keep this condition in sync with the `info` helper function.
const start = (verbosity >= VerbosityLevel.INFOS ? Date.now() : 0);
page.extractTextContent({
handler,
task,
sink,
normalizeWhitespace: data.normalizeWhitespace,
combineTextItems: data.combineTextItems,
}).then(function() {
finishWorkerTask(task);
if (start) {
info(`page=${pageIndex + 1} - getTextContent: time=` +
`${Date.now() - start}ms`);
}
sink.close();
}, function (reason) {
finishWorkerTask(task);
if (task.terminated) {
return; // ignoring errors from the terminated thread
}
sink.error(reason);
// TODO: Should `reason` be re-thrown here (currently that casues
// "Uncaught exception: ..." messages in the console)?
});
// TODO: Should `reason` be re-thrown here (currently that casues
// "Uncaught exception: ..." messages in the console)?
}
);
});
});
handler.on('FontFallback', function(data) {
handler.on("FontFallback", function(data) {
return pdfManager.fontFallback(data.id, handler);
});
handler.on('Cleanup', function wphCleanup(data) {
handler.on("Cleanup", function wphCleanup(data) {
return pdfManager.cleanup();
});
handler.on('Terminate', function wphTerminate(data) {
handler.on("Terminate", function wphTerminate(data) {
terminated = true;
if (pdfManager) {
pdfManager.terminate(new AbortException('Worker was terminated.'));
pdfManager.terminate(new AbortException("Worker was terminated."));
pdfManager = null;
}
if (cancelXHRs) {
cancelXHRs(new AbortException('Worker was terminated.'));
cancelXHRs(new AbortException("Worker was terminated."));
}
clearPrimitiveCaches();
var waitOn = [];
WorkerTasks.forEach(function (task) {
WorkerTasks.forEach(function(task) {
waitOn.push(task.finished);
task.terminate();
});
return Promise.all(waitOn).then(function () {
return Promise.all(waitOn).then(function() {
// Notice that even if we destroying handler, resolved response promise
// must be sent back.
handler.destroy();
@ -562,31 +615,33 @@ var WorkerMessageHandler = {
});
});
handler.on('Ready', function wphReady(data) {
handler.on("Ready", function wphReady(data) {
setupDoc(docParams);
docParams = null; // we don't need docParams anymore -- saving memory.
});
return workerHandlerName;
},
initializeFromPort(port) {
var handler = new MessageHandler('worker', 'main', port);
var handler = new MessageHandler("worker", "main", port);
WorkerMessageHandler.setup(handler, port);
handler.send('ready', null);
handler.send("ready", null);
},
};
function isMessagePort(maybePort) {
return typeof maybePort.postMessage === 'function' &&
('onmessage' in maybePort);
return (
typeof maybePort.postMessage === "function" && "onmessage" in maybePort
);
}
// Worker thread (and not node.js)?
if (typeof window === 'undefined' && !isNodeJS &&
typeof self !== 'undefined' && isMessagePort(self)) {
if (
typeof window === "undefined" &&
!isNodeJS &&
typeof self !== "undefined" &&
isMessagePort(self)
) {
WorkerMessageHandler.initializeFromPort(self);
}
export {
WorkerTask,
WorkerMessageHandler,
};
export { WorkerTask, WorkerMessageHandler };

View file

@ -14,7 +14,7 @@
*/
/* eslint no-var: error */
import { assert } from '../shared/util';
import { assert } from "../shared/util";
/** @implements {IPDFStream} */
class PDFWorkerStream {
@ -58,15 +58,16 @@ class PDFWorkerStreamReader {
this._isRangeSupported = false;
this._isStreamingSupported = false;
const readableStream = this._msgHandler.sendWithStream('GetReader');
const readableStream = this._msgHandler.sendWithStream("GetReader");
this._reader = readableStream.getReader();
this._headersReady = this._msgHandler.sendWithPromise('ReaderHeadersReady').
then((data) => {
this._isStreamingSupported = data.isStreamingSupported;
this._isRangeSupported = data.isRangeSupported;
this._contentLength = data.contentLength;
});
this._headersReady = this._msgHandler
.sendWithPromise("ReaderHeadersReady")
.then(data => {
this._isStreamingSupported = data.isStreamingSupported;
this._isRangeSupported = data.isRangeSupported;
this._contentLength = data.contentLength;
});
}
get headersReady() {
@ -86,13 +87,13 @@ class PDFWorkerStreamReader {
}
async read() {
const { value, done, } = await this._reader.read();
const { value, done } = await this._reader.read();
if (done) {
return { value: undefined, done: true, };
return { value: undefined, done: true };
}
// `value` is wrapped into Uint8Array, we need to
// unwrap it to ArrayBuffer for further processing.
return { value: value.buffer, done: false, };
return { value: value.buffer, done: false };
}
cancel(reason) {
@ -106,8 +107,10 @@ class PDFWorkerStreamRangeReader {
this._msgHandler = msgHandler;
this.onProgress = null;
const readableStream = this._msgHandler.sendWithStream('GetRangeReader',
{ begin, end, });
const readableStream = this._msgHandler.sendWithStream("GetRangeReader", {
begin,
end,
});
this._reader = readableStream.getReader();
}
@ -116,11 +119,11 @@ class PDFWorkerStreamRangeReader {
}
async read() {
const { value, done, } = await this._reader.read();
const { value, done } = await this._reader.read();
if (done) {
return { value: undefined, done: true, };
return { value: undefined, done: true };
}
return { value: value.buffer, done: false, };
return { value: value.buffer, done: false };
}
cancel(reason) {
@ -128,6 +131,4 @@ class PDFWorkerStreamRangeReader {
}
}
export {
PDFWorkerStream,
};
export { PDFWorkerStream };