Commit Graph

5943 Commits

Author SHA1 Message Date
calixteman
622465dc20
Merge pull request #16223 from calixteman/16221
Create a new chunk when the char is too rised compared to the previous one
2023-03-28 15:30:14 +02:00
Calixte Denizet
a96f10e55d Create a new chunk when the char is too rised compared to the previouse one 2023-03-28 13:56:46 +02:00
Jonas Jenwald
d584513cb2
Merge pull request #16213 from Snuffleupagus/validateCSSFont-quotes
Reduce duplication in the `validateCSSFont` helper function
2023-03-28 12:40:23 +02:00
Jonas Jenwald
ec08bd5c41
Merge pull request #16218 from Snuffleupagus/isPDFFunction-simplify
Simplify the `isPDFFunction` helper function
2023-03-28 11:16:25 +02:00
Calixte Denizet
5f5256b4c4 ESMify some modules for m-c (bug 1824610) 2023-03-27 12:01:31 +02:00
Jonas Jenwald
20cbb89412 Simplify the isPDFFunction helper function
Originally we used helper functions for checking if something was a Dictionary or Stream, and then having an initial `typeof` check probably made sense.
However, given that we're using `instanceof` nowadays the additional check longer seems necessary.
2023-03-27 11:34:20 +02:00
Jonas Jenwald
ef70988027 Reduce duplication in the validateCSSFont helper function
Currently we're *virtually* duplicating the same code, for validating quotation marks, twice in this helper function.

The size decrease is quite small (107 bytes) and this makes the code slightly harder to reader, hence I completely understand if this patch is rejected.
2023-03-26 12:12:49 +02:00
nmtigor
167b363eb3 Write some {Object} in api.js more precise 2023-03-25 22:00:06 +01:00
Jonas Jenwald
007c367018 Fix spelling of occurred in a couple of comments 2023-03-25 20:46:02 +01:00
Tim van der Meij
a1685fd0d8
Merge pull request #16199 from Snuffleupagus/commonobj-FontInspector
Slightly reduce the size of the `FontInspector`-integration in the API
2023-03-25 15:27:01 +01:00
Jonas Jenwald
035a273d30 Use replaceAll in the recoverJsURL helper function
We can just do direct replacement when building the regular expression, rather than splitting the string into an Array and then re-joining it.
2023-03-25 12:31:39 +01:00
Jonas Jenwald
96e34fbb7d Enable the unicorn/prefer-negative-index ESLint plugin rule
Please see https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-negative-index.md
2023-03-24 10:18:32 +01:00
Jonas Jenwald
378caa7203 Slightly reduce the size of the FontInspector-integration in the API
Given that this functionality only applies in the viewer, when `PDFBug` is being enabled and used, it can't hurt to slightly reduce the size of this code.
2023-03-23 14:07:10 +01:00
Jonas Jenwald
1fc09f0235 Enable the unicorn/prefer-string-replace-all ESLint plugin rule
Note that the `replaceAll` method still requires that a *global* regular expression is used, however by using this method it's immediately obvious when looking at the code that all occurrences will be replaced; please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll#parameters

Please find additional details at https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-string-replace-all.md
2023-03-23 12:57:10 +01:00
Jonas Jenwald
5f64621d46 Use String.prototype.replaceAll() where appropriate
This fairly new method allows replacing *multiple* occurrences within a string without having to use regular expressions.

Please refer to:
 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll#browser_compatibility
2023-03-22 15:31:10 +01:00
Jonas Jenwald
137a2d6e30 Add even more non-standard ligatures (PR 15517 follow-up)
Given that we already create multi-byte ToUnicode entries in other cases, see e.g. the `getNormalizedUnicodes` table, this is hopefully fine.
2023-03-22 10:42:52 +01:00
Jonas Jenwald
122d5e549a Track previous "XRefStm"s in a Set, rather than an Object
Having just reviewed a patch touching this code, I couldn't help noticing that an `Object` isn't really the optimal data-structure for this and nowadays we can do better by using a `Set` instead.
2023-03-22 09:41:19 +01:00
Jonas Jenwald
9321758d91
Merge pull request #16186 from Snuffleupagus/issue-16176
Support multi-byte ToUnicode entries, when using predefined CMaps (issue 16176)
2023-03-21 22:17:18 +01:00
Jonas Jenwald
d4bcfe8c16 Support multi-byte ToUnicode entries, when using predefined CMaps (issue 16176)
Hopefully this makes sense, since we already "create" multi-byte ToUnicode entries in other cases (see e.g. the `getNormalizedUnicodes` table).
2023-03-21 21:35:57 +01:00
calixteman
8bfebf1c24
Merge pull request #16188 from calixteman/bug1823296
Use the position of the previous xref stream if any when saving a pdf (bug 1823296)
2023-03-21 21:21:49 +01:00
Calixte Denizet
2d0f30a67c Use the position of the previous xref stream if any when saving a pdf (bug 1823296) 2023-03-21 19:27:24 +01:00
Jonas Jenwald
b1e0253f29
Merge pull request #16175 from Snuffleupagus/LoopbackPort-transfer
Fix the `transfer` parameter, for `structuredClone`, in the `LoopbackPort`
2023-03-20 14:22:09 +01:00
Jonas Jenwald
8bf5e96af9 Only warn about missing --scale-factor CSS-variable for visible textLayers (PR 16162 follow-up)
This is something that I completely overlooked in PR 16162, which in some cases cause the default viewer to incorrectly print warnings.
This can be reproduced with the PAGE scrolling-mode, and/or the PresentationMode, and this patch simply work-around it by checking the visibility as well (since the warning is a best-effort solution anyway).
2023-03-20 12:51:26 +01:00
Jonas Jenwald
cc9f6650a8 Stop passing in pageColors to the CanvasGraphics-constructor (PR 16075 follow-up)
The `pageColors`-option was removed from the `CanvasGraphics`-constructor in PR 16075, hence the code in the API no longer needs to pass in that option; this is something that I missed during review.
2023-03-20 11:41:57 +01:00
Jonas Jenwald
86cf62699c Add a helper, in DOMFilterFactory, to reduce duplication when creating <feFuncX>s
Currently we repeat the same code verbatim multiple times in the `DOMFilterFactory`, which seems completely unnecessary.
2023-03-20 11:27:47 +01:00
calixteman
3903391f3c
Merge pull request #16075 from calixteman/svg_filter_hcm
[api-minor] Use a SVG filter when rendering pages in HCM
2023-03-20 10:18:25 +01:00
Jonas Jenwald
c4a725fe98 Fix the transfer parameter, for structuredClone, in the LoopbackPort
The way that we handle the `transfer` parameter is unfortunately wrong, ever since PR 14392 which introduced the code, given that the MDN article originally contained incorrect information; please see https://github.com/mdn/content/pull/23164

By updating the `structuredClone` call such that it works correctly, we can enable more unit-tests in Node.js environments; please refer to https://developer.mozilla.org/en-US/docs/Web/API/structuredClone#parameters
2023-03-19 22:04:01 +01:00
Calixte Denizet
da080cc26e [api-minor] Use a SVG filter when rendering pages in HCM
The idea is to apply an overall filter on each page: the main advantage
is to have some filtered images which could help to make them visible for
some users.
2023-03-18 12:45:10 +01:00
Jonas Jenwald
2fea9ee21b Simplify the applyTransferMapsToCanvas method (PR 16151 follow-up)
During review of PR 16151 this method was simplified, however I overlooked the fact that we now can (and really should) improve this by removing duplication.
2023-03-17 12:58:35 +01:00
Jonas Jenwald
0e54a3c37a Warn about missing/incorrect --scale-factor CSS-variable in renderTextLayer (issue 16139)
Unfortunately I don't believe that we can simply add a default `--scale-factor` CSS-variable to the `container`-element, since that might not be entirely appropriate/correct in all cases.[1]
However, we can at least print a console-error to hopefully make this situation more apparent to users. (This is purposely not using the `warn` helper-function, since those messages can be disabled.)

