5351 Commits
Author SHA1 Message Date
Calixte Denizet 34246a0b6f Zoom the text layer with CSS and only set a transform on the spans which need one
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.
2026-11-30 13:45:33 +01:00
Jonas Jenwald 198220539a Reset the pageNumberToDestHash Map, in the outline, when the pages are edited
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.
2026-11-29 19:31:02 +01:00
Jonas Jenwald 98d2c6674e Merge pull request #22621 from Snuffleupagus/currentOutlineItem-lazy-page-init
Fix the `currentOutlineItem` functionality when pages are initialized lazily
2026-11-29 19:30:27 +01:00
Calixte Denizet 2477bb1aed Add the kerning of the PDF glyphs to the text layer fonts
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.
2026-11-29 15:04:20 +01:00
Jonas Jenwald f03c33a088 Fix the currentOutlineItem functionality when pages are initialized lazily
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`.
2026-11-29 14:55:09 +01:00
Jonas Jenwald 9548d30dd4 Merge pull request #22615 from Snuffleupagus/inline-loadPDFBug
Inline the `loadPDFBug` helper function (PR 22607 follow-up)
2026-11-27 22:13:32 +01:00
Jonas Jenwald 8f53fe1989 [api-minor] Remove the minDurationToUpdateCanvas viewer option
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`.
2026-11-27 14:17:32 +01:00
Jonas Jenwald fad8fcc3ad Inline the debuggerSrc definition in the viewer
It really doesn't seem necessary for this to be an `AppOptions`, when it's never loaded by default and only used for development/debugging purposes.
2026-11-27 14:05:29 +01:00
Jonas Jenwald 0d27521427 Inline the loadPDFBug helper function (PR 22607 follow-up)
After PR 22607 this function only has a single call-site, hence it can just be inlined instead.
2026-11-27 14:00:20 +01:00
Calixte Denizet 86d659b564 [api-major] Replace the getTextContent options with enableMarkedContent and enableNormalization
Both are true by default, which is what the viewer mostly uses.
2026-11-27 13:39:55 +01:00
Calixte Denizet 487aed7102 Use #textLayer=visible to draw the glyph boxes of the text layer fonts
The viewer gets debugTextLayerFonts from the page loading params, and the
"shadow" and "hover" debugging modes are removed.
2026-11-27 13:39:55 +01:00
Calixte Denizet 5a21695c4a Remove the old text content extraction
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.
2026-11-27 13:39:55 +01:00
Calixte Denizet 2e39fea37e Build the text layer items from the runs of glyphs drawn on the same baseline
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.
2026-11-27 13:39:55 +01:00
Calixte Denizet 5b1be07686 [api-major] Always include generated fonts in page text content 2026-11-27 13:39:55 +01:00
Nicolò Ribaudo ce052f009a Reject pagesCapability when the document is closed
This is so that those promises don't remain pending forever, and their
callers can clean up associated resources.
2026-11-24 15:57:43 +01:00
Nicolò Ribaudo b982ac42e4 Suppress more errors due to closed documents
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.
2026-11-24 15:57:14 +01:00
Calixte Denizet 376f8b3b93 Use fonts with empty glyphs and the PDF advances in the text layer
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.
2026-11-19 16:34:20 +01:00
Calixte Denizet 738f9191ef Schedule UI icon preloading during viewer initialization
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.
2026-11-19 09:32:51 +01:00
calixteman fc0822a07e Merge pull request #22554 from ohall-m/bug-2084998-signature-description
Bug 2084998 - Improve the presentation of signature elements that no longer have an edit toolbar
2026-11-18 20:48:51 +01:00
Olivia Hall f6f0b8dd98 Bug 2084998 - Improve the presentation of signature elements that no longer have an edit toolbar
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.
2026-11-18 10:46:50 -05:00
Jonas Jenwald 8ee5560ab5 Remove isOffscreenCanvasSupported from the AppOptions
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.
2026-11-18 16:39:52 +01:00
Calixte Denizet 476d8b48e4 Preserve page edges when rounding page dimensions (bug 2094402)
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.
2026-11-18 13:57:09 +01:00
calixteman 0322a85c19 Support flattening in Firefox save events (bug 2075337)
Forward flatten and intent through saving and extraction, including
unedited PDFs. Run WillPrint/DidPrint for print flattening.
2026-11-15 16:41:03 +01:00
Calixte Denizet cf8aa3c835 Require a user gesture to print, save, download or follow a link
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.
2026-11-15 15:05:22 +01:00
Calixte Denizet 290290c247 Only show a dialog from the sandbox while handling an event allowed by the viewer
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.
2026-11-15 15:01:19 +01:00
Calixte Denizet ad1c3b7f69 Coalesce repeated SaveAs requests into a single download
A script can request many downloads in a row: start a new one only once
the previous one is done.
2026-11-15 15:01:12 +01:00
Olivia Hall 0e733e7b94 Bug 2061833 - Begin using enableAndroidEditing Preference
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.
2026-11-12 18:15:30 -05:00
Jonas Jenwald 30c5523f3c Add a no-op helper function for catch handlers that ignore errors
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.
2026-11-12 12:33:59 +01:00
Jonas Jenwald 8ecaef3b3e Add a helper to avoid "uncaught exceptions" from Promise.withResolvers()
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.
2026-11-11 22:39:31 +01:00
calixteman b600aecd18 Merge pull request #22485 from calixteman/xfa_b_i_measure
Fix the measurement of b and i elements in XFA rich text
2026-11-09 22:02:28 +01:00
Tim van der Meij 50e7be91f1 Merge pull request #22457 from calixteman/preload-viewer-icons
Start loading the viewer icons earlier in Firefox
2026-11-09 20:18:38 +01:00
Jonas Jenwald c34a271db2 Merge pull request #22475 from Snuffleupagus/enableSplitMerge-true
Enable the split/merge feature by default
2026-11-09 16:01:30 +01:00
calixteman ab0433d107 Merge pull request #22472 from calixteman/bug1915803
Allow pinch-to-zoom while some momentum scrolling is in progress (bug 1915803)
2026-11-09 15:00:10 +01:00
Calixte Denizet 5a7a44db7a Fix the measurement of b and i elements in XFA rich text
`TextMeasure` has no `pushFont` method, so laying out any rich text
containing a <b> or an <i> threw and no page was rendered.

