mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-06-02 16:21:01 +02:00
This is necessary to prevent import cycles with the next patch. It also shouldn't hurt to reduce the size of `src/display/display_utils.js` a little bit, since utility-files have a tendency to increase in size over time.
233 lines
7.0 KiB
JavaScript
233 lines
7.0 KiB
JavaScript
/* Copyright 2015 Mozilla Foundation
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { shadow, Util } from "../shared/util.js";
|
|
|
|
/**
|
|
* @typedef {Object} PageViewportParameters
|
|
* @property {Array<number>} viewBox - The xMin, yMin, xMax and
|
|
* yMax coordinates.
|
|
* @property {number} userUnit - The size of units.
|
|
* @property {number} scale - The scale of the viewport.
|
|
* @property {number} rotation - The rotation, in degrees, of the viewport.
|
|
* @property {number} [offsetX] - The horizontal, i.e. x-axis, offset. The
|
|
* default value is `0`.
|
|
* @property {number} [offsetY] - The vertical, i.e. y-axis, offset. The
|
|
* default value is `0`.
|
|
* @property {boolean} [dontFlip] - If true, the y-axis will not be flipped.
|
|
* The default value is `false`.
|
|
*/
|
|
|
|
/**
|
|
* @typedef {Object} PageViewportCloneParameters
|
|
* @property {number} [scale] - The scale, overriding the one in the cloned
|
|
* viewport. The default value is `this.scale`.
|
|
* @property {number} [rotation] - The rotation, in degrees, overriding the one
|
|
* in the cloned viewport. The default value is `this.rotation`.
|
|
* @property {number} [offsetX] - The horizontal, i.e. x-axis, offset.
|
|
* The default value is `this.offsetX`.
|
|
* @property {number} [offsetY] - The vertical, i.e. y-axis, offset.
|
|
* The default value is `this.offsetY`.
|
|
* @property {boolean} [dontFlip] - If true, the x-axis will not be flipped.
|
|
* The default value is `false`.
|
|
*/
|
|
|
|
/**
|
|
* PDF page viewport created based on scale, rotation and offset.
|
|
*/
|
|
class PageViewport {
|
|
/**
|
|
* @param {PageViewportParameters}
|
|
*/
|
|
constructor({
|
|
viewBox,
|
|
userUnit,
|
|
scale,
|
|
rotation,
|
|
offsetX = 0,
|
|
offsetY = 0,
|
|
dontFlip = false,
|
|
}) {
|
|
this.viewBox = viewBox;
|
|
this.userUnit = userUnit;
|
|
this.scale = scale;
|
|
this.rotation = rotation;
|
|
this.offsetX = offsetX;
|
|
this.offsetY = offsetY;
|
|
|
|
scale *= userUnit; // Take the userUnit into account.
|
|
|
|
// creating transform to convert pdf coordinate system to the normal
|
|
// canvas like coordinates taking in account scale and rotation
|
|
const centerX = (viewBox[2] + viewBox[0]) / 2;
|
|
const centerY = (viewBox[3] + viewBox[1]) / 2;
|
|
let rotateA, rotateB, rotateC, rotateD;
|
|
// Normalize the rotation, by clamping it to the [0, 360) range.
|
|
rotation %= 360;
|
|
if (rotation < 0) {
|
|
rotation += 360;
|
|
}
|
|
switch (rotation) {
|
|
case 180:
|
|
rotateA = -1;
|
|
rotateB = 0;
|
|
rotateC = 0;
|
|
rotateD = 1;
|
|
break;
|
|
case 90:
|
|
rotateA = 0;
|
|
rotateB = 1;
|
|
rotateC = 1;
|
|
rotateD = 0;
|
|
break;
|
|
case 270:
|
|
rotateA = 0;
|
|
rotateB = -1;
|
|
rotateC = -1;
|
|
rotateD = 0;
|
|
break;
|
|
case 0:
|
|
rotateA = 1;
|
|
rotateB = 0;
|
|
rotateC = 0;
|
|
rotateD = -1;
|
|
break;
|
|
default:
|
|
throw new Error(
|
|
"PageViewport: Invalid rotation, must be a multiple of 90 degrees."
|
|
);
|
|
}
|
|
|
|
if (dontFlip) {
|
|
rotateC = -rotateC;
|
|
rotateD = -rotateD;
|
|
}
|
|
|
|
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;
|
|
width = (viewBox[3] - viewBox[1]) * scale;
|
|
height = (viewBox[2] - viewBox[0]) * scale;
|
|
} else {
|
|
offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
|
|
offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
|
|
width = (viewBox[2] - viewBox[0]) * scale;
|
|
height = (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.width = width;
|
|
this.height = height;
|
|
}
|
|
|
|
/**
|
|
* The original, un-scaled, viewport dimensions.
|
|
* @type {Object}
|
|
*/
|
|
get rawDims() {
|
|
const dims = this.viewBox;
|
|
|
|
return shadow(this, "rawDims", {
|
|
pageWidth: dims[2] - dims[0],
|
|
pageHeight: dims[3] - dims[1],
|
|
pageX: dims[0],
|
|
pageY: dims[1],
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Clones viewport, with optional additional properties.
|
|
* @param {PageViewportCloneParameters} [params]
|
|
* @returns {PageViewport} Cloned viewport.
|
|
*/
|
|
clone({
|
|
scale = this.scale,
|
|
rotation = this.rotation,
|
|
offsetX = this.offsetX,
|
|
offsetY = this.offsetY,
|
|
dontFlip = false,
|
|
} = {}) {
|
|
return new PageViewport({
|
|
viewBox: this.viewBox.slice(),
|
|
userUnit: this.userUnit,
|
|
scale,
|
|
rotation,
|
|
offsetX,
|
|
offsetY,
|
|
dontFlip,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Converts PDF point to the viewport coordinates. For examples, useful for
|
|
* converting PDF location into canvas pixel coordinates.
|
|
* @param {number} x - The x-coordinate.
|
|
* @param {number} y - The y-coordinate.
|
|
* @returns {Array} Array containing `x`- and `y`-coordinates of the
|
|
* point in the viewport coordinate space.
|
|
* @see {@link convertToPdfPoint}
|
|
* @see {@link convertToViewportRectangle}
|
|
*/
|
|
convertToViewportPoint(x, y) {
|
|
const p = [x, y];
|
|
Util.applyTransform(p, this.transform);
|
|
return p;
|
|
}
|
|
|
|
/**
|
|
* Converts PDF rectangle to the viewport coordinates.
|
|
* @param {Array} rect - The xMin, yMin, xMax and yMax coordinates.
|
|
* @returns {Array} Array containing corresponding coordinates of the
|
|
* rectangle in the viewport coordinate space.
|
|
* @see {@link convertToViewportPoint}
|
|
*/
|
|
convertToViewportRectangle(rect) {
|
|
const topLeft = [rect[0], rect[1]];
|
|
Util.applyTransform(topLeft, this.transform);
|
|
const bottomRight = [rect[2], rect[3]];
|
|
Util.applyTransform(bottomRight, this.transform);
|
|
return [topLeft[0], topLeft[1], bottomRight[0], bottomRight[1]];
|
|
}
|
|
|
|
/**
|
|
* Converts viewport coordinates to the PDF location. For examples, useful
|
|
* for converting canvas pixel location into PDF one.
|
|
* @param {number} x - The x-coordinate.
|
|
* @param {number} y - The y-coordinate.
|
|
* @returns {Array} Array containing `x`- and `y`-coordinates of the
|
|
* point in the PDF coordinate space.
|
|
* @see {@link convertToViewportPoint}
|
|
*/
|
|
convertToPdfPoint(x, y) {
|
|
const p = [x, y];
|
|
Util.applyInverseTransform(p, this.transform);
|
|
return p;
|
|
}
|
|
}
|
|
|
|
export { PageViewport };
|