From 19c2dfbb96d0e54969d58e2071fee446db77b0f5 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 28 Mar 2021 14:12:13 +0200 Subject: [PATCH] Move rotation normalization from `PDFViewerApplication` and into `BaseViewer` The rotation handling that's currently living in `PDFViewerApplication` is *very* old, and pre-dates the introduction of the viewer components by years. As can be seen in the `BaseViewer.pagesRotation` setter, we're not actually normalizing the rotation as intended and instead rely on the caller to handle that correctly. This is first of all inconsistent, given how other setters are implemented, and secondly it could also lead to the rotation being set to a value outside of the `[0, 360)`-range. Finally, for improved consistency the rotation handling in `PageViewport` is updated similarly. Please note that this case, it's *not* changing the pre-existing logic. --- src/display/display_utils.js | 7 +++++-- web/app.js | 6 +----- web/base_viewer.js | 5 +++++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 4e23ec876..d09e778f5 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -250,8 +250,11 @@ class PageViewport { const centerX = (viewBox[2] + viewBox[0]) / 2; const centerY = (viewBox[3] + viewBox[1]) / 2; let rotateA, rotateB, rotateC, rotateD; - rotation = rotation % 360; - rotation = rotation < 0 ? rotation + 360 : rotation; + // Normalize the rotation, by clamping it to the [0, 360) range. + rotation %= 360; + if (rotation < 0) { + rotation += 360; + } switch (rotation) { case 180: rotateA = -1; diff --git a/web/app.js b/web/app.js index e2f317fdb..125a56644 100644 --- a/web/app.js +++ b/web/app.js @@ -1856,11 +1856,7 @@ const PDFViewerApplication = { }, rotatePages(delta) { - if (!this.pdfDocument) { - return; - } - const newRotation = (this.pdfViewer.pagesRotation + 360 + delta) % 360; - this.pdfViewer.pagesRotation = newRotation; + this.pdfViewer.pagesRotation += delta; // Note that the thumbnail viewer is updated, and rendering is triggered, // in the 'rotationchanging' event handler. }, diff --git a/web/base_viewer.js b/web/base_viewer.js index 9d77e8bbb..b8ec64c54 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -388,6 +388,11 @@ class BaseViewer { if (!this.pdfDocument) { return; } + // Normalize the rotation, by clamping it to the [0, 360) range. + rotation %= 360; + if (rotation < 0) { + rotation += 360; + } if (this._pagesRotation === rotation) { return; // The rotation didn't change. }