[api-minor] Add a new PageViewport-getter to access the original, un-scaled, viewport dimensions

While reviewing recent patches, I couldn't help but noticing that we now have a lot of call-sites that manually access the `PageViewport.viewBox`-property.
Rather than repeating that verbatim all over the code-base, this patch adds a lazily computed and cached getter for this data instead.
This commit is contained in:
Jonas Jenwald 2022-12-08 12:37:18 +01:00
parent 8e11cf9b1c
commit cafdc48147
5 changed files with 45 additions and 41 deletions

View file

@ -196,18 +196,14 @@ class AnnotationElement {
* @returns {HTMLElement} A section element.
*/
_createContainer(ignoreBorder = false) {
const data = this.data,
page = this.page,
viewport = this.viewport;
const { data, page, viewport } = this;
const container = document.createElement("section");
const { width, height } = getRectDims(data.rect);
const [pageLLx, pageLLy, pageURx, pageURy] = viewport.viewBox;
const pageWidth = pageURx - pageLLx;
const pageHeight = pageURy - pageLLy;
container.setAttribute("data-annotation-id", data.id);
const { pageWidth, pageHeight, pageX, pageY } = viewport.rawDims;
const { width, height } = getRectDims(data.rect);
// Do *not* modify `data.rect`, since that will corrupt the annotation
// position on subsequent calls to `_createContainer` (see issue 6804).
const rect = Util.normalizeRect([
@ -268,8 +264,8 @@ class AnnotationElement {
}
}
container.style.left = `${(100 * (rect[0] - pageLLx)) / pageWidth}%`;
container.style.top = `${(100 * (rect[1] - pageLLy)) / pageHeight}%`;
container.style.left = `${(100 * (rect[0] - pageX)) / pageWidth}%`;
container.style.top = `${(100 * (rect[1] - pageY)) / pageHeight}%`;
const { rotation } = data;
if (data.hasOwnCanvas || rotation === 0) {
@ -283,9 +279,7 @@ class AnnotationElement {
}
setRotation(angle, container = this.container) {
const [pageLLx, pageLLy, pageURx, pageURy] = this.viewport.viewBox;
const pageWidth = pageURx - pageLLx;
const pageHeight = pageURy - pageLLy;
const { pageWidth, pageHeight } = this.viewport.rawDims;
const { width, height } = getRectDims(this.data.rect);
let elementWidth, elementHeight;
@ -1838,12 +1832,10 @@ class PopupAnnotationElement extends AnnotationElement {
rect[0] + this.data.parentRect[2] - this.data.parentRect[0];
const popupTop = rect[1];
const [pageLLx, pageLLy, pageURx, pageURy] = this.viewport.viewBox;
const pageWidth = pageURx - pageLLx;
const pageHeight = pageURy - pageLLy;
const { pageWidth, pageHeight, pageX, pageY } = this.viewport.rawDims;
this.container.style.left = `${(100 * (popupLeft - pageLLx)) / pageWidth}%`;
this.container.style.top = `${(100 * (popupTop - pageLLy)) / pageHeight}%`;
this.container.style.left = `${(100 * (popupLeft - pageX)) / pageWidth}%`;
this.container.style.top = `${(100 * (popupTop - pageY)) / pageHeight}%`;
this.container.append(popup.render());
return this.container;