mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Removing the rotatePoint, width, height from the API
This commit is contained in:
parent
737ed84174
commit
47c43b5779
5 changed files with 160 additions and 149 deletions
14
src/api.js
14
src/api.js
|
@ -6,11 +6,8 @@
|
|||
this.page = page;
|
||||
}
|
||||
PdfPageWrapper.prototype = {
|
||||
get width() {
|
||||
return this.page.width;
|
||||
},
|
||||
get height() {
|
||||
return this.page.height;
|
||||
get rotate() {
|
||||
return this.page.rotate;
|
||||
},
|
||||
get stats() {
|
||||
return this.page.stats;
|
||||
|
@ -21,8 +18,10 @@
|
|||
get view() {
|
||||
return this.page.view;
|
||||
},
|
||||
rotatePoint: function(x, y) {
|
||||
return this.page.rotatePoint(x, y);
|
||||
getViewport: function(scale, rotate) {
|
||||
if (arguments < 2)
|
||||
rotate = this.rotate;
|
||||
return new PDFJS.PageViewport(this.view, scale, rotate, 0, 0);
|
||||
},
|
||||
getAnnotations: function() {
|
||||
var promise = new PDFJS.Promise();
|
||||
|
@ -33,6 +32,7 @@
|
|||
render: function(renderContext) {
|
||||
var promise = new PDFJS.Promise();
|
||||
this.page.startRendering(renderContext.canvasContext,
|
||||
renderContext.viewport,
|
||||
function complete(error) {
|
||||
if (error)
|
||||
promise.reject(error);
|
||||
|
|
|
@ -241,27 +241,10 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
|||
'shadingFill': true
|
||||
},
|
||||
|
||||
beginDrawing: function CanvasGraphics_beginDrawing(mediaBox) {
|
||||
var cw = this.ctx.canvas.width, ch = this.ctx.canvas.height;
|
||||
beginDrawing: function CanvasGraphics_beginDrawing(viewport) {
|
||||
var transform = viewport.transform;
|
||||
this.ctx.save();
|
||||
switch (mediaBox.rotate) {
|
||||
case 0:
|
||||
this.ctx.transform(1, 0, 0, -1, 0, ch);
|
||||
break;
|
||||
case 90:
|
||||
this.ctx.transform(0, 1, 1, 0, 0, 0);
|
||||
break;
|
||||
case 180:
|
||||
this.ctx.transform(-1, 0, 0, 1, cw, 0);
|
||||
break;
|
||||
case 270:
|
||||
this.ctx.transform(0, -1, -1, 0, cw, ch);
|
||||
break;
|
||||
}
|
||||
// Scale so that canvas units are the same as PDF user space units
|
||||
this.ctx.scale(cw / mediaBox.width, ch / mediaBox.height);
|
||||
// Move the media left-top corner to the (0,0) canvas position
|
||||
this.ctx.translate(-mediaBox.x, -mediaBox.y);
|
||||
this.ctx.transform.apply(this.ctx, transform);
|
||||
|
||||
if (this.textLayer)
|
||||
this.textLayer.beginLayout();
|
||||
|
|
81
src/core.js
81
src/core.js
|
@ -100,18 +100,10 @@ var Page = (function PageClosure() {
|
|||
return shadow(this, 'mediaBox', obj);
|
||||
},
|
||||
get view() {
|
||||
var cropBox = this.inheritPageProp('CropBox');
|
||||
var view = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: this.width,
|
||||
height: this.height
|
||||
};
|
||||
if (!isArray(cropBox) || cropBox.length !== 4)
|
||||
return shadow(this, 'view', view);
|
||||
|
||||
var mediaBox = this.mediaBox;
|
||||
var offsetX = mediaBox[0], offsetY = mediaBox[1];
|
||||
var cropBox = this.inheritPageProp('CropBox');
|
||||
if (!isArray(cropBox) || cropBox.length !== 4)
|
||||
return shadow(this, 'view', mediaBox);
|
||||
|
||||
// From the spec, 6th ed., p.963:
|
||||
// "The crop, bleed, trim, and art boxes should not ordinarily
|
||||
|
@ -119,42 +111,13 @@ var Page = (function PageClosure() {
|
|||
// effectively reduced to their intersection with the media box."
|
||||
cropBox = Util.intersect(cropBox, mediaBox);
|
||||
if (!cropBox)
|
||||
return shadow(this, 'view', view);
|
||||
return shadow(this, 'view', mediaBox);
|
||||
|
||||
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);
|
||||
|
||||
return shadow(this, 'view', view);
|
||||
return shadow(this, 'view', cropBox);
|
||||
},
|
||||
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
|
||||
|
@ -238,7 +201,7 @@ var Page = (function PageClosure() {
|
|||
);
|
||||
},
|
||||
|
||||
display: function Page_display(gfx, callback) {
|
||||
display: function Page_display(gfx, viewport, callback) {
|
||||
var stats = this.stats;
|
||||
stats.time('Rendering');
|
||||
var xref = this.xref;
|
||||
|
@ -248,10 +211,7 @@ var Page = (function PageClosure() {
|
|||
|
||||
gfx.xref = xref;
|
||||
gfx.res = resources;
|
||||
gfx.beginDrawing({ x: mediaBox[0], y: mediaBox[1],
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
rotate: this.rotate });
|
||||
gfx.beginDrawing(viewport);
|
||||
|
||||
var startIdx = 0;
|
||||
var length = this.operatorList.fnArray.length;
|
||||
|
@ -276,21 +236,6 @@ var Page = (function PageClosure() {
|
|||
}
|
||||
next();
|
||||
},
|
||||
rotatePoint: function Page_rotatePoint(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 Page_getLinks() {
|
||||
var links = [];
|
||||
var annotations = pageGetAnnotations();
|
||||
|
@ -342,15 +287,10 @@ var Page = (function PageClosure() {
|
|||
if (!isName(subtype))
|
||||
continue;
|
||||
var rect = annotation.get('Rect');
|
||||
var topLeftCorner = this.rotatePoint(rect[0], rect[1]);
|
||||
var bottomRightCorner = this.rotatePoint(rect[2], rect[3]);
|
||||
|
||||
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);
|
||||
item.rect = rect;
|
||||
switch (subtype.name) {
|
||||
case 'Link':
|
||||
var a = annotation.get('A');
|
||||
|
@ -434,7 +374,8 @@ var Page = (function PageClosure() {
|
|||
}
|
||||
return items;
|
||||
},
|
||||
startRendering: function Page_startRendering(ctx, callback, textLayer) {
|
||||
startRendering: function Page_startRendering(ctx, viewport,
|
||||
callback, textLayer) {
|
||||
var stats = this.stats;
|
||||
stats.time('Overall');
|
||||
// If there is no displayReadyPromise yet, then the operatorList was never
|
||||
|
@ -449,7 +390,7 @@ var Page = (function PageClosure() {
|
|||
function pageDisplayReadyPromise() {
|
||||
var gfx = new CanvasGraphics(ctx, this.objs, textLayer);
|
||||
try {
|
||||
this.display(gfx, callback);
|
||||
this.display(gfx, viewport, callback);
|
||||
} catch (e) {
|
||||
if (callback)
|
||||
callback(e);
|
||||
|
|
88
src/util.js
88
src/util.js
|
@ -97,6 +97,19 @@ var Util = (function UtilClosure() {
|
|||
return [xt, yt];
|
||||
};
|
||||
|
||||
Util.applyInverseTransform = function Util_applyTransform(p, m) {
|
||||
var d = m[0] * m[3] - m[1] * m[2];
|
||||
var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
|
||||
var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
|
||||
return [xt, yt];
|
||||
};
|
||||
|
||||
Util.inverseTransform = function Util_inverseTransform(m) {
|
||||
var d = m[0] * m[3] - m[1] * m[2];
|
||||
return [m[3] / d, -m[1] / d, -m[2] / d, m[0] / d,
|
||||
(m[2] * m[5] - m[4] * m[3]) / d, (m[4] * m[1] - m[5] * m[0]) / d];
|
||||
};
|
||||
|
||||
// Apply a generic 3d matrix M on a 3-vector v:
|
||||
// | a b c | | X |
|
||||
// | d e f | x | Y |
|
||||
|
@ -165,7 +178,7 @@ var Util = (function UtilClosure() {
|
|||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
Util.sign = function Util_sign(num) {
|
||||
return num < 0 ? -1 : 1;
|
||||
|
@ -174,6 +187,79 @@ var Util = (function UtilClosure() {
|
|||
return Util;
|
||||
})();
|
||||
|
||||
var PageViewport = PDFJS.PageViewport = (function PageViewportClosure() {
|
||||
function PageViewport(viewBox, scale, rotate, offsetX, offsetY) {
|
||||
// creating transform to convert pdf coordinate system to the normal
|
||||
// canvas like coordinates taking in account scale and rotation
|
||||
var centerX = (viewBox[2] + viewBox[0]) / 2;
|
||||
var centerY = (viewBox[3] + viewBox[1]) / 2;
|
||||
var rotateA, rotateB, rotateC, rotateD;
|
||||
switch (rotate) {
|
||||
case -180:
|
||||
case 180:
|
||||
rotateA = -1; rotateB = 0; rotateC = 0; rotateD = 1;
|
||||
break;
|
||||
case -270:
|
||||
case 90:
|
||||
rotateA = 0; rotateB = 1; rotateC = 1; rotateD = 0;
|
||||
break;
|
||||
case -90:
|
||||
case 270:
|
||||
rotateA = 0; rotateB = -1; rotateC = -1; rotateD = 0;
|
||||
break;
|
||||
case 360:
|
||||
case 0:
|
||||
default:
|
||||
rotateA = 1; rotateB = 0; rotateC = 0; rotateD = -1;
|
||||
break;
|
||||
}
|
||||
var offsetCanvasX, offsetCanvasY;
|
||||
var width, height;
|
||||
if (rotateA == 0) {
|
||||
offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
|
||||
offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
|
||||
width = Math.abs(viewBox[3] - viewBox[1]) * scale;
|
||||
height = Math.abs(viewBox[2] - viewBox[0]) * scale;
|
||||
} else {
|
||||
offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
|
||||
offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
|
||||
width = Math.abs(viewBox[2] - viewBox[0]) * scale;
|
||||
height = Math.abs(viewBox[3] - viewBox[1]) * scale;
|
||||
}
|
||||
// creating transform for the following operations:
|
||||
// translate(-centerX, -centerY), rotate and flip vertically,
|
||||
// scale, and translate(offsetCanvasX, offsetCanvasY)
|
||||
this.transform = [
|
||||
rotateA * scale,
|
||||
rotateB * scale,
|
||||
rotateC * scale,
|
||||
rotateD * scale,
|
||||
offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY,
|
||||
offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY
|
||||
];
|
||||
|
||||
this.offsetX = offsetX;
|
||||
this.offsetY = offsetY;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
PageViewport.prototype = {
|
||||
convertPointToViewport: function PageViewport_convertPointToViewport(x, y) {
|
||||
return Util.applyTransform([x, y], this.transform);
|
||||
},
|
||||
convertRectangleToViewport:
|
||||
function PageViewport_convertRectangeToViewport(rect) {
|
||||
var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
|
||||
var br = Util.applyTransform([rect[2], rect[3]], this.transform);
|
||||
return [tl[0], tl[1], br[0], br[1]];
|
||||
},
|
||||
convertViewportToPoint: function PageViewport_convertViewportToPoint(x, y) {
|
||||
return Util.applyInverseTransform([x, y], this.transform);
|
||||
}
|
||||
};
|
||||
return PageViewport;
|
||||
})();
|
||||
|
||||
var PDFStringTranslateTable = [
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0x2D8, 0x2C7, 0x2C6, 0x2D9, 0x2DD, 0x2DB, 0x2DA, 0x2DC, 0, 0, 0, 0, 0, 0, 0,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue