Okular: Add support for JavaScript AFSimple_Calculate and textfield content calculation
Closed, ResolvedPublic


AFSimple_Calculate (doc) is the default function for calculate action's that is added when modifying "Calculate" in the Text Field Properties in Adobe Acrobat Pro:

(This is how you add the calculate function in Adobe)

The function itself is simple and easy to implement in JavaScript. But currently Okular's JavaScript executor runs in an Exception that AFSimple_Calculate is unknown.

For Okular most things to handle this are already in place, we evaluate the calculate already and execute the javascript in there. But to handle this properly I think four things are needed:

  1. Using the result of a JavaScript calculate action to set the value of text fields.
  2. Updating the text field based on the result of a calculate action.
  3. A way to add built-in JavaScript functions.
  4. Some way to handle localized numbers. (e.g. in German 1.3 is 1,3)
  5. A unit test :-)

Example document:

I have a patch for everything, which I will add as differentials, except localized number handling. For this I need to experiment a bit with where this would be ideally handled, as I would like to do it somehow with Qt and not in JavaScript.

Here is a behavior comparison between Adobe and Okular (with the patches):

aheinecke triaged this task as Wishlist priority.


Nice! Does this take care of https://bugs.kde.org/show_bug.cgi?id=271728

Indeed this bug should be fixed by this, but no it isnt, the scripts in there set event.value = "somecalculation". Instead of the return value.

I've seen this event.value syntax somewhere else already. This makes me think that I might be on the wrong path with D10047 and should somehow use an event object to communicate results instead of a return value from a script.

I'll look into it.


This example document contains so many different things that I stopped looking but I don't think that this bug will be fixed with it. Might be improved, though.

Ok. RTFM :-) http://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/Acro6JS.pdf page 202ff say something about the Event object and calculate events. So using the return value as the field value is wrong. This needs some event object handling.

The differentials are updated now with some better understanding. :-)
Patch 1 is now a bit larger as it introduces a new Event Object and exposes that through KJS. As I understand it the Context Object is basically a data exchange Object from the Application to JavaScript and Vice Versa. It's supported in many JavaScript Contexts. I think the suggested implementation here is flexible enough so that it can be easily extended for other events in the future.

The test document and the autotest added here still works.

Sadly bug 271728 is not fixed by this. I'll comment in that bug why that is the case.

D10547 Makes undo / redo work nicely with this.

Anything else I have overlooked? Otherwise I think this series is finally ready :-)

aheinecke moved this task from Backlog to Done on the Okular board.Feb 22 2018, 7:08 AM
aheinecke closed this task as Resolved.Mar 23 2018, 6:43 AM

Forgotten to resolve this.