---
[1] One example is in our reference-tests, where we don't need to add it to the `container`-element itself.
2023-03-16 11:53:12 +01:00
Jonas Jenwald
5e4b3d13eb
Merge pull request #16151 from Snuffleupagus/DefaultFilterFactory
[api-minor] Extend general transfer function support to browsers without `OffscreenCanvas`
2023-03-14 14:03:26 +01:00
Jonas Jenwald
50c844c5b8 Stop including isOffscreenCanvasSupported in the "StartRenderPage" message
With the previous commit this is now completely unused in API, hence it can be removed. This is done in a separate commit to make it easier to re-instate it, would the need ever arise.
2023-03-14 13:09:20 +01:00
Jonas Jenwald
fc055dbd80 [api-minor] Extend general transfer function support to browsers without OffscreenCanvas
This patch extends PR 16115 to work in all browsers, regardless of their `OffscreenCanvas` support, such that transfer functions will be applied to general rendering (and not just image data).
In order to do this we introduce the `BaseFilterFactory` that is then extended in browsers/Node.js environments, similar to all the other factories used in the API, such that we always have the necessary factory available in `src/display/canvas.js`.

These changes help simplify the existing `putBinaryImageData` function, and the new method can easily be stubbed-out in the Firefox PDF Viewer.

*Please note:* This patch removes the old *partial* transfer function support, which only applied to image data, from Node.js environments since the `node-canvas` package currently doesn't support filters. However, this should hopefully be fine given that:
 - Transfer functions are not very commonly used in PDF documents.
 - Browsers in general, and Firefox in particular, are the *primary* development target for the PDF.js library.
 - The FAQ only lists Node.js as *mostly* supported, see https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support
