Commit Graph

231 Commits

Author SHA1 Message Date
Jonas Jenwald
05169efec1 [Regression] Ensure that documents with varying page sizes are initialized correctly (PR 15812 follow-up)
The rotation-caching added in PR 15812 completely breaks initialization of PDF documents with varying page sizes, causing all pages to wrongly get the same size; see e.g. `sizes.pdf` from the test-suite.
To fix that without having to e.g. add a new parameter, which feels error prone, this patch changes the `PDFPageView.#setDimensions` method to completely ignore the rotation-caching until the `setPdfPage`-method has been called.
2022-12-27 12:14:42 +01:00
Calixte Denizet
c677b167b6 Set a z-index for the different layers within a page (fixes #15861)
Each layer has an absolute position, hence it induces the creation of
some stacking contexts (see https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context).
Thanks to this feature, we don't have to worry about the z-index used in
the annotation layer and how form elements interact with the other layers.
2022-12-26 22:55:08 +01:00
Calixte Denizet
663007a5c7 Only redraw after zooming is finished (bug 1661253)
Right now, the visible pages are redrawn for each scale change.
Consequently, zooming with mouse wheel or in pinching can be pretty janky
(even on a desktop machine but with a hdpi screen).
So the main idea in this patch is to draw the visible pages only once zooming
is finished.
2022-12-26 18:13:17 +01:00
Jonas Jenwald
d042a3ca76 Ensure that the loadingIconDiv is always visible (PR 15829 follow-up)
After the changes in PR 15829 the `loadingIconDiv` is no longer always visible when it should be, specifically in the case where we cancel and re-render a partially parsed/rendered page.
To reproduce this, try opening https://github.com/mozilla/pdf.js/files/1522715/wuppertal_2012.pdf in the viewer and change the zoom level while rendering is ongoing. In this case the `loadingIconDiv` doesn't actually become visible, despite being present in the DOM, since it's no longer at the end of the page-div.

I don't know to what extent this renders PR 15829 "pointless", however we're not repeatedly re-creating and re-inserting the `loadingIconDiv` but rather just *move* the existing element in the DOM.
2022-12-22 12:34:32 +01:00
Jonas Jenwald
e8c7278337 Improve the linkService handling in the DEFAULT_LAYER_PROPERTIES (PR 15811 follow-up)
We'll no longer import the `SimpleLinkService` dependency unconditionally in the file, since it's only used in COMPONENTS-builds.
Furthermore, for the COMPONENTS-builds, we'll create a `SimpleLinkService`-instance only for those layers that actually need it.
2022-12-19 01:04:04 +01:00
Jonas Jenwald
8b8d890064 [api-minor] Remove the xfaLayerFactory in the viewer
Please note that this functionality has never really mattered for the Firefox PDF Viewer, the GENERIC viewer, or even the "simpleviewer"/"singlepageviewer" component-examples. Hence, in practice this means that only the "pageviewer" component-example[1] have ever really utilized this.

Using factories to initialize various layers in the viewer, rather than simply invoking the relevant code directly, seems (at least to me) like a somewhat roundabout way of doing things.
Not only does this lead to more code, both to write and maintain, but since many of the layers have common parameters (e.g. an `AnnotationStorage`-instance) there's also some duplication.

Hence this patch, which removes the `xfaLayerFactory` and instead uses a lookup-function in the `PDFPageView`-class to access the external viewer-properties as necessary.
Note that this should even be an improvement for the "pageviewer" component-example, since most layers will now work by default rather than require manual configuration.

---
[1] In practice we generally suggest using the "simpleviewer", or "singlepageviewer", since it does *most* things out-of-the-box and given that a lot of functionality really require *a viewer* and not just a single page in order to work.
2022-12-18 13:26:54 +01:00
Jonas Jenwald
c393148748 [api-minor] Remove the textLayerFactory in the viewer
Please note that this functionality has never really mattered for the Firefox PDF Viewer, the GENERIC viewer, or even the "simpleviewer"/"singlepageviewer" component-examples. Hence, in practice this means that only the "pageviewer" component-example[1] have ever really utilized this.

Using factories to initialize various layers in the viewer, rather than simply invoking the relevant code directly, seems (at least to me) like a somewhat roundabout way of doing things.
Not only does this lead to more code, both to write and maintain, but since many of the layers have common parameters (e.g. an `AnnotationStorage`-instance) there's also some duplication.

Hence this patch, which removes the `textLayerFactory` and instead uses a lookup-function in the `PDFPageView`-class to access the external viewer-properties as necessary.
Note that this should even be an improvement for the "pageviewer" component-example, since most layers will now work by default rather than require manual configuration.

---
[1] In practice we generally suggest using the "simpleviewer", or "singlepageviewer", since it does *most* things out-of-the-box and given that a lot of functionality really require *a viewer* and not just a single page in order to work.
2022-12-18 13:26:33 +01:00
Jonas Jenwald
4c78290028 [api-minor] Remove the textHighlighterFactory in the viewer
Please note that this functionality has never really mattered for the Firefox PDF Viewer, the GENERIC viewer, or even the "simpleviewer"/"singlepageviewer" component-examples. Hence, in practice this means that only the "pageviewer" component-example[1] have ever really utilized this.

Using factories to initialize various layers in the viewer, rather than simply invoking the relevant code directly, seems (at least to me) like a somewhat roundabout way of doing things.
Not only does this lead to more code, both to write and maintain, but since many of the layers have common parameters (e.g. an `AnnotationStorage`-instance) there's also some duplication.

Hence this patch, which removes the `textHighlighterFactory` and instead uses a lookup-function in the `PDFPageView`-class to access the external viewer-properties as necessary.
Note that this should even be an improvement for the "pageviewer" component-example, since most layers will now work by default rather than require manual configuration.

---
[1] In practice we generally suggest using the "simpleviewer", or "singlepageviewer", since it does *most* things out-of-the-box and given that a lot of functionality really require *a viewer* and not just a single page in order to work.
2022-12-18 13:26:10 +01:00
Jonas Jenwald
f1d1f6edfd [api-minor] Remove the structTreeLayerFactory in the viewer
Please note that this functionality has never really mattered for the Firefox PDF Viewer, the GENERIC viewer, or even the "simpleviewer"/"singlepageviewer" component-examples. Hence, in practice this means that only the "pageviewer" component-example[1] have ever really utilized this.

Using factories to initialize various layers in the viewer, rather than simply invoking the relevant code directly, seems (at least to me) like a somewhat roundabout way of doing things.
Not only does this lead to more code, both to write and maintain, but since many of the layers have common parameters (e.g. an `AnnotationStorage`-instance) there's also some duplication.

Hence this patch, which removes the `structTreeLayerFactory` and instead uses a lookup-function in the `PDFPageView`-class to access the external viewer-properties as necessary.
Note that this should even be an improvement for the "pageviewer" component-example, since most layers will now work by default rather than require manual configuration.

---
[1] In practice we generally suggest using the "simpleviewer", or "singlepageviewer", since it does *most* things out-of-the-box and given that a lot of functionality really require *a viewer* and not just a single page in order to work.
2022-12-18 13:26:08 +01:00
Jonas Jenwald
ca69da735e [api-minor] Remove the annotationLayerFactory in the viewer
Please note that this functionality has never really mattered for the Firefox PDF Viewer, the GENERIC viewer, or even the "simpleviewer"/"singlepageviewer" component-examples. Hence, in practice this means that only the "pageviewer" component-example[1] have ever really utilized this.

Using factories to initialize various layers in the viewer, rather than simply invoking the relevant code directly, seems (at least to me) like a somewhat roundabout way of doing things.
Not only does this lead to more code, both to write and maintain, but since many of the layers have common parameters (e.g. an `AnnotationStorage`-instance) there's also some duplication.

Hence this patch, which removes the `annotationLayerFactory` and instead uses a lookup-function in the `PDFPageView`-class to access the external viewer-properties as necessary.
Note that this should even be an improvement for the "pageviewer" component-example, since most layers will now work by default rather than require manual configuration.

---
[1] In practice we generally suggest using the "simpleviewer", or "singlepageviewer", since it does *most* things out-of-the-box and given that a lot of functionality really require *a viewer* and not just a single page in order to work.
2022-12-18 13:25:45 +01:00
Jonas Jenwald
7aedb8ed7a [api-minor] Remove the annotationEditorLayerFactory in the viewer
Please note that this functionality has never really mattered for the Firefox PDF Viewer, the GENERIC viewer, or even the "simpleviewer"/"singlepageviewer" component-examples. Hence, in practice this means that only the "pageviewer" component-example[1] have ever really utilized this.

Using factories to initialize various layers in the viewer, rather than simply invoking the relevant code directly, seems (at least to me) like a somewhat roundabout way of doing things.
Not only does this lead to more code, both to write and maintain, but since many of the layers have common parameters (e.g. an `AnnotationStorage`-instance) there's also some duplication.

Hence this patch, which removes the `annotationEditorLayerFactory` and instead uses a lookup-function in the `PDFPageView`-class to access the external viewer-properties as necessary.
Note that this should even be an improvement for the "pageviewer" component-example, since most layers will now work by default rather than require manual configuration.

---
[1] In practice we generally suggest using the "simpleviewer", or "singlepageviewer", since it does *most* things out-of-the-box and given that a lot of functionality really require *a viewer* and not just a single page in order to work.
2022-12-18 13:10:23 +01:00
Jonas Jenwald
8fa8310ec9 Decouple the annotationLayer and annotationEditorLayer in the viewer
Currently we'll only initialize and render the `annotationEditorLayer` once the regular `annotationLayer` has been rendered.
While it obviously makes sense to render the `annotationEditorLayer` *last*, the way that the code is currently written means that if a third-party user disables the `annotationLayer` then the editing-functionality indirectly becomes disabled as well.
Given that this seems like a somewhat arbitrary limitation, this patch simply decouples these two layers while still keeping the rendering order consistent.
2022-12-18 13:10:23 +01:00
Jonas Jenwald
ded02941f2 [api-minor] Move, most of, the isPureXfa-handling from PDFViewer and into PDFPageView
By moving this code the "pageviewer"-component example will become slightly more usable on its own, it may simplify a future addition of XFA Foreground document support, and finally also serves as preparation for the following patches.
2022-12-18 13:10:23 +01:00
Calixte Denizet
20037e9919 Don't remove the loading icon from the DOM when a page is resetted 2022-12-15 10:19:49 +01:00
Jonas Jenwald
8ac94d6519 Initialize the TextHighlighter-instance lazily in PDFPageView
Depending on e.g. the `textLayerMode` option it might not actually be necessary to always initialize this eagerly.
*Please note:* Unfortunately we cannot `shadow` a private field, hence why this is only made semi-"private".
2022-12-14 13:23:05 +01:00
Jonas Jenwald
5df341ed7e Make the various layer-render methods, in PDFPageView, properly private 2022-12-14 13:12:49 +01:00
calixteman
d9f13558d6
Merge pull request #15770 from calixteman/set_dims
Set the dimensions of the various layers at their creation
2022-12-11 17:32:29 +01:00
Calixte Denizet
a989b5a879 Set the dimensions of the various layers at their creation
- Use a unique helper function in display/display_utils.js;
- Move those dimensions in css' side.
2022-12-10 14:35:06 +01:00
Jonas Jenwald
bed1a1baa1 [AnnotationEditorLayerBuilder] Inline the destroy code in the cancel method
It doesn't seem necessary to have a *separate* `destroy` method given that the `cancel` method always invokes it unconditionally.
In the `PDFPageView.reset` method we currently attempt to call `destroy` directly, however that'll never actually happen since either:
 - We're keeping the annotationEditorLayer, in which case we're just hiding the layer and nothing more (and the relevant branch is never entered).
 - We're removing the annotationEditorLayer, in which case the `PDFPageView.cancelRendering` method has already cancelled *and* nulled it (and there's thus nothing left to `destroy` here).

