7628 Commits

Author SHA1 Message Date
Jonas Jenwald
68366e31e4 Move the MathClamp helper function to its own file
This allows using it in the `src/scripting_api/` folder, without increasing the size of the scripting-bundle by also importing a bunch of unused code.
2026-04-02 11:22:28 +02:00
Jonas Jenwald
1bd4c4fbde
Merge pull request #21026 from Snuffleupagus/more-MathClamp
Use the `MathClamp` helper function more
2026-04-02 11:21:46 +02:00
Jonas Jenwald
c012ff6e10 Use the MathClamp helper function more
The idea with this helper function is that once https://github.com/tc39/proposal-math-clamp/ becomes stable, all its call-sites should then be replaced by the native functionality.
2026-04-02 10:10:39 +02:00
Calixte Denizet
f373923170 Encrypt pdf data when merging the same pdf (bug 2028369) 2026-04-01 19:01:11 +02:00
calixteman
399fce6471
Merge pull request #21010 from calixteman/ps_js
Add an interpreter for optimized ps code
2026-03-31 22:21:00 +02:00
Calixte Denizet
9f3de1edf6
Add an interpreter for optimized ps code
It'll be used as a fallback when wasm is disabled.
And add in the debugger a view for the generated js code and one for the ps code.
2026-03-31 21:00:22 +02:00
Calixte Denizet
3727b7095a Add support for function-based shadings (bug 1254066)
It fixes #5046.
We just generate a mesh for the pattern rectangle where the color of each vertex is computed from the function.
Since the mesh is generated in the worker we don't really take into account the current transform when it's drawn.
That being said, there are maybe some possible improvements in using directly the gpu for the shading creation
which could then take into account the current transform, but it could only work with ps function we can convert
ino wgsl language and simple enough color spaces (gray and rgb).
2026-03-31 20:46:01 +02:00
Tim van der Meij
58b807d8e8
Merge pull request #21008 from calixteman/ast_cse
Avoid expressions duplication in the ps AST and use a local instead when compiling to WASM
2026-03-31 20:21:59 +02:00
Tim van der Meij
b0c0680bea
Merge pull request #18815 from calixteman/dont_always_use_pattern
Don't use an intermediate canvas when rendering a tiling pattern bigger than the rectangle to fill
2026-03-31 20:18:08 +02:00
Tim van der Meij
48228e2756
Merge pull request #21013 from calixteman/bug2026956
Add attachments when merging/reorganizing a pdf (bug 2026956)
2026-03-31 20:17:54 +02:00
Calixte Denizet
5b8c04f383 Add attachments when merging/reorganizing a pdf (bug 2026956) 2026-03-31 14:48:06 +02:00
Calixte Denizet
4c019e7712
Don't use an intermediate canvas when rendering a tiling pattern bigger than the rectangle to fill 2026-03-30 18:38:56 +02:00
Calixte Denizet
63cf35b47f Avoid expressions duplication in the ps AST and use a local instead when compiling to WASM 2026-03-30 16:30:33 +02:00
Jonas Jenwald
bfffb6c0f0 Import fs/promises directly in a few spots in the unit-tests
Also, use the existing PDF.js helper function to fetch text-data when running the "bidi" tests in browsers.
2026-03-30 14:34:53 +02:00
Jonas Jenwald
90fe6c70ff [Node.js] Remove the node-readable-to-web-readable-stream polyfill
While `Readable.toWeb` wasn't marked as stable until more recently, the functionality itself has existed since Node.js version `17.0.0`; note https://nodejs.org/api/stream.html#streamreadabletowebstreamreadable-options

