The span font sizes are in page units and the transform is only set when the
span is rotated, skewed or scaled, which makes the layout cheaper.
The free highlight box comes from the editor layer: with the pre-standard CSS
zoom (Chrome < 128, Safari 18), the zoomed text layer client rect is scaled.
Given that page-editing will change the pageNumber mapping, we cannot keep the `pageNumberToDestHash` Map since that may cause the `currentOutlineItem` functionality to highlight the wrong outline-item.
Additionally, ensure that the `#getPageNumberToDestHash` method will always resolve its capability since previously e.g. closing of a document could leave promises pending indefinitely.
The pairs adjusted the same way are kerned in the generated font, so the
characters of a span stay at their PDF position. The highlights are painted
with a box shadow to avoid breaking the kerning.
For PDFs with more than `PagesCountLimit.FORCE_LAZY_PAGE_INIT` pages, currently set at 5000, pages are initialized lazily to improve performance.
This causes finding/highlighting of the current outline-item to not work correctly, hence we need to fallback to the `getPageIndex` API-method.
**Steps to reproduce:**
- Open a PDF with more than 5000 pages, e.g. https://www.cs.princeton.edu/courses/archive/fall19/cos217/reading/ArmArchitectureReferenceManual.pdf#pagemode=outline
- Goto the last page.
- Click on the "Find Current Outline Item" button.
**Expected behaviour:**
The *last* outline-item is scrolled into view and highlighted.
**Actual behaviour:**
The *first* outline-item is highlighted, when testing in `master`.
With worker-rendering enabled this option is not needed, hence move it to a shadowed getter in `BasePDFPageView` and remove it from various viewer-components respectively the `AppOptions`.
Annotation text and the highlight intersector now use the glyph runs too, so
the old item builder in the evaluator is gone.
Also make debugTextLayerFonts a getDocument option, and copy-all split the
text like the text layer.
Group glyphs with the same baseline, font and transform into runs. Infer
word spaces from the positions and tracking within each run, and split
runs into spans at wide gaps.
Compute span advances in the worker from the generated font metrics so
the text layer can lay out simple spans without measuring them. Find
uses the same extracted items as the text layer.
Closing/switching the document in the viewer will cause multiple
tasks to fail. Other code paths already have
`if (pdfDocument !== this.pdfDocument) return;` checks to know if the
document has been changed after an async step, but it was missing in
some places leading to errors being logged in the console.
This commit adds the check in those places.
The worker builds, with `includeTextLayerFonts`, a font per PDF font of the
page from the advances of the drawn glyphs, so that the characters of a span
are where the canvas draws them. The viewer uses them when the
`newTextLayerMode` option is 1, or 2 to draw the glyph boxes in red.
It avoids to do it while the first page is rendering on the main thread.
Thanks to this patch, preloading happens while the module is loading in the worker.
On Android, a signature that has already been placed (its edit toolbar is
closed) isn't presented as an actionable element. Android supports fewer
roles than desktop, so in this state it makes sense to expose the signature
as a "button", which makes it clear that it can be reactivated. This patch
sets that role on Android while the signature is unselected.
At this point in time `OffscreenCanvas` should be available in all supported browsers, see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas#browser_compatibility, which means that in practice it'll only be disabled in Node.js (and during some browser/unit tests).
Hence it seems unnecessary to list this in `AppOptions` in the viewer, since `isOffscreenCanvasSupported = false` will generally lead to worse performance by e.g. forcibly disabling worker-rendering.
At fractional device pixel ratios, rounding page dimensions can clip the
right and bottom edges. Fit the full viewport into the rounded canvas
to keep it aligned with the text and annotation layers. Preserve the
canvas sizing scale across redraws.
A script can trigger some events on its own, e.g. a timer, or focus an
element under the pointer or the keyboard, hence the viewer has to decide
by itself whether an action comes from the user.
The events caused by a trusted user gesture (Action, Mouse Down and Mouse
Up) and the Open event, which may only print, get a one-time token. The
host keeps it out of the sandbox and attaches it to the commands sent
while the event is handled. The annotation actions which print, save,
download or follow a link are checked the same way, and only one of them
is honored per gesture. A gesture is ignored when the keyboard activates
an element focused by a script, or when it follows a layout change made
by a script too closely.
Nothing coming from the sandbox can be trusted: it grants itself a user
activation for any event, and a script can trigger some events on its own
(e.g. a timer calling setFocus).
Hence the host only shows a dialog, once, while handling an event for
which the viewer allows it: the ones caused by the user, Open, and the
print/save ones. A focus change requested by a script no longer counts
as a user activation.
Patch adds `enableAndroidEditing` to better control PDF signatures on Android. This preference turns off the original PDF.js toolbar and gates the annotation editor, which is disabled unless this preference is on.
Currently there's a fair number of `catch` handlers that just suppress "uncaught exceptions", and rather than creating potentially a lot of inline functions we can re-use just a single helper instead.
Note that such a function already existed, in the `src/shared/message_handler.js` file, hence that one is simply moved and re-named.
The new `createPromiseCapability` helper is a simple wrapper around the `Promise.withResolvers()` function, to help avoid any "uncaught exception: Object"-messages in the console for unhandled promises.
This should hopefully remove the need for manual fixes all over the code-base, since we've seen a fair number of (suspected AI) reports about unhandled promises recently.
The CSS only fetches an icon once its element is rendered, so the toolbar is
first painted without icons and the menus load theirs when first opened.
Preload the toolbar icons from viewer.html (without crossorigin, since CSS
images from resource:// are fetched without CORS), and warm the image cache
up with the icons of the hidden UI once the first page is rendered.
Zooming with ctrl+wheel is ignored while scrolling in order to avoid
zooming when the ctrl key is pressed during momentum scrolling (issue #7022).
But a pinch-to-zoom gesture on a trackpad doesn't involve the physical ctrl key,
so it must zoom even if the document is still scrolling.
This option is removed now, since:
- It was originally added for the Boot to Gecko (B2G) project, later renamed to Firefox OS, which was discontinued in 2016.
This option was needed then, since B2G was intended to also run on low-end hardware.
- Outside of one example, which is a left-over from the B2G viewer, this option has been unused in the PDF.js project itself for a decade.
- This option has never been covered by *any* tests.
- The result of setting this option to a non-default value is that some images will not renderer, with only a console warning, which could easily make PDF.js appear "broken" to an end-user.
- Image decoding performance, in particular for large and/or complex images, has been significantly improved compared to a decade ago.
- If such an option was requested today, it seems unlikely that we'd actually consider adding it.
This option is removed now, since:
- It was originally added for the Boot to Gecko (B2G) project, later renamed to Firefox OS, which was discontinued in 2016.
The option was needed then, since B2G was intended to also run on low-end hardware.
- Outside of one example, which is a left-over from the B2G viewer, CSS-only zooming has not been used in the PDF.js project itself for a decade.
(However, there are a couple of integration-tests for it though.)
- CSS-only zooming often renders the document "useless" at higher zoom-levels, because of the blurriness.
- Given the current implementation of CSS-only zooming it can break if the initial scale is very large, or if a page has huge dimensions, since the rendering is done without enforcing any limits.
- Removing the option allows us to simplify the code a little bit.
*Note:* A much better user-experience can be had nowadays by setting the `maxCanvasPixels`, `maxCanvasDim`, and/or `capCanvasAreaFactor` options to fairly low values, and then via the `enableDetailCanvas` option (which is `true` by default) re-render only the currently visible viewport at high-resolution.
This option is removed now, since:
- It was never used anywhere within the PDF.js project itself.
- It has never been covered by any tests, and may already be partially broken.
- It was added in PR 5844, over a decade ago back when the viewer was considerably simpler overall and e.g. the scroll/spread modes didn't exist yet.
- Removing the option allows us to simplify both the JavaScript and CSS implementation a little bit.
- If such an option was requested today, it seems unlikely that we'd actually consider adding it.
(Also, the initial feature request came from a project that doesn't appear to have been updated since 2019.)
In Firefox the viewer uses the browser's integrated find bar, except when it's
in a frame. The button was only hidden once the viewer was initialized, so it
was visible for a short while when loading a PDF in a tab.
Now it's hidden in the markup and only shown when integrated find isn't
supported.
Depending on the `devicePixelRatio` the pages can move visibly in the viewer when they start rendering, which is a regression from PR 18698.
This regression is most easily noticeable in Wrapped scrolling-mode and at lower zoom-levels, which probably explains why it's not been reported until now.
To address this we just need to ensure that the CSS variables that control the rounding behaviour, i.e. `--scale-round-x` and `--scale-round-y`, are always set rather than waiting until the page is rendered.
More specifically we move the `approximateFraction` calculation into the `#computeScale` method, and ensure that it's called from the `PDFPageView` constructor.
Since most PDFs have equal pages-sizes, and especially in environments with non-integer `devicePixelRatio` values, this could potentially lead to a lot of "duplicate" `approximateFraction` calls during viewer initialization which we try to avoid by introducing a cache.