Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotate annotations based on the MK::R value (bug 1675139) #15060

Merged
merged 1 commit into from
Jun 21, 2022

Conversation

calixteman
Copy link
Contributor

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this perhaps also fix issue #9596?

this.rotation = 0;
if (mk instanceof Dict) {
let angle = mk.get("R") || 0;
if (angle !== 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's ensure that the angle is also an integer, since otherwise none of the code below makes sense.

Suggested change
if (angle !== 0) {
if (Number.isInteger(angle) && angle !== 0) {

Comment on lines 716 to 717
// If the angle is negative then angle % 360 is still negative.
angle = (360 + (angle % 360)) % 360;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm finding this a bit less clear than what we have in other parts of the code-base, see e.g.

// Normalize the rotation, by clamping it to the [0, 360) range.
rotation %= 360;
if (rotation < 0) {
rotation += 360;
}
and

pdf.js/web/base_viewer.js

Lines 509 to 513 in 45de73b

// Normalize the rotation, by clamping it to the [0, 360) range.
rotation %= 360;
if (rotation < 0) {
rotation += 360;
}

case 270:
return [0, -1, 1, 0, 0, height];
default:
return [1, 0, 0, 1, 0, 0];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be unreachable, given the if (rotation === 0) { ... } block above, right?
Hence I'd suggest replacing this with:

Suggested change
return [1, 0, 0, 1, 0, 0];
throw new Error("Invalid rotation");

Comment on lines 279 to 280
setRotation(angle, container) {
container ||= this.container;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just do the following?

Suggested change
setRotation(angle, container) {
container ||= this.container;
setRotation(angle, container = this.container) {

if (angle % 90 !== 0) {
throw new Error("Invalid rotation: must be a multiple of 90");
}
this._rotation = (360 + (angle % 360)) % 360;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, regarding the normalization of the rotation angle.

@calixteman calixteman force-pushed the annotation_rotation branch from 73a05e5 to b4e3f79 Compare June 19, 2022 20:04
Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the question in #15060 (review) :-)


r=me, with passing tests; thank you!

Comment on lines 716 to 718
// If the angle is negative then angle % 360 is still negative.
angle %= 360;
angle = angle >= 0 ? angle : 360 + angle;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just do exactly what's done elsewhere in the code-base, since re-assigning the value when it's non-negative just feels unnecessary?

Suggested change
// If the angle is negative then angle % 360 is still negative.
angle %= 360;
angle = angle >= 0 ? angle : 360 + angle;
// Normalize the rotation, by clamping it to the [0, 360) range.
angle %= 360;
if (angle < 0) {
angle += 360;
}

@calixteman calixteman linked an issue Jun 20, 2022 that may be closed by this pull request
@calixteman calixteman force-pushed the annotation_rotation branch from b4e3f79 to 3bebf31 Compare June 20, 2022 09:01
@calixteman
Copy link
Contributor Author

/botio test

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_test from @calixteman received. Current queue size: 0

Live output at: http://54.241.84.105:8877/1ceb5d3b61383b5/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_test from @calixteman received. Current queue size: 0

Live output at: http://54.193.163.58:8877/13263b2150144ac/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/1ceb5d3b61383b5/output.txt

Total script time: 23.40 mins

  • Font tests: Passed
  • Unit tests: FAILED
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 70
  different first/second rendering: 1

Image differences available at: http://54.241.84.105:8877/1ceb5d3b61383b5/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/13263b2150144ac/output.txt

Total script time: 24.89 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 26

Image differences available at: http://54.193.163.58:8877/13263b2150144ac/reftest-analyzer.html#web=eq.log

@calixteman calixteman force-pushed the annotation_rotation branch from 3bebf31 to 715d61d Compare June 20, 2022 15:26
@calixteman
Copy link
Contributor Author

/botio test

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_test from @calixteman received. Current queue size: 0

Live output at: http://54.241.84.105:8877/24f1c535738c29a/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_test from @calixteman received. Current queue size: 0

Live output at: http://54.193.163.58:8877/3de612af212205b/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/24f1c535738c29a/output.txt

Total script time: 24.16 mins

  • Font tests: Passed
  • Unit tests: FAILED
  • Integration Tests: FAILED
  • Regression tests: FAILED
  different ref/snapshot: 41
  different first/second rendering: 1

Image differences available at: http://54.241.84.105:8877/24f1c535738c29a/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/3de612af212205b/output.txt

Total script time: 25.24 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Integration Tests: FAILED
  • Regression tests: FAILED
  different ref/snapshot: 10

Image differences available at: http://54.193.163.58:8877/3de612af212205b/reftest-analyzer.html#web=eq.log

@calixteman calixteman force-pushed the annotation_rotation branch from 715d61d to 4d5ab84 Compare June 21, 2022 07:29
@calixteman
Copy link
Contributor Author

/botio test

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_test from @calixteman received. Current queue size: 0

Live output at: http://54.241.84.105:8877/e737b527aafe572/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_test from @calixteman received. Current queue size: 0

Live output at: http://54.193.163.58:8877/d55981fc023da10/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/e737b527aafe572/output.txt

Total script time: 14.58 mins

  • Font tests: Passed
  • Unit tests: FAILED
  • Integration Tests: Passed
  • Regression tests: FAILED

Image differences available at: http://54.241.84.105:8877/e737b527aafe572/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/d55981fc023da10/output.txt

Total script time: 28.40 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 4

Image differences available at: http://54.193.163.58:8877/d55981fc023da10/reftest-analyzer.html#web=eq.log

@calixteman
Copy link
Contributor Author

/botio browsertest

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_browsertest from @calixteman received. Current queue size: 1

Live output at: http://54.241.84.105:8877/e6f7dd620a48d4b/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_browsertest from @calixteman received. Current queue size: 0

Live output at: http://54.193.163.58:8877/f1d9074371d6af0/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/f1d9074371d6af0/output.txt

Total script time: 21.26 mins

  • Regression tests: FAILED
  different ref/snapshot: 2

Image differences available at: http://54.193.163.58:8877/f1d9074371d6af0/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/e6f7dd620a48d4b/output.txt

Total script time: 21.89 mins

  • Regression tests: FAILED
  different ref/snapshot: 25
  different first/second rendering: 1

Image differences available at: http://54.241.84.105:8877/e6f7dd620a48d4b/reftest-analyzer.html#web=eq.log

@calixteman
Copy link
Contributor Author

@Snuffleupagus, would you mind to review this patch again ?
I had to make few changes to handle correctly save/print stuff and to support correctly all the different type of fields.

await closePages(pages);
});

it("must data-annotation-attribute has the correct value", async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean something like the following here?

Suggested change
it("must data-annotation-attribute has the correct value", async () => {
it("must check that data-annotation-rotation is correct", async () => {

if (mk instanceof Dict) {
let angle = mk.get("R") || 0;
if (Number.isInteger(angle) && angle !== 0) {
// If the angle is negative then angle % 360 is still negative.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can probably be removed.

}
}

getRotationMatrix(annotationStorage) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary for all Annotation-types, or can we perhaps move it into the WidgetAnnotation-class instead?

Comment on lines 1684 to 1699
if (rotation || this.borderColor || this.backgroundColor) {
const MK = new Dict(xref);
MK.set("R", rotation);
MK.set("BC", getPDFRgbArray(this.borderColor));
MK.set("BG", getPDFRgbArray(this.backgroundColor));
dict.set("MK", MK);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is repeated multiple times, how about adding a helper-method on the WidgetAnnotation-class that returns a MK-dict when that's necessary?

Also, do we need to populate some of the dictionary entries with empty Arrays (and is that even generally correct)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if you add a method like this, on the WidgetAnnotation-class:

_getMKDict(rotation) {
  const mk = new Dict(null); // `null` can probably be used here, since none of the data below will be a /Ref
  if (rotation) {
    mk.set("R", rotation);
  }
  if (this.borderColor) {
    mk.set("BC", Array.from(this.borderColor).map(c => c / 255));
  }
  if (this.backgroundColor) {
    mk.set("BG", Array.from(this.backgroundColor).map(c => c / 255));
  }
  return mk.size > 0 ? mk : null;
}

and then you can simply do (here and elsewhere):

Suggested change
if (rotation || this.borderColor || this.backgroundColor) {
const MK = new Dict(xref);
MK.set("R", rotation);
MK.set("BC", getPDFRgbArray(this.borderColor));
MK.set("BG", getPDFRgbArray(this.backgroundColor));
dict.set("MK", MK);
}
const maybeMK = this._getMKDict(rotation);
if (maybeMK) {
dict.set("MK", maybeMK);
}

@@ -721,13 +778,24 @@ class Annotation {
}
}

getBorderAndBackgroundAppearances() {
getBorderAndBackgroundAppearances(annotationStorage) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is existing code, but the question above (regarding WidgetAnnotation) applies here as well.

}

if (rotation === 0) {
return [1, 0, 0, 1, 0, 0];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how much it matters, but you may want to return IDENTITY_MATRIX here (can be imported from src/shared/util.js)?

Comment on lines 1699 to 1709
if (rotationMatrix[0] !== 1) {
// The matrix isn't the identity one.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how much it matters here, but with a previous comment implemented you could just do the following here:

Suggested change
if (rotationMatrix[0] !== 1) {
// The matrix isn't the identity one.
if (rotationMatrix === IDENTITY_MATRIX) {

@@ -2220,6 +2343,17 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
: this.uncheckedAppearance;
if (appearance) {
const savedAppearance = this.appearance;
const savedMatrix = appearance.dict.getArray("Matrix") || [
1, 0, 0, 1, 0, 0,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, you could maybe just fallback on IDENTITY_MATRIX here?

Comment on lines 2428 to 2439
if (rotation || this.borderColor || this.backgroundColor) {
const MK = new Dict(evaluator.xref);
MK.set("R", rotation);
MK.set("BC", getPDFRgbArray(this.borderColor));
MK.set("BG", getPDFRgbArray(this.backgroundColor));
dict.set("MK", MK);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see previous comments.

Comment on lines 2515 to 2523
if (rotation || this.borderColor || this.backgroundColor) {
const MK = new Dict(evaluator.xref);
MK.set("R", rotation);
MK.set("BC", getPDFRgbArray(this.borderColor));
MK.set("BG", getPDFRgbArray(this.backgroundColor));
dict.set("MK", MK);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see previous comments.

@calixteman calixteman force-pushed the annotation_rotation branch from 863b4d0 to 34ba918 Compare June 21, 2022 15:19
@calixteman
Copy link
Contributor Author

/botio test

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_test from @calixteman received. Current queue size: 0

Live output at: http://54.241.84.105:8877/3273fbcaed07a30/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_test from @calixteman received. Current queue size: 0

Live output at: http://54.193.163.58:8877/d8a3b1614b4457a/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/3273fbcaed07a30/output.txt

Total script time: 26.20 mins

  • Font tests: Passed
  • Unit tests: FAILED
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 31

Image differences available at: http://54.241.84.105:8877/3273fbcaed07a30/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/d8a3b1614b4457a/output.txt

Total script time: 28.40 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 2

Image differences available at: http://54.193.163.58:8877/d8a3b1614b4457a/reftest-analyzer.html#web=eq.log

@calixteman
Copy link
Contributor Author

The "regression" is due to a rotation of 270deg on a checkbox and this one is not exactly a square, hence the change, so it's an improvement.

@@ -1535,20 +1596,42 @@ class WidgetAnnotation extends Annotation {
);
}

_getMKDict(rotation) {
const mk = new Dict(null); // `null` can probably be used here, since none of the data below will be a /Ref
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the comment, since I only included it to explain my reasoning as part of the review comment.

- it aims to fix: https://bugzilla.mozilla.org/show_bug.cgi?id=1675139;
- An annotation can be rotated (counterclockwise);
- the rotation can be set in using JS.
@calixteman calixteman force-pushed the annotation_rotation branch from 34ba918 to cdc58b7 Compare June 21, 2022 15:57
@calixteman calixteman merged commit 8d466f5 into mozilla:master Jun 21, 2022
@calixteman
Copy link
Contributor Author

/botio makeref

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_makeref from @calixteman received. Current queue size: 2

Live output at: http://54.193.163.58:8877/88023865608c3bd/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_makeref from @calixteman received. Current queue size: 1

Live output at: http://54.241.84.105:8877/b8be093142b6744/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/b8be093142b6744/output.txt

Total script time: 22.92 mins

  • Lint: Passed
  • Make references: Passed
  • Check references: Passed

@pdfjsbot
Copy link

From: Bot.io (Windows)


Success

Full output at http://54.193.163.58:8877/88023865608c3bd/output.txt

Total script time: 22.33 mins

  • Lint: Passed
  • Make references: Passed
  • Check references: Passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Acroform Field not rotating with document
3 participants