Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various PageViewport and MessageHandler cleanup #9777

14 changes: 3 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,24 +877,16 @@ gulp.task('lib', ['buildnumber'], function () {
var licenseHeaderLibre =
fs.readFileSync('./src/license_header_libre.js').toString();
var preprocessor2 = require('./external/builder/preprocessor2.js');
var sharedFiles = [
'compatibility',
'global_scope',
'is_node',
'streams_polyfill',
'util',
];
var buildLib = merge([
gulp.src([
'src/{core,display}/*.js',
'src/shared/{' + sharedFiles.join() + '}.js',
'src/{core,display,shared}/*.js',
'!src/shared/{cffStandardStrings,fonts_utils}.js',
'src/{pdf,pdf.worker}.js',
], { base: 'src/', }),
gulp.src([
'examples/node/domstubs.js',
'web/*.js',
'!web/pdfjs.js',
'!web/viewer.js',
'!web/{pdfjs,viewer}.js',
], { base: '.', }),
gulp.src('test/unit/*.js', { base: '.', }),
]).pipe(transform('utf8', preprocess))
Expand Down
3 changes: 2 additions & 1 deletion src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

import {
arrayByteLength, arraysToBytes, assert, createPromiseCapability, info,
InvalidPDFException, MessageHandler, MissingPDFException, PasswordException,
InvalidPDFException, MissingPDFException, PasswordException,
setVerbosityLevel, UnexpectedResponseException, UnknownErrorException,
UNSUPPORTED_FEATURES, warn, XRefParseException
} from '../shared/util';
import { LocalPdfManager, NetworkPdfManager } from './pdf_manager';
import isNodeJS from '../shared/is_node';
import { MessageHandler } from '../shared/message_handler';
import { Ref } from './primitives';

var WorkerTask = (function WorkerTaskClosure() {
Expand Down
19 changes: 12 additions & 7 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

import {
assert, createPromiseCapability, getVerbosityLevel, info, InvalidPDFException,
isArrayBuffer, isNum, isSameOrigin, MessageHandler, MissingPDFException,
NativeImageDecoding, PageViewport, PasswordException, setVerbosityLevel,
shadow, stringToBytes, UnexpectedResponseException, UnknownErrorException,
unreachable, Util, warn
isArrayBuffer, isNum, isSameOrigin, MissingPDFException, NativeImageDecoding,
PasswordException, setVerbosityLevel, shadow, stringToBytes,
UnexpectedResponseException, UnknownErrorException, unreachable, Util, warn
} from '../shared/util';
import {
DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer,
DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer, PageViewport,
RenderingCancelledException, StatTimer
} from './dom_utils';
import { FontFaceObject, FontLoader } from './font_loader';
import { apiCompatibilityParams } from './api_compatibility';
import { CanvasGraphics } from './canvas';
import globalScope from '../shared/global_scope';
import { GlobalWorkerOptions } from './worker_options';
import { MessageHandler } from '../shared/message_handler';
import { Metadata } from './metadata';
import { PDFDataTransportStream } from './transport_stream';
import { WebGLContext } from './webgl';
Expand Down Expand Up @@ -885,7 +885,12 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
* along with transforms required for rendering.
*/
getViewport(scale, rotate = this.rotate, dontFlip = false) {
return new PageViewport(this.view, scale, rotate, 0, 0, dontFlip);
return new PageViewport({
viewBox: this.view,
scale,
rotation: rotate,
dontFlip,
});
},
/**
* @param {GetAnnotationsParameters} params - Annotation parameters.
Expand Down Expand Up @@ -1200,7 +1205,7 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
})();

class LoopbackPort {
constructor(defer) {
constructor(defer = true) {
this._listeners = [];
this._defer = defer;
this._deferred = Promise.resolve(undefined);
Expand Down
155 changes: 154 additions & 1 deletion src/display/dom_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import {
assert, CMapCompressionType, removeNullCharacters, stringToBytes,
unreachable, warn
unreachable, Util, warn
} from '../shared/util';

const DEFAULT_LINK_REL = 'noopener noreferrer nofollow';
Expand Down Expand Up @@ -135,6 +135,158 @@ class DOMSVGFactory {
}
}

/**
* @typedef {Object} PageViewportParameters
* @property {Array} viewBox - The xMin, yMin, xMax and yMax coordinates.
* @property {number} scale - The scale of the viewport.
* @property {number} rotation - The rotation, in degrees, of the viewport.
* @property {number} offsetX - (optional) The vertical, i.e. x-axis, offset.
* The default value is `0`.
* @property {number} offsetY - (optional) The horizontal, i.e. y-axis, offset.
* The default value is `0`.
* @property {boolean} dontFlip - (optional) If true, the x-axis will not be
* flipped. The default value is `false`.
*/

/**
* @typedef {Object} PageViewportCloneParameters
* @property {number} scale - (optional) The scale, overriding the one in the
* cloned viewport. The default value is `this.scale`.
* @property {number} rotation - (optional) The rotation, in degrees, overriding
* the one in the cloned viewport. The default value is `this.rotation`.
* @property {boolean} dontFlip - (optional) 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, scale, rotation, offsetX = 0, offsetY = 0,
dontFlip = false, }) {
this.viewBox = viewBox;
this.scale = scale;
this.rotation = rotation;
this.offsetX = offsetX;
this.offsetY = offsetY;

// creating transform to convert pdf coordinate system to the normal
// canvas like coordinates taking in account scale and rotation
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) {
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:
default:
rotateA = 1; rotateB = 0; rotateC = 0; rotateD = -1;
break;
}

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 = 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.width = width;
this.height = height;
}

/**
* Clones viewport, with optional additional properties.
* @param {PageViewportCloneParameters} - (optional)
* @return {PageViewport} Cloned viewport.
*/
clone({ scale = this.scale, rotation = this.rotation,
dontFlip = false, } = {}) {
return new PageViewport({
viewBox: this.viewBox.slice(),
scale,
rotation,
offsetX: this.offsetX,
offsetY: this.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.
* @return {Object} Object containing `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 {Array} rect - The xMin, yMin, xMax and yMax coordinates.
* @return {Array} Array containing 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 {number} x - The x-coordinate.
* @param {number} y - The y-coordinate.
* @return {Object} Object containing `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) {
this.message = msg;
Expand Down Expand Up @@ -277,6 +429,7 @@ class DummyStatTimer {
}

export {
PageViewport,
RenderingCancelledException,
addLinkAttributes,
getFilenameFromUrl,
Expand Down
Loading