Merge pull request #2693 from bit/master

expose information about images on canvas
This commit is contained in:
Yury Delendik 2013-02-10 13:05:28 -08:00
commit 4317bc2586
2 changed files with 56 additions and 5 deletions

View File

@ -247,6 +247,8 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
* canvasContext(required): A 2D context of a DOM Canvas object., * canvasContext(required): A 2D context of a DOM Canvas object.,
* textLayer(optional): An object that has beginLayout, endLayout, and * textLayer(optional): An object that has beginLayout, endLayout, and
* appendText functions., * appendText functions.,
* imageLayer(optional): An object that has beginLayout, endLayout and
* appendImage functions.,
* continueCallback(optional): A function that will be called each time * continueCallback(optional): A function that will be called each time
* the rendering is paused. To continue * the rendering is paused. To continue
* rendering call the function that is the * rendering call the function that is the
@ -298,7 +300,7 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
} }
var gfx = new CanvasGraphics(params.canvasContext, this.commonObjs, var gfx = new CanvasGraphics(params.canvasContext, this.commonObjs,
this.objs, params.textLayer); this.objs, params.textLayer, params.imageLayer);
try { try {
this.display(gfx, params.viewport, complete, continueCallback); this.display(gfx, params.viewport, complete, continueCallback);
} catch (e) { } catch (e) {

View File

@ -214,7 +214,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
// before it stops and shedules a continue of execution. // before it stops and shedules a continue of execution.
var EXECUTION_TIME = 15; var EXECUTION_TIME = 15;
function CanvasGraphics(canvasCtx, commonObjs, objs, textLayer) { function CanvasGraphics(canvasCtx, commonObjs, objs, textLayer, imageLayer) {
this.ctx = canvasCtx; this.ctx = canvasCtx;
this.current = new CanvasExtraState(); this.current = new CanvasExtraState();
this.stateStack = []; this.stateStack = [];
@ -224,6 +224,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
this.commonObjs = commonObjs; this.commonObjs = commonObjs;
this.objs = objs; this.objs = objs;
this.textLayer = textLayer; this.textLayer = textLayer;
this.imageLayer = imageLayer;
if (canvasCtx) { if (canvasCtx) {
addContextCurrentTransform(canvasCtx); addContextCurrentTransform(canvasCtx);
} }
@ -399,8 +400,12 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
this.ctx.save(); this.ctx.save();
this.ctx.transform.apply(this.ctx, transform); this.ctx.transform.apply(this.ctx, transform);
if (this.textLayer) if (this.textLayer) {
this.textLayer.beginLayout(); this.textLayer.beginLayout();
}
if (this.imageLayer) {
this.imageLayer.beginLayout();
}
}, },
executeOperatorList: function CanvasGraphics_executeOperatorList( executeOperatorList: function CanvasGraphics_executeOperatorList(
@ -477,8 +482,12 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
endDrawing: function CanvasGraphics_endDrawing() { endDrawing: function CanvasGraphics_endDrawing() {
this.ctx.restore(); this.ctx.restore();
if (this.textLayer) if (this.textLayer) {
this.textLayer.endLayout(); this.textLayer.endLayout();
}
if (this.imageLayer) {
this.imageLayer.endLayout();
}
}, },
// Graphics state // Graphics state
@ -1275,7 +1284,19 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
ctx.drawImage(domImage, 0, 0, domImage.width, domImage.height, ctx.drawImage(domImage, 0, 0, domImage.width, domImage.height,
0, -h, w, h); 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(); this.restore();
}, },
@ -1374,6 +1395,17 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
} }
ctx.drawImage(tmpCanvas, 0, -height); 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(); this.restore();
}, },
@ -1394,6 +1426,16 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
ctx.scale(1, -1); ctx.scale(1, -1);
ctx.drawImage(tmpCanvas, entry.x, entry.y, entry.w, entry.h, ctx.drawImage(tmpCanvas, entry.x, entry.y, entry.w, entry.h,
0, -1, 1, 1); 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(); ctx.restore();
} }
}, },
@ -1459,6 +1501,13 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
return Math.sqrt(Math.max( return Math.sqrt(Math.max(
(inverse[0] * inverse[0] + inverse[1] * inverse[1]), (inverse[0] * inverse[0] + inverse[1] * inverse[1]),
(inverse[2] * inverse[2] + inverse[3] * inverse[3]))); (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]
];
} }
}; };