*Please note:* Hopefully I'm not overlooking something obvious here, since both reading through the code *and* also adding `console.log(this.annotationEditorLayer);` [before this line](9d4aadbf7a/web/pdf_page_view.js (L438)) suggests that it's indeed unnecessary.
2022-12-10 10:16:20 +01:00
Jonas Jenwald
527258c316 Ensure that the various layers always get the correct initial size (issue 15795) 2022-12-09 14:30:12 +01:00
Jonas Jenwald
cdd39ec69e
Merge pull request #15778 from Snuffleupagus/keep-structTree
Don't re-create the `structTreeLayer` on zooming and rotation
2022-12-06 10:02:20 +01:00
Jonas Jenwald
fe8fded23b [api-minor] Combine the textContent/textContentStream parameters
Rather than handling these parameters separately, which is a left-over from back when streaming of textContent was originally added, we can simply pass either data directly to the `TextLayer` and let it handle things accordingly.

Also, improves a few JSDoc comments and `typedef`-imports.
2022-12-04 21:22:14 +01:00
Jonas Jenwald
da0e6bc590 Don't re-create the structTreeLayer on zooming and rotation
Compared to the recent PR 15722 for the `textLayer` this one should be a (comparatively) much a smaller win overall, since most documents don't have any structTree-data and the required parsing should be cheaper. However, it seems to me that it cannot hurt to improve this nonetheless.