2023-03-14 13:09:08 +01:00
Jonas Jenwald
103fda1d91 Update the canvasContext parameter, in RenderParameters (issue 16133)
Hopefully this works correctly (since I don't know anything about TypeScript), given that `CanvasRenderingContext2D` is a standard name; please see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
2023-03-13 16:56:36 +01:00
Tim van der Meij
9819f1cc6b
Merge pull request #16108 from Snuffleupagus/delay-cleanup
Slightly delay cleanup, after rendering, in documents with large images
2023-03-11 15:52:12 +01:00
Tim van der Meij
05f29c8720
Merge pull request #16141 from Snuffleupagus/FilterFactory-docId
Include the document-id in the SVG-filter names (PR 16062 follow-up)
2023-03-11 15:45:42 +01:00
Tim van der Meij
1234d207e9
Merge pull request #16142 from Snuffleupagus/issue-16134
Fix the JSDoc `returns`-type for two `PageViewport`-methods (issue 16134)
2023-03-11 14:53:21 +01:00
Tim van der Meij
cc499505df
Merge pull request #16123 from Snuffleupagus/Node-16
[api-minor] Update the minimum supported Node.js version to 16
2023-03-11 14:49:59 +01:00
calixteman
b2a86350fc
Merge pull request #16096 from bungeman/fix_trig_functions
Correct PostScript trigonometric operators
2023-03-11 14:32:23 +01:00
Calixte Denizet
f9539e57ef Move the svg definitions in its own div
This way the svg element doesn't take up space in the viewer.
Fixes issue #16135.
2023-03-10 17:03:40 +01:00
Jonas Jenwald
9232264b8a Fix the JSDoc returns-type for two PageViewport-methods (issue 16134)
The affected methods have always returned Arrays, however the JSDoc did not accurately reflect that.
2023-03-09 15:57:16 +01:00
Jonas Jenwald
92296fa6a1 Include the document-id in the SVG-filter names (PR 16062 follow-up)
In the general PDF.js library multiple PDF documents may be opened on the same web-page, which is why we many years ago started using document-specific identifiers to prevent issues with global data such e.g. with fonts.
Hence we need to treat the identifiers generated by the `FilterFactory` in the same way, since the SVG-filters for two separate PDF documents may otherwise get identical ids.
2023-03-09 15:35:29 +01:00
Ben Wagner
5fad91a680 Better approximate gradient color stops
PDF gradients do not have color stops but an arbitrary PDF function of
the type f(t) -> color. CSS gradients are only based on color stops.
Most PDF gradient functions are produced from color stop oriented
gradients.

Take advantage of this by sampling the PDF function at a higher
frequency but not converting any samples which could be interpolated to
color stops. The sampling frequency is chosen to be the least common
multiple of as many values as practical to exactly re-create the common
case of the PDF function implementing equally spaced linearly
interpolated stops in RGB color space. This also allows for better
approximation of other smooth PDF functions (non-linear, or non-equally
spaced, or in different color space).

Fixes: #10572, #14165
2023-03-09 08:49:50 -05:00
Jonas Jenwald
c0671ac133 Slightly increase the maximum image sizes that we'll cache
The current value originated in PR 2317, and in the decade that have passed the amount of RAM available in (most) devices should have increased a fair bit.
Nowadays we also do a much better job of detecting repeated images at both the page- and document-level, which helps reduce overall memory-usage in many documents.

Finally the constant is also moved into the `src/shared/util.js` file, since it was implicitly used on both the main- and worker-thread previously.
2023-03-08 17:06:10 +01:00
Jonas Jenwald
15d9faba57 Slightly delay cleanup, after rendering, in documents with large images
Currently in PDF documents with large images we immediately cleanup once rendering has finished, in order to reduce memory-usage.
Normally that shouldn't be a big problem, however when e.g. repeated zooming happens in the viewer that could easily lead to a lot of wasted resources (and waiting).

Hence this patch, which introduces a new `PDFPageProxy` method that will slightly delay cleanup after rendering.
2023-03-08 17:06:09 +01:00
Jonas Jenwald
e7a7f02f4c Convert a couple of fields/methods into properly private ones in PDFPageProxy
These were always intended to be *private*, so let's use modern JS features to actually enforce that.
2023-03-08 17:06:09 +01:00
calixteman
a0ef5a4ae1
Merge pull request #16115 from calixteman/issue16114
Apply transfer filters to any graphic commands
2023-03-08 14:53:41 +01:00
calixteman
32e2f7c8e5
Merge pull request #16131 from calixteman/issue16049
Partially revert PR 15578
2023-03-08 12:57:18 +01:00
Jonas Jenwald
6839f15a32
Merge pull request #16128 from Snuffleupagus/issue-16127
Support (rare) Type3 fonts with Pattern resources (issue 16127)
2023-03-08 12:21:53 +01:00
Jonas Jenwald
e5427ab11b
Merge pull request #16122 from Snuffleupagus/rm-onUnsupportedFeature
[api-minor] Remove the deprecated `onUnsupportedFeature` functionality (PR 15758 follow-up)
2023-03-08 12:16:27 +01:00
Calixte Denizet
dabfbc623e Partially revert PR 15578
The dimensions still need to be fixed (from times to times they're in px)
but it doesn't have to be postponed anymore.
To test it: draw something and when resizing look at the dimensions of the div
in devtools, the units must be %.
2023-03-08 12:10:27 +01:00
calixteman
cc555a389b
Merge pull request #16117 from calixteman/workaround_bug1820511
Avoid to have a factor too close to 2 when downscaling image
2023-03-08 11:12:56 +01:00
Calixte Denizet
1617ee6c3f Avoid to have a factor too close to 2 when downscaling image
It's a workaround for bug 1820511: it only affects Firefox on Windows
using the D2D backend.
2023-03-08 11:05:46 +01:00
Calixte Denizet
e9474f1c84 [api-minor] Add an option to set the max canvas area 2023-03-08 10:37:06 +01:00
Jonas Jenwald
471aef5fc6 Support (rare) Type3 fonts with Pattern resources (issue 16127)
This simply extends the approach in PR 10727 to also cover Patterns, which shouldn't be a common occurrence in Type3 fonts (since this is the first issue we've seen).
2023-03-08 09:20:52 +01:00
Calixte Denizet
8304df2520 Apply transfer filters to any graphic commands 2023-03-07 22:17:19 +01:00
Calixte Denizet
b8dda089e2 Slightly modify the max width of a tracking space 2023-03-07 19:38:49 +01:00
Jonas Jenwald
6d3506548d [api-minor] Update the minimum supported Node.js version to 16
This patch updates the minimum supported environments as follows:
 - Node.js 16, which was released on 2021-04-20; see https://en.wikipedia.org/wiki/Node.js#Releases

Note also that Node.js 14 will very soon reach EOL, and thus no longer receive any security updates.
2023-03-07 16:33:56 +01:00
Calixte Denizet
8db77cc361 Use appearance stream to render locked annotations (bug 1723568) 2023-03-07 15:01:31 +01:00
Jonas Jenwald
2f3dcc2327 [api-minor] Remove the deprecated onUnsupportedFeature functionality (PR 15758 follow-up)
This was deprecated in PR 15758, which has now been included in three official PDF.js releases.
While PR 15880 did limit the bundle-size impact of this functionality on e.g. the Firefox PDF Viewer, it still leads to some unnecessary "bloat" that these changes remove.
Furthermore, with this being deprecated there'd also be no effort put into e.g. extending the `UNSUPPORTED_FEATURES` list when handling future error cases.
2023-03-07 10:18:43 +01:00
Calixte Denizet
3849063d36 [Annotation] Don't rotate an annotation when it has the NoRotate flag 2023-03-06 17:27:11 +01:00
Calixte Denizet
05b0c9d7e6 Render large images even if they're larger than the canvas limits (bug 1720282)
The idea is to encode large image in BMP format (which is very simple and doesn't
require to compute any checksums) and then use createImageBitmap with a BMP blob
(which doesn't suffer of the Canvas/ImageData limits).
From a performance point of view, it isn't crazy (generating a large blob + decoding
it on the main thread is really not ideal) but at least we've something to display
which is a way better than a blank page (and one can notice that most of the time is
spent in decoding the image from the pdf stream).
2023-03-05 14:07:07 +01:00
Ben Wagner
158c836e26 Correct PostScript trigonometric operators
PDF 32000-1:2008 7.10.5.1 "Type 4 (PostScript Calculator) Functions"
defers to the PostScript Language Reference for the description of these
functions. The PostScript Language Reference, third edition chapter 8
"Operators" defines the `angle` type as a "number of degrees". Section
8.1 defines "angle `sin` real", "angle `cos` real", and "num den `atan`
angle". The documentation for `atan` further states that it will return
an angle in degrees between 0 and 360.

Handle these operators correctly in `PostScriptEvaluator.execute`.
Convert the inputs to `sin` and `cos` from degrees to radians for use
with `Math.sin` and `Math.cos`. Correctly pop two values from the stack
for `atan`, use `Math.atan2`, and convert from radians to (positive)
degrees.
2023-03-03 17:25:11 -05:00
Jonas Jenwald
ceec93c832
[api-minor] Remove calling getDocument directly with a PDFDataRangeTransport-instance (PR 15943 follow-up)
This was deprecated in PR 15943, which has now been included in two official PDF.js releases.
Given that `PDFDataRangeTransport` is somewhat unlikely to be used outside of the *built-in* Firefox PDF Viewer, it doesn't seem necessary to wait longer before removing this.

Also, removes the specific error-message for GENERIC builds to not unnecessarily "advertise" using non-objects when calling the `getDocument`-function.

*Please note:* This patch is written using the GitHub UI, since I'm currently without a dev machine, so hopefully it works correctly.
2023-03-02 15:12:01 +01:00
Calixte Denizet
fd03cd5493 [api-minor] Generate images in the worker instead of the main thread.
We introduced the use of OffscreenCanvas in #14754 and this patch aims
to use them for all kind of images.
It'll slightly improve performances (and maybe slightly decrease memory use).
Since an image can be rendered in using some transfer maps but because of
OffscreenCanvas we don't have the underlying pixels array the transfer maps
stuff is re-implemented in using the SVG filter feComponentTransfer.
2023-03-01 17:40:12 +01:00
Jonas Jenwald
9640add1f7
Merge pull request #16100 from Snuffleupagus/getDocument-canvasFactory
[api-minor] Move the `canvasFactory` option into `getDocument`
2023-03-01 10:34:11 +01:00
Jonas Jenwald
f42a2e8451
[api-minor] Move the canvasFactory option into getDocument
Rather than repeatedly initializing a `canvasFactory`-instance for every page, move it to the document-level instead.

*Please note:* This patch is written using the GitHub UI, since I'm currently without a dev machine, so hopefully it works correctly.
2023-03-01 09:07:16 +01:00
Jonas Jenwald
45c332110e
Check OffscreenCanvas support once on the worker-thread
Currently we repeat the `FeatureTest.isOffscreenCanvasSupported` checks all over the worker-thread code, and with upcoming changes this will become even "worse".

Hence this patch, which changes the *worker-thread* default value for the `isOffscreenCanvasSupported`-parameter to `false` and moves the feature-testing into the `BasePdfManager`-constructor.

*Please note:* This patch is written using the GitHub UI, since I'm currently without a dev machine, so hopefully it works correctly.
2023-02-27 12:27:28 +01:00
Jonas Jenwald
5075d0495b
Use OffscreenCanvas as intended for all code-paths in src/display/text_layer.js (PR 15722 follow-up)
Currently some `getCtx` calls will have `isOffscreenCanvasSupported === undefined` set, meaning that `OffscreenCanvas` isn't being used as intended, since no `TextLayerRenderTask._isOffscreenCanvasSupported` property exists.

*Please note:* This patch is written using the GitHub UI, since I'm currently without a dev machine, so hopefully it works correctly.
2023-02-24 11:29:58 +01:00
Calixte Denizet
3a21423386 [Acroform] Use the full path to find the node in the XFA datasets where to store the value
I noticed several 'Path not found' errors because of a field called #subform[2].
From the XFA specs, the hash is used for a class of elements in the template tree.
When we're looking for a node in the datasets tree, it doesn't make sense to search
for a class. Hence the path element starting with a hash are just skipped.
2023-02-23 12:09:39 +01:00
Jonas Jenwald
1b076b7a35
Move the ImageBitmap clean-up into the PDFObjects class
With upcoming changes we'll potentially start to cache `ImageBitmap` data at the document-level, in addition to just at the page-level.
Hence we need to ensure that such data is actually released on clean-up, and rather than duplicating the existing *manual* handling this code is instead moved into the `PDFObjects.clear` method. (In my opinion, this is an overall improvement even without globally cached `ImageBitmap` data.)

*Please note:* This patch is written using the GitHub UI, since I'm currently without a dev machine, so hopefully it's correct and makes sense.
2023-02-21 12:00:45 +01:00
Calixte Denizet
dca54c8f8a [JS] Send a Validate action on change on Choice widget 2023-02-19 16:33:05 +01:00
Jonas Jenwald
b6ba8cc84a [api-minor] Deprecate providing binary data as Buffer in Node.js environments
The `Buffer`-object is Node.js specific functionality[1], thus (obviously) not found in browsers. Please note that the PDF.js library has never officially supported/documented that binary data can be passed as a `Buffer`, and that *internally* in the `src/core`-code we only work with standard `Uint8Array`s.
This means that if, in Node.js environments, a `Buffer` is passed to the API we need to wrap it into a `Uint8Array`, which essentially means creating a copy of the data and thus increasing memory usage.

---
[1] Refer to https://nodejs.org/api/buffer.html#buffer
2023-02-14 11:30:40 +01:00
Jonas Jenwald
df3b359280 Remove "else after return" from the getUrlProp/getDataProp helper functions
This helps readability of this code a little bit, in my opinion, and it's actually ever so slightly less code in the *built* `pdf.js` file.
2023-02-14 10:50:22 +01:00
Jonas Jenwald
8026ed6b0a Reduce duplication for reference tests with an annotationStorage entry
Currently we duplicate the same code more than once in the `test/driver.js` file, which we can avoid by adding a new `AnnotationStorage` helper method instead.
2023-02-13 11:09:16 +01:00
Tim van der Meij
22618213c7
Merge pull request #16040 from Snuffleupagus/arrayBuffersToBytes
Re-factor the `arraysToBytes` helper function (PR 16032 follow-up)
2023-02-12 11:47:57 +01:00
Jonas Jenwald
6d4d402a78 Move the arrayBuffersToBytes helper function into the worker-thread
Given that this helper function is only used on the worker-thread, there's no reason to duplicate it in both of the *built* `pdf.js` and `pdf.worker.js` files.
2023-02-11 21:34:37 +01:00
Jonas Jenwald
18042163ce Improve the consistency between the LocalPdfManager/NetworkPdfManager constructor
Currently these classes take a bunch of parameters (somewhat randomly ordered), probably because this is very old code that's been extended over the years.
Hence this patch changes the constructors to use parameter-objects instead, which improves consistency and (slightly) reduces the amount of code as well.

*Please note:* Also removes the `msgHandler`-property on these classes, since I cannot find a single call-site that accesses it.
2023-02-11 13:39:52 +01:00
Jonas Jenwald
14b0e8c0b6 Ensure that "GetAnnotations" errors are propagated to the main-thread (PR 15267 follow-up)
With the changes in PR 15267 we're now accidentally swallowing "GetAnnotations" errors, rather than propagating them to the main-thread as intended.
2023-02-10 12:18:35 +01:00
Jonas Jenwald
c56f25409d Re-factor the arraysToBytes helper function (PR 16032 follow-up)
Currently this helper function only has two call-sites, and both of them only pass in `ArrayBuffer` data. Given how it's implemented there's a couple of code-paths that are completely unused (e.g. the "string" one), and in particular the intended fast-paths don't actually work.
This patch re-factors and simplifies the helper function, and it'll no longer accept anything except `ArrayBuffer` data (hence why it's also re-named).

