Skip to content

Commit

Permalink
Add width/height getters in the Annotation class
Browse files Browse the repository at this point in the history
Currently we're manually computing the width/height of the /Rect-entry in a number of spots throughout the worker-thread Annotation code, which these new getters help avoid.
  • Loading branch information
Snuffleupagus committed Jan 31, 2025
1 parent 58c8f06 commit 6f29666
Showing 1 changed file with 20 additions and 35 deletions.
55 changes: 20 additions & 35 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ class Annotation {
const isUsingOwnCanvas = !!(
hasOwnCanvas && intent & RenderingIntentFlag.DISPLAY
);
if (isUsingOwnCanvas && (rect[0] === rect[2] || rect[1] === rect[3])) {
if (isUsingOwnCanvas && (this.width === 0 || this.height === 0)) {
// Empty annotation, don't draw anything.
this.data.hasOwnCanvas = false;
return {
Expand Down Expand Up @@ -1411,6 +1411,14 @@ class Annotation {
}
return fieldName.join(".");
}

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

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

/**
Expand Down Expand Up @@ -1960,14 +1968,9 @@ class WidgetAnnotation extends Annotation {
rotation = this.rotation;
}

if (rotation === 0) {
return IDENTITY_MATRIX;
}

const width = this.data.rect[2] - this.data.rect[0];
const height = this.data.rect[3] - this.data.rect[1];

return getRotationMatrix(rotation, width, height);
return rotation === 0
? IDENTITY_MATRIX
: getRotationMatrix(rotation, this.width, this.height);
}

getBorderAndBackgroundAppearances(annotationStorage) {
Expand All @@ -1979,12 +1982,10 @@ class WidgetAnnotation extends Annotation {
if (!this.backgroundColor && !this.borderColor) {
return "";
}
const width = this.data.rect[2] - this.data.rect[0];
const height = this.data.rect[3] - this.data.rect[1];
const rect =
rotation === 0 || rotation === 180
? `0 0 ${width} ${height} re`
: `0 0 ${height} ${width} re`;
? `0 0 ${this.width} ${this.height} re`
: `0 0 ${this.height} ${this.width} re`;

let str = "";
if (this.backgroundColor) {
Expand Down Expand Up @@ -2048,12 +2049,7 @@ class WidgetAnnotation extends Annotation {
);

const matrix = [1, 0, 0, 1, 0, 0];
const bbox = [
0,
0,
this.data.rect[2] - this.data.rect[0],
this.data.rect[3] - this.data.rect[1],
];
const bbox = [0, 0, this.width, this.height];
const transform = getTransformMatrix(this.data.rect, bbox, matrix);

let optionalContent;
Expand Down Expand Up @@ -2238,12 +2234,7 @@ class WidgetAnnotation extends Annotation {
const appearanceDict = (appearanceStream.dict = new Dict(xref));
appearanceDict.set("Subtype", Name.get("Form"));
appearanceDict.set("Resources", resources);
appearanceDict.set("BBox", [
0,
0,
this.data.rect[2] - this.data.rect[0],
this.data.rect[3] - this.data.rect[1],
]);
appearanceDict.set("BBox", [0, 0, this.width, this.height]);

const rotationMatrix = this.getRotationMatrix(annotationStorage);
if (rotationMatrix !== IDENTITY_MATRIX) {
Expand Down Expand Up @@ -2343,8 +2334,7 @@ class WidgetAnnotation extends Annotation {

const defaultPadding = 1;
const defaultHPadding = 2;
let totalHeight = this.data.rect[3] - this.data.rect[1];
let totalWidth = this.data.rect[2] - this.data.rect[0];
let { width: totalWidth, height: totalHeight } = this;

if (rotation === 90 || rotation === 270) {
[totalWidth, totalHeight] = [totalHeight, totalWidth];
Expand Down Expand Up @@ -3210,8 +3200,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}

_getDefaultCheckedAppearance(params, type) {
const width = this.data.rect[2] - this.data.rect[0];
const height = this.data.rect[3] - this.data.rect[1];
const { width, height } = this;
const bbox = [0, 0, width, height];

// Ratio used to have a mark slightly smaller than the bbox.
Expand Down Expand Up @@ -3596,8 +3585,7 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {

const defaultPadding = 1;
const defaultHPadding = 2;
let totalHeight = this.data.rect[3] - this.data.rect[1];
let totalWidth = this.data.rect[2] - this.data.rect[0];
let { width: totalWidth, height: totalHeight } = this;

if (rotation === 90 || rotation === 270) {
[totalWidth, totalHeight] = [totalHeight, totalWidth];
Expand Down Expand Up @@ -3809,10 +3797,7 @@ class PopupAnnotation extends Annotation {
// version.
this.data.noHTML = false;

if (
this.data.rect[0] === this.data.rect[2] ||
this.data.rect[1] === this.data.rect[3]
) {
if (this.width === 0 || this.height === 0) {
this.data.rect = null;
}

Expand Down

0 comments on commit 6f29666

Please sign in to comment.