Note that by moving the `structTreeLayer` initialization we remove the need for the "textlayerrendered" event listener, which thus simplifies the code a little bit.

Also, removes the API-caching of the structTree-data since this was basically done to offset the lack of caching in the viewer.
2022-12-04 10:18:58 +01:00
Jonas Jenwald
cd72818438 Remove unused StructTreeLayerBuilder constructor parameter
Also change the "private" methods into properly private ones.
2022-12-03 22:58:37 +01:00
Calixte Denizet
eed9bf71c5 Refactor the text layer code in order to avoid to recompute it on each draw
The idea is just to resuse what we got on the first draw.
Now, we only update the scaleX of the different spans and the other values
are dependant of --scale-factor.
Move some properties in the CSS in order to avoid any updates in JS.
2022-12-01 18:42:43 +01:00
Calixte Denizet
403aa5cfa0 [XFA] - Avoid an exception when zooming on a XFA 2022-09-19 21:31:52 +02:00
Jonas Jenwald
38ee28b1d3 Use more optional chaining in the code-base
This patch updates a bunch of older code, that makes conditional function calls, to use optional chaining rather than `if`-blocks.

These mostly mechanical changes reduce the size of the `gulp mozcentral` build by a little over 1 kB.
2022-09-05 15:41:53 +02:00
Jonas Jenwald
571ce13dd6 [api-major] Remove the enhanceTextSelection functionality (PR 15145 follow-up)
For the `gulp mozcentral` command, this reduces the size of the *built* `pdf.js` file by `> 10` kB.
2022-08-28 15:04:47 +02:00
Federico Bond
df912e9ab6 Update types in PDFPageViewOptions to properly declare optional parameters 2022-08-25 13:36:05 -03:00
Jonas Jenwald
5e126032ff Correctly mimic the proper event-format in AnnotationElement._setDefaultPropertiesFromJS (bug 1785218)
*This is a follow-up to PR 14869.*