Note that at the time when `arraysToBytes` was added we still supported browsers without TypedArray functionality, and we'd then simulate them using regular Arrays.
2023-02-10 10:26:35 +01:00
Jonas Jenwald
5ba596786c Change WorkerTasks, in WorkerMessageHandler.createDocumentHandler, to a use a Set
This is a tiny bit more compact, thanks to the `Set.prototype.delete` method.
2023-02-09 22:01:16 +01:00
calixteman
0fca6e187c
Merge pull request #16035 from calixteman/fix_combo_value
[Annotation] A combo can have a value other than one in the options
2023-02-09 19:56:16 +01:00
Jonas Jenwald
1fc8350795
Merge pull request #16032 from Snuffleupagus/less-arrayByteLength
Reduce usage of the `arrayByteLength` helper function
2023-02-09 18:56:20 +01:00
Calixte Denizet
cb1638530d [Annotation] A combo can have a value other than one in the options
When printing the pdf in #12233 in Acrobat, we can see that the combo for country
is empty: it's because the V entry doesn't have to be one of the options.
2023-02-09 18:50:57 +01:00
calixteman
972744a68f
Merge pull request #16033 from calixteman/bug1640217
Ignore position of combining diacritics when getting text (bug 1640217)
2023-02-09 18:23:59 +01:00
calixteman
533a461db0
Merge pull request #16031 from calixteman/bug1770750
[Annotation] For choice widget, use the I entry instead of the V one (bug 1770750)
2023-02-09 18:01:28 +01:00
Calixte Denizet
58e4d92884 [Annotation] For choice widget, use the I entry instead of the V one (bug 1770750)
It isn't really conform to the specifications but Acrobat is working like that...
2023-02-09 17:26:13 +01:00
Calixte Denizet
4e9f26afa3 Ignore position of combining diacritics when getting text (bug 1640217) 2023-02-09 17:13:57 +01:00
Jonas Jenwald
96d338e437 Reduce usage of the arrayByteLength helper function
We're using this helper function when reading data from the [`PDFWorkerStreamReader.read`](a49d1d1615/src/core/worker_stream.js (L90-L98)) and [`PDFWorkerStreamRangeReader.read`](a49d1d1615/src/core/worker_stream.js (L122-L128)) methods, and as can be seen they always return `ArrayBuffer` data. Hence we can simply get the `byteLength` directly, and don't need to use the helper function.

Note that at the time when `arrayByteLength` was added we still supported browsers without TypedArray functionality, and we'd then simulate them using regular Arrays.
2023-02-09 15:50:38 +01:00
Jonas Jenwald
323d3d246a Re-factor the readChunk function in ChunkedStreamManager.sendRequest
Move the `done` branch to the top of the function, similar to how we usually format things when `ReadableStream`s are used.
2023-02-09 15:33:06 +01:00
Jonas Jenwald
9d29abdfa0 Change the LoopbackPort class to use a Set internally
This is a tiny bit more compact, thanks to the `Set.prototype.delete` method.
2023-02-09 12:34:41 +01:00
Calixte Denizet
c92ba393c2 Fix pinch-to-zoom on mac for the Firefox builtin viewer
In the mac case we don't want to care about the scaleFactor threshold
because else if too big another move could start and then subsequent
events aren't considered as wheel events.
It isn't really ideal and at some point we'll need to find a way at
least for the Firefox case to get the real events instead of the fake
wheel ones.
2023-02-08 15:04:41 +01:00
Calixte Denizet
a25895bf72 [Annotation] Take into account the stroke alpha for a FreeText without appearance 2023-02-07 22:15:27 +01:00
Calixte Denizet
ea7b4b4d6c [Annotation] Avoid to encrypt the appearance stream two times (bug 1815476) 2023-02-07 19:26:46 +01:00
Jonas Jenwald
0a0f3fc733 Move the main-thread CMap/StandardFontData factory initialization to getDocument
By default we're using worker-thread fetching (in browsers) of this data nowadays, however in Node.js environments or if the user provides custom factories we still fallback to main-thread fetching.
Hence it makes sense, as far as I'm concerned, to move this initialization into the `getDocument` function to ensure that the factories can actually be initialized *before* attempting to load the document.

Also, this further reduces the amount of `getDocument` parameters that we need to pass into into the `WorkerTransport` class.
2023-02-05 11:52:35 +01:00
Jonas Jenwald
ce8ac6d96a Only pass the necessary parameters to _fetchDocument and WorkerTransport
Currently we're passing all available parameters to this function respectively class, despite that not actually being necessary.
By splitting the parameters we not only improve the structure, and basically "document" the code a little bit, but we can also simplify the `_fetchDocument` function considerably.
2023-02-05 11:52:33 +01:00
Jonas Jenwald
512aa50fdd Re-factor the parameter parsing/validation in getDocument
This is very old code, where we loop through the user-provided options and build an internal parameter object. To prevent errors we also need to ensure that the parameters are correct/valid, which is especially important for the ones that are sent to the worker-thread such that structured cloning won't fail.[1]

Over the years this has led to more and more code being added in `getDocument` to validate the user-provided options, and at this point *most* of them have at least basic validation. However the way that this is implemented feels slightly backwards, since we first build the internal parameter object and only *afterwards* validate those parameters.[2]

Hence this patch changes the `getDocument` function to instead check/validate the supported options upfront, and then *explicitly* build the internal parameter object with only the needed properties.

---
[1] Note the supported types at https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types

[2] The internal parameter object may also, because of the loop, end up with lots of unnecessary properties since anything that the user provides is being copied.
2023-02-05 11:52:25 +01:00
Tim van der Meij
e698664927
Merge pull request #16004 from Snuffleupagus/WorkerTransport-cacheSimpleMethod
Improve how we cache Promises in `WorkerTransport`
2023-02-04 15:13:12 +01:00
Tim van der Meij
b75dafba87
Merge pull request #15987 from Snuffleupagus/onOpenWithTransport-params
Remove unused parameters from the `onOpenWithTransport` method in `PDFViewerApplication.initPassiveLoading`
2023-02-04 15:07:42 +01:00
Tim van der Meij
e848a0e61c
Merge pull request #15981 from Snuffleupagus/cMapPacked-true
[api-minor] Let the `cMapPacked` parameter, in `getDocument`, default to `true`
2023-02-04 15:00:26 +01:00
Jonas Jenwald
3a7fce49a3 A tiny improvement of the MetadataParser._repair method
We can just insert the initial greater-than sign at the start of the buffer, rather than doing that manually at the end.
2023-02-04 12:43:55 +01:00
Jonas Jenwald
2de03a7d91 Improve how we cache Promises in WorkerTransport
A number of methods have their Promises cached, to avoid repeated worker round-trips, since they're expected to be called more than once from the default viewer. The way that the caching is currently implemented means that we need to remember to manually clear these Promises on document cleanup/destruction, and it'd be nice to avoid that.

With this patch the relevant Promises are now instead placed in just one `Map`, which is easy to clear, and a new helper method is also introduced to reduce duplication for *simple* `WorkerTransport` methods.
2023-02-04 11:57:37 +01:00
Calixte Denizet
185281957d [Editor] Make the annotation editor layer invisible when disabled and empty
It'll help to avoid to consider them when the browser is restyling.
2023-02-01 17:53:44 +01:00
Jonas Jenwald
cf8ee47589 Remove unused parameters from the onOpenWithTransport method in PDFViewerApplication.initPassiveLoading
The only parameter that we actually need here is the `PDFDataRangeTransport`-instance, since the others are not necessary.
 - The `url` parameter, as passed to the `getDocument` function in the API, is simply being ignored; see 2d87a2eb1c/src/display/api.js (L447-L458)
 - The `length` parameter, as passed to the `getDocument` function in the API, is always being overwritten; see 2d87a2eb1c/src/display/api.js (L519-L525)
2023-02-01 09:33:22 +01:00
Jonas Jenwald
5e88228767 Allow, optionally, using worker-modules during local development
Until PR 12563 is deemed safe to land, I'd still like to be able to use worker-modules in the viewer during local development.
Hence this patch which *temporarily* adds a new `workerModules` hash-parameter, only available in non-PRODUCTION mode, that allows using worker-modules in the development viewer.

