Skip to content

Refactor annotation code to use a factory - #6273

Merged
Snuffleupagus merged 1 commit into
mozilla:masterfrom
timvandermeij:annotation-factory
Jul 29, 2015
Merged

Snuffleupagus merged 1 commit into
mozilla:masterfrom
timvandermeij:annotation-factory

Conversation

@timvandermeij

Copy link
Copy Markdown
Collaborator

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.

Comment thread src/core/annotation.js Outdated

Copy link
Copy Markdown
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
Copy Markdown
Collaborator 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.

Comment thread src/core/annotation.js Outdated

Copy link
Copy Markdown
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
Copy Markdown
Collaborator 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
Copy Markdown
Collaborator 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
Copy Markdown
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
Copy Markdown
Collaborator Author

/botio-linux preview

@pdfjsbot

Copy link
Copy Markdown

@timvandermeij

Copy link
Copy Markdown
Collaborator Author

/botio test

@pdfjsbot

Copy link
Copy Markdown

@pdfjsbot

Copy link
Copy Markdown

@pdfjsbot

Copy link
Copy Markdown

@pdfjsbot

Copy link
Copy Markdown

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
Copy Markdown
Collaborator

/botio-windows test

@pdfjsbot

Copy link
Copy Markdown

@pdfjsbot

Copy link
Copy Markdown

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
Copy Markdown
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
Comment thread src/core/annotation.js

Copy link
Copy Markdown
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
Copy Markdown
Collaborator 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