In the old code we're accidentally "swallowing" part of the event-details, which explains why the annotationLayer didn't render.
One thing that made debugging a lot harder was the lack of error messages, from the viewer, and a few `PDFPageView`-methods were updated to improve this situation.
2022-08-17 15:44:27 +02:00
Calixte Denizet
f316300113 [Annotations] Add some aria-owns in the text layer to link to annotations (bug 1780375)
This patch doesn't structurally change the text layer: it just adds some aria-owns
attributes to some spans.
The aria-owns attribute expect to have an element id, hence it's why it adds back an
id on the element rendering an annotation, but this id is built in using crypto.randomUUID
to avoid any potential issues with the hash in the url.
The elements in the annotation layer are moved into the DOM in order to have them in the
same "order" as they visually are.
The overall goal is to help screen readers to present to the user the annotations as
they visually are and as they come in the text flow.
It is clearly not perfect, but it should improve readability for some people with visual
disabilities.
2022-08-12 14:35:26 +02:00
Jonas Jenwald
0c31320c12 [api-minor] Improve thumbnail handling in documents that contain interactive forms
To improve performance of the sidebar we use the page-canvases to generate the thumbnails whenever possible, since that avoids unnecessary re-rendering when the sidebar is open. This works generally well, however there's an old problem in PDF documents that contain interactive forms (when those are enabled): Note how the thumbnails become partially (or fully) blank, since those Annotations are not included in the OperatorList.[1]