To enable this functionality, simply use http://localhost:8888/web/viewer.html#workerModules=true
2023-01-31 12:09:44 +01:00
Jonas Jenwald
c5d6391898 [api-minor] Let the cMapPacked parameter, in getDocument, default to true
The initial CMap support was added in PR 4259 using the "raw" Adobe files, however they were quickly deemed to be unnecessarily large. As a result PR 4470 introduced the more compact "binary" CMap format, with both of those PRs being included in the very same release (version `0.8.1334`) .

Please note that we've thus never shipped anything *except* the "binary" CMap files with the PDF library, and furthermore note that we've not even once updated the CMap files since they were originally added almost nine years ago.

Requiring users to remember that `cMapPacked = true` is necessary, in addition to setting the `cMapUrl` parameter, in order for CMap loading to work feels like a less than ideal API.
Hence this patch, which suggests that we simply let `cMapPacked` default to `true` now.
2023-01-30 15:35:02 +01:00
Jonas Jenwald
808ca828f1 Extend getGlyphMapForStandardFonts with additional entries (issue 15977) 2023-01-30 12:13:21 +01:00
Tim van der Meij
ee3be2f979
Merge pull request #15951 from Snuffleupagus/polyfill-Path2D
Polyfill `Path2D` in Node.js environments
2023-01-28 19:06:54 +01:00
Tim van der Meij
e539d2da1e
Merge pull request #15964 from Snuffleupagus/getDocument-non-object
Only accept non-objects passed to `getDocument` in GENERIC builds
2023-01-28 18:42:09 +01:00
Jonas Jenwald
cf0369d622 Polyfill Path2D in Node.js environments
Until just recently the only existing `Path2D` polyfill didn't have support for Node.js and/or the `node-canvas` package. Given that this was just fixed, in the latest version, we can now finally remove our inline-checks at the relevant call-sites; please also see https://github.com/nilzona/path2d-polyfill#usage-with-node-canvas
2023-01-28 18:28:22 +01:00
Tim van der Meij
cb5a28ceca
Merge pull request #15954 from Snuffleupagus/getDocument-URL-tweaks
Tweak the internal handling of the `url`-parameter in `getDocument` (PR 13166 follow-up)
2023-01-28 18:18:17 +01:00
Tim van der Meij
16aef95937
Merge pull request #15966 from Snuffleupagus/GlobalWorkerOptions-defaults
Simplify setting the `GlobalWorkerOptions` default values (PR 9480 follow-up)
2023-01-28 18:15:32 +01:00
Calixte Denizet
6f4d037a8e [JS] Correctly format field with numbers (bug 1811694, bug 1811510)
In PR #15757, a value is automatically converted into a number when it's possible
but the case of numbers like "000123" has been overlooked and their format must
be preserved.
When a script is doing something like "foo.value + bar.value" and the values are
numbers then "foo.value" must return a number but the displayed value must be what
the user entered or what a script set, so this patch is just adding a a field
_orginalValue in order to track the value has it has defined.
Some people are used to use a comma as decimal separator, hence it must be considered
when a value is parsed into a number.
This patch is fixing a regression introduced by #15757.
2023-01-26 14:57:02 +01:00
Jonas Jenwald
1c4af2727c Simplify setting the GlobalWorkerOptions default values (PR 9480 follow-up)
There's really no need for these "complicated" default value assignments, since `GlobalWorkerOptions` is a local variable at this point, and this is rather a case of too much copy-and-paste.
Note that years ago, when all options were set using a global `PDFJS` object, it's possible that options had been set (from the outside) *before* the object had been properly initialized; see e.g. a89071bdef/src/display/global.js
2023-01-26 14:16:01 +01:00
Jonas Jenwald
4758e6649c Only accept non-objects passed to getDocument in GENERIC builds
In general it's always recommended to pass a *parameter object* when calling the `getDocument`-function in the API, since that's the only way to provide additional options, and the fact that it also accepts a URL or TypedArray directly is now mostly for backwards compatibility reasons.
Unfortunately we cannot really remove this, since that code has existed since "forever", however we can limit it to only the GENERIC build to avoid completely unnecessary checks in e.g. the Firefox PDF Viewer.

Finally, note that the default-viewer always provides a *parameter object* when calling the `getDocument`-function and it's thus completely unaffected by these changes.
2023-01-26 10:48:58 +01:00
Jonas Jenwald
755319130e Tweak the internal handling of the url-parameter in getDocument (PR 13166 follow-up)
- Use a `URL`-instance directly, since it's by definition an absolute URL.
 - Actually limit the "raw" url-string handling to Node.js environments, as intended.
 - Skip the warning, since we're already throwing an Error if the `url`-parameter is invalid.
2023-01-24 11:18:41 +01:00
Tim van der Meij
edfdb693e5
Merge pull request #15948 from Snuffleupagus/bug-1811668
Tweak `adjustType1ToUnicode` for fonts with a predefined *named* encoding (bug 1811668, PR 14050 follow-up)
2023-01-21 14:02:40 +01:00
Tim van der Meij
a27d7ba524
Merge pull request #15943 from Snuffleupagus/deprecate-direct-PDFDataRangeTransport
[api-minor] Deprecate calling `getDocument` directly with a `PDFDataRangeTransport`-instance
2023-01-21 13:50:20 +01:00
Jonas Jenwald
40a46e4397 Tweak adjustType1ToUnicode for fonts with a predefined *named* encoding (bug 1811668, PR 14050 follow-up)
*Please note:* I cannot reproduce the problem reported in bug 1811668, regarding the context menu, and in any case it's not clear that that part is even a PDF Viewer bug.

Looking at bug 1811668 I couldn't help but noticing that the textLayer isn't correct, and it's unfortunately once again a problem with the `adjustType1ToUnicode` function. That's intended to help improve text-selection for fonts without a /ToUnicode-entry, and in many cases it does help (the original PR fixed lots of issues) however it's also caused some problems.

In order to improve text-selection in bug 1811668, we'll now properly ignore fonts that have a predefined *named* encoding specified since that's really the intention with PR 14050.
2023-01-21 12:21:21 +01:00
Jonas Jenwald
f2fce93826 [JBIG2] Ensure that the decodeInteger function returns valid integers (issue 15942)
The JBIG2 images in this PDF document are corrupt enough that even Adobe Reader warns about it when opening the file.
*Please note:* I don't really know the JBIG2 image format at all, however from a very brief look at the specification it seems that integers should be 32-bit.
2023-01-19 17:14:17 +01:00
Jonas Jenwald
7976fc7851 [api-minor] Deprecate calling getDocument directly with a PDFDataRangeTransport-instance
In general it's recommended to pass a *parameter object* when calling the `getDocument`-function in the API, since that's the only way to provide additional options, and the fact that it also accepts a URL or TypedArray directly is now mostly for backwards compatibility reasons.
However, the `getDocument`-function also accepts a direct `PDFDataRangeTransport`-instance which just seems unnecessary.

*Please note:* The `PDFDataRangeTransport`-implementation was added specifically for the *built-in* Firefox PDF Viewer, however it's most likely not commonly used by any third-party (given that it requires manual PDF-data loading).
Furthermore, the default-viewer always provides a *parameter object* when calling the `getDocument`-function and it's thus completely unaffected by these changes.
2023-01-19 14:25:55 +01:00
Jonas Jenwald
7b36686fca Update the year in the license_header files 2023-01-18 22:28:18 +01:00
Jonas Jenwald
d6be5141e9 Fallback to using the name table to infer the encoding for TrueType fonts missing such data (issue 15910)
The relevant TrueType font is missing both /ToUnicode *and* /Encoding entires, either of which would have prevented the (current) broken textLayer rendering.
My first idea was that we could use the `post` table in the TrueType font, see https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6post.html, to get the actual glyphNames and amend the fallback ToUnicode-map that way. Unfortunately that didn't work, since the `post` table only contained ".notdef" and "" (i.e. empty string) entries.

Instead we try to use the `name` table in the TrueType font, see https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html, to determine if the platform is Windows and thus fallback to generate a ToUnicode-map from the `WinAnsiEncoding`.
2023-01-17 16:04:51 +01:00
Jonas Jenwald
d8d5545e03
Merge pull request #15926 from Snuffleupagus/annotation-appearance-stream
Ensure that Annotation `appearance`-entries are actually Streams
2023-01-16 15:00:12 +01:00
Jonas Jenwald
cefaecc2e8 Ensure that Annotation appearance-entries are actually Streams
Note how all over the `src/core/annotation.js`-code we're assuming that if an `appearance`-entry exists it's also a Stream. However, we're not actually checking that thoroughly enough which causes issues in some badly generated PDF documents.
2023-01-16 13:02:53 +01:00
Jonas Jenwald
397f943ca3 [api-minor] Enable transferring of TypedArray PDF data by default (PR 15908 follow-up)
This patch removes the recently introduced `transferPdfData` API-option, and simply enables transferring of TypedArray data *by default* instead of copying it. This will help reduce main-thread memory usage, however it will take ownership of the TypedArrays. Currently this only applies to the following cases:
 - TypedArrays passed to the `getDocument`-function in the API, in order to open PDF documents from binary data.
 - TypedArrays passed to a `PDFDataRangeTransport`-instance, used to support custom PDF document fetching/loading (see e.g. the Firefox PDF Viewer).

