Convert the PageViewport
to a proper ES6 class
Also converts all `var` to `let` for good measure.
This commit is contained in:
parent
5917b21702
commit
51673dbc5a
@ -137,13 +137,9 @@ class DOMSVGFactory {
|
||||
|
||||
/**
|
||||
* PDF page viewport created based on scale, rotation and offset.
|
||||
* @class
|
||||
* @alias PageViewport
|
||||
*/
|
||||
var PageViewport = (function PageViewportClosure() {
|
||||
class PageViewport {
|
||||
/**
|
||||
* @constructor
|
||||
* @private
|
||||
* @param viewBox {Array} xMin, yMin, xMax and yMax coordinates.
|
||||
* @param scale {number} scale of the viewport.
|
||||
* @param rotation {number} rotations of the viewport in degrees.
|
||||
@ -151,7 +147,7 @@ var PageViewport = (function PageViewportClosure() {
|
||||
* @param offsetY {number} offset Y
|
||||
* @param dontFlip {boolean} if true, axis Y will not be flipped.
|
||||
*/
|
||||
function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
|
||||
constructor(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
|
||||
this.viewBox = viewBox;
|
||||
this.scale = scale;
|
||||
this.rotation = rotation;
|
||||
@ -160,9 +156,9 @@ var PageViewport = (function PageViewportClosure() {
|
||||
|
||||
// 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;
|
||||
let centerX = (viewBox[2] + viewBox[0]) / 2;
|
||||
let centerY = (viewBox[3] + viewBox[1]) / 2;
|
||||
let rotateA, rotateB, rotateC, rotateD;
|
||||
rotation = rotation % 360;
|
||||
rotation = rotation < 0 ? rotation + 360 : rotation;
|
||||
switch (rotation) {
|
||||
@ -185,8 +181,8 @@ var PageViewport = (function PageViewportClosure() {
|
||||
rotateC = -rotateC; rotateD = -rotateD;
|
||||
}
|
||||
|
||||
var offsetCanvasX, offsetCanvasY;
|
||||
var width, height;
|
||||
let offsetCanvasX, offsetCanvasY;
|
||||
let width, height;
|
||||
if (rotateA === 0) {
|
||||
offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
|
||||
offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
|
||||
@ -213,62 +209,62 @@ var PageViewport = (function PageViewportClosure() {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
PageViewport.prototype = /** @lends PageViewport.prototype */ {
|
||||
/**
|
||||
* Clones viewport with additional properties.
|
||||
* @param args {Object} (optional) If specified, may contain the 'scale' or
|
||||
* 'rotation' properties to override the corresponding properties in
|
||||
* the cloned viewport.
|
||||
* @returns {PageViewport} Cloned viewport.
|
||||
*/
|
||||
clone: function PageViewPort_clone(args) {
|
||||
args = args || {};
|
||||
var scale = 'scale' in args ? args.scale : this.scale;
|
||||
var rotation = 'rotation' in args ? args.rotation : this.rotation;
|
||||
return new PageViewport(this.viewBox.slice(), scale, rotation,
|
||||
this.offsetX, this.offsetY, args.dontFlip);
|
||||
},
|
||||
/**
|
||||
* Converts PDF point to the viewport coordinates. For examples, useful for
|
||||
* converting PDF location into canvas pixel coordinates.
|
||||
* @param x {number} X coordinate.
|
||||
* @param y {number} Y coordinate.
|
||||
* @returns {Object} Object that contains 'x' and 'y' properties of the
|
||||
* point in the viewport coordinate space.
|
||||
* @see {@link convertToPdfPoint}
|
||||
* @see {@link convertToViewportRectangle}
|
||||
*/
|
||||
convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
|
||||
return Util.applyTransform([x, y], this.transform);
|
||||
},
|
||||
/**
|
||||
* Converts PDF rectangle to the viewport coordinates.
|
||||
* @param rect {Array} xMin, yMin, xMax and yMax coordinates.
|
||||
* @returns {Array} Contains corresponding coordinates of the rectangle
|
||||
* in the viewport coordinate space.
|
||||
* @see {@link convertToViewportPoint}
|
||||
*/
|
||||
convertToViewportRectangle:
|
||||
function PageViewport_convertToViewportRectangle(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]];
|
||||
},
|
||||
/**
|
||||
* Converts viewport coordinates to the PDF location. For examples, useful
|
||||
* for converting canvas pixel location into PDF one.
|
||||
* @param x {number} X coordinate.
|
||||
* @param y {number} Y coordinate.
|
||||
* @returns {Object} Object that contains 'x' and 'y' properties of the
|
||||
* point in the PDF coordinate space.
|
||||
* @see {@link convertToViewportPoint}
|
||||
*/
|
||||
convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
|
||||
return Util.applyInverseTransform([x, y], this.transform);
|
||||
},
|
||||
};
|
||||
return PageViewport;
|
||||
})();
|
||||
|
||||
/**
|
||||
* Clones viewport with additional properties.
|
||||
* @param args {Object} (optional) If specified, may contain the 'scale' or
|
||||
* 'rotation' properties to override the corresponding properties in
|
||||
* the cloned viewport.
|
||||
* @returns {PageViewport} Cloned viewport.
|
||||
*/
|
||||
clone(args) {
|
||||
args = args || {};
|
||||
let scale = 'scale' in args ? args.scale : this.scale;
|
||||
let rotation = 'rotation' in args ? args.rotation : this.rotation;
|
||||
return new PageViewport(this.viewBox.slice(), scale, rotation,
|
||||
this.offsetX, this.offsetY, args.dontFlip);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts PDF point to the viewport coordinates. For examples, useful for
|
||||
* converting PDF location into canvas pixel coordinates.
|
||||
* @param x {number} X coordinate.
|
||||
* @param y {number} Y coordinate.
|
||||
* @returns {Object} Object that contains 'x' and 'y' properties of the
|
||||
* point in the viewport coordinate space.
|
||||
* @see {@link convertToPdfPoint}
|
||||
* @see {@link convertToViewportRectangle}
|
||||
*/
|
||||
convertToViewportPoint(x, y) {
|
||||
return Util.applyTransform([x, y], this.transform);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts PDF rectangle to the viewport coordinates.
|
||||
* @param rect {Array} xMin, yMin, xMax and yMax coordinates.
|
||||
* @returns {Array} Contains corresponding coordinates of the rectangle
|
||||
* in the viewport coordinate space.
|
||||
* @see {@link convertToViewportPoint}
|
||||
*/
|
||||
convertToViewportRectangle(rect) {
|
||||
let tl = Util.applyTransform([rect[0], rect[1]], this.transform);
|
||||
let br = Util.applyTransform([rect[2], rect[3]], this.transform);
|
||||
return [tl[0], tl[1], br[0], br[1]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts viewport coordinates to the PDF location. For examples, useful
|
||||
* for converting canvas pixel location into PDF one.
|
||||
* @param x {number} X coordinate.
|
||||
* @param y {number} Y coordinate.
|
||||
* @returns {Object} Object that contains 'x' and 'y' properties of the
|
||||
* point in the PDF coordinate space.
|
||||
* @see {@link convertToViewportPoint}
|
||||
*/
|
||||
convertToPdfPoint(x, y) {
|
||||
return Util.applyInverseTransform([x, y], this.transform);
|
||||
}
|
||||
}
|
||||
|
||||
var RenderingCancelledException = (function RenderingCancelledException() {
|
||||
function RenderingCancelledException(msg, type) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user