We obviously want to keep using the `PDFThumbnailView.setImage`-method for most documents, however we need a way to skip it only for those pages that contain interactive forms.
As it turns out it's unfortunately not all that simple to tell, after the fact, from looking only at the OperatorList that some Annotations were skipped. While it might have been possible to try and infer that in the viewer, it'd not have been pretty considering that at the time when rendering finishes the annotationLayer has not yet been built.
The overall simplest solution that I could come up with, was instead to include a *summary* of the interactive form-state when doing the final "flushing" of the OperatorList and expose that information in the API.

---
[1] Some examples from our test-suite: `annotation-tx2.pdf` where the thumbnail is completely blank, and `bug1737260.pdf` where the thumbnail is missing the "buttons" found on the page.
2022-07-30 16:53:32 +02:00
Tim van der Meij
c7b71a3376
Merge pull request #15215 from Snuffleupagus/optional-content-initial
[api-minor] Improve how we disable `PDFThumbnailView.setImage` for documents with Optional Content
2022-07-30 12:04:23 +02:00
Jonas Jenwald
2e059727a9 [api-minor] Change the various factories, in the viewer, to accept Objects
Currently all of these factories take a bunch of (randomly ordered) parameters, which first of all doesn't look that nice in the `PDFPageView`-class when some parameters are optional.
Furthermore, it also makes deprecation/removal of any existing parameter a *potentially* breaking change.
Finally, using an Object will provide a small amount of "documentation" at the call-site which isn't really the case with a bunch of "regular" parameters.

Note that all of the `viewer component` examples still work as-is with this patch, which is why I don't believe that we necessarily have to deprecate in the usual fashion.
2022-07-29 16:31:04 +02:00
Jonas Jenwald
37dc0e7d6e Limit the PDFPageView._isStandalone handling to the GENERIC viewer
This code is completely unnecessary in e.g. the Firefox PDF Viewer.
2022-07-24 17:29:37 +02:00
Jonas Jenwald
3446f15bf3 Improve how we disable PDFThumbnailView.setImage for documents with Optional Content (PR 12170 follow-up)
Rather than always disable `PDFThumbnailView.setImage` as soon as user has changed the visibility of the Optional Content, we can utilize the new method added in the previous patch to improve thumbnail performance. Note in particular how, in the old code, even *resetting* of the Optional Content to its default state wouldn't enable `PDFThumbnailView.setImage` again.

While slightly unrelated, this patch also removes the `PDFThumbnailViewer._optionalContentConfigPromise`-property since it's completely unused.
2022-07-24 17:29:37 +02:00
Jonas Jenwald
f4e60ae986 Limit even more SVG-specific code to the GENERIC viewer
Given that the SVG back-end is not defined anywhere except in GENERIC builds, we can remove a bit more unnecessary code in e.g. the Firefox PDF Viewer.
2022-07-21 10:03:02 +02:00
Jonas Jenwald
290aeaf755 Limit more SVG-specific code to the GENERIC viewer
Given that the SVG back-end is not defined anywhere except in GENERIC builds, we can remove a little bit more unnecessary code in e.g. the Firefox PDF Viewer.
2022-07-16 11:24:09 +02:00
Jonas Jenwald
d7c9a0a793 Fix (obvious) typo in typedef-import of IPDFXfaLayerFactory (PR 14373 follow-up)
This only affects the viewer-components TypeScript definitions, and not any actual code.
2022-06-22 11:06:11 +02:00
Calixte Denizet
e2db9bacef Get rid of CSS transform on each annotation in the annotation layer
- each annotation has its coordinates/dimensions expressed in percentage,
  hence it's correctly positioned whatever the scale factor is;
