Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Re-factor PDFFunction to be class-like, and introduce a classFactory in PDFDocument that lazily creates an instance of PDFFunction #8931

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/core/colorspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import { FormatError, info, isString, shadow, warn } from '../shared/util';
import { isDict, isName, isStream } from './primitives';
import { PDFFunction } from './function';

var ColorSpace = (function ColorSpaceClosure() {
/**
Expand Down Expand Up @@ -200,12 +199,12 @@ var ColorSpace = (function ColorSpaceClosure() {
usesZeroToOneRange: true,
};

ColorSpace.parse = function ColorSpace_parse(cs, xref, res) {
let IR = ColorSpace.parseToIR(cs, xref, res);
return ColorSpace.fromIR(IR);
ColorSpace.parse = function(cs, xref, res, classFactory) {
let IR = ColorSpace.parseToIR(cs, xref, res, classFactory);
return ColorSpace.fromIR(IR, classFactory);
};

ColorSpace.fromIR = function ColorSpace_fromIR(IR) {
ColorSpace.fromIR = function(IR, classFactory) {
var name = Array.isArray(IR) ? IR[0] : IR;
var whitePoint, blackPoint, gamma;

Expand All @@ -230,21 +229,21 @@ var ColorSpace = (function ColorSpaceClosure() {
case 'PatternCS':
var basePatternCS = IR[1];
if (basePatternCS) {
basePatternCS = ColorSpace.fromIR(basePatternCS);
basePatternCS = ColorSpace.fromIR(basePatternCS, classFactory);
}
return new PatternCS(basePatternCS);
case 'IndexedCS':
var baseIndexedCS = IR[1];
var hiVal = IR[2];
var lookup = IR[3];
return new IndexedCS(ColorSpace.fromIR(baseIndexedCS), hiVal, lookup);
return new IndexedCS(ColorSpace.fromIR(baseIndexedCS, classFactory),
hiVal, lookup);
case 'AlternateCS':
var numComps = IR[1];
var alt = IR[2];
var tintFnIR = IR[3];

return new AlternateCS(numComps, ColorSpace.fromIR(alt),
PDFFunction.fromIR(tintFnIR));
return new AlternateCS(numComps, ColorSpace.fromIR(alt, classFactory),
classFactory.getPDFFunction().fromIR(tintFnIR));
case 'LabCS':
whitePoint = IR[1];
blackPoint = IR[2];
Expand All @@ -255,7 +254,7 @@ var ColorSpace = (function ColorSpaceClosure() {
}
};

ColorSpace.parseToIR = function ColorSpace_parseToIR(cs, xref, res) {
ColorSpace.parseToIR = function(cs, xref, res, classFactory) {
if (isName(cs)) {
var colorSpaces = res.get('ColorSpace');
if (isDict(colorSpaces)) {
Expand Down Expand Up @@ -317,10 +316,10 @@ var ColorSpace = (function ColorSpaceClosure() {
numComps = dict.get('N');
alt = dict.get('Alternate');
if (alt) {
var altIR = ColorSpace.parseToIR(alt, xref, res);
var altIR = ColorSpace.parseToIR(alt, xref, res, classFactory);
// Parse the /Alternate CS to ensure that the number of components
// are correct, and also (indirectly) that it is not a PatternCS.
var altCS = ColorSpace.fromIR(altIR);
var altCS = ColorSpace.fromIR(altIR, classFactory);
if (altCS.numComps === numComps) {
return altIR;
}
Expand All @@ -337,12 +336,14 @@ var ColorSpace = (function ColorSpaceClosure() {
case 'Pattern':
var basePatternCS = cs[1] || null;
if (basePatternCS) {
basePatternCS = ColorSpace.parseToIR(basePatternCS, xref, res);
basePatternCS = ColorSpace.parseToIR(basePatternCS, xref, res,
classFactory);
}
return ['PatternCS', basePatternCS];
case 'Indexed':
case 'I':
var baseIndexedCS = ColorSpace.parseToIR(cs[1], xref, res);
var baseIndexedCS = ColorSpace.parseToIR(cs[1], xref, res,
classFactory);
var hiVal = xref.fetchIfRef(cs[2]) + 1;
var lookup = xref.fetchIfRef(cs[3]);
if (isStream(lookup)) {
Expand All @@ -353,8 +354,9 @@ var ColorSpace = (function ColorSpaceClosure() {
case 'DeviceN':
var name = xref.fetchIfRef(cs[1]);
numComps = Array.isArray(name) ? name.length : 1;
alt = ColorSpace.parseToIR(cs[2], xref, res);
var tintFnIR = PDFFunction.getIR(xref, xref.fetchIfRef(cs[3]));
alt = ColorSpace.parseToIR(cs[2], xref, res, classFactory);
let tintFnIR =
classFactory.getPDFFunction().getIR(xref, xref.fetchIfRef(cs[3]));
return ['AlternateCS', numComps, alt, tintFnIR];
case 'Lab':
params = xref.fetchIfRef(cs[1]);
Expand Down
54 changes: 46 additions & 8 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ var Page = (function PageClosure() {
(intent === 'print' && annotation.printable);
}

function Page(pdfManager, xref, pageIndex, pageDict, ref, fontCache,
builtInCMapCache) {
function Page({ pdfManager, xref, pageIndex, pageDict, ref, fontCache,
builtInCMapCache, pdfFunctionFactory, }) {
this.pdfManager = pdfManager;
this.pageIndex = pageIndex;
this.pageDict = pageDict;
this.xref = xref;
this.ref = ref;
this.fontCache = fontCache;
this.builtInCMapCache = builtInCMapCache;
this.pdfFunctionFactory = pdfFunctionFactory;
this.evaluatorOptions = pdfManager.evaluatorOptions;
this.resourcesPromise = null;

Expand Down Expand Up @@ -215,6 +216,7 @@ var Page = (function PageClosure() {
fontCache: this.fontCache,
builtInCMapCache: this.builtInCMapCache,
options: this.evaluatorOptions,
pdfFunctionFactory: this.pdfFunctionFactory,
});

var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
Expand Down Expand Up @@ -290,6 +292,7 @@ var Page = (function PageClosure() {
fontCache: this.fontCache,
builtInCMapCache: this.builtInCMapCache,
options: this.evaluatorOptions,
pdfFunctionFactory: this.pdfFunctionFactory,
});

return partialEvaluator.getTextContent({
Expand Down Expand Up @@ -361,6 +364,35 @@ var PDFDocument = (function PDFDocumentClosure() {
this.pdfManager = pdfManager;
this.stream = stream;
this.xref = new XRef(stream, pdfManager);

let self = this, pdfFunction = null;
this.pdfFunctionFactory = {
_getInstance() {
if (!pdfFunction) {
let evaluatorOptions = pdfManager.evaluatorOptions;
pdfFunction = new PDFFunction({
isEvalSupported: evaluatorOptions.isEvalSupported,
});
}
return pdfFunction;
},

create(fn) {
return this._getInstance().parse(self.xref, fn);
},

createFromArray(fnArray) {
return this._getInstance().parseArray(self.xref, fnArray);
},

createFromIR(IR) {
return this._getInstance().fromIR(IR);
},

parseToIR(fn) {
return this._getInstance().getIR(self.xref, fn);
},
};
}

function find(stream, needle, limit, backwards) {
Expand Down Expand Up @@ -528,14 +560,20 @@ var PDFDocument = (function PDFDocumentClosure() {
this.xref.parse(recoveryMode);
var pageFactory = {
createPage: (pageIndex, dict, ref, fontCache, builtInCMapCache) => {
return new Page(this.pdfManager, this.xref, pageIndex, dict, ref,
fontCache, builtInCMapCache);
return new Page({
pdfManager: this.pdfManager,
xref: this.xref,
pageIndex,
pageDict: dict,
ref,
fontCache,
builtInCMapCache,
pdfFunctionFactory: this.pdfFunctionFactory,
});
},
};
this.catalog = new Catalog(this.pdfManager, this.xref, pageFactory);

let evaluatorOptions = this.pdfManager.evaluatorOptions;
PDFFunction.setIsEvalSupported(evaluatorOptions.isEvalSupported);
this.catalog = new Catalog(this.pdfManager, this.xref, pageFactory,
this.pdfFunctionFactory);
},
get numPages() {
var linearization = this.linearization;
Expand Down
Loading