Hence the polyfill shouldn't actually be necessary, which is confirmed by the unit-tests passing in Node.js version `20` in GitHub Actions.
2026-03-30 13:45:13 +02:00
calixteman
952952c905
[api-minor] Rewrite the ps lexer & parser and add a small Wasm compiler
The main goal is to remove the eval-based interpreter.
In order to have some good performances, the new parser performs some optimizations
on the AST (similar to the ones in the previous implementation),
and the Wasm compiler generates code for the optimized AST.
For now, in case of errors or unsupported features, the Wasm compiler returns null
and the old interpreter is used as a fallback.
Few things are still missing:
 - a wasm-based interpreter using a stack (in case the ps code isn't stack-free);
 - a better js implementation in case of disabled wasm.

 but they will be added in follow-up patches.
2026-03-30 09:22:33 +02:00
Tim van der Meij
ada3438039
Merge pull request #21001 from Snuffleupagus/getDestFromStructElement-unit-test
Add a unit-test for the `Catalog.#getDestFromStructElement` method
2026-03-29 16:08:21 +02:00
Tim van der Meij
9026329d3d
Merge pull request #21003 from Snuffleupagus/applyOpacity-map
Simplify the `applyOpacity` helper function
2026-03-29 16:06:28 +02:00
Tim van der Meij
80d0d7349c
Merge pull request #20997 from Snuffleupagus/StatTimer-Map
Re-factor the `StatTimer` class a little bit
2026-03-29 16:05:15 +02:00
Tim van der Meij
37f5902c3c
Merge pull request #20996 from Snuffleupagus/more-logical-assignment
Use more logical assignment in the code-base
2026-03-29 16:03:40 +02:00
Jonas Jenwald
498daadf3c Simplify the applyOpacity helper function
This function only has a single call-site (if we ignore the unit-tests), where the colors are split into separate parameters.
Given that all the color components are modified in the exact same way, it seems easier (and shorter) to pass the colors as-is to `applyOpacity` and have it use `Array.prototype.map()` instead.
2026-03-29 14:52:06 +02:00
Jonas Jenwald
d1f15fe352 Add a unit-test for the Catalog.#getDestFromStructElement method
This code already has an integration-test, however also having a unit-test shouldn't hurt since those are often easier to run and debug (and it nicely complements the existing `outline` unit-tests).

The patch also makes the following smaller changes to the method itself:
 - Avoid creating and parsing an empty Array, when doing the `pageRef` search.
 - Use `XRef.prototype.fetch` directly, when walking the parent chain, since the check just above ensures that the value is a Reference.
 - Use the `lookupRect` helper when parsing the /BBox entry.
2026-03-29 14:01:43 +02:00
Jonas Jenwald
8121bc0dd2 Remove a tiny bit of unnecessary "rgba" parsing in the getRGB function
This obviously won't matter in practice, however it seems more "correct" to only extract the necessary number of color components rather than slicing off excess ones at the end.
2026-03-29 12:13:59 +02:00
Jonas Jenwald
256be7f3a6 Remove manual loops in the StatTimer.prototype.toString method
We can use Array methods instead, which is a tiny bit shorter.
2026-03-27 15:35:25 +01:00
Jonas Jenwald
522f5f85b9 Re-factor the StatTimer class to track started times in a private Map 2026-03-27 15:35:23 +01:00
Jonas Jenwald
0060eeb726 Use more logical assignment in the code-base 2026-03-27 12:18:04 +01:00
Calixte Denizet
013a209e77
Use non-breakable spaces in options for the choice widget (bug 2026037)
Usual white spaces are collapsed.
2026-03-26 23:52:46 +01:00
calixteman
018c1539dd
Merge pull request #20957 from calixteman/issue20956
Break text chunks only if the base font is different
2026-03-26 22:24:23 +01:00
calixteman
27e619f00d
Merge pull request #20992 from calixteman/organize_avoid_exception
Avoid exception when cloning a page with some modified annotations
2026-03-26 22:15:06 +01:00
Calixte Denizet
2e3d79e616
Break text chunks only if the base font is different
It fixes #20956.
2026-03-26 21:39:32 +01:00
calixteman
484518614d
Merge pull request #20976 from calixteman/bidi_tests
Add the bidi tests coming from BidiTest.txt and BidiCharacterTest.txt
2026-03-26 21:36:18 +01:00
Calixte Denizet
2b20e012bb
Avoid exception when cloning a page with some modified annotations
Some tests will come later.
2026-03-26 20:53:09 +01:00
Tim van der Meij
bf7d25b35c
Merge pull request #20975 from Snuffleupagus/AnnotationStorage-rm-typeof-function
Use optional chaining rather than `typeof` checks when invoking the `AnnotationStorage` callbacks
2026-03-26 20:35:24 +01:00
Tim van der Meij
f52a50ba09
Merge pull request #20986 from Snuffleupagus/FontInfo-readString-simplify
Reduce allocations in the `FontInfo.prototype.#readString` method (PR 20197 follow-up)
2026-03-26 20:34:03 +01:00
Tim van der Meij
d3fd72e7e8
Merge pull request #20982 from calixteman/debugger_images_preview
Display mask in the tree view in the debugger
2026-03-26 20:29:42 +01:00
Jonas Jenwald
42566f40fb Reduce allocations in the FontInfo.prototype.#readString method (PR 20197 follow-up)
Looking at the very similar `CssFontInfo.prototype.#readString` and `SystemFontInfo.prototype.#readString` methods they decode using the data as-is, but the `FontInfo.prototype.#readString` method for some reason copies the data into a new `Uint8Array` first; fixes yet another bug/inefficiency in PR 20197.
2026-03-26 16:49:41 +01:00
calixteman
601d961fc3
Merge pull request #20900 from wooorm/wooorm/image-mask-perf
Refactor to improve performance around image masks (bug 1941562)
2026-03-26 16:10:30 +01:00
Titus Wormer
e914326346
Refactor to improve performance around image masks (bug 1941562)
* add support for directly writing masks into `rgbaBuf` if their size matches
* add support for writing into `rgbaBuf` to `resizeImageMask` to avoid extra
  allocs/copies
* respect `actualHeight` to avoid unnecessary work on non-emitted rows
* mark more operations as `internal`

This changes the path for what I believe is the common case for masks:
a mask to add transparency to the accompanying opaque image, both being
equal in size.
The other paths are not meaninfully unchanged.
That increases my confidence as these new paths can be easily tested
with a PNG with transparency.
2026-03-26 14:39:50 +01:00
Calixte Denizet
867af5c1e5
Display mask in the tree view in the debugger 2026-03-26 14:35:31 +01:00
Jonas Jenwald
39dd02cf32 Remove unused preInit parameter from the CanvasExtraState constructor (PR 19043 follow-up)
This parameter was added in PR 19043, however it never actually appears to have been used.
2026-03-26 11:07:47 +01:00
Calixte Denizet
42c229c267
Add the bidi tests coming from BidiTest.txt and BidiCharacterTest.txt
Some tests were failing and has been fixed:
 - "Hello" + Alef + "(" + Bet: the "(" (neutral) was not considered as a part of the group Alef(Bet and the group wasn't reverted;
 - some intermediate neutrals were considered as strong.
2026-03-25 15:18:50 +01:00
Jonas Jenwald
fc3407cabd Use optional chaining rather than typeof checks when invoking the AnnotationStorage callbacks
This is a little bit shorter, and it should be fine considering that in the API there are no `typeof` checks when invoking user-provided callbacks (see e.g. `onPassword` and `onProgress`).
2026-03-25 13:57:00 +01:00
Jonas Jenwald
777251da85
Merge pull request #20968 from Snuffleupagus/getNetworkStream
Move the `NetworkStream` choice from `src/display/api.js` and into a separate file
2026-03-25 08:33:08 +01:00
Calixte Denizet
c0e3977321 Remove the canvases cache
The cache has been added in #3312 in 2013 and a lot of things changed since.
Having too many cached accelerated canvases can lead to have to move their data from the GPU to the RAM
which is costly.
So this patch:
 - removes all the cached canvases;
 - destroys the useless canvases in order to free their associated memory asap;
 - slightly rewrite canvas.js::_scaleImage to avoid too much canvas creation.
2026-03-24 23:39:44 +01:00
Jonas Jenwald
a0102abe76 Move the NetworkStream choice from src/display/api.js and into a separate file
This code already isn't used (or even bundled) in the Firefox PDF Viewer, and it also slightly reduces the number of import maps that need to be maintained.
2026-03-24 17:08:04 +01:00
Jonas Jenwald
ca8f8074a2 Remove the deprecated PDFWorker.fromPort method (PR 19943 follow-up)
This has been deprecated in ten releases, so let's just remove it now.
2026-03-23 17:24:54 +01:00
Tim van der Meij
1756b48417
Merge pull request #20949 from Snuffleupagus/BinaryDataFactory-2
[api-minor] Replace the `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory` API options with a single factory/option
2026-03-22 21:20:49 +01:00
Tim van der Meij
0a6894d28b
Merge pull request #20948 from calixteman/issue20914
Interpret empty annotation border as [0, 0, 0]
2026-03-22 19:08:19 +01:00
Jonas Jenwald
3a372fde94 [api-minor] Replace the CMapReaderFactory, StandardFontDataFactory, and WasmFactory API options with a single factory/option
Currently we have no less than three different, but very similar, factories for reading built-in CMap files, standard font files, and wasm files on the main-thread.[1]
These factories were added at different points in time, since I cannot imagine that we'd add essentially three copies of the same code otherwise.

Nowadays these factories are often not even used[2], since worker-thread fetching is used whenever possible to improve performance. In particular, they will *only* be used when either:
 - The PDF.js library runs in Node.js environments.
 - The user manually sets `useWorkerFetch = false` when calling `getDocument`.
 - The user provides custom `CMapReaderFactory`, `StandardFontDataFactory`, and/or `WasmFactory` instances when calling `getDocument`.

By replacing these factories with *a single* new `BinaryDataFactory` factory/option the number of `getDocument` options are thus reduced, which cannot hurt.
This also reduces the total bundle-size of the Firefox PDF Viewer a little bit, and it slightly reduces the number of import maps that need to be maintained.

*Please note:* For users that provide custom `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory` instances when calling `getDocument` this will be a breaking change, however it's unlikely that (many) such users exist.
(The *internal* format data-format of `CMapReaderFactory` was changed in PR 18951, and there hasn't been a single question/complaint about it in well over a year.)

---

[1] Any new functionality could easily lead to more such factories being added in the future, which wouldn't be great.

[2] Note that the Firefox PDF Viewer no longer use these factories, since it "forcibly" sets `useWorkerFetch = true` during building.
2026-03-22 15:49:06 +01:00
calixteman
de48af76d4
Interpret empty annotation border as [0, 0, 0]
It fixes #20914.
2026-03-22 15:49:04 +01:00