- the font sizes are expressed in percentage too and the main font size
  is scaled thanks a css var (--scale-factor);
- the rotation is now applied on the div annotationLayer;
- this patch improve the rendering of some strings where the glyph spacing
  was not correct (it's a Firefox bug);
- it helps to simplify the code and it should slightly improve the update of
  page (on zoom or rotation).
2022-06-18 17:54:59 +02:00
Jonas Jenwald
df79b18a31 Only define the renderer-option in the GENERIC viewer
Given that the SVG back-end is not defined anywhere except in GENERIC builds, we can remove a little bit of unnecessary code in e.g. the Firefox PDF Viewer.
2022-06-16 16:11:47 +02:00
Jonas Jenwald
4d39898823 Enable the unicorn/prefer-modern-dom-apis ESLint plugin rule
This rule will help enforce slightly shorter code, and according to MDN both `Element.replaceWith()` and `Element.before()` are available in all browsers that we currently support.

Please find additional information here:
 - https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-modern-dom-apis.md
 - https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceWith
 - https://developer.mozilla.org/en-US/docs/Web/API/Element/before
2022-06-12 20:05:05 +02:00
Jonas Jenwald
8129815538 Enable the unicorn/prefer-dom-node-append ESLint plugin rule
This rule will help enforce slightly shorter code, especially since you can insert multiple elements at once, and according to MDN `Element.append()` is available in all browsers that we currently support.

Please find additional information here:
 - https://developer.mozilla.org/en-US/docs/Web/API/Element/append
 - https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-dom-node-append.md
2022-06-12 13:07:03 +02:00
Marco Castelluccio
8c9678158e Set "presentation" role for page canvases (issue 14437) 2022-06-07 18:56:30 +02:00
Calixte Denizet
be1aa11986 [edition] Add a FreeText editor (#14970)
- add a basic UI to edit some text in a pdf;
- an editor can be moved, suppressed, cut, copied, pasted, selected;
- add an undo/redo manager.
2022-06-04 18:20:11 +02:00
Jonas Jenwald
ca244d9bca Introduce a viewer constant for document.documentElement.style
Over time, as we've been introducing JavaScript code to modify CSS variables, we've been adding shorthand properties to various classes to reduce unnecessary repetition when accessing the document-styles.
Rather than repeating this in multiple places, it seems overall simpler to just introduce a constant and re-use that throughout the viewer instead.
2022-05-25 09:55:54 +02:00
Jonas Jenwald
af5789125f Try to remove the mozOpaque canvas-property (PR 6551 follow-up)
According to MDN, see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/mozOpaque, the `mozOpaque` canvas-property is not only non-standard (obviously) but it's also been deprecated.
Instead it's recommended to use `alpha = false` when getting the canvas-context, see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext#contextattributes, which all of our affected code is already doing.
2022-05-09 13:03:08 +02:00
Calixte Denizet
c8afd6ce8c [api-minor] Improve pdf reading in high contrast mode
- Use Canvas & CanvasText color when they don't have their default value
  as background and foreground colors.
- The colors used to draw (stroke/fill) in a pdf are replaced by the bg/fg
  ones according to their luminance.
2022-05-05 16:34:51 +02:00
Jonas Jenwald
e67cd7fae0 Replace the --viewport-scale-factor CSS variable
This CSS variable is only used together with the `annotationCanvasMap`-functionality in the canvas-code, however its value can be *trivially* computed by using the older `--zoom-factor` CSS variable together with the `PixelsPerInch`-structure.
Rather than having *two different* CSS variables that are this closely linked, it seems better to simplify things by using just one CSS variable instead.
2022-04-14 12:43:57 +02:00
Jonas Jenwald
f2c9a6bc24 Revert "[GENERIC viewer] Add fallback logic for the old PDFPageView.update method signature"
This reverts commit 8466204384, since it's now been included in three official releases.
2022-03-02 11:15:18 +01:00