Skip to content

Commit

Permalink
Add width/height getters in the AnnotationElement class
Browse files Browse the repository at this point in the history
This is similar to PR 19397, but for the main-thread code, and helps to slightly shorten the code.
  • Loading branch information
Snuffleupagus committed Feb 1, 2025
1 parent b48717a commit d48ffbe
Showing 1 changed file with 23 additions and 32 deletions.
55 changes: 23 additions & 32 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ const DEFAULT_TAB_INDEX = 1000;
const DEFAULT_FONT_SIZE = 9;
const GetElementsByNameSet = new WeakSet();

function getRectDims(rect) {
return {
width: rect[2] - rect[0],
height: rect[3] - rect[1],
};
}

/**
* @typedef {Object} AnnotationElementParameters
* @property {Object} data
Expand Down Expand Up @@ -243,12 +236,11 @@ class AnnotationElement {
},
} = this;
currentRect?.splice(0, 4, ...rect);
const { width, height } = getRectDims(rect);
style.left = `${(100 * (rect[0] - pageX)) / pageWidth}%`;
style.top = `${(100 * (pageHeight - rect[3] + pageY)) / pageHeight}%`;
if (rotation === 0) {
style.width = `${(100 * width) / pageWidth}%`;
style.height = `${(100 * height) / pageHeight}%`;
style.width = `${(100 * /* width = */ (rect[2] - rect[0])) / pageWidth}%`;
style.height = `${(100 * /* height = */ (rect[3] - rect[1])) / pageHeight}%`;
} else {
this.setRotation(rotation);
}
Expand All @@ -266,6 +258,8 @@ class AnnotationElement {
const {
data,
parent: { page, viewport },
width,
height,
} = this;

const container = document.createElement("section");
Expand Down Expand Up @@ -298,8 +292,6 @@ class AnnotationElement {
return container;
}

const { width, height } = getRectDims(data.rect);

if (!ignoreBorder && data.borderStyle.width > 0) {
style.borderWidth = `${data.borderStyle.width}px`;

Expand Down Expand Up @@ -381,19 +373,13 @@ class AnnotationElement {
return;
}
const { pageWidth, pageHeight } = this.parent.viewport.rawDims;
const { width, height } = getRectDims(this.data.rect);
let { width, height } = this;

let elementWidth, elementHeight;
if (angle % 180 === 0) {
elementWidth = (100 * width) / pageWidth;
elementHeight = (100 * height) / pageHeight;
} else {
elementWidth = (100 * height) / pageWidth;
elementHeight = (100 * width) / pageHeight;
if (angle % 180 !== 0) {
[width, height] = [height, width];
}

container.style.width = `${elementWidth}%`;
container.style.height = `${elementHeight}%`;
container.style.width = `${(100 * width) / pageWidth}%`;
container.style.height = `${(100 * height) / pageHeight}%`;

container.setAttribute("data-main-rotation", (360 - angle) % 360);
}
Expand Down Expand Up @@ -748,6 +734,14 @@ class AnnotationElement {
});
});
}

get width() {
return this.data.rect[2] - this.data.rect[0];
}

get height() {
return this.data.rect[3] - this.data.rect[1];
}
}

class LinkAnnotationElement extends AnnotationElement {
Expand Down Expand Up @@ -2537,8 +2531,7 @@ class LineAnnotationElement extends AnnotationElement {
// Create an invisible line with the same starting and ending coordinates
// that acts as the trigger for the popup. Only the line itself should
// trigger the popup, not the entire container.
const data = this.data;
const { width, height } = getRectDims(data.rect);
const { data, width, height } = this;
const svg = this.svgFactory.create(
width,
height,
Expand Down Expand Up @@ -2592,8 +2585,7 @@ class SquareAnnotationElement extends AnnotationElement {
// Create an invisible square with the same rectangle that acts as the
// trigger for the popup. Only the square itself should trigger the
// popup, not the entire container.
const data = this.data;
const { width, height } = getRectDims(data.rect);
const { data, width, height } = this;
const svg = this.svgFactory.create(
width,
height,
Expand Down Expand Up @@ -2649,8 +2641,7 @@ class CircleAnnotationElement extends AnnotationElement {
// Create an invisible circle with the same ellipse that acts as the
// trigger for the popup. Only the circle itself should trigger the
// popup, not the entire container.
const data = this.data;
const { width, height } = getRectDims(data.rect);
const { data, width, height } = this;
const svg = this.svgFactory.create(
width,
height,
Expand Down Expand Up @@ -2712,11 +2703,12 @@ class PolylineAnnotationElement extends AnnotationElement {
// popup, not the entire container.
const {
data: { rect, vertices, borderStyle, popupRef },
width,
height,
} = this;
if (!vertices) {
return this.container;
}
const { width, height } = getRectDims(rect);
const svg = this.svgFactory.create(
width,
height,
Expand Down Expand Up @@ -3221,8 +3213,7 @@ class AnnotationLayer {
}
const isPopupAnnotation = data.annotationType === AnnotationType.POPUP;
if (!isPopupAnnotation) {
const { width, height } = getRectDims(data.rect);
if (width <= 0 || height <= 0) {
if (data.rect[2] === data.rect[0] || data.rect[3] === data.rect[1]) {
continue; // Ignore empty annotations.
}
} else {
Expand Down

0 comments on commit d48ffbe

Please sign in to comment.