From cd42ccd4391a73e83963a7871bea5211f9629e4d Mon Sep 17 00:00:00 2001 From: j Date: Sun, 10 Feb 2013 23:49:36 +0530 Subject: [PATCH] add imageLayer add optional imageLayer object to access images and there position and size as they are rendered on the canvas. --- src/api.js | 4 +++- src/canvas.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/api.js b/src/api.js index f4e27db80..aa3356ee7 100644 --- a/src/api.js +++ b/src/api.js @@ -247,6 +247,8 @@ var PDFPageProxy = (function PDFPageProxyClosure() { * canvasContext(required): A 2D context of a DOM Canvas object., * textLayer(optional): An object that has beginLayout, endLayout, and * appendText functions., + * imageLayer(optional): An object that has beginLayout, endLayout and + * appendImage functions., * continueCallback(optional): A function that will be called each time * the rendering is paused. To continue * rendering call the function that is the @@ -298,7 +300,7 @@ var PDFPageProxy = (function PDFPageProxyClosure() { } var gfx = new CanvasGraphics(params.canvasContext, this.commonObjs, - this.objs, params.textLayer); + this.objs, params.textLayer, params.imageLayer); try { this.display(gfx, params.viewport, complete, continueCallback); } catch (e) { diff --git a/src/canvas.js b/src/canvas.js index dd94b3701..794a2f473 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -214,7 +214,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { // before it stops and shedules a continue of execution. var EXECUTION_TIME = 15; - function CanvasGraphics(canvasCtx, commonObjs, objs, textLayer) { + function CanvasGraphics(canvasCtx, commonObjs, objs, textLayer, imageLayer) { this.ctx = canvasCtx; this.current = new CanvasExtraState(); this.stateStack = []; @@ -224,6 +224,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { this.commonObjs = commonObjs; this.objs = objs; this.textLayer = textLayer; + this.imageLayer = imageLayer; if (canvasCtx) { addContextCurrentTransform(canvasCtx); } @@ -399,8 +400,12 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { this.ctx.save(); this.ctx.transform.apply(this.ctx, transform); - if (this.textLayer) + if (this.textLayer) { this.textLayer.beginLayout(); + } + if (this.imageLayer) { + this.imageLayer.beginLayout(); + } }, executeOperatorList: function CanvasGraphics_executeOperatorList( @@ -477,8 +482,12 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { endDrawing: function CanvasGraphics_endDrawing() { this.ctx.restore(); - if (this.textLayer) + if (this.textLayer) { this.textLayer.endLayout(); + } + if (this.imageLayer) { + this.imageLayer.endLayout(); + } }, // Graphics state @@ -1275,7 +1284,19 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { ctx.drawImage(domImage, 0, 0, domImage.width, domImage.height, 0, -h, w, h); - + if (this.imageLayer) { + var currentTransform = ctx.mozCurrentTransformInverse; + var widthScale = Math.max(Math.abs(currentTransform[0]), 1); + var heightScale = Math.max(Math.abs(currentTransform[3]), 1); + var position = this.getCanvasPosition(0, 0); + this.imageLayer.appendImage({ + objId: objId, + left: position[0], + top: position[1], + width: w / widthScale, + height: h / heightScale + }); + } this.restore(); }, @@ -1374,6 +1395,17 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { } ctx.drawImage(tmpCanvas, 0, -height); } + + if (this.imageLayer) { + var position = this.getCanvasPosition(0, -height); + this.imageLayer.appendImage({ + imgData: imgData, + left: position[0], + top: position[1], + width: width / widthScale, + height: height / heightScale + }); + } this.restore(); }, @@ -1394,6 +1426,16 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { ctx.scale(1, -1); ctx.drawImage(tmpCanvas, entry.x, entry.y, entry.w, entry.h, 0, -1, 1, 1); + if (this.imageLayer) { + var position = this.getCanvasPosition(entry.x, entry.y); + this.imageLayer.appendImage({ + imgData: imgData, + left: position[0], + top: position[1], + width: w, + height: h + }); + } ctx.restore(); } }, @@ -1459,6 +1501,13 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { return Math.sqrt(Math.max( (inverse[0] * inverse[0] + inverse[1] * inverse[1]), (inverse[2] * inverse[2] + inverse[3] * inverse[3]))); + }, + getCanvasPosition: function CanvasGraphics_getCanvasPosition(x, y) { + var transform = this.ctx.mozCurrentTransform; + return [ + transform[0] * x + transform[2] * y + transform[4], + transform[1] * x + transform[3] * y + transform[5] + ]; } };