From cead2bbef3c2878dfb42173f9885e8ab8a23cd86 Mon Sep 17 00:00:00 2001 From: Andre Heinecke Date: Wed, 21 Feb 2018 23:56:51 +0100 Subject: [PATCH] Add initial support for built-in javascript functs Summary: This starts by adding support for AFSimple_Calculate which is the standard "calculate" function in Adobe Acrobat Pro. Reviewers: #okular Subscribers: aacid Tags: #okular Maniphest Tasks: T7805 Differential Revision: https://phabricator.kde.org/D10049 --- CMakeLists.txt | 4 +++ core/script/builtin.js | 62 +++++++++++++++++++++++++++++++++++++++++ core/script/builtin.qrc | 5 ++++ core/scripter.cpp | 18 +++++++++++- 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 core/script/builtin.js create mode 100644 core/script/builtin.qrc diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b575972d..a8d416f88 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,6 +175,10 @@ set(okularcore_SRCS core/synctex/synctex_parser_utils.c ) +qt5_add_resources(okularcore_SRCS + core/script/builtin.qrc +) + ki18n_wrap_ui(okularcore_SRCS conf/textdocumentsettings.ui ) diff --git a/core/script/builtin.js b/core/script/builtin.js new file mode 100644 index 000000000..2090c942d --- /dev/null +++ b/core/script/builtin.js @@ -0,0 +1,62 @@ +/*************************************************************************** + * Copyright (C) 2018 Intevation GmbH * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +/* Builtin functions for Okular's PDF JavaScript interpretation. */ + +/** AFSimple_Calculate + * + * cFunction is a string that identifies the operation. + * It is one of AVG, SUM, PRD, MIN, MAX + * cFields is an array of the names of the fields used to calculate. + */ +function AFSimple_Calculate( cFunction, cFields ) +{ + var ret = 0; + + if ( cFunction === "PRD" ) + { + ret = 1; + } + + for (i = 0; i < cFields.length; i++) + { + var field = Doc.getField( cFields[i] ); + var val = Number( field.value ); + + if ( cFunction === "SUM" || cFunction === "AVG" ) + { + ret += val; + } + else if ( cFunction === "PRD" ) + { + ret *= val; + } + else if ( cFunction === "MIN" ) + { + if ( i === 0 || val < ret ) + { + ret = val; + } + } + else if ( cFunction === "MAX" ) + { + if ( i === 0 || val > ret ) + { + ret = val; + } + } + } + + if ( cFunction === "AVG" ) + { + ret /= cFields.length; + } + + event.value = ret; +} diff --git a/core/script/builtin.qrc b/core/script/builtin.qrc new file mode 100644 index 000000000..9c83d4f61 --- /dev/null +++ b/core/script/builtin.qrc @@ -0,0 +1,5 @@ + + + builtin.js + + diff --git a/core/scripter.cpp b/core/scripter.cpp index 15af8766e..431dff433 100644 --- a/core/scripter.cpp +++ b/core/scripter.cpp @@ -10,6 +10,7 @@ #include "scripter.h" #include +#include #include "debug_p.h" #include "script/executor_kjs_p.h" @@ -53,6 +54,21 @@ QString Scripter::execute( ScriptType type, const QString &script ) else qDebug() << script.left( 1000 ) << "[...]"; #endif + static QString builtInScript; + if ( builtInScript.isNull() ) + { + QFile builtInResource ( ":/script/builtin.js" ); + if (!builtInResource.open( QIODevice::ReadOnly )) + { + qCDebug(OkularCoreDebug) << "failed to load builtin script"; + } + else + { + builtInScript = QString::fromUtf8( builtInResource.readAll() ); + builtInResource.close(); + } + } + switch ( type ) { case JavaScript: @@ -60,7 +76,7 @@ QString Scripter::execute( ScriptType type, const QString &script ) { d->m_kjs = new ExecutorKJS( d->m_doc ); } - d->m_kjs->execute( script, d->m_event ); + d->m_kjs->execute( builtInScript + script, d->m_event ); break; } return QString();