pdf.js/src/core.js

860 lines
28 KiB
JavaScript
Raw Normal View History

2011-10-25 10:13:12 +09:00
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
2011-10-26 10:18:22 +09:00
'use strict';
var globalScope = (typeof window === 'undefined') ? this : window;
var isWorker = (typeof window == 'undefined');
2011-10-25 10:13:12 +09:00
var ERRORS = 0, WARNINGS = 1, TODOS = 5;
var verbosity = WARNINGS;
2011-10-26 07:43:41 +09:00
2011-10-27 03:46:57 +09:00
// The global PDFJS object exposes the API
2011-10-26 07:43:41 +09:00
// In production, it will be declared outside a global wrapper
// In development, it will be declared here
2011-10-27 03:46:57 +09:00
if (!globalScope.PDFJS) {
globalScope.PDFJS = {};
2011-10-26 07:43:41 +09:00
}
2011-10-25 10:13:12 +09:00
// getPdf()
// Convenience function to perform binary Ajax GET
// Usage: getPdf('http://...', callback)
// getPdf({
// url:String ,
// [,progress:Function, error:Function]
// },
// callback)
function getPdf(arg, callback) {
var params = arg;
if (typeof arg === 'string')
params = { url: arg };
var xhr = new XMLHttpRequest();
xhr.open('GET', params.url);
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
var protocol = params.url.indexOf(':') < 0 ? window.location.protocol :
params.url.substring(0, params.url.indexOf(':') + 1);
xhr.expected = (protocol === 'http:' || protocol === 'https:') ? 200 : 0;
2011-10-25 10:13:12 +09:00
if ('progress' in params)
xhr.onprogress = params.progress || undefined;
if ('error' in params)
xhr.onerror = params.error || undefined;
xhr.onreadystatechange = function getPdfOnreadystatechange(e) {
if (xhr.readyState === 4) {
if (xhr.status === xhr.expected) {
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
xhr.responseArrayBuffer || xhr.response);
callback(data);
2011-12-02 02:11:33 +09:00
} else if (params.error) {
params.error(e);
}
2011-10-25 10:13:12 +09:00
}
};
xhr.send(null);
}
2011-10-27 03:46:57 +09:00
globalScope.PDFJS.getPdf = getPdf;
2012-02-16 07:55:16 +09:00
globalScope.PDFJS.pdfBug = false;
2011-10-25 10:13:12 +09:00
2011-12-09 07:18:43 +09:00
var Page = (function PageClosure() {
function Page(xref, pageNumber, pageDict, ref) {
2011-10-25 10:13:12 +09:00
this.pageNumber = pageNumber;
this.pageDict = pageDict;
this.stats = {
create: Date.now(),
compile: 0.0,
fonts: 0.0,
images: 0.0,
render: 0.0
};
this.xref = xref;
this.ref = ref;
this.displayReadyPromise = null;
2011-10-25 10:13:12 +09:00
}
2011-12-09 07:18:43 +09:00
Page.prototype = {
2011-10-25 10:13:12 +09:00
getPageProp: function pageGetPageProp(key) {
return this.xref.fetchIfRef(this.pageDict.get(key));
},
inheritPageProp: function pageInheritPageProp(key) {
var dict = this.pageDict;
var obj = dict.get(key);
while (obj === undefined) {
dict = this.xref.fetchIfRef(dict.get('Parent'));
if (!dict)
break;
obj = dict.get(key);
}
return obj;
},
get content() {
return shadow(this, 'content', this.getPageProp('Contents'));
},
get resources() {
return shadow(this, 'resources', this.inheritPageProp('Resources'));
},
get mediaBox() {
var obj = this.inheritPageProp('MediaBox');
// Reset invalid media box to letter size.
if (!isArray(obj) || obj.length !== 4)
obj = [0, 0, 612, 792];
return shadow(this, 'mediaBox', obj);
},
get view() {
2012-02-15 04:48:58 +09:00
var cropBox = this.inheritPageProp('CropBox');
2011-10-25 10:13:12 +09:00
var view = {
x: 0,
y: 0,
width: this.width,
height: this.height
};
2012-02-15 04:48:58 +09:00
if (!isArray(cropBox) || cropBox.length !== 4)
2012-02-17 02:02:18 +09:00
return shadow(this, 'view', view);
2012-02-15 04:48:58 +09:00
2011-12-31 09:52:15 +09:00
var mediaBox = this.mediaBox;
var offsetX = mediaBox[0], offsetY = mediaBox[1];
2012-02-15 04:48:58 +09:00
// From the spec, 6th ed., p.963:
// "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."
cropBox = Util.intersect(cropBox, mediaBox);
if (!cropBox)
2012-02-17 02:02:18 +09:00
return shadow(this, 'view', view);
2012-02-15 04:48:58 +09:00
var tl = this.rotatePoint(cropBox[0] - offsetX, cropBox[1] - offsetY);
var br = this.rotatePoint(cropBox[2] - offsetX, cropBox[3] - offsetY);
view.x = Math.min(tl.x, br.x);
view.y = Math.min(tl.y, br.y);
view.width = Math.abs(tl.x - br.x);
view.height = Math.abs(tl.y - br.y);
2011-10-25 10:13:12 +09:00
2012-02-17 02:02:18 +09:00
return shadow(this, 'view', view);
2011-10-25 10:13:12 +09:00
},
get annotations() {
return shadow(this, 'annotations', this.inheritPageProp('Annots'));
},
get width() {
var mediaBox = this.mediaBox;
var rotate = this.rotate;
var width;
if (rotate == 0 || rotate == 180) {
width = (mediaBox[2] - mediaBox[0]);
} else {
width = (mediaBox[3] - mediaBox[1]);
}
return shadow(this, 'width', width);
},
get height() {
var mediaBox = this.mediaBox;
var rotate = this.rotate;
var height;
if (rotate == 0 || rotate == 180) {
height = (mediaBox[3] - mediaBox[1]);
} else {
height = (mediaBox[2] - mediaBox[0]);
}
return shadow(this, 'height', height);
},
get rotate() {
var rotate = this.inheritPageProp('Rotate') || 0;
// Normalize rotation so it's a multiple of 90 and between 0 and 270
if (rotate % 90 != 0) {
rotate = 0;
} else if (rotate >= 360) {
rotate = rotate % 360;
} else if (rotate < 0) {
// The spec doesn't cover negatives, assume its counterclockwise
// rotation. The following is the other implementation of modulo.
rotate = ((rotate % 360) + 360) % 360;
}
return shadow(this, 'rotate', rotate);
},
2011-10-30 19:41:55 +09:00
startRenderingFromIRQueue: function pageStartRenderingFromIRQueue(
2011-10-25 10:13:12 +09:00
IRQueue, fonts) {
var self = this;
this.IRQueue = IRQueue;
var displayContinuation = function pageDisplayContinuation() {
// Always defer call to display() to work around bug in
// Firefox error reporting from XHR callbacks.
setTimeout(function pageSetTimeout() {
self.displayReadyPromise.resolve();
2011-10-25 10:13:12 +09:00
});
};
2011-10-30 19:41:55 +09:00
this.ensureFonts(fonts,
function pageStartRenderingFromIRQueueEnsureFonts() {
2011-10-25 10:13:12 +09:00
displayContinuation();
});
},
2011-10-30 19:41:55 +09:00
getIRQueue: function pageGetIRQueue(handler, dependency) {
2011-10-25 10:13:12 +09:00
if (this.IRQueue) {
// content was compiled
return this.IRQueue;
}
var xref = this.xref;
var content = xref.fetchIfRef(this.content);
var resources = xref.fetchIfRef(this.resources);
if (isArray(content)) {
// fetching items
var i, n = content.length;
for (i = 0; i < n; ++i)
content[i] = xref.fetchIfRef(content[i]);
content = new StreamsSequenceStream(content);
} else if (!content) {
// replacing non-existent page content with empty one
content = new Stream(new Uint8Array(0));
2011-10-25 10:13:12 +09:00
}
var pe = this.pe = new PartialEvaluator(
xref, handler, 'p' + this.pageNumber + '_');
var IRQueue = {};
2011-11-04 06:26:58 +09:00
return (this.IRQueue = pe.getIRQueue(content, resources, IRQueue,
dependency));
2011-10-25 10:13:12 +09:00
},
2011-10-30 19:41:55 +09:00
ensureFonts: function pageEnsureFonts(fonts, callback) {
2011-10-25 10:13:12 +09:00
// Convert the font names to the corresponding font obj.
2011-11-03 04:21:45 +09:00
for (var i = 0, ii = fonts.length; i < ii; i++) {
2011-10-25 10:13:12 +09:00
fonts[i] = this.objs.objs[fonts[i]].data;
}
// Load all the fonts
var fontObjs = FontLoader.bind(
fonts,
2011-10-30 19:41:55 +09:00
function pageEnsureFontsFontObjs(fontObjs) {
2011-10-25 10:13:12 +09:00
this.stats.fonts = Date.now();
callback.call(this);
}.bind(this),
this.objs
);
},
2011-10-30 19:41:55 +09:00
display: function pageDisplay(gfx, callback) {
2011-10-25 10:13:12 +09:00
var xref = this.xref;
var resources = xref.fetchIfRef(this.resources);
var mediaBox = xref.fetchIfRef(this.mediaBox);
assertWellFormed(isDict(resources), 'invalid page resources');
gfx.xref = xref;
gfx.res = resources;
gfx.beginDrawing({ x: mediaBox[0], y: mediaBox[1],
width: this.width,
height: this.height,
rotate: this.rotate });
var startIdx = 0;
var length = this.IRQueue.fnArray.length;
var IRQueue = this.IRQueue;
var stepper = null;
2012-02-16 07:12:58 +09:00
if (PDFJS.pdfBug && StepperManager.enabled) {
stepper = StepperManager.create(this.pageNumber);
stepper.init(IRQueue);
stepper.nextBreakPoint = stepper.getNextBreakPoint();
}
2011-10-25 10:13:12 +09:00
var self = this;
function next() {
startIdx = gfx.executeIRQueue(IRQueue, startIdx, next, stepper);
2011-10-25 10:13:12 +09:00
if (startIdx == length) {
self.stats.render = Date.now();
2011-10-29 06:37:55 +09:00
gfx.endDrawing();
2011-10-25 10:13:12 +09:00
if (callback) callback();
}
}
next();
},
rotatePoint: function pageRotatePoint(x, y, reverse) {
var rotate = reverse ? (360 - this.rotate) : this.rotate;
switch (rotate) {
case 180:
return {x: this.width - x, y: y};
case 90:
return {x: this.width - y, y: this.height - x};
case 270:
return {x: y, y: x};
case 360:
case 0:
default:
return {x: x, y: this.height - y};
}
},
getLinks: function pageGetLinks() {
2011-11-03 10:46:39 +09:00
var links = [];
var annotations = pageGetAnnotations();
var i, n = annotations.length;
for (i = 0; i < n; ++i) {
if (annotations[i].type != 'Link')
continue;
links.push(annotations[i]);
}
return links;
},
getAnnotations: function pageGetAnnotations() {
2011-10-25 10:13:12 +09:00
var xref = this.xref;
function getInheritableProperty(annotation, name) {
var item = annotation;
while (item && !item.has(name)) {
item = xref.fetchIfRef(item.get('Parent'));
}
if (!item)
return null;
return item.get(name);
}
2011-10-25 10:13:12 +09:00
var annotations = xref.fetchIfRef(this.annotations) || [];
var i, n = annotations.length;
2011-11-03 10:46:39 +09:00
var items = [];
2011-10-25 10:13:12 +09:00
for (i = 0; i < n; ++i) {
var annotationRef = annotations[i];
var annotation = xref.fetch(annotationRef);
2011-10-25 10:13:12 +09:00
if (!isDict(annotation))
continue;
var subtype = annotation.get('Subtype');
2011-11-03 10:46:39 +09:00
if (!isName(subtype))
2011-10-25 10:13:12 +09:00
continue;
var rect = annotation.get('Rect');
var topLeftCorner = this.rotatePoint(rect[0], rect[1]);
var bottomRightCorner = this.rotatePoint(rect[2], rect[3]);
2011-11-03 10:46:39 +09:00
var item = {};
item.type = subtype.name;
item.x = Math.min(topLeftCorner.x, bottomRightCorner.x);
item.y = Math.min(topLeftCorner.y, bottomRightCorner.y);
item.width = Math.abs(topLeftCorner.x - bottomRightCorner.x);
item.height = Math.abs(topLeftCorner.y - bottomRightCorner.y);
switch (subtype.name) {
case 'Link':
var a = this.xref.fetchIfRef(annotation.get('A'));
if (a) {
switch (a.get('S').name) {
case 'URI':
item.url = a.get('URI');
2011-11-03 10:46:39 +09:00
break;
case 'GoTo':
item.dest = a.get('D');
2011-11-03 10:46:39 +09:00
break;
default:
TODO('other link types');
}
} else if (annotation.has('Dest')) {
// simple destination link
var dest = annotation.get('Dest');
item.dest = isName(dest) ? dest.name : dest;
2011-11-03 10:46:39 +09:00
}
break;
case 'Widget':
var fieldType = getInheritableProperty(annotation, 'FT');
2011-11-03 10:46:39 +09:00
if (!isName(fieldType))
2011-10-25 10:13:12 +09:00
break;
2011-11-03 10:46:39 +09:00
item.fieldType = fieldType.name;
// Building the full field name by collecting the field and
// its ancestors 'T' properties and joining them using '.'.
2011-11-03 10:46:39 +09:00
var fieldName = [];
var namedItem = annotation, ref = annotationRef;
while (namedItem) {
var parentRef = namedItem.get('Parent');
var parent = xref.fetchIfRef(parentRef);
var name = namedItem.get('T');
2011-11-03 10:46:39 +09:00
if (name)
fieldName.unshift(stringToPDFString(name));
else {
// The field name is absent, that means more than one field
// with the same name may exist. Replacing the empty name
// with the '`' plus index in the parent's 'Kids' array.
// This is not in the PDF spec but necessary to id the
// the input controls.
var kids = xref.fetchIfRef(parent.get('Kids'));
var j, jj;
for (j = 0, jj = kids.length; j < jj; j++) {
if (kids[j].num == ref.num && kids[j].gen == ref.gen)
break;
}
fieldName.unshift('`' + j);
}
namedItem = parent;
ref = parentRef;
2011-11-03 10:46:39 +09:00
}
item.fullName = fieldName.join('.');
var alternativeText = stringToPDFString(annotation.get('TU') || '');
item.alternativeText = alternativeText;
var da = getInheritableProperty(annotation, 'DA') || '';
2011-11-03 10:46:39 +09:00
var m = /([\d\.]+)\sTf/.exec(da);
if (m)
item.fontSize = parseFloat(m[1]);
item.textAlignment = getInheritableProperty(annotation, 'Q');
item.flags = getInheritableProperty(annotation, 'Ff') || 0;
2011-11-03 10:46:39 +09:00
break;
case 'Text':
var content = annotation.get('Contents');
var title = annotation.get('T');
2011-12-22 07:37:52 +09:00
item.content = stringToPDFString(content || '');
item.title = stringToPDFString(title || '');
item.name = annotation.get('Name').name;
break;
2011-12-22 07:37:52 +09:00
default:
TODO('unimplemented annotation type: ' + subtype.name);
break;
2011-10-25 10:13:12 +09:00
}
2011-11-03 10:46:39 +09:00
items.push(item);
2011-10-25 10:13:12 +09:00
}
2011-11-03 10:46:39 +09:00
return items;
2011-10-25 10:13:12 +09:00
},
startRendering: function pageStartRendering(ctx, callback, textLayer) {
2011-10-25 10:13:12 +09:00
this.startRenderingTime = Date.now();
// If there is no displayReadyPromise yet, then the IRQueue was never
// requested before. Make the request and create the promise.
if (!this.displayReadyPromise) {
this.pdf.startRendering(this);
this.displayReadyPromise = new Promise();
}
// Once the IRQueue and fonts are loaded, perform the actual rendering.
2012-01-05 09:13:53 +09:00
this.displayReadyPromise.then(
function pageDisplayReadyPromise() {
var gfx = new CanvasGraphics(ctx, this.objs, textLayer);
try {
this.display(gfx, callback);
} catch (e) {
if (callback)
callback(e);
else
2012-01-25 05:04:59 +09:00
error(e);
2012-01-05 09:13:53 +09:00
}
}.bind(this),
function pageDisplayReadPromiseError(reason) {
if (callback)
callback(reason);
else
2012-01-25 05:04:59 +09:00
error(reason);
}
2012-01-05 09:13:53 +09:00
);
2011-10-25 10:13:12 +09:00
}
};
2011-12-09 07:18:43 +09:00
return Page;
2011-10-25 10:13:12 +09:00
})();
/**
* The `PDFDocModel` holds all the data of the PDF file. Compared to the
* `PDFDoc`, this one doesn't have any job management code.
* Right now there exists one PDFDocModel on the main thread + one object
* for each worker. If there is no worker support enabled, there are two
* `PDFDocModel` objects on the main thread created.
* TODO: Refactor the internal object structure, such that there is no
* need for the `PDFDocModel` anymore and there is only one object on the
* main thread and not one entire copy on each worker instance.
*/
2011-12-09 07:18:43 +09:00
var PDFDocModel = (function PDFDocModelClosure() {
function PDFDocModel(arg, callback) {
2011-10-25 10:13:12 +09:00
if (isStream(arg))
init.call(this, arg);
else if (isArrayBuffer(arg))
init.call(this, new Stream(arg));
else
error('PDFDocModel: Unknown argument type');
}
function init(stream) {
assertWellFormed(stream.length > 0, 'stream must have data');
this.stream = stream;
this.setup();
2011-11-03 10:46:39 +09:00
this.acroForm = this.xref.fetchIfRef(this.catalog.catDict.get('AcroForm'));
2011-10-25 10:13:12 +09:00
}
function find(stream, needle, limit, backwards) {
var pos = stream.pos;
var end = stream.end;
var str = '';
if (pos + limit > end)
limit = end - pos;
for (var n = 0; n < limit; ++n)
str += stream.getChar();
stream.pos = pos;
var index = backwards ? str.lastIndexOf(needle) : str.indexOf(needle);
if (index == -1)
return false; /* not found */
stream.pos += index;
return true; /* found */
}
2011-12-09 07:18:43 +09:00
PDFDocModel.prototype = {
2011-10-25 10:13:12 +09:00
get linearization() {
var length = this.stream.length;
var linearization = false;
if (length) {
linearization = new Linearization(this.stream);
if (linearization.length != length)
linearization = false;
}
// shadow the prototype getter with a data property
return shadow(this, 'linearization', linearization);
},
get startXRef() {
var stream = this.stream;
var startXRef = 0;
var linearization = this.linearization;
if (linearization) {
// Find end of first obj.
stream.reset();
if (find(stream, 'endobj', 1024))
startXRef = stream.pos + 6;
} else {
2011-12-05 07:00:22 +09:00
// Find startxref by jumping backward from the end of the file.
var step = 1024;
var found = false, pos = stream.end;
2011-12-05 07:00:22 +09:00
while (!found && pos > 0) {
pos -= step - 'startxref'.length;
if (pos < 0)
pos = 0;
stream.pos = pos;
2011-12-05 07:00:22 +09:00
found = find(stream, 'startxref', step, true);
}
if (found) {
2011-10-25 10:13:12 +09:00
stream.skip(9);
var ch;
do {
ch = stream.getChar();
} while (Lexer.isSpace(ch));
var str = '';
while ((ch - '0') <= 9) {
str += ch;
ch = stream.getChar();
}
startXRef = parseInt(str, 10);
if (isNaN(startXRef))
startXRef = 0;
}
}
// shadow the prototype getter with a data property
return shadow(this, 'startXRef', startXRef);
},
get mainXRefEntriesOffset() {
var mainXRefEntriesOffset = 0;
var linearization = this.linearization;
if (linearization)
mainXRefEntriesOffset = linearization.mainXRefEntriesOffset;
// shadow the prototype getter with a data property
return shadow(this, 'mainXRefEntriesOffset', mainXRefEntriesOffset);
},
// Find the header, remove leading garbage and setup the stream
// starting from the header.
checkHeader: function pdfDocCheckHeader() {
var stream = this.stream;
stream.reset();
if (find(stream, '%PDF-', 1024)) {
// Found the header, trim off any garbage before it.
stream.moveStart();
return;
}
// May not be a PDF file, continue anyway.
},
setup: function pdfDocSetup(ownerPassword, userPassword) {
this.checkHeader();
var xref = new XRef(this.stream,
this.startXRef,
this.mainXRefEntriesOffset);
this.xref = xref;
this.catalog = new Catalog(xref);
if (xref.trailer && xref.trailer.has('ID')) {
var fileID = '';
var id = xref.fetchIfRef(xref.trailer.get('ID'))[0];
id.split('').forEach(function(el) {
fileID += Number(el.charCodeAt(0)).toString(16);
});
this.fileID = fileID;
}
2011-10-25 10:13:12 +09:00
},
get numPages() {
var linearization = this.linearization;
var num = linearization ? linearization.numPages : this.catalog.numPages;
// shadow the prototype getter
return shadow(this, 'numPages', num);
},
getFingerprint: function pdfDocGetFingerprint() {
if (this.fileID) {
return this.fileID;
} else {
// If we got no fileID, then we generate one,
// from the first 100 bytes of PDF
var data = this.stream.bytes.subarray(0, 100);
var hash = calculateMD5(data, 0, data.length);
var strHash = '';
for (var i = 0, length = hash.length; i < length; i++) {
strHash += Number(hash[i]).toString(16);
}
return strHash;
}
},
2011-10-25 10:13:12 +09:00
getPage: function pdfDocGetPage(n) {
return this.catalog.getPage(n);
}
};
2011-12-09 07:18:43 +09:00
return PDFDocModel;
2011-10-25 10:13:12 +09:00
})();
2011-12-09 07:18:43 +09:00
var PDFDoc = (function PDFDocClosure() {
function PDFDoc(arg, callback) {
2011-10-25 10:13:12 +09:00
var stream = null;
var data = null;
if (isStream(arg)) {
stream = arg;
data = arg.bytes;
} else if (isArrayBuffer(arg)) {
stream = new Stream(arg);
data = arg;
} else {
error('PDFDoc: Unknown argument type');
}
this.data = data;
this.stream = stream;
this.pdf = new PDFDocModel(stream);
this.fingerprint = this.pdf.getFingerprint();
2011-10-25 10:13:12 +09:00
this.catalog = this.pdf.catalog;
this.objs = new PDFObjects();
this.pageCache = [];
this.fontsLoading = {};
this.workerReadyPromise = new Promise('workerReady');
2011-10-25 10:13:12 +09:00
// If worker support isn't disabled explicit and the browser has worker
// support, create a new web worker and test if it/the browser fullfills
// all requirements to run parts of pdf.js in a web worker.
// Right now, the requirement is, that an Uint8Array is still an Uint8Array
// as it arrives on the worker. Chrome added this with version 15.
if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') {
var workerSrc = PDFJS.workerSrc;
if (typeof workerSrc === 'undefined') {
2012-01-25 05:04:59 +09:00
error('No PDFJS.workerSrc specified');
}
try {
var worker;
if (PDFJS.isFirefoxExtension) {
// The firefox extension can't load the worker from the resource://
// url so we have to inline the script and then use the blob loader.
var bb = new MozBlobBuilder();
bb.append(document.querySelector('#PDFJS_SCRIPT_TAG').textContent);
var blobUrl = window.URL.createObjectURL(bb.getBlob());
worker = new Worker(blobUrl);
} else {
// Some versions of FF can't create a worker on localhost, see:
// https://bugzilla.mozilla.org/show_bug.cgi?id=683280
worker = new Worker(workerSrc);
}
2011-12-29 01:32:54 +09:00
var messageHandler = new MessageHandler('main', worker);
messageHandler.on('test', function pdfDocTest(supportTypedArray) {
if (supportTypedArray) {
this.worker = worker;
this.setupMessageHandler(messageHandler);
} else {
globalScope.PDFJS.disableWorker = true;
this.setupFakeWorker();
}
}.bind(this));
2011-10-25 10:13:12 +09:00
var testObj = new Uint8Array(1);
2011-12-29 03:57:12 +09:00
// Some versions of Opera throw a DATA_CLONE_ERR on
2011-12-29 01:32:54 +09:00
// serializing the typed array.
messageHandler.send('test', testObj);
return;
} catch (e) {
2012-01-24 10:52:53 +09:00
warn('The worker has been disabled.');
}
}
2011-12-28 23:23:39 +09:00
// Either workers are disabled, not supported or have thrown an exception.
// Thus, we fallback to a faked worker.
globalScope.PDFJS.disableWorker = true;
this.setupFakeWorker();
}
2011-12-09 07:18:43 +09:00
PDFDoc.prototype = {
setupFakeWorker: function() {
2011-10-25 10:13:12 +09:00
// If we don't use a worker, just post/sendMessage to the main thread.
var fakeWorker = {
2011-10-30 19:41:55 +09:00
postMessage: function pdfDocPostMessage(obj) {
fakeWorker.onmessage({data: obj});
2011-10-25 10:13:12 +09:00
},
2011-10-30 19:41:55 +09:00
terminate: function pdfDocTerminate() {}
2011-10-25 10:13:12 +09:00
};
var messageHandler = new MessageHandler('main', fakeWorker);
this.setupMessageHandler(messageHandler);
2011-10-25 10:13:12 +09:00
// If the main thread is our worker, setup the handling for the messages
// the main thread sends to it self.
WorkerMessageHandler.setup(messageHandler);
},
2011-10-25 10:13:12 +09:00
setupMessageHandler: function(messageHandler) {
this.messageHandler = messageHandler;
2011-10-25 10:13:12 +09:00
messageHandler.on('page', function pdfDocPage(data) {
var pageNum = data.pageNum;
var page = this.pageCache[pageNum];
var depFonts = data.depFonts;
2011-10-25 10:13:12 +09:00
page.startRenderingFromIRQueue(data.IRQueue, depFonts);
}, this);
2011-10-25 10:13:12 +09:00
messageHandler.on('obj', function pdfDocObj(data) {
var id = data[0];
var type = data[1];
switch (type) {
case 'JpegStream':
var imageData = data[2];
loadJpegStream(id, imageData, this.objs);
break;
case 'Image':
var imageData = data[2];
this.objs.resolve(id, imageData);
2011-11-02 04:27:19 +09:00
break;
case 'Font':
var name = data[2];
var file = data[3];
var properties = data[4];
if (file) {
2011-11-30 07:50:19 +09:00
// Rewrap the ArrayBuffer in a stream.
var fontFileDict = new Dict();
2011-11-30 07:50:19 +09:00
file = new Stream(file, 0, file.length, fontFileDict);
}
2011-10-25 10:13:12 +09:00
// For now, resolve the font object here direclty. The real font
// object is then created in FontLoader.bind().
this.objs.resolve(id, {
name: name,
file: file,
properties: properties
});
2011-11-02 04:27:19 +09:00
break;
default:
2012-01-25 05:04:59 +09:00
error('Got unkown object type ' + type);
}
}, this);
2011-10-25 10:13:12 +09:00
messageHandler.on('font_ready', function pdfDocFontReady(data) {
var id = data[0];
var font = new FontShape(data[1]);
2011-10-25 10:13:12 +09:00
// If there is no string, then there is nothing to attach to the DOM.
if (!font.str) {
this.objs.resolve(id, font);
} else {
this.objs.setData(id, font);
}
}.bind(this));
messageHandler.on('page_error', function pdfDocError(data) {
var page = this.pageCache[data.pageNum];
2012-01-05 10:22:07 +09:00
if (page.displayReadyPromise)
page.displayReadyPromise.reject(data.error);
else
2012-01-25 05:04:59 +09:00
error(data.error);
}, this);
2011-12-15 07:02:00 +09:00
messageHandler.on('jpeg_decode', function(data, promise) {
var imageData = data[0];
var components = data[1];
if (components != 3 && components != 1)
error('Only 3 component or 1 component can be returned');
var img = new Image();
img.onload = (function jpegImageLoaderOnload() {
var width = img.width;
var height = img.height;
var size = width * height;
var rgbaLength = size * 4;
var buf = new Uint8Array(size * components);
2011-12-13 08:09:05 +09:00
var tmpCanvas = new ScratchCanvas(width, height);
var tmpCtx = tmpCanvas.getContext('2d');
tmpCtx.drawImage(img, 0, 0);
var data = tmpCtx.getImageData(0, 0, width, height).data;
if (components == 3) {
for (var i = 0, j = 0; i < rgbaLength; i += 4, j += 3) {
buf[j] = data[i];
buf[j + 1] = data[i + 1];
buf[j + 2] = data[i + 2];
}
2011-12-13 02:26:24 +09:00
} else if (components == 1) {
for (var i = 0, j = 0; i < rgbaLength; i += 4, j++) {
buf[j] = data[i];
}
}
2011-12-15 07:02:00 +09:00
promise.resolve({ data: buf, width: width, height: height});
}).bind(this);
var src = 'data:image/jpeg;base64,' + window.btoa(imageData);
img.src = src;
});
setTimeout(function pdfDocFontReadySetTimeout() {
messageHandler.send('doc', this.data);
this.workerReadyPromise.resolve(true);
}.bind(this));
},
2011-10-25 10:13:12 +09:00
get numPages() {
return this.pdf.numPages;
},
2011-10-30 19:41:55 +09:00
startRendering: function pdfDocStartRendering(page) {
2011-10-25 10:13:12 +09:00
// The worker might not be ready to receive the page request yet.
2011-10-30 19:41:55 +09:00
this.workerReadyPromise.then(function pdfDocStartRenderingThen() {
this.messageHandler.send('page_request', page.pageNumber + 1);
2011-10-25 10:13:12 +09:00
}.bind(this));
},
2011-10-30 19:41:55 +09:00
getPage: function pdfDocGetPage(n) {
2011-10-25 10:13:12 +09:00
if (this.pageCache[n])
return this.pageCache[n];
var page = this.pdf.getPage(n);
// Add a reference to the objects such that Page can forward the reference
// to the CanvasGraphics and so on.
page.objs = this.objs;
page.pdf = this;
2011-11-04 06:26:58 +09:00
return (this.pageCache[n] = page);
2011-10-25 10:13:12 +09:00
},
2011-10-30 19:41:55 +09:00
destroy: function pdfDocDestroy() {
2011-10-25 10:13:12 +09:00
if (this.worker)
this.worker.terminate();
if (this.fontWorker)
this.fontWorker.terminate();
for (var n in this.pageCache)
delete this.pageCache[n];
delete this.data;
delete this.stream;
delete this.pdf;
delete this.catalog;
}
};
2011-12-09 07:18:43 +09:00
return PDFDoc;
2011-10-25 10:13:12 +09:00
})();
2011-10-27 03:46:57 +09:00
globalScope.PDFJS.PDFDoc = PDFDoc;