Skip to content

Commit

Permalink
Removes global PDFJS usage from the src/core/.
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed Mar 3, 2016
1 parent e190603 commit f111142
Show file tree
Hide file tree
Showing 19 changed files with 351 additions and 277 deletions.
14 changes: 4 additions & 10 deletions src/core/bidi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('pdfjs/core/bidi', ['exports', 'pdfjs/shared/global'], factory);
define('pdfjs/core/bidi', ['exports'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports, require('../shared/global.js'));
factory(exports);
} else {
factory((root.pdfjsCoreBidi = {}), root.pdfjsSharedGlobal);
factory((root.pdfjsCoreBidi = {}));
}
}(this, function (exports, sharedGlobal) {
}(this, function (exports) {

var PDFJS = sharedGlobal.PDFJS;

var bidi = PDFJS.bidi = (function bidiClosure() {
// Character types for symbols from 0000 to 00FF.
var baseTypes = [
'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'S', 'B', 'S', 'WS',
Expand Down Expand Up @@ -430,8 +427,5 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
return createBidiText(chars.join(''), isLTR);
}

return bidi;
})();

exports.bidi = bidi;
}));
4 changes: 2 additions & 2 deletions src/core/cmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFJS */
/* globals window */

'use strict';

Expand Down Expand Up @@ -424,7 +424,7 @@ var IdentityCMap = (function IdentityCMapClosure() {

var BinaryCMapReader = (function BinaryCMapReaderClosure() {
function fetchBinaryData(url) {
var nonBinaryRequest = PDFJS.disableWorker;
var nonBinaryRequest = typeof window !== 'undefined';
var request = new XMLHttpRequest();
request.open('GET', url, false);
if (!nonBinaryRequest) {
Expand Down
7 changes: 5 additions & 2 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var Page = (function PageClosure() {
this.idCounters = {
obj: 0
};
this.evaluatorOptions = pdfManager.evaluatorOptions;
this.resourcesPromise = null;
}

Expand Down Expand Up @@ -224,7 +225,8 @@ var Page = (function PageClosure() {
handler, this.pageIndex,
'p' + this.pageIndex + '_',
this.idCounters,
this.fontCache);
this.fontCache,
this.evaluatorOptions);

var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
var pageListPromise = dataPromises.then(function(data) {
Expand Down Expand Up @@ -289,7 +291,8 @@ var Page = (function PageClosure() {
handler, self.pageIndex,
'p' + self.pageIndex + '_',
self.idCounters,
self.fontCache);
self.fontCache,
self.evaluatorOptions);

return partialEvaluator.getTextContent(contentStream,
task,
Expand Down
32 changes: 20 additions & 12 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFJS */

'use strict';

Expand Down Expand Up @@ -105,15 +104,23 @@ var getNormalizedUnicodes = coreUnicode.getNormalizedUnicodes;
var reverseIfRtl = coreUnicode.reverseIfRtl;

var PartialEvaluator = (function PartialEvaluatorClosure() {
var DefaultPartialEvaluatorOptions = {
forceDataSchema: false,
maxImageSize: -1,
disableFontFace: false,
cmapOptions: { url: null, packed: false }
};

function PartialEvaluator(pdfManager, xref, handler, pageIndex,
uniquePrefix, idCounters, fontCache) {
uniquePrefix, idCounters, fontCache, options) {
this.pdfManager = pdfManager;
this.xref = xref;
this.handler = handler;
this.pageIndex = pageIndex;
this.uniquePrefix = uniquePrefix;
this.idCounters = idCounters;
this.fontCache = fontCache;
this.options = options || DefaultPartialEvaluatorOptions;
}

// Trying to minimize Date.now() usage and check every 100 time
Expand Down Expand Up @@ -272,7 +279,8 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
warn('Image dimensions are missing, or not numbers.');
return;
}
if (PDFJS.maxImageSize !== -1 && w * h > PDFJS.maxImageSize) {
var maxImageSize = this.options.maxImageSize;
if (maxImageSize !== -1 && w * h > maxImageSize) {
warn('Image exceeded maximum allowed size and was removed.');
return;
}
Expand Down Expand Up @@ -336,11 +344,13 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
// These JPEGs don't need any more processing so we can just send it.
operatorList.addOp(OPS.paintJpegXObject, args);
this.handler.send('obj',
[objId, this.pageIndex, 'JpegStream', image.getIR()]);
[objId, this.pageIndex, 'JpegStream',
image.getIR(this.options.forceDataSchema)]);
return;
}

PDFImage.buildImage(self.handler, self.xref, resources, image, inline).
PDFImage.buildImage(self.handler, self.xref, resources, image, inline,
this.options.forceDataSchema).
then(function(imageObj) {
var imgData = imageObj.createImageData(/* forceRGBA = */ false);
self.handler.send('obj', [objId, self.pageIndex, 'Image', imgData],
Expand Down Expand Up @@ -448,7 +458,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var glyphs = font.charsToGlyphs(chars);
var isAddToPathSet = !!(state.textRenderingMode &
TextRenderingMode.ADD_TO_PATH_FLAG);
if (font.data && (isAddToPathSet || PDFJS.disableFontFace)) {
if (font.data && (isAddToPathSet || this.options.disableFontFace)) {
var buildPath = function (fontChar) {
if (!font.renderer.hasBuiltPath(fontChar)) {
var path = font.renderer.getPathJs(fontChar);
Expand Down Expand Up @@ -1172,7 +1182,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {

function runBidiTransform(textChunk) {
var str = textChunk.str.join('');
var bidiResult = PDFJS.bidi(str, -1, textChunk.vertical);
var bidiResult = bidi(str, -1, textChunk.vertical);
return {
str: (normalizeWhitespace ? replaceWhitespace(bidiResult.str) :
bidiResult.str),
Expand Down Expand Up @@ -1642,15 +1652,13 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
readToUnicode: function PartialEvaluator_readToUnicode(toUnicode) {
var cmap, cmapObj = toUnicode;
if (isName(cmapObj)) {
cmap = CMapFactory.create(cmapObj,
{ url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
cmap = CMapFactory.create(cmapObj, this.options.cmapOptions, null);
if (cmap instanceof IdentityCMap) {
return new IdentityToUnicodeMap(0, 0xFFFF);
}
return new ToUnicodeMap(cmap.getMap());
} else if (isStream(cmapObj)) {
cmap = CMapFactory.create(cmapObj,
{ url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
cmap = CMapFactory.create(cmapObj, this.options.cmapOptions, null);
if (cmap instanceof IdentityCMap) {
return new IdentityToUnicodeMap(0, 0xFFFF);
}
Expand Down Expand Up @@ -2064,7 +2072,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
properties.cidEncoding = cidEncoding.name;
}
properties.cMap = CMapFactory.create(cidEncoding,
{ url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
this.options.cmapOptions, null);
properties.vertical = properties.cMap.vertical;
}
this.extractDataStructures(dict, baseDict, xref, properties);
Expand Down
11 changes: 5 additions & 6 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFJS */

'use strict';

Expand Down Expand Up @@ -464,7 +463,7 @@ var ProblematicCharRanges = new Int32Array([
* type1Font.bind();
*/
var Font = (function FontClosure() {
function Font(name, file, properties) {
function Font(name, file, properties, cmapOptions) {
var charCode, glyphName, fontChar;

this.name = name;
Expand Down Expand Up @@ -500,7 +499,8 @@ var Font = (function FontClosure() {
this.fontMatrix = properties.fontMatrix;
this.bbox = properties.bbox;

this.toUnicode = properties.toUnicode = this.buildToUnicode(properties);
this.toUnicode = properties.toUnicode =
this.buildToUnicode(properties, cmapOptions);

this.toFontChar = [];

Expand Down Expand Up @@ -2632,7 +2632,7 @@ var Font = (function FontClosure() {
* @param {Object} properties Font properties object.
* @return {Object} A ToUnicodeMap object.
*/
buildToUnicode: function Font_buildToUnicode(properties) {
buildToUnicode: function Font_buildToUnicode(properties, cmapOptions) {
// Section 9.10.2 Mapping Character Codes to Unicode Values
if (properties.toUnicode && properties.toUnicode.length !== 0) {
return properties.toUnicode;
Expand Down Expand Up @@ -2730,8 +2730,7 @@ var Font = (function FontClosure() {
var ucs2CMapName = new Name(registry + '-' + ordering + '-UCS2');
// d) Obtain the CMap with the name constructed in step (c) (available
// from the ASN Web site; see the Bibliography).
var ucs2CMap = CMapFactory.create(ucs2CMapName,
{ url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
var ucs2CMap = CMapFactory.create(ucs2CMapName, cmapOptions, null);
var cMap = properties.cMap;
toUnicode = [];
cMap.forEach(function(charcode, cid) {
Expand Down
17 changes: 11 additions & 6 deletions src/core/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ var PDFImage = (function PDFImageClosure() {
* Decode the image in the main thread if it supported. Resovles the promise
* when the image data is ready.
*/
function handleImageData(handler, xref, res, image) {
function handleImageData(handler, xref, res, image, forceDataSchema) {
if (image instanceof JpegStream && image.isNativelyDecodable(xref, res)) {
// For natively supported jpegs send them to the main thread for decoding.
var dict = image.dict;
var colorSpace = dict.get('ColorSpace', 'CS');
colorSpace = ColorSpace.parse(colorSpace, xref, res);
var numComps = colorSpace.numComps;
var decodePromise = handler.sendWithPromise('JpegDecode',
[image.getIR(), numComps]);
[image.getIR(forceDataSchema),
numComps]);
return decodePromise.then(function (message) {
var data = message.data;
return new Stream(data, 0, data.length, image.dict);
Expand Down Expand Up @@ -184,22 +185,26 @@ var PDFImage = (function PDFImageClosure() {
* with a PDFImage when the image is ready to be used.
*/
PDFImage.buildImage = function PDFImage_buildImage(handler, xref,
res, image, inline) {
var imagePromise = handleImageData(handler, xref, res, image);
res, image, inline,
forceDataSchema) {
var imagePromise = handleImageData(handler, xref, res, image,
forceDataSchema);
var smaskPromise;
var maskPromise;

var smask = image.dict.get('SMask');
var mask = image.dict.get('Mask');

if (smask) {
smaskPromise = handleImageData(handler, xref, res, smask);
smaskPromise = handleImageData(handler, xref, res, smask,
forceDataSchema);
maskPromise = Promise.resolve(null);
} else {
smaskPromise = Promise.resolve(null);
if (mask) {
if (isStream(mask)) {
maskPromise = handleImageData(handler, xref, res, mask);
maskPromise = handleImageData(handler, xref, res, mask,
forceDataSchema);
} else if (isArray(mask)) {
maskPromise = Promise.resolve(mask);
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/core/pdf_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ var BasePdfManager = (function BasePdfManagerClosure() {
})();

var LocalPdfManager = (function LocalPdfManagerClosure() {
function LocalPdfManager(docId, data, password) {
function LocalPdfManager(docId, data, password, evaluatorOptions) {
this._docId = docId;
this.evaluatorOptions = evaluatorOptions;
var stream = new Stream(data);
this.pdfDocument = new PDFDocument(this, stream, password);
this._loadedStreamCapability = createPromiseCapability();
Expand Down Expand Up @@ -157,9 +158,10 @@ var LocalPdfManager = (function LocalPdfManagerClosure() {
})();

var NetworkPdfManager = (function NetworkPdfManagerClosure() {
function NetworkPdfManager(docId, pdfNetworkStream, args) {
function NetworkPdfManager(docId, pdfNetworkStream, args, evaluatorOptions) {
this._docId = docId;
this.msgHandler = args.msgHandler;
this.evaluatorOptions = evaluatorOptions;

var params = {
msgHandler: args.msgHandler,
Expand Down
6 changes: 3 additions & 3 deletions src/core/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFJS */

'use strict';

Expand All @@ -36,6 +35,7 @@ var Util = sharedUtil.Util;
var error = sharedUtil.error;
var info = sharedUtil.info;
var isArray = sharedUtil.isArray;
var createObjectURL = sharedUtil.createObjectURL;
var shadow = sharedUtil.shadow;
var warn = sharedUtil.warn;
var Dict = corePrimitives.Dict;
Expand Down Expand Up @@ -963,8 +963,8 @@ var JpegStream = (function JpegStreamClosure() {
return this.buffer;
};

JpegStream.prototype.getIR = function JpegStream_getIR() {
return PDFJS.createObjectURL(this.bytes, 'image/jpeg');
JpegStream.prototype.getIR = function JpegStream_getIR(forceDataSchema) {
return createObjectURL(this.bytes, 'image/jpeg', forceDataSchema);
};
/**
* Checks if the image can be decoded and displayed by the browser without any
Expand Down
Loading

0 comments on commit f111142

Please sign in to comment.