1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00
Commit graph

20693 commits

Author SHA1 Message Date
Jonas Jenwald
1d1e6e7b92 Simplify Object.fromEntries usage a tiny bit in the web/generic_signature_storage.js file
As can be seen [at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries#parameters) the `Object.fromEntries` method accepts a `Map` directly, hence using the `Map.prototype.entries` method is superfluous here.
2025-04-09 12:06:40 +02:00
Jonas Jenwald
22657e2b6e
Merge pull request #19778 from Snuffleupagus/replace-getAll
[api-minor] Replace various `getAll` methods with iterators
2025-04-08 17:26:24 +02:00
Jonas Jenwald
12c7c7b0af
Merge pull request #19773 from Snuffleupagus/inline-PDFImage-createRawMask
Inline `PDFImage.createRawMask` in the `PDFImage.createMask` method
2025-04-08 17:19:09 +02:00
Jonas Jenwald
19486952c2
Merge pull request #19781 from Snuffleupagus/mv-IDENTITY_MATRIX
Move the `IDENTITY_MATRIX` constant into `src/core/core_utils.js` (PR 19772 follow-up)
2025-04-08 17:16:31 +02:00
Jonas Jenwald
0e50125a6c
Merge pull request #19788 from Snuffleupagus/no-array-reduce
Enable the `no-array-reduce` ESLint plugin rule
2025-04-08 17:09:44 +02:00
Jonas Jenwald
38d01250f4 Enable the no-array-reduce ESLint plugin rule
Please see https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-array-reduce.md

Note that this still allows "simple" usage of `Array.prototype.reduce`, however most of those cases will be possible to replace with `Math.sumPrecise` once that becomes generally available (currently not supported in Node.js or QuickJS).
2025-04-08 12:21:20 +02:00
Jonas Jenwald
dc3e24a76a Inline PDFImage.createRawMask in the PDFImage.createMask method
After the introduction of `OffscreenCanvas` support we now have *two separate* mask-methods in the `PDFImage` class, and the reason that they were not combined is likely that we need the "raw" bytes when parsing Type3-glyph image masks.
However, that case is easy to support simply by disabling `OffscreenCanvas` usage when parsing Type3-glyphs and that way we're able to reduce some code duplication.

Another slightly strange property of the `PDFImage.createMask` method is that it needs various image-dictionary parameters *manually* provided, which is probably because this is very old code.
That feels slightly unwieldy, and we instead change the method to pass in the image-stream directly and do the necessary data-lookup internally.

A side-effect of this re-factoring is that we now support using the custom `isSingleOpaquePixel` operator in Type3-glyphs, which shouldn't hurt even though it seems extremely unlikely for that to ever happen in Type3-glyphs.
2025-04-08 12:01:50 +02:00
Jonas Jenwald
85e6f3c92b
Merge pull request #19785 from nicolo-ribaudo/chrome-selection-fix
[chrome] Fix text selection with `.markedContent`
2025-04-08 11:57:32 +02:00
Jonas Jenwald
115ea6afa3
Merge pull request #19786 from nicolo-ribaudo/linting-export
Lint against `export [some declaration]`
2025-04-08 11:07:07 +02:00
Nicolò Ribaudo
f654fb1b83
Lint against export [some declaration]
This disallowd the following types of `export` declaration:
- `export class A {}`/`export function A() {}`
- `export default class A {}`/`export default function A() {}`
- `export let A`/`export const A`/`export var A`

While allowing
- `export { A }`
- `export default A`
2025-04-07 17:51:17 +02:00
Nicolò Ribaudo
da5b681b16
[chrome] Fix text selection with .markedContent
The current text layer approach based on absolutely positioned
`<span>` elements by default causes flickering with text selection,
and we have browser-specific workarounds to solve that.

In Chrome, the workaround involves moving the `.endOfContent` element to
right after the last element that contains some selected content. This
works well in simple PDFs, but breaks when we have `span.markedContent`
elements. Given a text layer structure like the following, rendered
as four consecutive lines:
```html
<span class="markedContent">
  <br>
  <span>development enter the construction phase (estimated at around</span>
</span>
<span class="markedContent">
  <br>
  <span>300 MEUR).</span>
</span>
<span class="markedContent">
  <br>
  <span>Kreate's EBITA increased to 2.8 MEUR (Q4'23: 2.7 MEUR) and the</span>
</span>
<span class="markedContent">
  <br>
  <span>margin rose to 3.7% (Q4'23: 3.4%). However, profitability was</span>
</span>
```
when starting to select from inside the first line and dragging down
to the empty space after the second line, Chrome will anchor the
selection at the beginning of either the `<br>` or the `<span>` inside
the last `.markedContent`, depending on whether the selection is in
"per-character mode" (i.e. click and drag) or "per-word mode" (i.e.
double click and drag). This causes us to insert the `.endOfContent`
element in the wrong place (one element too far), which causes one
more line to be selected, which triggers another `"selecctionchange"`
event, which causes us to move `.endOfContent` again, and so on, looping
until when the whole page is selected.

This commit fixes the issue by making sure that when the end of the
selection range points to the _begining_ of an element, we walk back
the dom finding the first non-empty element, and attatch `.endOfContent`
to the end of that.
2025-04-07 17:36:40 +02:00
Jonas Jenwald
d882d0869c Move the IDENTITY_MATRIX constant into src/core/core_utils.js (PR 19772 follow-up)
After the changes in PR 19772 the `IDENTITY_MATRIX` constant is now only used on the worker-thread, which leads to Webpack marking the code as unused in the *built* `pdf.mjs` file; see https://phabricator.services.mozilla.com/D244533#change-8oITAexCvrlQ
2025-04-07 11:40:18 +02:00
Jonas Jenwald
2c593b06e4 [api-minor] Replace various getAll methods with iterators
These `getAll` methods are not used anywhere within the PDF.js code-base, outside of tests, and were mostly added (speculatively) for third-party users.
To still allow access to the same data we instead introduce iterators on these classes, which (slightly) shortens the code and allows us to remove the `objectFromMap` helper function.

A summary of the changes in this patch:
 - Replace the `getAll` methods with iterators in the following classes: `AnnotationStorage`, `Metadata`, and `OptionalContentGroup`.

 - Change, and also re-name, `AnnotationStorage.prototype.setAll` into a test-only method since it's not used elsewhere.

 - Remove the `Metadata.prototype.has` method, since it's only used in tests and can be trivially replaced by calling `Metadata.prototype.get` and checking if the returned value is `null`.
2025-04-06 21:43:16 +02:00
Tim van der Meij
72feb4c256
Merge pull request #19775 from Snuffleupagus/AFSimple_Calculate-MIN-MAX-destructuring
Use Array-destructuring when computing MIN/MAX in `AFSimple_Calculate`
2025-04-06 18:32:37 +02:00
Tim van der Meij
36ece9af8d
Merge pull request #19777 from Snuffleupagus/Node-engines-20.16.0
Tweak the Node.js version listed in "engines", to ensure that `process.getBuiltinModule` is available
2025-04-06 18:25:26 +02:00
Tim van der Meij
082485f094
Merge pull request #19776 from Snuffleupagus/rm-reduce-unit-tests
Remove `Array.prototype.reduce` usage from the unit-tests
2025-04-06 18:24:29 +02:00
Jonas Jenwald
18617eb792 Tweak the Node.js version listed in "engines", to ensure that process.getBuiltinModule is available
In order to use the PDF.js library in Node.js environments the `process.getBuiltinModule` functionality must be available, which was released in [version `20.16.0`](https://nodejs.org/en/blog/release/v20.16.0), however we've seen repeated issues filed by users on older `20.x` versions.
2025-04-06 14:32:36 +02:00
Jonas Jenwald
ec5b5184d3 Remove Array.prototype.reduce usage from the unit-tests
Using `Array.prototype.reduce` often leads to less readable code, and in these cases we can replace it with other Array-methods instead.
2025-04-06 12:12:37 +02:00
Jonas Jenwald
0845552ff9 Use Array-destructuring when computing MIN/MAX in AFSimple_Calculate
This appears to work fine with QuickJS, as evident by the added unit-test, and allows us to remove more `Array.prototype.reduce` usage.
2025-04-06 11:44:08 +02:00
calixteman
6cc37c8415
Merge pull request #19772 from calixteman/settextmatrix
Avoid to create an array when setting the text matrix
2025-04-05 21:59:30 +02:00
Calixte Denizet
4c63905a18 Avoid to create an array when setting the text matrix 2025-04-05 20:45:26 +02:00
Jonas Jenwald
9217d253aa
Merge pull request #19771 from Snuffleupagus/canvas-shorter-CanvasExtraState
Change how (most) fields are initialized in the `CanvasExtraState` class
2025-04-05 18:46:24 +02:00
Jonas Jenwald
7cfb1be650
Merge pull request #19758 from Snuffleupagus/OperatorList-setOptions
Initialize the `isOffscreenCanvasSupported` option, in the `OperatorList` class, once per document
2025-04-05 18:45:55 +02:00
Jonas Jenwald
a70f42339e
Merge pull request #19756 from Snuffleupagus/canvas-unconditional-ctx-filter
Use `ctx.filter` unconditionally in the `src/display/canvas.js` file
2025-04-05 18:45:19 +02:00
Jonas Jenwald
010b6ad886 Change how (most) fields are initialized in the CanvasExtraState class
The majority of the class fields don't depend on any parameters, hence we can re-factor and shorten by this using modern JavaScript features.
2025-04-05 15:03:06 +02:00
Jonas Jenwald
624d8a418e Remove "unnecessary" inline function names in the src/display/canvas.js file
This is ever so slightly shorter, which cannot hurt.
2025-04-05 14:59:04 +02:00
calixteman
7eef7dfc78
Merge pull request #19763 from calixteman/simplify_updaterect
Replace UpdateRectMinMax by getAxialAlignedBoundingBox
2025-04-04 21:33:05 +02:00
Jonas Jenwald
a6934a4dd5
Merge pull request #19768 from Snuffleupagus/writer-infoMap
Pass the /Info-strings as a `Map` to the `src/core/writer.js` code
2025-04-04 20:53:55 +02:00
Calixte Denizet
e7a951547d Replace UpdateRectMinMax by getAxialAlignedBoundingBox
and don't use array destructuring because it induces a memory and perf penalties.
2025-04-04 19:57:55 +02:00
Jonas Jenwald
dad6febc39 Pass the /Info-strings as a Map to the src/core/writer.js code
We want to iterate through the data in the `computeMD5` function, and `Map`s have "nicer" support for that than generic objects.
(Somewhat recently `Map` performance was improved in Firefox, however this also isn't really performance sensitive code.)
2025-04-04 13:36:13 +02:00
Jonas Jenwald
0bde69c7bf
Merge pull request #19767 from Snuffleupagus/issue-19761
Attempt to get bundlers to leave Emscripten Compiler generated `URL`s alone (issue 19761)
2025-04-04 12:30:15 +02:00
Jonas Jenwald
20e06c4bb5 Attempt to get bundlers to leave Emscripten Compiler generated URLs alone (issue 19761)
Note that we load all wasm-files manually, however the Emscripten Compiler (emcc) unfortunately generates `URL`s for fallback wasm-file loading.
In the PDF.js build-scripts we work-around that by using suitable Webpack-options, however that apparently doesn't work when third-party users re-bundle our code and we thus try to work-around this by adding "ignore comments" to these `URL`s (similar to how we handle `import`-statements).
2025-04-04 11:45:54 +02:00
Jonas Jenwald
bd06061e85
Merge pull request #19766 from Snuffleupagus/version-5.2
Bump library version to `5.2`
2025-04-04 08:58:16 +02:00
Jonas Jenwald
bd418de6b0 Bump library version to 5.2 2025-04-04 08:54:06 +02:00
Jonas Jenwald
16c876569f
Merge pull request #19760 from Snuffleupagus/OperatorList-transfers-bitmap
Also transfer `ImageBitmap`s for image masks and inline images
2025-04-03 20:10:27 +02:00
Jonas Jenwald
8c5fb7979f Also transfer ImageBitmaps for image masks and inline images
Currently we're only transferring TypedArrays, however there's no reason (as far as I can tell) to not also transfer `ImageBitmap`s in these cases.
2025-04-03 18:50:39 +02:00
calixteman
ff2d95a9b6
Merge pull request #19752 from calixteman/simplify_updateRectMinMax
Simplify updateRectMinMax in order to use slightly less memory
2025-04-03 18:25:30 +02:00
Calixte Denizet
41bed561f0 Simplify updateRectMinMax in order to use slightly less memory 2025-04-03 17:06:58 +02:00
Jonas Jenwald
4a6c47489e Initialize the isOffscreenCanvasSupported option, in the OperatorList class, once per document
Currently we're setting this option for each small inline image, which seems unnecessary since it should suffice to do that once per document.
2025-04-03 14:00:07 +02:00
Jonas Jenwald
0c78b46184 Use ctx.filter unconditionally in the src/display/canvas.js file
It seems that the `@napi-rs/canvas` dependency has *basic* canvas-filter support, whereas the "old" `canvas` dependency didn't, hence we no longer need the Node.js-specific checks in the `src/display/canvas.js` file.

Note that I've successfully tested the [`pdf2png` example](https://github.com/mozilla/pdf.js/tree/master/examples/node/pdf2png) with this patch applied and things appear to work as before.
2025-04-03 12:30:29 +02:00
Jonas Jenwald
a45f961a1c
Merge pull request #19751 from Snuffleupagus/applyTransform-in-place
Change `Util.applyTransform` to use the point-argument as an in/out parameter
2025-04-02 20:47:21 +02:00
calixteman
b7437376f1
Merge pull request #19732 from calixteman/rm_useless_beginpath
Remove few useless beginPaths
2025-04-02 13:46:05 +02:00
Jonas Jenwald
4262603b06 Re-name the Util.applyTransformToBezierInPlace method
Given that all `Util.apply...` methods are now using in/out parameters, we can slightly shorten the name of this one.
2025-04-01 23:03:50 +02:00
Jonas Jenwald
c852e877d8 Change Util.applyInverseTransform to use the point-argument as an in/out parameter
This will help reduce the total number of Array allocations, which cannot hurt.
2025-04-01 23:03:44 +02:00
Jonas Jenwald
fa643bb22f Change Util.applyTransform to use the point-argument as an in/out parameter
This will help reduce the total number of Array allocations, which cannot hurt, and also allows us to remove the `Util.applyTransformInPlace` method.
2025-04-01 23:03:36 +02:00
Jonas Jenwald
e5fbf52405
Merge pull request #19736 from Snuffleupagus/compileType3Glyph-worker
[api-minor] Move Type3-glyph compilation to the worker-thread
2025-04-01 19:40:30 +02:00
Jonas Jenwald
b33522a208
Merge pull request #19746 from Snuffleupagus/evaluator-img-cache-tweaks
Reduce some code duplication when handling globally cached images
2025-04-01 19:15:10 +02:00
calixteman
25693dc0ee
Merge pull request #19712 from calixteman/optimize_save_construct
Optimize save-transform-constructPath-restore
2025-04-01 18:20:59 +02:00
Calixte Denizet
a35443ff45 Remove few useless beginPaths.
The ctx current path is no more changed since we use some Path2D when we want to fill/stroke them.
It makes calling ctx.beginPath useless.
2025-04-01 16:02:38 +02:00
Jonas Jenwald
9cd5a9658a [api-minor] Move Type3-glyph compilation to the worker-thread
After PR 19731 the format of compiled Type3-glyphs is now simple enough that the compilation can be moved to the worker-thread, without introducing any significant additional complexity.
This allows us to, ever so slightly, simplify the implementation in `src/display/canvas.js` since the Type3 operatorLists will now directly include standard path-rendering operators (using the format introduced in PR 19689).

As part of these changes we also stop caching Type3 image masks since: we've not come across any cases where that actually helps, they're usually fairly small, and it simplifies the code.

Note that one "negative" change introduced in this patch is that we'll now compile Type3-glyphs *eagerly*, whereas previously we'd only do that lazily upon their first use.
However, this doesn't seem to impact performance in any noticeable way since the compilation is fast enough (way below 1 ms/glyph in my testing) and Type3-fonts are also limited to just 256 glyphs. Also, many (or most?) Type3-fonts don't even use image masks and are thus not affected by these changes.
2025-04-01 09:09:00 +02:00