It fixes #22023.
2026-11-09 14:39:43 +01:00
Calixte Denizet ce31961cb1 Start loading the viewer icons earlier in Firefox
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.
2026-11-08 22:23:25 +01:00
calixteman 9f439dd1a4 Allow pinch-to-zoom while some momentum scrolling is in progress (bug 1915803)
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.
2026-11-08 22:00:28 +01:00
Jonas Jenwald 8a18b3d15b Limit all defaultRenderingQueue logic in PDFViewer to GENERIC builds
Avoid bundling dead code in the Firefox PDF Viewer, since it already can't work without a `renderingQueue` option provided.
2026-11-08 15:52:07 +01:00
Tim van der Meij c9b0aba31b Merge pull request #22471 from Snuffleupagus/rm-api-maxImageSize
[api-major] Remove the `maxImageSize` option from `getDocument`
2026-11-08 13:49:31 +01:00
Jonas Jenwald 0600b01dfe Enable the split/merge feature by default
This feature has been enabled in Firefox since [bug 2018919](https://bugzilla.mozilla.org/show_bug.cgi?id=2018919) and [bug 2030179](https://bugzilla.mozilla.org/show_bug.cgi?id=2030179), hence it seems that we can just enable it by default now.
2026-11-08 11:33:25 +01:00
Jonas Jenwald 945dbf6785 Combine the enableMerge and enableSplitMerge options/preferences (issue 21679)
This removes the separate `enableMerge` option/preference, since the `enableSplitMerge` name already suggests that it controls both aspects.
2026-11-07 17:16:39 +01:00
Jonas Jenwald a30646be03 [api-major] Remove the maxImageSize option from getDocument
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.
2026-11-07 17:02:45 +01:00
Jonas Jenwald a2f67dad8a [api-major] Remove the maxCanvasPixels = 0 option, i.e. forced CSS-only zooming, from PDFViewer
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.
2026-11-07 13:55:43 +01:00
Jonas Jenwald 4b7618bf8b [api-major] Remove the removePageBorders option from PDFViewer
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.)
2026-11-06 11:07:31 +01:00
Tim van der Meij 2b183c9754 Merge pull request #22453 from Snuffleupagus/fix-page-move-on-draw
Prevent the pages from moving slightly when drawing starts (PR 18698 follow-up)
2026-11-05 21:18:30 +01:00
Tim van der Meij 245a6e7a7b Merge pull request #22427 from nicolo-ribaudo/two-viewers-race
Fix global selection event handlers abort for multiple viewers
2026-11-05 21:08:30 +01:00
Tim van der Meij 106a7df155 Merge pull request #22456 from calixteman/mozcentral-find-button-flash
Hide the find button by default in Firefox
2026-11-05 21:03:21 +01:00
Tim van der Meij 086ad86031 Merge pull request #22442 from calixteman/save-document-options
[api-minor] Pass the saveDocument parameters in an object
2026-11-05 21:00:07 +01:00
Calixte Denizet b53065b88b Hide the find button by default in Firefox
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.
2026-11-05 20:23:32 +01:00
Jonas Jenwald 9016170fa1 Prevent the pages from moving slightly when drawing starts (PR 18698 follow-up)
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.
2026-11-05 19:56:01 +01:00
calixteman 09715bbcea Keep page placeholders visible after early rendering failures
Discard empty canvases on rendering errors so the page background stays
visible. Preserve partially rendered canvases.

Fixes #22437.
2026-11-04 21:05:23 +01:00