Skip to content

Commit

Permalink
Merge pull request #8968 from Snuffleupagus/PDFFunctionFactory-2
Browse files Browse the repository at this point in the history
Split the existing `PDFFunction` in two classes, a private `PDFFunction` and a public `PDFFunctionFactory``, and utilize the latter in `PDFDocument` to allow various code to access the methods of `PDFFunction`
  • Loading branch information
Snuffleupagus authored Sep 29, 2017
2 parents f206ee5 + b8ec518 commit b3f8411
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 158 deletions.
38 changes: 21 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, pdfFunctionFactory) {
let IR = ColorSpace.parseToIR(cs, xref, res, pdfFunctionFactory);
return ColorSpace.fromIR(IR, pdfFunctionFactory);
};

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

Expand All @@ -230,21 +229,23 @@ var ColorSpace = (function ColorSpaceClosure() {
case 'PatternCS':
var basePatternCS = IR[1];
if (basePatternCS) {
basePatternCS = ColorSpace.fromIR(basePatternCS);
basePatternCS = ColorSpace.fromIR(basePatternCS, pdfFunctionFactory);
}
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,
pdfFunctionFactory),
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,
pdfFunctionFactory),
pdfFunctionFactory.createFromIR(tintFnIR));
case 'LabCS':
whitePoint = IR[1];
blackPoint = IR[2];
Expand All @@ -255,7 +256,7 @@ var ColorSpace = (function ColorSpaceClosure() {
}
};

ColorSpace.parseToIR = function ColorSpace_parseToIR(cs, xref, res) {
ColorSpace.parseToIR = function(cs, xref, res, pdfFunctionFactory) {
if (isName(cs)) {
var colorSpaces = res.get('ColorSpace');
if (isDict(colorSpaces)) {
Expand Down Expand Up @@ -317,10 +318,11 @@ 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,
pdfFunctionFactory);
// 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, pdfFunctionFactory);
if (altCS.numComps === numComps) {
return altIR;
}
Expand All @@ -337,12 +339,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,
pdfFunctionFactory);
}
return ['PatternCS', basePatternCS];
case 'Indexed':
case 'I':
var baseIndexedCS = ColorSpace.parseToIR(cs[1], xref, res);
var baseIndexedCS = ColorSpace.parseToIR(cs[1], xref, res,
pdfFunctionFactory);
var hiVal = xref.fetchIfRef(cs[2]) + 1;
var lookup = xref.fetchIfRef(cs[3]);
if (isStream(lookup)) {
Expand All @@ -353,8 +357,8 @@ 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, pdfFunctionFactory);
let tintFnIR = pdfFunctionFactory.createIR(xref.fetchIfRef(cs[3]));
return ['AlternateCS', numComps, alt, tintFnIR];
case 'Lab':
params = xref.fetchIfRef(cs[1]);
Expand Down
30 changes: 22 additions & 8 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { OperatorList, PartialEvaluator } from './evaluator';
import { AnnotationFactory } from './annotation';
import { calculateMD5 } from './crypto';
import { Linearization } from './parser';
import { PDFFunction } from './function';
import { PDFFunctionFactory } from './function';

var Page = (function PageClosure() {

Expand All @@ -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,12 @@ var PDFDocument = (function PDFDocumentClosure() {
this.pdfManager = pdfManager;
this.stream = stream;
this.xref = new XRef(stream, pdfManager);

let evaluatorOptions = pdfManager.evaluatorOptions;
this.pdfFunctionFactory = new PDFFunctionFactory({
xref: this.xref,
isEvalSupported: evaluatorOptions.isEvalSupported,
});
}

function find(stream, needle, limit, backwards) {
Expand Down Expand Up @@ -528,14 +537,19 @@ 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);
},
get numPages() {
var linearization = this.linearization;
Expand Down
Loading

0 comments on commit b3f8411

Please sign in to comment.