Convert the PageViewport to a proper ES6 class

Also converts all `var` to `let` for good measure.
This commit is contained in:
Jonas Jenwald 2018-06-04 12:36:53 +02:00
parent 5917b21702
commit 51673dbc5a

View File

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