*PLEASE NOTE:* To avoid being affected by this, please simply *copy* any TypedArray data before passing it to either of the functions/methods mentioned above.

Now that we transfer TypedArray data that we previously only copied, we need to be more careful with input validation. Given how the `{IPDFStreamReader, IPDFStreamRangeReader}.read` methods will always return ArrayBuffer data, which is then transferred to the worker-thread[1], the actual TypedArray data passed to the API thus need to have the same exact size as its underlying ArrayBuffer to prevent issues.
Hence we'll check for this and only allow transferring of *safe* TypedArray data, and fallback to simply copying the data just as before. This obviously shouldn't be an issue in the Firefox PDF Viewer, but for the general PDF.js library we need to be more careful here.

---
[1] See e09ad99973/src/display/api.js (L2492-L2506) respectively e09ad99973/src/display/api.js (L2578-L2590)
2023-01-14 10:39:36 +01:00
Jonas Jenwald
99cfab18c1 Combine the array-like and ArrayBuffer branches, when handling binary data, in getDocument 2023-01-13 13:28:44 +01:00
Jonas Jenwald
e09ad99973
Merge pull request #15916 from Snuffleupagus/fetch-transfer
[api-minor] Enabling transferring of data fetched with the `PDFFetchStream` implementation
2023-01-13 13:28:12 +01:00
Jonas Jenwald
1362cd91d0 Improve input validation in PDFDataTransportStream._onReceiveData (PR 15908 follow-up)
The mozilla-central [method `PdfDataListener.readData`](https://searchfox.org/mozilla-central/rev/893a8f062ec6144c84403fbfb0a57234418b89cf/toolkit/components/pdfjs/content/PdfStreamConverter.jsm#207-210) can return `null`, hence it seems like a very good idea to update `PDFDataTransportStream._onReceiveData` to handle that gracefully since the current code will throw in that case.

Also, improves the JSDocs for the `PDFDataRangeTransport` class in the API.
2023-01-12 15:24:59 +01:00
Jonas Jenwald
cee97fcd15 [api-minor] Enabling transferring of data fetched with the PDFFetchStream implementation
Note how in the API we're transferring the PDF data that's fetched over the network[1]:
 - f28bf23a31/src/display/api.js (L2467-L2480)
 - f28bf23a31/src/display/api.js (L2553-L2564)

To support that functionality we have the `PDFDataTransportStream`, `PDFFetchStream`, `PDFNetworkStream`, and `PDFNodeStream` implementations. Here these stream-implementations vary slightly in how they handle `ArrayBuffer`s internally, w.r.t. transferring or copying the data:
 - In `PDFDataTransportStream` we optionally, after PR 15908, allow transferring of the PDF data as provided externally (used e.g. in the Firefox PDF Viewer).
 - In `PDFFetchStream` we're currenly always copying the PDF data returned by the Fetch API, which seems unnecessary. As discussed in PR 15908, it'd seem very weird if this sort of browser API didn't allow transferring of the returned data.
 - In `PDFNetworkStream` we're already, since many years, transferring the PDF data returned by the `XMLHttpRequest` functionality. Note how the `getArrayBuffer` helper function simply returns an `ArrayBuffer` response as-is.
 - In `PDFNodeStream` we're currently copying the PDF data, however this is unfortunately necessary since Node.js returns data as a `Buffer` object[2].

Given that the `PDFNetworkStream` has been, indirectly, supporting transferring of PDF data for years it would seem really strange if this didn't also apply to the `PDFFetchStream`-implementation.
Hence this patch simply enables transferring of PDF data, when accessed using the Fetch API, unconditionally to help reduced main-thread memory usage since the `PDFFetchStream`-implementation is used *by default* in browsers (for the GENERIC build).

---
[1] As opposed to PDF data being provided as e.g. a TypedArray when calling `getDocument` in the API.

[2] This is a "special" Node.js object, see https://nodejs.org/api/buffer.html#buffer, which doesn't exist in browsers.
2023-01-12 13:59:21 +01:00
Jonas Jenwald
bbe629018d [api-minor] Add a new transferPdfData option to allow transferring more data to the worker-thread (bug 1809164)
Also, removes the `initialData`-parameter JSDocs for the `getDocument`-function given that this parameter has been completely unused since PR 8982 (over five years ago). Note that the `initialData`-parameter is, and always was, intended to be provided when initializing a `PDFDataRangeTransport`-instance.
2023-01-10 21:03:44 +01:00
calixteman
fcaeb5db88
Merge pull request #15901 from calixteman/15289_followup
Avoid null ExpansionFactor in type1 fonts (follow-up of #15289)
2023-01-07 18:20:31 +01:00
Jonas Jenwald
74e4b515c5
Merge pull request #15897 from Snuffleupagus/issue-15893
Support parsing encrypted documents in `XRef.indexObjects` (issue 15893)
2023-01-07 16:55:41 +01:00
Calixte Denizet
c170245fc0 Avoid null ExpansionFactor in type1 fonts (follow-up of #15289) 2023-01-07 16:25:24 +01:00
Calixte Denizet
e565e455e2 Set ExpansionFactor to 0.06 when it's equals to 0 in the private dict of CFF fonts 2023-01-07 14:53:13 +01:00
Tim van der Meij
69113f08f2
Merge pull request #15887 from Snuffleupagus/rm-setPDFNetworkStreamFactory
Inline the `setPDFNetworkStreamFactory` functionality in `src/display/api.js`
2023-01-07 13:16:23 +01:00
Tim van der Meij
b428824269
Merge pull request #15879 from Snuffleupagus/useWorkerFetch-defaults
[api-minor] Improve the `useWorkerFetch` default value checks
2023-01-07 13:13:25 +01:00
Jonas Jenwald
1d5de9f4f4 Inline the setPDFNetworkStreamFactory functionality in src/display/api.js
Given that this is internal functionality, not exposed in the official API, it's not entirely clear (at least to me) why we can't just initialize this directly in `src/display/api.js` instead.
When testing both the development viewer and all the ways in which we run tests, everthing still appears to work just fine with this patch.
2023-01-06 13:23:07 +01:00
Jonas Jenwald
7d94fdeb48 Support parsing encrypted documents in XRef.indexObjects (issue 15893)
*Please note:* The reduced test-case is *not* a perfect reproduction of the original PDF document, since this one fails to open in e.g. Adobe Reader, but I do believe that it captures the most important points here.

For corrupt *and* encrypted PDF documents, it's possible that only some trailer dictionaries actually contain an /Encrypt-entry. Previously we'd could easily miss that, since we generally pick the first not obviously corrupt trailer dictionary, and the solution implemented here is to simply pre-parse all trailer dictionaries to see if there's any /Encrypt-entries.
2023-01-06 13:09:37 +01:00
Calixte Denizet
dea2471e96 [JS] UserActivation must be enabled before running document actions
else auto-print is broken (it's a regression from patch #15822).
2023-01-04 21:26:36 +01:00
Jonas Jenwald
6bdbb5c5ca Update the type/subtype at the end of font parsing
This fixes a warning reported by CodeQL, and should also make general sense given that we parse the font-data to determine the *actual* `type`/`subtype` rather than trusting the PDF document.
2023-01-02 16:21:48 +01:00
Jonas Jenwald
1a69d537c1 [api-minor] Limit the PDFDocumentLoadingTask.onUnsupportedFeature functionality to GENERIC builds (PR 15758 follow-up)
This was deprecated in PR 15758 but it's unfortunately quite difficult to tell if third-party users are depending on this, e.g. to implement custom error reporting, and if so to what extent.
However, thanks to the pre-processor we can limit *most* of this code to GENERIC builds which still seem like a worthwhile change.

These changes reduce the bundle size of the Firefox PDF Viewer by 3.8 kB in total.
2023-01-01 17:53:12 +01:00
Jonas Jenwald
0c1fb4e740 [api-minor] Remove the PDFDocumentProxy.stats getter (PR 15758 follow-up)
This was deprecated in PR 15758 and given that it's quite unlikely that any third-party users are relying on this functionality, since it was only ever added to support telemetry reporting in the Firefox PDF Viewer, it should hopefully be fine to remove this fairly quickly.

These changes reduce the bundle size of the Firefox PDF Viewer by 4.5 kB in total.
2023-01-01 17:06:47 +01:00
Jonas Jenwald
2c57a4232c [api-minor] Improve the useWorkerFetch default value checks
Given that the Fetch API only supports the http/https protocols, worker-thread fetching of CMaps and Standard-fonts may thus fail in certain cases. To improve the default behaviour we'll now also check that the `cMapUrl` and `standardFontDataUrl` options are appropriate, except in Firefox where this should always work.
2023-01-01 14:48:28 +01:00
Jonas Jenwald
3110d1f29a
Merge pull request #15869 from Snuffleupagus/_abortOperatorList-clearTimeout
Always abort a pending `streamReader` cancel timeout in `PDFPageProxy._abortOperatorList` (PR 15825 follow-up)
2022-12-27 13:26:43 +01:00
Jonas Jenwald
841abb53e6 Remove PDFPageProxy.getJSActions caching, since it's unused, in the API
Note how, in the scripting initialization in the viewer, we only ever invoke `PDFPageProxy.getJSActions` *once* per page in order to improve overall performance; see a575aa13b9/web/pdf_scripting_manager.js (L372-L375)

Hence it really shouldn't be necessary to cache its result in the API, especially when that is done *manually* rather than using something like `shadow`.
2022-12-27 10:39:33 +01:00
Jonas Jenwald
ae24dbd064 Always abort a pending streamReader cancel timeout in PDFPageProxy._abortOperatorList (PR 15825 follow-up)
When we're destroying a `PDFPageProxy`-instance, during full document destruction, we'll force-abort any worker-thread parsing of operatorLists. Hence we should make sure that any pending cancel timeout is always aborted, since a later `PDFPageProxy._abortOperatorList` call should always "replace" a previous one.

*Please note:* Technically this was always wrong, but with the changes in PR 15825 it became *ever so slightly* easier to trigger this thanks to the potentially longer timeout.
2022-12-27 10:19:39 +01:00
Jonas Jenwald
2fcf8bb5be Re-factor searching for incomplete objects in XRef.indexObjects (issue 15803)
When trying to find incomplete objects, i.e. those missing the "endobj"-string at the end, there's unfortunately a number of possible operators that we need to check for. Otherwise we could miss e.g. the "trailer" at the end of a corrupt PDF document, which is why the referenced document didn't work.

Currently we do all searching on the "raw" bytes of the PDF document, for efficiency, however this doesn't really work when we need to check for *multiple* potential command-strings. To keep the complexity manageable we'll instead use regular expressions here, but we can at least avoid creating lots of substrings thanks to the `RegExp.lastIndex` property; which is well supported across browsers according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex#browser_compatibility

Note that this repeated regular expression usage could perhaps be slightly less efficient than the old code, however this method is only invoked for corrupt PDF documents.
2022-12-19 23:01:09 +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
a84d14b382 [Editor] Avoid to scroll when an annotation is commited (fixes issue #15744) 2022-12-17 13:48:19 +01:00
calixteman
cb212b24fd
Merge pull request #15841 from calixteman/15784
Strip out a reserved operator (9) from CFF char strings (fixes issue #15784)
2022-12-16 15:55:02 +01:00
Calixte Denizet
f80880ccaa Strip out a reserved operator (9) from CFF char strings (fixes issue #15784) 2022-12-16 15:17:46 +01:00
Jonas Jenwald
0c83bebf03
Merge pull request #15832 from Snuffleupagus/issue-15828
Attempt to expose `OnProgressParameters` in the TypeScript definitions (issue 15828)
2022-12-16 12:44:29 +01:00
Jonas Jenwald
26135b0313 Always parse the entire startXRefQueue in XRef.readXRef (issue 15833)
Previously we'd abort all parsing if an Error was encountered, despite the fact that multiple `startXRefQueue`-entries may be available and that continued parsing could thus eventually be able to find usable data.

Note that in the referenced PDF document the `startxref`-operator, at the end of the file, points to a position in the middle of an arbitrary `stream` which is why things break.
2022-12-15 13:46:28 +01:00
Jonas Jenwald
0ef72044e2 Attempt to expose OnProgressParameters in the TypeScript definitions (issue 15828)
Hopefully this works, since as usual I don't really know anything about TypeScript...
2022-12-14 21:36:31 +01:00
Jonas Jenwald
506bbb7283
Merge pull request #15825 from Snuffleupagus/cancel-extraDelay
[api-minor] Allow specifying an extra-delay, in `RenderTask.cancel`, for worker-thread aborting of operatorList parsing
2022-12-14 19:26:39 +01:00
Jonas Jenwald
91524d1a60 [api-minor] Allow specifying an extra-delay, in RenderTask.cancel, for worker-thread aborting of operatorList parsing
This is done to support upcoming viewer-changes, and in order to prevent third-party users from outright breaking things we'll simply ignore too large values.
2022-12-14 12:34:16 +01:00
Jonas Jenwald
dcf9ff2182 Handle possibly undefined parameters *once* per AnnotationLayer.render invocation
There's no reason to repeat this for every single annotation. Also, adds a couple of missing JSDoc-parameters.
2022-12-14 12:23:24 +01:00
Calixte Denizet
2ebf8745a2 [JS] Run the named actions before running the format when the file is open (issue #15818)
It's a follow-up of #14950: some format actions are ran when the document is open
but we must be sure we've everything ready for that, hence we have to run some
named actions before runnig the global format.
In playing with the form, I discovered that the blur event wasn't triggered when
JS called `setFocus` (because in such a case the mouse was never down). So I removed
the mouseState thing to just use the correct commitKey when blur is triggered by a
TAB key.
2022-12-13 21:12:32 +01:00
Calixte Denizet
0c1ec946aa [JS] Handle correctly choice widgets where the display and the export values are different (issue #15815) 2022-12-13 19:08:26 +01:00
Calixte Denizet
1a397681fe The annotation layer dimensions must be set before adding some elements (follow-up of #15770)
In order to move the annotations in the DOM to have something which corresponds
to the visual order, we need to have their dimensions/positions which means that
the parent must have some dimensions.
2022-12-13 14:54:45 +01:00
Jonas Jenwald
cafdc48147 [api-minor] Add a new PageViewport-getter to access the original, un-scaled, viewport dimensions
While reviewing recent patches, I couldn't help but noticing that we now have a lot of call-sites that manually access the `PageViewport.viewBox`-property.
Rather than repeating that verbatim all over the code-base, this patch adds a lazily computed and cached getter for this data instead.
2022-12-11 18:37:35 +01:00
Jonas Jenwald
9b6d0d994d Remove the API-caching of annotation-data
This was essentially done only to compensate for the viewer calling `PDFPageProxy.getAnnotations` unconditionally on every annotationLayer-rendering invocation. With the previous patch that's no longer happening, and this API-caching should thus no longer be necessary.
2022-12-11 18:12:10 +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
Calixte Denizet
4f0bfabe7a Take all the viewBox into account when computing the coordinates of an annotation in the page (fixes #15789) 2022-12-08 15:02:20 +01:00
Calixte Denizet
b93bf9f654 [Editor] Don't use the editor parent which can be null.
An annotation editor layer can be destroyed when it's invisible, hence some
annotations can have a null parent but when printing/saving or when changing
font size, color, ... of all added annotations (when selected with ctrl+a) we
still need to have some parent properties especially the page dimensions, global
scale factor and global rotation angle.
This patch aims to remove all the references to the parent in the editor instances
except in some cases where an editor should obviously have one.
It fixes #15780.
2022-12-08 14:06:06 +01:00
Calixte Denizet
9af89381cd [Editor] Add a very basic and incomplete workaround for issue #15780
The main issue is due to the fact that an editor's parent can be null when
we want to serialize it and that lead to an exception which break all the
saving/printing process.
So this incomplete patch fixes only the saving/printing issue but not the
underlying problem (i.e. having a null parent) and doesn't bring that much
complexity, so it should help to uplift it the next Firefox release.
2022-12-06 16:22:24 +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
0274245e90 Remove the unused TextLayerRenderTask._renderingDone property (PR 15259 follow-up)
This is yet another property that I forgot to remove in PR 15259.
2022-12-05 11:49:14 +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
Tim van der Meij
67e1c37e0f
Merge pull request #15773 from Snuffleupagus/view-worker-normalize
[api-minor] Normalize the `view`-getter on the worker-thread
2022-12-02 19:52:44 +01:00
Tim van der Meij
99cfef882f
Merge pull request #15752 from Snuffleupagus/no-typeof-undefined
Enable the `no-typeof-undefined` ESLint plugin rule
2022-12-02 19:48:16 +01:00
Jonas Jenwald
5f8598abb7 [api-minor] Normalize the view-getter on the worker-thread
*Please note:* I don't really expect that this is will be an observable change, since virtually all PDF documents already order e.g. /MediaBox and /CropBox entries correctly.

By normalizing boundingBoxes already on the worker-thread, we can be sure that even a corrupt document won't cause issues.
Note how we're passing the `view`-getter to the `PartialEvaluator.getTextContent` method, in order to detect textContent which is outside of the page, hence it makes sense to ensure that it's formatted as expected.
Furthermore, by normalizing this once on the worker-tread we should no longer have to worry about a possibly negative width/height in the `PageViewport` constructor.

Finally, the patch also simplifies the `view`-getter a little bit.
2022-12-02 15:46:39 +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
Jonas Jenwald
47dbfc4ade Enable the no-typeof-undefined ESLint plugin rule
Please see https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-typeof-undefined.md
2022-12-01 18:20:39 +01:00
Jonas Jenwald
fa54a58790
Merge pull request #15765 from Snuffleupagus/rm-textLayer-timeout
[api-minor] Remove the TextLayer `timeout` parameter (PR 15742 follow-up)
2022-11-29 21:21:45 +01:00
calixteman
f3206b351f
Merge pull request #15764 from calixteman/15753
[Annotation] Send correctly the updated values to the JS sandbox
2022-11-29 20:04:12 +01:00
Jonas Jenwald
7c25b1b455 [api-minor] Remove the TextLayer timeout parameter (PR 15742 follow-up)
The deprecation is included in the current release, i.e. version `3.1.81`, and given the edge-case nature of this option I really don't think that we need to keep it deprecated for multiple releases.
2022-11-29 19:57:38 +01:00
Calixte Denizet
20fd9099f8 [Annotation] Send correctly the updated values to the JS sandbox 2022-11-29 17:34:06 +01:00
Jonas Jenwald
1f082d3e1d
Merge pull request #15761 from Snuffleupagus/platform
Stop duplicating the `platform` getter in multiple files
2022-11-29 17:32:52 +01:00
Jonas Jenwald
0d648f531b Ignore PDF documents opened from "data:"-URLs when handling internal links (bug 1803050)
This patch has been successfully tested in a local, artifact, Firefox build.

*Please note:* The only thing that'll no longer work for PDF documents opened using "data:"-URLs is middle-clicking on internal/outline links, in order to open the destination in a new tab. This is however an extremely small loss of functionality, and as can be seen in the bug the alternative (i.e. doing nothing) is surely much worse.
2022-11-29 14:08:01 +01:00
Jonas Jenwald
82d127883d Stop duplicating the platform getter in multiple files
Currently both of the `AnnotationElement` and `KeyboardManager` classes contain *identical* `platform` getters, which seems like unnecessary duplication.
With the pre-processor we can also limit the feature-testing to only GENERIC builds, since `navigator` should always be available in browsers.
2022-11-29 12:14:40 +01:00
calixteman
44bc315444
Merge pull request #15758 from calixteman/cleanup_telemetry
[api-minor] Remove all the useless telemetry stuff in the viewer (bug 1802468)
2022-11-28 21:54:00 +01:00
Calixte Denizet
b9cb651c44 [api-minor] Remove all the useless telemetry stuff in the viewer (bug 1802468)
Add a deprecation notification for PDFDocumentLoadingTask.onUnsupportedFeature and PDFDocumentProxy.stats
which are likely useless.
The unsupported feature stuff have initially been added in (#4048) in order to be able to display a
warning bar and to help to have some numbers to know how a feature was used.
Those data are no more used in Firefox.
2022-11-28 20:55:15 +01:00
Calixte Denizet
ae7da6ae48 [JS] By default, a text field value must be treated as a number (bug 1802888) 2022-11-28 16:24:01 +01:00
calixteman
33f9d1aab2
Merge pull request #15755 from calixteman/rounding_printf
[JS] Fix a rounding issue in printf (bug 1802888)
2022-11-28 15:39:36 +01:00
Calixte Denizet
4ee0c83548 [JS] Fix a rounding issue in printf (bug 1802888) 2022-11-28 14:37:15 +01:00
Jonas Jenwald
85f03c0ea4 Slightly modernize the FontLoader.isSyncFontLoadingSupported getter
This is very old code, which is unused (by default) in browsers nowadays since the Font Loading API will always be preferred.
For Node.js environments we use the same constant as elsewhere throughout the code-base, and we can also simplify the Firefox-specific check given that the lowest supported version is `102` (as of this writing).

Finally the old TODO is removed, since the general availability of the Font Loading API has made it redundant.
2022-11-27 12:19:11 +01:00
Jonas Jenwald
aa5b678f94 Add default icons for FileAttachment annotations (bug 1230933)
*Please note:* This "borrows" the icons from Thunderbird.

According to the PDF specification, see https://web.archive.org/web/20220309040754if_/https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf#G11.2096626, we should be providing default icons for FileAttachment annotations without appearances.
2022-11-26 11:24:59 +01:00
Jonas Jenwald
4b02610e8c Re-factor and simplify the getQuadPoints helper function
The use of `Array.prototype.reduce()` is, in my opinion, hurting overall readability since it's not particularly easy to look at the relevant code and immediately understand what's going on here. Furthermore this code leads to strictly speaking unnecessary allocations and parsing, since we could just track the min/max values directly in the relevant loop instead.
2022-11-25 10:40:16 +01:00
Jonas Jenwald
b3e161c328 [api-minor] Deprecate the TextLayer timeout parameter
This has never really been used anywhere within the PDF.js library[1], and when streaming of textContent was introduced this parameter was effectively made redundant.
Note that when streaming of textContent is used, all text-layout has already happened by the time that this `timeout`-functionality is actually invoked (thus making it pointless).
While the `timeout`-functionality may still "work" when the textContent is provided upfront, although it's never been used/tested, streaming will generally perform better (in e.g. a viewer setting).

*Please note:* While unrelated here, also removes a now unused property that I forgot in PR 15259.

---
[1] At least not since the code was moved into its current file, which happened in PR 6619 and landed seven years ago.
2022-11-24 23:08:39 +01:00
Jonas Jenwald
8fda3f04fe
Merge pull request #15732 from Snuffleupagus/issue-15719
Add a fallback for non-embedded *composite* Tahoma fonts (issue 15719)
2022-11-24 19:09:12 +01:00
Jonas Jenwald
d1c01b3164 Add a fallback for non-embedded *composite* Tahoma fonts (issue 15719) 2022-11-23 15:51:18 +01:00
Jonas Jenwald
47682985d3 Add support for Optional Content in TilingPatterns (issue 15716)
This can't be a particularly common feature, since we've supported Optional Content for over two years and this is the very first TilingPattern-case we've seen.
2022-11-23 12:58:00 +01:00
Jonas Jenwald
f3e0f86641 Simplify the getFilenameFromUrl helper function 2022-11-23 11:48:08 +01:00
Jonas Jenwald
0ba242ea4a Support FileAttachments with hash-signs in the filename (issue 15729)
The reason for the issue is that we use the generic `getFilenameFromUrl` helper function, which was originally intended for regular URLs.
For the filenames we're dealing with in FileAttachments, we really only want to strip the path when one exists[1].

---
[1] See [bug 1230933](https://bugzilla.mozilla.org/show_bug.cgi?id=1230933) for an example of such a case.
2022-11-23 10:47:33 +01:00
Jonas Jenwald
2ff9799e7a Tweak assignment of common parameters in the Annotation classes
This is slightly more compact, and also unifies the format across the various classes.
2022-11-20 12:29:59 +01:00
Jonas Jenwald
c92de947b6 Reduce duplication when creating a fallback appearance for MarkupAnnotations
Currently we repeat the same color-conversion code verbatim in lots of classes, which seems completely unnecessary.
2022-11-20 12:05:25 +01:00
Tim van der Meij
d6908ee145
Merge pull request #15701 from Snuffleupagus/move-string-helpers
Move some string helper functions to the worker-thread
2022-11-19 11:20:07 +01:00