Skip to content

Commit 7913281

Browse files
committed
upgrade pdf.js
1 parent 70867a6 commit 7913281

File tree

2 files changed

+106
-54
lines changed

2 files changed

+106
-54
lines changed

example/js/lib/pdf.js

Lines changed: 75 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2-
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
31
/* Copyright 2012 Mozilla Foundation
42
*
53
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,8 +20,8 @@ if (typeof PDFJS === 'undefined') {
2220
(typeof window !== 'undefined' ? window : this).PDFJS = {};
2321
}
2422

25-
PDFJS.version = '1.2.83';
26-
PDFJS.build = '1280b7b';
23+
PDFJS.version = '1.2.131';
24+
PDFJS.build = '194994a';
2725

2826
(function pdfjsWrapper() {
2927
// Use strict in our context only - users might not want it
@@ -1972,9 +1970,9 @@ PDFJS.getDocument = function getDocument(src,
19721970
/**
19731971
* PDF document loading operation.
19741972
* @class
1973+
* @alias PDFDocumentLoadingTask
19751974
*/
19761975
var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() {
1977-
/** @constructs PDFDocumentLoadingTask */
19781976
function PDFDocumentLoadingTask() {
19791977
this._capability = createPromiseCapability();
19801978
this._transport = null;
@@ -2031,13 +2029,11 @@ var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() {
20312029
/**
20322030
* Abstract class to support range requests file loading.
20332031
* @class
2032+
* @alias PDFJS.PDFDataRangeTransport
2033+
* @param {number} length
2034+
* @param {Uint8Array} initialData
20342035
*/
20352036
var PDFDataRangeTransport = (function pdfDataRangeTransportClosure() {
2036-
/**
2037-
* @constructs PDFDataRangeTransport
2038-
* @param {number} length
2039-
* @param {Uint8Array} initialData
2040-
*/
20412037
function PDFDataRangeTransport(length, initialData) {
20422038
this.length = length;
20432039
this.initialData = initialData;
@@ -2111,6 +2107,7 @@ PDFJS.PDFDataRangeTransport = PDFDataRangeTransport;
21112107
* Proxy to a PDFDocument in the worker thread. Also, contains commonly used
21122108
* properties that can be read synchronously.
21132109
* @class
2110+
* @alias PDFDocumentProxy
21142111
*/
21152112
var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
21162113
function PDFDocumentProxy(pdfInfo, transport, loadingTask) {
@@ -2285,6 +2282,8 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
22852282
* calling of PDFPage.getViewport method.
22862283
* @property {string} intent - Rendering intent, can be 'display' or 'print'
22872284
* (default value is 'display').
2285+
* @property {Array} transform - (optional) Additional transform, applied
2286+
* just before viewport transform.
22882287
* @property {Object} imageLayer - (optional) An object that has beginLayout,
22892288
* endLayout and appendImage functions.
22902289
* @property {function} continueCallback - (deprecated) A function that will be
@@ -2305,6 +2304,7 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
23052304
/**
23062305
* Proxy to a PDFPage in the worker thread.
23072306
* @class
2307+
* @alias PDFPageProxy
23082308
*/
23092309
var PDFPageProxy = (function PDFPageProxyClosure() {
23102310
function PDFPageProxy(pageIndex, pageInfo, transport) {
@@ -3250,6 +3250,7 @@ var PDFObjects = (function PDFObjectsClosure() {
32503250
/**
32513251
* Allows controlling of the rendering tasks.
32523252
* @class
3253+
* @alias RenderTask
32533254
*/
32543255
var RenderTask = (function RenderTaskClosure() {
32553256
function RenderTask(internalRenderTask) {
@@ -3345,7 +3346,7 @@ var InternalRenderTask = (function InternalRenderTaskClosure() {
33453346
this.gfx = new CanvasGraphics(params.canvasContext, this.commonObjs,
33463347
this.objs, params.imageLayer);
33473348

3348-
this.gfx.beginDrawing(params.viewport, transparency);
3349+
this.gfx.beginDrawing(params.transform, params.viewport, transparency);
33493350
this.operatorListIdx = 0;
33503351
this.graphicsReady = true;
33513352
if (this.graphicsReadyCallback) {
@@ -3642,13 +3643,15 @@ function addContextCurrentTransform(ctx) {
36423643
}
36433644

36443645
var CachedCanvases = (function CachedCanvasesClosure() {
3645-
var cache = {};
3646-
return {
3646+
function CachedCanvases() {
3647+
this.cache = Object.create(null);
3648+
}
3649+
CachedCanvases.prototype = {
36473650
getCanvas: function CachedCanvases_getCanvas(id, width, height,
36483651
trackTransform) {
36493652
var canvasEntry;
3650-
if (cache[id] !== undefined) {
3651-
canvasEntry = cache[id];
3653+
if (this.cache[id] !== undefined) {
3654+
canvasEntry = this.cache[id];
36523655
canvasEntry.canvas.width = width;
36533656
canvasEntry.canvas.height = height;
36543657
// reset canvas transform for emulated mozCurrentTransform, if needed
@@ -3659,21 +3662,22 @@ var CachedCanvases = (function CachedCanvasesClosure() {
36593662
if (trackTransform) {
36603663
addContextCurrentTransform(ctx);
36613664
}
3662-
cache[id] = canvasEntry = {canvas: canvas, context: ctx};
3665+
this.cache[id] = canvasEntry = {canvas: canvas, context: ctx};
36633666
}
36643667
return canvasEntry;
36653668
},
36663669
clear: function () {
3667-
for (var id in cache) {
3668-
var canvasEntry = cache[id];
3670+
for (var id in this.cache) {
3671+
var canvasEntry = this.cache[id];
36693672
// Zeroing the width and height causes Firefox to release graphics
36703673
// resources immediately, which can greatly reduce memory consumption.
36713674
canvasEntry.canvas.width = 0;
36723675
canvasEntry.canvas.height = 0;
3673-
delete cache[id];
3676+
delete this.cache[id];
36743677
}
36753678
}
36763679
};
3680+
return CachedCanvases;
36773681
})();
36783682

36793683
function compileType3Glyph(imgData) {
@@ -3911,6 +3915,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
39113915
this.smaskStack = [];
39123916
this.smaskCounter = 0;
39133917
this.tempSMask = null;
3918+
this.cachedCanvases = new CachedCanvases();
39143919
if (canvasCtx) {
39153920
// NOTE: if mozCurrentTransform is polyfilled, then the current state of
39163921
// the transformation must already be set in canvasCtx._transformMatrix.
@@ -4187,28 +4192,39 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
41874192

41884193
CanvasGraphics.prototype = {
41894194

4190-
beginDrawing: function CanvasGraphics_beginDrawing(viewport, transparency) {
4195+
beginDrawing: function CanvasGraphics_beginDrawing(transform, viewport,
4196+
transparency) {
41914197
// For pdfs that use blend modes we have to clear the canvas else certain
41924198
// blend modes can look wrong since we'd be blending with a white
41934199
// backdrop. The problem with a transparent backdrop though is we then
4194-
// don't get sub pixel anti aliasing on text, so we fill with white if
4195-
// we can.
4200+
// don't get sub pixel anti aliasing on text, creating temporary
4201+
// transparent canvas when we have blend modes.
41964202
var width = this.ctx.canvas.width;
41974203
var height = this.ctx.canvas.height;
4204+
4205+
this.ctx.save();
4206+
this.ctx.fillStyle = 'rgb(255, 255, 255)';
4207+
this.ctx.fillRect(0, 0, width, height);
4208+
this.ctx.restore();
4209+
41984210
if (transparency) {
4199-
this.ctx.clearRect(0, 0, width, height);
4200-
} else {
4201-
this.ctx.mozOpaque = true;
4211+
var transparentCanvas = this.cachedCanvases.getCanvas(
4212+
'transparent', width, height, true);
4213+
this.compositeCtx = this.ctx;
4214+
this.transparentCanvas = transparentCanvas.canvas;
4215+
this.ctx = transparentCanvas.context;
42024216
this.ctx.save();
4203-
this.ctx.fillStyle = 'rgb(255, 255, 255)';
4204-
this.ctx.fillRect(0, 0, width, height);
4205-
this.ctx.restore();
4217+
// The transform can be applied before rendering, transferring it to
4218+
// the new canvas.
4219+
this.ctx.transform.apply(this.ctx,
4220+
this.compositeCtx.mozCurrentTransform);
42064221
}
42074222

4208-
var transform = viewport.transform;
4209-
42104223
this.ctx.save();
4211-
this.ctx.transform.apply(this.ctx, transform);
4224+
if (transform) {
4225+
this.ctx.transform.apply(this.ctx, transform);
4226+
}
4227+
this.ctx.transform.apply(this.ctx, viewport.transform);
42124228

42134229
this.baseTransform = this.ctx.mozCurrentTransform.slice();
42144230

@@ -4290,7 +4306,14 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
42904306

42914307
endDrawing: function CanvasGraphics_endDrawing() {
42924308
this.ctx.restore();
4293-
CachedCanvases.clear();
4309+
4310+
if (this.transparentCanvas) {
4311+
this.ctx = this.compositeCtx;
4312+
this.ctx.drawImage(this.transparentCanvas, 0, 0);
4313+
this.transparentCanvas = null;
4314+
}
4315+
4316+
this.cachedCanvases.clear();
42944317
WebGLUtils.clear();
42954318

42964319
if (this.imageLayer) {
@@ -4404,7 +4427,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
44044427
var drawnWidth = activeSMask.canvas.width;
44054428
var drawnHeight = activeSMask.canvas.height;
44064429
var cacheId = 'smaskGroupAt' + this.groupLevel;
4407-
var scratchCanvas = CachedCanvases.getCanvas(
4430+
var scratchCanvas = this.cachedCanvases.getCanvas(
44084431
cacheId, drawnWidth, drawnHeight, true);
44094432

44104433
var currentCtx = this.ctx;
@@ -5188,7 +5211,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
51885211
// Using two cache entries is case if masks are used one after another.
51895212
cacheId += '_smask_' + ((this.smaskCounter++) % 2);
51905213
}
5191-
var scratchCanvas = CachedCanvases.getCanvas(
5214+
var scratchCanvas = this.cachedCanvases.getCanvas(
51925215
cacheId, drawnWidth, drawnHeight, true);
51935216
var groupCtx = scratchCanvas.context;
51945217

@@ -5329,7 +5352,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
53295352
return;
53305353
}
53315354

5332-
var maskCanvas = CachedCanvases.getCanvas('maskCanvas', width, height);
5355+
var maskCanvas = this.cachedCanvases.getCanvas('maskCanvas',
5356+
width, height);
53335357
var maskCtx = maskCanvas.context;
53345358
maskCtx.save();
53355359

@@ -5354,7 +5378,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
53545378
var fillColor = this.current.fillColor;
53555379
var isPatternFill = this.current.patternFill;
53565380

5357-
var maskCanvas = CachedCanvases.getCanvas('maskCanvas', width, height);
5381+
var maskCanvas = this.cachedCanvases.getCanvas('maskCanvas',
5382+
width, height);
53585383
var maskCtx = maskCanvas.context;
53595384
maskCtx.save();
53605385

@@ -5389,7 +5414,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
53895414
var image = images[i];
53905415
var width = image.width, height = image.height;
53915416

5392-
var maskCanvas = CachedCanvases.getCanvas('maskCanvas', width, height);
5417+
var maskCanvas = this.cachedCanvases.getCanvas('maskCanvas',
5418+
width, height);
53935419
var maskCtx = maskCanvas.context;
53945420
maskCtx.save();
53955421

@@ -5462,7 +5488,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
54625488
if (imgData instanceof HTMLElement || !imgData.data) {
54635489
imgToPaint = imgData;
54645490
} else {
5465-
tmpCanvas = CachedCanvases.getCanvas('inlineImage', width, height);
5491+
tmpCanvas = this.cachedCanvases.getCanvas('inlineImage',
5492+
width, height);
54665493
var tmpCtx = tmpCanvas.context;
54675494
putBinaryImageData(tmpCtx, imgData);
54685495
imgToPaint = tmpCanvas.canvas;
@@ -5484,7 +5511,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
54845511
newHeight = Math.ceil(paintHeight / 2);
54855512
heightScale /= paintHeight / newHeight;
54865513
}
5487-
tmpCanvas = CachedCanvases.getCanvas(tmpCanvasId, newWidth, newHeight);
5514+
tmpCanvas = this.cachedCanvases.getCanvas(tmpCanvasId,
5515+
newWidth, newHeight);
54885516
tmpCtx = tmpCanvas.context;
54895517
tmpCtx.clearRect(0, 0, newWidth, newHeight);
54905518
tmpCtx.drawImage(imgToPaint, 0, 0, paintWidth, paintHeight,
@@ -5516,7 +5544,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
55165544
var w = imgData.width;
55175545
var h = imgData.height;
55185546

5519-
var tmpCanvas = CachedCanvases.getCanvas('inlineImage', w, h);
5547+
var tmpCanvas = this.cachedCanvases.getCanvas('inlineImage', w, h);
55205548
var tmpCtx = tmpCanvas.context;
55215549
putBinaryImageData(tmpCtx, imgData);
55225550

@@ -6174,7 +6202,7 @@ var createMeshCanvas = (function createMeshCanvasClosure() {
61746202
}
61756203

61766204
function createMeshCanvas(bounds, combinesScale, coords, colors, figures,
6177-
backgroundColor) {
6205+
backgroundColor, cachedCanvases) {
61786206
// we will increase scale on some weird factor to let antialiasing take
61796207
// care of "rough" edges
61806208
var EXPECTED_SCALE = 1.1;
@@ -6208,11 +6236,11 @@ var createMeshCanvas = (function createMeshCanvasClosure() {
62086236
figures, context);
62096237

62106238
// https://bugzilla.mozilla.org/show_bug.cgi?id=972126
6211-
tmpCanvas = CachedCanvases.getCanvas('mesh', width, height, false);
6239+
tmpCanvas = cachedCanvases.getCanvas('mesh', width, height, false);
62126240
tmpCanvas.context.drawImage(canvas, 0, 0);
62136241
canvas = tmpCanvas.canvas;
62146242
} else {
6215-
tmpCanvas = CachedCanvases.getCanvas('mesh', width, height, false);
6243+
tmpCanvas = cachedCanvases.getCanvas('mesh', width, height, false);
62166244
var tmpCtx = tmpCanvas.context;
62176245

62186246
var data = tmpCtx.createImageData(width, height);
@@ -6268,7 +6296,8 @@ ShadingIRs.Mesh = {
62686296
// Rasterizing on the main thread since sending/queue large canvases
62696297
// might cause OOM.
62706298
var temporaryPatternCanvas = createMeshCanvas(bounds, scale, coords,
6271-
colors, figures, shadingFill ? null : background);
6299+
colors, figures, shadingFill ? null : background,
6300+
owner.cachedCanvases);
62726301

62736302
if (!shadingFill) {
62746303
ctx.setTransform.apply(ctx, owner.baseTransform);
@@ -6371,7 +6400,8 @@ var TilingPattern = (function TilingPatternClosure() {
63716400
height = Math.min(Math.ceil(Math.abs(height * combinedScale[1])),
63726401
MAX_PATTERN_SIZE);
63736402

6374-
var tmpCanvas = CachedCanvases.getCanvas('pattern', width, height, true);
6403+
var tmpCanvas = owner.cachedCanvases.getCanvas('pattern',
6404+
width, height, true);
63756405
var tmpCtx = tmpCanvas.context;
63766406
var graphics = new CanvasGraphics(tmpCtx, commonObjs, objs);
63776407
graphics.groupLevel = owner.groupLevel;

0 commit comments

Comments
 (0)