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

Annotations: move operator list addition logic to src/core/document.js #8072

Merged

Conversation

timvandermeij
Copy link
Contributor

@timvandermeij timvandermeij commented Feb 15, 2017

Ideally, the Annotation class should not have anything to do with the page's operator list. How annotations are added to the page's operator list is logic that belongs in src/core/document.js instead where the operator list is constructed.

Moreover, some comments have been added to clarify the intent of the code.

@timvandermeij
Copy link
Contributor Author

/botio-linux preview

@timvandermeij
Copy link
Contributor Author

/botio test

@Snuffleupagus Snuffleupagus self-assigned this Feb 18, 2017
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.

Sorry about the delay here!
My initial reaction was that this feels like a lot of code to be putting in the Page.getOperatorList method. However, I'm not sure that moving this annotation code to a helper method in the Page "class" would be much better, considering the number of parameters that you'd still need to pass around then.

Since I cannot really argue with the intent of this patch, I suppose that I'm fine with it :-)
However, I've got one (sort of related) suggestion here though: Would it now perhaps make sense to add a helper function for checking the viewability/printability of annotations (e.g. like below)?

diff --git a/src/core/document.js b/src/core/document.js
index 9217710..4b9b988 100644
--- a/src/core/document.js
+++ b/src/core/document.js
@@ -71,6 +71,11 @@ var Page = (function PageClosure() {
   var DEFAULT_USER_UNIT = 1.0;
   var LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
 
+  function isAnnotationRenderable(annotation, intent) {
+    return (intent === 'display' && annotation.viewable) ||
+           (intent === 'print' && annotation.printable);
+  }
+
   function Page(pdfManager, xref, pageIndex, pageDict, ref, fontCache) {
     this.pdfManager = pdfManager;
     this.pageIndex = pageIndex;
@@ -284,8 +289,7 @@ var Page = (function PageClosure() {
         var i, ii;
         var opListPromises = [];
         for (i = 0, ii = annotations.length; i < ii; i++) {
-          if ((intent === 'display' && annotations[i].viewable) ||
-              (intent === 'print' && annotations[i].printable)) {
+          if (isAnnotationRenderable(annotations[i], intent)) {
             opListPromises.push(annotations[i].getOperatorList(
               partialEvaluator, task, renderInteractiveForms));
           }
@@ -347,13 +351,9 @@ var Page = (function PageClosure() {
       var annotations = this.annotations;
       var annotationsData = [];
       for (var i = 0, n = annotations.length; i < n; ++i) {
-        if (intent) {
-          if (!(intent === 'display' && annotations[i].viewable) &&
-              !(intent === 'print' && annotations[i].printable)) {
-            continue;
-          }
+        if (!intent || isAnnotationRenderable(annotations[i], intent)) {
+          annotationsData.push(annotations[i].data);
         }
-        annotationsData.push(annotations[i].data);
       }
       return annotationsData;
     },

@timvandermeij timvandermeij force-pushed the annotation-append-operator-list branch 2 times, most recently from 9c6ee0b to 1a85896 Compare February 26, 2017 21:20
@timvandermeij
Copy link
Contributor Author

timvandermeij commented Feb 26, 2017

My initial reaction was that this feels like a lot of code to be putting in the Page.getOperatorList method. However, I'm not sure that moving this annotation code to a helper method in the Page "class" would be much better, considering the number of parameters that you'd still need to pass around then.

I thought about that as well and came to the same conclusion as you did. In the new patch I managed to remove one more line that wasn't really necessary. In total we're adding around 15 lines to Page.getOperatorList, which in my opinion is okay, especially since some of them are just comments.

Would it now perhaps make sense to add a helper function for checking the viewability/printability of annotations (e.g. like below)?

It most certainly does! I added it to the new patch. This makes the patch size +27 -30, which is also nice.

@timvandermeij
Copy link
Contributor Author

/botio-linux preview

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.

r=me, with nits addressed and passing tests.
Thanks for the patch.

for (i = 0, ii = opLists.length; i < ii; i++) {
pageOpList.addOpList(opLists[i]);
}
pageOpList.addOp(OPS.endAnnotations, []);
Copy link
Collaborator

@Snuffleupagus Snuffleupagus Feb 27, 2017

Choose a reason for hiding this comment

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

Nit: For improved readability, considering the importance of flushing the operatorList, I think that it'd be a good idea to add a new line after the annotation OPS here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the new commit.

// Collect the operator list promises for the annotations. Each promise
// is resolved with the complete operator list for a single annotation.
var i, ii;
var opListPromises = [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I you want to offset the (tiny) increase in diff size from the previous comment, I think that you could simply do var i, ii, opListPromises = []; here instead since all these variables are sort of temporary anyway :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the new commit.

Ideally, the `Annotation` class should not have anything to do with the
page's operator list. How annotations are added to the page's operator
list is logic that belongs in `src/core/document.js` instead where the
operator list is constructed.

Moreover, some comments have been added to clarify the intent of the
code.
@timvandermeij
Copy link
Contributor Author

/botio-linux preview

@pdfjsbot
Copy link

From: Bot.io (Linux)


Received

Command cmd_preview from @timvandermeij received. Current queue size: 0

Live output at: http://107.21.233.14:8877/9e6b3b52c977444/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux)


Success

Full output at http://107.21.233.14:8877/9e6b3b52c977444/output.txt

Total script time: 2.19 mins

Published

@timvandermeij
Copy link
Contributor Author

/botio test

@pdfjsbot
Copy link

From: Bot.io (Linux)


Received

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

Live output at: http://107.21.233.14:8877/a0e2843f64163a8/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_test from @timvandermeij received. Current queue size: 1

Live output at: http://54.215.176.217:8877/0b8790c920e1197/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Success

Full output at http://54.215.176.217:8877/0b8790c920e1197/output.txt

Total script time: 21.66 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Regression tests: Passed

@pdfjsbot
Copy link

From: Bot.io (Linux)


Success

Full output at http://107.21.233.14:8877/a0e2843f64163a8/output.txt

Total script time: 26.38 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Regression tests: Passed

@timvandermeij timvandermeij merged commit 4e201d3 into mozilla:master Feb 27, 2017
@timvandermeij timvandermeij deleted the annotation-append-operator-list branch February 27, 2017 21:50
movsb pushed a commit to movsb/pdf.js that referenced this pull request Jul 14, 2018
…operator-list

Annotations: move operator list addition logic to `src/core/document.js`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants