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

Refactor annotation code to use a factory #6273

Merged
merged 1 commit into from
Jul 29, 2015

Conversation

timvandermeij
Copy link
Contributor

Currently, src/core/core.js uses the fromRef method on an Annotation object to obtain the right annotation type object (such as LinkAnnotation or TextAnnotation). That method in turn uses a method getConstructor to find out which annotation type object must be returned.

Aside from the fact that there is currently a lot of code to achieve this, these methods should not be part of the base Annotation class at all. Creation of annotation object should be done by a factory (as also recommended by @yurydelendik at #5218 (comment)) that handles finding out the correct annotation type object and returning it. This patch implements this separation of concerns.

Doing this allows us to also simplify the code quite a bit and to make it more readable. Additionally, we are now able to get rid of the hardcoded array of supported annotation types. The factory takes care of checking the annotation types and falls back to returning the base annotation type (and issuing a warning, which the current code also does not do well) when an annotation type is unsupported.

I have manually tested this commit with 20 test PDFs with different annotation types, such as /Link, /Text, /Widget, /FileAttachment and /FreeText. All render identically before and after the patch, and unsupported annotation types are now properly indicated with a warning in the console.

@Snuffleupagus Would you be so kind again to review this? To test this, you can use all PDFs in issues with the 4-annotation label (both open and closed) and check rendering and the console before and after the patch. If you want even more test files, I can provide the 20 test PDFs I used to test this, but I think the 4-annotation label contains enough PDFs to properly verify this. A sample PDF to see that warnings are now properly put in the console is http://mirrors.rit.edu/CTAN/macros/latex/contrib/pdfcomment/doc/example.pdf.

return;
}

// Determine the annotation's subtype
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Can you add a period at the end of the sentence, both here and below :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in the new commit.

var subtype = dict.get('Subtype');
subtype = isName(subtype) ? subtype.name : '';

// Determine the annotation's field type.
Copy link
Contributor

Choose a reason for hiding this comment

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

is this prepared for other annotations which have a field type? because i think this could be also done on demand on below switch and done only when actually needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Only Widget annotations (for interactive forms) use field types and that is also how the current code uses it, so we might as well get it over there where it is really needed. Fixed in the new commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By the way, the test suite contains annotation-tx.pdf to verify that this change still works as expected.

@CodingFabian
Copy link
Contributor

lgtm

Currently, `src/core/core.js` uses the `fromRef` method on an `Annotation` object to obtain the right annotation type object (such as `LinkAnnotation` or `TextAnnotation`). That method in turn uses a method `getConstructor` to find out which annotation type object must be returned.

Aside from the fact that there is currently a lot of code to achieve this, these methods should not be part of the base `Annotation` class at all. Creation of annotation object should be done by a factory (as also recommended by @yurydelendik at mozilla#5218 (comment)) that handles finding out the correct annotation type object and returning it. This patch implements this separation of concerns.

Doing this allows us to also simplify the code quite a bit and to make it more readable. Additionally, we are now able to get rid of the hardcoded array of supported annotation types. The factory takes care of checking the annotation types and falls back to returning the base annotation type (and issuing a warning, which the current code also does not do well) when an annotation type is unsupported.

I have manually tested this commit with 20 test PDFs with different annotation types, such as /Link, /Text, /Widget, /FileAttachment and /FreeText. All render identically before and after the patch, and unsupported annotation types are now properly indicated with a warning in the console.
@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/2976fbf6657adee/output.txt

@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/bfad4c828ed68e9/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

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

Live output at: http://107.22.172.223:8877/0c61e98948fa15c/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Failed

Full output at http://107.22.172.223:8877/0c61e98948fa15c/output.txt

Total script time: 2.98 mins

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

Image differences available at: http://107.22.172.223:8877/0c61e98948fa15c/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

From: Bot.io (Linux)


Success

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

Total script time: 18.92 mins

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

@Snuffleupagus
Copy link
Collaborator

/botio-windows test

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

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

Live output at: http://107.22.172.223:8877/004ce1855676f7b/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Success

Full output at http://107.22.172.223:8877/004ce1855676f7b/output.txt

Total script time: 18.58 mins

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

Snuffleupagus added a commit that referenced this pull request Jul 29, 2015
Refactor annotation code to use a factory
@Snuffleupagus Snuffleupagus merged commit e876dd8 into mozilla:master Jul 29, 2015
@Snuffleupagus
Copy link
Collaborator

Improved code structure, and a net reduction in number of code lines, that's always nice :)
Thanks for the patch; and also for doing the annotation re-factoring in smaller chunks, since that makes reviewing easier!

@timvandermeij timvandermeij deleted the annotation-factory branch July 29, 2015 17:56
return new WidgetAnnotation(parameters);

default:
warn('Unimplemented annotation type "' + subtype + '", ' +
Copy link
Contributor

Choose a reason for hiding this comment

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

This message is shown when loading a document with highlights. Is this expected? Highlight annotations are rendered fine and it doesn't need any special implementation (unless there is plan to support interactive annotations).

pdf.worker.js:224 Warning: Unimplemented annotation type "Highlight", falling back to base annotation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is expected as it was not listed as supported in the original code. This patch just refactored the original code and I did not do further checks on the other annotation types yet. We will get to the other annotation types though, so if there is indeed nothing else to be done for Highlight annotations, then that will be changed.

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.

5 participants