[api-minor] Support custom offsetX/offsetY values in PDFPageProxy.getViewport and PageViewport.clone

There's no good reason, as far as I can tell, to not also support `offsetX`/`offsetY` in addition to e.g. `dontFlip`.
This commit is contained in:
Jonas Jenwald 2019-10-23 20:35:49 +02:00
parent 2046adcc49
commit 681bc9d70e
3 changed files with 21 additions and 5 deletions

View File

@ -805,6 +805,10 @@ class PDFDocumentProxy {
* @property {number} scale - The desired scale of the viewport. * @property {number} scale - The desired scale of the viewport.
* @property {number} [rotation] - The desired rotation, in degrees, of * @property {number} [rotation] - The desired rotation, in degrees, of
* the viewport. If omitted it defaults to the page rotation. * the viewport. If omitted it defaults to the page rotation.
* @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 * @property {boolean} [dontFlip] - If true, the y-axis will not be
* flipped. The default value is `false`. * flipped. The default value is `false`.
*/ */
@ -958,7 +962,8 @@ class PDFPageProxy {
* @returns {PageViewport} Contains 'width' and 'height' properties * @returns {PageViewport} Contains 'width' and 'height' properties
* along with transforms required for rendering. * along with transforms required for rendering.
*/ */
getViewport({ scale, rotation = this.rotate, dontFlip = false, } = {}) { getViewport({ scale, rotation = this.rotate,
offsetX = 0, offsetY = 0, dontFlip = false, } = {}) {
if ((typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) && if ((typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) &&
(arguments.length > 1 || typeof arguments[0] === 'number')) { (arguments.length > 1 || typeof arguments[0] === 'number')) {
throw new Error( throw new Error(
@ -968,6 +973,8 @@ class PDFPageProxy {
viewBox: this.view, viewBox: this.view,
scale, scale,
rotation, rotation,
offsetX,
offsetY,
dontFlip, dontFlip,
}); });
} }

View File

@ -174,6 +174,10 @@ class DOMSVGFactory {
* viewport. The default value is `this.scale`. * viewport. The default value is `this.scale`.
* @property {number} [rotation] - The rotation, in degrees, overriding the one * @property {number} [rotation] - The rotation, in degrees, overriding the one
* in the cloned viewport. The default value is `this.rotation`. * 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. * @property {boolean} [dontFlip] - If true, the x-axis will not be flipped.
* The default value is `false`. * The default value is `false`.
*/ */
@ -254,14 +258,14 @@ class PageViewport {
* @param {PageViewportCloneParameters} [params] * @param {PageViewportCloneParameters} [params]
* @returns {PageViewport} Cloned viewport. * @returns {PageViewport} Cloned viewport.
*/ */
clone({ scale = this.scale, rotation = this.rotation, clone({ scale = this.scale, rotation = this.rotation, offsetX = this.offsetX,
dontFlip = false, } = {}) { offsetY = this.offsetY, dontFlip = false, } = {}) {
return new PageViewport({ return new PageViewport({
viewBox: this.viewBox.slice(), viewBox: this.viewBox.slice(),
scale, scale,
rotation, rotation,
offsetX: this.offsetX, offsetX,
offsetY: this.offsetY, offsetY,
dontFlip, dontFlip,
}); });
} }

View File

@ -1100,6 +1100,11 @@ describe('api', function() {
expect(viewport.width).toEqual(1262.835); expect(viewport.width).toEqual(1262.835);
expect(viewport.height).toEqual(892.92); expect(viewport.height).toEqual(892.92);
}); });
it('gets viewport with "offsetX/offsetY" arguments', function () {
const viewport = page.getViewport({ scale: 1, rotation: 0,
offsetX: 100, offsetY: -100, });
expect(viewport.transform).toEqual([1, 0, 0, -1, 100, 741.89]);
});
it('gets viewport respecting "dontFlip" argument', function () { it('gets viewport respecting "dontFlip" argument', function () {
const scale = 1, rotation = 0; const scale = 1, rotation = 0;
let viewport = page.getViewport({ scale, rotation, }); let viewport = page.getViewport({ scale, rotation, });