Skip to content

Commit

Permalink
Merge pull request #17475 from calixteman/bug1872721
Browse files Browse the repository at this point in the history
[Editor] Take into account the crop box when saving/printing some highlighted text (bug 1872721)
  • Loading branch information
calixteman authored Jan 3, 2024
2 parents 231c798 + 35863cd commit dc01782
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/display/editor/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,15 @@ class HighlightEditor extends AnnotationEditor {
this.parent?.drawLayer.removeClass(this.#outlineId, "selected");
}

#serializeBoxes() {
#serializeBoxes(rect) {
const [pageWidth, pageHeight] = this.pageDimensions;
const boxes = this.#boxes;
const quadPoints = new Array(boxes.length * 8);
const [tx, ty] = rect;
let i = 0;
for (const { x, y, width, height } of boxes) {
const sx = x * pageWidth;
const sy = (1 - y - height) * pageHeight;
const sx = tx + x * pageWidth;
const sy = ty + (1 - y - height) * pageHeight;
// The specifications say that the rectangle should start from the bottom
// left corner and go counter-clockwise.
// But when opening the file in Adobe Acrobat it appears that this isn't
Expand All @@ -382,12 +383,11 @@ class HighlightEditor extends AnnotationEditor {
return quadPoints;
}

#serializeOutlines() {
#serializeOutlines(rect) {
const [pageWidth, pageHeight] = this.pageDimensions;
const width = this.width * pageWidth;
const height = this.height * pageHeight;
const tx = this.x * pageWidth;
const ty = (1 - this.y - this.height) * pageHeight;
const [tx, ty] = rect;
const outlines = [];
for (const outline of this.#highlightOutlines.outlines) {
const points = new Array(outline.length);
Expand All @@ -404,18 +404,22 @@ class HighlightEditor extends AnnotationEditor {
static deserialize(data, parent, uiManager) {
const editor = super.deserialize(data, parent, uiManager);

const { rect, color, quadPoints } = data;
const {
rect: [blX, blY, trX, trY],
color,
quadPoints,
} = data;
editor.color = Util.makeHexColor(...color);
editor.#opacity = data.opacity;

const [pageWidth, pageHeight] = editor.pageDimensions;
editor.width = (rect[2] - rect[0]) / pageWidth;
editor.height = (rect[3] - rect[1]) / pageHeight;
editor.width = (trX - blX) / pageWidth;
editor.height = (trY - blY) / pageHeight;
const boxes = (editor.#boxes = []);
for (let i = 0; i < quadPoints.length; i += 8) {
boxes.push({
x: quadPoints[4] / pageWidth,
y: 1 - quadPoints[i + 5] / pageHeight,
x: (quadPoints[4] - trX) / pageWidth,
y: (trY - (1 - quadPoints[i + 5])) / pageHeight,
width: (quadPoints[i + 2] - quadPoints[i]) / pageWidth,
height: (quadPoints[i + 5] - quadPoints[i + 1]) / pageHeight,
});
Expand All @@ -439,8 +443,8 @@ class HighlightEditor extends AnnotationEditor {
annotationType: AnnotationEditorType.HIGHLIGHT,
color,
opacity: this.#opacity,
quadPoints: this.#serializeBoxes(),
outlines: this.#serializeOutlines(),
quadPoints: this.#serializeBoxes(rect),
outlines: this.#serializeOutlines(rect),
pageIndex: this.pageIndex,
rect,
rotation: 0,
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,4 @@
!issue17215.pdf
!bug1863910.pdf
!bug1865341.pdf
!bug1872721.pdf
Binary file added test/pdfs/bug1872721.pdf
Binary file not shown.
47 changes: 47 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8411,5 +8411,52 @@
"rotation": 0
}
}
},
{
"id": "bug1872721-highlight-editor-save-print",
"file": "pdfs/bug1872721.pdf",
"md5": "8318654cda60dc71df9e62a3c78cf193",
"rounds": 1,
"type": "eq",
"save": true,
"print": true,
"annotationStorage": {
"pdfjs_internal_editor_0": {
"annotationType": 9,
"color": [ 83, 255, 188 ],
"opacity": 1,
"quadPoints": [
209.0454157057542,
718.4044878560951,
270.0049972773625,
718.4044878560951,
209.0454157057542,
731.7427487256604,
270.0049972773625,
731.7427487256604
],
"outlines": [
[
197.85841250000001,
709.458959,
197.85841250000001,
722.8621772,
259.003853,
722.8621772,
259.003853,
709.458959
]
],
"pageIndex": 0,
"rect": [
197.85841250000001,
709.458959,
259.003853,
722.8621772
],
"rotation": 0,
"structTreeParentId": null
}
}
}
]

0 comments on commit dc01782

Please sign in to comment.