diff options
Diffstat (limited to 'fxjs')
83 files changed, 14677 insertions, 0 deletions
@@ -1,4 +1,10 @@ include_rules = [ + '+core/fdrm', + '+core/fpdfapi', + '+core/fpdfdoc', '+core/fxcrt', + '+core/fxge', + '+public', + '+fpdfsdk', '+v8/include', ] diff --git a/fxjs/JS_Define.cpp b/fxjs/JS_Define.cpp new file mode 100644 index 0000000000..90f7557422 --- /dev/null +++ b/fxjs/JS_Define.cpp @@ -0,0 +1,308 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/JS_Define.h" + +#include <time.h> + +#include <algorithm> +#include <cmath> +#include <limits> +#include <vector> + +#include "fxjs/cjs_document.h" +#include "fxjs/cjs_object.h" + +namespace { + +double GetLocalTZA() { + if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) + return 0; + time_t t = 0; + time(&t); + localtime(&t); +#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ + // In gcc 'timezone' is a global variable declared in time.h. In VC++, that + // variable was removed in VC++ 2015, with _get_timezone replacing it. + long timezone = 0; + _get_timezone(&timezone); +#endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ + return (double)(-(timezone * 1000)); +} + +int GetDaylightSavingTA(double d) { + if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) + return 0; + time_t t = (time_t)(d / 1000); + struct tm* tmp = localtime(&t); + if (!tmp) + return 0; + if (tmp->tm_isdst > 0) + // One hour. + return (int)60 * 60 * 1000; + return 0; +} + +double Mod(double x, double y) { + double r = fmod(x, y); + if (r < 0) + r += y; + return r; +} + +bool IsLeapYear(int year) { + return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0)); +} + +int DayFromYear(int y) { + return (int)(365 * (y - 1970.0) + floor((y - 1969.0) / 4) - + floor((y - 1901.0) / 100) + floor((y - 1601.0) / 400)); +} + +double TimeFromYear(int y) { + return 86400000.0 * DayFromYear(y); +} + +static const uint16_t daysMonth[12] = {0, 31, 59, 90, 120, 151, + 181, 212, 243, 273, 304, 334}; +static const uint16_t leapDaysMonth[12] = {0, 31, 60, 91, 121, 152, + 182, 213, 244, 274, 305, 335}; + +double TimeFromYearMonth(int y, int m) { + const uint16_t* pMonth = IsLeapYear(y) ? leapDaysMonth : daysMonth; + return TimeFromYear(y) + ((double)pMonth[m]) * 86400000; +} + +int Day(double t) { + return static_cast<int>(floor(t / 86400000.0)); +} + +int YearFromTime(double t) { + // estimate the time. + int y = 1970 + static_cast<int>(t / (365.2425 * 86400000.0)); + if (TimeFromYear(y) <= t) { + while (TimeFromYear(y + 1) <= t) + y++; + } else { + while (TimeFromYear(y) > t) + y--; + } + return y; +} + +int DayWithinYear(double t) { + int year = YearFromTime(t); + int day = Day(t); + return day - DayFromYear(year); +} + +int MonthFromTime(double t) { + int day = DayWithinYear(t); + int year = YearFromTime(t); + if (0 <= day && day < 31) + return 0; + if (31 <= day && day < 59 + IsLeapYear(year)) + return 1; + if ((59 + IsLeapYear(year)) <= day && day < (90 + IsLeapYear(year))) + return 2; + if ((90 + IsLeapYear(year)) <= day && day < (120 + IsLeapYear(year))) + return 3; + if ((120 + IsLeapYear(year)) <= day && day < (151 + IsLeapYear(year))) + return 4; + if ((151 + IsLeapYear(year)) <= day && day < (181 + IsLeapYear(year))) + return 5; + if ((181 + IsLeapYear(year)) <= day && day < (212 + IsLeapYear(year))) + return 6; + if ((212 + IsLeapYear(year)) <= day && day < (243 + IsLeapYear(year))) + return 7; + if ((243 + IsLeapYear(year)) <= day && day < (273 + IsLeapYear(year))) + return 8; + if ((273 + IsLeapYear(year)) <= day && day < (304 + IsLeapYear(year))) + return 9; + if ((304 + IsLeapYear(year)) <= day && day < (334 + IsLeapYear(year))) + return 10; + if ((334 + IsLeapYear(year)) <= day && day < (365 + IsLeapYear(year))) + return 11; + + return -1; +} + +int DateFromTime(double t) { + int day = DayWithinYear(t); + int year = YearFromTime(t); + int leap = IsLeapYear(year); + int month = MonthFromTime(t); + switch (month) { + case 0: + return day + 1; + case 1: + return day - 30; + case 2: + return day - 58 - leap; + case 3: + return day - 89 - leap; + case 4: + return day - 119 - leap; + case 5: + return day - 150 - leap; + case 6: + return day - 180 - leap; + case 7: + return day - 211 - leap; + case 8: + return day - 242 - leap; + case 9: + return day - 272 - leap; + case 10: + return day - 303 - leap; + case 11: + return day - 333 - leap; + default: + return 0; + } +} + +} // namespace + +double JS_GetDateTime() { + if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) + return 0; + time_t t = time(nullptr); + struct tm* pTm = localtime(&t); + + int year = pTm->tm_year + 1900; + double t1 = TimeFromYear(year); + + return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 + + pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0; +} + +int JS_GetYearFromTime(double dt) { + return YearFromTime(dt); +} + +int JS_GetMonthFromTime(double dt) { + return MonthFromTime(dt); +} + +int JS_GetDayFromTime(double dt) { + return DateFromTime(dt); +} + +int JS_GetHourFromTime(double dt) { + return (int)Mod(floor(dt / (60 * 60 * 1000)), 24); +} + +int JS_GetMinFromTime(double dt) { + return (int)Mod(floor(dt / (60 * 1000)), 60); +} + +int JS_GetSecFromTime(double dt) { + return (int)Mod(floor(dt / 1000), 60); +} + +double JS_LocalTime(double d) { + return d + GetLocalTZA() + GetDaylightSavingTA(d); +} + +double JS_DateParse(const WideString& str) { + v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); + v8::Isolate::Scope isolate_scope(pIsolate); + v8::HandleScope scope(pIsolate); + + v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); + + // Use the built-in object method. + v8::Local<v8::Value> v = + context->Global() + ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", + v8::NewStringType::kNormal) + .ToLocalChecked()) + .ToLocalChecked(); + if (v->IsObject()) { + v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked(); + v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse", + v8::NewStringType::kNormal) + .ToLocalChecked()) + .ToLocalChecked(); + if (v->IsFunction()) { + v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v); + const int argc = 1; + v8::Local<v8::Value> timeStr = + CJS_Runtime::CurrentRuntimeFromIsolate(pIsolate)->NewString( + str.AsStringView()); + v8::Local<v8::Value> argv[argc] = {timeStr}; + v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); + if (v->IsNumber()) { + double date = v->ToNumber(context).ToLocalChecked()->Value(); + if (!std::isfinite(date)) + return date; + return JS_LocalTime(date); + } + } + } + return 0; +} + +double JS_MakeDay(int nYear, int nMonth, int nDate) { + double y = static_cast<double>(nYear); + double m = static_cast<double>(nMonth); + double dt = static_cast<double>(nDate); + double ym = y + floor(m / 12); + double mn = Mod(m, 12); + double t = TimeFromYearMonth(static_cast<int>(ym), static_cast<int>(mn)); + if (YearFromTime(t) != ym || MonthFromTime(t) != mn || DateFromTime(t) != 1) + return std::nan(""); + + return Day(t) + dt - 1; +} + +double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) { + double h = static_cast<double>(nHour); + double m = static_cast<double>(nMin); + double s = static_cast<double>(nSec); + double milli = static_cast<double>(nMs); + return h * 3600000 + m * 60000 + s * 1000 + milli; +} + +double JS_MakeDate(double day, double time) { + if (!std::isfinite(day) || !std::isfinite(time)) + return std::nan(""); + + return day * 86400000 + time; +} + +std::vector<v8::Local<v8::Value>> ExpandKeywordParams( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& originals, + size_t nKeywords, + ...) { + ASSERT(nKeywords); + + std::vector<v8::Local<v8::Value>> result(nKeywords, v8::Local<v8::Value>()); + size_t size = std::min(originals.size(), nKeywords); + for (size_t i = 0; i < size; ++i) + result[i] = originals[i]; + + if (originals.size() != 1 || !originals[0]->IsObject() || + originals[0]->IsArray()) { + return result; + } + result[0] = v8::Local<v8::Value>(); // Make unknown. + + v8::Local<v8::Object> pObj = pRuntime->ToObject(originals[0]); + va_list ap; + va_start(ap, nKeywords); + for (size_t i = 0; i < nKeywords; ++i) { + const wchar_t* property = va_arg(ap, const wchar_t*); + v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property); + if (!v8Value->IsUndefined()) + result[i] = v8Value; + } + va_end(ap); + + return result; +} diff --git a/fxjs/JS_Define.h b/fxjs/JS_Define.h new file mode 100644 index 0000000000..6de56f40e0 --- /dev/null +++ b/fxjs/JS_Define.h @@ -0,0 +1,166 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_JS_DEFINE_H_ +#define FXJS_JS_DEFINE_H_ + +#include <vector> + +#include "fxjs/cjs_object.h" +#include "fxjs/cjs_return.h" +#include "fxjs/fxjs_v8.h" +#include "fxjs/js_resources.h" + +double JS_GetDateTime(); +int JS_GetYearFromTime(double dt); +int JS_GetMonthFromTime(double dt); +int JS_GetDayFromTime(double dt); +int JS_GetHourFromTime(double dt); +int JS_GetMinFromTime(double dt); +int JS_GetSecFromTime(double dt); +double JS_LocalTime(double d); +double JS_DateParse(const WideString& str); +double JS_MakeDay(int nYear, int nMonth, int nDay); +double JS_MakeTime(int nHour, int nMin, int nSec, int nMs); +double JS_MakeDate(double day, double time); + +// Some JS methods have the bizarre convention that they may also be called +// with a single argument which is an object containing the actual arguments +// as its properties. The varying arguments to this method are the property +// names as wchar_t string literals corresponding to each positional argument. +// The result will always contain |nKeywords| value, with unspecified ones +// being set to type VT_unknown. +std::vector<v8::Local<v8::Value>> ExpandKeywordParams( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& originals, + size_t nKeywords, + ...); + +// All JS classes have a name, an object defintion ID, and the ability to +// register themselves with FXJS_V8. We never make a BASE class on its own +// because it can't really do anything. + +// Rich JS classes provide constants, methods, properties, and the ability +// to construct native object state. + +template <class T, class A> +static void JSConstructor(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj) { + CJS_Object* pObj = new T(obj); + pObj->SetEmbedObject(new A(pObj)); + pEngine->SetObjectPrivate(obj, pObj); + pObj->InitInstance(static_cast<CJS_Runtime*>(pEngine)); +} + +template <class T> +static void JSDestructor(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj) { + delete static_cast<T*>(pEngine->GetObjectPrivate(obj)); +} + +template <class C, CJS_Return (C::*M)(CJS_Runtime*)> +void JSPropGetter(const char* prop_name_string, + const char* class_name_string, + v8::Local<v8::String> property, + const v8::PropertyCallbackInfo<v8::Value>& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); + CJS_Return result = (pObj->*M)(pRuntime); + if (result.HasError()) { + pRuntime->Error(JSFormatErrorString(class_name_string, prop_name_string, + result.Error())); + return; + } + + if (result.HasReturn()) + info.GetReturnValue().Set(result.Return()); +} + +template <class C, CJS_Return (C::*M)(CJS_Runtime*, v8::Local<v8::Value>)> +void JSPropSetter(const char* prop_name_string, + const char* class_name_string, + v8::Local<v8::String> property, + v8::Local<v8::Value> value, + const v8::PropertyCallbackInfo<void>& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); + CJS_Return result = (pObj->*M)(pRuntime, value); + if (result.HasError()) { + pRuntime->Error(JSFormatErrorString(class_name_string, prop_name_string, + result.Error())); + } +} + +template <class C, + CJS_Return (C::*M)(CJS_Runtime*, + const std::vector<v8::Local<v8::Value>>&)> +void JSMethod(const char* method_name_string, + const char* class_name_string, + const v8::FunctionCallbackInfo<v8::Value>& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + std::vector<v8::Local<v8::Value>> parameters; + for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) + parameters.push_back(info[i]); + + C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); + CJS_Return result = (pObj->*M)(pRuntime, parameters); + if (result.HasError()) { + pRuntime->Error(JSFormatErrorString(class_name_string, method_name_string, + result.Error())); + return; + } + + if (result.HasReturn()) + info.GetReturnValue().Set(result.Return()); +} + +#define JS_STATIC_PROP(err_name, prop_name, class_name) \ + static void get_##prop_name##_static( \ + v8::Local<v8::String> property, \ + const v8::PropertyCallbackInfo<v8::Value>& info) { \ + JSPropGetter<class_name, &class_name::get_##prop_name>( \ + #err_name, #class_name, property, info); \ + } \ + static void set_##prop_name##_static( \ + v8::Local<v8::String> property, v8::Local<v8::Value> value, \ + const v8::PropertyCallbackInfo<void>& info) { \ + JSPropSetter<class_name, &class_name::set_##prop_name>( \ + #err_name, #class_name, property, value, info); \ + } + +#define JS_STATIC_METHOD(method_name, class_name) \ + static void method_name##_static( \ + const v8::FunctionCallbackInfo<v8::Value>& info) { \ + JSMethod<class_name, &class_name::method_name>(#method_name, #class_name, \ + info); \ + } + +#endif // FXJS_JS_DEFINE_H_ diff --git a/fxjs/JS_GlobalData.cpp b/fxjs/JS_GlobalData.cpp new file mode 100644 index 0000000000..78e19c9482 --- /dev/null +++ b/fxjs/JS_GlobalData.cpp @@ -0,0 +1,397 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/JS_GlobalData.h" + +#include <utility> + +#include "core/fdrm/crypto/fx_crypt.h" +#include "third_party/base/ptr_util.h" +#include "third_party/base/stl_util.h" + +#define JS_MAXGLOBALDATA (1024 * 4 - 8) + +#define READER_JS_GLOBALDATA_FILENAME L"Reader_JsGlobal.Data" +#define PHANTOM_JS_GLOBALDATA_FILENAME L"Phantom_JsGlobal.Data" +#define SDK_JS_GLOBALDATA_FILENAME L"SDK_JsGlobal.Data" + +namespace { + +const uint8_t JS_RC4KEY[] = { + 0x19, 0xa8, 0xe8, 0x01, 0xf6, 0xa8, 0xb6, 0x4d, 0x82, 0x04, 0x45, 0x6d, + 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51, + 0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16, + 0xd0, 0xdf, 0x8c, 0xfa, 0x2a, 0xa9, 0x49, 0xfd, 0x97, 0x1c, 0x0e, 0x22, + 0x13, 0x28, 0x7c, 0xaf, 0xc4, 0xfc, 0x9c, 0x12, 0x65, 0x8c, 0x4e, 0x5b, + 0x04, 0x75, 0x89, 0xc9, 0xb1, 0xed, 0x50, 0xca, 0x96, 0x6f, 0x1a, 0x7a, + 0xfe, 0x58, 0x5d, 0xec, 0x19, 0x4a, 0xf6, 0x35, 0x6a, 0x97, 0x14, 0x00, + 0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0, + 0xf8, 0x77, 0xd5, 0xa3}; + +// Returns true if non-empty, setting sPropName +bool TrimPropName(ByteString* sPropName) { + sPropName->TrimLeft(); + sPropName->TrimRight(); + return sPropName->GetLength() != 0; +} + +CJS_GlobalData* g_pInstance = nullptr; + +} // namespace + +// static +CJS_GlobalData* CJS_GlobalData::GetRetainedInstance( + CPDFSDK_FormFillEnvironment* pApp) { + if (!g_pInstance) { + g_pInstance = new CJS_GlobalData(); + } + ++g_pInstance->m_RefCount; + return g_pInstance; +} + +void CJS_GlobalData::Release() { + if (!--m_RefCount) { + delete g_pInstance; + g_pInstance = nullptr; + } +} + +CJS_GlobalData::CJS_GlobalData() + : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) { + LoadGlobalPersistentVariables(); +} + +CJS_GlobalData::~CJS_GlobalData() { + SaveGlobalPersisitentVariables(); +} + +CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable( + const ByteString& propname) { + for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); + ++it) { + if ((*it)->data.sKey == propname) + return it; + } + return m_arrayGlobalData.end(); +} + +CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable( + const ByteString& propname) const { + for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); + ++it) { + if ((*it)->data.sKey == propname) + return it; + } + return m_arrayGlobalData.end(); +} + +CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable( + const ByteString& propname) { + auto iter = FindGlobalVariable(propname); + return iter != m_arrayGlobalData.end() ? iter->get() : nullptr; +} + +void CJS_GlobalData::SetGlobalVariableNumber(const ByteString& propname, + double dData) { + ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) + return; + + if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { + pData->data.nType = JS_GlobalDataType::NUMBER; + pData->data.dData = dData; + return; + } + auto pNewData = pdfium::MakeUnique<CJS_GlobalData_Element>(); + pNewData->data.sKey = sPropName; + pNewData->data.nType = JS_GlobalDataType::NUMBER; + pNewData->data.dData = dData; + m_arrayGlobalData.push_back(std::move(pNewData)); +} + +void CJS_GlobalData::SetGlobalVariableBoolean(const ByteString& propname, + bool bData) { + ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) + return; + + if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { + pData->data.nType = JS_GlobalDataType::BOOLEAN; + pData->data.bData = bData; + return; + } + auto pNewData = pdfium::MakeUnique<CJS_GlobalData_Element>(); + pNewData->data.sKey = sPropName; + pNewData->data.nType = JS_GlobalDataType::BOOLEAN; + pNewData->data.bData = bData; + m_arrayGlobalData.push_back(std::move(pNewData)); +} + +void CJS_GlobalData::SetGlobalVariableString(const ByteString& propname, + const ByteString& sData) { + ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) + return; + + if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { + pData->data.nType = JS_GlobalDataType::STRING; + pData->data.sData = sData; + return; + } + auto pNewData = pdfium::MakeUnique<CJS_GlobalData_Element>(); + pNewData->data.sKey = sPropName; + pNewData->data.nType = JS_GlobalDataType::STRING; + pNewData->data.sData = sData; + m_arrayGlobalData.push_back(std::move(pNewData)); +} + +void CJS_GlobalData::SetGlobalVariableObject( + const ByteString& propname, + const CJS_GlobalVariableArray& array) { + ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) + return; + + if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { + pData->data.nType = JS_GlobalDataType::OBJECT; + pData->data.objData.Copy(array); + return; + } + auto pNewData = pdfium::MakeUnique<CJS_GlobalData_Element>(); + pNewData->data.sKey = sPropName; + pNewData->data.nType = JS_GlobalDataType::OBJECT; + pNewData->data.objData.Copy(array); + m_arrayGlobalData.push_back(std::move(pNewData)); +} + +void CJS_GlobalData::SetGlobalVariableNull(const ByteString& propname) { + ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) + return; + + if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { + pData->data.nType = JS_GlobalDataType::NULLOBJ; + return; + } + auto pNewData = pdfium::MakeUnique<CJS_GlobalData_Element>(); + pNewData->data.sKey = sPropName; + pNewData->data.nType = JS_GlobalDataType::NULLOBJ; + m_arrayGlobalData.push_back(std::move(pNewData)); +} + +bool CJS_GlobalData::SetGlobalVariablePersistent(const ByteString& propname, + bool bPersistent) { + ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) + return false; + + CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName); + if (!pData) + return false; + + pData->bPersistent = bPersistent; + return true; +} + +bool CJS_GlobalData::DeleteGlobalVariable(const ByteString& propname) { + ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) + return false; + + auto iter = FindGlobalVariable(sPropName); + if (iter == m_arrayGlobalData.end()) + return false; + + m_arrayGlobalData.erase(iter); + return true; +} + +int32_t CJS_GlobalData::GetSize() const { + return pdfium::CollectionSize<int32_t>(m_arrayGlobalData); +} + +CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const { + if (index < 0 || index >= GetSize()) + return nullptr; + return m_arrayGlobalData[index].get(); +} + +void CJS_GlobalData::LoadGlobalPersistentVariables() { + uint8_t* pBuffer = nullptr; + int32_t nLength = 0; + + LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength); + CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY)); + + if (pBuffer) { + uint8_t* p = pBuffer; + uint16_t wType = *((uint16_t*)p); + p += sizeof(uint16_t); + + if (wType == (uint16_t)(('X' << 8) | 'F')) { + uint16_t wVersion = *((uint16_t*)p); + p += sizeof(uint16_t); + + ASSERT(wVersion <= 2); + + uint32_t dwCount = *((uint32_t*)p); + p += sizeof(uint32_t); + + uint32_t dwSize = *((uint32_t*)p); + p += sizeof(uint32_t); + + if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(uint32_t) * 2) { + for (int32_t i = 0, sz = dwCount; i < sz; i++) { + if (p > pBuffer + nLength) + break; + + uint32_t dwNameLen = *((uint32_t*)p); + p += sizeof(uint32_t); + + if (p + dwNameLen > pBuffer + nLength) + break; + + ByteString sEntry = ByteString(p, dwNameLen); + p += sizeof(char) * dwNameLen; + + JS_GlobalDataType wDataType = + static_cast<JS_GlobalDataType>(*((uint16_t*)p)); + p += sizeof(uint16_t); + + switch (wDataType) { + case JS_GlobalDataType::NUMBER: { + double dData = 0; + switch (wVersion) { + case 1: { + uint32_t dwData = *((uint32_t*)p); + p += sizeof(uint32_t); + dData = dwData; + } break; + case 2: { + dData = *((double*)p); + p += sizeof(double); + } break; + } + SetGlobalVariableNumber(sEntry, dData); + SetGlobalVariablePersistent(sEntry, true); + } break; + case JS_GlobalDataType::BOOLEAN: { + uint16_t wData = *((uint16_t*)p); + p += sizeof(uint16_t); + SetGlobalVariableBoolean(sEntry, (bool)(wData == 1)); + SetGlobalVariablePersistent(sEntry, true); + } break; + case JS_GlobalDataType::STRING: { + uint32_t dwLength = *((uint32_t*)p); + p += sizeof(uint32_t); + + if (p + dwLength > pBuffer + nLength) + break; + + SetGlobalVariableString(sEntry, ByteString(p, dwLength)); + SetGlobalVariablePersistent(sEntry, true); + p += sizeof(char) * dwLength; + } break; + case JS_GlobalDataType::NULLOBJ: { + SetGlobalVariableNull(sEntry); + SetGlobalVariablePersistent(sEntry, true); + } + case JS_GlobalDataType::OBJECT: + break; + } + } + } + } + FX_Free(pBuffer); + } +} + +void CJS_GlobalData::SaveGlobalPersisitentVariables() { + uint32_t nCount = 0; + CFX_BinaryBuf sData; + for (const auto& pElement : m_arrayGlobalData) { + if (pElement->bPersistent) { + CFX_BinaryBuf sElement; + MakeByteString(pElement->data.sKey, &pElement->data, sElement); + if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA) + break; + + sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize()); + nCount++; + } + } + + CFX_BinaryBuf sFile; + uint16_t wType = (uint16_t)(('X' << 8) | 'F'); + sFile.AppendBlock(&wType, sizeof(uint16_t)); + uint16_t wVersion = 2; + sFile.AppendBlock(&wVersion, sizeof(uint16_t)); + sFile.AppendBlock(&nCount, sizeof(uint32_t)); + uint32_t dwSize = sData.GetSize(); + sFile.AppendBlock(&dwSize, sizeof(uint32_t)); + + sFile.AppendBlock(sData.GetBuffer(), sData.GetSize()); + + CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY, + sizeof(JS_RC4KEY)); + WriteFileBuffer(m_sFilePath.c_str(), (const char*)sFile.GetBuffer(), + sFile.GetSize()); +} + +void CJS_GlobalData::LoadFileBuffer(const wchar_t* sFilePath, + uint8_t*& pBuffer, + int32_t& nLength) { + // UnSupport. +} + +void CJS_GlobalData::WriteFileBuffer(const wchar_t* sFilePath, + const char* pBuffer, + int32_t nLength) { + // UnSupport. +} + +void CJS_GlobalData::MakeByteString(const ByteString& name, + CJS_KeyValue* pData, + CFX_BinaryBuf& sData) { + switch (pData->nType) { + case JS_GlobalDataType::NUMBER: { + uint32_t dwNameLen = (uint32_t)name.GetLength(); + sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); + sData.AppendString(name); + sData.AppendBlock(&pData->nType, sizeof(uint16_t)); + + double dData = pData->dData; + sData.AppendBlock(&dData, sizeof(double)); + } break; + case JS_GlobalDataType::BOOLEAN: { + uint32_t dwNameLen = (uint32_t)name.GetLength(); + sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); + sData.AppendString(name); + sData.AppendBlock(&pData->nType, sizeof(uint16_t)); + + uint16_t wData = (uint16_t)pData->bData; + sData.AppendBlock(&wData, sizeof(uint16_t)); + } break; + case JS_GlobalDataType::STRING: { + uint32_t dwNameLen = (uint32_t)name.GetLength(); + sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); + sData.AppendString(name); + sData.AppendBlock(&pData->nType, sizeof(uint16_t)); + + uint32_t dwDataLen = (uint32_t)pData->sData.GetLength(); + sData.AppendBlock(&dwDataLen, sizeof(uint32_t)); + sData.AppendString(pData->sData); + } break; + case JS_GlobalDataType::NULLOBJ: { + uint32_t dwNameLen = (uint32_t)name.GetLength(); + sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); + sData.AppendString(name); + sData.AppendBlock(&pData->nType, sizeof(uint32_t)); + } break; + default: + break; + } +} diff --git a/fxjs/JS_GlobalData.h b/fxjs/JS_GlobalData.h new file mode 100644 index 0000000000..c167d1e4e1 --- /dev/null +++ b/fxjs/JS_GlobalData.h @@ -0,0 +1,77 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_JS_GLOBALDATA_H_ +#define FXJS_JS_GLOBALDATA_H_ + +#include <memory> +#include <vector> + +#include "core/fxcrt/cfx_binarybuf.h" +#include "fxjs/JS_KeyValue.h" + +class CPDFSDK_FormFillEnvironment; + +class CJS_GlobalData_Element { + public: + CJS_GlobalData_Element() {} + ~CJS_GlobalData_Element() {} + + CJS_KeyValue data; + bool bPersistent; +}; + +class CJS_GlobalData { + public: + static CJS_GlobalData* GetRetainedInstance(CPDFSDK_FormFillEnvironment* pApp); + void Release(); + + void SetGlobalVariableNumber(const ByteString& propname, double dData); + void SetGlobalVariableBoolean(const ByteString& propname, bool bData); + void SetGlobalVariableString(const ByteString& propname, + const ByteString& sData); + void SetGlobalVariableObject(const ByteString& propname, + const CJS_GlobalVariableArray& array); + void SetGlobalVariableNull(const ByteString& propname); + bool SetGlobalVariablePersistent(const ByteString& propname, + bool bPersistent); + bool DeleteGlobalVariable(const ByteString& propname); + + int32_t GetSize() const; + CJS_GlobalData_Element* GetAt(int index) const; + + private: + using iterator = + std::vector<std::unique_ptr<CJS_GlobalData_Element>>::iterator; + using const_iterator = + std::vector<std::unique_ptr<CJS_GlobalData_Element>>::const_iterator; + + CJS_GlobalData(); + ~CJS_GlobalData(); + + void LoadGlobalPersistentVariables(); + void SaveGlobalPersisitentVariables(); + + CJS_GlobalData_Element* GetGlobalVariable(const ByteString& sPropname); + iterator FindGlobalVariable(const ByteString& sPropname); + const_iterator FindGlobalVariable(const ByteString& sPropname) const; + + void LoadFileBuffer(const wchar_t* sFilePath, + uint8_t*& pBuffer, + int32_t& nLength); + void WriteFileBuffer(const wchar_t* sFilePath, + const char* pBuffer, + int32_t nLength); + void MakeByteString(const ByteString& name, + CJS_KeyValue* pData, + CFX_BinaryBuf& sData); + + size_t m_RefCount; + std::vector<std::unique_ptr<CJS_GlobalData_Element>> m_arrayGlobalData; + WideString m_sFilePath; +}; + +#endif // FXJS_JS_GLOBALDATA_H_ diff --git a/fxjs/JS_KeyValue.cpp b/fxjs/JS_KeyValue.cpp new file mode 100644 index 0000000000..aabfc38c76 --- /dev/null +++ b/fxjs/JS_KeyValue.cpp @@ -0,0 +1,70 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/JS_KeyValue.h" + +CJS_GlobalVariableArray::CJS_GlobalVariableArray() {} + +CJS_GlobalVariableArray::~CJS_GlobalVariableArray() {} + +void CJS_GlobalVariableArray::Copy(const CJS_GlobalVariableArray& array) { + m_Array.clear(); + for (int i = 0, sz = array.Count(); i < sz; i++) { + CJS_KeyValue* pOldObjData = array.GetAt(i); + switch (pOldObjData->nType) { + case JS_GlobalDataType::NUMBER: { + CJS_KeyValue* pNewObjData = new CJS_KeyValue; + pNewObjData->sKey = pOldObjData->sKey; + pNewObjData->nType = pOldObjData->nType; + pNewObjData->dData = pOldObjData->dData; + Add(pNewObjData); + } break; + case JS_GlobalDataType::BOOLEAN: { + CJS_KeyValue* pNewObjData = new CJS_KeyValue; + pNewObjData->sKey = pOldObjData->sKey; + pNewObjData->nType = pOldObjData->nType; + pNewObjData->bData = pOldObjData->bData; + Add(pNewObjData); + } break; + case JS_GlobalDataType::STRING: { + CJS_KeyValue* pNewObjData = new CJS_KeyValue; + pNewObjData->sKey = pOldObjData->sKey; + pNewObjData->nType = pOldObjData->nType; + pNewObjData->sData = pOldObjData->sData; + Add(pNewObjData); + } break; + case JS_GlobalDataType::OBJECT: { + CJS_KeyValue* pNewObjData = new CJS_KeyValue; + pNewObjData->sKey = pOldObjData->sKey; + pNewObjData->nType = pOldObjData->nType; + pNewObjData->objData.Copy(pOldObjData->objData); + Add(pNewObjData); + } break; + case JS_GlobalDataType::NULLOBJ: { + CJS_KeyValue* pNewObjData = new CJS_KeyValue; + pNewObjData->sKey = pOldObjData->sKey; + pNewObjData->nType = pOldObjData->nType; + Add(pNewObjData); + } break; + } + } +} + +void CJS_GlobalVariableArray::Add(CJS_KeyValue* p) { + m_Array.push_back(std::unique_ptr<CJS_KeyValue>(p)); +} + +int CJS_GlobalVariableArray::Count() const { + return m_Array.size(); +} + +CJS_KeyValue* CJS_GlobalVariableArray::GetAt(int index) const { + return m_Array.at(index).get(); +} + +CJS_KeyValue::CJS_KeyValue() {} + +CJS_KeyValue::~CJS_KeyValue() {} diff --git a/fxjs/JS_KeyValue.h b/fxjs/JS_KeyValue.h new file mode 100644 index 0000000000..a81a6b432e --- /dev/null +++ b/fxjs/JS_KeyValue.h @@ -0,0 +1,46 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_JS_KEYVALUE_H_ +#define FXJS_JS_KEYVALUE_H_ + +#include <memory> +#include <vector> + +#include "core/fxcrt/fx_string.h" + +enum class JS_GlobalDataType { NUMBER = 0, BOOLEAN, STRING, OBJECT, NULLOBJ }; + +class CJS_KeyValue; + +class CJS_GlobalVariableArray { + public: + CJS_GlobalVariableArray(); + ~CJS_GlobalVariableArray(); + + void Add(CJS_KeyValue* p); + int Count() const; + CJS_KeyValue* GetAt(int index) const; + void Copy(const CJS_GlobalVariableArray& array); + + private: + std::vector<std::unique_ptr<CJS_KeyValue>> m_Array; +}; + +class CJS_KeyValue { + public: + CJS_KeyValue(); + ~CJS_KeyValue(); + + ByteString sKey; + JS_GlobalDataType nType; + double dData; + bool bData; + ByteString sData; + CJS_GlobalVariableArray objData; +}; + +#endif // FXJS_JS_KEYVALUE_H_ diff --git a/fxjs/cjs_annot.cpp b/fxjs/cjs_annot.cpp new file mode 100644 index 0000000000..f4f9669eff --- /dev/null +++ b/fxjs/cjs_annot.cpp @@ -0,0 +1,112 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_annot.h" + +#include "fxjs/JS_Define.h" +#include "fxjs/cjs_event_context.h" +#include "fxjs/cjs_object.h" +#include "fxjs/js_resources.h" + +namespace { + +CPDFSDK_BAAnnot* ToBAAnnot(CPDFSDK_Annot* annot) { + return static_cast<CPDFSDK_BAAnnot*>(annot); +} + +} // namespace + +const JSPropertySpec CJS_Annot::PropertySpecs[] = { + {"hidden", get_hidden_static, set_hidden_static}, + {"name", get_name_static, set_name_static}, + {"type", get_type_static, set_type_static}, + {0, 0, 0}}; + +int CJS_Annot::ObjDefnID = -1; + +// static +int CJS_Annot::GetObjDefnID() { + return ObjDefnID; +} + +// static +void CJS_Annot::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = pEngine->DefineObj("Annot", FXJSOBJTYPE_DYNAMIC, + JSConstructor<CJS_Annot, Annot>, + JSDestructor<CJS_Annot>); + DefineProps(pEngine, ObjDefnID, PropertySpecs); +} + +Annot::Annot(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} + +Annot::~Annot() {} + +CJS_Return Annot::get_hidden(CJS_Runtime* pRuntime) { + if (!m_pAnnot) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + CPDF_Annot* pPDFAnnot = ToBAAnnot(m_pAnnot.Get())->GetPDFAnnot(); + return CJS_Return(pRuntime->NewBoolean( + CPDF_Annot::IsAnnotationHidden(pPDFAnnot->GetAnnotDict()))); +} + +CJS_Return Annot::set_hidden(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + // May invalidate m_pAnnot. + bool bHidden = pRuntime->ToBoolean(vp); + if (!m_pAnnot) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + uint32_t flags = ToBAAnnot(m_pAnnot.Get())->GetFlags(); + if (bHidden) { + flags |= ANNOTFLAG_HIDDEN; + flags |= ANNOTFLAG_INVISIBLE; + flags |= ANNOTFLAG_NOVIEW; + flags &= ~ANNOTFLAG_PRINT; + } else { + flags &= ~ANNOTFLAG_HIDDEN; + flags &= ~ANNOTFLAG_INVISIBLE; + flags &= ~ANNOTFLAG_NOVIEW; + flags |= ANNOTFLAG_PRINT; + } + ToBAAnnot(m_pAnnot.Get())->SetFlags(flags); + + return CJS_Return(true); +} + +CJS_Return Annot::get_name(CJS_Runtime* pRuntime) { + if (!m_pAnnot) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + return CJS_Return( + pRuntime->NewString(ToBAAnnot(m_pAnnot.Get())->GetAnnotName().c_str())); +} + +CJS_Return Annot::set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + // May invalidate m_pAnnot. + WideString annotName = pRuntime->ToWideString(vp); + if (!m_pAnnot) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + ToBAAnnot(m_pAnnot.Get())->SetAnnotName(annotName); + return CJS_Return(true); +} + +CJS_Return Annot::get_type(CJS_Runtime* pRuntime) { + if (!m_pAnnot) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + return CJS_Return(pRuntime->NewString( + WideString::FromLocal(CPDF_Annot::AnnotSubtypeToString( + ToBAAnnot(m_pAnnot.Get())->GetAnnotSubtype()) + .c_str()) + .c_str())); +} + +CJS_Return Annot::set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSREADONLY)); +} + +void Annot::SetSDKAnnot(CPDFSDK_BAAnnot* annot) { + m_pAnnot.Reset(annot); +} diff --git a/fxjs/cjs_annot.h b/fxjs/cjs_annot.h new file mode 100644 index 0000000000..18124f6216 --- /dev/null +++ b/fxjs/cjs_annot.h @@ -0,0 +1,50 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_ANNOT_H_ +#define FXJS_CJS_ANNOT_H_ + +#include "fpdfsdk/cpdfsdk_baannot.h" +#include "fxjs/JS_Define.h" + +class Annot : public CJS_EmbedObj { + public: + explicit Annot(CJS_Object* pJSObject); + ~Annot() override; + + CJS_Return get_hidden(CJS_Runtime* pRuntime); + CJS_Return set_hidden(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_name(CJS_Runtime* pRuntime); + CJS_Return set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_type(CJS_Runtime* pRuntime); + CJS_Return set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + void SetSDKAnnot(CPDFSDK_BAAnnot* annot); + + private: + CPDFSDK_Annot::ObservedPtr m_pAnnot; +}; + +class CJS_Annot : public CJS_Object { + public: + static int GetObjDefnID(); + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Annot(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Annot() override {} + + JS_STATIC_PROP(hidden, hidden, Annot); + JS_STATIC_PROP(name, name, Annot); + JS_STATIC_PROP(type, type, Annot); + + private: + static int ObjDefnID; + static const JSPropertySpec PropertySpecs[]; +}; + +#endif // FXJS_CJS_ANNOT_H_ diff --git a/fxjs/cjs_app.cpp b/fxjs/cjs_app.cpp new file mode 100644 index 0000000000..f013e7eaa8 --- /dev/null +++ b/fxjs/cjs_app.cpp @@ -0,0 +1,579 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_app.h" + +#include "fpdfsdk/cpdfsdk_interform.h" +#include "fxjs/cjs_document.h" +#include "fxjs/cjs_timerobj.h" +#include "fxjs/global_timer.h" +#include "fxjs/ijs_event_context.h" +#include "fxjs/js_resources.h" + +namespace { + +bool IsTypeKnown(v8::Local<v8::Value> value) { + return !value.IsEmpty() && + (value->IsString() || value->IsNumber() || value->IsBoolean() || + value->IsDate() || value->IsObject() || value->IsNull() || + value->IsUndefined()); +} + +} // namespace + +#define JS_STR_VIEWERTYPE L"pdfium" +#define JS_STR_VIEWERVARIATION L"Full" +#define JS_STR_PLATFORM L"WIN" +#define JS_STR_LANGUAGE L"ENU" +#define JS_NUM_VIEWERVERSION 8 +#ifdef PDF_ENABLE_XFA +#define JS_NUM_VIEWERVERSION_XFA 11 +#endif // PDF_ENABLE_XFA +#define JS_NUM_FORMSVERSION 7 + +const JSPropertySpec CJS_App::PropertySpecs[] = { + {"activeDocs", get_active_docs_static, set_active_docs_static}, + {"calculate", get_calculate_static, set_calculate_static}, + {"formsVersion", get_forms_version_static, set_forms_version_static}, + {"fs", get_fs_static, set_fs_static}, + {"fullscreen", get_fullscreen_static, set_fullscreen_static}, + {"language", get_language_static, set_language_static}, + {"media", get_media_static, set_media_static}, + {"platform", get_platform_static, set_platform_static}, + {"runtimeHighlight", get_runtime_highlight_static, + set_runtime_highlight_static}, + {"viewerType", get_viewer_type_static, set_viewer_type_static}, + {"viewerVariation", get_viewer_variation_static, + set_viewer_variation_static}, + {"viewerVersion", get_viewer_version_static, set_viewer_version_static}, + {0, 0, 0}}; + +const JSMethodSpec CJS_App::MethodSpecs[] = { + {"alert", alert_static}, + {"beep", beep_static}, + {"browseForDoc", browseForDoc_static}, + {"clearInterval", clearInterval_static}, + {"clearTimeOut", clearTimeOut_static}, + {"execDialog", execDialog_static}, + {"execMenuItem", execMenuItem_static}, + {"findComponent", findComponent_static}, + {"goBack", goBack_static}, + {"goForward", goForward_static}, + {"launchURL", launchURL_static}, + {"mailMsg", mailMsg_static}, + {"newFDF", newFDF_static}, + {"newDoc", newDoc_static}, + {"openDoc", openDoc_static}, + {"openFDF", openFDF_static}, + {"popUpMenuEx", popUpMenuEx_static}, + {"popUpMenu", popUpMenu_static}, + {"response", response_static}, + {"setInterval", setInterval_static}, + {"setTimeOut", setTimeOut_static}, + {0, 0}}; + +int CJS_App::ObjDefnID = -1; + +// static +void CJS_App::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("app", FXJSOBJTYPE_STATIC, JSConstructor<CJS_App, app>, + JSDestructor<CJS_App>); + DefineProps(pEngine, ObjDefnID, PropertySpecs); + DefineMethods(pEngine, ObjDefnID, MethodSpecs); +} + +app::app(CJS_Object* pJSObject) + : CJS_EmbedObj(pJSObject), m_bCalculate(true), m_bRuntimeHighLight(false) {} + +app::~app() {} + +CJS_Return app::get_active_docs(CJS_Runtime* pRuntime) { + CJS_Document* pJSDocument = nullptr; + v8::Local<v8::Object> pObj = pRuntime->GetThisObj(); + if (CFXJS_Engine::GetObjDefnID(pObj) == CJS_Document::GetObjDefnID()) + pJSDocument = static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pObj)); + + v8::Local<v8::Array> aDocs = pRuntime->NewArray(); + pRuntime->PutArrayElement( + aDocs, 0, + pJSDocument ? v8::Local<v8::Value>(pJSDocument->ToV8Object()) + : v8::Local<v8::Value>()); + if (pRuntime->GetArrayLength(aDocs) > 0) + return CJS_Return(aDocs); + return CJS_Return(pRuntime->NewUndefined()); +} + +CJS_Return app::set_active_docs(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return app::get_calculate(CJS_Runtime* pRuntime) { + return CJS_Return(pRuntime->NewBoolean(m_bCalculate)); +} + +CJS_Return app::set_calculate(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + m_bCalculate = pRuntime->ToBoolean(vp); + pRuntime->GetFormFillEnv()->GetInterForm()->EnableCalculate(m_bCalculate); + return CJS_Return(true); +} + +CJS_Return app::get_forms_version(CJS_Runtime* pRuntime) { + return CJS_Return(pRuntime->NewNumber(JS_NUM_FORMSVERSION)); +} + +CJS_Return app::set_forms_version(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return app::get_viewer_type(CJS_Runtime* pRuntime) { + return CJS_Return(pRuntime->NewString(JS_STR_VIEWERTYPE)); +} + +CJS_Return app::set_viewer_type(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return app::get_viewer_variation(CJS_Runtime* pRuntime) { + return CJS_Return(pRuntime->NewString(JS_STR_VIEWERVARIATION)); +} + +CJS_Return app::set_viewer_variation(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return app::get_viewer_version(CJS_Runtime* pRuntime) { +#ifdef PDF_ENABLE_XFA + CPDFXFA_Context* pXFAContext = pRuntime->GetFormFillEnv()->GetXFAContext(); + if (pXFAContext->ContainsXFAForm()) + return CJS_Return(pRuntime->NewNumber(JS_NUM_VIEWERVERSION_XFA)); +#endif // PDF_ENABLE_XFA + return CJS_Return(pRuntime->NewNumber(JS_NUM_VIEWERVERSION)); +} + +CJS_Return app::set_viewer_version(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return app::get_platform(CJS_Runtime* pRuntime) { +#ifdef PDF_ENABLE_XFA + CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv(); + if (!pFormFillEnv) + return CJS_Return(false); + + WideString platfrom = pFormFillEnv->GetPlatform(); + if (!platfrom.IsEmpty()) + return CJS_Return(pRuntime->NewString(platfrom.c_str())); +#endif + return CJS_Return(pRuntime->NewString(JS_STR_PLATFORM)); +} + +CJS_Return app::set_platform(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return app::get_language(CJS_Runtime* pRuntime) { +#ifdef PDF_ENABLE_XFA + CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv(); + if (!pFormFillEnv) + return CJS_Return(false); + + WideString language = pFormFillEnv->GetLanguage(); + if (!language.IsEmpty()) + return CJS_Return(pRuntime->NewString(language.c_str())); +#endif + return CJS_Return(pRuntime->NewString(JS_STR_LANGUAGE)); +} + +CJS_Return app::set_language(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +// creates a new fdf object that contains no data +// comment: need reader support +// note: +// CFDF_Document * CPDFSDK_FormFillEnvironment::NewFDF(); +CJS_Return app::newFDF(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +// opens a specified pdf document and returns its document object +// comment:need reader support +// note: as defined in js reference, the proto of this function's fourth +// parmeters, how old an fdf document while do not show it. +// CFDF_Document * CPDFSDK_FormFillEnvironment::OpenFDF(string strPath,bool +// bUserConv); + +CJS_Return app::openFDF(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return app::alert(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + std::vector<v8::Local<v8::Value>> newParams = ExpandKeywordParams( + pRuntime, params, 4, L"cMsg", L"nIcon", L"nType", L"cTitle"); + + if (!IsTypeKnown(newParams[0])) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv(); + if (!pFormFillEnv) + return CJS_Return(pRuntime->NewNumber(0)); + + WideString swMsg; + if (newParams[0]->IsArray()) { + v8::Local<v8::Array> carray = pRuntime->ToArray(newParams[0]); + swMsg = L"["; + for (size_t i = 0; i < pRuntime->GetArrayLength(carray); ++i) { + if (i) + swMsg += L", "; + + swMsg += pRuntime->ToWideString(pRuntime->GetArrayElement(carray, i)); + } + swMsg += L"]"; + } else { + swMsg = pRuntime->ToWideString(newParams[0]); + } + + int iIcon = 0; + if (IsTypeKnown(newParams[1])) + iIcon = pRuntime->ToInt32(newParams[1]); + + int iType = 0; + if (IsTypeKnown(newParams[2])) + iType = pRuntime->ToInt32(newParams[2]); + + WideString swTitle; + if (IsTypeKnown(newParams[3])) + swTitle = pRuntime->ToWideString(newParams[3]); + else + swTitle = JSGetStringFromID(IDS_STRING_JSALERT); + + pRuntime->BeginBlock(); + pFormFillEnv->KillFocusAnnot(0); + + v8::Local<v8::Value> ret = pRuntime->NewNumber( + pFormFillEnv->JS_appAlert(swMsg.c_str(), swTitle.c_str(), iType, iIcon)); + pRuntime->EndBlock(); + + return CJS_Return(ret); +} + +CJS_Return app::beep(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() == 1) { + pRuntime->GetFormFillEnv()->JS_appBeep(pRuntime->ToInt32(params[0])); + return CJS_Return(true); + } + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); +} + +CJS_Return app::findComponent(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return app::popUpMenuEx(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return app::get_fs(CJS_Runtime* pRuntime) { + return CJS_Return(false); +} + +CJS_Return app::set_fs(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return app::setInterval(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() > 2 || params.size() == 0) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + WideString script = + params.size() > 0 ? pRuntime->ToWideString(params[0]) : L""; + if (script.IsEmpty()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE)); + + uint32_t dwInterval = params.size() > 1 ? pRuntime->ToInt32(params[1]) : 1000; + GlobalTimer* timerRef = new GlobalTimer(this, pRuntime->GetFormFillEnv(), + pRuntime, 0, script, dwInterval, 0); + m_Timers.insert(std::unique_ptr<GlobalTimer>(timerRef)); + + v8::Local<v8::Object> pRetObj = + pRuntime->NewFxDynamicObj(CJS_TimerObj::GetObjDefnID()); + if (pRetObj.IsEmpty()) + return CJS_Return(false); + + CJS_TimerObj* pJS_TimerObj = + static_cast<CJS_TimerObj*>(pRuntime->GetObjectPrivate(pRetObj)); + TimerObj* pTimerObj = static_cast<TimerObj*>(pJS_TimerObj->GetEmbedObject()); + pTimerObj->SetTimer(timerRef); + + return CJS_Return(pRetObj); +} + +CJS_Return app::setTimeOut(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() > 2 || params.size() == 0) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + WideString script = pRuntime->ToWideString(params[0]); + if (script.IsEmpty()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE)); + + uint32_t dwTimeOut = params.size() > 1 ? pRuntime->ToInt32(params[1]) : 1000; + GlobalTimer* timerRef = + new GlobalTimer(this, pRuntime->GetFormFillEnv(), pRuntime, 1, script, + dwTimeOut, dwTimeOut); + m_Timers.insert(std::unique_ptr<GlobalTimer>(timerRef)); + + v8::Local<v8::Object> pRetObj = + pRuntime->NewFxDynamicObj(CJS_TimerObj::GetObjDefnID()); + if (pRetObj.IsEmpty()) + return CJS_Return(false); + + CJS_TimerObj* pJS_TimerObj = + static_cast<CJS_TimerObj*>(pRuntime->GetObjectPrivate(pRetObj)); + TimerObj* pTimerObj = static_cast<TimerObj*>(pJS_TimerObj->GetEmbedObject()); + pTimerObj->SetTimer(timerRef); + + return CJS_Return(pRetObj); +} + +CJS_Return app::clearTimeOut(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + app::ClearTimerCommon(pRuntime, params[0]); + return CJS_Return(true); +} + +CJS_Return app::clearInterval(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + app::ClearTimerCommon(pRuntime, params[0]); + return CJS_Return(true); +} + +void app::ClearTimerCommon(CJS_Runtime* pRuntime, v8::Local<v8::Value> param) { + if (!param->IsObject()) + return; + + v8::Local<v8::Object> pObj = pRuntime->ToObject(param); + if (CFXJS_Engine::GetObjDefnID(pObj) != CJS_TimerObj::GetObjDefnID()) + return; + + CJS_Object* pJSObj = + static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(pObj)); + if (!pJSObj) + return; + + TimerObj* pTimerObj = static_cast<TimerObj*>(pJSObj->GetEmbedObject()); + if (!pTimerObj) + return; + + GlobalTimer::Cancel(pTimerObj->GetTimerID()); +} + +CJS_Return app::execMenuItem(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +void app::TimerProc(GlobalTimer* pTimer) { + CJS_Runtime* pRuntime = pTimer->GetRuntime(); + if (pRuntime && (!pTimer->IsOneShot() || pTimer->GetTimeOut() > 0)) + RunJsScript(pRuntime, pTimer->GetJScript()); +} + +void app::CancelProc(GlobalTimer* pTimer) { + m_Timers.erase(pdfium::FakeUniquePtr<GlobalTimer>(pTimer)); +} + +void app::RunJsScript(CJS_Runtime* pRuntime, const WideString& wsScript) { + if (!pRuntime->IsBlocking()) { + IJS_EventContext* pContext = pRuntime->NewEventContext(); + pContext->OnExternal_Exec(); + WideString wtInfo; + pContext->RunScript(wsScript, &wtInfo); + pRuntime->ReleaseEventContext(pContext); + } +} + +CJS_Return app::goBack(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Not supported. + return CJS_Return(true); +} + +CJS_Return app::goForward(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Not supported. + return CJS_Return(true); +} + +CJS_Return app::mailMsg(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + std::vector<v8::Local<v8::Value>> newParams = + ExpandKeywordParams(pRuntime, params, 6, L"bUI", L"cTo", L"cCc", L"cBcc", + L"cSubject", L"cMsg"); + + if (!IsTypeKnown(newParams[0])) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + bool bUI = pRuntime->ToBoolean(newParams[0]); + WideString cTo; + if (IsTypeKnown(newParams[1])) { + cTo = pRuntime->ToWideString(newParams[1]); + } else { + // cTo parameter required when UI not invoked. + if (!bUI) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + } + + WideString cCc; + if (IsTypeKnown(newParams[2])) + cCc = pRuntime->ToWideString(newParams[2]); + + WideString cBcc; + if (IsTypeKnown(newParams[3])) + cBcc = pRuntime->ToWideString(newParams[3]); + + WideString cSubject; + if (IsTypeKnown(newParams[4])) + cSubject = pRuntime->ToWideString(newParams[4]); + + WideString cMsg; + if (IsTypeKnown(newParams[5])) + cMsg = pRuntime->ToWideString(newParams[5]); + + pRuntime->BeginBlock(); + pRuntime->GetFormFillEnv()->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), + cSubject.c_str(), cCc.c_str(), + cBcc.c_str(), cMsg.c_str()); + pRuntime->EndBlock(); + return CJS_Return(true); +} + +CJS_Return app::launchURL(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return app::get_runtime_highlight(CJS_Runtime* pRuntime) { + return CJS_Return(pRuntime->NewBoolean(m_bRuntimeHighLight)); +} + +CJS_Return app::set_runtime_highlight(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + m_bRuntimeHighLight = pRuntime->ToBoolean(vp); + return CJS_Return(true); +} + +CJS_Return app::get_fullscreen(CJS_Runtime* pRuntime) { + return CJS_Return(false); +} + +CJS_Return app::set_fullscreen(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return app::popUpMenu(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return app::browseForDoc(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +WideString app::SysPathToPDFPath(const WideString& sOldPath) { + WideString sRet = L"/"; + for (const wchar_t& c : sOldPath) { + if (c != L':') + sRet += (c == L'\\') ? L'/' : c; + } + return sRet; +} + +CJS_Return app::newDoc(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return app::openDoc(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return app::response(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + std::vector<v8::Local<v8::Value>> newParams = + ExpandKeywordParams(pRuntime, params, 5, L"cQuestion", L"cTitle", + L"cDefault", L"bPassword", L"cLabel"); + + if (!IsTypeKnown(newParams[0])) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + WideString swQuestion = pRuntime->ToWideString(newParams[0]); + WideString swTitle = L"PDF"; + if (IsTypeKnown(newParams[1])) + swTitle = pRuntime->ToWideString(newParams[1]); + + WideString swDefault; + if (IsTypeKnown(newParams[2])) + swDefault = pRuntime->ToWideString(newParams[2]); + + bool bPassword = false; + if (IsTypeKnown(newParams[3])) + bPassword = pRuntime->ToBoolean(newParams[3]); + + WideString swLabel; + if (IsTypeKnown(newParams[4])) + swLabel = pRuntime->ToWideString(newParams[4]); + + const int MAX_INPUT_BYTES = 2048; + std::vector<uint8_t> pBuff(MAX_INPUT_BYTES + 2); + int nLengthBytes = pRuntime->GetFormFillEnv()->JS_appResponse( + swQuestion.c_str(), swTitle.c_str(), swDefault.c_str(), swLabel.c_str(), + bPassword, pBuff.data(), MAX_INPUT_BYTES); + + if (nLengthBytes < 0 || nLengthBytes > MAX_INPUT_BYTES) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG)); + + return CJS_Return(pRuntime->NewString( + WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()), + nLengthBytes / sizeof(uint16_t)) + .c_str())); +} + +CJS_Return app::get_media(CJS_Runtime* pRuntime) { + return CJS_Return(false); +} + +CJS_Return app::set_media(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return app::execDialog(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} diff --git a/fxjs/cjs_app.h b/fxjs/cjs_app.h new file mode 100644 index 0000000000..703c7052d3 --- /dev/null +++ b/fxjs/cjs_app.h @@ -0,0 +1,169 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_APP_H_ +#define FXJS_CJS_APP_H_ + +#include <memory> +#include <set> +#include <vector> + +#include "fxjs/JS_Define.h" + +class CJS_Runtime; +class GlobalTimer; + +class app : public CJS_EmbedObj { + public: + explicit app(CJS_Object* pJSObject); + ~app() override; + + CJS_Return get_active_docs(CJS_Runtime* pRuntime); + CJS_Return set_active_docs(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_calculate(CJS_Runtime* pRuntime); + CJS_Return set_calculate(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_forms_version(CJS_Runtime* pRuntime); + CJS_Return set_forms_version(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_fs(CJS_Runtime* pRuntime); + CJS_Return set_fs(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_fullscreen(CJS_Runtime* pRuntime); + CJS_Return set_fullscreen(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_language(CJS_Runtime* pRuntime); + CJS_Return set_language(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_media(CJS_Runtime* pRuntime); + CJS_Return set_media(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_platform(CJS_Runtime* pRuntime); + CJS_Return set_platform(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_runtime_highlight(CJS_Runtime* pRuntime); + CJS_Return set_runtime_highlight(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_viewer_type(CJS_Runtime* pRuntime); + CJS_Return set_viewer_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_viewer_variation(CJS_Runtime* pRuntime); + CJS_Return set_viewer_variation(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_viewer_version(CJS_Runtime* pRuntime); + CJS_Return set_viewer_version(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return alert(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return beep(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return browseForDoc(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return clearInterval(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return clearTimeOut(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return execDialog(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return execMenuItem(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return findComponent(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return goBack(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return goForward(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return launchURL(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return mailMsg(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return newFDF(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return newDoc(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return openDoc(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return openFDF(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return popUpMenuEx(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return popUpMenu(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return response(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return setInterval(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return setTimeOut(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + + void TimerProc(GlobalTimer* pTimer); + void CancelProc(GlobalTimer* pTimer); + + static WideString SysPathToPDFPath(const WideString& sOldPath); + + private: + // CJS_EmbedObj + void RunJsScript(CJS_Runtime* pRuntime, const WideString& wsScript); + + void ClearTimerCommon(CJS_Runtime* pRuntime, v8::Local<v8::Value> param); + + bool m_bCalculate; + bool m_bRuntimeHighLight; + std::set<std::unique_ptr<GlobalTimer>> m_Timers; +}; + +class CJS_App : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_App(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_App() override {} + + JS_STATIC_PROP(activeDocs, active_docs, app); + JS_STATIC_PROP(calculate, calculate, app); + JS_STATIC_PROP(formsVersion, forms_version, app); + JS_STATIC_PROP(fs, fs, app); + JS_STATIC_PROP(fullscreen, fullscreen, app); + JS_STATIC_PROP(language, language, app); + JS_STATIC_PROP(media, media, app); + JS_STATIC_PROP(platform, platform, app); + JS_STATIC_PROP(runtimeHighlight, runtime_highlight, app); + JS_STATIC_PROP(viewerType, viewer_type, app); + JS_STATIC_PROP(viewerVariation, viewer_variation, app); + JS_STATIC_PROP(viewerVersion, viewer_version, app); + + JS_STATIC_METHOD(alert, app); + JS_STATIC_METHOD(beep, app); + JS_STATIC_METHOD(browseForDoc, app); + JS_STATIC_METHOD(clearInterval, app); + JS_STATIC_METHOD(clearTimeOut, app); + JS_STATIC_METHOD(execDialog, app); + JS_STATIC_METHOD(execMenuItem, app); + JS_STATIC_METHOD(findComponent, app); + JS_STATIC_METHOD(goBack, app); + JS_STATIC_METHOD(goForward, app); + JS_STATIC_METHOD(launchURL, app); + JS_STATIC_METHOD(mailMsg, app); + JS_STATIC_METHOD(newFDF, app); + JS_STATIC_METHOD(newDoc, app); + JS_STATIC_METHOD(openDoc, app); + JS_STATIC_METHOD(openFDF, app); + JS_STATIC_METHOD(popUpMenuEx, app); + JS_STATIC_METHOD(popUpMenu, app); + JS_STATIC_METHOD(response, app); + JS_STATIC_METHOD(setInterval, app); + JS_STATIC_METHOD(setTimeOut, app); + + private: + static int ObjDefnID; + static const JSPropertySpec PropertySpecs[]; + static const JSMethodSpec MethodSpecs[]; +}; + +#endif // FXJS_CJS_APP_H_ diff --git a/fxjs/cjs_border.cpp b/fxjs/cjs_border.cpp new file mode 100644 index 0000000000..24d50ffcad --- /dev/null +++ b/fxjs/cjs_border.cpp @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_border.h" + +const JSConstSpec CJS_Border::ConstSpecs[] = { + {"s", JSConstSpec::String, 0, "solid"}, + {"b", JSConstSpec::String, 0, "beveled"}, + {"d", JSConstSpec::String, 0, "dashed"}, + {"i", JSConstSpec::String, 0, "inset"}, + {"u", JSConstSpec::String, 0, "underline"}, + {0, JSConstSpec::Number, 0, 0}}; + +int CJS_Border::ObjDefnID = -1; + +// static +void CJS_Border::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("border", FXJSOBJTYPE_STATIC, nullptr, nullptr); + DefineConsts(pEngine, ObjDefnID, ConstSpecs); +} diff --git a/fxjs/cjs_border.h b/fxjs/cjs_border.h new file mode 100644 index 0000000000..b3fcef7f21 --- /dev/null +++ b/fxjs/cjs_border.h @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_BORDER_H_ +#define FXJS_CJS_BORDER_H_ + +#include "fxjs/JS_Define.h" + +class CJS_Border : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Border(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Border() override {} + + private: + static int ObjDefnID; + static const JSConstSpec ConstSpecs[]; +}; + +#endif // FXJS_CJS_BORDER_H_ diff --git a/fxjs/cjs_color.cpp b/fxjs/cjs_color.cpp new file mode 100644 index 0000000000..1bebf79ea8 --- /dev/null +++ b/fxjs/cjs_color.cpp @@ -0,0 +1,304 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_color.h" + +#include <vector> + +#include "fxjs/JS_Define.h" +#include "fxjs/cjs_event_context.h" +#include "fxjs/cjs_eventhandler.h" +#include "fxjs/cjs_object.h" +#include "fxjs/cjs_runtime.h" + +const JSPropertySpec CJS_Color::PropertySpecs[] = { + {"black", get_black_static, set_black_static}, + {"blue", get_blue_static, set_blue_static}, + {"cyan", get_cyan_static, set_cyan_static}, + {"dkGray", get_dark_gray_static, set_dark_gray_static}, + {"gray", get_gray_static, set_gray_static}, + {"green", get_green_static, set_green_static}, + {"ltGray", get_light_gray_static, set_light_gray_static}, + {"magenta", get_magenta_static, set_magenta_static}, + {"red", get_red_static, set_red_static}, + {"transparent", get_transparent_static, set_transparent_static}, + {"white", get_white_static, set_white_static}, + {"yellow", get_yellow_static, set_yellow_static}, + {0, 0, 0}}; + +const JSMethodSpec CJS_Color::MethodSpecs[] = {{"convert", convert_static}, + {"equal", equal_static}, + {0, 0}}; + +int CJS_Color::ObjDefnID = -1; + +// static +void CJS_Color::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = pEngine->DefineObj("color", FXJSOBJTYPE_STATIC, + JSConstructor<CJS_Color, color>, + JSDestructor<CJS_Color>); + DefineProps(pEngine, ObjDefnID, PropertySpecs); + DefineMethods(pEngine, ObjDefnID, MethodSpecs); +} + +// static +v8::Local<v8::Array> color::ConvertPWLColorToArray(CJS_Runtime* pRuntime, + const CFX_Color& color) { + v8::Local<v8::Array> array; + switch (color.nColorType) { + case CFX_Color::kTransparent: + array = pRuntime->NewArray(); + pRuntime->PutArrayElement(array, 0, pRuntime->NewString(L"T")); + break; + case CFX_Color::kGray: + array = pRuntime->NewArray(); + pRuntime->PutArrayElement(array, 0, pRuntime->NewString(L"G")); + pRuntime->PutArrayElement(array, 1, pRuntime->NewNumber(color.fColor1)); + break; + case CFX_Color::kRGB: + array = pRuntime->NewArray(); + pRuntime->PutArrayElement(array, 0, pRuntime->NewString(L"RGB")); + pRuntime->PutArrayElement(array, 1, pRuntime->NewNumber(color.fColor1)); + pRuntime->PutArrayElement(array, 2, pRuntime->NewNumber(color.fColor2)); + pRuntime->PutArrayElement(array, 3, pRuntime->NewNumber(color.fColor3)); + break; + case CFX_Color::kCMYK: + array = pRuntime->NewArray(); + pRuntime->PutArrayElement(array, 0, pRuntime->NewString(L"CMYK")); + pRuntime->PutArrayElement(array, 1, pRuntime->NewNumber(color.fColor1)); + pRuntime->PutArrayElement(array, 2, pRuntime->NewNumber(color.fColor2)); + pRuntime->PutArrayElement(array, 3, pRuntime->NewNumber(color.fColor3)); + pRuntime->PutArrayElement(array, 4, pRuntime->NewNumber(color.fColor4)); + break; + } + return array; +} + +// static +CFX_Color color::ConvertArrayToPWLColor(CJS_Runtime* pRuntime, + v8::Local<v8::Array> array) { + int nArrayLen = pRuntime->GetArrayLength(array); + if (nArrayLen < 1) + return CFX_Color(); + + WideString sSpace = + pRuntime->ToWideString(pRuntime->GetArrayElement(array, 0)); + if (sSpace == L"T") + return CFX_Color(CFX_Color::kTransparent); + + float d1 = 0; + if (nArrayLen > 1) { + d1 = static_cast<float>( + pRuntime->ToDouble(pRuntime->GetArrayElement(array, 1))); + } + + if (sSpace == L"G") + return CFX_Color(CFX_Color::kGray, d1); + + float d2 = 0; + float d3 = 0; + if (nArrayLen > 2) { + d2 = static_cast<float>( + pRuntime->ToDouble(pRuntime->GetArrayElement(array, 2))); + } + if (nArrayLen > 3) { + d3 = static_cast<float>( + pRuntime->ToDouble(pRuntime->GetArrayElement(array, 3))); + } + + if (sSpace == L"RGB") + return CFX_Color(CFX_Color::kRGB, d1, d2, d3); + + float d4 = 0; + if (nArrayLen > 4) { + d4 = static_cast<float>( + pRuntime->ToDouble(pRuntime->GetArrayElement(array, 4))); + } + if (sSpace == L"CMYK") + return CFX_Color(CFX_Color::kCMYK, d1, d2, d3, d4); + + return CFX_Color(); +} + +color::color(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) { + m_crTransparent = CFX_Color(CFX_Color::kTransparent); + m_crBlack = CFX_Color(CFX_Color::kGray, 0); + m_crWhite = CFX_Color(CFX_Color::kGray, 1); + m_crRed = CFX_Color(CFX_Color::kRGB, 1, 0, 0); + m_crGreen = CFX_Color(CFX_Color::kRGB, 0, 1, 0); + m_crBlue = CFX_Color(CFX_Color::kRGB, 0, 0, 1); + m_crCyan = CFX_Color(CFX_Color::kCMYK, 1, 0, 0, 0); + m_crMagenta = CFX_Color(CFX_Color::kCMYK, 0, 1, 0, 0); + m_crYellow = CFX_Color(CFX_Color::kCMYK, 0, 0, 1, 0); + m_crDKGray = CFX_Color(CFX_Color::kGray, 0.25); + m_crGray = CFX_Color(CFX_Color::kGray, 0.5); + m_crLTGray = CFX_Color(CFX_Color::kGray, 0.75); +} + +color::~color() {} + +CJS_Return color::get_transparent(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crTransparent); +} + +CJS_Return color::set_transparent(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crTransparent); +} + +CJS_Return color::get_black(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crBlack); +} + +CJS_Return color::set_black(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crBlack); +} + +CJS_Return color::get_white(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crWhite); +} + +CJS_Return color::set_white(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crWhite); +} + +CJS_Return color::get_red(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crRed); +} + +CJS_Return color::set_red(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crRed); +} + +CJS_Return color::get_green(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crGreen); +} + +CJS_Return color::set_green(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crGreen); +} + +CJS_Return color::get_blue(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crBlue); +} + +CJS_Return color::set_blue(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crBlue); +} + +CJS_Return color::get_cyan(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crCyan); +} + +CJS_Return color::set_cyan(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crCyan); +} + +CJS_Return color::get_magenta(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crMagenta); +} + +CJS_Return color::set_magenta(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crMagenta); +} + +CJS_Return color::get_yellow(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crYellow); +} + +CJS_Return color::set_yellow(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crYellow); +} + +CJS_Return color::get_dark_gray(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crDKGray); +} + +CJS_Return color::set_dark_gray(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crDKGray); +} + +CJS_Return color::get_gray(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crGray); +} + +CJS_Return color::set_gray(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crGray); +} + +CJS_Return color::get_light_gray(CJS_Runtime* pRuntime) { + return GetPropertyHelper(pRuntime, &m_crLTGray); +} + +CJS_Return color::set_light_gray(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return SetPropertyHelper(pRuntime, vp, &m_crLTGray); +} + +CJS_Return color::GetPropertyHelper(CJS_Runtime* pRuntime, CFX_Color* var) { + v8::Local<v8::Value> array = ConvertPWLColorToArray(pRuntime, *var); + if (array.IsEmpty()) + return CJS_Return(pRuntime->NewArray()); + return CJS_Return(array); +} + +CJS_Return color::SetPropertyHelper(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp, + CFX_Color* var) { + if (vp.IsEmpty() || !vp->IsArray()) + return CJS_Return(false); + + *var = ConvertArrayToPWLColor(pRuntime, pRuntime->ToArray(vp)); + return CJS_Return(true); +} + +CJS_Return color::convert(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + int iSize = params.size(); + if (iSize < 2) + return CJS_Return(false); + if (params[0].IsEmpty() || !params[0]->IsArray()) + return CJS_Return(false); + + WideString sDestSpace = pRuntime->ToWideString(params[1]); + int nColorType = CFX_Color::kTransparent; + if (sDestSpace == L"T") + nColorType = CFX_Color::kTransparent; + else if (sDestSpace == L"G") + nColorType = CFX_Color::kGray; + else if (sDestSpace == L"RGB") + nColorType = CFX_Color::kRGB; + else if (sDestSpace == L"CMYK") + nColorType = CFX_Color::kCMYK; + + CFX_Color color = + ConvertArrayToPWLColor(pRuntime, pRuntime->ToArray(params[0])); + + v8::Local<v8::Value> array = + ConvertPWLColorToArray(pRuntime, color.ConvertColorType(nColorType)); + if (array.IsEmpty()) + return CJS_Return(pRuntime->NewArray()); + return CJS_Return(array); +} + +CJS_Return color::equal(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() < 2) + return CJS_Return(false); + if (params[0].IsEmpty() || !params[0]->IsArray() || params[1].IsEmpty() || + !params[1]->IsArray()) { + return CJS_Return(false); + } + + CFX_Color color1 = + ConvertArrayToPWLColor(pRuntime, pRuntime->ToArray(params[0])); + CFX_Color color2 = + ConvertArrayToPWLColor(pRuntime, pRuntime->ToArray(params[1])); + + color1 = color1.ConvertColorType(color2.nColorType); + return CJS_Return(pRuntime->NewBoolean(color1 == color2)); +} diff --git a/fxjs/cjs_color.h b/fxjs/cjs_color.h new file mode 100644 index 0000000000..5f7c1e5e30 --- /dev/null +++ b/fxjs/cjs_color.h @@ -0,0 +1,115 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_COLOR_H_ +#define FXJS_CJS_COLOR_H_ + +#include <vector> + +#include "fpdfsdk/pwl/cpwl_wnd.h" +#include "fxjs/JS_Define.h" + +class color : public CJS_EmbedObj { + public: + static v8::Local<v8::Array> ConvertPWLColorToArray(CJS_Runtime* pRuntime, + const CFX_Color& color); + static CFX_Color ConvertArrayToPWLColor(CJS_Runtime* pRuntime, + v8::Local<v8::Array> array); + + explicit color(CJS_Object* pJSObject); + ~color() override; + + CJS_Return get_black(CJS_Runtime* pRuntime); + CJS_Return set_black(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_blue(CJS_Runtime* pRuntime); + CJS_Return set_blue(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_cyan(CJS_Runtime* pRuntime); + CJS_Return set_cyan(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_dark_gray(CJS_Runtime* pRuntime); + CJS_Return set_dark_gray(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_gray(CJS_Runtime* pRuntime); + CJS_Return set_gray(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_green(CJS_Runtime* pRuntime); + CJS_Return set_green(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_light_gray(CJS_Runtime* pRuntime); + CJS_Return set_light_gray(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_magenta(CJS_Runtime* pRuntime); + CJS_Return set_magenta(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_red(CJS_Runtime* pRuntime); + CJS_Return set_red(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_transparent(CJS_Runtime* pRuntime); + CJS_Return set_transparent(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_white(CJS_Runtime* pRuntime); + CJS_Return set_white(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_yellow(CJS_Runtime* pRuntime); + CJS_Return set_yellow(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return convert(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return equal(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + + private: + CJS_Return GetPropertyHelper(CJS_Runtime* pRuntime, CFX_Color* val); + CJS_Return SetPropertyHelper(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp, + CFX_Color* val); + + CFX_Color m_crTransparent; + CFX_Color m_crBlack; + CFX_Color m_crWhite; + CFX_Color m_crRed; + CFX_Color m_crGreen; + CFX_Color m_crBlue; + CFX_Color m_crCyan; + CFX_Color m_crMagenta; + CFX_Color m_crYellow; + CFX_Color m_crDKGray; + CFX_Color m_crGray; + CFX_Color m_crLTGray; +}; + +class CJS_Color : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Color(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Color() override {} + + JS_STATIC_PROP(black, black, color); + JS_STATIC_PROP(blue, blue, color); + JS_STATIC_PROP(cyan, cyan, color); + JS_STATIC_PROP(dkGray, dark_gray, color); + JS_STATIC_PROP(gray, gray, color); + JS_STATIC_PROP(green, green, color); + JS_STATIC_PROP(ltGray, light_gray, color); + JS_STATIC_PROP(magenta, magenta, color); + JS_STATIC_PROP(red, red, color); + JS_STATIC_PROP(transparent, transparent, color); + JS_STATIC_PROP(white, white, color); + JS_STATIC_PROP(yellow, yellow, color); + + JS_STATIC_METHOD(convert, color); + JS_STATIC_METHOD(equal, color); + + private: + static int ObjDefnID; + static const JSPropertySpec PropertySpecs[]; + static const JSMethodSpec MethodSpecs[]; +}; + +#endif // FXJS_CJS_COLOR_H_ diff --git a/fxjs/cjs_console.cpp b/fxjs/cjs_console.cpp new file mode 100644 index 0000000000..c5e23a3c27 --- /dev/null +++ b/fxjs/cjs_console.cpp @@ -0,0 +1,54 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_console.h" + +#include <vector> + +#include "fxjs/JS_Define.h" +#include "fxjs/cjs_event_context.h" +#include "fxjs/cjs_eventhandler.h" +#include "fxjs/cjs_object.h" + +const JSMethodSpec CJS_Console::MethodSpecs[] = {{"clear", clear_static}, + {"hide", hide_static}, + {"println", println_static}, + {"show", show_static}, + {0, 0}}; + +int CJS_Console::ObjDefnID = -1; + +// static +void CJS_Console::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = pEngine->DefineObj("console", FXJSOBJTYPE_STATIC, + JSConstructor<CJS_Console, console>, + JSDestructor<CJS_Console>); + DefineMethods(pEngine, ObjDefnID, MethodSpecs); +} + +console::console(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} + +console::~console() {} + +CJS_Return console::clear(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return console::hide(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return console::println(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(params.size() > 0); +} + +CJS_Return console::show(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} diff --git a/fxjs/cjs_console.h b/fxjs/cjs_console.h new file mode 100644 index 0000000000..43a55bc7b0 --- /dev/null +++ b/fxjs/cjs_console.h @@ -0,0 +1,47 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_CONSOLE_H_ +#define FXJS_CJS_CONSOLE_H_ + +#include <vector> + +#include "fxjs/JS_Define.h" + +class console : public CJS_EmbedObj { + public: + explicit console(CJS_Object* pJSObject); + ~console() override; + + public: + CJS_Return clear(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return hide(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return println(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return show(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); +}; + +class CJS_Console : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Console(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Console() override {} + + JS_STATIC_METHOD(clear, console); + JS_STATIC_METHOD(hide, console); + JS_STATIC_METHOD(println, console); + JS_STATIC_METHOD(show, console); + + private: + static int ObjDefnID; + static const JSMethodSpec MethodSpecs[]; +}; + +#endif // FXJS_CJS_CONSOLE_H_ diff --git a/fxjs/cjs_delaydata.cpp b/fxjs/cjs_delaydata.cpp new file mode 100644 index 0000000000..d7e1f78004 --- /dev/null +++ b/fxjs/cjs_delaydata.cpp @@ -0,0 +1,12 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_delaydata.h" + +CJS_DelayData::CJS_DelayData(FIELD_PROP prop, int idx, const WideString& name) + : eProp(prop), nControlIndex(idx), sFieldName(name) {} + +CJS_DelayData::~CJS_DelayData() {} diff --git a/fxjs/cjs_delaydata.h b/fxjs/cjs_delaydata.h new file mode 100644 index 0000000000..deeb65d56f --- /dev/null +++ b/fxjs/cjs_delaydata.h @@ -0,0 +1,35 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_DELAYDATA_H_ +#define FXJS_CJS_DELAYDATA_H_ + +#include <string> +#include <vector> + +#include "core/fxcrt/fx_coordinates.h" +#include "core/fxcrt/widestring.h" +#include "core/fxge/cfx_color.h" +#include "fxjs/cjs_field.h" + +struct CJS_DelayData { + CJS_DelayData(FIELD_PROP prop, int idx, const WideString& name); + ~CJS_DelayData(); + + FIELD_PROP eProp; + int nControlIndex; + WideString sFieldName; + int32_t num; + bool b; + ByteString string; + WideString widestring; + CFX_FloatRect rect; + CFX_Color color; + std::vector<uint32_t> wordarray; + std::vector<WideString> widestringarray; +}; + +#endif // FXJS_CJS_DELAYDATA_H_ diff --git a/fxjs/cjs_display.cpp b/fxjs/cjs_display.cpp new file mode 100644 index 0000000000..a52b51d462 --- /dev/null +++ b/fxjs/cjs_display.cpp @@ -0,0 +1,23 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_display.h" + +const JSConstSpec CJS_Display::ConstSpecs[] = { + {"visible", JSConstSpec::Number, 0, 0}, + {"hidden", JSConstSpec::Number, 1, 0}, + {"noPrint", JSConstSpec::Number, 2, 0}, + {"noView", JSConstSpec::Number, 3, 0}, + {0, JSConstSpec::Number, 0, 0}}; + +int CJS_Display::ObjDefnID = -1; + +// static +void CJS_Display::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("display", FXJSOBJTYPE_STATIC, nullptr, nullptr); + DefineConsts(pEngine, ObjDefnID, ConstSpecs); +} diff --git a/fxjs/cjs_display.h b/fxjs/cjs_display.h new file mode 100644 index 0000000000..7b13c38cbe --- /dev/null +++ b/fxjs/cjs_display.h @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_DISPLAY_H_ +#define FXJS_CJS_DISPLAY_H_ + +#include "fxjs/JS_Define.h" + +class CJS_Display : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Display(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Display() override {} + + private: + static int ObjDefnID; + static const JSConstSpec ConstSpecs[]; +}; + +#endif // FXJS_CJS_DISPLAY_H_ diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp new file mode 100644 index 0000000000..5383c9e45e --- /dev/null +++ b/fxjs/cjs_document.cpp @@ -0,0 +1,1503 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_document.h" + +#include <utility> + +#include "core/fpdfapi/font/cpdf_font.h" +#include "core/fpdfapi/page/cpdf_pageobject.h" +#include "core/fpdfapi/page/cpdf_textobject.h" +#include "core/fpdfapi/parser/cpdf_array.h" +#include "core/fpdfapi/parser/cpdf_name.h" +#include "core/fpdfapi/parser/cpdf_string.h" +#include "core/fpdfdoc/cpdf_interform.h" +#include "core/fpdfdoc/cpdf_nametree.h" +#include "fpdfsdk/cpdfsdk_annotiteration.h" +#include "fpdfsdk/cpdfsdk_interform.h" +#include "fpdfsdk/cpdfsdk_pageview.h" +#include "fxjs/cjs_annot.h" +#include "fxjs/cjs_app.h" +#include "fxjs/cjs_delaydata.h" +#include "fxjs/cjs_field.h" +#include "fxjs/cjs_icon.h" +#include "fxjs/cjs_printparamsobj.h" +#include "fxjs/js_resources.h" + +const JSPropertySpec CJS_Document::PropertySpecs[] = { + {"ADBE", get_ADBE_static, set_ADBE_static}, + {"author", get_author_static, set_author_static}, + {"baseURL", get_base_URL_static, set_base_URL_static}, + {"bookmarkRoot", get_bookmark_root_static, set_bookmark_root_static}, + {"calculate", get_calculate_static, set_calculate_static}, + {"Collab", get_collab_static, set_collab_static}, + {"creationDate", get_creation_date_static, set_creation_date_static}, + {"creator", get_creator_static, set_creator_static}, + {"delay", get_delay_static, set_delay_static}, + {"dirty", get_dirty_static, set_dirty_static}, + {"documentFileName", get_document_file_name_static, + set_document_file_name_static}, + {"external", get_external_static, set_external_static}, + {"filesize", get_filesize_static, set_filesize_static}, + {"icons", get_icons_static, set_icons_static}, + {"info", get_info_static, set_info_static}, + {"keywords", get_keywords_static, set_keywords_static}, + {"layout", get_layout_static, set_layout_static}, + {"media", get_media_static, set_media_static}, + {"modDate", get_mod_date_static, set_mod_date_static}, + {"mouseX", get_mouse_x_static, set_mouse_x_static}, + {"mouseY", get_mouse_y_static, set_mouse_y_static}, + {"numFields", get_num_fields_static, set_num_fields_static}, + {"numPages", get_num_pages_static, set_num_pages_static}, + {"pageNum", get_page_num_static, set_page_num_static}, + {"pageWindowRect", get_page_window_rect_static, + set_page_window_rect_static}, + {"path", get_path_static, set_path_static}, + {"producer", get_producer_static, set_producer_static}, + {"subject", get_subject_static, set_subject_static}, + {"title", get_title_static, set_title_static}, + {"URL", get_URL_static, set_URL_static}, + {"zoom", get_zoom_static, set_zoom_static}, + {"zoomType", get_zoom_type_static, set_zoom_type_static}, + {0, 0, 0}}; + +const JSMethodSpec CJS_Document::MethodSpecs[] = { + {"addAnnot", addAnnot_static}, + {"addField", addField_static}, + {"addLink", addLink_static}, + {"addIcon", addIcon_static}, + {"calculateNow", calculateNow_static}, + {"closeDoc", closeDoc_static}, + {"createDataObject", createDataObject_static}, + {"deletePages", deletePages_static}, + {"exportAsText", exportAsText_static}, + {"exportAsFDF", exportAsFDF_static}, + {"exportAsXFDF", exportAsXFDF_static}, + {"extractPages", extractPages_static}, + {"getAnnot", getAnnot_static}, + {"getAnnots", getAnnots_static}, + {"getAnnot3D", getAnnot3D_static}, + {"getAnnots3D", getAnnots3D_static}, + {"getField", getField_static}, + {"getIcon", getIcon_static}, + {"getLinks", getLinks_static}, + {"getNthFieldName", getNthFieldName_static}, + {"getOCGs", getOCGs_static}, + {"getPageBox", getPageBox_static}, + {"getPageNthWord", getPageNthWord_static}, + {"getPageNthWordQuads", getPageNthWordQuads_static}, + {"getPageNumWords", getPageNumWords_static}, + {"getPrintParams", getPrintParams_static}, + {"getURL", getURL_static}, + {"gotoNamedDest", gotoNamedDest_static}, + {"importAnFDF", importAnFDF_static}, + {"importAnXFDF", importAnXFDF_static}, + {"importTextData", importTextData_static}, + {"insertPages", insertPages_static}, + {"mailForm", mailForm_static}, + {"print", print_static}, + {"removeField", removeField_static}, + {"replacePages", replacePages_static}, + {"resetForm", resetForm_static}, + {"removeIcon", removeIcon_static}, + {"saveAs", saveAs_static}, + {"submitForm", submitForm_static}, + {"syncAnnotScan", syncAnnotScan_static}, + {"mailDoc", mailDoc_static}, + {0, 0}}; + +int CJS_Document::ObjDefnID = -1; + +// static +int CJS_Document::GetObjDefnID() { + return ObjDefnID; +} + +// static +void CJS_Document::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = pEngine->DefineObj("Document", FXJSOBJTYPE_GLOBAL, + JSConstructor<CJS_Document, Document>, + JSDestructor<CJS_Document>); + DefineProps(pEngine, ObjDefnID, PropertySpecs); + DefineMethods(pEngine, ObjDefnID, MethodSpecs); +} + +void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) { + CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime); + Document* pDoc = static_cast<Document*>(GetEmbedObject()); + pDoc->SetFormFillEnv(pRuntime->GetFormFillEnv()); +} + +Document::Document(CJS_Object* pJSObject) + : CJS_EmbedObj(pJSObject), + m_pFormFillEnv(nullptr), + m_cwBaseURL(L""), + m_bDelay(false) {} + +Document::~Document() {} + +// The total number of fields in document. +CJS_Return Document::get_num_fields(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + CPDF_InterForm* pPDFForm = pInterForm->GetInterForm(); + return CJS_Return(pRuntime->NewNumber( + static_cast<int>(pPDFForm->CountFields(WideString())))); +} + +CJS_Return Document::set_num_fields(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSREADONLY)); +} + +CJS_Return Document::get_dirty(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + return CJS_Return(pRuntime->NewBoolean(!!m_pFormFillEnv->GetChangeMark())); +} + +CJS_Return Document::set_dirty(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + pRuntime->ToBoolean(vp) ? m_pFormFillEnv->SetChangeMark() + : m_pFormFillEnv->ClearChangeMark(); + return CJS_Return(true); +} + +CJS_Return Document::get_ADBE(CJS_Runtime* pRuntime) { + return CJS_Return(pRuntime->NewUndefined()); +} + +CJS_Return Document::set_ADBE(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::get_page_num(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView(); + if (!pPageView) + return CJS_Return(pRuntime->NewUndefined()); + return CJS_Return(pRuntime->NewNumber(pPageView->GetPageIndex())); +} + +CJS_Return Document::set_page_num(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + int iPageCount = m_pFormFillEnv->GetPageCount(); + int iPageNum = pRuntime->ToInt32(vp); + if (iPageNum >= 0 && iPageNum < iPageCount) + m_pFormFillEnv->JS_docgotoPage(iPageNum); + else if (iPageNum >= iPageCount) + m_pFormFillEnv->JS_docgotoPage(iPageCount - 1); + else if (iPageNum < 0) + m_pFormFillEnv->JS_docgotoPage(0); + + return CJS_Return(true); +} + +CJS_Return Document::addAnnot(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Not supported. + return CJS_Return(true); +} + +CJS_Return Document::addField(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Not supported. + return CJS_Return(true); +} + +CJS_Return Document::exportAsText( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::exportAsFDF( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::exportAsXFDF( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::getField(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() < 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + WideString wideName = pRuntime->ToWideString(params[0]); + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + CPDF_InterForm* pPDFForm = pInterForm->GetInterForm(); + if (pPDFForm->CountFields(wideName) <= 0) + return CJS_Return(pRuntime->NewUndefined()); + + v8::Local<v8::Object> pFieldObj = + pRuntime->NewFxDynamicObj(CJS_Field::GetObjDefnID()); + if (pFieldObj.IsEmpty()) + return CJS_Return(false); + + CJS_Field* pJSField = + static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj)); + Field* pField = static_cast<Field*>(pJSField->GetEmbedObject()); + pField->AttachField(this, wideName); + if (!pJSField) + return CJS_Return(false); + + return CJS_Return(pJSField->ToV8Object()); +} + +// Gets the name of the nth field in the document +CJS_Return Document::getNthFieldName( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + int nIndex = pRuntime->ToInt32(params[0]); + if (nIndex < 0) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSVALUEERROR)); + + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + CPDF_InterForm* pPDFForm = pInterForm->GetInterForm(); + CPDF_FormField* pField = pPDFForm->GetField(nIndex, WideString()); + if (!pField) + return CJS_Return(false); + return CJS_Return(pRuntime->NewString(pField->GetFullName().c_str())); +} + +CJS_Return Document::importAnFDF( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::importAnXFDF( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::importTextData( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +// exports the form data and mails the resulting fdf file as an attachment to +// all recipients. +// comment: need reader supports +CJS_Return Document::mailForm(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOPERMISSION)); + + int iLength = params.size(); + bool bUI = iLength > 0 ? pRuntime->ToBoolean(params[0]) : true; + WideString cTo = iLength > 1 ? pRuntime->ToWideString(params[1]) : L""; + WideString cCc = iLength > 2 ? pRuntime->ToWideString(params[2]) : L""; + WideString cBcc = iLength > 3 ? pRuntime->ToWideString(params[3]) : L""; + WideString cSubject = iLength > 4 ? pRuntime->ToWideString(params[4]) : L""; + WideString cMsg = iLength > 5 ? pRuntime->ToWideString(params[5]) : L""; + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + ByteString sTextBuf = pInterForm->ExportFormToFDFTextBuf(); + if (sTextBuf.GetLength() == 0) + return CJS_Return(false); + + size_t nBufSize = sTextBuf.GetLength(); + char* pMutableBuf = FX_Alloc(char, nBufSize); + memcpy(pMutableBuf, sTextBuf.c_str(), nBufSize); + + pRuntime->BeginBlock(); + CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv(); + pFormFillEnv->JS_docmailForm(pMutableBuf, nBufSize, bUI, cTo.c_str(), + cSubject.c_str(), cCc.c_str(), cBcc.c_str(), + cMsg.c_str()); + pRuntime->EndBlock(); + FX_Free(pMutableBuf); + return CJS_Return(true); +} + +CJS_Return Document::print(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + bool bUI = true; + int nStart = 0; + int nEnd = 0; + bool bSilent = false; + bool bShrinkToFit = false; + bool bPrintAsImage = false; + bool bReverse = false; + bool bAnnotations = false; + int nlength = params.size(); + if (nlength == 9) { + if (params[8]->IsObject()) { + v8::Local<v8::Object> pObj = pRuntime->ToObject(params[8]); + if (CFXJS_Engine::GetObjDefnID(pObj) == + CJS_PrintParamsObj::GetObjDefnID()) { + v8::Local<v8::Object> pObj = pRuntime->ToObject(params[8]); + CJS_Object* pJSObj = + static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(pObj)); + if (pJSObj) { + if (PrintParamsObj* pprintparamsObj = + static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) { + bUI = pprintparamsObj->bUI; + nStart = pprintparamsObj->nStart; + nEnd = pprintparamsObj->nEnd; + bSilent = pprintparamsObj->bSilent; + bShrinkToFit = pprintparamsObj->bShrinkToFit; + bPrintAsImage = pprintparamsObj->bPrintAsImage; + bReverse = pprintparamsObj->bReverse; + bAnnotations = pprintparamsObj->bAnnotations; + } + } + } + } + } else { + if (nlength >= 1) + bUI = pRuntime->ToBoolean(params[0]); + if (nlength >= 2) + nStart = pRuntime->ToInt32(params[1]); + if (nlength >= 3) + nEnd = pRuntime->ToInt32(params[2]); + if (nlength >= 4) + bSilent = pRuntime->ToBoolean(params[3]); + if (nlength >= 5) + bShrinkToFit = pRuntime->ToBoolean(params[4]); + if (nlength >= 6) + bPrintAsImage = pRuntime->ToBoolean(params[5]); + if (nlength >= 7) + bReverse = pRuntime->ToBoolean(params[6]); + if (nlength >= 8) + bAnnotations = pRuntime->ToBoolean(params[7]); + } + + if (!m_pFormFillEnv) + return CJS_Return(false); + + m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit, + bPrintAsImage, bReverse, bAnnotations); + return CJS_Return(true); +} + +// removes the specified field from the document. +// comment: +// note: if the filed name is not rational, adobe is dumb for it. + +CJS_Return Document::removeField( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) || + m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOPERMISSION)); + + WideString sFieldName = pRuntime->ToWideString(params[0]); + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + std::vector<CPDFSDK_Annot::ObservedPtr> widgets; + pInterForm->GetWidgets(sFieldName, &widgets); + if (widgets.empty()) + return CJS_Return(true); + + for (const auto& pAnnot : widgets) { + CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot.Get()); + if (!pWidget) + continue; + + CFX_FloatRect rcAnnot = pWidget->GetRect(); + --rcAnnot.left; + --rcAnnot.bottom; + ++rcAnnot.right; + ++rcAnnot.top; + + std::vector<CFX_FloatRect> aRefresh(1, rcAnnot); + UnderlyingPageType* pPage = pWidget->GetUnderlyingPage(); + ASSERT(pPage); + + // If there is currently no pageview associated with the page being used + // do not create one. We may be in the process of tearing down the document + // and creating a new pageview at this point will cause bad things. + CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false); + if (pPageView) { +#if PDF_ENABLE_XFA + pPageView->DeleteAnnot(pWidget); +#endif // PDF_ENABLE_XFA + pPageView->UpdateRects(aRefresh); + } + } + m_pFormFillEnv->SetChangeMark(); + + return CJS_Return(true); +} + +// reset filed values within a document. +// comment: +// note: if the fields names r not rational, aodbe is dumb for it. + +CJS_Return Document::resetForm( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) || + m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) || + m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOPERMISSION)); + } + + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + CPDF_InterForm* pPDFForm = pInterForm->GetInterForm(); + if (params.empty()) { + pPDFForm->ResetForm(true); + m_pFormFillEnv->SetChangeMark(); + return CJS_Return(true); + } + + v8::Local<v8::Array> array; + if (params[0]->IsString()) { + array = pRuntime->NewArray(); + pRuntime->PutArrayElement(array, 0, params[0]); + } else { + array = pRuntime->ToArray(params[0]); + } + + std::vector<CPDF_FormField*> aFields; + for (size_t i = 0; i < pRuntime->GetArrayLength(array); ++i) { + WideString swVal = + pRuntime->ToWideString(pRuntime->GetArrayElement(array, i)); + for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j) + aFields.push_back(pPDFForm->GetField(j, swVal)); + } + + if (!aFields.empty()) { + pPDFForm->ResetForm(aFields, true, true); + m_pFormFillEnv->SetChangeMark(); + } + + return CJS_Return(true); +} + +CJS_Return Document::saveAs(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::syncAnnotScan( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Document::submitForm( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + int nSize = params.size(); + if (nSize < 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + v8::Local<v8::Array> aFields; + WideString strURL; + bool bFDF = true; + bool bEmpty = false; + if (params[0]->IsString()) { + strURL = pRuntime->ToWideString(params[0]); + if (nSize > 1) + bFDF = pRuntime->ToBoolean(params[1]); + if (nSize > 2) + bEmpty = pRuntime->ToBoolean(params[2]); + if (nSize > 3) + aFields = pRuntime->ToArray(params[3]); + } else if (params[0]->IsObject()) { + v8::Local<v8::Object> pObj = pRuntime->ToObject(params[0]); + v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL"); + if (!pValue.IsEmpty()) + strURL = pRuntime->ToWideString(pValue); + + bFDF = pRuntime->ToBoolean(pRuntime->GetObjectProperty(pObj, L"bFDF")); + bEmpty = pRuntime->ToBoolean(pRuntime->GetObjectProperty(pObj, L"bEmpty")); + aFields = pRuntime->ToArray(pRuntime->GetObjectProperty(pObj, L"aFields")); + } + + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm(); + + if (pRuntime->GetArrayLength(aFields) == 0 && bEmpty) { + if (pPDFInterForm->CheckRequiredFields(nullptr, true)) { + pRuntime->BeginBlock(); + pInterForm->SubmitForm(strURL, false); + pRuntime->EndBlock(); + } + return CJS_Return(true); + } + + std::vector<CPDF_FormField*> fieldObjects; + for (size_t i = 0; i < pRuntime->GetArrayLength(aFields); ++i) { + WideString sName = + pRuntime->ToWideString(pRuntime->GetArrayElement(aFields, i)); + CPDF_InterForm* pPDFForm = pInterForm->GetInterForm(); + for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) { + CPDF_FormField* pField = pPDFForm->GetField(j, sName); + if (!bEmpty && pField->GetValue().IsEmpty()) + continue; + + fieldObjects.push_back(pField); + } + } + + if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) { + pRuntime->BeginBlock(); + pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF); + pRuntime->EndBlock(); + } + return CJS_Return(true); +} + +void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pFormFillEnv.Reset(pFormFillEnv); +} + +CJS_Return Document::get_bookmark_root(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Document::set_bookmark_root(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::mailDoc(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // TODO(tsepez): Check maximum number of allowed params. + bool bUI = true; + WideString cTo = L""; + WideString cCc = L""; + WideString cBcc = L""; + WideString cSubject = L""; + WideString cMsg = L""; + + if (params.size() >= 1) + bUI = pRuntime->ToBoolean(params[0]); + if (params.size() >= 2) + cTo = pRuntime->ToWideString(params[1]); + if (params.size() >= 3) + cCc = pRuntime->ToWideString(params[2]); + if (params.size() >= 4) + cBcc = pRuntime->ToWideString(params[3]); + if (params.size() >= 5) + cSubject = pRuntime->ToWideString(params[4]); + if (params.size() >= 6) + cMsg = pRuntime->ToWideString(params[5]); + + if (params.size() >= 1 && params[0]->IsObject()) { + v8::Local<v8::Object> pObj = pRuntime->ToObject(params[0]); + bUI = pRuntime->ToBoolean(pRuntime->GetObjectProperty(pObj, L"bUI")); + cTo = pRuntime->ToWideString(pRuntime->GetObjectProperty(pObj, L"cTo")); + cCc = pRuntime->ToWideString(pRuntime->GetObjectProperty(pObj, L"cCc")); + cBcc = pRuntime->ToWideString(pRuntime->GetObjectProperty(pObj, L"cBcc")); + cSubject = + pRuntime->ToWideString(pRuntime->GetObjectProperty(pObj, L"cSubject")); + cMsg = pRuntime->ToWideString(pRuntime->GetObjectProperty(pObj, L"cMsg")); + } + + pRuntime->BeginBlock(); + CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv(); + pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(), + cCc.c_str(), cBcc.c_str(), cMsg.c_str()); + pRuntime->EndBlock(); + return CJS_Return(true); +} + +CJS_Return Document::get_author(CJS_Runtime* pRuntime) { + return getPropertyInternal(pRuntime, "Author"); +} + +CJS_Return Document::set_author(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return setPropertyInternal(pRuntime, vp, "Author"); +} + +CJS_Return Document::get_info(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv) + CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + const auto* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo(); + if (!pDictionary) + return CJS_Return(false); + + WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author"); + WideString cwTitle = pDictionary->GetUnicodeTextFor("Title"); + WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject"); + WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords"); + WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator"); + WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer"); + WideString cwCreationDate = pDictionary->GetUnicodeTextFor("CreationDate"); + WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate"); + WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped"); + + v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1); + pRuntime->PutObjectProperty(pObj, L"Author", + pRuntime->NewString(cwAuthor.AsStringView())); + pRuntime->PutObjectProperty(pObj, L"Title", + pRuntime->NewString(cwTitle.AsStringView())); + pRuntime->PutObjectProperty(pObj, L"Subject", + pRuntime->NewString(cwSubject.AsStringView())); + pRuntime->PutObjectProperty(pObj, L"Keywords", + pRuntime->NewString(cwKeywords.AsStringView())); + pRuntime->PutObjectProperty(pObj, L"Creator", + pRuntime->NewString(cwCreator.AsStringView())); + pRuntime->PutObjectProperty(pObj, L"Producer", + pRuntime->NewString(cwProducer.AsStringView())); + pRuntime->PutObjectProperty( + pObj, L"CreationDate", + pRuntime->NewString(cwCreationDate.AsStringView())); + pRuntime->PutObjectProperty(pObj, L"ModDate", + pRuntime->NewString(cwModDate.AsStringView())); + pRuntime->PutObjectProperty(pObj, L"Trapped", + pRuntime->NewString(cwTrapped.AsStringView())); + + // It's to be compatible to non-standard info dictionary. + for (const auto& it : *pDictionary) { + const ByteString& bsKey = it.first; + CPDF_Object* pValueObj = it.second.get(); + WideString wsKey = WideString::FromUTF8(bsKey.AsStringView()); + if (pValueObj->IsString() || pValueObj->IsName()) { + pRuntime->PutObjectProperty( + pObj, wsKey, + pRuntime->NewString(pValueObj->GetUnicodeText().AsStringView())); + } else if (pValueObj->IsNumber()) { + pRuntime->PutObjectProperty(pObj, wsKey, + pRuntime->NewNumber(pValueObj->GetNumber())); + } else if (pValueObj->IsBoolean()) { + pRuntime->PutObjectProperty( + pObj, wsKey, pRuntime->NewBoolean(!!pValueObj->GetInteger())); + } + } + return CJS_Return(pObj); +} + +CJS_Return Document::set_info(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSREADONLY)); +} + +CJS_Return Document::getPropertyInternal(CJS_Runtime* pRuntime, + const ByteString& propName) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo(); + if (!pDictionary) + return CJS_Return(false); + return CJS_Return( + pRuntime->NewString(pDictionary->GetUnicodeTextFor(propName).c_str())); +} + +CJS_Return Document::setPropertyInternal(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp, + const ByteString& propName) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo(); + if (!pDictionary) + return CJS_Return(false); + + if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOPERMISSION)); + + WideString csProperty = pRuntime->ToWideString(vp); + pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty), + false); + m_pFormFillEnv->SetChangeMark(); + return CJS_Return(true); +} + +CJS_Return Document::get_creation_date(CJS_Runtime* pRuntime) { + return getPropertyInternal(pRuntime, "CreationDate"); +} + +CJS_Return Document::set_creation_date(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return setPropertyInternal(pRuntime, vp, "CreationDate"); +} + +CJS_Return Document::get_creator(CJS_Runtime* pRuntime) { + return getPropertyInternal(pRuntime, "Creator"); +} + +CJS_Return Document::set_creator(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return setPropertyInternal(pRuntime, vp, "Creator"); +} + +CJS_Return Document::get_delay(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + return CJS_Return(pRuntime->NewBoolean(m_bDelay)); +} + +CJS_Return Document::set_delay(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOPERMISSION)); + + m_bDelay = pRuntime->ToBoolean(vp); + if (m_bDelay) { + m_DelayData.clear(); + return CJS_Return(true); + } + + std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess; + DelayDataToProcess.swap(m_DelayData); + for (const auto& pData : DelayDataToProcess) + Field::DoDelay(m_pFormFillEnv.Get(), pData.get()); + + return CJS_Return(true); +} + +CJS_Return Document::get_keywords(CJS_Runtime* pRuntime) { + return getPropertyInternal(pRuntime, "Keywords"); +} + +CJS_Return Document::set_keywords(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return setPropertyInternal(pRuntime, vp, "Keywords"); +} + +CJS_Return Document::get_mod_date(CJS_Runtime* pRuntime) { + return getPropertyInternal(pRuntime, "ModDate"); +} + +CJS_Return Document::set_mod_date(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return setPropertyInternal(pRuntime, vp, "ModDate"); +} + +CJS_Return Document::get_producer(CJS_Runtime* pRuntime) { + return getPropertyInternal(pRuntime, "Producer"); +} + +CJS_Return Document::set_producer(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return setPropertyInternal(pRuntime, vp, "Producer"); +} + +CJS_Return Document::get_subject(CJS_Runtime* pRuntime) { + return getPropertyInternal(pRuntime, "Subject"); +} + +CJS_Return Document::set_subject(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return setPropertyInternal(pRuntime, vp, "Subject"); +} + +CJS_Return Document::get_title(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + return getPropertyInternal(pRuntime, "Title"); +} + +CJS_Return Document::set_title(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + return setPropertyInternal(pRuntime, vp, "Title"); +} + +CJS_Return Document::get_num_pages(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + return CJS_Return(pRuntime->NewNumber(m_pFormFillEnv->GetPageCount())); +} + +CJS_Return Document::set_num_pages(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSREADONLY)); +} + +CJS_Return Document::get_external(CJS_Runtime* pRuntime) { + // In Chrome case, should always return true. + return CJS_Return(pRuntime->NewBoolean(true)); +} + +CJS_Return Document::set_external(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::get_filesize(CJS_Runtime* pRuntime) { + return CJS_Return(pRuntime->NewNumber(0)); +} + +CJS_Return Document::set_filesize(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSREADONLY)); +} + +CJS_Return Document::get_mouse_x(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Document::set_mouse_x(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::get_mouse_y(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Document::set_mouse_y(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::get_URL(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + return CJS_Return( + pRuntime->NewString(m_pFormFillEnv->JS_docGetFilePath().c_str())); +} + +CJS_Return Document::set_URL(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSREADONLY)); +} + +CJS_Return Document::get_base_URL(CJS_Runtime* pRuntime) { + return CJS_Return(pRuntime->NewString(m_cwBaseURL.c_str())); +} + +CJS_Return Document::set_base_URL(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + m_cwBaseURL = pRuntime->ToWideString(vp); + return CJS_Return(true); +} + +CJS_Return Document::get_calculate(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + return CJS_Return(pRuntime->NewBoolean(!!pInterForm->IsCalculateEnabled())); +} + +CJS_Return Document::set_calculate(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + pInterForm->EnableCalculate(pRuntime->ToBoolean(vp)); + return CJS_Return(true); +} + +CJS_Return Document::get_document_file_name(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath(); + size_t i = wsFilePath.GetLength(); + for (; i > 0; i--) { + if (wsFilePath[i - 1] == L'\\' || wsFilePath[i - 1] == L'/') + break; + } + + if (i > 0 && i < wsFilePath.GetLength()) { + return CJS_Return( + pRuntime->NewString(wsFilePath.GetBuffer(wsFilePath.GetLength()) + i)); + } + return CJS_Return(pRuntime->NewString(L"")); +} + +CJS_Return Document::set_document_file_name(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSREADONLY)); +} + +CJS_Return Document::get_path(CJS_Runtime* pRuntime) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + return CJS_Return(pRuntime->NewString( + app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath()).c_str())); +} + +CJS_Return Document::set_path(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSREADONLY)); +} + +CJS_Return Document::get_page_window_rect(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Document::set_page_window_rect(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::get_layout(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Document::set_layout(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::addLink(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Document::closeDoc(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Document::getPageBox( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Document::getAnnot(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 2) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + int nPageNo = pRuntime->ToInt32(params[0]); + WideString swAnnotName = pRuntime->ToWideString(params[1]); + CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo); + if (!pPageView) + return CJS_Return(false); + + CPDFSDK_AnnotIteration annotIteration(pPageView, false); + CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr; + for (const auto& pSDKAnnotCur : annotIteration) { + CPDFSDK_BAAnnot* pBAAnnot = + static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()); + if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) { + pSDKBAAnnot = pBAAnnot; + break; + } + } + if (!pSDKBAAnnot) + return CJS_Return(false); + + v8::Local<v8::Object> pObj = + pRuntime->NewFxDynamicObj(CJS_Annot::GetObjDefnID()); + if (pObj.IsEmpty()) + return CJS_Return(false); + + CJS_Annot* pJS_Annot = + static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj)); + if (!pJS_Annot) + return CJS_Return(false); + + Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject()); + pAnnot->SetSDKAnnot(pSDKBAAnnot); + + return CJS_Return(pJS_Annot->ToV8Object()); +} + +CJS_Return Document::getAnnots( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + // TODO(tonikitoo): Add support supported parameters as per + // the PDF spec. + + int nPageNo = m_pFormFillEnv->GetPageCount(); + v8::Local<v8::Array> annots = pRuntime->NewArray(); + for (int i = 0; i < nPageNo; ++i) { + CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i); + if (!pPageView) + return CJS_Return(false); + + CPDFSDK_AnnotIteration annotIteration(pPageView, false); + for (const auto& pSDKAnnotCur : annotIteration) { + if (!pSDKAnnotCur) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + v8::Local<v8::Object> pObj = + pRuntime->NewFxDynamicObj(CJS_Annot::GetObjDefnID()); + if (pObj.IsEmpty()) + return CJS_Return(false); + + CJS_Annot* pJS_Annot = + static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj)); + Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject()); + pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get())); + pRuntime->PutArrayElement( + annots, i, + pJS_Annot ? v8::Local<v8::Value>(pJS_Annot->ToV8Object()) + : v8::Local<v8::Value>()); + } + } + return CJS_Return(annots); +} + +CJS_Return Document::getAnnot3D( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(pRuntime->NewUndefined()); +} + +CJS_Return Document::getAnnots3D( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Document::getOCGs(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Document::getLinks(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) { + return (rect.left <= LinkRect.left && rect.top <= LinkRect.top && + rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom); +} + +CJS_Return Document::addIcon(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 2) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + WideString swIconName = pRuntime->ToWideString(params[0]); + if (!params[1]->IsObject()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSTYPEERROR)); + + v8::Local<v8::Object> pJSIcon = pRuntime->ToObject(params[1]); + if (CFXJS_Engine::GetObjDefnID(pJSIcon) != CJS_Icon::GetObjDefnID()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSTYPEERROR)); + + v8::Local<v8::Object> pObj = pRuntime->ToObject(params[1]); + CJS_Object* obj = static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(pObj)); + if (!obj->GetEmbedObject()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSTYPEERROR)); + + m_IconNames.push_back(swIconName); + return CJS_Return(true); +} + +CJS_Return Document::get_icons(CJS_Runtime* pRuntime) { + if (m_IconNames.empty()) + return CJS_Return(pRuntime->NewUndefined()); + + v8::Local<v8::Array> Icons = pRuntime->NewArray(); + int i = 0; + for (const auto& name : m_IconNames) { + v8::Local<v8::Object> pObj = + pRuntime->NewFxDynamicObj(CJS_Icon::GetObjDefnID()); + if (pObj.IsEmpty()) + return CJS_Return(false); + + CJS_Icon* pJS_Icon = + static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj)); + Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject()); + pIcon->SetIconName(name); + pRuntime->PutArrayElement(Icons, i++, + pJS_Icon + ? v8::Local<v8::Value>(pJS_Icon->ToV8Object()) + : v8::Local<v8::Value>()); + } + return CJS_Return(Icons); +} + +CJS_Return Document::set_icons(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSREADONLY)); +} + +CJS_Return Document::getIcon(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + WideString swIconName = pRuntime->ToWideString(params[0]); + auto it = std::find(m_IconNames.begin(), m_IconNames.end(), swIconName); + if (it == m_IconNames.end()) + return CJS_Return(false); + + v8::Local<v8::Object> pObj = + pRuntime->NewFxDynamicObj(CJS_Icon::GetObjDefnID()); + if (pObj.IsEmpty()) + return CJS_Return(false); + + CJS_Icon* pJS_Icon = static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj)); + if (!pJS_Icon) + return CJS_Return(false); + + Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject()); + pIcon->SetIconName(*it); + return CJS_Return(pJS_Icon->ToV8Object()); +} + +CJS_Return Document::removeIcon( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, no supported. + return CJS_Return(true); +} + +CJS_Return Document::createDataObject( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not implemented. + return CJS_Return(true); +} + +CJS_Return Document::get_media(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Document::set_media(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::calculateNow( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) || + m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) || + m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOPERMISSION)); + } + + m_pFormFillEnv->GetInterForm()->OnCalculate(); + return CJS_Return(true); +} + +CJS_Return Document::get_collab(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Document::set_collab(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::getPageNthWord( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOPERMISSION)); + + // TODO(tsepez): check maximum allowable params. + + int nPageNo = params.size() > 0 ? pRuntime->ToInt32(params[0]) : 0; + int nWordNo = params.size() > 1 ? pRuntime->ToInt32(params[1]) : 0; + bool bStrip = params.size() > 2 ? pRuntime->ToBoolean(params[2]) : true; + + CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument(); + if (!pDocument) + return CJS_Return(false); + + if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSVALUEERROR)); + + CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo); + if (!pPageDict) + return CJS_Return(false); + + CPDF_Page page(pDocument, pPageDict, true); + page.ParseContent(); + + int nWords = 0; + WideString swRet; + for (auto& pPageObj : *page.GetPageObjectList()) { + if (pPageObj->IsText()) { + CPDF_TextObject* pTextObj = pPageObj->AsText(); + int nObjWords = CountWords(pTextObj); + if (nWords + nObjWords >= nWordNo) { + swRet = GetObjWordStr(pTextObj, nWordNo - nWords); + break; + } + nWords += nObjWords; + } + } + + if (bStrip) { + swRet.TrimLeft(); + swRet.TrimRight(); + } + return CJS_Return(pRuntime->NewString(swRet.c_str())); +} + +CJS_Return Document::getPageNthWordQuads( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + return CJS_Return(false); +} + +CJS_Return Document::getPageNumWords( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOPERMISSION)); + + int nPageNo = params.size() > 0 ? pRuntime->ToInt32(params[0]) : 0; + CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument(); + if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSVALUEERROR)); + + CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo); + if (!pPageDict) + return CJS_Return(false); + + CPDF_Page page(pDocument, pPageDict, true); + page.ParseContent(); + + int nWords = 0; + for (auto& pPageObj : *page.GetPageObjectList()) { + if (pPageObj->IsText()) + nWords += CountWords(pPageObj->AsText()); + } + + return CJS_Return(pRuntime->NewNumber(nWords)); +} + +CJS_Return Document::getPrintParams( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + v8::Local<v8::Object> pRetObj = + pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::GetObjDefnID()); + if (pRetObj.IsEmpty()) + return CJS_Return(false); + + // Not implemented yet. + + return CJS_Return(pRetObj); +} + +#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF) + +int Document::CountWords(CPDF_TextObject* pTextObj) { + if (!pTextObj) + return 0; + + int nWords = 0; + + CPDF_Font* pFont = pTextObj->GetFont(); + if (!pFont) + return 0; + + bool bIsLatin = false; + + for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) { + uint32_t charcode = CPDF_Font::kInvalidCharCode; + float kerning; + + pTextObj->GetCharInfo(i, &charcode, &kerning); + WideString swUnicode = pFont->UnicodeFromCharCode(charcode); + + uint16_t unicode = 0; + if (swUnicode.GetLength() > 0) + unicode = swUnicode[0]; + + if (ISLATINWORD(unicode) && bIsLatin) + continue; + + bIsLatin = ISLATINWORD(unicode); + if (unicode != 0x20) + nWords++; + } + + return nWords; +} + +WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj, int nWordIndex) { + WideString swRet; + + CPDF_Font* pFont = pTextObj->GetFont(); + if (!pFont) + return L""; + + int nWords = 0; + bool bIsLatin = false; + + for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) { + uint32_t charcode = CPDF_Font::kInvalidCharCode; + float kerning; + + pTextObj->GetCharInfo(i, &charcode, &kerning); + WideString swUnicode = pFont->UnicodeFromCharCode(charcode); + + uint16_t unicode = 0; + if (swUnicode.GetLength() > 0) + unicode = swUnicode[0]; + + if (ISLATINWORD(unicode) && bIsLatin) { + } else { + bIsLatin = ISLATINWORD(unicode); + if (unicode != 0x20) + nWords++; + } + + if (nWords - 1 == nWordIndex) + swRet += unicode; + } + + return swRet; +} + +CJS_Return Document::get_zoom(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Document::set_zoom(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::get_zoom_type(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Document::set_zoom_type(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Document::deletePages( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::extractPages( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::insertPages( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::replacePages( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::getURL(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Document::gotoNamedDest( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + if (!m_pFormFillEnv) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + WideString wideName = pRuntime->ToWideString(params[0]); + CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument(); + if (!pDocument) + return CJS_Return(false); + + CPDF_NameTree nameTree(pDocument, "Dests"); + CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, wideName); + if (!destArray) + return CJS_Return(false); + + CPDF_Dest dest(destArray); + const CPDF_Array* arrayObject = ToArray(dest.GetObject()); + std::vector<float> scrollPositionArray; + if (arrayObject) { + for (size_t i = 2; i < arrayObject->GetCount(); i++) + scrollPositionArray.push_back(arrayObject->GetFloatAt(i)); + } + pRuntime->BeginBlock(); + m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(), + scrollPositionArray.data(), + scrollPositionArray.size()); + pRuntime->EndBlock(); + return CJS_Return(true); +} + +void Document::AddDelayData(CJS_DelayData* pData) { + m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData)); +} + +void Document::DoFieldDelay(const WideString& sFieldName, int nControlIndex) { + std::vector<std::unique_ptr<CJS_DelayData>> delayed_data; + auto iter = m_DelayData.begin(); + while (iter != m_DelayData.end()) { + auto old = iter++; + if ((*old)->sFieldName == sFieldName && + (*old)->nControlIndex == nControlIndex) { + delayed_data.push_back(std::move(*old)); + m_DelayData.erase(old); + } + } + + for (const auto& pData : delayed_data) + Field::DoDelay(m_pFormFillEnv.Get(), pData.get()); +} + +CJS_Document* Document::GetCJSDoc() const { + return static_cast<CJS_Document*>(m_pJSObject.Get()); +} diff --git a/fxjs/cjs_document.h b/fxjs/cjs_document.h new file mode 100644 index 0000000000..edfaf96200 --- /dev/null +++ b/fxjs/cjs_document.h @@ -0,0 +1,330 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_DOCUMENT_H_ +#define FXJS_CJS_DOCUMENT_H_ + +#include <list> +#include <memory> +#include <vector> + +#include "fxjs/JS_Define.h" + +class CJS_Document; +class CPDF_TextObject; + +struct CJS_DelayData; + +class Document : public CJS_EmbedObj { + public: + explicit Document(CJS_Object* pJSObject); + ~Document() override; + + CJS_Return get_ADBE(CJS_Runtime* pRuntime); + CJS_Return set_ADBE(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_author(CJS_Runtime* pRuntime); + CJS_Return set_author(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_base_URL(CJS_Runtime* pRuntime); + CJS_Return set_base_URL(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_bookmark_root(CJS_Runtime* pRuntime); + CJS_Return set_bookmark_root(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_calculate(CJS_Runtime* pRuntime); + CJS_Return set_calculate(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_collab(CJS_Runtime* pRuntime); + CJS_Return set_collab(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_creation_date(CJS_Runtime* pRuntime); + CJS_Return set_creation_date(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_creator(CJS_Runtime* pRuntime); + CJS_Return set_creator(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_delay(CJS_Runtime* pRuntime); + CJS_Return set_delay(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_dirty(CJS_Runtime* pRuntime); + CJS_Return set_dirty(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_document_file_name(CJS_Runtime* pRuntime); + CJS_Return set_document_file_name(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_external(CJS_Runtime* pRuntime); + CJS_Return set_external(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_filesize(CJS_Runtime* pRuntime); + CJS_Return set_filesize(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_icons(CJS_Runtime* pRuntime); + CJS_Return set_icons(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_info(CJS_Runtime* pRuntime); + CJS_Return set_info(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_keywords(CJS_Runtime* pRuntime); + CJS_Return set_keywords(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_layout(CJS_Runtime* pRuntime); + CJS_Return set_layout(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_media(CJS_Runtime* pRuntime); + CJS_Return set_media(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_mod_date(CJS_Runtime* pRuntime); + CJS_Return set_mod_date(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_mouse_x(CJS_Runtime* pRuntime); + CJS_Return set_mouse_x(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_mouse_y(CJS_Runtime* pRuntime); + CJS_Return set_mouse_y(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_num_fields(CJS_Runtime* pRuntime); + CJS_Return set_num_fields(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_num_pages(CJS_Runtime* pRuntime); + CJS_Return set_num_pages(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_page_num(CJS_Runtime* pRuntime); + CJS_Return set_page_num(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_page_window_rect(CJS_Runtime* pRuntime); + CJS_Return set_page_window_rect(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_path(CJS_Runtime* pRuntime); + CJS_Return set_path(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_producer(CJS_Runtime* pRuntime); + CJS_Return set_producer(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_subject(CJS_Runtime* pRuntime); + CJS_Return set_subject(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_title(CJS_Runtime* pRuntime); + CJS_Return set_title(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_zoom(CJS_Runtime* pRuntime); + CJS_Return set_zoom(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_zoom_type(CJS_Runtime* pRuntime); + CJS_Return set_zoom_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_URL(CJS_Runtime* pRuntime); + CJS_Return set_URL(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return addAnnot(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return addField(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return addLink(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return addIcon(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return calculateNow(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return closeDoc(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return createDataObject(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return deletePages(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return exportAsText(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return exportAsFDF(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return exportAsXFDF(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return extractPages(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getAnnot(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getAnnots(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getAnnot3D(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getAnnots3D(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getField(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getIcon(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getLinks(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getNthFieldName(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getOCGs(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getPageBox(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getPageNthWord(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getPageNthWordQuads( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getPageNumWords(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getPrintParams(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getURL(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return gotoNamedDest(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return importAnFDF(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return importAnXFDF(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return importTextData(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return insertPages(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return mailForm(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return print(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return removeField(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return replacePages(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return resetForm(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return saveAs(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return submitForm(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return syncAnnotScan(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return mailDoc(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return removeIcon(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + + void SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv); + CPDFSDK_FormFillEnvironment* GetFormFillEnv() const { + return m_pFormFillEnv.Get(); + } + void AddDelayData(CJS_DelayData* pData); + void DoFieldDelay(const WideString& sFieldName, int nControlIndex); + CJS_Document* GetCJSDoc() const; + + private: + bool IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect); + int CountWords(CPDF_TextObject* pTextObj); + WideString GetObjWordStr(CPDF_TextObject* pTextObj, int nWordIndex); + + CJS_Return getPropertyInternal(CJS_Runtime* pRuntime, + const ByteString& propName); + CJS_Return setPropertyInternal(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp, + const ByteString& propName); + + CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv; + WideString m_cwBaseURL; + std::list<std::unique_ptr<CJS_DelayData>> m_DelayData; + // Needs to be a std::list for iterator stability. + std::list<WideString> m_IconNames; + bool m_bDelay; +}; + +class CJS_Document : public CJS_Object { + public: + static int GetObjDefnID(); + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Document(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Document() override {} + + // CJS_Object + void InitInstance(IJS_Runtime* pIRuntime) override; + + JS_STATIC_PROP(ADBE, ADBE, Document); + JS_STATIC_PROP(author, author, Document); + JS_STATIC_PROP(baseURL, base_URL, Document); + JS_STATIC_PROP(bookmarkRoot, bookmark_root, Document); + JS_STATIC_PROP(calculate, calculate, Document); + JS_STATIC_PROP(Collab, collab, Document); + JS_STATIC_PROP(creationDate, creation_date, Document); + JS_STATIC_PROP(creator, creator, Document); + JS_STATIC_PROP(delay, delay, Document); + JS_STATIC_PROP(dirty, dirty, Document); + JS_STATIC_PROP(documentFileName, document_file_name, Document); + JS_STATIC_PROP(external, external, Document); + JS_STATIC_PROP(filesize, filesize, Document); + JS_STATIC_PROP(icons, icons, Document); + JS_STATIC_PROP(info, info, Document); + JS_STATIC_PROP(keywords, keywords, Document); + JS_STATIC_PROP(layout, layout, Document); + JS_STATIC_PROP(media, media, Document); + JS_STATIC_PROP(modDate, mod_date, Document); + JS_STATIC_PROP(mouseX, mouse_x, Document); + JS_STATIC_PROP(mouseY, mouse_y, Document); + JS_STATIC_PROP(numFields, num_fields, Document); + JS_STATIC_PROP(numPages, num_pages, Document); + JS_STATIC_PROP(pageNum, page_num, Document); + JS_STATIC_PROP(pageWindowRect, page_window_rect, Document); + JS_STATIC_PROP(path, path, Document); + JS_STATIC_PROP(producer, producer, Document); + JS_STATIC_PROP(subject, subject, Document); + JS_STATIC_PROP(title, title, Document); + JS_STATIC_PROP(URL, URL, Document); + JS_STATIC_PROP(zoom, zoom, Document); + JS_STATIC_PROP(zoomType, zoom_type, Document); + + JS_STATIC_METHOD(addAnnot, Document); + JS_STATIC_METHOD(addField, Document); + JS_STATIC_METHOD(addLink, Document); + JS_STATIC_METHOD(addIcon, Document); + JS_STATIC_METHOD(calculateNow, Document); + JS_STATIC_METHOD(closeDoc, Document); + JS_STATIC_METHOD(createDataObject, Document); + JS_STATIC_METHOD(deletePages, Document); + JS_STATIC_METHOD(exportAsText, Document); + JS_STATIC_METHOD(exportAsFDF, Document); + JS_STATIC_METHOD(exportAsXFDF, Document); + JS_STATIC_METHOD(extractPages, Document); + JS_STATIC_METHOD(getAnnot, Document); + JS_STATIC_METHOD(getAnnots, Document); + JS_STATIC_METHOD(getAnnot3D, Document); + JS_STATIC_METHOD(getAnnots3D, Document); + JS_STATIC_METHOD(getField, Document); + JS_STATIC_METHOD(getIcon, Document); + JS_STATIC_METHOD(getLinks, Document); + JS_STATIC_METHOD(getNthFieldName, Document); + JS_STATIC_METHOD(getOCGs, Document); + JS_STATIC_METHOD(getPageBox, Document); + JS_STATIC_METHOD(getPageNthWord, Document); + JS_STATIC_METHOD(getPageNthWordQuads, Document); + JS_STATIC_METHOD(getPageNumWords, Document); + JS_STATIC_METHOD(getPrintParams, Document); + JS_STATIC_METHOD(getURL, Document); + JS_STATIC_METHOD(gotoNamedDest, Document); + JS_STATIC_METHOD(importAnFDF, Document); + JS_STATIC_METHOD(importAnXFDF, Document); + JS_STATIC_METHOD(importTextData, Document); + JS_STATIC_METHOD(insertPages, Document); + JS_STATIC_METHOD(mailForm, Document); + JS_STATIC_METHOD(print, Document); + JS_STATIC_METHOD(removeField, Document); + JS_STATIC_METHOD(replacePages, Document); + JS_STATIC_METHOD(removeIcon, Document); + JS_STATIC_METHOD(resetForm, Document); + JS_STATIC_METHOD(saveAs, Document); + JS_STATIC_METHOD(submitForm, Document); + JS_STATIC_METHOD(syncAnnotScan, Document); + JS_STATIC_METHOD(mailDoc, Document); + + private: + static int ObjDefnID; + static const JSPropertySpec PropertySpecs[]; + static const JSMethodSpec MethodSpecs[]; +}; + +#endif // FXJS_CJS_DOCUMENT_H_ diff --git a/fxjs/cjs_embedobj.cpp b/fxjs/cjs_embedobj.cpp new file mode 100644 index 0000000000..cbebd6311b --- /dev/null +++ b/fxjs/cjs_embedobj.cpp @@ -0,0 +1,13 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_embedobj.h" + +#include "fxjs/cjs_object.h" + +CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {} + +CJS_EmbedObj::~CJS_EmbedObj() {} diff --git a/fxjs/cjs_embedobj.h b/fxjs/cjs_embedobj.h new file mode 100644 index 0000000000..82e549e321 --- /dev/null +++ b/fxjs/cjs_embedobj.h @@ -0,0 +1,25 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_EMBEDOBJ_H_ +#define FXJS_CJS_EMBEDOBJ_H_ + +#include "core/fxcrt/unowned_ptr.h" + +class CJS_Object; + +class CJS_EmbedObj { + public: + explicit CJS_EmbedObj(CJS_Object* pJSObject); + virtual ~CJS_EmbedObj(); + + CJS_Object* GetJSObject() const { return m_pJSObject.Get(); } + + protected: + UnownedPtr<CJS_Object> const m_pJSObject; +}; + +#endif // FXJS_CJS_EMBEDOBJ_H_ diff --git a/fxjs/cjs_event.cpp b/fxjs/cjs_event.cpp new file mode 100644 index 0000000000..4e20bbc8ba --- /dev/null +++ b/fxjs/cjs_event.cpp @@ -0,0 +1,308 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_event.h" + +#include "fxjs/JS_Define.h" +#include "fxjs/cjs_event_context.h" +#include "fxjs/cjs_eventhandler.h" +#include "fxjs/cjs_field.h" +#include "fxjs/cjs_object.h" + +const JSPropertySpec CJS_Event::PropertySpecs[] = { + {"change", get_change_static, set_change_static}, + {"changeEx", get_change_ex_static, set_change_ex_static}, + {"commitKey", get_commit_key_static, set_commit_key_static}, + {"fieldFull", get_field_full_static, set_field_full_static}, + {"keyDown", get_key_down_static, set_key_down_static}, + {"modifier", get_modifier_static, set_modifier_static}, + {"name", get_name_static, set_name_static}, + {"rc", get_rc_static, set_rc_static}, + {"richChange", get_rich_change_static, set_rich_change_static}, + {"richChangeEx", get_rich_change_ex_static, set_rich_change_ex_static}, + {"richValue", get_rich_value_static, set_rich_value_static}, + {"selEnd", get_sel_end_static, set_sel_end_static}, + {"selStart", get_sel_start_static, set_sel_start_static}, + {"shift", get_shift_static, set_shift_static}, + {"source", get_source_static, set_source_static}, + {"target", get_target_static, set_target_static}, + {"targetName", get_target_name_static, set_target_name_static}, + {"type", get_type_static, set_type_static}, + {"value", get_value_static, set_value_static}, + {"willCommit", get_will_commit_static, set_will_commit_static}, + {0, 0, 0}}; + +int CJS_Event::ObjDefnID = -1; + +// static +void CJS_Event::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = pEngine->DefineObj("event", FXJSOBJTYPE_STATIC, + JSConstructor<CJS_Event, event>, + JSDestructor<CJS_Event>); + DefineProps(pEngine, ObjDefnID, PropertySpecs); +} + +event::event(CJS_Object* pJsObject) : CJS_EmbedObj(pJsObject) {} + +event::~event() {} + +CJS_Return event::get_change(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pRuntime->NewString(pEvent->Change().c_str())); +} + +CJS_Return event::set_change(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + if (vp->IsString()) { + WideString& wChange = pEvent->Change(); + wChange = pRuntime->ToWideString(vp); + } + return CJS_Return(true); +} + +CJS_Return event::get_change_ex(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + return CJS_Return(pRuntime->NewString(pEvent->ChangeEx().c_str())); +} + +CJS_Return event::set_change_ex(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_commit_key(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + return CJS_Return(pRuntime->NewNumber(pEvent->CommitKey())); +} + +CJS_Return event::set_commit_key(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_field_full(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean(pEvent->FieldFull())); +} + +CJS_Return event::set_field_full(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_key_down(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pRuntime->NewBoolean(pEvent->KeyDown())); +} + +CJS_Return event::set_key_down(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_modifier(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pRuntime->NewBoolean(pEvent->Modifier())); +} + +CJS_Return event::set_modifier(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_name(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pRuntime->NewString(pEvent->Name())); +} + +CJS_Return event::set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_rc(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pRuntime->NewBoolean(pEvent->Rc())); +} + +CJS_Return event::set_rc(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + pEvent->Rc() = pRuntime->ToBoolean(vp); + return CJS_Return(true); +} + +CJS_Return event::get_rich_change(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return event::set_rich_change(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return event::get_rich_change_ex(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return event::set_rich_change_ex(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return event::get_rich_value(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return event::set_rich_value(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return event::get_sel_end(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) + return CJS_Return(true); + + return CJS_Return(pRuntime->NewNumber(pEvent->SelEnd())); +} + +CJS_Return event::set_sel_end(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) + return CJS_Return(true); + + pEvent->SelEnd() = pRuntime->ToInt32(vp); + return CJS_Return(true); +} + +CJS_Return event::get_sel_start(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) + return CJS_Return(true); + + return CJS_Return(pRuntime->NewNumber(pEvent->SelStart())); +} + +CJS_Return event::set_sel_start(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) + return CJS_Return(true); + + pEvent->SelStart() = pRuntime->ToInt32(vp); + return CJS_Return(true); +} + +CJS_Return event::get_shift(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pRuntime->NewBoolean(pEvent->Shift())); +} + +CJS_Return event::set_shift(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_source(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pEvent->Source()->GetJSObject()->ToV8Object()); +} + +CJS_Return event::set_source(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_target(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pEvent->Target_Field()->GetJSObject()->ToV8Object()); +} + +CJS_Return event::set_target(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_target_name(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pRuntime->NewString(pEvent->TargetName().c_str())); +} + +CJS_Return event::set_target_name(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_type(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pRuntime->NewString(pEvent->Type())); +} + +CJS_Return event::set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return event::get_value(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + if (wcscmp((const wchar_t*)pEvent->Type(), L"Field") != 0) + return CJS_Return(false); + + if (!pEvent->m_pValue) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewString(pEvent->Value().c_str())); +} + +CJS_Return event::set_value(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + if (wcscmp((const wchar_t*)pEvent->Type(), L"Field") != 0) + return CJS_Return(false); + + if (!pEvent->m_pValue) + return CJS_Return(false); + + pEvent->Value() = pRuntime->ToWideString(vp); + return CJS_Return(true); +} + +CJS_Return event::get_will_commit(CJS_Runtime* pRuntime) { + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + return CJS_Return(pRuntime->NewBoolean(pEvent->WillCommit())); +} + +CJS_Return event::set_will_commit(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} diff --git a/fxjs/cjs_event.h b/fxjs/cjs_event.h new file mode 100644 index 0000000000..4ae5fa234f --- /dev/null +++ b/fxjs/cjs_event.h @@ -0,0 +1,111 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_EVENT_H_ +#define FXJS_CJS_EVENT_H_ + +#include "fxjs/JS_Define.h" + +class event : public CJS_EmbedObj { + public: + explicit event(CJS_Object* pJSObject); + ~event() override; + + CJS_Return get_change(CJS_Runtime* pRuntime); + CJS_Return set_change(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_change_ex(CJS_Runtime* pRuntime); + CJS_Return set_change_ex(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_commit_key(CJS_Runtime* pRuntime); + CJS_Return set_commit_key(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_field_full(CJS_Runtime* pRuntime); + CJS_Return set_field_full(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_key_down(CJS_Runtime* pRuntime); + CJS_Return set_key_down(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_modifier(CJS_Runtime* pRuntime); + CJS_Return set_modifier(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_name(CJS_Runtime* pRuntime); + CJS_Return set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_rc(CJS_Runtime* pRuntime); + CJS_Return set_rc(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_rich_change(CJS_Runtime* pRuntime); + CJS_Return set_rich_change(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_rich_change_ex(CJS_Runtime* pRuntime); + CJS_Return set_rich_change_ex(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_rich_value(CJS_Runtime* pRuntime); + CJS_Return set_rich_value(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_sel_end(CJS_Runtime* pRuntime); + CJS_Return set_sel_end(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_sel_start(CJS_Runtime* pRuntime); + CJS_Return set_sel_start(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_shift(CJS_Runtime* pRuntime); + CJS_Return set_shift(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_source(CJS_Runtime* pRuntime); + CJS_Return set_source(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_target(CJS_Runtime* pRuntime); + CJS_Return set_target(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_target_name(CJS_Runtime* pRuntime); + CJS_Return set_target_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_type(CJS_Runtime* pRuntime); + CJS_Return set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_value(CJS_Runtime* pRuntime); + CJS_Return set_value(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_will_commit(CJS_Runtime* pRuntime); + CJS_Return set_will_commit(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); +}; + +class CJS_Event : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Event(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Event() override {} + + JS_STATIC_PROP(change, change, event); + JS_STATIC_PROP(changeEx, change_ex, event); + JS_STATIC_PROP(commitKey, commit_key, event); + JS_STATIC_PROP(fieldFull, field_full, event); + JS_STATIC_PROP(keyDown, key_down, event); + JS_STATIC_PROP(modifier, modifier, event); + JS_STATIC_PROP(name, name, event); + JS_STATIC_PROP(rc, rc, event); + JS_STATIC_PROP(richChange, rich_change, event); + JS_STATIC_PROP(richChangeEx, rich_change_ex, event); + JS_STATIC_PROP(richValue, rich_value, event); + JS_STATIC_PROP(selEnd, sel_end, event); + JS_STATIC_PROP(selStart, sel_start, event); + JS_STATIC_PROP(shift, shift, event); + JS_STATIC_PROP(source, source, event); + JS_STATIC_PROP(target, target, event); + JS_STATIC_PROP(targetName, target_name, event); + JS_STATIC_PROP(type, type, event); + JS_STATIC_PROP(value, value, event); + JS_STATIC_PROP(willCommit, will_commit, event); + + private: + static int ObjDefnID; + static const JSPropertySpec PropertySpecs[]; +}; + +#endif // FXJS_CJS_EVENT_H_ diff --git a/fxjs/cjs_event_context.cpp b/fxjs/cjs_event_context.cpp new file mode 100644 index 0000000000..bc908a5db2 --- /dev/null +++ b/fxjs/cjs_event_context.cpp @@ -0,0 +1,281 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_event_context.h" + +#include "core/fxcrt/autorestorer.h" +#include "fxjs/JS_Define.h" +#include "fxjs/cjs_eventhandler.h" +#include "fxjs/cjs_runtime.h" +#include "fxjs/js_resources.h" + +CJS_EventContext::CJS_EventContext(CJS_Runtime* pRuntime) + : m_pRuntime(pRuntime), + m_pEventHandler(new CJS_EventHandler(this)), + m_bBusy(false) { + ASSERT(pRuntime); +} + +CJS_EventContext::~CJS_EventContext() {} + +CPDFSDK_FormFillEnvironment* CJS_EventContext::GetFormFillEnv() { + return m_pRuntime->GetFormFillEnv(); +} + +bool CJS_EventContext::RunScript(const WideString& script, WideString* info) { + v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate()); + v8::HandleScope handle_scope(m_pRuntime->GetIsolate()); + v8::Local<v8::Context> context = m_pRuntime->NewLocalContext(); + v8::Context::Scope context_scope(context); + + if (m_bBusy) { + *info = JSGetStringFromID(IDS_STRING_JSBUSY); + return false; + } + + AutoRestorer<bool> restorer(&m_bBusy); + m_bBusy = true; + + ASSERT(m_pEventHandler->IsValid()); + CJS_Runtime::FieldEvent event(m_pEventHandler->TargetName(), + m_pEventHandler->EventType()); + if (!m_pRuntime->AddEventToSet(event)) { + *info = JSGetStringFromID(IDS_STRING_JSEVENT); + return false; + } + + WideString sErrorMessage; + int nRet = 0; + if (script.GetLength() > 0) + nRet = m_pRuntime->ExecuteScript(script.c_str(), &sErrorMessage); + + if (nRet < 0) + *info += sErrorMessage; + else + *info = JSGetStringFromID(IDS_STRING_RUN); + + m_pRuntime->RemoveEventFromSet(event); + m_pEventHandler->Destroy(); + return nRet >= 0; +} + +void CJS_EventContext::OnApp_Init() { + m_pEventHandler->OnApp_Init(); +} + +void CJS_EventContext::OnDoc_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& strTargetName) { + m_pEventHandler->OnDoc_Open(pFormFillEnv, strTargetName); +} + +void CJS_EventContext::OnDoc_WillPrint( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnDoc_WillPrint(pFormFillEnv); +} + +void CJS_EventContext::OnDoc_DidPrint( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnDoc_DidPrint(pFormFillEnv); +} + +void CJS_EventContext::OnDoc_WillSave( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnDoc_WillSave(pFormFillEnv); +} + +void CJS_EventContext::OnDoc_DidSave( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnDoc_DidSave(pFormFillEnv); +} + +void CJS_EventContext::OnDoc_WillClose( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnDoc_WillClose(pFormFillEnv); +} + +void CJS_EventContext::OnPage_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnPage_Open(pFormFillEnv); +} + +void CJS_EventContext::OnPage_Close(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnPage_Close(pFormFillEnv); +} + +void CJS_EventContext::OnPage_InView( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnPage_InView(pFormFillEnv); +} + +void CJS_EventContext::OnPage_OutView( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnPage_OutView(pFormFillEnv); +} + +void CJS_EventContext::OnField_MouseDown(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) { + m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget); +} + +void CJS_EventContext::OnField_MouseEnter(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) { + m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget); +} + +void CJS_EventContext::OnField_MouseExit(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) { + m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget); +} + +void CJS_EventContext::OnField_MouseUp(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) { + m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget); +} + +void CJS_EventContext::OnField_Focus(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value) { + m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value); +} + +void CJS_EventContext::OnField_Blur(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value) { + m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value); +} + +void CJS_EventContext::OnField_Calculate(CPDF_FormField* pSource, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc) { + m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc); +} + +void CJS_EventContext::OnField_Format(CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit) { + m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit); +} + +void CJS_EventContext::OnField_Keystroke(WideString& strChange, + const WideString& strChangeEx, + bool bKeyDown, + bool bModifier, + int& nSelEnd, + int& nSelStart, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit, + bool bFieldFull, + bool& bRc) { + m_pEventHandler->OnField_Keystroke( + strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift, + pTarget, Value, bWillCommit, bFieldFull, bRc); +} + +void CJS_EventContext::OnField_Validate(WideString& strChange, + const WideString& strChangeEx, + bool bKeyDown, + bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc) { + m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier, + bShift, pTarget, Value, bRc); +} + +void CJS_EventContext::OnScreen_Focus(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen); +} + +void CJS_EventContext::OnScreen_Blur(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen); +} + +void CJS_EventContext::OnScreen_Open(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen); +} + +void CJS_EventContext::OnScreen_Close(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen); +} + +void CJS_EventContext::OnScreen_MouseDown(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen); +} + +void CJS_EventContext::OnScreen_MouseUp(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen); +} + +void CJS_EventContext::OnScreen_MouseEnter(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen); +} + +void CJS_EventContext::OnScreen_MouseExit(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen); +} + +void CJS_EventContext::OnScreen_InView(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen); +} + +void CJS_EventContext::OnScreen_OutView(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen); +} + +void CJS_EventContext::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) { + m_pEventHandler->OnBookmark_MouseUp(pBookMark); +} + +void CJS_EventContext::OnLink_MouseUp( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnLink_MouseUp(pFormFillEnv); +} + +void CJS_EventContext::OnConsole_Exec() { + m_pEventHandler->OnConsole_Exec(); +} + +void CJS_EventContext::OnExternal_Exec() { + m_pEventHandler->OnExternal_Exec(); +} + +void CJS_EventContext::OnBatchExec(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pEventHandler->OnBatchExec(pFormFillEnv); +} + +void CJS_EventContext::OnMenu_Exec(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& strTargetName) { + m_pEventHandler->OnMenu_Exec(pFormFillEnv, strTargetName); +} diff --git a/fxjs/cjs_event_context.h b/fxjs/cjs_event_context.h new file mode 100644 index 0000000000..f9572909e8 --- /dev/null +++ b/fxjs/cjs_event_context.h @@ -0,0 +1,136 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_EVENT_CONTEXT_H_ +#define FXJS_CJS_EVENT_CONTEXT_H_ + +#include <memory> + +#include "core/fxcrt/fx_string.h" +#include "core/fxcrt/fx_system.h" +#include "core/fxcrt/unowned_ptr.h" +#include "fxjs/ijs_event_context.h" + +class CJS_EventHandler; +class CJS_Runtime; +class CPDFSDK_FormFillEnvironment; + +class CJS_EventContext : public IJS_EventContext { + public: + explicit CJS_EventContext(CJS_Runtime* pRuntime); + ~CJS_EventContext() override; + + // IJS_EventContext + bool RunScript(const WideString& script, WideString* info) override; + void OnApp_Init() override; + void OnDoc_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& strTargetName) override; + void OnDoc_WillPrint(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnDoc_DidPrint(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnDoc_WillSave(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnDoc_DidSave(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnDoc_WillClose(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnPage_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnPage_Close(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnPage_InView(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnPage_OutView(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnField_MouseDown(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) override; + void OnField_MouseEnter(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) override; + void OnField_MouseExit(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) override; + void OnField_MouseUp(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) override; + void OnField_Focus(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value) override; + void OnField_Blur(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value) override; + void OnField_Calculate(CPDF_FormField* pSource, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc) override; + void OnField_Format(CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit) override; + void OnField_Keystroke(WideString& strChange, + const WideString& strChangeEx, + bool bKeyDown, + bool bModifier, + int& nSelEnd, + int& nSelStart, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit, + bool bFieldFull, + bool& bRc) override; + void OnField_Validate(WideString& strChange, + const WideString& strChangeEx, + bool bKeyDown, + bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc) override; + void OnScreen_Focus(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override; + void OnScreen_Blur(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override; + void OnScreen_Open(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override; + void OnScreen_Close(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override; + void OnScreen_MouseDown(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override; + void OnScreen_MouseUp(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override; + void OnScreen_MouseEnter(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override; + void OnScreen_MouseExit(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override; + void OnScreen_InView(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override; + void OnScreen_OutView(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override; + void OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) override; + void OnLink_MouseUp(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnMenu_Exec(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& strTargetName) override; + void OnBatchExec(CPDFSDK_FormFillEnvironment* pFormFillEnv) override; + void OnConsole_Exec() override; + void OnExternal_Exec() override; + + CJS_Runtime* GetJSRuntime() const { return m_pRuntime.Get(); } + CJS_EventHandler* GetEventHandler() const { return m_pEventHandler.get(); } + + CPDFSDK_FormFillEnvironment* GetFormFillEnv(); + + private: + UnownedPtr<CJS_Runtime> const m_pRuntime; + std::unique_ptr<CJS_EventHandler> m_pEventHandler; + bool m_bBusy; +}; + +#endif // FXJS_CJS_EVENT_CONTEXT_H_ diff --git a/fxjs/cjs_event_context_stub.cpp b/fxjs/cjs_event_context_stub.cpp new file mode 100644 index 0000000000..0517ab2c49 --- /dev/null +++ b/fxjs/cjs_event_context_stub.cpp @@ -0,0 +1,12 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_event_context_stub.h" + +bool CJS_EventContextStub::RunScript(const WideString& script, + WideString* info) { + return false; +} diff --git a/fxjs/cjs_event_context_stub.h b/fxjs/cjs_event_context_stub.h new file mode 100644 index 0000000000..bc853694df --- /dev/null +++ b/fxjs/cjs_event_context_stub.h @@ -0,0 +1,118 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_EVENT_CONTEXT_STUB_H_ +#define FXJS_CJS_EVENT_CONTEXT_STUB_H_ + +#include "fxjs/ijs_event_context.h" + +class CJS_EventContextStub final : public IJS_EventContext { + public: + CJS_EventContextStub() {} + ~CJS_EventContextStub() override {} + + // IJS_EventContext: + bool RunScript(const WideString& script, WideString* info) override; + + void OnApp_Init() override {} + void OnDoc_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& strTargetName) override {} + void OnDoc_WillPrint(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnDoc_DidPrint(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnDoc_WillSave(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnDoc_DidSave(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnDoc_WillClose(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnPage_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnPage_Close(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnPage_InView(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnPage_OutView(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnField_MouseDown(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) override {} + void OnField_MouseEnter(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) override {} + void OnField_MouseExit(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) override {} + void OnField_MouseUp(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) override {} + void OnField_Focus(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value) override {} + void OnField_Blur(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value) override {} + void OnField_Calculate(CPDF_FormField* pSource, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc) override {} + void OnField_Format(CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit) override {} + void OnField_Keystroke(WideString& strChange, + const WideString& strChangeEx, + bool KeyDown, + bool bModifier, + int& nSelEnd, + int& nSelStart, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit, + bool bFieldFull, + bool& bRc) override {} + void OnField_Validate(WideString& strChange, + const WideString& strChangeEx, + bool bKeyDown, + bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc) override {} + void OnScreen_Focus(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override {} + void OnScreen_Blur(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override {} + void OnScreen_Open(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override {} + void OnScreen_Close(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override {} + void OnScreen_MouseDown(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override {} + void OnScreen_MouseUp(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override {} + void OnScreen_MouseEnter(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override {} + void OnScreen_MouseExit(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override {} + void OnScreen_InView(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override {} + void OnScreen_OutView(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) override {} + void OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) override {} + void OnLink_MouseUp(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnMenu_Exec(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString&) override {} + void OnBatchExec(CPDFSDK_FormFillEnvironment* pFormFillEnv) override {} + void OnConsole_Exec() override {} + void OnExternal_Exec() override {} +}; + +#endif // FXJS_CJS_EVENT_CONTEXT_STUB_H_ diff --git a/fxjs/cjs_eventhandler.cpp b/fxjs/cjs_eventhandler.cpp new file mode 100644 index 0000000000..c052f9ed58 --- /dev/null +++ b/fxjs/cjs_eventhandler.cpp @@ -0,0 +1,653 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_eventhandler.h" + +#include "core/fpdfdoc/cpdf_formfield.h" +#include "fxjs/JS_Define.h" +#include "fxjs/cjs_document.h" +#include "fxjs/cjs_event_context.h" +#include "fxjs/cjs_field.h" +#include "fxjs/cjs_object.h" +#include "fxjs/cjs_runtime.h" + +CJS_EventHandler::CJS_EventHandler(CJS_EventContext* pContext) + : m_pJSEventContext(pContext), + m_eEventType(JET_UNKNOWN), + m_bValid(false), + m_pWideStrChange(nullptr), + m_nCommitKey(-1), + m_bKeyDown(false), + m_bModifier(false), + m_bShift(false), + m_pISelEnd(nullptr), + m_nSelEndDu(0), + m_pISelStart(nullptr), + m_nSelStartDu(0), + m_bWillCommit(false), + m_pValue(nullptr), + m_bFieldFull(false), + m_pbRc(nullptr), + m_bRcDu(false), + m_pTargetBookMark(nullptr), + m_pTargetFormFillEnv(nullptr), + m_pTargetAnnot(nullptr) {} + +CJS_EventHandler::~CJS_EventHandler() {} + +void CJS_EventHandler::OnApp_Init() { + Initial(JET_APP_INIT); +} + +void CJS_EventHandler::OnDoc_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& strTargetName) { + Initial(JET_DOC_OPEN); + m_pTargetFormFillEnv.Reset(pFormFillEnv); + m_strTargetName = strTargetName; +} + +void CJS_EventHandler::OnDoc_WillPrint( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + Initial(JET_DOC_WILLPRINT); + m_pTargetFormFillEnv.Reset(pFormFillEnv); +} + +void CJS_EventHandler::OnDoc_DidPrint( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + Initial(JET_DOC_DIDPRINT); + m_pTargetFormFillEnv.Reset(pFormFillEnv); +} + +void CJS_EventHandler::OnDoc_WillSave( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + Initial(JET_DOC_WILLSAVE); + m_pTargetFormFillEnv.Reset(pFormFillEnv); +} + +void CJS_EventHandler::OnDoc_DidSave( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + Initial(JET_DOC_DIDSAVE); + m_pTargetFormFillEnv.Reset(pFormFillEnv); +} + +void CJS_EventHandler::OnDoc_WillClose( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + Initial(JET_DOC_WILLCLOSE); + m_pTargetFormFillEnv.Reset(pFormFillEnv); +} + +void CJS_EventHandler::OnPage_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + Initial(JET_PAGE_OPEN); + m_pTargetFormFillEnv.Reset(pFormFillEnv); +} + +void CJS_EventHandler::OnPage_Close(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + Initial(JET_PAGE_CLOSE); + m_pTargetFormFillEnv.Reset(pFormFillEnv); +} + +void CJS_EventHandler::OnPage_InView( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + Initial(JET_PAGE_INVIEW); + m_pTargetFormFillEnv.Reset(pFormFillEnv); +} + +void CJS_EventHandler::OnPage_OutView( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + Initial(JET_PAGE_OUTVIEW); + m_pTargetFormFillEnv.Reset(pFormFillEnv); +} + +void CJS_EventHandler::OnField_MouseEnter(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) { + Initial(JET_FIELD_MOUSEENTER); + + m_bModifier = bModifier; + m_bShift = bShift; + + m_strTargetName = pTarget->GetFullName(); +} + +void CJS_EventHandler::OnField_MouseExit(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) { + Initial(JET_FIELD_MOUSEEXIT); + + m_bModifier = bModifier; + m_bShift = bShift; + m_strTargetName = pTarget->GetFullName(); +} + +void CJS_EventHandler::OnField_MouseDown(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) { + Initial(JET_FIELD_MOUSEDOWN); + m_eEventType = JET_FIELD_MOUSEDOWN; + + m_bModifier = bModifier; + m_bShift = bShift; + m_strTargetName = pTarget->GetFullName(); +} + +void CJS_EventHandler::OnField_MouseUp(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) { + Initial(JET_FIELD_MOUSEUP); + + m_bModifier = bModifier; + m_bShift = bShift; + m_strTargetName = pTarget->GetFullName(); +} + +void CJS_EventHandler::OnField_Focus(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value) { + Initial(JET_FIELD_FOCUS); + + m_bModifier = bModifier; + m_bShift = bShift; + m_strTargetName = pTarget->GetFullName(); + m_pValue = (WideString*)&Value; +} + +void CJS_EventHandler::OnField_Blur(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value) { + Initial(JET_FIELD_BLUR); + + m_bModifier = bModifier; + m_bShift = bShift; + m_strTargetName = pTarget->GetFullName(); + m_pValue = (WideString*)&Value; +} + +void CJS_EventHandler::OnField_Keystroke(WideString& strChange, + const WideString& strChangeEx, + bool KeyDown, + bool bModifier, + int& nSelEnd, + int& nSelStart, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit, + bool bFieldFull, + bool& bRc) { + Initial(JET_FIELD_KEYSTROKE); + + m_nCommitKey = 0; + m_pWideStrChange = &strChange; + m_WideStrChangeEx = strChangeEx; + m_bKeyDown = KeyDown; + m_bModifier = bModifier; + m_pISelEnd = &nSelEnd; + m_pISelStart = &nSelStart; + m_bShift = bShift; + m_strTargetName = pTarget->GetFullName(); + m_pValue = &Value; + m_bWillCommit = bWillCommit; + m_pbRc = &bRc; + m_bFieldFull = bFieldFull; +} + +void CJS_EventHandler::OnField_Validate(WideString& strChange, + const WideString& strChangeEx, + bool bKeyDown, + bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc) { + Initial(JET_FIELD_VALIDATE); + + m_pWideStrChange = &strChange; + m_WideStrChangeEx = strChangeEx; + m_bKeyDown = bKeyDown; + m_bModifier = bModifier; + m_bShift = bShift; + m_strTargetName = pTarget->GetFullName(); + m_pValue = &Value; + m_pbRc = &bRc; +} + +void CJS_EventHandler::OnField_Calculate(CPDF_FormField* pSource, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc) { + Initial(JET_FIELD_CALCULATE); + + if (pSource) + m_strSourceName = pSource->GetFullName(); + m_strTargetName = pTarget->GetFullName(); + m_pValue = &Value; + m_pbRc = &bRc; +} + +void CJS_EventHandler::OnField_Format(CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit) { + Initial(JET_FIELD_FORMAT); + + m_nCommitKey = 0; + m_strTargetName = pTarget->GetFullName(); + m_pValue = &Value; + m_bWillCommit = bWillCommit; +} + +void CJS_EventHandler::OnScreen_Focus(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + Initial(JET_SCREEN_FOCUS); + + m_bModifier = bModifier; + m_bShift = bShift; + m_pTargetAnnot.Reset(pScreen); +} + +void CJS_EventHandler::OnScreen_Blur(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + Initial(JET_SCREEN_BLUR); + + m_bModifier = bModifier; + m_bShift = bShift; + m_pTargetAnnot.Reset(pScreen); +} + +void CJS_EventHandler::OnScreen_Open(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + Initial(JET_SCREEN_OPEN); + + m_bModifier = bModifier; + m_bShift = bShift; + m_pTargetAnnot.Reset(pScreen); +} + +void CJS_EventHandler::OnScreen_Close(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + Initial(JET_SCREEN_CLOSE); + + m_bModifier = bModifier; + m_bShift = bShift; + m_pTargetAnnot.Reset(pScreen); +} + +void CJS_EventHandler::OnScreen_MouseDown(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + Initial(JET_SCREEN_MOUSEDOWN); + + m_bModifier = bModifier; + m_bShift = bShift; + m_pTargetAnnot.Reset(pScreen); +} + +void CJS_EventHandler::OnScreen_MouseUp(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + Initial(JET_SCREEN_MOUSEUP); + + m_bModifier = bModifier; + m_bShift = bShift; + m_pTargetAnnot.Reset(pScreen); +} + +void CJS_EventHandler::OnScreen_MouseEnter(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + Initial(JET_SCREEN_MOUSEENTER); + + m_bModifier = bModifier; + m_bShift = bShift; + m_pTargetAnnot.Reset(pScreen); +} + +void CJS_EventHandler::OnScreen_MouseExit(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + Initial(JET_SCREEN_MOUSEEXIT); + + m_bModifier = bModifier; + m_bShift = bShift; + m_pTargetAnnot.Reset(pScreen); +} + +void CJS_EventHandler::OnScreen_InView(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + Initial(JET_SCREEN_INVIEW); + m_bModifier = bModifier; + m_bShift = bShift; + m_pTargetAnnot.Reset(pScreen); +} + +void CJS_EventHandler::OnScreen_OutView(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) { + Initial(JET_SCREEN_OUTVIEW); + m_bModifier = bModifier; + m_bShift = bShift; + m_pTargetAnnot.Reset(pScreen); +} + +void CJS_EventHandler::OnLink_MouseUp( + CPDFSDK_FormFillEnvironment* pTargetFormFillEnv) { + Initial(JET_LINK_MOUSEUP); + m_pTargetFormFillEnv.Reset(pTargetFormFillEnv); +} + +void CJS_EventHandler::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) { + Initial(JET_BOOKMARK_MOUSEUP); + m_pTargetBookMark = pBookMark; +} + +void CJS_EventHandler::OnMenu_Exec( + CPDFSDK_FormFillEnvironment* pTargetFormFillEnv, + const WideString& strTargetName) { + Initial(JET_MENU_EXEC); + m_pTargetFormFillEnv.Reset(pTargetFormFillEnv); + m_strTargetName = strTargetName; +} + +void CJS_EventHandler::OnExternal_Exec() { + Initial(JET_EXTERNAL_EXEC); +} + +void CJS_EventHandler::OnBatchExec( + CPDFSDK_FormFillEnvironment* pTargetFormFillEnv) { + Initial(JET_BATCH_EXEC); + m_pTargetFormFillEnv.Reset(pTargetFormFillEnv); +} + +void CJS_EventHandler::OnConsole_Exec() { + Initial(JET_CONSOLE_EXEC); +} + +void CJS_EventHandler::Initial(JS_EVENT_T type) { + m_eEventType = type; + + m_strTargetName = L""; + m_strSourceName = L""; + m_pWideStrChange = nullptr; + m_WideStrChangeDu = L""; + m_WideStrChangeEx = L""; + m_nCommitKey = -1; + m_bKeyDown = false; + m_bModifier = false; + m_bShift = false; + m_pISelEnd = nullptr; + m_nSelEndDu = 0; + m_pISelStart = nullptr; + m_nSelStartDu = 0; + m_bWillCommit = false; + m_pValue = nullptr; + m_bFieldFull = false; + m_pbRc = nullptr; + m_bRcDu = false; + + m_pTargetBookMark = nullptr; + m_pTargetFormFillEnv.Reset(); + m_pTargetAnnot.Reset(); + + m_bValid = true; +} + +void CJS_EventHandler::Destroy() { + m_bValid = false; +} + +bool CJS_EventHandler::IsValid() { + return m_bValid; +} + +WideString& CJS_EventHandler::Change() { + if (m_pWideStrChange) { + return *m_pWideStrChange; + } + return m_WideStrChangeDu; +} + +WideString CJS_EventHandler::ChangeEx() { + return m_WideStrChangeEx; +} + +int CJS_EventHandler::CommitKey() { + return m_nCommitKey; +} + +bool CJS_EventHandler::FieldFull() { + return m_bFieldFull; +} + +bool CJS_EventHandler::KeyDown() { + return m_bKeyDown; +} + +bool CJS_EventHandler::Modifier() { + return m_bModifier; +} + +const wchar_t* CJS_EventHandler::Name() { + switch (m_eEventType) { + case JET_APP_INIT: + return L"Init"; + case JET_BATCH_EXEC: + return L"Exec"; + case JET_BOOKMARK_MOUSEUP: + return L"Mouse Up"; + case JET_CONSOLE_EXEC: + return L"Exec"; + case JET_DOC_DIDPRINT: + return L"DidPrint"; + case JET_DOC_DIDSAVE: + return L"DidSave"; + case JET_DOC_OPEN: + return L"Open"; + case JET_DOC_WILLCLOSE: + return L"WillClose"; + case JET_DOC_WILLPRINT: + return L"WillPrint"; + case JET_DOC_WILLSAVE: + return L"WillSave"; + case JET_EXTERNAL_EXEC: + return L"Exec"; + case JET_FIELD_FOCUS: + case JET_SCREEN_FOCUS: + return L"Focus"; + case JET_FIELD_BLUR: + case JET_SCREEN_BLUR: + return L"Blur"; + case JET_FIELD_MOUSEDOWN: + case JET_SCREEN_MOUSEDOWN: + return L"Mouse Down"; + case JET_FIELD_MOUSEUP: + case JET_SCREEN_MOUSEUP: + return L"Mouse Up"; + case JET_FIELD_MOUSEENTER: + case JET_SCREEN_MOUSEENTER: + return L"Mouse Enter"; + case JET_FIELD_MOUSEEXIT: + case JET_SCREEN_MOUSEEXIT: + return L"Mouse Exit"; + case JET_FIELD_CALCULATE: + return L"Calculate"; + case JET_FIELD_FORMAT: + return L"Format"; + case JET_FIELD_KEYSTROKE: + return L"Keystroke"; + case JET_FIELD_VALIDATE: + return L"Validate"; + case JET_LINK_MOUSEUP: + return L"Mouse Up"; + case JET_MENU_EXEC: + return L"Exec"; + case JET_PAGE_OPEN: + case JET_SCREEN_OPEN: + return L"Open"; + case JET_PAGE_CLOSE: + case JET_SCREEN_CLOSE: + return L"Close"; + case JET_SCREEN_INVIEW: + case JET_PAGE_INVIEW: + return L"InView"; + case JET_PAGE_OUTVIEW: + case JET_SCREEN_OUTVIEW: + return L"OutView"; + default: + return L""; + } +} + +const wchar_t* CJS_EventHandler::Type() { + switch (m_eEventType) { + case JET_APP_INIT: + return L"App"; + case JET_BATCH_EXEC: + return L"Batch"; + case JET_BOOKMARK_MOUSEUP: + return L"BookMark"; + case JET_CONSOLE_EXEC: + return L"Console"; + case JET_DOC_DIDPRINT: + case JET_DOC_DIDSAVE: + case JET_DOC_OPEN: + case JET_DOC_WILLCLOSE: + case JET_DOC_WILLPRINT: + case JET_DOC_WILLSAVE: + return L"Doc"; + case JET_EXTERNAL_EXEC: + return L"External"; + case JET_FIELD_BLUR: + case JET_FIELD_FOCUS: + case JET_FIELD_MOUSEDOWN: + case JET_FIELD_MOUSEENTER: + case JET_FIELD_MOUSEEXIT: + case JET_FIELD_MOUSEUP: + case JET_FIELD_CALCULATE: + case JET_FIELD_FORMAT: + case JET_FIELD_KEYSTROKE: + case JET_FIELD_VALIDATE: + return L"Field"; + case JET_SCREEN_FOCUS: + case JET_SCREEN_BLUR: + case JET_SCREEN_OPEN: + case JET_SCREEN_CLOSE: + case JET_SCREEN_MOUSEDOWN: + case JET_SCREEN_MOUSEUP: + case JET_SCREEN_MOUSEENTER: + case JET_SCREEN_MOUSEEXIT: + case JET_SCREEN_INVIEW: + case JET_SCREEN_OUTVIEW: + return L"Screen"; + case JET_LINK_MOUSEUP: + return L"Link"; + case JET_MENU_EXEC: + return L"Menu"; + case JET_PAGE_OPEN: + case JET_PAGE_CLOSE: + case JET_PAGE_INVIEW: + case JET_PAGE_OUTVIEW: + return L"Page"; + default: + return L""; + } +} + +bool& CJS_EventHandler::Rc() { + if (m_pbRc) { + return *m_pbRc; + } + return m_bRcDu; +} + +int& CJS_EventHandler::SelEnd() { + if (m_pISelEnd) { + return *m_pISelEnd; + } + return m_nSelEndDu; +} + +int& CJS_EventHandler::SelStart() { + if (m_pISelStart) { + return *m_pISelStart; + } + return m_nSelStartDu; +} + +bool CJS_EventHandler::Shift() { + return m_bShift; +} + +Field* CJS_EventHandler::Source() { + CJS_Runtime* pRuntime = m_pJSEventContext->GetJSRuntime(); + v8::Local<v8::Object> pDocObj = + pRuntime->NewFxDynamicObj(CJS_Document::GetObjDefnID()); + if (pDocObj.IsEmpty()) + return nullptr; + + v8::Local<v8::Object> pFieldObj = + pRuntime->NewFxDynamicObj(CJS_Field::GetObjDefnID()); + if (pFieldObj.IsEmpty()) + return nullptr; + + CJS_Document* pJSDocument = + static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pDocObj)); + CJS_Field* pJSField = + static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj)); + + Document* pDocument = static_cast<Document*>(pJSDocument->GetEmbedObject()); + pDocument->SetFormFillEnv(m_pTargetFormFillEnv + ? m_pTargetFormFillEnv.Get() + : m_pJSEventContext->GetFormFillEnv()); + + Field* pField = static_cast<Field*>(pJSField->GetEmbedObject()); + pField->AttachField(pDocument, m_strSourceName); + return pField; +} + +Field* CJS_EventHandler::Target_Field() { + CJS_Runtime* pRuntime = m_pJSEventContext->GetJSRuntime(); + v8::Local<v8::Object> pDocObj = + pRuntime->NewFxDynamicObj(CJS_Document::GetObjDefnID()); + if (pDocObj.IsEmpty()) + return nullptr; + + v8::Local<v8::Object> pFieldObj = + pRuntime->NewFxDynamicObj(CJS_Field::GetObjDefnID()); + if (pFieldObj.IsEmpty()) + return nullptr; + + CJS_Document* pJSDocument = + static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pDocObj)); + CJS_Field* pJSField = + static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj)); + + Document* pDocument = static_cast<Document*>(pJSDocument->GetEmbedObject()); + pDocument->SetFormFillEnv(m_pTargetFormFillEnv + ? m_pTargetFormFillEnv.Get() + : m_pJSEventContext->GetFormFillEnv()); + + Field* pField = static_cast<Field*>(pJSField->GetEmbedObject()); + pField->AttachField(pDocument, m_strTargetName); + return pField; +} + +WideString& CJS_EventHandler::Value() { + return *m_pValue; +} + +bool CJS_EventHandler::WillCommit() { + return m_bWillCommit; +} + +WideString CJS_EventHandler::TargetName() { + return m_strTargetName; +} diff --git a/fxjs/cjs_eventhandler.h b/fxjs/cjs_eventhandler.h new file mode 100644 index 0000000000..cb7b3c09b1 --- /dev/null +++ b/fxjs/cjs_eventhandler.h @@ -0,0 +1,195 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_EVENTHANDLER_H_ +#define FXJS_CJS_EVENTHANDLER_H_ + +#include "core/fxcrt/fx_string.h" +#include "core/fxcrt/fx_system.h" +#include "core/fxcrt/unowned_ptr.h" +#include "fpdfsdk/cpdfsdk_formfillenvironment.h" + +class CJS_EventContext; +class CPDFSDK_Annot; +class CPDF_Bookmark; +class CPDF_FormField; +class Field; + +enum JS_EVENT_T { + JET_UNKNOWN, + JET_APP_INIT, + JET_DOC_OPEN, + JET_DOC_WILLPRINT, + JET_DOC_DIDPRINT, + JET_DOC_WILLSAVE, + JET_DOC_DIDSAVE, + JET_DOC_WILLCLOSE, + JET_PAGE_OPEN, + JET_PAGE_CLOSE, + JET_PAGE_INVIEW, + JET_PAGE_OUTVIEW, + JET_FIELD_MOUSEDOWN, + JET_FIELD_MOUSEUP, + JET_FIELD_MOUSEENTER, + JET_FIELD_MOUSEEXIT, + JET_FIELD_FOCUS, + JET_FIELD_BLUR, + JET_FIELD_KEYSTROKE, + JET_FIELD_VALIDATE, + JET_FIELD_CALCULATE, + JET_FIELD_FORMAT, + JET_SCREEN_FOCUS, + JET_SCREEN_BLUR, + JET_SCREEN_OPEN, + JET_SCREEN_CLOSE, + JET_SCREEN_MOUSEDOWN, + JET_SCREEN_MOUSEUP, + JET_SCREEN_MOUSEENTER, + JET_SCREEN_MOUSEEXIT, + JET_SCREEN_INVIEW, + JET_SCREEN_OUTVIEW, + JET_BATCH_EXEC, + JET_MENU_EXEC, + JET_CONSOLE_EXEC, + JET_EXTERNAL_EXEC, + JET_BOOKMARK_MOUSEUP, + JET_LINK_MOUSEUP +}; + +class CJS_EventHandler { + public: + explicit CJS_EventHandler(CJS_EventContext* pContext); + virtual ~CJS_EventHandler(); + + void OnApp_Init(); + + void OnDoc_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& strTargetName); + void OnDoc_WillPrint(CPDFSDK_FormFillEnvironment* pFormFillEnv); + void OnDoc_DidPrint(CPDFSDK_FormFillEnvironment* pFormFillEnv); + void OnDoc_WillSave(CPDFSDK_FormFillEnvironment* pFormFillEnv); + void OnDoc_DidSave(CPDFSDK_FormFillEnvironment* pFormFillEnv); + void OnDoc_WillClose(CPDFSDK_FormFillEnvironment* pFormFillEnv); + + void OnPage_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv); + void OnPage_Close(CPDFSDK_FormFillEnvironment* pFormFillEnv); + void OnPage_InView(CPDFSDK_FormFillEnvironment* pFormFillEnv); + void OnPage_OutView(CPDFSDK_FormFillEnvironment* pFormFillEnv); + + void OnField_Calculate(CPDF_FormField* pSource, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc); + void OnField_Format(CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit); + void OnField_Keystroke(WideString& strChange, + const WideString& strChangeEx, + bool KeyDown, + bool bModifier, + int& nSelEnd, + int& nSelStart, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit, + bool bFieldFull, + bool& bRc); + void OnField_Validate(WideString& strChange, + const WideString& strChangeEx, + bool bKeyDown, + bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc); + + void OnField_MouseDown(bool bModifier, bool bShift, CPDF_FormField* pTarget); + void OnField_MouseEnter(bool bModifier, bool bShift, CPDF_FormField* pTarget); + void OnField_MouseExit(bool bModifier, bool bShift, CPDF_FormField* pTarget); + void OnField_MouseUp(bool bModifier, bool bShift, CPDF_FormField* pTarget); + void OnField_Blur(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value); + void OnField_Focus(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value); + + void OnScreen_Focus(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); + void OnScreen_Blur(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); + void OnScreen_Open(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); + void OnScreen_Close(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); + void OnScreen_MouseDown(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); + void OnScreen_MouseUp(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); + void OnScreen_MouseEnter(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); + void OnScreen_MouseExit(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); + void OnScreen_InView(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); + void OnScreen_OutView(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); + + void OnBookmark_MouseUp(CPDF_Bookmark* pBookMark); + void OnLink_MouseUp(CPDFSDK_FormFillEnvironment* pFormFillEnv); + + void OnMenu_Exec(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& strTargetName); + void OnBatchExec(CPDFSDK_FormFillEnvironment* pFormFillEnv); + void OnConsole_Exec(); + void OnExternal_Exec(); + + void Initial(JS_EVENT_T type); + void Destroy(); + bool IsValid(); + + WideString& Change(); + WideString ChangeEx(); + int CommitKey(); + bool FieldFull(); + bool KeyDown(); + bool Modifier(); + const wchar_t* Name(); + const wchar_t* Type(); + bool& Rc(); + int& SelEnd(); + int& SelStart(); + bool Shift(); + Field* Source(); + Field* Target_Field(); + WideString& Value(); + bool WillCommit(); + WideString TargetName(); + + JS_EVENT_T EventType() { return m_eEventType; } + + UnownedPtr<CJS_EventContext> const m_pJSEventContext; + JS_EVENT_T m_eEventType; + bool m_bValid; + + WideString m_strTargetName; + WideString m_strSourceName; + UnownedPtr<WideString> m_pWideStrChange; + WideString m_WideStrChangeDu; + WideString m_WideStrChangeEx; + int m_nCommitKey; + bool m_bKeyDown; + bool m_bModifier; + bool m_bShift; + int* m_pISelEnd; + int m_nSelEndDu; + int* m_pISelStart; + int m_nSelStartDu; + bool m_bWillCommit; + UnownedPtr<WideString> m_pValue; + bool m_bFieldFull; + bool* m_pbRc; + bool m_bRcDu; + + UnownedPtr<CPDF_Bookmark> m_pTargetBookMark; + CPDFSDK_FormFillEnvironment::ObservedPtr m_pTargetFormFillEnv; + CPDFSDK_Annot::ObservedPtr m_pTargetAnnot; +}; + +#endif // FXJS_CJS_EVENTHANDLER_H_ diff --git a/fxjs/cjs_field.cpp b/fxjs/cjs_field.cpp new file mode 100644 index 0000000000..51f8c5aafc --- /dev/null +++ b/fxjs/cjs_field.cpp @@ -0,0 +1,2670 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_field.h" + +#include <algorithm> +#include <memory> + +#include "core/fpdfapi/font/cpdf_font.h" +#include "core/fpdfdoc/cpdf_formfield.h" +#include "core/fpdfdoc/cpdf_interform.h" +#include "fpdfsdk/cpdfsdk_interform.h" +#include "fpdfsdk/cpdfsdk_pageview.h" +#include "fpdfsdk/cpdfsdk_widget.h" +#include "fxjs/cjs_color.h" +#include "fxjs/cjs_delaydata.h" +#include "fxjs/cjs_document.h" +#include "fxjs/cjs_icon.h" +#include "fxjs/js_resources.h" + +namespace { + +bool SetWidgetDisplayStatus(CPDFSDK_Widget* pWidget, int value) { + if (!pWidget) + return false; + + uint32_t dwFlag = pWidget->GetFlags(); + switch (value) { + case 0: + dwFlag &= ~ANNOTFLAG_INVISIBLE; + dwFlag &= ~ANNOTFLAG_HIDDEN; + dwFlag &= ~ANNOTFLAG_NOVIEW; + dwFlag |= ANNOTFLAG_PRINT; + break; + case 1: + dwFlag &= ~ANNOTFLAG_INVISIBLE; + dwFlag &= ~ANNOTFLAG_NOVIEW; + dwFlag |= (ANNOTFLAG_HIDDEN | ANNOTFLAG_PRINT); + break; + case 2: + dwFlag &= ~ANNOTFLAG_INVISIBLE; + dwFlag &= ~ANNOTFLAG_PRINT; + dwFlag &= ~ANNOTFLAG_HIDDEN; + dwFlag &= ~ANNOTFLAG_NOVIEW; + break; + case 3: + dwFlag |= ANNOTFLAG_NOVIEW; + dwFlag |= ANNOTFLAG_PRINT; + dwFlag &= ~ANNOTFLAG_HIDDEN; + break; + } + + if (dwFlag != pWidget->GetFlags()) { + pWidget->SetFlags(dwFlag); + return true; + } + + return false; +} + +} // namespace + +const JSPropertySpec CJS_Field::PropertySpecs[] = { + {"alignment", get_alignment_static, set_alignment_static}, + {"borderStyle", get_border_style_static, set_border_style_static}, + {"buttonAlignX", get_button_align_x_static, set_button_align_x_static}, + {"buttonAlignY", get_button_align_y_static, set_button_align_y_static}, + {"buttonFitBounds", get_button_fit_bounds_static, + set_button_fit_bounds_static}, + {"buttonPosition", get_button_position_static, set_button_position_static}, + {"buttonScaleHow", get_button_scale_how_static, + set_button_scale_how_static}, + {"buttonScaleWhen", get_button_scale_when_static, + set_button_scale_when_static}, + {"calcOrderIndex", get_calc_order_index_static, + set_calc_order_index_static}, + {"charLimit", get_char_limit_static, set_char_limit_static}, + {"comb", get_comb_static, set_comb_static}, + {"commitOnSelChange", get_commit_on_sel_change_static, + set_commit_on_sel_change_static}, + {"currentValueIndices", get_current_value_indices_static, + set_current_value_indices_static}, + {"defaultStyle", get_default_style_static, set_default_style_static}, + {"defaultValue", get_default_value_static, set_default_value_static}, + {"doNotScroll", get_do_not_scroll_static, set_do_not_scroll_static}, + {"doNotSpellCheck", get_do_not_spell_check_static, + set_do_not_spell_check_static}, + {"delay", get_delay_static, set_delay_static}, + {"display", get_display_static, set_display_static}, + {"doc", get_doc_static, set_doc_static}, + {"editable", get_editable_static, set_editable_static}, + {"exportValues", get_export_values_static, set_export_values_static}, + {"hidden", get_hidden_static, set_hidden_static}, + {"fileSelect", get_file_select_static, set_file_select_static}, + {"fillColor", get_fill_color_static, set_fill_color_static}, + {"lineWidth", get_line_width_static, set_line_width_static}, + {"highlight", get_highlight_static, set_highlight_static}, + {"multiline", get_multiline_static, set_multiline_static}, + {"multipleSelection", get_multiple_selection_static, + set_multiple_selection_static}, + {"name", get_name_static, set_name_static}, + {"numItems", get_num_items_static, set_num_items_static}, + {"page", get_page_static, set_page_static}, + {"password", get_password_static, set_password_static}, + {"print", get_print_static, set_print_static}, + {"radiosInUnison", get_radios_in_unison_static, + set_radios_in_unison_static}, + {"readonly", get_readonly_static, set_readonly_static}, + {"rect", get_rect_static, set_rect_static}, + {"required", get_required_static, set_required_static}, + {"richText", get_rich_text_static, set_rich_text_static}, + {"richValue", get_rich_value_static, set_rich_value_static}, + {"rotation", get_rotation_static, set_rotation_static}, + {"strokeColor", get_stroke_color_static, set_stroke_color_static}, + {"style", get_style_static, set_style_static}, + {"submitName", get_submit_name_static, set_submit_name_static}, + {"textColor", get_text_color_static, set_text_color_static}, + {"textFont", get_text_font_static, set_text_font_static}, + {"textSize", get_text_size_static, set_text_size_static}, + {"type", get_type_static, set_type_static}, + {"userName", get_user_name_static, set_user_name_static}, + {"value", get_value_static, set_value_static}, + {"valueAsString", get_value_as_string_static, set_value_as_string_static}, + {"source", get_source_static, set_source_static}, + {0, 0, 0}}; + +const JSMethodSpec CJS_Field::MethodSpecs[] = { + {"browseForFileToSubmit", browseForFileToSubmit_static}, + {"buttonGetCaption", buttonGetCaption_static}, + {"buttonGetIcon", buttonGetIcon_static}, + {"buttonImportIcon", buttonImportIcon_static}, + {"buttonSetCaption", buttonSetCaption_static}, + {"buttonSetIcon", buttonSetIcon_static}, + {"checkThisBox", checkThisBox_static}, + {"clearItems", clearItems_static}, + {"defaultIsChecked", defaultIsChecked_static}, + {"deleteItemAt", deleteItemAt_static}, + {"getArray", getArray_static}, + {"getItemAt", getItemAt_static}, + {"getLock", getLock_static}, + {"insertItemAt", insertItemAt_static}, + {"isBoxChecked", isBoxChecked_static}, + {"isDefaultChecked", isDefaultChecked_static}, + {"setAction", setAction_static}, + {"setFocus", setFocus_static}, + {"setItems", setItems_static}, + {"setLock", setLock_static}, + {"signatureGetModifications", signatureGetModifications_static}, + {"signatureGetSeedValue", signatureGetSeedValue_static}, + {"signatureInfo", signatureInfo_static}, + {"signatureSetSeedValue", signatureSetSeedValue_static}, + {"signatureSign", signatureSign_static}, + {"signatureValidate", signatureValidate_static}, + {0, 0}}; + +int CJS_Field::ObjDefnID = -1; + +// static +int CJS_Field::GetObjDefnID() { + return ObjDefnID; +} + +// static +void CJS_Field::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = pEngine->DefineObj("Field", FXJSOBJTYPE_DYNAMIC, + JSConstructor<CJS_Field, Field>, + JSDestructor<CJS_Field>); + DefineProps(pEngine, ObjDefnID, PropertySpecs); + DefineMethods(pEngine, ObjDefnID, MethodSpecs); +} + +void CJS_Field::InitInstance(IJS_Runtime* pIRuntime) {} + +Field::Field(CJS_Object* pJSObject) + : CJS_EmbedObj(pJSObject), + m_pJSDoc(nullptr), + m_pFormFillEnv(nullptr), + m_nFormControlIndex(-1), + m_bCanSet(false), + m_bDelay(false) {} + +Field::~Field() {} + +// note: iControlNo = -1, means not a widget. +void Field::ParseFieldName(const std::wstring& strFieldNameParsed, + std::wstring& strFieldName, + int& iControlNo) { + int iStart = strFieldNameParsed.find_last_of(L'.'); + if (iStart == -1) { + strFieldName = strFieldNameParsed; + iControlNo = -1; + return; + } + std::wstring suffixal = strFieldNameParsed.substr(iStart + 1); + iControlNo = FXSYS_wtoi(suffixal.c_str()); + if (iControlNo == 0) { + int iSpaceStart; + while ((iSpaceStart = suffixal.find_last_of(L" ")) != -1) { + suffixal.erase(iSpaceStart, 1); + } + + if (suffixal.compare(L"0") != 0) { + strFieldName = strFieldNameParsed; + iControlNo = -1; + return; + } + } + strFieldName = strFieldNameParsed.substr(0, iStart); +} + +bool Field::AttachField(Document* pDocument, const WideString& csFieldName) { + m_pJSDoc = pDocument; + m_pFormFillEnv.Reset(pDocument->GetFormFillEnv()); + m_bCanSet = m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM) || + m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) || + m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY); + + CPDFSDK_InterForm* pRDInterForm = m_pFormFillEnv->GetInterForm(); + CPDF_InterForm* pInterForm = pRDInterForm->GetInterForm(); + WideString swFieldNameTemp = csFieldName; + swFieldNameTemp.Replace(L"..", L"."); + + if (pInterForm->CountFields(swFieldNameTemp) <= 0) { + std::wstring strFieldName; + int iControlNo = -1; + ParseFieldName(swFieldNameTemp.c_str(), strFieldName, iControlNo); + if (iControlNo == -1) + return false; + + m_FieldName = strFieldName.c_str(); + m_nFormControlIndex = iControlNo; + return true; + } + + m_FieldName = swFieldNameTemp; + m_nFormControlIndex = -1; + + return true; +} + +std::vector<CPDF_FormField*> Field::GetFormFields( + CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& csFieldName) { + std::vector<CPDF_FormField*> fields; + CPDFSDK_InterForm* pReaderInterForm = pFormFillEnv->GetInterForm(); + CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm(); + for (int i = 0, sz = pInterForm->CountFields(csFieldName); i < sz; ++i) { + if (CPDF_FormField* pFormField = pInterForm->GetField(i, csFieldName)) + fields.push_back(pFormField); + } + return fields; +} + +std::vector<CPDF_FormField*> Field::GetFormFields( + const WideString& csFieldName) const { + return Field::GetFormFields(m_pFormFillEnv.Get(), csFieldName); +} + +void Field::UpdateFormField(CPDFSDK_FormFillEnvironment* pFormFillEnv, + CPDF_FormField* pFormField, + bool bChangeMark, + bool bResetAP, + bool bRefresh) { + CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm(); + + if (bResetAP) { + std::vector<CPDFSDK_Annot::ObservedPtr> widgets; + pInterForm->GetWidgets(pFormField, &widgets); + + int nFieldType = pFormField->GetFieldType(); + if (nFieldType == FIELDTYPE_COMBOBOX || nFieldType == FIELDTYPE_TEXTFIELD) { + for (auto& pObserved : widgets) { + if (pObserved) { + bool bFormatted = false; + WideString sValue = static_cast<CPDFSDK_Widget*>(pObserved.Get()) + ->OnFormat(bFormatted); + if (pObserved) { // Not redundant, may be clobbered by OnFormat. + static_cast<CPDFSDK_Widget*>(pObserved.Get()) + ->ResetAppearance(bFormatted ? &sValue : nullptr, false); + } + } + } + } else { + for (auto& pObserved : widgets) { + if (pObserved) { + static_cast<CPDFSDK_Widget*>(pObserved.Get()) + ->ResetAppearance(nullptr, false); + } + } + } + } + + if (bRefresh) { + // Refresh the widget list. The calls in |bResetAP| may have caused widgets + // to be removed from the list. We need to call |GetWidgets| again to be + // sure none of the widgets have been deleted. + std::vector<CPDFSDK_Annot::ObservedPtr> widgets; + pInterForm->GetWidgets(pFormField, &widgets); + + // TODO(dsinclair): Determine if all widgets share the same + // CPDFSDK_InterForm. If that's the case, we can move the code to + // |GetFormFillEnv| out of the loop. + for (auto& pObserved : widgets) { + if (pObserved) { + CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pObserved.Get()); + pWidget->GetInterForm()->GetFormFillEnv()->UpdateAllViews(nullptr, + pWidget); + } + } + } + + if (bChangeMark) + pFormFillEnv->SetChangeMark(); +} + +void Field::UpdateFormControl(CPDFSDK_FormFillEnvironment* pFormFillEnv, + CPDF_FormControl* pFormControl, + bool bChangeMark, + bool bResetAP, + bool bRefresh) { + ASSERT(pFormControl); + + CPDFSDK_InterForm* pForm = pFormFillEnv->GetInterForm(); + CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl); + + if (pWidget) { + CPDFSDK_Widget::ObservedPtr observed_widget(pWidget); + if (bResetAP) { + int nFieldType = pWidget->GetFieldType(); + if (nFieldType == FIELDTYPE_COMBOBOX || + nFieldType == FIELDTYPE_TEXTFIELD) { + bool bFormatted = false; + WideString sValue = pWidget->OnFormat(bFormatted); + if (!observed_widget) + return; + pWidget->ResetAppearance(bFormatted ? &sValue : nullptr, false); + } else { + pWidget->ResetAppearance(nullptr, false); + } + if (!observed_widget) + return; + } + + if (bRefresh) { + CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm(); + pInterForm->GetFormFillEnv()->UpdateAllViews(nullptr, pWidget); + } + } + + if (bChangeMark) + pFormFillEnv->SetChangeMark(); +} + +CPDFSDK_Widget* Field::GetWidget(CPDFSDK_FormFillEnvironment* pFormFillEnv, + CPDF_FormControl* pFormControl) { + CPDFSDK_InterForm* pInterForm = + static_cast<CPDFSDK_InterForm*>(pFormFillEnv->GetInterForm()); + return pInterForm ? pInterForm->GetWidget(pFormControl) : nullptr; +} + +bool Field::ValueIsOccur(CPDF_FormField* pFormField, WideString csOptLabel) { + for (int i = 0, sz = pFormField->CountOptions(); i < sz; i++) { + if (csOptLabel.Compare(pFormField->GetOptionLabel(i)) == 0) + return true; + } + + return false; +} + +CPDF_FormControl* Field::GetSmartFieldControl(CPDF_FormField* pFormField) { + if (!pFormField->CountControls() || + m_nFormControlIndex >= pFormField->CountControls()) + return nullptr; + if (m_nFormControlIndex < 0) + return pFormField->GetControl(0); + return pFormField->GetControl(m_nFormControlIndex); +} + +CJS_Return Field::get_alignment(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + switch (pFormControl->GetControlAlignment()) { + case 0: + return CJS_Return(pRuntime->NewString(L"left")); + case 1: + return CJS_Return(pRuntime->NewString(L"center")); + case 2: + return CJS_Return(pRuntime->NewString(L"right")); + } + return CJS_Return(pRuntime->NewString(L"")); +} + +CJS_Return Field::set_alignment(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_border_style(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (!pFormField) + return CJS_Return(false); + + CPDFSDK_Widget* pWidget = + GetWidget(m_pFormFillEnv.Get(), GetSmartFieldControl(pFormField)); + if (!pWidget) + return CJS_Return(false); + + switch (pWidget->GetBorderStyle()) { + case BorderStyle::SOLID: + return CJS_Return(pRuntime->NewString(L"solid")); + case BorderStyle::DASH: + return CJS_Return(pRuntime->NewString(L"dashed")); + case BorderStyle::BEVELED: + return CJS_Return(pRuntime->NewString(L"beveled")); + case BorderStyle::INSET: + return CJS_Return(pRuntime->NewString(L"inset")); + case BorderStyle::UNDERLINE: + return CJS_Return(pRuntime->NewString(L"underline")); + } + return CJS_Return(pRuntime->NewString(L"")); +} + +CJS_Return Field::set_border_style(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + + if (!m_bCanSet) + return CJS_Return(false); + + ByteString byte_str = ByteString::FromUnicode(pRuntime->ToWideString(vp)); + if (m_bDelay) { + AddDelay_String(FP_BORDERSTYLE, byte_str); + } else { + Field::SetBorderStyle(m_pFormFillEnv.Get(), m_FieldName, + m_nFormControlIndex, byte_str); + } + return CJS_Return(true); +} + +void Field::SetBorderStyle(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + const ByteString& string) { + ASSERT(pFormFillEnv); + + BorderStyle nBorderStyle = BorderStyle::SOLID; + if (string == "solid") + nBorderStyle = BorderStyle::SOLID; + else if (string == "beveled") + nBorderStyle = BorderStyle::BEVELED; + else if (string == "dashed") + nBorderStyle = BorderStyle::DASH; + else if (string == "inset") + nBorderStyle = BorderStyle::INSET; + else if (string == "underline") + nBorderStyle = BorderStyle::UNDERLINE; + else + return; + + std::vector<CPDF_FormField*> FieldArray = + GetFormFields(pFormFillEnv, swFieldName); + for (CPDF_FormField* pFormField : FieldArray) { + if (nControlIndex < 0) { + bool bSet = false; + for (int i = 0, sz = pFormField->CountControls(); i < sz; ++i) { + if (CPDFSDK_Widget* pWidget = + GetWidget(pFormFillEnv, pFormField->GetControl(i))) { + if (pWidget->GetBorderStyle() != nBorderStyle) { + pWidget->SetBorderStyle(nBorderStyle); + bSet = true; + } + } + } + if (bSet) + UpdateFormField(pFormFillEnv, pFormField, true, true, true); + } else { + if (nControlIndex >= pFormField->CountControls()) + return; + if (CPDF_FormControl* pFormControl = + pFormField->GetControl(nControlIndex)) { + if (CPDFSDK_Widget* pWidget = GetWidget(pFormFillEnv, pFormControl)) { + if (pWidget->GetBorderStyle() != nBorderStyle) { + pWidget->SetBorderStyle(nBorderStyle); + UpdateFormControl(pFormFillEnv, pFormControl, true, true, true); + } + } + } + } + } +} + +CJS_Return Field::get_button_align_x(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + CPDF_IconFit IconFit = pFormControl->GetIconFit(); + + float fLeft; + float fBottom; + IconFit.GetIconPosition(fLeft, fBottom); + + return CJS_Return(pRuntime->NewNumber(static_cast<int32_t>(fLeft))); +} + +CJS_Return Field::set_button_align_x(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_button_align_y(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + CPDF_IconFit IconFit = pFormControl->GetIconFit(); + + float fLeft; + float fBottom; + IconFit.GetIconPosition(fLeft, fBottom); + + return CJS_Return(pRuntime->NewNumber(static_cast<int32_t>(fBottom))); +} + +CJS_Return Field::set_button_align_y(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_button_fit_bounds(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + return CJS_Return( + pRuntime->NewBoolean(pFormControl->GetIconFit().GetFittingBounds())); +} + +CJS_Return Field::set_button_fit_bounds(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_button_position(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewNumber(pFormControl->GetTextPosition())); +} + +CJS_Return Field::set_button_position(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_button_scale_how(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + pFormControl->GetIconFit().IsProportionalScale() ? 0 : 1)); +} + +CJS_Return Field::set_button_scale_how(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_button_scale_when(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + CPDF_IconFit IconFit = pFormControl->GetIconFit(); + int ScaleM = IconFit.GetScaleMethod(); + switch (ScaleM) { + case CPDF_IconFit::Always: + return CJS_Return( + pRuntime->NewNumber(static_cast<int32_t>(CPDF_IconFit::Always))); + case CPDF_IconFit::Bigger: + return CJS_Return( + pRuntime->NewNumber(static_cast<int32_t>(CPDF_IconFit::Bigger))); + case CPDF_IconFit::Never: + return CJS_Return( + pRuntime->NewNumber(static_cast<int32_t>(CPDF_IconFit::Never))); + case CPDF_IconFit::Smaller: + return CJS_Return( + pRuntime->NewNumber(static_cast<int32_t>(CPDF_IconFit::Smaller))); + } + return CJS_Return(true); +} + +CJS_Return Field::set_button_scale_when(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_calc_order_index(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_COMBOBOX && + pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD) { + return CJS_Return(false); + } + + CPDFSDK_InterForm* pRDInterForm = m_pFormFillEnv->GetInterForm(); + CPDF_InterForm* pInterForm = pRDInterForm->GetInterForm(); + return CJS_Return(pRuntime->NewNumber(static_cast<int32_t>( + pInterForm->FindFieldInCalculationOrder(pFormField)))); +} + +CJS_Return Field::set_calc_order_index(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_char_limit(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD) + return CJS_Return(false); + return CJS_Return( + pRuntime->NewNumber(static_cast<int32_t>(pFormField->GetMaxLen()))); +} + +CJS_Return Field::set_char_limit(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_comb(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD) + return CJS_Return(false); + + return CJS_Return( + pRuntime->NewBoolean(!!(pFormField->GetFieldFlags() & FIELDFLAG_COMB))); +} + +CJS_Return Field::set_comb(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_commit_on_sel_change(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_COMBOBOX && + pFormField->GetFieldType() != FIELDTYPE_LISTBOX) { + return CJS_Return(false); + } + + return CJS_Return(pRuntime->NewBoolean( + !!(pFormField->GetFieldFlags() & FIELDFLAG_COMMITONSELCHANGE))); +} + +CJS_Return Field::set_commit_on_sel_change(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_current_value_indices(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_COMBOBOX && + pFormField->GetFieldType() != FIELDTYPE_LISTBOX) { + return CJS_Return(false); + } + + int count = pFormField->CountSelectedItems(); + if (count <= 0) + return CJS_Return(pRuntime->NewNumber(-1)); + if (count == 1) + return CJS_Return(pRuntime->NewNumber(pFormField->GetSelectedIndex(0))); + + v8::Local<v8::Array> SelArray = pRuntime->NewArray(); + for (int i = 0; i < count; i++) { + pRuntime->PutArrayElement( + SelArray, i, pRuntime->NewNumber(pFormField->GetSelectedIndex(i))); + } + if (SelArray.IsEmpty()) + return CJS_Return(pRuntime->NewArray()); + return CJS_Return(SelArray); +} + +CJS_Return Field::set_current_value_indices(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + if (!m_bCanSet) + return CJS_Return(false); + + std::vector<uint32_t> array; + if (vp->IsNumber()) { + array.push_back(pRuntime->ToInt32(vp)); + } else if (!vp.IsEmpty() && vp->IsArray()) { + v8::Local<v8::Array> SelArray = pRuntime->ToArray(vp); + for (size_t i = 0; i < pRuntime->GetArrayLength(SelArray); i++) { + array.push_back( + pRuntime->ToInt32(pRuntime->GetArrayElement(SelArray, i))); + } + } + + if (m_bDelay) { + AddDelay_WordArray(FP_CURRENTVALUEINDICES, array); + } else { + Field::SetCurrentValueIndices(m_pFormFillEnv.Get(), m_FieldName, + m_nFormControlIndex, array); + } + return CJS_Return(true); +} + +void Field::SetCurrentValueIndices(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + const std::vector<uint32_t>& array) { + ASSERT(pFormFillEnv); + std::vector<CPDF_FormField*> FieldArray = + GetFormFields(pFormFillEnv, swFieldName); + + for (CPDF_FormField* pFormField : FieldArray) { + int nFieldType = pFormField->GetFieldType(); + if (nFieldType == FIELDTYPE_COMBOBOX || nFieldType == FIELDTYPE_LISTBOX) { + uint32_t dwFieldFlags = pFormField->GetFieldFlags(); + pFormField->ClearSelection(true); + for (size_t i = 0; i < array.size(); ++i) { + if (i != 0 && !(dwFieldFlags & (1 << 21))) + break; + if (array[i] < static_cast<uint32_t>(pFormField->CountOptions()) && + !pFormField->IsItemSelected(array[i])) { + pFormField->SetItemSelection(array[i], true); + } + } + UpdateFormField(pFormFillEnv, pFormField, true, true, true); + } + } +} + +CJS_Return Field::get_default_style(CJS_Runtime* pRuntime) { + return CJS_Return(false); +} + +CJS_Return Field::set_default_style(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return Field::get_default_value(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() == FIELDTYPE_PUSHBUTTON || + pFormField->GetFieldType() == FIELDTYPE_SIGNATURE) { + return CJS_Return(false); + } + + return CJS_Return(pRuntime->NewString(pFormField->GetDefaultValue().c_str())); +} + +CJS_Return Field::set_default_value(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_do_not_scroll(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + !!(pFormField->GetFieldFlags() & FIELDFLAG_DONOTSCROLL))); +} + +CJS_Return Field::set_do_not_scroll(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_do_not_spell_check(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD && + pFormField->GetFieldType() != FIELDTYPE_COMBOBOX) { + return CJS_Return(false); + } + + return CJS_Return(pRuntime->NewBoolean( + !!(pFormField->GetFieldFlags() & FIELDFLAG_DONOTSPELLCHECK))); +} + +CJS_Return Field::set_do_not_spell_check(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +void Field::SetDelay(bool bDelay) { + m_bDelay = bDelay; + + if (m_bDelay) + return; + if (m_pJSDoc) + m_pJSDoc->DoFieldDelay(m_FieldName, m_nFormControlIndex); +} + +CJS_Return Field::get_delay(CJS_Runtime* pRuntime) { + return CJS_Return(pRuntime->NewBoolean(m_bDelay)); +} + +CJS_Return Field::set_delay(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + if (!m_bCanSet) + return CJS_Return(false); + + SetDelay(pRuntime->ToBoolean(vp)); + return CJS_Return(true); +} + +CJS_Return Field::get_display(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + ASSERT(pFormField); + + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + CPDFSDK_Widget* pWidget = + pInterForm->GetWidget(GetSmartFieldControl(pFormField)); + if (!pWidget) + return CJS_Return(false); + + uint32_t dwFlag = pWidget->GetFlags(); + if (ANNOTFLAG_INVISIBLE & dwFlag || ANNOTFLAG_HIDDEN & dwFlag) + return CJS_Return(pRuntime->NewNumber(1)); + + if (ANNOTFLAG_PRINT & dwFlag) { + if (ANNOTFLAG_NOVIEW & dwFlag) + return CJS_Return(pRuntime->NewNumber(3)); + return CJS_Return(pRuntime->NewNumber(0)); + } + return CJS_Return(pRuntime->NewNumber(2)); +} + +CJS_Return Field::set_display(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + if (!m_bCanSet) + return CJS_Return(false); + + if (m_bDelay) { + AddDelay_Int(FP_DISPLAY, pRuntime->ToInt32(vp)); + } else { + Field::SetDisplay(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex, + pRuntime->ToInt32(vp)); + } + return CJS_Return(true); +} + +void Field::SetDisplay(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + int number) { + CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm(); + std::vector<CPDF_FormField*> FieldArray = + GetFormFields(pFormFillEnv, swFieldName); + for (CPDF_FormField* pFormField : FieldArray) { + if (nControlIndex < 0) { + bool bAnySet = false; + for (int i = 0, sz = pFormField->CountControls(); i < sz; ++i) { + CPDF_FormControl* pFormControl = pFormField->GetControl(i); + ASSERT(pFormControl); + + CPDFSDK_Widget* pWidget = pInterForm->GetWidget(pFormControl); + if (SetWidgetDisplayStatus(pWidget, number)) + bAnySet = true; + } + + if (bAnySet) + UpdateFormField(pFormFillEnv, pFormField, true, false, true); + } else { + if (nControlIndex >= pFormField->CountControls()) + return; + + CPDF_FormControl* pFormControl = pFormField->GetControl(nControlIndex); + if (!pFormControl) + return; + + CPDFSDK_Widget* pWidget = pInterForm->GetWidget(pFormControl); + if (SetWidgetDisplayStatus(pWidget, number)) + UpdateFormControl(pFormFillEnv, pFormControl, true, false, true); + } + } +} + +CJS_Return Field::get_doc(CJS_Runtime* pRuntime) { + return CJS_Return(m_pJSDoc->GetCJSDoc()->ToV8Object()); +} + +CJS_Return Field::set_doc(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return Field::get_editable(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_COMBOBOX) + return CJS_Return(false); + + return CJS_Return( + pRuntime->NewBoolean(!!(pFormField->GetFieldFlags() & FIELDFLAG_EDIT))); +} + +CJS_Return Field::set_editable(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_export_values(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_CHECKBOX && + pFormField->GetFieldType() != FIELDTYPE_RADIOBUTTON) { + return CJS_Return(false); + } + + v8::Local<v8::Array> ExportValuesArray = pRuntime->NewArray(); + if (m_nFormControlIndex < 0) { + for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) { + CPDF_FormControl* pFormControl = pFormField->GetControl(i); + pRuntime->PutArrayElement( + ExportValuesArray, i, + pRuntime->NewString(pFormControl->GetExportValue().c_str())); + } + } else { + if (m_nFormControlIndex >= pFormField->CountControls()) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = + pFormField->GetControl(m_nFormControlIndex); + if (!pFormControl) + return CJS_Return(false); + + pRuntime->PutArrayElement( + ExportValuesArray, 0, + pRuntime->NewString(pFormControl->GetExportValue().c_str())); + } + return CJS_Return(ExportValuesArray); +} + +CJS_Return Field::set_export_values(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_CHECKBOX && + pFormField->GetFieldType() != FIELDTYPE_RADIOBUTTON) { + return CJS_Return(false); + } + + return CJS_Return(m_bCanSet && !vp.IsEmpty() && vp->IsArray()); +} + +CJS_Return Field::get_file_select(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + !!(pFormField->GetFieldFlags() & FIELDFLAG_FILESELECT))); +} + +CJS_Return Field::set_file_select(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD) + return CJS_Return(false); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_fill_color(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + ASSERT(pFormField); + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + int iColorType; + pFormControl->GetBackgroundColor(iColorType); + + CFX_Color color; + if (iColorType == CFX_Color::kTransparent) { + color = CFX_Color(CFX_Color::kTransparent); + } else if (iColorType == CFX_Color::kGray) { + color = CFX_Color(CFX_Color::kGray, + pFormControl->GetOriginalBackgroundColor(0)); + } else if (iColorType == CFX_Color::kRGB) { + color = + CFX_Color(CFX_Color::kRGB, pFormControl->GetOriginalBackgroundColor(0), + pFormControl->GetOriginalBackgroundColor(1), + pFormControl->GetOriginalBackgroundColor(2)); + } else if (iColorType == CFX_Color::kCMYK) { + color = + CFX_Color(CFX_Color::kCMYK, pFormControl->GetOriginalBackgroundColor(0), + pFormControl->GetOriginalBackgroundColor(1), + pFormControl->GetOriginalBackgroundColor(2), + pFormControl->GetOriginalBackgroundColor(3)); + } else { + return CJS_Return(false); + } + + v8::Local<v8::Value> array = color::ConvertPWLColorToArray(pRuntime, color); + if (array.IsEmpty()) + return CJS_Return(pRuntime->NewArray()); + return CJS_Return(array); +} + +CJS_Return Field::set_fill_color(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + if (!m_bCanSet) + return CJS_Return(false); + if (vp.IsEmpty() || !vp->IsArray()) + return CJS_Return(false); + return CJS_Return(true); +} + +CJS_Return Field::get_hidden(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + ASSERT(pFormField); + + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + CPDFSDK_Widget* pWidget = + pInterForm->GetWidget(GetSmartFieldControl(pFormField)); + if (!pWidget) + return CJS_Return(false); + + uint32_t dwFlags = pWidget->GetFlags(); + return CJS_Return(pRuntime->NewBoolean(ANNOTFLAG_INVISIBLE & dwFlags || + ANNOTFLAG_HIDDEN & dwFlags)); +} + +CJS_Return Field::set_hidden(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + if (!m_bCanSet) + return CJS_Return(false); + + if (m_bDelay) { + AddDelay_Bool(FP_HIDDEN, pRuntime->ToBoolean(vp)); + } else { + Field::SetHidden(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex, + pRuntime->ToBoolean(vp)); + } + return CJS_Return(true); +} + +void Field::SetHidden(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + bool b) { + int display = b ? 1 /*Hidden*/ : 0 /*Visible*/; + SetDisplay(pFormFillEnv, swFieldName, nControlIndex, display); +} + +CJS_Return Field::get_highlight(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + int eHM = pFormControl->GetHighlightingMode(); + switch (eHM) { + case CPDF_FormControl::None: + return CJS_Return(pRuntime->NewString(L"none")); + case CPDF_FormControl::Push: + return CJS_Return(pRuntime->NewString(L"push")); + case CPDF_FormControl::Invert: + return CJS_Return(pRuntime->NewString(L"invert")); + case CPDF_FormControl::Outline: + return CJS_Return(pRuntime->NewString(L"outline")); + case CPDF_FormControl::Toggle: + return CJS_Return(pRuntime->NewString(L"toggle")); + } + return CJS_Return(true); +} + +CJS_Return Field::set_highlight(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_line_width(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + ASSERT(pFormField); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + if (!pFormField->CountControls()) + return CJS_Return(false); + + CPDFSDK_Widget* pWidget = pInterForm->GetWidget(pFormField->GetControl(0)); + if (!pWidget) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewNumber(pWidget->GetBorderWidth())); +} + +CJS_Return Field::set_line_width(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + if (!m_bCanSet) + return CJS_Return(false); + + if (m_bDelay) { + AddDelay_Int(FP_LINEWIDTH, pRuntime->ToInt32(vp)); + } else { + Field::SetLineWidth(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex, + pRuntime->ToInt32(vp)); + } + return CJS_Return(true); +} + +void Field::SetLineWidth(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + int number) { + CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm(); + std::vector<CPDF_FormField*> FieldArray = + GetFormFields(pFormFillEnv, swFieldName); + for (CPDF_FormField* pFormField : FieldArray) { + if (nControlIndex < 0) { + bool bSet = false; + for (int i = 0, sz = pFormField->CountControls(); i < sz; ++i) { + CPDF_FormControl* pFormControl = pFormField->GetControl(i); + ASSERT(pFormControl); + + if (CPDFSDK_Widget* pWidget = pInterForm->GetWidget(pFormControl)) { + if (number != pWidget->GetBorderWidth()) { + pWidget->SetBorderWidth(number); + bSet = true; + } + } + } + if (bSet) + UpdateFormField(pFormFillEnv, pFormField, true, true, true); + } else { + if (nControlIndex >= pFormField->CountControls()) + return; + if (CPDF_FormControl* pFormControl = + pFormField->GetControl(nControlIndex)) { + if (CPDFSDK_Widget* pWidget = pInterForm->GetWidget(pFormControl)) { + if (number != pWidget->GetBorderWidth()) { + pWidget->SetBorderWidth(number); + UpdateFormControl(pFormFillEnv, pFormControl, true, true, true); + } + } + } + } + } +} + +CJS_Return Field::get_multiline(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + !!(pFormField->GetFieldFlags() & FIELDFLAG_MULTILINE))); +} + +CJS_Return Field::set_multiline(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_multiple_selection(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_LISTBOX) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + !!(pFormField->GetFieldFlags() & FIELDFLAG_MULTISELECT))); +} + +CJS_Return Field::set_multiple_selection(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_name(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewString(m_FieldName.c_str())); +} + +CJS_Return Field::set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return Field::get_num_items(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_COMBOBOX && + pFormField->GetFieldType() != FIELDTYPE_LISTBOX) { + return CJS_Return(false); + } + + return CJS_Return(pRuntime->NewNumber(pFormField->CountOptions())); +} + +CJS_Return Field::set_num_items(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return Field::get_page(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (!pFormField) + return CJS_Return(false); + + std::vector<CPDFSDK_Annot::ObservedPtr> widgets; + m_pFormFillEnv->GetInterForm()->GetWidgets(pFormField, &widgets); + if (widgets.empty()) + return CJS_Return(pRuntime->NewNumber(-1)); + + v8::Local<v8::Array> PageArray = pRuntime->NewArray(); + int i = 0; + for (const auto& pObserved : widgets) { + if (!pObserved) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSBADOBJECT)); + + auto* pWidget = static_cast<CPDFSDK_Widget*>(pObserved.Get()); + CPDFSDK_PageView* pPageView = pWidget->GetPageView(); + if (!pPageView) + return CJS_Return(false); + + pRuntime->PutArrayElement( + PageArray, i, + pRuntime->NewNumber(static_cast<int32_t>(pPageView->GetPageIndex()))); + ++i; + } + return CJS_Return(PageArray); +} + +CJS_Return Field::set_page(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(JSGetStringFromID(IDS_STRING_JSREADONLY)); +} + +CJS_Return Field::get_password(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + !!(pFormField->GetFieldFlags() & FIELDFLAG_PASSWORD))); +} + +CJS_Return Field::set_password(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_print(CJS_Runtime* pRuntime) { + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + CPDFSDK_Widget* pWidget = + pInterForm->GetWidget(GetSmartFieldControl(pFormField)); + if (!pWidget) + return CJS_Return(false); + + return CJS_Return( + pRuntime->NewBoolean(!!(pWidget->GetFlags() & ANNOTFLAG_PRINT))); +} + +CJS_Return Field::set_print(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + if (!m_bCanSet) + return CJS_Return(false); + + for (CPDF_FormField* pFormField : FieldArray) { + if (m_nFormControlIndex < 0) { + bool bSet = false; + for (int i = 0, sz = pFormField->CountControls(); i < sz; ++i) { + if (CPDFSDK_Widget* pWidget = + pInterForm->GetWidget(pFormField->GetControl(i))) { + uint32_t dwFlags = pWidget->GetFlags(); + if (pRuntime->ToBoolean(vp)) + dwFlags |= ANNOTFLAG_PRINT; + else + dwFlags &= ~ANNOTFLAG_PRINT; + + if (dwFlags != pWidget->GetFlags()) { + pWidget->SetFlags(dwFlags); + bSet = true; + } + } + } + + if (bSet) + UpdateFormField(m_pFormFillEnv.Get(), pFormField, true, false, true); + + continue; + } + + if (m_nFormControlIndex >= pFormField->CountControls()) + return CJS_Return(false); + + if (CPDF_FormControl* pFormControl = + pFormField->GetControl(m_nFormControlIndex)) { + if (CPDFSDK_Widget* pWidget = pInterForm->GetWidget(pFormControl)) { + uint32_t dwFlags = pWidget->GetFlags(); + if (pRuntime->ToBoolean(vp)) + dwFlags |= ANNOTFLAG_PRINT; + else + dwFlags &= ~ANNOTFLAG_PRINT; + + if (dwFlags != pWidget->GetFlags()) { + pWidget->SetFlags(dwFlags); + UpdateFormControl(m_pFormFillEnv.Get(), + pFormField->GetControl(m_nFormControlIndex), true, + false, true); + } + } + } + } + return CJS_Return(true); +} + +CJS_Return Field::get_radios_in_unison(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_RADIOBUTTON) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + !!(pFormField->GetFieldFlags() & FIELDFLAG_RADIOSINUNISON))); +} + +CJS_Return Field::set_radios_in_unison(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_readonly(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + !!(FieldArray[0]->GetFieldFlags() & FIELDFLAG_READONLY))); +} + +CJS_Return Field::set_readonly(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_rect(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + CPDFSDK_Widget* pWidget = + pInterForm->GetWidget(GetSmartFieldControl(pFormField)); + if (!pWidget) + return CJS_Return(false); + + CFX_FloatRect crRect = pWidget->GetRect(); + v8::Local<v8::Array> rcArray = pRuntime->NewArray(); + pRuntime->PutArrayElement( + rcArray, 0, pRuntime->NewNumber(static_cast<int32_t>(crRect.left))); + pRuntime->PutArrayElement( + rcArray, 1, pRuntime->NewNumber(static_cast<int32_t>(crRect.top))); + pRuntime->PutArrayElement( + rcArray, 2, pRuntime->NewNumber(static_cast<int32_t>(crRect.right))); + pRuntime->PutArrayElement( + rcArray, 3, pRuntime->NewNumber(static_cast<int32_t>(crRect.bottom))); + + return CJS_Return(rcArray); +} + +CJS_Return Field::set_rect(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + if (!m_bCanSet) + return CJS_Return(false); + if (vp.IsEmpty() || !vp->IsArray()) + return CJS_Return(false); + + v8::Local<v8::Array> rcArray = pRuntime->ToArray(vp); + if (pRuntime->GetArrayLength(rcArray) < 4) + return CJS_Return(false); + + float pArray[4]; + pArray[0] = static_cast<float>( + pRuntime->ToInt32(pRuntime->GetArrayElement(rcArray, 0))); + pArray[1] = static_cast<float>( + pRuntime->ToInt32(pRuntime->GetArrayElement(rcArray, 1))); + pArray[2] = static_cast<float>( + pRuntime->ToInt32(pRuntime->GetArrayElement(rcArray, 2))); + pArray[3] = static_cast<float>( + pRuntime->ToInt32(pRuntime->GetArrayElement(rcArray, 3))); + + CFX_FloatRect crRect(pArray); + if (m_bDelay) { + AddDelay_Rect(FP_RECT, crRect); + } else { + Field::SetRect(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex, + crRect); + } + return CJS_Return(true); +} + +void Field::SetRect(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + const CFX_FloatRect& rect) { + CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm(); + std::vector<CPDF_FormField*> FieldArray = + GetFormFields(pFormFillEnv, swFieldName); + for (CPDF_FormField* pFormField : FieldArray) { + if (nControlIndex < 0) { + bool bSet = false; + for (int i = 0, sz = pFormField->CountControls(); i < sz; ++i) { + CPDF_FormControl* pFormControl = pFormField->GetControl(i); + ASSERT(pFormControl); + + if (CPDFSDK_Widget* pWidget = pInterForm->GetWidget(pFormControl)) { + CFX_FloatRect crRect = rect; + + CPDF_Page* pPDFPage = pWidget->GetPDFPage(); + crRect.Intersect(pPDFPage->GetPageBBox()); + + if (!crRect.IsEmpty()) { + CFX_FloatRect rcOld = pWidget->GetRect(); + if (crRect.left != rcOld.left || crRect.right != rcOld.right || + crRect.top != rcOld.top || crRect.bottom != rcOld.bottom) { + pWidget->SetRect(crRect); + bSet = true; + } + } + } + } + + if (bSet) + UpdateFormField(pFormFillEnv, pFormField, true, true, true); + + continue; + } + + if (nControlIndex >= pFormField->CountControls()) + return; + if (CPDF_FormControl* pFormControl = + pFormField->GetControl(nControlIndex)) { + if (CPDFSDK_Widget* pWidget = pInterForm->GetWidget(pFormControl)) { + CFX_FloatRect crRect = rect; + + CPDF_Page* pPDFPage = pWidget->GetPDFPage(); + crRect.Intersect(pPDFPage->GetPageBBox()); + + if (!crRect.IsEmpty()) { + CFX_FloatRect rcOld = pWidget->GetRect(); + if (crRect.left != rcOld.left || crRect.right != rcOld.right || + crRect.top != rcOld.top || crRect.bottom != rcOld.bottom) { + pWidget->SetRect(crRect); + UpdateFormControl(pFormFillEnv, pFormControl, true, true, true); + } + } + } + } + } +} + +CJS_Return Field::get_required(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() == FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + !!(pFormField->GetFieldFlags() & FIELDFLAG_REQUIRED))); +} + +CJS_Return Field::set_required(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_rich_text(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_TEXTFIELD) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + !!(pFormField->GetFieldFlags() & FIELDFLAG_RICHTEXT))); +} + +CJS_Return Field::set_rich_text(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_rich_value(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Field::set_rich_value(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Field::get_rotation(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewNumber(pFormControl->GetRotation())); +} + +CJS_Return Field::set_rotation(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_stroke_color(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + int iColorType; + pFormControl->GetBorderColor(iColorType); + + CFX_Color color; + if (iColorType == CFX_Color::kTransparent) { + color = CFX_Color(CFX_Color::kTransparent); + } else if (iColorType == CFX_Color::kGray) { + color = + CFX_Color(CFX_Color::kGray, pFormControl->GetOriginalBorderColor(0)); + } else if (iColorType == CFX_Color::kRGB) { + color = CFX_Color(CFX_Color::kRGB, pFormControl->GetOriginalBorderColor(0), + pFormControl->GetOriginalBorderColor(1), + pFormControl->GetOriginalBorderColor(2)); + } else if (iColorType == CFX_Color::kCMYK) { + color = CFX_Color(CFX_Color::kCMYK, pFormControl->GetOriginalBorderColor(0), + pFormControl->GetOriginalBorderColor(1), + pFormControl->GetOriginalBorderColor(2), + pFormControl->GetOriginalBorderColor(3)); + } else { + return CJS_Return(false); + } + + v8::Local<v8::Value> array = color::ConvertPWLColorToArray(pRuntime, color); + if (array.IsEmpty()) + return CJS_Return(pRuntime->NewArray()); + return CJS_Return(array); +} + +CJS_Return Field::set_stroke_color(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + if (!m_bCanSet) + return CJS_Return(false); + if (vp.IsEmpty() || !vp->IsArray()) + return CJS_Return(false); + return CJS_Return(true); +} + +CJS_Return Field::get_style(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_RADIOBUTTON && + pFormField->GetFieldType() != FIELDTYPE_CHECKBOX) { + return CJS_Return(false); + } + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + WideString csWCaption = pFormControl->GetNormalCaption(); + ByteString csBCaption; + + switch (csWCaption[0]) { + case L'l': + csBCaption = "circle"; + break; + case L'8': + csBCaption = "cross"; + break; + case L'u': + csBCaption = "diamond"; + break; + case L'n': + csBCaption = "square"; + break; + case L'H': + csBCaption = "star"; + break; + default: // L'4' + csBCaption = "check"; + break; + } + return CJS_Return( + pRuntime->NewString(WideString::FromLocal(csBCaption.c_str()).c_str())); +} + +CJS_Return Field::set_style(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_submit_name(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Field::set_submit_name(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +CJS_Return Field::get_text_color(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + int iColorType; + FX_ARGB color; + CPDF_DefaultAppearance FieldAppearance = pFormControl->GetDefaultAppearance(); + FieldAppearance.GetColor(color, iColorType); + + int32_t a; + int32_t r; + int32_t g; + int32_t b; + std::tie(a, r, g, b) = ArgbDecode(color); + + CFX_Color crRet = + CFX_Color(CFX_Color::kRGB, r / 255.0f, g / 255.0f, b / 255.0f); + + if (iColorType == CFX_Color::kTransparent) + crRet = CFX_Color(CFX_Color::kTransparent); + + v8::Local<v8::Value> array = color::ConvertPWLColorToArray(pRuntime, crRet); + if (array.IsEmpty()) + return CJS_Return(pRuntime->NewArray()); + return CJS_Return(array); +} + +CJS_Return Field::set_text_color(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + if (!m_bCanSet) + return CJS_Return(false); + if (vp.IsEmpty() || !vp->IsArray()) + return CJS_Return(false); + return CJS_Return(true); +} + +CJS_Return Field::get_text_font(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + ASSERT(pFormField); + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + int nFieldType = pFormField->GetFieldType(); + if (nFieldType != FIELDTYPE_PUSHBUTTON && nFieldType != FIELDTYPE_COMBOBOX && + nFieldType != FIELDTYPE_LISTBOX && nFieldType != FIELDTYPE_TEXTFIELD) { + return CJS_Return(false); + } + + CPDF_Font* pFont = pFormControl->GetDefaultControlFont(); + if (!pFont) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewString( + WideString::FromLocal(pFont->GetBaseFont().c_str()).c_str())); +} + +CJS_Return Field::set_text_font(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + + if (!m_bCanSet) + return CJS_Return(false); + return CJS_Return( + !ByteString::FromUnicode(pRuntime->ToWideString(vp)).IsEmpty()); +} + +CJS_Return Field::get_text_size(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + ASSERT(pFormField); + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + float fFontSize; + CPDF_DefaultAppearance FieldAppearance = pFormControl->GetDefaultAppearance(); + FieldAppearance.GetFont(&fFontSize); + return CJS_Return(pRuntime->NewNumber(static_cast<int>(fFontSize))); +} + +CJS_Return Field::set_text_size(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_type(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + switch (pFormField->GetFieldType()) { + case FIELDTYPE_UNKNOWN: + return CJS_Return(pRuntime->NewString(L"unknown")); + case FIELDTYPE_PUSHBUTTON: + return CJS_Return(pRuntime->NewString(L"button")); + case FIELDTYPE_CHECKBOX: + return CJS_Return(pRuntime->NewString(L"checkbox")); + case FIELDTYPE_RADIOBUTTON: + return CJS_Return(pRuntime->NewString(L"radiobutton")); + case FIELDTYPE_COMBOBOX: + return CJS_Return(pRuntime->NewString(L"combobox")); + case FIELDTYPE_LISTBOX: + return CJS_Return(pRuntime->NewString(L"listbox")); + case FIELDTYPE_TEXTFIELD: + return CJS_Return(pRuntime->NewString(L"text")); + case FIELDTYPE_SIGNATURE: + return CJS_Return(pRuntime->NewString(L"signature")); + } + return CJS_Return(pRuntime->NewString(L"unknown")); +} + +CJS_Return Field::set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return Field::get_user_name(CJS_Runtime* pRuntime) { + ASSERT(m_pFormFillEnv); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + return CJS_Return( + pRuntime->NewString(FieldArray[0]->GetAlternateName().c_str())); +} + +CJS_Return Field::set_user_name(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + ASSERT(m_pFormFillEnv); + return CJS_Return(m_bCanSet); +} + +CJS_Return Field::get_value(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + v8::Local<v8::Value> ret; + + CPDF_FormField* pFormField = FieldArray[0]; + switch (pFormField->GetFieldType()) { + case FIELDTYPE_PUSHBUTTON: + return CJS_Return(false); + case FIELDTYPE_COMBOBOX: + case FIELDTYPE_TEXTFIELD: + ret = pRuntime->NewString(pFormField->GetValue().c_str()); + break; + case FIELDTYPE_LISTBOX: { + if (pFormField->CountSelectedItems() > 1) { + v8::Local<v8::Array> ValueArray = pRuntime->NewArray(); + v8::Local<v8::Value> ElementValue; + int iIndex; + for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz; i++) { + iIndex = pFormField->GetSelectedIndex(i); + ElementValue = + pRuntime->NewString(pFormField->GetOptionValue(iIndex).c_str()); + if (wcslen(pRuntime->ToWideString(ElementValue).c_str()) == 0) { + ElementValue = + pRuntime->NewString(pFormField->GetOptionLabel(iIndex).c_str()); + } + pRuntime->PutArrayElement(ValueArray, i, ElementValue); + } + ret = ValueArray; + } else { + ret = pRuntime->NewString(pFormField->GetValue().c_str()); + } + break; + } + case FIELDTYPE_CHECKBOX: + case FIELDTYPE_RADIOBUTTON: { + bool bFind = false; + for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) { + if (pFormField->GetControl(i)->IsChecked()) { + ret = pRuntime->NewString( + pFormField->GetControl(i)->GetExportValue().c_str()); + bFind = true; + break; + } + } + if (!bFind) + ret = pRuntime->NewString(L"Off"); + + break; + } + default: + ret = pRuntime->NewString(pFormField->GetValue().c_str()); + break; + } + return CJS_Return(pRuntime->MaybeCoerceToNumber(ret)); +} + +CJS_Return Field::set_value(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + if (!m_bCanSet) + return CJS_Return(false); + + std::vector<WideString> strArray; + if (!vp.IsEmpty() && vp->IsArray()) { + v8::Local<v8::Array> ValueArray = pRuntime->ToArray(vp); + for (size_t i = 0; i < pRuntime->GetArrayLength(ValueArray); i++) { + strArray.push_back( + pRuntime->ToWideString(pRuntime->GetArrayElement(ValueArray, i))); + } + } else { + strArray.push_back(pRuntime->ToWideString(vp)); + } + + if (m_bDelay) { + AddDelay_WideStringArray(FP_VALUE, strArray); + } else { + Field::SetValue(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex, + strArray); + } + return CJS_Return(true); +} + +void Field::SetValue(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + const std::vector<WideString>& strArray) { + ASSERT(pFormFillEnv); + if (strArray.empty()) + return; + + std::vector<CPDF_FormField*> FieldArray = + GetFormFields(pFormFillEnv, swFieldName); + + for (CPDF_FormField* pFormField : FieldArray) { + if (pFormField->GetFullName().Compare(swFieldName) != 0) + continue; + + switch (pFormField->GetFieldType()) { + case FIELDTYPE_TEXTFIELD: + case FIELDTYPE_COMBOBOX: + if (pFormField->GetValue() != strArray[0]) { + pFormField->SetValue(strArray[0], true); + UpdateFormField(pFormFillEnv, pFormField, true, false, true); + } + break; + case FIELDTYPE_CHECKBOX: + case FIELDTYPE_RADIOBUTTON: + if (pFormField->GetValue() != strArray[0]) { + pFormField->SetValue(strArray[0], true); + UpdateFormField(pFormFillEnv, pFormField, true, false, true); + } + break; + case FIELDTYPE_LISTBOX: { + bool bModified = false; + for (const auto& str : strArray) { + if (!pFormField->IsItemSelected(pFormField->FindOption(str))) { + bModified = true; + break; + } + } + if (bModified) { + pFormField->ClearSelection(true); + for (const auto& str : strArray) { + int index = pFormField->FindOption(str); + if (!pFormField->IsItemSelected(index)) + pFormField->SetItemSelection(index, true, true); + } + UpdateFormField(pFormFillEnv, pFormField, true, false, true); + } + break; + } + default: + break; + } + } +} + +CJS_Return Field::get_value_as_string(CJS_Runtime* pRuntime) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() == FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + if (pFormField->GetFieldType() == FIELDTYPE_CHECKBOX) { + if (!pFormField->CountControls()) + return CJS_Return(false); + return CJS_Return(pRuntime->NewString( + pFormField->GetControl(0)->IsChecked() ? L"Yes" : L"Off")); + } + + if (pFormField->GetFieldType() == FIELDTYPE_RADIOBUTTON && + !(pFormField->GetFieldFlags() & FIELDFLAG_RADIOSINUNISON)) { + for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) { + if (pFormField->GetControl(i)->IsChecked()) { + return CJS_Return(pRuntime->NewString( + pFormField->GetControl(i)->GetExportValue().c_str())); + } + } + return CJS_Return(pRuntime->NewString(L"Off")); + } + + if (pFormField->GetFieldType() == FIELDTYPE_LISTBOX && + (pFormField->CountSelectedItems() > 1)) { + return CJS_Return(pRuntime->NewString(L"")); + } + return CJS_Return(pRuntime->NewString(pFormField->GetValue().c_str())); +} + +CJS_Return Field::set_value_as_string(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp) { + return CJS_Return(false); +} + +CJS_Return Field::browseForFileToSubmit( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if ((pFormField->GetFieldFlags() & FIELDFLAG_FILESELECT) && + (pFormField->GetFieldType() == FIELDTYPE_TEXTFIELD)) { + WideString wsFileName = m_pFormFillEnv->JS_fieldBrowse(); + if (!wsFileName.IsEmpty()) { + pFormField->SetValue(wsFileName); + UpdateFormField(m_pFormFillEnv.Get(), pFormField, true, true, true); + } + return CJS_Return(true); + } + return CJS_Return(false); +} + +CJS_Return Field::buttonGetCaption( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + int nface = 0; + int iSize = params.size(); + if (iSize >= 1) + nface = pRuntime->ToInt32(params[0]); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + if (nface == 0) { + return CJS_Return( + pRuntime->NewString(pFormControl->GetNormalCaption().c_str())); + } else if (nface == 1) { + return CJS_Return( + pRuntime->NewString(pFormControl->GetDownCaption().c_str())); + } else if (nface == 2) { + return CJS_Return( + pRuntime->NewString(pFormControl->GetRolloverCaption().c_str())); + } + return CJS_Return(false); +} + +CJS_Return Field::buttonGetIcon( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() >= 1) { + int nFace = pRuntime->ToInt32(params[0]); + if (nFace < 0 || nFace > 2) + return CJS_Return(false); + } + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_PUSHBUTTON) + return CJS_Return(false); + + CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField); + if (!pFormControl) + return CJS_Return(false); + + v8::Local<v8::Object> pObj = + pRuntime->NewFxDynamicObj(CJS_Icon::GetObjDefnID()); + if (pObj.IsEmpty()) + return CJS_Return(false); + + CJS_Icon* pJS_Icon = static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj)); + if (!pJS_Icon) + return CJS_Return(false); + return CJS_Return(pJS_Icon->ToV8Object()); +} + +CJS_Return Field::buttonImportIcon( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Field::buttonSetCaption( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return Field::buttonSetIcon( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return Field::checkThisBox( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + int iSize = params.size(); + if (iSize < 1) + return CJS_Return(false); + + if (!m_bCanSet) + return CJS_Return(false); + + int nWidget = pRuntime->ToInt32(params[0]); + bool bCheckit = true; + if (iSize >= 2) + bCheckit = pRuntime->ToBoolean(params[1]); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (pFormField->GetFieldType() != FIELDTYPE_CHECKBOX && + pFormField->GetFieldType() != FIELDTYPE_RADIOBUTTON) { + return CJS_Return(false); + } + if (nWidget < 0 || nWidget >= pFormField->CountControls()) + return CJS_Return(false); + // TODO(weili): Check whether anything special needed for radio button, + // otherwise merge these branches. + if (pFormField->GetFieldType() == FIELDTYPE_RADIOBUTTON) + pFormField->CheckControl(nWidget, bCheckit, true); + else + pFormField->CheckControl(nWidget, bCheckit, true); + + UpdateFormField(m_pFormFillEnv.Get(), pFormField, true, true, true); + return CJS_Return(true); +} + +CJS_Return Field::clearItems(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Field::defaultIsChecked( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (!m_bCanSet) + return CJS_Return(false); + + int iSize = params.size(); + if (iSize < 1) + return CJS_Return(false); + + int nWidget = pRuntime->ToInt32(params[0]); + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (nWidget < 0 || nWidget >= pFormField->CountControls()) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + pFormField->GetFieldType() == FIELDTYPE_CHECKBOX || + pFormField->GetFieldType() == FIELDTYPE_RADIOBUTTON)); +} + +CJS_Return Field::deleteItemAt( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Field::getArray(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + std::vector<std::unique_ptr<WideString>> swSort; + for (CPDF_FormField* pFormField : FieldArray) { + swSort.push_back( + std::unique_ptr<WideString>(new WideString(pFormField->GetFullName()))); + } + + std::sort(swSort.begin(), swSort.end(), + [](const std::unique_ptr<WideString>& p1, + const std::unique_ptr<WideString>& p2) { return *p1 < *p2; }); + + v8::Local<v8::Array> FormFieldArray = pRuntime->NewArray(); + int j = 0; + for (const auto& pStr : swSort) { + v8::Local<v8::Object> pObj = + pRuntime->NewFxDynamicObj(CJS_Field::GetObjDefnID()); + if (pObj.IsEmpty()) + return CJS_Return(false); + + CJS_Field* pJSField = + static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pObj)); + Field* pField = static_cast<Field*>(pJSField->GetEmbedObject()); + pField->AttachField(m_pJSDoc, *pStr); + pRuntime->PutArrayElement(FormFieldArray, j++, + pJSField + ? v8::Local<v8::Value>(pJSField->ToV8Object()) + : v8::Local<v8::Value>()); + } + return CJS_Return(FormFieldArray); +} + +CJS_Return Field::getItemAt(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + int iSize = params.size(); + int nIdx = -1; + if (iSize >= 1) + nIdx = pRuntime->ToInt32(params[0]); + + bool bExport = true; + if (iSize >= 2) + bExport = pRuntime->ToBoolean(params[1]); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if ((pFormField->GetFieldType() == FIELDTYPE_LISTBOX) || + (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX)) { + if (nIdx == -1 || nIdx > pFormField->CountOptions()) + nIdx = pFormField->CountOptions() - 1; + if (bExport) { + WideString strval = pFormField->GetOptionValue(nIdx); + if (strval.IsEmpty()) { + return CJS_Return( + pRuntime->NewString(pFormField->GetOptionLabel(nIdx).c_str())); + } + return CJS_Return(pRuntime->NewString(strval.c_str())); + } + return CJS_Return( + pRuntime->NewString(pFormField->GetOptionLabel(nIdx).c_str())); + } + return CJS_Return(false); +} + +CJS_Return Field::getLock(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return Field::insertItemAt( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Field::isBoxChecked( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + int nIndex = -1; + if (params.size() >= 1) + nIndex = pRuntime->ToInt32(params[0]); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (nIndex < 0 || nIndex >= pFormField->CountControls()) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + ((pFormField->GetFieldType() == FIELDTYPE_CHECKBOX || + pFormField->GetFieldType() == FIELDTYPE_RADIOBUTTON) && + pFormField->GetControl(nIndex)->IsChecked() != 0))); +} + +CJS_Return Field::isDefaultChecked( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + int nIndex = -1; + if (params.size() >= 1) + nIndex = pRuntime->ToInt32(params[0]); + + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + if (nIndex < 0 || nIndex >= pFormField->CountControls()) + return CJS_Return(false); + + return CJS_Return(pRuntime->NewBoolean( + ((pFormField->GetFieldType() == FIELDTYPE_CHECKBOX || + pFormField->GetFieldType() == FIELDTYPE_RADIOBUTTON) && + pFormField->GetControl(nIndex)->IsDefaultChecked() != 0))); +} + +CJS_Return Field::setAction(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Field::setFocus(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); + if (FieldArray.empty()) + return CJS_Return(false); + + CPDF_FormField* pFormField = FieldArray[0]; + int32_t nCount = pFormField->CountControls(); + if (nCount < 1) + return CJS_Return(false); + + CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm(); + CPDFSDK_Widget* pWidget = nullptr; + if (nCount == 1) { + pWidget = pInterForm->GetWidget(pFormField->GetControl(0)); + } else { + UnderlyingPageType* pPage = + UnderlyingFromFPDFPage(m_pFormFillEnv->GetCurrentPage( + m_pFormFillEnv->GetUnderlyingDocument())); + if (!pPage) + return CJS_Return(false); + if (CPDFSDK_PageView* pCurPageView = + m_pFormFillEnv->GetPageView(pPage, true)) { + for (int32_t i = 0; i < nCount; i++) { + if (CPDFSDK_Widget* pTempWidget = + pInterForm->GetWidget(pFormField->GetControl(i))) { + if (pTempWidget->GetPDFPage() == pCurPageView->GetPDFPage()) { + pWidget = pTempWidget; + break; + } + } + } + } + } + + if (pWidget) { + CPDFSDK_Annot::ObservedPtr pObserved(pWidget); + m_pFormFillEnv->SetFocusAnnot(&pObserved); + } + + return CJS_Return(true); +} + +CJS_Return Field::setItems(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(true); +} + +CJS_Return Field::setLock(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return Field::signatureGetModifications( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return Field::signatureGetSeedValue( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return Field::signatureInfo( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return Field::signatureSetSeedValue( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return Field::signatureSign( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return Field::signatureValidate( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return CJS_Return(false); +} + +CJS_Return Field::get_source(CJS_Runtime* pRuntime) { + return CJS_Return(true); +} + +CJS_Return Field::set_source(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(true); +} + +void Field::AddDelay_Int(FIELD_PROP prop, int32_t n) { + CJS_DelayData* pNewData = + new CJS_DelayData(prop, m_nFormControlIndex, m_FieldName); + pNewData->num = n; + m_pJSDoc->AddDelayData(pNewData); +} + +void Field::AddDelay_Bool(FIELD_PROP prop, bool b) { + CJS_DelayData* pNewData = + new CJS_DelayData(prop, m_nFormControlIndex, m_FieldName); + pNewData->b = b; + m_pJSDoc->AddDelayData(pNewData); +} + +void Field::AddDelay_String(FIELD_PROP prop, const ByteString& string) { + CJS_DelayData* pNewData = + new CJS_DelayData(prop, m_nFormControlIndex, m_FieldName); + pNewData->string = string; + m_pJSDoc->AddDelayData(pNewData); +} + +void Field::AddDelay_Rect(FIELD_PROP prop, const CFX_FloatRect& rect) { + CJS_DelayData* pNewData = + new CJS_DelayData(prop, m_nFormControlIndex, m_FieldName); + pNewData->rect = rect; + m_pJSDoc->AddDelayData(pNewData); +} + +void Field::AddDelay_WordArray(FIELD_PROP prop, + const std::vector<uint32_t>& array) { + CJS_DelayData* pNewData = + new CJS_DelayData(prop, m_nFormControlIndex, m_FieldName); + pNewData->wordarray = array; + m_pJSDoc->AddDelayData(pNewData); +} + +void Field::AddDelay_WideStringArray(FIELD_PROP prop, + const std::vector<WideString>& array) { + CJS_DelayData* pNewData = + new CJS_DelayData(prop, m_nFormControlIndex, m_FieldName); + pNewData->widestringarray = array; + m_pJSDoc->AddDelayData(pNewData); +} + +void Field::DoDelay(CPDFSDK_FormFillEnvironment* pFormFillEnv, + CJS_DelayData* pData) { + ASSERT(pFormFillEnv); + switch (pData->eProp) { + case FP_BORDERSTYLE: + Field::SetBorderStyle(pFormFillEnv, pData->sFieldName, + pData->nControlIndex, pData->string); + break; + case FP_CURRENTVALUEINDICES: + Field::SetCurrentValueIndices(pFormFillEnv, pData->sFieldName, + pData->nControlIndex, pData->wordarray); + break; + case FP_DISPLAY: + Field::SetDisplay(pFormFillEnv, pData->sFieldName, pData->nControlIndex, + pData->num); + break; + case FP_HIDDEN: + Field::SetHidden(pFormFillEnv, pData->sFieldName, pData->nControlIndex, + pData->b); + break; + case FP_LINEWIDTH: + Field::SetLineWidth(pFormFillEnv, pData->sFieldName, pData->nControlIndex, + pData->num); + break; + case FP_RECT: + Field::SetRect(pFormFillEnv, pData->sFieldName, pData->nControlIndex, + pData->rect); + break; + case FP_VALUE: + Field::SetValue(pFormFillEnv, pData->sFieldName, pData->nControlIndex, + pData->widestringarray); + break; + default: + NOTREACHED(); + } +} diff --git a/fxjs/cjs_field.h b/fxjs/cjs_field.h new file mode 100644 index 0000000000..8116e073cf --- /dev/null +++ b/fxjs/cjs_field.h @@ -0,0 +1,438 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_FIELD_H_ +#define FXJS_CJS_FIELD_H_ + +#include <string> +#include <vector> + +#include "fxjs/JS_Define.h" + +class CPDF_FormControl; +class CPDFSDK_Widget; +class Document; +struct CJS_DelayData; + +enum FIELD_PROP { + FP_BORDERSTYLE, + FP_CURRENTVALUEINDICES, + FP_DISPLAY, + FP_HIDDEN, + FP_LINEWIDTH, + FP_RECT, + FP_VALUE +}; + +class Field : public CJS_EmbedObj { + public: + static void DoDelay(CPDFSDK_FormFillEnvironment* pFormFillEnv, + CJS_DelayData* pData); + + explicit Field(CJS_Object* pJSObject); + ~Field() override; + + CJS_Return get_alignment(CJS_Runtime* pRuntime); + CJS_Return set_alignment(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_border_style(CJS_Runtime* pRuntime); + CJS_Return set_border_style(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_button_align_x(CJS_Runtime* pRuntime); + CJS_Return set_button_align_x(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_button_align_y(CJS_Runtime* pRuntime); + CJS_Return set_button_align_y(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_button_fit_bounds(CJS_Runtime* pRuntime); + CJS_Return set_button_fit_bounds(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_button_position(CJS_Runtime* pRuntime); + CJS_Return set_button_position(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_button_scale_how(CJS_Runtime* pRuntime); + CJS_Return set_button_scale_how(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_button_scale_when(CJS_Runtime* pRuntime); + CJS_Return set_button_scale_when(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_calc_order_index(CJS_Runtime* pRuntime); + CJS_Return set_calc_order_index(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_char_limit(CJS_Runtime* pRuntime); + CJS_Return set_char_limit(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_comb(CJS_Runtime* pRuntime); + CJS_Return set_comb(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_commit_on_sel_change(CJS_Runtime* pRuntime); + CJS_Return set_commit_on_sel_change(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_current_value_indices(CJS_Runtime* pRuntime); + CJS_Return set_current_value_indices(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_default_style(CJS_Runtime* pRuntime); + CJS_Return set_default_style(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_default_value(CJS_Runtime* pRuntime); + CJS_Return set_default_value(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_do_not_scroll(CJS_Runtime* pRuntime); + CJS_Return set_do_not_scroll(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_do_not_spell_check(CJS_Runtime* pRuntime); + CJS_Return set_do_not_spell_check(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_delay(CJS_Runtime* pRuntime); + CJS_Return set_delay(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_display(CJS_Runtime* pRuntime); + CJS_Return set_display(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_doc(CJS_Runtime* pRuntime); + CJS_Return set_doc(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_editable(CJS_Runtime* pRuntime); + CJS_Return set_editable(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_export_values(CJS_Runtime* pRuntime); + CJS_Return set_export_values(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_file_select(CJS_Runtime* pRuntime); + CJS_Return set_file_select(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_fill_color(CJS_Runtime* pRuntime); + CJS_Return set_fill_color(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_hidden(CJS_Runtime* pRuntime); + CJS_Return set_hidden(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_highlight(CJS_Runtime* pRuntime); + CJS_Return set_highlight(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_line_width(CJS_Runtime* pRuntime); + CJS_Return set_line_width(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_multiline(CJS_Runtime* pRuntime); + CJS_Return set_multiline(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_multiple_selection(CJS_Runtime* pRuntime); + CJS_Return set_multiple_selection(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_name(CJS_Runtime* pRuntime); + CJS_Return set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_num_items(CJS_Runtime* pRuntime); + CJS_Return set_num_items(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_page(CJS_Runtime* pRuntime); + CJS_Return set_page(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_password(CJS_Runtime* pRuntime); + CJS_Return set_password(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_print(CJS_Runtime* pRuntime); + CJS_Return set_print(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_radios_in_unison(CJS_Runtime* pRuntime); + CJS_Return set_radios_in_unison(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_readonly(CJS_Runtime* pRuntime); + CJS_Return set_readonly(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_rect(CJS_Runtime* pRuntime); + CJS_Return set_rect(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_required(CJS_Runtime* pRuntime); + CJS_Return set_required(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_rich_text(CJS_Runtime* pRuntime); + CJS_Return set_rich_text(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_rich_value(CJS_Runtime* pRuntime); + CJS_Return set_rich_value(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_rotation(CJS_Runtime* pRuntime); + CJS_Return set_rotation(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_stroke_color(CJS_Runtime* pRuntime); + CJS_Return set_stroke_color(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_style(CJS_Runtime* pRuntime); + CJS_Return set_style(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_submit_name(CJS_Runtime* pRuntime); + CJS_Return set_submit_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_text_color(CJS_Runtime* pRuntime); + CJS_Return set_text_color(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_text_font(CJS_Runtime* pRuntime); + CJS_Return set_text_font(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_text_size(CJS_Runtime* pRuntime); + CJS_Return set_text_size(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_type(CJS_Runtime* pRuntime); + CJS_Return set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_user_name(CJS_Runtime* pRuntime); + CJS_Return set_user_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_value(CJS_Runtime* pRuntime); + CJS_Return set_value(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return get_value_as_string(CJS_Runtime* pRuntime); + CJS_Return set_value_as_string(CJS_Runtime* pRuntime, + v8::Local<v8::Value> vp); + + CJS_Return get_source(CJS_Runtime* pRuntime); + CJS_Return set_source(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + CJS_Return browseForFileToSubmit( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return buttonGetCaption(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return buttonGetIcon(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return buttonImportIcon(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return buttonSetCaption(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return buttonSetIcon(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return checkThisBox(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return clearItems(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return defaultIsChecked(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return deleteItemAt(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getArray(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getItemAt(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return getLock(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return insertItemAt(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return isBoxChecked(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return isDefaultChecked(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return setAction(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return setFocus(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return setItems(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return setLock(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return signatureGetModifications( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return signatureGetSeedValue( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return signatureInfo(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return signatureSetSeedValue( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return signatureSign(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return signatureValidate(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + + bool AttachField(Document* pDocument, const WideString& csFieldName); + + private: + static void SetBorderStyle(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + const ByteString& string); + static void SetCurrentValueIndices(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + const std::vector<uint32_t>& array); + static void SetDisplay(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + int number); + static void SetHidden(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + bool b); + static void SetLineWidth(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + int number); + static void SetMultiline(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + bool b); + static void SetRect(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + const CFX_FloatRect& rect); + static void SetValue(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& swFieldName, + int nControlIndex, + const std::vector<WideString>& strArray); + + static void UpdateFormField(CPDFSDK_FormFillEnvironment* pFormFillEnv, + CPDF_FormField* pFormField, + bool bChangeMark, + bool bResetAP, + bool bRefresh); + static void UpdateFormControl(CPDFSDK_FormFillEnvironment* pFormFillEnv, + CPDF_FormControl* pFormControl, + bool bChangeMark, + bool bResetAP, + bool bRefresh); + + static CPDFSDK_Widget* GetWidget(CPDFSDK_FormFillEnvironment* pFormFillEnv, + CPDF_FormControl* pFormControl); + static std::vector<CPDF_FormField*> GetFormFields( + CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& csFieldName); + + void SetDelay(bool bDelay); + void ParseFieldName(const std::wstring& strFieldNameParsed, + std::wstring& strFieldName, + int& iControlNo); + std::vector<CPDF_FormField*> GetFormFields( + const WideString& csFieldName) const; + CPDF_FormControl* GetSmartFieldControl(CPDF_FormField* pFormField); + bool ValueIsOccur(CPDF_FormField* pFormField, WideString csOptLabel); + + void AddDelay_Int(FIELD_PROP prop, int32_t n); + void AddDelay_Bool(FIELD_PROP prop, bool b); + void AddDelay_String(FIELD_PROP prop, const ByteString& string); + void AddDelay_Rect(FIELD_PROP prop, const CFX_FloatRect& rect); + void AddDelay_WordArray(FIELD_PROP prop, const std::vector<uint32_t>& array); + void AddDelay_WideStringArray(FIELD_PROP prop, + const std::vector<WideString>& array); + + void DoDelay(); + + Document* m_pJSDoc; + CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv; + WideString m_FieldName; + int m_nFormControlIndex; + bool m_bCanSet; + bool m_bDelay; +}; + +class CJS_Field : public CJS_Object { + public: + static int GetObjDefnID(); + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Field(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Field() override {} + + void InitInstance(IJS_Runtime* pIRuntime) override; + + JS_STATIC_PROP(alignment, alignment, Field); + JS_STATIC_PROP(borderStyle, border_style, Field); + JS_STATIC_PROP(buttonAlignX, button_align_x, Field); + JS_STATIC_PROP(buttonAlignY, button_align_y, Field); + JS_STATIC_PROP(buttonFitBounds, button_fit_bounds, Field); + JS_STATIC_PROP(buttonPosition, button_position, Field); + JS_STATIC_PROP(buttonScaleHow, button_scale_how, Field); + JS_STATIC_PROP(ButtonScaleWhen, button_scale_when, Field); + JS_STATIC_PROP(calcOrderIndex, calc_order_index, Field); + JS_STATIC_PROP(charLimit, char_limit, Field); + JS_STATIC_PROP(comb, comb, Field); + JS_STATIC_PROP(commitOnSelChange, commit_on_sel_change, Field); + JS_STATIC_PROP(currentValueIndices, current_value_indices, Field); + JS_STATIC_PROP(defaultStyle, default_style, Field); + JS_STATIC_PROP(defaultValue, default_value, Field); + JS_STATIC_PROP(doNotScroll, do_not_scroll, Field); + JS_STATIC_PROP(doNotSpellCheck, do_not_spell_check, Field); + JS_STATIC_PROP(delay, delay, Field); + JS_STATIC_PROP(display, display, Field); + JS_STATIC_PROP(doc, doc, Field); + JS_STATIC_PROP(editable, editable, Field); + JS_STATIC_PROP(exportValues, export_values, Field); + JS_STATIC_PROP(fileSelect, file_select, Field); + JS_STATIC_PROP(fillColor, fill_color, Field); + JS_STATIC_PROP(hidden, hidden, Field); + JS_STATIC_PROP(highlight, highlight, Field); + JS_STATIC_PROP(lineWidth, line_width, Field); + JS_STATIC_PROP(multiline, multiline, Field); + JS_STATIC_PROP(multipleSelection, multiple_selection, Field); + JS_STATIC_PROP(name, name, Field); + JS_STATIC_PROP(numItems, num_items, Field); + JS_STATIC_PROP(page, page, Field); + JS_STATIC_PROP(password, password, Field); + JS_STATIC_PROP(print, print, Field); + JS_STATIC_PROP(radiosInUnison, radios_in_unison, Field); + JS_STATIC_PROP(readonly, readonly, Field); + JS_STATIC_PROP(rect, rect, Field); + JS_STATIC_PROP(required, required, Field); + JS_STATIC_PROP(richText, rich_text, Field); + JS_STATIC_PROP(richValue, rich_value, Field); + JS_STATIC_PROP(rotation, rotation, Field); + JS_STATIC_PROP(strokeColor, stroke_color, Field); + JS_STATIC_PROP(style, style, Field); + JS_STATIC_PROP(submitName, submit_name, Field); + JS_STATIC_PROP(textColor, text_color, Field); + JS_STATIC_PROP(textFont, text_font, Field); + JS_STATIC_PROP(textSize, text_size, Field); + JS_STATIC_PROP(type, type, Field); + JS_STATIC_PROP(userName, user_name, Field); + JS_STATIC_PROP(value, value, Field); + JS_STATIC_PROP(valueAsString, value_as_string, Field); + JS_STATIC_PROP(source, source, Field); + + JS_STATIC_METHOD(browseForFileToSubmit, Field); + JS_STATIC_METHOD(buttonGetCaption, Field); + JS_STATIC_METHOD(buttonGetIcon, Field); + JS_STATIC_METHOD(buttonImportIcon, Field); + JS_STATIC_METHOD(buttonSetCaption, Field); + JS_STATIC_METHOD(buttonSetIcon, Field); + JS_STATIC_METHOD(checkThisBox, Field); + JS_STATIC_METHOD(clearItems, Field); + JS_STATIC_METHOD(defaultIsChecked, Field); + JS_STATIC_METHOD(deleteItemAt, Field); + JS_STATIC_METHOD(getArray, Field); + JS_STATIC_METHOD(getItemAt, Field); + JS_STATIC_METHOD(getLock, Field); + JS_STATIC_METHOD(insertItemAt, Field); + JS_STATIC_METHOD(isBoxChecked, Field); + JS_STATIC_METHOD(isDefaultChecked, Field); + JS_STATIC_METHOD(setAction, Field); + JS_STATIC_METHOD(setFocus, Field); + JS_STATIC_METHOD(setItems, Field); + JS_STATIC_METHOD(setLock, Field); + JS_STATIC_METHOD(signatureGetModifications, Field); + JS_STATIC_METHOD(signatureGetSeedValue, Field); + JS_STATIC_METHOD(signatureInfo, Field); + JS_STATIC_METHOD(signatureSetSeedValue, Field); + JS_STATIC_METHOD(signatureSign, Field); + JS_STATIC_METHOD(signatureValidate, Field); + + private: + static int ObjDefnID; + static const JSPropertySpec PropertySpecs[]; + static const JSMethodSpec MethodSpecs[]; +}; + +#endif // FXJS_CJS_FIELD_H_ diff --git a/fxjs/cjs_font.cpp b/fxjs/cjs_font.cpp new file mode 100644 index 0000000000..b4ef66b869 --- /dev/null +++ b/fxjs/cjs_font.cpp @@ -0,0 +1,32 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_font.h" + +const JSConstSpec CJS_Font::ConstSpecs[] = { + {"Times", JSConstSpec::String, 0, "Times-Roman"}, + {"TimesB", JSConstSpec::String, 0, "Times-Bold"}, + {"TimesI", JSConstSpec::String, 0, "Times-Italic"}, + {"TimesBI", JSConstSpec::String, 0, "Times-BoldItalic"}, + {"Helv", JSConstSpec::String, 0, "Helvetica"}, + {"HelvB", JSConstSpec::String, 0, "Helvetica-Bold"}, + {"HelvI", JSConstSpec::String, 0, "Helvetica-Oblique"}, + {"HelvBI", JSConstSpec::String, 0, "Helvetica-BoldOblique"}, + {"Cour", JSConstSpec::String, 0, "Courier"}, + {"CourB", JSConstSpec::String, 0, "Courier-Bold"}, + {"CourI", JSConstSpec::String, 0, "Courier-Oblique"}, + {"CourBI", JSConstSpec::String, 0, "Courier-BoldOblique"}, + {"Symbol", JSConstSpec::String, 0, "Symbol"}, + {"ZapfD", JSConstSpec::String, 0, "ZapfDingbats"}, + {0, JSConstSpec::Number, 0, 0}}; + +int CJS_Font::ObjDefnID = -1; + +// static +void CJS_Font::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = pEngine->DefineObj("font", FXJSOBJTYPE_STATIC, nullptr, nullptr); + DefineConsts(pEngine, ObjDefnID, ConstSpecs); +} diff --git a/fxjs/cjs_font.h b/fxjs/cjs_font.h new file mode 100644 index 0000000000..31edf06836 --- /dev/null +++ b/fxjs/cjs_font.h @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_FONT_H_ +#define FXJS_CJS_FONT_H_ + +#include "fxjs/JS_Define.h" + +class CJS_Font : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Font(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Font() override {} + + private: + static int ObjDefnID; + static const JSConstSpec ConstSpecs[]; +}; + +#endif // FXJS_CJS_FONT_H_ diff --git a/fxjs/cjs_global.cpp b/fxjs/cjs_global.cpp new file mode 100644 index 0000000000..c4a8edcf32 --- /dev/null +++ b/fxjs/cjs_global.cpp @@ -0,0 +1,614 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_global.h" + +#include <map> +#include <memory> +#include <utility> +#include <vector> + +#include "core/fxcrt/fx_extension.h" +#include "fxjs/JS_Define.h" +#include "fxjs/JS_GlobalData.h" +#include "fxjs/JS_KeyValue.h" +#include "fxjs/cjs_event_context.h" +#include "fxjs/cjs_eventhandler.h" +#include "fxjs/cjs_object.h" +#include "fxjs/js_resources.h" + +namespace { + +WideString PropFromV8Prop(v8::Local<v8::String> property) { + v8::String::Utf8Value utf8_value(property); + return WideString::FromUTF8(ByteStringView(*utf8_value, utf8_value.length())); +} + +template <class Alt> +void JSSpecialPropQuery(const char*, + v8::Local<v8::String> property, + const v8::PropertyCallbackInfo<v8::Integer>& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); + CJS_Return result = pObj->QueryProperty(PropFromV8Prop(property).c_str()); + info.GetReturnValue().Set(!result.HasError() ? 4 : 0); +} + +template <class Alt> +void JSSpecialPropGet(const char* class_name, + v8::Local<v8::String> property, + const v8::PropertyCallbackInfo<v8::Value>& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); + CJS_Return result = + pObj->GetProperty(pRuntime, PropFromV8Prop(property).c_str()); + if (result.HasError()) { + pRuntime->Error( + JSFormatErrorString(class_name, "GetProperty", result.Error())); + return; + } + + if (result.HasReturn()) + info.GetReturnValue().Set(result.Return()); +} + +template <class Alt> +void JSSpecialPropPut(const char* class_name, + v8::Local<v8::String> property, + v8::Local<v8::Value> value, + const v8::PropertyCallbackInfo<v8::Value>& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); + CJS_Return result = + pObj->SetProperty(pRuntime, PropFromV8Prop(property).c_str(), value); + if (result.HasError()) { + pRuntime->Error( + JSFormatErrorString(class_name, "PutProperty", result.Error())); + } +} + +template <class Alt> +void JSSpecialPropDel(const char* class_name, + v8::Local<v8::String> property, + const v8::PropertyCallbackInfo<v8::Boolean>& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); + CJS_Return result = + pObj->DelProperty(pRuntime, PropFromV8Prop(property).c_str()); + if (result.HasError()) { + // TODO(dsinclair): Should this set the pRuntime->Error result? + // ByteString cbName; + // cbName.Format("%s.%s", class_name, "DelProperty"); + } +} + +struct JSGlobalData { + JSGlobalData(); + ~JSGlobalData(); + + JS_GlobalDataType nType; + double dData; + bool bData; + ByteString sData; + v8::Global<v8::Object> pData; + bool bPersistent; + bool bDeleted; +}; + +class JSGlobalAlternate : public CJS_EmbedObj { + public: + explicit JSGlobalAlternate(CJS_Object* pJSObject); + ~JSGlobalAlternate() override; + + CJS_Return setPersistent(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return QueryProperty(const wchar_t* propname); + CJS_Return GetProperty(CJS_Runtime* pRuntime, const wchar_t* propname); + CJS_Return SetProperty(CJS_Runtime* pRuntime, + const wchar_t* propname, + v8::Local<v8::Value> vp); + CJS_Return DelProperty(CJS_Runtime* pRuntime, const wchar_t* propname); + void Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv); + + private: + void UpdateGlobalPersistentVariables(); + void CommitGlobalPersisitentVariables(CJS_Runtime* pRuntime); + void DestroyGlobalPersisitentVariables(); + CJS_Return SetGlobalVariables(const ByteString& propname, + JS_GlobalDataType nType, + double dData, + bool bData, + const ByteString& sData, + v8::Local<v8::Object> pData, + bool bDefaultPersistent); + void ObjectToArray(CJS_Runtime* pRuntime, + v8::Local<v8::Object> pObj, + CJS_GlobalVariableArray& array); + void PutObjectProperty(v8::Local<v8::Object> obj, CJS_KeyValue* pData); + + std::map<ByteString, std::unique_ptr<JSGlobalData>> m_MapGlobal; + WideString m_sFilePath; + CJS_GlobalData* m_pGlobalData; + CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv; +}; + +} // namespace + +const JSMethodSpec CJS_Global::MethodSpecs[] = { + {"setPersistent", setPersistent_static}, + {0, 0}}; + +int CJS_Global::ObjDefnID = -1; + +// static +void CJS_Global::setPersistent_static( + const v8::FunctionCallbackInfo<v8::Value>& info) { + JSMethod<JSGlobalAlternate, &JSGlobalAlternate::setPersistent>( + "setPersistent", "global", info); +} + +// static +void CJS_Global::queryprop_static( + v8::Local<v8::String> property, + const v8::PropertyCallbackInfo<v8::Integer>& info) { + JSSpecialPropQuery<JSGlobalAlternate>("global", property, info); +} + +// static +void CJS_Global::getprop_static( + v8::Local<v8::String> property, + const v8::PropertyCallbackInfo<v8::Value>& info) { + JSSpecialPropGet<JSGlobalAlternate>("global", property, info); +} + +// static +void CJS_Global::putprop_static( + v8::Local<v8::String> property, + v8::Local<v8::Value> value, + const v8::PropertyCallbackInfo<v8::Value>& info) { + JSSpecialPropPut<JSGlobalAlternate>("global", property, value, info); +} + +// static +void CJS_Global::delprop_static( + v8::Local<v8::String> property, + const v8::PropertyCallbackInfo<v8::Boolean>& info) { + JSSpecialPropDel<JSGlobalAlternate>("global", property, info); +} + +// static +void CJS_Global::DefineAllProperties(CFXJS_Engine* pEngine) { + pEngine->DefineObjAllProperties( + ObjDefnID, CJS_Global::queryprop_static, CJS_Global::getprop_static, + CJS_Global::putprop_static, CJS_Global::delprop_static); +} + +// static +void CJS_Global::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = pEngine->DefineObj("global", FXJSOBJTYPE_STATIC, + JSConstructor<CJS_Global, JSGlobalAlternate>, + JSDestructor<CJS_Global>); + DefineMethods(pEngine, ObjDefnID, MethodSpecs); + DefineAllProperties(pEngine); +} + +void CJS_Global::InitInstance(IJS_Runtime* pIRuntime) { + CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime); + JSGlobalAlternate* pGlobal = + static_cast<JSGlobalAlternate*>(GetEmbedObject()); + pGlobal->Initial(pRuntime->GetFormFillEnv()); +} + +JSGlobalData::JSGlobalData() + : nType(JS_GlobalDataType::NUMBER), + dData(0), + bData(false), + sData(""), + bPersistent(false), + bDeleted(false) {} + +JSGlobalData::~JSGlobalData() { + pData.Reset(); +} + +JSGlobalAlternate::JSGlobalAlternate(CJS_Object* pJSObject) + : CJS_EmbedObj(pJSObject), m_pFormFillEnv(nullptr) {} + +JSGlobalAlternate::~JSGlobalAlternate() { + DestroyGlobalPersisitentVariables(); + m_pGlobalData->Release(); +} + +void JSGlobalAlternate::Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pFormFillEnv.Reset(pFormFillEnv); + m_pGlobalData = CJS_GlobalData::GetRetainedInstance(pFormFillEnv); + UpdateGlobalPersistentVariables(); +} + +CJS_Return JSGlobalAlternate::QueryProperty(const wchar_t* propname) { + return CJS_Return(WideString(propname) != L"setPersistent"); +} + +CJS_Return JSGlobalAlternate::DelProperty(CJS_Runtime* pRuntime, + const wchar_t* propname) { + auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); + if (it == m_MapGlobal.end()) + return CJS_Return(false); + + it->second->bDeleted = true; + return CJS_Return(true); +} + +CJS_Return JSGlobalAlternate::GetProperty(CJS_Runtime* pRuntime, + const wchar_t* propname) { + auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); + if (it == m_MapGlobal.end()) + return CJS_Return(true); + + JSGlobalData* pData = it->second.get(); + if (pData->bDeleted) + return CJS_Return(true); + + switch (pData->nType) { + case JS_GlobalDataType::NUMBER: + return CJS_Return(pRuntime->NewNumber(pData->dData)); + case JS_GlobalDataType::BOOLEAN: + return CJS_Return(pRuntime->NewBoolean(pData->bData)); + case JS_GlobalDataType::STRING: + return CJS_Return(pRuntime->NewString( + WideString::FromLocal(pData->sData.c_str()).c_str())); + case JS_GlobalDataType::OBJECT: + return CJS_Return( + v8::Local<v8::Object>::New(pRuntime->GetIsolate(), pData->pData)); + case JS_GlobalDataType::NULLOBJ: + return CJS_Return(pRuntime->NewNull()); + default: + break; + } + return CJS_Return(false); +} + +CJS_Return JSGlobalAlternate::SetProperty(CJS_Runtime* pRuntime, + const wchar_t* propname, + v8::Local<v8::Value> vp) { + ByteString sPropName = ByteString::FromUnicode(propname); + if (vp->IsNumber()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, + pRuntime->ToDouble(vp), false, "", + v8::Local<v8::Object>(), false); + } + if (vp->IsBoolean()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::BOOLEAN, 0, + pRuntime->ToBoolean(vp), "", + v8::Local<v8::Object>(), false); + } + if (vp->IsString()) { + return SetGlobalVariables( + sPropName, JS_GlobalDataType::STRING, 0, false, + ByteString::FromUnicode(pRuntime->ToWideString(vp)), + v8::Local<v8::Object>(), false); + } + if (vp->IsObject()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, false, + "", pRuntime->ToObject(vp), false); + } + if (vp->IsNull()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::NULLOBJ, 0, false, + "", v8::Local<v8::Object>(), false); + } + if (vp->IsUndefined()) { + DelProperty(pRuntime, propname); + return CJS_Return(true); + } + return CJS_Return(false); +} + +CJS_Return JSGlobalAlternate::setPersistent( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 2) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + auto it = m_MapGlobal.find( + ByteString::FromUnicode(pRuntime->ToWideString(params[0]))); + if (it == m_MapGlobal.end() || it->second->bDeleted) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOGLOBAL)); + + it->second->bPersistent = pRuntime->ToBoolean(params[1]); + return CJS_Return(true); +} + +void JSGlobalAlternate::UpdateGlobalPersistentVariables() { + CJS_Runtime* pRuntime = + static_cast<CJS_Runtime*>(CFXJS_Engine::CurrentEngineFromIsolate( + m_pJSObject->ToV8Object()->GetIsolate())); + + for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) { + CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i); + switch (pData->data.nType) { + case JS_GlobalDataType::NUMBER: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NUMBER, + pData->data.dData, false, "", + v8::Local<v8::Object>(), pData->bPersistent == 1); + pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), + pData->data.sKey.UTF8Decode(), + pRuntime->NewNumber(pData->data.dData)); + break; + case JS_GlobalDataType::BOOLEAN: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::BOOLEAN, 0, + pData->data.bData == 1, "", v8::Local<v8::Object>(), + pData->bPersistent == 1); + pRuntime->PutObjectProperty( + m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), + pRuntime->NewBoolean(pData->data.bData == 1)); + break; + case JS_GlobalDataType::STRING: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::STRING, 0, + false, pData->data.sData, v8::Local<v8::Object>(), + pData->bPersistent == 1); + pRuntime->PutObjectProperty( + m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), + pRuntime->NewString(pData->data.sData.UTF8Decode().AsStringView())); + break; + case JS_GlobalDataType::OBJECT: { + v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1); + if (!pObj.IsEmpty()) { + PutObjectProperty(pObj, &pData->data); + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::OBJECT, 0, + false, "", pObj, pData->bPersistent == 1); + pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), + pData->data.sKey.UTF8Decode(), pObj); + } + } break; + case JS_GlobalDataType::NULLOBJ: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NULLOBJ, 0, + false, "", v8::Local<v8::Object>(), + pData->bPersistent == 1); + pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), + pData->data.sKey.UTF8Decode(), + pRuntime->NewNull()); + break; + } + } +} + +void JSGlobalAlternate::CommitGlobalPersisitentVariables( + CJS_Runtime* pRuntime) { + for (const auto& iter : m_MapGlobal) { + ByteString name = iter.first; + JSGlobalData* pData = iter.second.get(); + if (pData->bDeleted) { + m_pGlobalData->DeleteGlobalVariable(name); + continue; + } + switch (pData->nType) { + case JS_GlobalDataType::NUMBER: + m_pGlobalData->SetGlobalVariableNumber(name, pData->dData); + m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); + break; + case JS_GlobalDataType::BOOLEAN: + m_pGlobalData->SetGlobalVariableBoolean(name, pData->bData); + m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); + break; + case JS_GlobalDataType::STRING: + m_pGlobalData->SetGlobalVariableString(name, pData->sData); + m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); + break; + case JS_GlobalDataType::OBJECT: { + CJS_GlobalVariableArray array; + v8::Local<v8::Object> obj = v8::Local<v8::Object>::New( + GetJSObject()->GetIsolate(), pData->pData); + ObjectToArray(pRuntime, obj, array); + m_pGlobalData->SetGlobalVariableObject(name, array); + m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); + } break; + case JS_GlobalDataType::NULLOBJ: + m_pGlobalData->SetGlobalVariableNull(name); + m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); + break; + } + } +} + +void JSGlobalAlternate::ObjectToArray(CJS_Runtime* pRuntime, + v8::Local<v8::Object> pObj, + CJS_GlobalVariableArray& array) { + std::vector<WideString> pKeyList = pRuntime->GetObjectPropertyNames(pObj); + for (const auto& ws : pKeyList) { + ByteString sKey = ws.UTF8Encode(); + v8::Local<v8::Value> v = pRuntime->GetObjectProperty(pObj, ws); + if (v->IsNumber()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::NUMBER; + pObjElement->sKey = sKey; + pObjElement->dData = pRuntime->ToDouble(v); + array.Add(pObjElement); + continue; + } + if (v->IsBoolean()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::BOOLEAN; + pObjElement->sKey = sKey; + pObjElement->dData = pRuntime->ToBoolean(v); + array.Add(pObjElement); + continue; + } + if (v->IsString()) { + ByteString sValue = ByteString::FromUnicode(pRuntime->ToWideString(v)); + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::STRING; + pObjElement->sKey = sKey; + pObjElement->sData = sValue; + array.Add(pObjElement); + continue; + } + if (v->IsObject()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::OBJECT; + pObjElement->sKey = sKey; + ObjectToArray(pRuntime, pRuntime->ToObject(v), pObjElement->objData); + array.Add(pObjElement); + continue; + } + if (v->IsNull()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::NULLOBJ; + pObjElement->sKey = sKey; + array.Add(pObjElement); + } + } +} + +void JSGlobalAlternate::PutObjectProperty(v8::Local<v8::Object> pObj, + CJS_KeyValue* pData) { + CJS_Runtime* pRuntime = CJS_Runtime::CurrentRuntimeFromIsolate( + m_pJSObject->ToV8Object()->GetIsolate()); + + for (int i = 0, sz = pData->objData.Count(); i < sz; i++) { + CJS_KeyValue* pObjData = pData->objData.GetAt(i); + switch (pObjData->nType) { + case JS_GlobalDataType::NUMBER: + pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), + pRuntime->NewNumber(pObjData->dData)); + break; + case JS_GlobalDataType::BOOLEAN: + pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), + pRuntime->NewBoolean(pObjData->bData == 1)); + break; + case JS_GlobalDataType::STRING: + pRuntime->PutObjectProperty( + pObj, pObjData->sKey.UTF8Decode(), + pRuntime->NewString(pObjData->sData.UTF8Decode().AsStringView())); + break; + case JS_GlobalDataType::OBJECT: { + v8::Local<v8::Object> pNewObj = pRuntime->NewFxDynamicObj(-1); + if (!pNewObj.IsEmpty()) { + PutObjectProperty(pNewObj, pObjData); + pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), + pNewObj); + } + } break; + case JS_GlobalDataType::NULLOBJ: + pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), + pRuntime->NewNull()); + break; + } + } +} + +void JSGlobalAlternate::DestroyGlobalPersisitentVariables() { + m_MapGlobal.clear(); +} + +CJS_Return JSGlobalAlternate::SetGlobalVariables(const ByteString& propname, + JS_GlobalDataType nType, + double dData, + bool bData, + const ByteString& sData, + v8::Local<v8::Object> pData, + bool bDefaultPersistent) { + if (propname.IsEmpty()) + return CJS_Return(false); + + auto it = m_MapGlobal.find(propname); + if (it != m_MapGlobal.end()) { + JSGlobalData* pTemp = it->second.get(); + if (pTemp->bDeleted || pTemp->nType != nType) { + pTemp->dData = 0; + pTemp->bData = 0; + pTemp->sData = ""; + pTemp->nType = nType; + } + pTemp->bDeleted = false; + switch (nType) { + case JS_GlobalDataType::NUMBER: + pTemp->dData = dData; + break; + case JS_GlobalDataType::BOOLEAN: + pTemp->bData = bData; + break; + case JS_GlobalDataType::STRING: + pTemp->sData = sData; + break; + case JS_GlobalDataType::OBJECT: + pTemp->pData.Reset(pData->GetIsolate(), pData); + break; + case JS_GlobalDataType::NULLOBJ: + break; + default: + return CJS_Return(false); + } + return CJS_Return(true); + } + + auto pNewData = pdfium::MakeUnique<JSGlobalData>(); + switch (nType) { + case JS_GlobalDataType::NUMBER: + pNewData->nType = JS_GlobalDataType::NUMBER; + pNewData->dData = dData; + pNewData->bPersistent = bDefaultPersistent; + break; + case JS_GlobalDataType::BOOLEAN: + pNewData->nType = JS_GlobalDataType::BOOLEAN; + pNewData->bData = bData; + pNewData->bPersistent = bDefaultPersistent; + break; + case JS_GlobalDataType::STRING: + pNewData->nType = JS_GlobalDataType::STRING; + pNewData->sData = sData; + pNewData->bPersistent = bDefaultPersistent; + break; + case JS_GlobalDataType::OBJECT: + pNewData->nType = JS_GlobalDataType::OBJECT; + pNewData->pData.Reset(pData->GetIsolate(), pData); + pNewData->bPersistent = bDefaultPersistent; + break; + case JS_GlobalDataType::NULLOBJ: + pNewData->nType = JS_GlobalDataType::NULLOBJ; + pNewData->bPersistent = bDefaultPersistent; + break; + default: + return CJS_Return(false); + } + m_MapGlobal[propname] = std::move(pNewData); + return CJS_Return(true); +} diff --git a/fxjs/cjs_global.h b/fxjs/cjs_global.h new file mode 100644 index 0000000000..203d6e969d --- /dev/null +++ b/fxjs/cjs_global.h @@ -0,0 +1,42 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_GLOBAL_H_ +#define FXJS_CJS_GLOBAL_H_ + +#include "fxjs/JS_Define.h" + +class CJS_Global : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + static void DefineAllProperties(CFXJS_Engine* pEngine); + + static void queryprop_static( + v8::Local<v8::String> property, + const v8::PropertyCallbackInfo<v8::Integer>& info); + static void getprop_static(v8::Local<v8::String> property, + const v8::PropertyCallbackInfo<v8::Value>& info); + static void putprop_static(v8::Local<v8::String> property, + v8::Local<v8::Value> value, + const v8::PropertyCallbackInfo<v8::Value>& info); + static void delprop_static(v8::Local<v8::String> property, + const v8::PropertyCallbackInfo<v8::Boolean>& info); + + static void setPersistent_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + + explicit CJS_Global(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Global() override {} + + // CJS_Object + void InitInstance(IJS_Runtime* pIRuntime) override; + + private: + static int ObjDefnID; + static const JSMethodSpec MethodSpecs[]; +}; + +#endif // FXJS_CJS_GLOBAL_H_ diff --git a/fxjs/cjs_globalarrays.cpp b/fxjs/cjs_globalarrays.cpp new file mode 100644 index 0000000000..7857161205 --- /dev/null +++ b/fxjs/cjs_globalarrays.cpp @@ -0,0 +1,74 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_globalarrays.h" + +#define GLOBAL_ARRAY(rt, name, ...) \ + { \ + const wchar_t* values[] = {__VA_ARGS__}; \ + v8::Local<v8::Array> array = (rt)->NewArray(); \ + for (size_t i = 0; i < FX_ArraySize(values); ++i) \ + array->Set(i, (rt)->NewString(values[i])); \ + (rt)->SetConstArray((name), array); \ + (rt)->DefineGlobalConst( \ + (name), [](const v8::FunctionCallbackInfo<v8::Value>& info) { \ + CJS_Runtime* pCurrentRuntime = \ + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); \ + if (pCurrentRuntime) \ + info.GetReturnValue().Set(pCurrentRuntime->GetConstArray(name)); \ + }); \ + } + +// static +void CJS_GlobalArrays::DefineJSObjects(CJS_Runtime* pRuntime) { + GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_ENTRY_DOT_SEP", L"[+-]?\\d*\\.?\\d*"); + GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_COMMIT_DOT_SEP", + L"[+-]?\\d+(\\.\\d+)?", // -1.0 or -1 + L"[+-]?\\.\\d+", // -.1 + L"[+-]?\\d+\\."); // -1. + + GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_ENTRY_COMMA_SEP", L"[+-]?\\d*,?\\d*"); + GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_COMMIT_COMMA_SEP", + L"[+-]?\\d+([.,]\\d+)?", // -1,0 or -1 + L"[+-]?[.,]\\d+", // -,1 + L"[+-]?\\d+[.,]"); // -1, + + GLOBAL_ARRAY(pRuntime, L"RE_ZIP_ENTRY", L"\\d{0,5}"); + GLOBAL_ARRAY(pRuntime, L"RE_ZIP_COMMIT", L"\\d{5}"); + GLOBAL_ARRAY(pRuntime, L"RE_ZIP4_ENTRY", L"\\d{0,5}(\\.|[- ])?\\d{0,4}"); + GLOBAL_ARRAY(pRuntime, L"RE_ZIP4_COMMIT", L"\\d{5}(\\.|[- ])?\\d{4}"); + GLOBAL_ARRAY(pRuntime, L"RE_PHONE_ENTRY", + // 555-1234 or 408 555-1234 + L"\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + + // (408 + L"\\(\\d{0,3}", + + // (408) 555-1234 + // (allow the addition of parens as an afterthought) + L"\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + + // (408 555-1234 + L"\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + + // 408) 555-1234 + L"\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + + // international + L"011(\\.|[- \\d])*"); + + GLOBAL_ARRAY( + pRuntime, L"RE_PHONE_COMMIT", L"\\d{3}(\\.|[- ])?\\d{4}", // 555-1234 + L"\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", // 408 555-1234 + L"\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", // (408) 555-1234 + L"011(\\.|[- \\d])*"); // international + + GLOBAL_ARRAY(pRuntime, L"RE_SSN_ENTRY", + L"\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}"); + + GLOBAL_ARRAY(pRuntime, L"RE_SSN_COMMIT", + L"\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}"); +} diff --git a/fxjs/cjs_globalarrays.h b/fxjs/cjs_globalarrays.h new file mode 100644 index 0000000000..297b97b614 --- /dev/null +++ b/fxjs/cjs_globalarrays.h @@ -0,0 +1,17 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_GLOBALARRAYS_H_ +#define FXJS_CJS_GLOBALARRAYS_H_ + +#include "fxjs/JS_Define.h" + +class CJS_GlobalArrays : public CJS_Object { + public: + static void DefineJSObjects(CJS_Runtime* pRuntmie); +}; + +#endif // FXJS_CJS_GLOBALARRAYS_H_ diff --git a/fxjs/cjs_globalconsts.cpp b/fxjs/cjs_globalconsts.cpp new file mode 100644 index 0000000000..598479780a --- /dev/null +++ b/fxjs/cjs_globalconsts.cpp @@ -0,0 +1,47 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_globalconsts.h" + +#define GLOBAL_STRING(rt, name, value) \ + (rt)->DefineGlobalConst( \ + (name), [](const v8::FunctionCallbackInfo<v8::Value>& info) { \ + info.GetReturnValue().Set( \ + CFXJS_Engine::CurrentEngineFromIsolate(info.GetIsolate()) \ + ->NewString(value)); \ + }) + +// static +void CJS_GlobalConsts::DefineJSObjects(CJS_Runtime* pRuntime) { + GLOBAL_STRING(pRuntime, L"IDS_GREATER_THAN", + L"Invalid value: must be greater than or equal to % s."); + + GLOBAL_STRING(pRuntime, L"IDS_GT_AND_LT", + L"Invalid value: must be greater than or equal to % s " + L"and less than or equal to % s."); + + GLOBAL_STRING(pRuntime, L"IDS_LESS_THAN", + L"Invalid value: must be less than or equal to % s."); + + GLOBAL_STRING(pRuntime, L"IDS_INVALID_MONTH", L"**Invalid**"); + GLOBAL_STRING( + pRuntime, L"IDS_INVALID_DATE", + L"Invalid date / time: please ensure that the date / time exists.Field"); + + GLOBAL_STRING(pRuntime, L"IDS_INVALID_VALUE", + L"The value entered does not match the format of the field"); + + GLOBAL_STRING(pRuntime, L"IDS_AM", L"am"); + GLOBAL_STRING(pRuntime, L"IDS_PM", L"pm"); + GLOBAL_STRING(pRuntime, L"IDS_MONTH_INFO", + L"January[1] February[2] March[3] April[4] May[5] " + L"June[6] July[7] August[8] September[9] October[10] " + L"November[11] December[12] Sept[9] Jan[1] Feb[2] Mar[3] " + L"Apr[4] Jun[6] Jul[7] Aug[8] Sep[9] Oct[10] Nov[11] " + L"Dec[12]"); + + GLOBAL_STRING(pRuntime, L"IDS_STARTUP_CONSOLE_MSG", L"** ^ _ ^ **"); +} diff --git a/fxjs/cjs_globalconsts.h b/fxjs/cjs_globalconsts.h new file mode 100644 index 0000000000..8c6618ee6f --- /dev/null +++ b/fxjs/cjs_globalconsts.h @@ -0,0 +1,17 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_GLOBALCONSTS_H_ +#define FXJS_CJS_GLOBALCONSTS_H_ + +#include "fxjs/JS_Define.h" + +class CJS_GlobalConsts : public CJS_Object { + public: + static void DefineJSObjects(CJS_Runtime* pRuntime); +}; + +#endif // FXJS_CJS_GLOBALCONSTS_H_ diff --git a/fxjs/cjs_highlight.cpp b/fxjs/cjs_highlight.cpp new file mode 100644 index 0000000000..453a2662fb --- /dev/null +++ b/fxjs/cjs_highlight.cpp @@ -0,0 +1,23 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_highlight.h" + +const JSConstSpec CJS_Highlight::ConstSpecs[] = { + {"n", JSConstSpec::String, 0, "none"}, + {"i", JSConstSpec::String, 0, "invert"}, + {"p", JSConstSpec::String, 0, "push"}, + {"o", JSConstSpec::String, 0, "outline"}, + {0, JSConstSpec::Number, 0, 0}}; + +int CJS_Highlight::ObjDefnID = -1; + +// static +void CJS_Highlight::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("highlight", FXJSOBJTYPE_STATIC, nullptr, nullptr); + DefineConsts(pEngine, ObjDefnID, ConstSpecs); +} diff --git a/fxjs/cjs_highlight.h b/fxjs/cjs_highlight.h new file mode 100644 index 0000000000..74091d341d --- /dev/null +++ b/fxjs/cjs_highlight.h @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_HIGHLIGHT_H_ +#define FXJS_CJS_HIGHLIGHT_H_ + +#include "fxjs/JS_Define.h" + +class CJS_Highlight : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Highlight(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Highlight() override {} + + private: + static int ObjDefnID; + static const JSConstSpec ConstSpecs[]; +}; + +#endif // FXJS_CJS_HIGHLIGHT_H_ diff --git a/fxjs/cjs_icon.cpp b/fxjs/cjs_icon.cpp new file mode 100644 index 0000000000..c4ef8e1684 --- /dev/null +++ b/fxjs/cjs_icon.cpp @@ -0,0 +1,39 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_icon.h" + +const JSPropertySpec CJS_Icon::PropertySpecs[] = { + {"name", get_name_static, set_name_static}, + {0, 0, 0}}; + +int CJS_Icon::ObjDefnID = -1; + +// static +int CJS_Icon::GetObjDefnID() { + return ObjDefnID; +} + +// static +void CJS_Icon::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("Icon", FXJSOBJTYPE_DYNAMIC, + JSConstructor<CJS_Icon, Icon>, JSDestructor<CJS_Icon>); + DefineProps(pEngine, ObjDefnID, PropertySpecs); +} + +Icon::Icon(CJS_Object* pJSObject) + : CJS_EmbedObj(pJSObject), m_swIconName(L"") {} + +Icon::~Icon() {} + +CJS_Return Icon::get_name(CJS_Runtime* pRuntime) { + return CJS_Return(pRuntime->NewString(m_swIconName.c_str())); +} + +CJS_Return Icon::set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) { + return CJS_Return(false); +} diff --git a/fxjs/cjs_icon.h b/fxjs/cjs_icon.h new file mode 100644 index 0000000000..05b84384b3 --- /dev/null +++ b/fxjs/cjs_icon.h @@ -0,0 +1,42 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_ICON_H_ +#define FXJS_CJS_ICON_H_ + +#include "fxjs/JS_Define.h" + +class Icon : public CJS_EmbedObj { + public: + explicit Icon(CJS_Object* pJSObject); + ~Icon() override; + + CJS_Return get_name(CJS_Runtime* pRuntime); + CJS_Return set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp); + + WideString GetIconName() const { return m_swIconName; } + void SetIconName(WideString name) { m_swIconName = name; } + + private: + WideString m_swIconName; +}; + +class CJS_Icon : public CJS_Object { + public: + static int GetObjDefnID(); + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Icon(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Icon() override {} + + JS_STATIC_PROP(name, name, Icon); + + private: + static int ObjDefnID; + static const JSPropertySpec PropertySpecs[]; +}; + +#endif // FXJS_CJS_ICON_H_ diff --git a/fxjs/cjs_object.cpp b/fxjs/cjs_object.cpp new file mode 100644 index 0000000000..ccddc7f8df --- /dev/null +++ b/fxjs/cjs_object.cpp @@ -0,0 +1,47 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_object.h" + +// static +void CJS_Object::DefineConsts(CFXJS_Engine* pEngine, + int objId, + const JSConstSpec consts[]) { + for (size_t i = 0; consts[i].pName != 0; ++i) { + pEngine->DefineObjConst( + objId, consts[i].pName, + consts[i].eType == JSConstSpec::Number + ? pEngine->NewNumber(consts[i].number).As<v8::Value>() + : pEngine->NewString(consts[i].pStr).As<v8::Value>()); + } +} + +// static +void CJS_Object::DefineProps(CFXJS_Engine* pEngine, + int objId, + const JSPropertySpec props[]) { + for (size_t i = 0; props[i].pName != 0; ++i) { + pEngine->DefineObjProperty(objId, props[i].pName, props[i].pPropGet, + props[i].pPropPut); + } +} + +// static +void CJS_Object::DefineMethods(CFXJS_Engine* pEngine, + int objId, + const JSMethodSpec methods[]) { + for (size_t i = 0; methods[i].pName != 0; ++i) + pEngine->DefineObjMethod(objId, methods[i].pName, methods[i].pMethodCall); +} + +CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) { + m_pIsolate = pObject->GetIsolate(); + m_pV8Object.Reset(m_pIsolate, pObject); +} + +CJS_Object::~CJS_Object() {} + +void CJS_Object::InitInstance(IJS_Runtime* pIRuntime) {} diff --git a/fxjs/cjs_object.h b/fxjs/cjs_object.h new file mode 100644 index 0000000000..bed5088f03 --- /dev/null +++ b/fxjs/cjs_object.h @@ -0,0 +1,68 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_OBJECT_H_ +#define FXJS_CJS_OBJECT_H_ + +#include <memory> + +#include "fpdfsdk/fsdk_define.h" +#include "fxjs/cjs_embedobj.h" +#include "fxjs/cjs_runtime.h" +#include "fxjs/fxjs_v8.h" + +struct JSConstSpec { + enum Type { Number = 0, String = 1 }; + + const char* pName; + Type eType; + double number; + const char* pStr; +}; + +struct JSPropertySpec { + const char* pName; + v8::AccessorGetterCallback pPropGet; + v8::AccessorSetterCallback pPropPut; +}; + +struct JSMethodSpec { + const char* pName; + v8::FunctionCallback pMethodCall; +}; + +class CJS_Object { + public: + static void DefineConsts(CFXJS_Engine* pEngine, + int objId, + const JSConstSpec consts[]); + static void DefineProps(CFXJS_Engine* pEngine, + int objId, + const JSPropertySpec props[]); + static void DefineMethods(CFXJS_Engine* pEngine, + int objId, + const JSMethodSpec methods[]); + + explicit CJS_Object(v8::Local<v8::Object> pObject); + virtual ~CJS_Object(); + + virtual void InitInstance(IJS_Runtime* pIRuntime); + + v8::Local<v8::Object> ToV8Object() { return m_pV8Object.Get(m_pIsolate); } + + // Takes ownership of |pObj|. + void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj.reset(pObj); } + CJS_EmbedObj* GetEmbedObject() const { return m_pEmbedObj.get(); } + + v8::Isolate* GetIsolate() const { return m_pIsolate; } + + protected: + std::unique_ptr<CJS_EmbedObj> m_pEmbedObj; + v8::Global<v8::Object> m_pV8Object; + v8::Isolate* m_pIsolate; +}; + +#endif // FXJS_CJS_OBJECT_H_ diff --git a/fxjs/cjs_position.cpp b/fxjs/cjs_position.cpp new file mode 100644 index 0000000000..ba1f3d947c --- /dev/null +++ b/fxjs/cjs_position.cpp @@ -0,0 +1,26 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_position.h" + +const JSConstSpec CJS_Position::ConstSpecs[] = { + {"textOnly", JSConstSpec::Number, 0, 0}, + {"iconOnly", JSConstSpec::Number, 1, 0}, + {"iconTextV", JSConstSpec::Number, 2, 0}, + {"textIconV", JSConstSpec::Number, 3, 0}, + {"iconTextH", JSConstSpec::Number, 4, 0}, + {"textIconH", JSConstSpec::Number, 5, 0}, + {"overlay", JSConstSpec::Number, 6, 0}, + {0, JSConstSpec::Number, 0, 0}}; + +int CJS_Position::ObjDefnID = -1; + +// static +void CJS_Position::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("position", FXJSOBJTYPE_STATIC, nullptr, nullptr); + DefineConsts(pEngine, ObjDefnID, ConstSpecs); +} diff --git a/fxjs/cjs_position.h b/fxjs/cjs_position.h new file mode 100644 index 0000000000..7557f01ab7 --- /dev/null +++ b/fxjs/cjs_position.h @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_POSITION_H_ +#define FXJS_CJS_POSITION_H_ + +#include "fxjs/JS_Define.h" + +class CJS_Position : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Position(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Position() override {} + + private: + static int ObjDefnID; + static const JSConstSpec ConstSpecs[]; +}; + +#endif // FXJS_CJS_POSITION_H_ diff --git a/fxjs/cjs_printparamsobj.cpp b/fxjs/cjs_printparamsobj.cpp new file mode 100644 index 0000000000..296c241736 --- /dev/null +++ b/fxjs/cjs_printparamsobj.cpp @@ -0,0 +1,34 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_printparamsobj.h" + +int CJS_PrintParamsObj::ObjDefnID = -1; + +// static +int CJS_PrintParamsObj::GetObjDefnID() { + return ObjDefnID; +} + +// static +void CJS_PrintParamsObj::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("PrintParamsObj", FXJSOBJTYPE_DYNAMIC, + JSConstructor<CJS_PrintParamsObj, PrintParamsObj>, + JSDestructor<CJS_PrintParamsObj>); +} + +PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject) + : CJS_EmbedObj(pJSObject) { + bUI = true; + nStart = 0; + nEnd = 0; + bSilent = false; + bShrinkToFit = false; + bPrintAsImage = false; + bReverse = false; + bAnnotations = true; +} diff --git a/fxjs/cjs_printparamsobj.h b/fxjs/cjs_printparamsobj.h new file mode 100644 index 0000000000..a0c91b0d43 --- /dev/null +++ b/fxjs/cjs_printparamsobj.h @@ -0,0 +1,41 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_PRINTPARAMSOBJ_H_ +#define FXJS_CJS_PRINTPARAMSOBJ_H_ + +#include "fxjs/JS_Define.h" + +class PrintParamsObj : public CJS_EmbedObj { + public: + explicit PrintParamsObj(CJS_Object* pJSObject); + ~PrintParamsObj() override {} + + public: + bool bUI; + int nStart; + int nEnd; + bool bSilent; + bool bShrinkToFit; + bool bPrintAsImage; + bool bReverse; + bool bAnnotations; +}; + +class CJS_PrintParamsObj : public CJS_Object { + public: + static int GetObjDefnID(); + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_PrintParamsObj(v8::Local<v8::Object> pObject) + : CJS_Object(pObject) {} + ~CJS_PrintParamsObj() override {} + + private: + static int ObjDefnID; +}; + +#endif // FXJS_CJS_PRINTPARAMSOBJ_H_ diff --git a/fxjs/cjs_publicmethods.cpp b/fxjs/cjs_publicmethods.cpp new file mode 100644 index 0000000000..8c5ab07a2e --- /dev/null +++ b/fxjs/cjs_publicmethods.cpp @@ -0,0 +1,1758 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_publicmethods.h" + +#include <algorithm> +#include <cmath> +#include <cwctype> +#include <iomanip> +#include <limits> +#include <sstream> +#include <string> +#include <vector> + +#include "core/fpdfdoc/cpdf_interform.h" +#include "core/fxcrt/fx_extension.h" +#include "fpdfsdk/cpdfsdk_formfillenvironment.h" +#include "fpdfsdk/cpdfsdk_interform.h" +#include "fxjs/JS_Define.h" +#include "fxjs/cjs_color.h" +#include "fxjs/cjs_event_context.h" +#include "fxjs/cjs_eventhandler.h" +#include "fxjs/cjs_field.h" +#include "fxjs/cjs_object.h" +#include "fxjs/cjs_runtime.h" +#include "fxjs/cjs_util.h" +#include "fxjs/js_resources.h" + +#define DOUBLE_CORRECT 0.000000000000001 + +const JSMethodSpec CJS_PublicMethods::GlobalFunctionSpecs[] = { + {"AFNumber_Format", AFNumber_Format_static}, + {"AFNumber_Keystroke", AFNumber_Keystroke_static}, + {"AFPercent_Format", AFPercent_Format_static}, + {"AFPercent_Keystroke", AFPercent_Keystroke_static}, + {"AFDate_FormatEx", AFDate_FormatEx_static}, + {"AFDate_KeystrokeEx", AFDate_KeystrokeEx_static}, + {"AFDate_Format", AFDate_Format_static}, + {"AFDate_Keystroke", AFDate_Keystroke_static}, + {"AFTime_FormatEx", AFTime_FormatEx_static}, + {"AFTime_KeystrokeEx", AFTime_KeystrokeEx_static}, + {"AFTime_Format", AFTime_Format_static}, + {"AFTime_Keystroke", AFTime_Keystroke_static}, + {"AFSpecial_Format", AFSpecial_Format_static}, + {"AFSpecial_Keystroke", AFSpecial_Keystroke_static}, + {"AFSpecial_KeystrokeEx", AFSpecial_KeystrokeEx_static}, + {"AFSimple", AFSimple_static}, + {"AFMakeNumber", AFMakeNumber_static}, + {"AFSimple_Calculate", AFSimple_Calculate_static}, + {"AFRange_Validate", AFRange_Validate_static}, + {"AFMergeChange", AFMergeChange_static}, + {"AFParseDateEx", AFParseDateEx_static}, + {"AFExtractNums", AFExtractNums_static}, + {0, 0}}; + +namespace { + +const wchar_t* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr", + L"May", L"Jun", L"Jul", L"Aug", + L"Sep", L"Oct", L"Nov", L"Dec"}; + +const wchar_t* const fullmonths[] = {L"January", L"February", L"March", + L"April", L"May", L"June", + L"July", L"August", L"September", + L"October", L"November", L"December"}; + +ByteString StrTrim(const ByteString& pStr) { + ByteString result(pStr); + result.TrimLeft(' '); + result.TrimRight(' '); + return result; +} + +WideString StrTrim(const WideString& pStr) { + WideString result(pStr); + result.TrimLeft(' '); + result.TrimRight(' '); + return result; +} + +void AlertIfPossible(CJS_EventContext* pContext, const wchar_t* swMsg) { + CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv(); + if (pFormFillEnv) + pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3); +} + +#if _FX_OS_ != _FX_OS_ANDROID_ +ByteString CalculateString(double dValue, + int iDec, + int* iDec2, + bool* bNegative) { + *bNegative = dValue < 0; + if (*bNegative) + dValue = -dValue; + + // Make sure the number of precision characters will fit. + if (iDec > std::numeric_limits<double>::digits10) + iDec = std::numeric_limits<double>::digits10; + + std::stringstream ss; + ss << std::fixed << std::setprecision(iDec) << dValue; + std::string stringValue = ss.str(); + size_t iDecimalPos = stringValue.find("."); + *iDec2 = iDecimalPos == std::string::npos ? stringValue.size() + : static_cast<int>(iDecimalPos); + return ByteString(stringValue.c_str()); +} +#endif + +template <CJS_Return (*F)(CJS_Runtime*, + const std::vector<v8::Local<v8::Value>>&)> +void JSGlobalFunc(const char* func_name_string, + const v8::FunctionCallbackInfo<v8::Value>& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + std::vector<v8::Local<v8::Value>> parameters; + for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) + parameters.push_back(info[i]); + + CJS_Return result = (*F)(pRuntime, parameters); + if (result.HasError()) { + pRuntime->Error( + JSFormatErrorString(func_name_string, nullptr, result.Error())); + return; + } + + if (result.HasReturn()) + info.GetReturnValue().Set(result.Return()); +} + +} // namespace + +// static +void CJS_PublicMethods::DefineJSObjects(CFXJS_Engine* pEngine) { + for (size_t i = 0; i < FX_ArraySize(GlobalFunctionSpecs) - 1; ++i) { + pEngine->DefineGlobalMethod( + CJS_PublicMethods::GlobalFunctionSpecs[i].pName, + CJS_PublicMethods::GlobalFunctionSpecs[i].pMethodCall); + } +} + +#define JS_STATIC_GLOBAL_FUN(fun_name) \ + void CJS_PublicMethods::fun_name##_static( \ + const v8::FunctionCallbackInfo<v8::Value>& info) { \ + JSGlobalFunc<fun_name>(#fun_name, info); \ + } + +JS_STATIC_GLOBAL_FUN(AFNumber_Format); +JS_STATIC_GLOBAL_FUN(AFNumber_Keystroke); +JS_STATIC_GLOBAL_FUN(AFPercent_Format); +JS_STATIC_GLOBAL_FUN(AFPercent_Keystroke); +JS_STATIC_GLOBAL_FUN(AFDate_FormatEx); +JS_STATIC_GLOBAL_FUN(AFDate_KeystrokeEx); +JS_STATIC_GLOBAL_FUN(AFDate_Format); +JS_STATIC_GLOBAL_FUN(AFDate_Keystroke); +JS_STATIC_GLOBAL_FUN(AFTime_FormatEx); +JS_STATIC_GLOBAL_FUN(AFTime_KeystrokeEx); +JS_STATIC_GLOBAL_FUN(AFTime_Format); +JS_STATIC_GLOBAL_FUN(AFTime_Keystroke); +JS_STATIC_GLOBAL_FUN(AFSpecial_Format); +JS_STATIC_GLOBAL_FUN(AFSpecial_Keystroke); +JS_STATIC_GLOBAL_FUN(AFSpecial_KeystrokeEx); +JS_STATIC_GLOBAL_FUN(AFSimple); +JS_STATIC_GLOBAL_FUN(AFMakeNumber); +JS_STATIC_GLOBAL_FUN(AFSimple_Calculate); +JS_STATIC_GLOBAL_FUN(AFRange_Validate); +JS_STATIC_GLOBAL_FUN(AFMergeChange); +JS_STATIC_GLOBAL_FUN(AFParseDateEx); +JS_STATIC_GLOBAL_FUN(AFExtractNums); + +bool CJS_PublicMethods::IsNumber(const WideString& str) { + WideString sTrim = StrTrim(str); + const wchar_t* pTrim = sTrim.c_str(); + const wchar_t* p = pTrim; + bool bDot = false; + bool bKXJS = false; + + wchar_t c; + while ((c = *p) != L'\0') { + if (c == L'.' || c == L',') { + if (bDot) + return false; + bDot = true; + } else if (c == L'-' || c == L'+') { + if (p != pTrim) + return false; + } else if (c == L'e' || c == L'E') { + if (bKXJS) + return false; + + p++; + c = *p; + if (c != L'+' && c != L'-') + return false; + bKXJS = true; + } else if (!std::iswdigit(c)) { + return false; + } + p++; + } + + return true; +} + +bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) { + switch (c_Mask) { + case L'9': + return !!std::iswdigit(c_Change); + case L'A': + return FXSYS_iswalpha(c_Change); + case L'O': + return FXSYS_iswalnum(c_Change); + case L'X': + return true; + default: + return (c_Change == c_Mask); + } +} + +bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) { + return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X'; +} + +double CJS_PublicMethods::AF_Simple(const wchar_t* sFuction, + double dValue1, + double dValue2) { + if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 || + FXSYS_wcsicmp(sFuction, L"SUM") == 0) { + return dValue1 + dValue2; + } + if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) { + return dValue1 * dValue2; + } + if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) { + return std::min(dValue1, dValue2); + } + if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) { + return std::max(dValue1, dValue2); + } + return dValue1; +} + +v8::Local<v8::Array> CJS_PublicMethods::AF_MakeArrayFromList( + CJS_Runtime* pRuntime, + v8::Local<v8::Value> val) { + if (!val.IsEmpty() && val->IsArray()) + return pRuntime->ToArray(val); + + WideString wsStr = pRuntime->ToWideString(val); + ByteString t = ByteString::FromUnicode(wsStr); + const char* p = t.c_str(); + + int ch = ','; + int nIndex = 0; + + v8::Local<v8::Array> StrArray = pRuntime->NewArray(); + while (*p) { + const char* pTemp = strchr(p, ch); + if (!pTemp) { + pRuntime->PutArrayElement( + StrArray, nIndex, + pRuntime->NewString(StrTrim(ByteString(p)).c_str())); + break; + } + + char* pSub = new char[pTemp - p + 1]; + strncpy(pSub, p, pTemp - p); + *(pSub + (pTemp - p)) = '\0'; + + pRuntime->PutArrayElement( + StrArray, nIndex, + pRuntime->NewString(StrTrim(ByteString(pSub)).c_str())); + delete[] pSub; + + nIndex++; + p = ++pTemp; + } + return StrArray; +} + +int CJS_PublicMethods::ParseStringInteger(const WideString& str, + size_t nStart, + size_t& nSkip, + size_t nMaxStep) { + int nRet = 0; + nSkip = 0; + for (size_t i = nStart, sz = str.GetLength(); i < sz; i++) { + if (i - nStart > 10) + break; + + wchar_t c = str[i]; + if (!std::iswdigit(c)) + break; + + nRet = nRet * 10 + FXSYS_DecimalCharToInt(c); + nSkip = i - nStart + 1; + if (nSkip >= nMaxStep) + break; + } + + return nRet; +} + +WideString CJS_PublicMethods::ParseStringString(const WideString& str, + size_t nStart, + size_t& nSkip) { + WideString swRet; + nSkip = 0; + for (size_t i = nStart, sz = str.GetLength(); i < sz; i++) { + wchar_t c = str[i]; + if (!std::iswdigit(c)) + break; + + swRet += c; + nSkip = i - nStart + 1; + } + + return swRet; +} + +double CJS_PublicMethods::ParseNormalDate(const WideString& value, + bool* bWrongFormat) { + double dt = JS_GetDateTime(); + + int nYear = JS_GetYearFromTime(dt); + int nMonth = JS_GetMonthFromTime(dt) + 1; + int nDay = JS_GetDayFromTime(dt); + int nHour = JS_GetHourFromTime(dt); + int nMin = JS_GetMinFromTime(dt); + int nSec = JS_GetSecFromTime(dt); + + int number[3]; + + size_t nSkip = 0; + size_t nLen = value.GetLength(); + size_t nIndex = 0; + size_t i = 0; + while (i < nLen) { + if (nIndex > 2) + break; + + wchar_t c = value[i]; + if (std::iswdigit(c)) { + number[nIndex++] = ParseStringInteger(value, i, nSkip, 4); + i += nSkip; + } else { + i++; + } + } + + if (nIndex == 2) { + // case2: month/day + // case3: day/month + if ((number[0] >= 1 && number[0] <= 12) && + (number[1] >= 1 && number[1] <= 31)) { + nMonth = number[0]; + nDay = number[1]; + } else if ((number[0] >= 1 && number[0] <= 31) && + (number[1] >= 1 && number[1] <= 12)) { + nDay = number[0]; + nMonth = number[1]; + } + + if (bWrongFormat) + *bWrongFormat = false; + } else if (nIndex == 3) { + // case1: year/month/day + // case2: month/day/year + // case3: day/month/year + + if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) && + (number[2] >= 1 && number[2] <= 31)) { + nYear = number[0]; + nMonth = number[1]; + nDay = number[2]; + } else if ((number[0] >= 1 && number[0] <= 12) && + (number[1] >= 1 && number[1] <= 31) && number[2] > 31) { + nMonth = number[0]; + nDay = number[1]; + nYear = number[2]; + } else if ((number[0] >= 1 && number[0] <= 31) && + (number[1] >= 1 && number[1] <= 12) && number[2] > 31) { + nDay = number[0]; + nMonth = number[1]; + nYear = number[2]; + } + + if (bWrongFormat) + *bWrongFormat = false; + } else { + if (bWrongFormat) + *bWrongFormat = true; + return dt; + } + + WideString swTemp; + swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec); + return JS_DateParse(swTemp); +} + +double CJS_PublicMethods::MakeRegularDate(const WideString& value, + const WideString& format, + bool* bWrongFormat) { + double dt = JS_GetDateTime(); + + if (format.IsEmpty() || value.IsEmpty()) + return dt; + + int nYear = JS_GetYearFromTime(dt); + int nMonth = JS_GetMonthFromTime(dt) + 1; + int nDay = JS_GetDayFromTime(dt); + int nHour = JS_GetHourFromTime(dt); + int nMin = JS_GetMinFromTime(dt); + int nSec = JS_GetSecFromTime(dt); + + int nYearSub = 99; // nYear - 2000; + + bool bPm = false; + bool bExit = false; + bool bBadFormat = false; + + size_t i = 0; + size_t j = 0; + + while (i < format.GetLength()) { + if (bExit) + break; + + wchar_t c = format[i]; + switch (c) { + case ':': + case '.': + case '-': + case '\\': + case '/': + i++; + j++; + break; + + case 'y': + case 'm': + case 'd': + case 'H': + case 'h': + case 'M': + case 's': + case 't': { + size_t oldj = j; + size_t nSkip = 0; + size_t remaining = format.GetLength() - i - 1; + + if (remaining == 0 || format[i + 1] != c) { + switch (c) { + case 'y': + i++; + j++; + break; + case 'm': + nMonth = ParseStringInteger(value, j, nSkip, 2); + i++; + j += nSkip; + break; + case 'd': + nDay = ParseStringInteger(value, j, nSkip, 2); + i++; + j += nSkip; + break; + case 'H': + nHour = ParseStringInteger(value, j, nSkip, 2); + i++; + j += nSkip; + break; + case 'h': + nHour = ParseStringInteger(value, j, nSkip, 2); + i++; + j += nSkip; + break; + case 'M': + nMin = ParseStringInteger(value, j, nSkip, 2); + i++; + j += nSkip; + break; + case 's': + nSec = ParseStringInteger(value, j, nSkip, 2); + i++; + j += nSkip; + break; + case 't': + bPm = (j < value.GetLength() && value[j] == 'p'); + i++; + j++; + break; + } + } else if (remaining == 1 || format[i + 2] != c) { + switch (c) { + case 'y': + nYear = ParseStringInteger(value, j, nSkip, 4); + i += 2; + j += nSkip; + break; + case 'm': + nMonth = ParseStringInteger(value, j, nSkip, 2); + i += 2; + j += nSkip; + break; + case 'd': + nDay = ParseStringInteger(value, j, nSkip, 2); + i += 2; + j += nSkip; + break; + case 'H': + nHour = ParseStringInteger(value, j, nSkip, 2); + i += 2; + j += nSkip; + break; + case 'h': + nHour = ParseStringInteger(value, j, nSkip, 2); + i += 2; + j += nSkip; + break; + case 'M': + nMin = ParseStringInteger(value, j, nSkip, 2); + i += 2; + j += nSkip; + break; + case 's': + nSec = ParseStringInteger(value, j, nSkip, 2); + i += 2; + j += nSkip; + break; + case 't': + bPm = (j + 1 < value.GetLength() && value[j] == 'p' && + value[j + 1] == 'm'); + i += 2; + j += 2; + break; + } + } else if (remaining == 2 || format[i + 3] != c) { + switch (c) { + case 'm': { + WideString sMonth = ParseStringString(value, j, nSkip); + bool bFind = false; + for (int m = 0; m < 12; m++) { + if (sMonth.CompareNoCase(months[m]) == 0) { + nMonth = m + 1; + i += 3; + j += nSkip; + bFind = true; + break; + } + } + + if (!bFind) { + nMonth = ParseStringInteger(value, j, nSkip, 3); + i += 3; + j += nSkip; + } + } break; + case 'y': + break; + default: + i += 3; + j += 3; + break; + } + } else if (remaining == 3 || format[i + 4] != c) { + switch (c) { + case 'y': + nYear = ParseStringInteger(value, j, nSkip, 4); + j += nSkip; + i += 4; + break; + case 'm': { + bool bFind = false; + + WideString sMonth = ParseStringString(value, j, nSkip); + sMonth.MakeLower(); + + for (int m = 0; m < 12; m++) { + WideString sFullMonths = fullmonths[m]; + sFullMonths.MakeLower(); + + if (sFullMonths.Contains(sMonth.c_str())) { + nMonth = m + 1; + i += 4; + j += nSkip; + bFind = true; + break; + } + } + + if (!bFind) { + nMonth = ParseStringInteger(value, j, nSkip, 4); + i += 4; + j += nSkip; + } + } break; + default: + i += 4; + j += 4; + break; + } + } else { + if (j >= value.GetLength() || format[i] != value[j]) { + bBadFormat = true; + bExit = true; + } + i++; + j++; + } + + if (oldj == j) { + bBadFormat = true; + bExit = true; + } + } + + break; + default: + if (value.GetLength() <= j) { + bExit = true; + } else if (format[i] != value[j]) { + bBadFormat = true; + bExit = true; + } + + i++; + j++; + break; + } + } + + if (bPm) + nHour += 12; + + if (nYear >= 0 && nYear <= nYearSub) + nYear += 2000; + + if (nMonth < 1 || nMonth > 12) + bBadFormat = true; + + if (nDay < 1 || nDay > 31) + bBadFormat = true; + + if (nHour < 0 || nHour > 24) + bBadFormat = true; + + if (nMin < 0 || nMin > 60) + bBadFormat = true; + + if (nSec < 0 || nSec > 60) + bBadFormat = true; + + double dRet = 0; + if (bBadFormat) { + dRet = ParseNormalDate(value, &bBadFormat); + } else { + dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay), + JS_MakeTime(nHour, nMin, nSec, 0)); + if (std::isnan(dRet)) + dRet = JS_DateParse(value); + } + + if (std::isnan(dRet)) + dRet = ParseNormalDate(value, &bBadFormat); + + if (bWrongFormat) + *bWrongFormat = bBadFormat; + + return dRet; +} + +WideString CJS_PublicMethods::MakeFormatDate(double dDate, + const WideString& format) { + WideString sRet = L"", sPart = L""; + + int nYear = JS_GetYearFromTime(dDate); + int nMonth = JS_GetMonthFromTime(dDate) + 1; + int nDay = JS_GetDayFromTime(dDate); + int nHour = JS_GetHourFromTime(dDate); + int nMin = JS_GetMinFromTime(dDate); + int nSec = JS_GetSecFromTime(dDate); + + size_t i = 0; + while (i < format.GetLength()) { + wchar_t c = format[i]; + size_t remaining = format.GetLength() - i - 1; + sPart = L""; + switch (c) { + case 'y': + case 'm': + case 'd': + case 'H': + case 'h': + case 'M': + case 's': + case 't': + if (remaining == 0 || format[i + 1] != c) { + switch (c) { + case 'y': + sPart += c; + break; + case 'm': + sPart.Format(L"%d", nMonth); + break; + case 'd': + sPart.Format(L"%d", nDay); + break; + case 'H': + sPart.Format(L"%d", nHour); + break; + case 'h': + sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour); + break; + case 'M': + sPart.Format(L"%d", nMin); + break; + case 's': + sPart.Format(L"%d", nSec); + break; + case 't': + sPart += nHour > 12 ? 'p' : 'a'; + break; + } + i++; + } else if (remaining == 1 || format[i + 2] != c) { + switch (c) { + case 'y': + sPart.Format(L"%02d", nYear - (nYear / 100) * 100); + break; + case 'm': + sPart.Format(L"%02d", nMonth); + break; + case 'd': + sPart.Format(L"%02d", nDay); + break; + case 'H': + sPart.Format(L"%02d", nHour); + break; + case 'h': + sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour); + break; + case 'M': + sPart.Format(L"%02d", nMin); + break; + case 's': + sPart.Format(L"%02d", nSec); + break; + case 't': + sPart = nHour > 12 ? L"pm" : L"am"; + break; + } + i += 2; + } else if (remaining == 2 || format[i + 3] != c) { + switch (c) { + case 'm': + i += 3; + if (nMonth > 0 && nMonth <= 12) + sPart += months[nMonth - 1]; + break; + default: + i += 3; + sPart += c; + sPart += c; + sPart += c; + break; + } + } else if (remaining == 3 || format[i + 4] != c) { + switch (c) { + case 'y': + sPart.Format(L"%04d", nYear); + i += 4; + break; + case 'm': + i += 4; + if (nMonth > 0 && nMonth <= 12) + sPart += fullmonths[nMonth - 1]; + break; + default: + i += 4; + sPart += c; + sPart += c; + sPart += c; + sPart += c; + break; + } + } else { + i++; + sPart += c; + } + break; + default: + i++; + sPart += c; + break; + } + + sRet += sPart; + } + + return sRet; +} + +// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency, +// bCurrencyPrepend) +CJS_Return CJS_PublicMethods::AFNumber_Format( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { +#if _FX_OS_ != _FX_OS_ANDROID_ + if (params.size() != 6) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + if (!pEvent->m_pValue) + return CJS_Return(false); + + WideString& Value = pEvent->Value(); + ByteString strValue = StrTrim(ByteString::FromUnicode(Value)); + if (strValue.IsEmpty()) + return CJS_Return(true); + + int iDec = pRuntime->ToInt32(params[0]); + int iSepStyle = pRuntime->ToInt32(params[1]); + int iNegStyle = pRuntime->ToInt32(params[2]); + // params[3] is iCurrStyle, it's not used. + WideString wstrCurrency = pRuntime->ToWideString(params[4]); + bool bCurrencyPrepend = pRuntime->ToBoolean(params[5]); + + if (iDec < 0) + iDec = -iDec; + + if (iSepStyle < 0 || iSepStyle > 3) + iSepStyle = 0; + + if (iNegStyle < 0 || iNegStyle > 3) + iNegStyle = 0; + + // Processing decimal places + strValue.Replace(",", "."); + double dValue = atof(strValue.c_str()); + if (iDec > 0) + dValue += DOUBLE_CORRECT; + + // Calculating number string + bool bNegative; + int iDec2; + strValue = CalculateString(dValue, iDec, &iDec2, &bNegative); + if (strValue.IsEmpty()) { + dValue = 0; + strValue = CalculateString(dValue, iDec, &iDec2, &bNegative); + if (strValue.IsEmpty()) { + strValue = "0"; + iDec2 = 1; + } + } + + // Processing separator style + if (static_cast<size_t>(iDec2) < strValue.GetLength()) { + if (iSepStyle == 2 || iSepStyle == 3) + strValue.Replace(".", ","); + + if (iDec2 == 0) + strValue.Insert(iDec2, '0'); + } + if (iSepStyle == 0 || iSepStyle == 2) { + char cSeparator; + if (iSepStyle == 0) + cSeparator = ','; + else + cSeparator = '.'; + + for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) + strValue.Insert(iDecPositive, cSeparator); + } + + // Processing currency string + Value = WideString::FromLocal(strValue.AsStringView()); + if (bCurrencyPrepend) + Value = wstrCurrency + Value; + else + Value = Value + wstrCurrency; + + // Processing negative style + if (bNegative) { + if (iNegStyle == 0) + Value = L"-" + Value; + else if (iNegStyle == 2 || iNegStyle == 3) + Value = L"(" + Value + L")"; + if (iNegStyle == 1 || iNegStyle == 3) { + if (Field* fTarget = pEvent->Target_Field()) { + v8::Local<v8::Array> arColor = pRuntime->NewArray(); + pRuntime->PutArrayElement(arColor, 0, pRuntime->NewString(L"RGB")); + pRuntime->PutArrayElement(arColor, 1, pRuntime->NewNumber(1)); + pRuntime->PutArrayElement(arColor, 2, pRuntime->NewNumber(0)); + pRuntime->PutArrayElement(arColor, 3, pRuntime->NewNumber(0)); + fTarget->set_text_color(pRuntime, arColor); + } + } + } else { + if (iNegStyle == 1 || iNegStyle == 3) { + if (Field* fTarget = pEvent->Target_Field()) { + v8::Local<v8::Array> arColor = pRuntime->NewArray(); + pRuntime->PutArrayElement(arColor, 0, pRuntime->NewString(L"RGB")); + pRuntime->PutArrayElement(arColor, 1, pRuntime->NewNumber(0)); + pRuntime->PutArrayElement(arColor, 2, pRuntime->NewNumber(0)); + pRuntime->PutArrayElement(arColor, 3, pRuntime->NewNumber(0)); + + CJS_Return result = fTarget->get_text_color(pRuntime); + CFX_Color crProp = color::ConvertArrayToPWLColor( + pRuntime, pRuntime->ToArray(result.Return())); + CFX_Color crColor = color::ConvertArrayToPWLColor(pRuntime, arColor); + if (crColor != crProp) + fTarget->set_text_color(pRuntime, arColor); + } + } + } +#endif + return CJS_Return(true); +} + +// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency, +// bCurrencyPrepend) +CJS_Return CJS_PublicMethods::AFNumber_Keystroke( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() < 2) + return CJS_Return(false); + + CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); + CJS_EventHandler* pEvent = pContext->GetEventHandler(); + if (!pEvent->m_pValue) + return CJS_Return(false); + + WideString& val = pEvent->Value(); + WideString& wstrChange = pEvent->Change(); + WideString wstrValue = val; + + if (pEvent->WillCommit()) { + WideString swTemp = StrTrim(wstrValue); + if (swTemp.IsEmpty()) + return CJS_Return(true); + + swTemp.Replace(L",", L"."); + if (!IsNumber(swTemp.c_str())) { + pEvent->Rc() = false; + WideString sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE); + AlertIfPossible(pContext, sError.c_str()); + return CJS_Return(sError); + } + // It happens after the last keystroke and before validating, + return CJS_Return(true); + } + + WideString wstrSelected; + if (pEvent->SelStart() != -1) { + wstrSelected = wstrValue.Mid(pEvent->SelStart(), + pEvent->SelEnd() - pEvent->SelStart()); + } + + bool bHasSign = wstrValue.Contains(L'-') && !wstrSelected.Contains(L'-'); + if (bHasSign) { + // can't insert "change" in front to sign postion. + if (pEvent->SelStart() == 0) { + pEvent->Rc() = false; + return CJS_Return(true); + } + } + + int iSepStyle = pRuntime->ToInt32(params[1]); + if (iSepStyle < 0 || iSepStyle > 3) + iSepStyle = 0; + const wchar_t cSep = iSepStyle < 2 ? L'.' : L','; + + bool bHasSep = wstrValue.Contains(cSep); + for (size_t i = 0; i < wstrChange.GetLength(); ++i) { + if (wstrChange[i] == cSep) { + if (bHasSep) { + pEvent->Rc() = false; + return CJS_Return(true); + } + bHasSep = true; + continue; + } + if (wstrChange[i] == L'-') { + if (bHasSign) { + pEvent->Rc() = false; + return CJS_Return(true); + } + // sign's position is not correct + if (i != 0) { + pEvent->Rc() = false; + return CJS_Return(true); + } + if (pEvent->SelStart() != 0) { + pEvent->Rc() = false; + return CJS_Return(true); + } + bHasSign = true; + continue; + } + + if (!std::iswdigit(wstrChange[i])) { + pEvent->Rc() = false; + return CJS_Return(true); + } + } + + WideString wprefix = wstrValue.Left(pEvent->SelStart()); + WideString wpostfix; + if (pEvent->SelEnd() >= 0 && + static_cast<size_t>(pEvent->SelEnd()) < wstrValue.GetLength()) + wpostfix = wstrValue.Right(wstrValue.GetLength() - + static_cast<size_t>(pEvent->SelEnd())); + val = wprefix + wstrChange + wpostfix; + return CJS_Return(true); +} + +// function AFPercent_Format(nDec, sepStyle) +CJS_Return CJS_PublicMethods::AFPercent_Format( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { +#if _FX_OS_ != _FX_OS_ANDROID_ + if (params.size() != 2) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + if (!pEvent->m_pValue) + return CJS_Return(false); + + WideString& Value = pEvent->Value(); + ByteString strValue = StrTrim(ByteString::FromUnicode(Value)); + if (strValue.IsEmpty()) + return CJS_Return(true); + + int iDec = pRuntime->ToInt32(params[0]); + if (iDec < 0) + iDec = -iDec; + + int iSepStyle = pRuntime->ToInt32(params[1]); + if (iSepStyle < 0 || iSepStyle > 3) + iSepStyle = 0; + + // for processing decimal places + double dValue = atof(strValue.c_str()); + dValue *= 100; + if (iDec > 0) + dValue += DOUBLE_CORRECT; + + int iDec2; + int iNegative = 0; + strValue = fcvt(dValue, iDec, &iDec2, &iNegative); + if (strValue.IsEmpty()) { + dValue = 0; + strValue = fcvt(dValue, iDec, &iDec2, &iNegative); + } + + if (iDec2 < 0) { + for (int iNum = 0; iNum < abs(iDec2); iNum++) + strValue = "0" + strValue; + + iDec2 = 0; + } + int iMax = strValue.GetLength(); + if (iDec2 > iMax) { + for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) + strValue += "0"; + + iMax = iDec2 + 1; + } + + // for processing seperator style + if (iDec2 < iMax) { + if (iSepStyle == 0 || iSepStyle == 1) { + strValue.Insert(iDec2, '.'); + iMax++; + } else if (iSepStyle == 2 || iSepStyle == 3) { + strValue.Insert(iDec2, ','); + iMax++; + } + + if (iDec2 == 0) + strValue.Insert(iDec2, '0'); + } + if (iSepStyle == 0 || iSepStyle == 2) { + char cSeperator; + if (iSepStyle == 0) + cSeperator = ','; + else + cSeperator = '.'; + + for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) { + strValue.Insert(iDecPositive, cSeperator); + iMax++; + } + } + + // negative mark + if (iNegative) + strValue = "-" + strValue; + + strValue += "%"; + Value = WideString::FromLocal(strValue.AsStringView()); +#endif + return CJS_Return(true); +} + +// AFPercent_Keystroke(nDec, sepStyle) +CJS_Return CJS_PublicMethods::AFPercent_Keystroke( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return AFNumber_Keystroke(pRuntime, params); +} + +// function AFDate_FormatEx(cFormat) +CJS_Return CJS_PublicMethods::AFDate_FormatEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); + CJS_EventHandler* pEvent = pContext->GetEventHandler(); + if (!pEvent->m_pValue) + return CJS_Return(false); + + WideString& val = pEvent->Value(); + WideString strValue = val; + if (strValue.IsEmpty()) + return CJS_Return(true); + + WideString sFormat = pRuntime->ToWideString(params[0]); + double dDate = 0.0f; + + if (strValue.Contains(L"GMT")) { + // for GMT format time + // such as "Tue Aug 11 14:24:16 GMT+08002009" + dDate = MakeInterDate(strValue); + } else { + dDate = MakeRegularDate(strValue, sFormat, nullptr); + } + + if (std::isnan(dDate)) { + WideString swMsg; + swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(), + sFormat.c_str()); + AlertIfPossible(pContext, swMsg.c_str()); + return CJS_Return(false); + } + + val = MakeFormatDate(dDate, sFormat); + return CJS_Return(true); +} + +double CJS_PublicMethods::MakeInterDate(const WideString& strValue) { + std::vector<WideString> wsArray; + WideString sTemp = L""; + for (const auto& c : strValue) { + if (c == L' ' || c == L':') { + wsArray.push_back(sTemp); + sTemp = L""; + continue; + } + sTemp += c; + } + wsArray.push_back(sTemp); + if (wsArray.size() != 8) + return 0; + + int nMonth = 1; + sTemp = wsArray[1]; + if (sTemp.Compare(L"Jan") == 0) + nMonth = 1; + else if (sTemp.Compare(L"Feb") == 0) + nMonth = 2; + else if (sTemp.Compare(L"Mar") == 0) + nMonth = 3; + else if (sTemp.Compare(L"Apr") == 0) + nMonth = 4; + else if (sTemp.Compare(L"May") == 0) + nMonth = 5; + else if (sTemp.Compare(L"Jun") == 0) + nMonth = 6; + else if (sTemp.Compare(L"Jul") == 0) + nMonth = 7; + else if (sTemp.Compare(L"Aug") == 0) + nMonth = 8; + else if (sTemp.Compare(L"Sep") == 0) + nMonth = 9; + else if (sTemp.Compare(L"Oct") == 0) + nMonth = 10; + else if (sTemp.Compare(L"Nov") == 0) + nMonth = 11; + else if (sTemp.Compare(L"Dec") == 0) + nMonth = 12; + + int nDay = FX_atof(wsArray[2].AsStringView()); + int nHour = FX_atof(wsArray[3].AsStringView()); + int nMin = FX_atof(wsArray[4].AsStringView()); + int nSec = FX_atof(wsArray[5].AsStringView()); + int nYear = FX_atof(wsArray[7].AsStringView()); + double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay), + JS_MakeTime(nHour, nMin, nSec, 0)); + if (std::isnan(dRet)) + dRet = JS_DateParse(strValue); + + return dRet; +} + +// AFDate_KeystrokeEx(cFormat) +CJS_Return CJS_PublicMethods::AFDate_KeystrokeEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return( + WideString(L"AFDate_KeystrokeEx's parameters' size r not correct")); + + CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); + CJS_EventHandler* pEvent = pContext->GetEventHandler(); + if (pEvent->WillCommit()) { + if (!pEvent->m_pValue) + return CJS_Return(false); + + WideString strValue = pEvent->Value(); + if (strValue.IsEmpty()) + return CJS_Return(true); + + WideString sFormat = pRuntime->ToWideString(params[0]); + bool bWrongFormat = false; + double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat); + if (bWrongFormat || std::isnan(dRet)) { + WideString swMsg; + swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(), + sFormat.c_str()); + AlertIfPossible(pContext, swMsg.c_str()); + pEvent->Rc() = false; + return CJS_Return(true); + } + } + return CJS_Return(true); +} + +CJS_Return CJS_PublicMethods::AFDate_Format( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + int iIndex = pRuntime->ToInt32(params[0]); + const wchar_t* cFormats[] = {L"m/d", + L"m/d/yy", + L"mm/dd/yy", + L"mm/yy", + L"d-mmm", + L"d-mmm-yy", + L"dd-mmm-yy", + L"yy-mm-dd", + L"mmm-yy", + L"mmmm-yy", + L"mmm d, yyyy", + L"mmmm d, yyyy", + L"m/d/yy h:MM tt", + L"m/d/yy HH:MM"}; + + if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats))) + iIndex = 0; + + std::vector<v8::Local<v8::Value>> newParams; + newParams.push_back(pRuntime->NewString(cFormats[iIndex])); + return AFDate_FormatEx(pRuntime, newParams); +} + +// AFDate_KeystrokeEx(cFormat) +CJS_Return CJS_PublicMethods::AFDate_Keystroke( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + int iIndex = pRuntime->ToInt32(params[0]); + const wchar_t* cFormats[] = {L"m/d", + L"m/d/yy", + L"mm/dd/yy", + L"mm/yy", + L"d-mmm", + L"d-mmm-yy", + L"dd-mmm-yy", + L"yy-mm-dd", + L"mmm-yy", + L"mmmm-yy", + L"mmm d, yyyy", + L"mmmm d, yyyy", + L"m/d/yy h:MM tt", + L"m/d/yy HH:MM"}; + + if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats))) + iIndex = 0; + + std::vector<v8::Local<v8::Value>> newParams; + newParams.push_back(pRuntime->NewString(cFormats[iIndex])); + return AFDate_KeystrokeEx(pRuntime, newParams); +} + +// function AFTime_Format(ptf) +CJS_Return CJS_PublicMethods::AFTime_Format( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + int iIndex = pRuntime->ToInt32(params[0]); + const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", + L"h:MM:ss tt"}; + + if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats))) + iIndex = 0; + + std::vector<v8::Local<v8::Value>> newParams; + newParams.push_back(pRuntime->NewString(cFormats[iIndex])); + return AFDate_FormatEx(pRuntime, newParams); +} + +CJS_Return CJS_PublicMethods::AFTime_Keystroke( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + int iIndex = pRuntime->ToInt32(params[0]); + const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", + L"h:MM:ss tt"}; + + if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats))) + iIndex = 0; + + std::vector<v8::Local<v8::Value>> newParams; + newParams.push_back(pRuntime->NewString(cFormats[iIndex])); + return AFDate_KeystrokeEx(pRuntime, newParams); +} + +CJS_Return CJS_PublicMethods::AFTime_FormatEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return AFDate_FormatEx(pRuntime, params); +} + +CJS_Return CJS_PublicMethods::AFTime_KeystrokeEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + return AFDate_KeystrokeEx(pRuntime, params); +} + +// function AFSpecial_Format(psf) +CJS_Return CJS_PublicMethods::AFSpecial_Format( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + if (!pEvent->m_pValue) + return CJS_Return(false); + + WideString wsSource = pEvent->Value(); + WideString wsFormat; + switch (pRuntime->ToInt32(params[0])) { + case 0: + wsFormat = L"99999"; + break; + case 1: + wsFormat = L"99999-9999"; + break; + case 2: + if (util::printx(L"9999999999", wsSource).GetLength() >= 10) + wsFormat = L"(999) 999-9999"; + else + wsFormat = L"999-9999"; + break; + case 3: + wsFormat = L"999-99-9999"; + break; + } + + pEvent->Value() = util::printx(wsFormat, wsSource); + return CJS_Return(true); +} + +// function AFSpecial_KeystrokeEx(mask) +CJS_Return CJS_PublicMethods::AFSpecial_KeystrokeEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() < 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); + CJS_EventHandler* pEvent = pContext->GetEventHandler(); + if (!pEvent->m_pValue) + return CJS_Return(false); + + WideString& valEvent = pEvent->Value(); + WideString wstrMask = pRuntime->ToWideString(params[0]); + if (wstrMask.IsEmpty()) + return CJS_Return(true); + + if (pEvent->WillCommit()) { + if (valEvent.IsEmpty()) + return CJS_Return(true); + + size_t iIndexMask = 0; + for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) { + if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask])) + break; + } + + if (iIndexMask != wstrMask.GetLength() || + (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) { + AlertIfPossible( + pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str()); + pEvent->Rc() = false; + } + return CJS_Return(true); + } + + WideString& wideChange = pEvent->Change(); + if (wideChange.IsEmpty()) + return CJS_Return(true); + + WideString wChange = wideChange; + size_t iIndexMask = pEvent->SelStart(); + size_t combined_len = valEvent.GetLength() + wChange.GetLength() + + pEvent->SelStart() - pEvent->SelEnd(); + if (combined_len > wstrMask.GetLength()) { + AlertIfPossible(pContext, + JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str()); + pEvent->Rc() = false; + return CJS_Return(true); + } + + if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) { + AlertIfPossible(pContext, + JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str()); + pEvent->Rc() = false; + return CJS_Return(true); + } + + for (size_t i = 0; i < wChange.GetLength(); ++i) { + if (iIndexMask >= wstrMask.GetLength()) { + AlertIfPossible(pContext, + JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str()); + pEvent->Rc() = false; + return CJS_Return(true); + } + wchar_t wMask = wstrMask[iIndexMask]; + if (!isReservedMaskChar(wMask)) + wChange.SetAt(i, wMask); + + if (!maskSatisfied(wChange[i], wMask)) { + pEvent->Rc() = false; + return CJS_Return(true); + } + iIndexMask++; + } + wideChange = wChange; + return CJS_Return(true); +} + +// function AFSpecial_Keystroke(psf) +CJS_Return CJS_PublicMethods::AFSpecial_Keystroke( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + CJS_EventHandler* pEvent = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + if (!pEvent->m_pValue) + return CJS_Return(false); + + const char* cFormat = ""; + switch (pRuntime->ToInt32(params[0])) { + case 0: + cFormat = "99999"; + break; + case 1: + cFormat = "999999999"; + break; + case 2: + if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7) + cFormat = "9999999999"; + else + cFormat = "9999999"; + break; + case 3: + cFormat = "999999999"; + break; + } + + std::vector<v8::Local<v8::Value>> params2; + params2.push_back(pRuntime->NewString(cFormat)); + return AFSpecial_KeystrokeEx(pRuntime, params2); +} + +CJS_Return CJS_PublicMethods::AFMergeChange( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + CJS_EventHandler* pEventHandler = + pRuntime->GetCurrentEventContext()->GetEventHandler(); + + WideString swValue; + if (pEventHandler->m_pValue) + swValue = pEventHandler->Value(); + + if (pEventHandler->WillCommit()) + return CJS_Return(pRuntime->NewString(swValue.c_str())); + + WideString prefix; + WideString postfix; + if (pEventHandler->SelStart() >= 0) + prefix = swValue.Left(pEventHandler->SelStart()); + else + prefix = L""; + + if (pEventHandler->SelEnd() >= 0 && + static_cast<size_t>(pEventHandler->SelEnd()) <= swValue.GetLength()) + postfix = swValue.Right(swValue.GetLength() - + static_cast<size_t>(pEventHandler->SelEnd())); + else + postfix = L""; + + return CJS_Return(pRuntime->NewString( + (prefix + pEventHandler->Change() + postfix).c_str())); +} + +CJS_Return CJS_PublicMethods::AFParseDateEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 2) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + WideString sValue = pRuntime->ToWideString(params[0]); + WideString sFormat = pRuntime->ToWideString(params[1]); + double dDate = MakeRegularDate(sValue, sFormat, nullptr); + if (std::isnan(dDate)) { + WideString swMsg; + swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(), + sFormat.c_str()); + AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str()); + return CJS_Return(false); + } + return CJS_Return(pRuntime->NewNumber(dDate)); +} + +CJS_Return CJS_PublicMethods::AFSimple( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 3) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + return CJS_Return(pRuntime->NewNumber(static_cast<double>(AF_Simple( + pRuntime->ToWideString(params[0]).c_str(), pRuntime->ToDouble(params[1]), + pRuntime->ToDouble(params[2]))))); +} + +CJS_Return CJS_PublicMethods::AFMakeNumber( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + WideString ws = pRuntime->ToWideString(params[0]); + ws.Replace(L",", L"."); + + v8::Local<v8::Value> val = + pRuntime->MaybeCoerceToNumber(pRuntime->NewString(ws.c_str())); + if (!val->IsNumber()) + return CJS_Return(pRuntime->NewNumber(0)); + return CJS_Return(val); +} + +CJS_Return CJS_PublicMethods::AFSimple_Calculate( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 2) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + if ((params[1].IsEmpty() || !params[1]->IsArray()) && !params[1]->IsString()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + CPDFSDK_InterForm* pReaderInterForm = + pRuntime->GetFormFillEnv()->GetInterForm(); + CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm(); + + WideString sFunction = pRuntime->ToWideString(params[0]); + double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0; + + v8::Local<v8::Array> FieldNameArray = + AF_MakeArrayFromList(pRuntime, params[0]); + int nFieldsCount = 0; + for (size_t i = 0; i < pRuntime->GetArrayLength(FieldNameArray); i++) { + WideString wsFieldName = + pRuntime->ToWideString(pRuntime->GetArrayElement(FieldNameArray, i)); + + for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) { + if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) { + double dTemp = 0.0; + switch (pFormField->GetFieldType()) { + case FIELDTYPE_TEXTFIELD: + case FIELDTYPE_COMBOBOX: { + WideString trimmed = pFormField->GetValue(); + trimmed.TrimRight(); + trimmed.TrimLeft(); + dTemp = FX_atof(trimmed.AsStringView()); + break; + } + case FIELDTYPE_PUSHBUTTON: + dTemp = 0.0; + break; + case FIELDTYPE_CHECKBOX: + case FIELDTYPE_RADIOBUTTON: + dTemp = 0.0; + for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) { + if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) { + if (pFormCtrl->IsChecked()) { + WideString trimmed = pFormCtrl->GetExportValue(); + trimmed.TrimRight(); + trimmed.TrimLeft(); + dTemp = FX_atof(trimmed.AsStringView()); + break; + } + } + } + break; + case FIELDTYPE_LISTBOX: + if (pFormField->CountSelectedItems() <= 1) { + WideString trimmed = pFormField->GetValue(); + trimmed.TrimRight(); + trimmed.TrimLeft(); + dTemp = FX_atof(trimmed.AsStringView()); + } + break; + default: + break; + } + + if (i == 0 && j == 0 && + (wcscmp(sFunction.c_str(), L"MIN") == 0 || + wcscmp(sFunction.c_str(), L"MAX") == 0)) { + dValue = dTemp; + } + dValue = AF_Simple(sFunction.c_str(), dValue, dTemp); + + nFieldsCount++; + } + } + } + + if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0) + dValue /= nFieldsCount; + + dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) / + FXSYS_pow((double)10, (double)6); + + CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); + if (pContext->GetEventHandler()->m_pValue) { + pContext->GetEventHandler()->Value() = + pRuntime->ToWideString(pRuntime->NewNumber(dValue)); + } + + return CJS_Return(true); +} + +/* This function validates the current event to ensure that its value is +** within the specified range. */ + +CJS_Return CJS_PublicMethods::AFRange_Validate( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 4) + CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); + CJS_EventHandler* pEvent = pContext->GetEventHandler(); + if (!pEvent->m_pValue) + return CJS_Return(false); + + if (pEvent->Value().IsEmpty()) + return CJS_Return(true); + + double dEentValue = atof(ByteString::FromUnicode(pEvent->Value()).c_str()); + bool bGreaterThan = pRuntime->ToBoolean(params[0]); + double dGreaterThan = pRuntime->ToDouble(params[1]); + bool bLessThan = pRuntime->ToBoolean(params[2]); + double dLessThan = pRuntime->ToDouble(params[3]); + WideString swMsg; + + if (bGreaterThan && bLessThan) { + if (dEentValue < dGreaterThan || dEentValue > dLessThan) + swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(), + pRuntime->ToWideString(params[1]).c_str(), + pRuntime->ToWideString(params[3]).c_str()); + } else if (bGreaterThan) { + if (dEentValue < dGreaterThan) + swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(), + pRuntime->ToWideString(params[1]).c_str()); + } else if (bLessThan) { + if (dEentValue > dLessThan) + swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(), + pRuntime->ToWideString(params[3]).c_str()); + } + + if (!swMsg.IsEmpty()) { + AlertIfPossible(pContext, swMsg.c_str()); + pEvent->Rc() = false; + } + return CJS_Return(true); +} + +CJS_Return CJS_PublicMethods::AFExtractNums( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() != 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + WideString str = pRuntime->ToWideString(params[0]); + if (str.GetLength() > 0 && (str[0] == L'.' || str[0] == L',')) + str = L"0" + str; + + WideString sPart; + v8::Local<v8::Array> nums = pRuntime->NewArray(); + int nIndex = 0; + for (const auto& wc : str) { + if (std::iswdigit(wc)) { + sPart += wc; + } else if (sPart.GetLength() > 0) { + pRuntime->PutArrayElement(nums, nIndex, + pRuntime->NewString(sPart.c_str())); + sPart = L""; + nIndex++; + } + } + if (sPart.GetLength() > 0) + pRuntime->PutArrayElement(nums, nIndex, pRuntime->NewString(sPart.c_str())); + + if (pRuntime->GetArrayLength(nums) > 0) + return CJS_Return(nums); + return CJS_Return(pRuntime->NewUndefined()); +} diff --git a/fxjs/cjs_publicmethods.h b/fxjs/cjs_publicmethods.h new file mode 100644 index 0000000000..95ee4995c9 --- /dev/null +++ b/fxjs/cjs_publicmethods.h @@ -0,0 +1,159 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_PUBLICMETHODS_H_ +#define FXJS_CJS_PUBLICMETHODS_H_ + +#include <string> +#include <vector> + +#include "fxjs/JS_Define.h" + +class CJS_PublicMethods : public CJS_Object { + public: + explicit CJS_PublicMethods(v8::Local<v8::Object> pObject) + : CJS_Object(pObject) {} + ~CJS_PublicMethods() override {} + + static CJS_Return AFNumber_Format( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFNumber_Keystroke( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFPercent_Format( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFPercent_Keystroke( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFDate_FormatEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFDate_KeystrokeEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFDate_Format( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFDate_Keystroke( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFTime_FormatEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFTime_KeystrokeEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFTime_Format( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFTime_Keystroke( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFSpecial_Format( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFSpecial_Keystroke( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFSpecial_KeystrokeEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFSimple(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFMakeNumber( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFSimple_Calculate( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFRange_Validate( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFMergeChange( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFParseDateEx( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + static CJS_Return AFExtractNums( + CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + + static void AFNumber_Format_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFNumber_Keystroke_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFPercent_Format_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFPercent_Keystroke_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFDate_FormatEx_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFDate_KeystrokeEx_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFDate_Format_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFDate_Keystroke_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFTime_FormatEx_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFTime_KeystrokeEx_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFTime_Format_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFTime_Keystroke_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFSpecial_Format_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFSpecial_Keystroke_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFSpecial_KeystrokeEx_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFSimple_static(const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFMakeNumber_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFSimple_Calculate_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFRange_Validate_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFMergeChange_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFParseDateEx_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + static void AFExtractNums_static( + const v8::FunctionCallbackInfo<v8::Value>& info); + + static const JSMethodSpec GlobalFunctionSpecs[]; + static void DefineJSObjects(CFXJS_Engine* pEngine); + static int ParseStringInteger(const WideString& string, + size_t nStart, + size_t& nSkip, + size_t nMaxStep); + static WideString ParseStringString(const WideString& string, + size_t nStart, + size_t& nSkip); + static double MakeRegularDate(const WideString& value, + const WideString& format, + bool* bWrongFormat); + static WideString MakeFormatDate(double dDate, const WideString& format); + static double ParseNormalDate(const WideString& value, bool* bWrongFormat); + static double MakeInterDate(const WideString& value); + + static bool IsNumber(const WideString& str); + + static bool maskSatisfied(wchar_t c_Change, wchar_t c_Mask); + static bool isReservedMaskChar(wchar_t ch); + + static double AF_Simple(const wchar_t* sFuction, + double dValue1, + double dValue2); + static v8::Local<v8::Array> AF_MakeArrayFromList(CJS_Runtime* pRuntime, + v8::Local<v8::Value> val); +}; + +#endif // FXJS_CJS_PUBLICMETHODS_H_ diff --git a/fxjs/cjs_publicmethods_embeddertest.cpp b/fxjs/cjs_publicmethods_embeddertest.cpp new file mode 100644 index 0000000000..dbab0817ce --- /dev/null +++ b/fxjs/cjs_publicmethods_embeddertest.cpp @@ -0,0 +1,162 @@ +// Copyright 2015 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <cmath> + +#include "core/fxcrt/fx_string.h" +#include "fxjs/cjs_publicmethods.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/js_embedder_test.h" + +namespace { + +double RoundDownDate(double date) { + return date - fmod(date, 86400000); +} + +} // namespace + +class CJS_PublicMethodsEmbedderTest : public JSEmbedderTest {}; + +TEST_F(CJS_PublicMethodsEmbedderTest, MakeRegularDate) { + v8::Isolate::Scope isolate_scope(isolate()); + v8::HandleScope handle_scope(isolate()); + v8::Context::Scope context_scope(GetV8Context()); + bool bWrongFormat; + double date; + + // 1968 + bWrongFormat = false; + date = CJS_PublicMethods::MakeRegularDate(L"06/25/1968", L"mm/dd/yyyy", + &bWrongFormat); + date = RoundDownDate(date); + EXPECT_DOUBLE_EQ(-47865600000, date); + EXPECT_FALSE(bWrongFormat); + + // 1968 + bWrongFormat = false; + date = CJS_PublicMethods::MakeRegularDate(L"25061968", L"ddmmyyyy", + &bWrongFormat); + date = RoundDownDate(date); + EXPECT_DOUBLE_EQ(-47865600000, date); + EXPECT_FALSE(bWrongFormat); + + // 1968 + bWrongFormat = false; + date = CJS_PublicMethods::MakeRegularDate(L"19680625", L"yyyymmdd", + &bWrongFormat); + date = RoundDownDate(date); + EXPECT_DOUBLE_EQ(-47865600000, date); + EXPECT_FALSE(bWrongFormat); + + // 1985 + bWrongFormat = false; + date = CJS_PublicMethods::MakeRegularDate(L"31121985", L"ddmmyyyy", + &bWrongFormat); + date = RoundDownDate(date); + EXPECT_DOUBLE_EQ(504835200000.0, date); + EXPECT_FALSE(bWrongFormat); + + // 2085, the other '85. + bWrongFormat = false; + date = + CJS_PublicMethods::MakeRegularDate(L"311285", L"ddmmyy", &bWrongFormat); + date = RoundDownDate(date); + EXPECT_DOUBLE_EQ(3660595200000.0, date); + EXPECT_FALSE(bWrongFormat); + + // 1995 + bWrongFormat = false; + date = CJS_PublicMethods::MakeRegularDate(L"01021995", L"ddmmyyyy", + &bWrongFormat); + date = RoundDownDate(date); + EXPECT_DOUBLE_EQ(791596800000.0, date); + EXPECT_FALSE(bWrongFormat); + + // 2095, the other '95. + bWrongFormat = false; + date = + CJS_PublicMethods::MakeRegularDate(L"010295", L"ddmmyy", &bWrongFormat); + date = RoundDownDate(date); + EXPECT_DOUBLE_EQ(3947356800000.0, date); + EXPECT_FALSE(bWrongFormat); + + // 2005 + bWrongFormat = false; + date = CJS_PublicMethods::MakeRegularDate(L"01022005", L"ddmmyyyy", + &bWrongFormat); + date = RoundDownDate(date); + EXPECT_DOUBLE_EQ(1107216000000.0, date); + EXPECT_FALSE(bWrongFormat); + + // 2005 + bWrongFormat = false; + date = + CJS_PublicMethods::MakeRegularDate(L"010205", L"ddmmyy", &bWrongFormat); + date = RoundDownDate(date); + EXPECT_DOUBLE_EQ(1107216000000.0, date); + EXPECT_FALSE(bWrongFormat); +} + +TEST_F(CJS_PublicMethodsEmbedderTest, MakeFormatDate) { + v8::Isolate::Scope isolate_scope(isolate()); + v8::HandleScope handle_scope(isolate()); + v8::Context::Scope context_scope(GetV8Context()); + WideString formatted_date; + + // 1968-06-25 + formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"ddmmyy"); + EXPECT_STREQ(L"250668", formatted_date.c_str()); + formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"yy/mm/dd"); + EXPECT_STREQ(L"68/06/25", formatted_date.c_str()); + + // 1969-12-31 + formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"ddmmyy"); + EXPECT_STREQ(L"311269", formatted_date.c_str()); + formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"yy!mmdd"); + EXPECT_STREQ(L"69!1231", formatted_date.c_str()); + + // 1970-01-01 + formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"ddmmyy"); + EXPECT_STREQ(L"010170", formatted_date.c_str()); + formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"mm-yyyy-dd"); + EXPECT_STREQ(L"01-1970-01", formatted_date.c_str()); + + // 1985-12-31 + formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"ddmmyy"); + EXPECT_STREQ(L"311285", formatted_date.c_str()); + formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"yymmdd"); + EXPECT_STREQ(L"851231", formatted_date.c_str()); + + // 1995-02-01 + formatted_date = CJS_PublicMethods::MakeFormatDate(791596800000.0, L"ddmmyy"); + EXPECT_STREQ(L"010295", formatted_date.c_str()); + formatted_date = + CJS_PublicMethods::MakeFormatDate(791596800000.0, L"yyyymmdd"); + EXPECT_STREQ(L"19950201", formatted_date.c_str()); + + // 2005-02-01 + formatted_date = + CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"ddmmyy"); + EXPECT_STREQ(L"010205", formatted_date.c_str()); + formatted_date = + CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"yyyyddmm"); + EXPECT_STREQ(L"20050102", formatted_date.c_str()); + + // 2085-12-31 + formatted_date = + CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"ddmmyy"); + EXPECT_STREQ(L"311285", formatted_date.c_str()); + formatted_date = + CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"yyyydd"); + EXPECT_STREQ(L"208531", formatted_date.c_str()); + + // 2095-02-01 + formatted_date = + CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"ddmmyy"); + EXPECT_STREQ(L"010295", formatted_date.c_str()); + formatted_date = + CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"mmddyyyy"); + EXPECT_STREQ(L"02012095", formatted_date.c_str()); +} diff --git a/fxjs/cjs_publicmethods_unittest.cpp b/fxjs/cjs_publicmethods_unittest.cpp new file mode 100644 index 0000000000..37bf93ba43 --- /dev/null +++ b/fxjs/cjs_publicmethods_unittest.cpp @@ -0,0 +1,51 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "fxjs/cjs_publicmethods.h" + +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/test_support.h" + +TEST(CJS_PublicMethods, IsNumber) { + // TODO(weili): Check whether results from case 0, 1, 10, 15 are intended. + struct { + const wchar_t* input; + bool expected; + } test_data[] = { + // Empty string. + {L"", true}, + // Only whitespaces. + {L" ", true}, + // Content with invalid characters. + {L"xyz00", false}, + {L"1%", false}, + // Hex string. + {L"0x234", false}, + // Signed numbers. + {L"+123", true}, + {L"-98765", true}, + // Numbers with whitespaces. + {L" 345 ", true}, + // Float numbers. + {L"-1e5", false}, + {L"-2e", false}, + {L"e-5", true}, + {L"0.023", true}, + {L".356089", true}, + {L"1e-9", true}, + {L"-1.23e+23", true}, + // Numbers with commas. + {L"1,000,000", false}, + {L"560,024", true}, + // Regular numbers. + {L"0", true}, + {L"0123", true}, + {L"9876123", true}, + }; + for (size_t i = 0; i < FX_ArraySize(test_data); ++i) { + EXPECT_EQ(test_data[i].expected, + CJS_PublicMethods::IsNumber(test_data[i].input)) + << "for case " << i; + } +} diff --git a/fxjs/cjs_report.cpp b/fxjs/cjs_report.cpp new file mode 100644 index 0000000000..2aa3f49078 --- /dev/null +++ b/fxjs/cjs_report.cpp @@ -0,0 +1,42 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_report.h" + +#include <vector> + +#include "fxjs/JS_Define.h" +#include "fxjs/cjs_object.h" + +const JSMethodSpec CJS_Report::MethodSpecs[] = {{"save", save_static}, + {"writeText", writeText_static}, + {0, 0}}; + +int CJS_Report::ObjDefnID = -1; + +// static +void CJS_Report::DefineJSObjects(CFXJS_Engine* pEngine, FXJSOBJTYPE eObjType) { + ObjDefnID = + pEngine->DefineObj("Report", eObjType, JSConstructor<CJS_Report, Report>, + JSDestructor<CJS_Report>); + DefineMethods(pEngine, ObjDefnID, MethodSpecs); +} + +Report::Report(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} + +Report::~Report() {} + +CJS_Return Report::writeText(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} + +CJS_Return Report::save(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + // Unsafe, not supported. + return CJS_Return(true); +} diff --git a/fxjs/cjs_report.h b/fxjs/cjs_report.h new file mode 100644 index 0000000000..50259ef56f --- /dev/null +++ b/fxjs/cjs_report.h @@ -0,0 +1,41 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_REPORT_H_ +#define FXJS_CJS_REPORT_H_ + +#include <vector> + +#include "fxjs/JS_Define.h" + +class Report : public CJS_EmbedObj { + public: + explicit Report(CJS_Object* pJSObject); + ~Report() override; + + public: + CJS_Return save(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return writeText(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); +}; + +class CJS_Report : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine, FXJSOBJTYPE eObjType); + + explicit CJS_Report(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Report() override {} + + JS_STATIC_METHOD(save, Report) + JS_STATIC_METHOD(writeText, Report); + + private: + static int ObjDefnID; + static const JSMethodSpec MethodSpecs[]; +}; + +#endif // FXJS_CJS_REPORT_H_ diff --git a/fxjs/cjs_return.cpp b/fxjs/cjs_return.cpp new file mode 100644 index 0000000000..adfd1c5c9f --- /dev/null +++ b/fxjs/cjs_return.cpp @@ -0,0 +1,18 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_return.h" + +CJS_Return::CJS_Return(bool result) : is_error_(!result) {} + +CJS_Return::CJS_Return(const WideString& err) : is_error_(true), error_(err) {} + +CJS_Return::CJS_Return(v8::Local<v8::Value> ret) + : is_error_(false), return_(ret) {} + +CJS_Return::CJS_Return(const CJS_Return&) = default; + +CJS_Return::~CJS_Return() = default; diff --git a/fxjs/cjs_return.h b/fxjs/cjs_return.h new file mode 100644 index 0000000000..61c5778cda --- /dev/null +++ b/fxjs/cjs_return.h @@ -0,0 +1,34 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_RETURN_H_ +#define FXJS_CJS_RETURN_H_ + +#include "fxjs/fxjs_v8.h" + +class CJS_Return { + public: + explicit CJS_Return(bool); + explicit CJS_Return(const WideString&); + explicit CJS_Return(v8::Local<v8::Value>); + CJS_Return(const CJS_Return&); + ~CJS_Return(); + + bool HasError() const { return is_error_; } + WideString Error() const { return error_; } + + bool HasReturn() const { return !return_.IsEmpty(); } + v8::Local<v8::Value> Return() const { return return_; } + + private: + CJS_Return() = delete; + + bool is_error_ = false; + WideString error_; + v8::Local<v8::Value> return_; +}; + +#endif // FXJS_CJS_RETURN_H_ diff --git a/fxjs/cjs_runtime.cpp b/fxjs/cjs_runtime.cpp new file mode 100644 index 0000000000..6043debc44 --- /dev/null +++ b/fxjs/cjs_runtime.cpp @@ -0,0 +1,292 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_runtime.h" + +#include <algorithm> + +#include "fpdfsdk/cpdfsdk_formfillenvironment.h" +#include "fxjs/JS_Define.h" +#include "fxjs/JS_GlobalData.h" +#include "fxjs/cjs_annot.h" +#include "fxjs/cjs_app.h" +#include "fxjs/cjs_border.h" +#include "fxjs/cjs_color.h" +#include "fxjs/cjs_console.h" +#include "fxjs/cjs_display.h" +#include "fxjs/cjs_document.h" +#include "fxjs/cjs_event.h" +#include "fxjs/cjs_event_context.h" +#include "fxjs/cjs_eventhandler.h" +#include "fxjs/cjs_field.h" +#include "fxjs/cjs_font.h" +#include "fxjs/cjs_global.h" +#include "fxjs/cjs_globalarrays.h" +#include "fxjs/cjs_globalconsts.h" +#include "fxjs/cjs_highlight.h" +#include "fxjs/cjs_icon.h" +#include "fxjs/cjs_object.h" +#include "fxjs/cjs_position.h" +#include "fxjs/cjs_printparamsobj.h" +#include "fxjs/cjs_publicmethods.h" +#include "fxjs/cjs_report.h" +#include "fxjs/cjs_scalehow.h" +#include "fxjs/cjs_scalewhen.h" +#include "fxjs/cjs_style.h" +#include "fxjs/cjs_timerobj.h" +#include "fxjs/cjs_util.h" +#include "fxjs/cjs_zoomtype.h" +#include "public/fpdf_formfill.h" +#include "third_party/base/stl_util.h" + +#ifdef PDF_ENABLE_XFA +#include "fxjs/cfxjse_value.h" +#endif // PDF_ENABLE_XFA + +// static +void IJS_Runtime::Initialize(unsigned int slot, void* isolate) { + FXJS_Initialize(slot, reinterpret_cast<v8::Isolate*>(isolate)); +} + +// static +void IJS_Runtime::Destroy() { + FXJS_Release(); +} + +// static +IJS_Runtime* IJS_Runtime::Create(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + return new CJS_Runtime(pFormFillEnv); +} + +// static +CJS_Runtime* CJS_Runtime::CurrentRuntimeFromIsolate(v8::Isolate* pIsolate) { + return static_cast<CJS_Runtime*>( + CFXJS_Engine::CurrentEngineFromIsolate(pIsolate)); +} + +CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv) + : m_pFormFillEnv(pFormFillEnv), + m_bBlocking(false), + m_isolateManaged(false) { + v8::Isolate* pIsolate = nullptr; + + IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform; + if (pPlatform->version <= 2) { + unsigned int embedderDataSlot = 0; + v8::Isolate* pExternalIsolate = nullptr; + if (pPlatform->version == 2) { + pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); + embedderDataSlot = pPlatform->m_v8EmbedderSlot; + } + FXJS_Initialize(embedderDataSlot, pExternalIsolate); + } + m_isolateManaged = FXJS_GetIsolate(&pIsolate); + SetIsolate(pIsolate); + +#ifdef PDF_ENABLE_XFA + v8::Isolate::Scope isolate_scope(pIsolate); + v8::HandleScope handle_scope(pIsolate); +#endif + + if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) + DefineJSObjects(); + + IJS_EventContext* pContext = NewEventContext(); + InitializeEngine(); + ReleaseEventContext(pContext); + SetFormFillEnvToDocument(); +} + +CJS_Runtime::~CJS_Runtime() { + NotifyObservedPtrs(); + ReleaseEngine(); + if (m_isolateManaged) { + GetIsolate()->Dispose(); + SetIsolate(nullptr); + } +} + +void CJS_Runtime::DefineJSObjects() { + v8::Isolate::Scope isolate_scope(GetIsolate()); + v8::HandleScope handle_scope(GetIsolate()); + v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); + v8::Context::Scope context_scope(context); + + // The call order determines the "ObjDefID" assigned to each class. + // ObjDefIDs 0 - 2 + CJS_Border::DefineJSObjects(this); + CJS_Display::DefineJSObjects(this); + CJS_Font::DefineJSObjects(this); + + // ObjDefIDs 3 - 5 + CJS_Highlight::DefineJSObjects(this); + CJS_Position::DefineJSObjects(this); + CJS_ScaleHow::DefineJSObjects(this); + + // ObjDefIDs 6 - 8 + CJS_ScaleWhen::DefineJSObjects(this); + CJS_Style::DefineJSObjects(this); + CJS_Zoomtype::DefineJSObjects(this); + + // ObjDefIDs 9 - 11 + CJS_App::DefineJSObjects(this); + CJS_Color::DefineJSObjects(this); + CJS_Console::DefineJSObjects(this); + + // ObjDefIDs 12 - 14 + CJS_Document::DefineJSObjects(this); + CJS_Event::DefineJSObjects(this); + CJS_Field::DefineJSObjects(this); + + // ObjDefIDs 15 - 17 + CJS_Global::DefineJSObjects(this); + CJS_Icon::DefineJSObjects(this); + CJS_Util::DefineJSObjects(this); + + // ObjDefIDs 18 - 20 (these can't fail, return void). + CJS_PublicMethods::DefineJSObjects(this); + CJS_GlobalConsts::DefineJSObjects(this); + CJS_GlobalArrays::DefineJSObjects(this); + + // ObjDefIDs 21 - 23. + CJS_TimerObj::DefineJSObjects(this); + CJS_PrintParamsObj::DefineJSObjects(this); + CJS_Annot::DefineJSObjects(this); +} + +IJS_EventContext* CJS_Runtime::NewEventContext() { + m_EventContextArray.push_back(pdfium::MakeUnique<CJS_EventContext>(this)); + return m_EventContextArray.back().get(); +} + +void CJS_Runtime::ReleaseEventContext(IJS_EventContext* pContext) { + auto it = std::find(m_EventContextArray.begin(), m_EventContextArray.end(), + pdfium::FakeUniquePtr<CJS_EventContext>( + static_cast<CJS_EventContext*>(pContext))); + if (it != m_EventContextArray.end()) + m_EventContextArray.erase(it); +} + +CJS_EventContext* CJS_Runtime::GetCurrentEventContext() const { + return m_EventContextArray.empty() ? nullptr + : m_EventContextArray.back().get(); +} + +void CJS_Runtime::SetFormFillEnvToDocument() { + v8::Isolate::Scope isolate_scope(GetIsolate()); + v8::HandleScope handle_scope(GetIsolate()); + v8::Local<v8::Context> context = NewLocalContext(); + v8::Context::Scope context_scope(context); + + v8::Local<v8::Object> pThis = GetThisObj(); + if (pThis.IsEmpty()) + return; + + if (CFXJS_Engine::GetObjDefnID(pThis) != CJS_Document::GetObjDefnID()) + return; + + CJS_Document* pJSDocument = + static_cast<CJS_Document*>(GetObjectPrivate(pThis)); + if (!pJSDocument) + return; + + Document* pDocument = static_cast<Document*>(pJSDocument->GetEmbedObject()); + if (!pDocument) + return; + + pDocument->SetFormFillEnv(m_pFormFillEnv.Get()); +} + +CPDFSDK_FormFillEnvironment* CJS_Runtime::GetFormFillEnv() const { + return m_pFormFillEnv.Get(); +} + +int CJS_Runtime::ExecuteScript(const WideString& script, WideString* info) { + FXJSErr error = {}; + int nRet = Execute(script, &error); + if (nRet < 0) { + info->Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline, + error.message); + } + return nRet; +} + +bool CJS_Runtime::AddEventToSet(const FieldEvent& event) { + return m_FieldEventSet.insert(event).second; +} + +void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) { + m_FieldEventSet.erase(event); +} + +#ifdef PDF_ENABLE_XFA +WideString ChangeObjName(const WideString& str) { + WideString sRet = str; + sRet.Replace(L"_", L"."); + return sRet; +} + +bool CJS_Runtime::GetValueByName(const ByteStringView& utf8Name, + CFXJSE_Value* pValue) { + v8::Isolate::Scope isolate_scope(GetIsolate()); + v8::HandleScope handle_scope(GetIsolate()); + v8::Local<v8::Context> context = NewLocalContext(); + v8::Context::Scope context_scope(context); + v8::Local<v8::Value> propvalue = context->Global()->Get( + v8::String::NewFromUtf8(GetIsolate(), utf8Name.unterminated_c_str(), + v8::String::kNormalString, utf8Name.GetLength())); + if (propvalue.IsEmpty()) { + pValue->SetUndefined(); + return false; + } + pValue->ForceSetValue(propvalue); + return true; +} + +bool CJS_Runtime::SetValueByName(const ByteStringView& utf8Name, + CFXJSE_Value* pValue) { + if (utf8Name.IsEmpty() || !pValue) + return false; + + v8::Isolate* pIsolate = GetIsolate(); + v8::Isolate::Scope isolate_scope(pIsolate); + v8::HandleScope handle_scope(pIsolate); + v8::Local<v8::Context> context = NewLocalContext(); + v8::Context::Scope context_scope(context); + v8::Local<v8::Value> propvalue = + v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); + context->Global()->Set( + v8::String::NewFromUtf8(pIsolate, utf8Name.unterminated_c_str(), + v8::String::kNormalString, utf8Name.GetLength()), + propvalue); + return true; +} +#endif + +v8::Local<v8::Value> CJS_Runtime::MaybeCoerceToNumber( + v8::Local<v8::Value> value) { + bool bAllowNaN = false; + if (value->IsString()) { + ByteString bstr = ByteString::FromUnicode(ToWideString(value)); + if (bstr.GetLength() == 0) + return value; + if (bstr == "NaN") + bAllowNaN = true; + } + + v8::Isolate* pIsolate = GetIsolate(); + v8::TryCatch try_catch(pIsolate); + v8::MaybeLocal<v8::Number> maybeNum = + value->ToNumber(pIsolate->GetCurrentContext()); + if (maybeNum.IsEmpty()) + return value; + + v8::Local<v8::Number> num = maybeNum.ToLocalChecked(); + if (std::isnan(num->Value()) && !bAllowNaN) + return value; + + return num; +} diff --git a/fxjs/cjs_runtime.h b/fxjs/cjs_runtime.h new file mode 100644 index 0000000000..7ecc0d5ce4 --- /dev/null +++ b/fxjs/cjs_runtime.h @@ -0,0 +1,73 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_RUNTIME_H_ +#define FXJS_CJS_RUNTIME_H_ + +#include <map> +#include <memory> +#include <set> +#include <utility> +#include <vector> + +#include "core/fxcrt/observable.h" +#include "fpdfsdk/cpdfsdk_formfillenvironment.h" +#include "fxjs/cjs_eventhandler.h" +#include "fxjs/fxjs_v8.h" +#include "fxjs/ijs_runtime.h" + +class CJS_EventContext; + +class CJS_Runtime : public IJS_Runtime, + public CFXJS_Engine, + public Observable<CJS_Runtime> { + public: + using FieldEvent = std::pair<WideString, JS_EVENT_T>; + + static CJS_Runtime* CurrentRuntimeFromIsolate(v8::Isolate* pIsolate); + + explicit CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv); + ~CJS_Runtime() override; + + // IJS_Runtime + IJS_EventContext* NewEventContext() override; + void ReleaseEventContext(IJS_EventContext* pContext) override; + CPDFSDK_FormFillEnvironment* GetFormFillEnv() const override; + int ExecuteScript(const WideString& script, WideString* info) override; + + CJS_EventContext* GetCurrentEventContext() const; + + // Returns true if the event isn't already found in the set. + bool AddEventToSet(const FieldEvent& event); + void RemoveEventFromSet(const FieldEvent& event); + + void BeginBlock() { m_bBlocking = true; } + void EndBlock() { m_bBlocking = false; } + bool IsBlocking() const { return m_bBlocking; } + + // Attempt to convert the |value| into a number. If successful the number + // value will be returned, otherwise |value| is returned. + v8::Local<v8::Value> MaybeCoerceToNumber(v8::Local<v8::Value> value); + +#ifdef PDF_ENABLE_XFA + bool GetValueByName(const ByteStringView& utf8Name, + CFXJSE_Value* pValue) override; + bool SetValueByName(const ByteStringView& utf8Name, + CFXJSE_Value* pValue) override; +#endif // PDF_ENABLE_XFA + + private: + void DefineJSObjects(); + void SetFormFillEnvToDocument(); + + std::vector<std::unique_ptr<CJS_EventContext>> m_EventContextArray; + CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv; + bool m_bBlocking; + bool m_isolateManaged; + std::set<FieldEvent> m_FieldEventSet; +}; + +#endif // FXJS_CJS_RUNTIME_H_ diff --git a/fxjs/cjs_runtimestub.cpp b/fxjs/cjs_runtimestub.cpp new file mode 100644 index 0000000000..919672832d --- /dev/null +++ b/fxjs/cjs_runtimestub.cpp @@ -0,0 +1,60 @@ +// Copyright 2015 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include <memory> + +#include "core/fxcrt/unowned_ptr.h" +#include "fxjs/cjs_event_context_stub.h" +#include "fxjs/ijs_runtime.h" +#include "third_party/base/ptr_util.h" + +class CJS_RuntimeStub final : public IJS_Runtime { + public: + explicit CJS_RuntimeStub(CPDFSDK_FormFillEnvironment* pFormFillEnv) + : m_pFormFillEnv(pFormFillEnv) {} + ~CJS_RuntimeStub() override {} + + IJS_EventContext* NewEventContext() override { + if (!m_pContext) + m_pContext = pdfium::MakeUnique<CJS_EventContextStub>(); + return m_pContext.get(); + } + + void ReleaseEventContext(IJS_EventContext* pContext) override {} + + CPDFSDK_FormFillEnvironment* GetFormFillEnv() const override { + return m_pFormFillEnv.Get(); + } + +#ifdef PDF_ENABLE_XFA + bool GetValueByName(const ByteStringView&, CFXJSE_Value*) override { + return false; + } + + bool SetValueByName(const ByteStringView&, CFXJSE_Value*) override { + return false; + } +#endif // PDF_ENABLE_XFA + + int ExecuteScript(const WideString& script, WideString* info) override { + return 0; + } + + protected: + UnownedPtr<CPDFSDK_FormFillEnvironment> const m_pFormFillEnv; + std::unique_ptr<CJS_EventContextStub> m_pContext; +}; + +// static +void IJS_Runtime::Initialize(unsigned int slot, void* isolate) {} + +// static +void IJS_Runtime::Destroy() {} + +// static +IJS_Runtime* IJS_Runtime::Create(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + return new CJS_RuntimeStub(pFormFillEnv); +} diff --git a/fxjs/cjs_scalehow.cpp b/fxjs/cjs_scalehow.cpp new file mode 100644 index 0000000000..35c5ec22bf --- /dev/null +++ b/fxjs/cjs_scalehow.cpp @@ -0,0 +1,21 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_scalehow.h" + +const JSConstSpec CJS_ScaleHow::ConstSpecs[] = { + {"proportional", JSConstSpec::Number, 0, 0}, + {"anamorphic", JSConstSpec::Number, 1, 0}, + {0, JSConstSpec::Number, 0, 0}}; + +int CJS_ScaleHow::ObjDefnID = -1; + +// static +void CJS_ScaleHow::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("scaleHow", FXJSOBJTYPE_STATIC, nullptr, nullptr); + DefineConsts(pEngine, ObjDefnID, ConstSpecs); +} diff --git a/fxjs/cjs_scalehow.h b/fxjs/cjs_scalehow.h new file mode 100644 index 0000000000..8177a3eabb --- /dev/null +++ b/fxjs/cjs_scalehow.h @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_SCALEHOW_H_ +#define FXJS_CJS_SCALEHOW_H_ + +#include "fxjs/JS_Define.h" + +class CJS_ScaleHow : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_ScaleHow(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_ScaleHow() override {} + + private: + static int ObjDefnID; + static const JSConstSpec ConstSpecs[]; +}; + +#endif // FXJS_CJS_SCALEHOW_H_ diff --git a/fxjs/cjs_scalewhen.cpp b/fxjs/cjs_scalewhen.cpp new file mode 100644 index 0000000000..b13e7ed928 --- /dev/null +++ b/fxjs/cjs_scalewhen.cpp @@ -0,0 +1,23 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_scalewhen.h" + +const JSConstSpec CJS_ScaleWhen::ConstSpecs[] = { + {"always", JSConstSpec::Number, 0, 0}, + {"never", JSConstSpec::Number, 1, 0}, + {"tooBig", JSConstSpec::Number, 2, 0}, + {"tooSmall", JSConstSpec::Number, 3, 0}, + {0, JSConstSpec::Number, 0, 0}}; + +int CJS_ScaleWhen::ObjDefnID = -1; + +// static +void CJS_ScaleWhen::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("scaleWhen", FXJSOBJTYPE_STATIC, nullptr, nullptr); + DefineConsts(pEngine, ObjDefnID, ConstSpecs); +} diff --git a/fxjs/cjs_scalewhen.h b/fxjs/cjs_scalewhen.h new file mode 100644 index 0000000000..e97a285119 --- /dev/null +++ b/fxjs/cjs_scalewhen.h @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_SCALEWHEN_H_ +#define FXJS_CJS_SCALEWHEN_H_ + +#include "fxjs/JS_Define.h" + +class CJS_ScaleWhen : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_ScaleWhen(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_ScaleWhen() override {} + + private: + static int ObjDefnID; + static const JSConstSpec ConstSpecs[]; +}; + +#endif // FXJS_CJS_SCALEWHEN_H_ diff --git a/fxjs/cjs_style.cpp b/fxjs/cjs_style.cpp new file mode 100644 index 0000000000..7458b2a18b --- /dev/null +++ b/fxjs/cjs_style.cpp @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_style.h" + +const JSConstSpec CJS_Style::ConstSpecs[] = { + {"ch", JSConstSpec::String, 0, "check"}, + {"cr", JSConstSpec::String, 0, "cross"}, + {"di", JSConstSpec::String, 0, "diamond"}, + {"ci", JSConstSpec::String, 0, "circle"}, + {"st", JSConstSpec::String, 0, "star"}, + {"sq", JSConstSpec::String, 0, "square"}, + {0, JSConstSpec::Number, 0, 0}}; + +int CJS_Style::ObjDefnID = -1; + +// static +void CJS_Style::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = pEngine->DefineObj("style", FXJSOBJTYPE_STATIC, nullptr, nullptr); + DefineConsts(pEngine, ObjDefnID, ConstSpecs); +} diff --git a/fxjs/cjs_style.h b/fxjs/cjs_style.h new file mode 100644 index 0000000000..46ab66b617 --- /dev/null +++ b/fxjs/cjs_style.h @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_STYLE_H_ +#define FXJS_CJS_STYLE_H_ + +#include "fxjs/JS_Define.h" + +class CJS_Style : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Style(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Style() override {} + + private: + static int ObjDefnID; + static const JSConstSpec ConstSpecs[]; +}; + +#endif // FXJS_CJS_STYLE_H_ diff --git a/fxjs/cjs_timerobj.cpp b/fxjs/cjs_timerobj.cpp new file mode 100644 index 0000000000..410ad0846e --- /dev/null +++ b/fxjs/cjs_timerobj.cpp @@ -0,0 +1,32 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_timerobj.h" + +#include "fxjs/global_timer.h" + +int CJS_TimerObj::ObjDefnID = -1; + +// static +int CJS_TimerObj::GetObjDefnID() { + return ObjDefnID; +} + +// static +void CJS_TimerObj::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = pEngine->DefineObj("TimerObj", FXJSOBJTYPE_DYNAMIC, + JSConstructor<CJS_TimerObj, TimerObj>, + JSDestructor<CJS_TimerObj>); +} + +TimerObj::TimerObj(CJS_Object* pJSObject) + : CJS_EmbedObj(pJSObject), m_nTimerID(0) {} + +TimerObj::~TimerObj() {} + +void TimerObj::SetTimer(GlobalTimer* pTimer) { + m_nTimerID = pTimer->GetTimerID(); +} diff --git a/fxjs/cjs_timerobj.h b/fxjs/cjs_timerobj.h new file mode 100644 index 0000000000..be095558e2 --- /dev/null +++ b/fxjs/cjs_timerobj.h @@ -0,0 +1,38 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_TIMEROBJ_H_ +#define FXJS_CJS_TIMEROBJ_H_ + +#include "fxjs/JS_Define.h" + +class GlobalTimer; + +class TimerObj : public CJS_EmbedObj { + public: + explicit TimerObj(CJS_Object* pJSObject); + ~TimerObj() override; + + void SetTimer(GlobalTimer* pTimer); + int GetTimerID() const { return m_nTimerID; } + + private: + int m_nTimerID; // Weak reference to GlobalTimer through global map. +}; + +class CJS_TimerObj : public CJS_Object { + public: + static int GetObjDefnID(); + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_TimerObj(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_TimerObj() override {} + + private: + static int ObjDefnID; +}; + +#endif // FXJS_CJS_TIMEROBJ_H_ diff --git a/fxjs/cjs_util.cpp b/fxjs/cjs_util.cpp new file mode 100644 index 0000000000..7a80700e7d --- /dev/null +++ b/fxjs/cjs_util.cpp @@ -0,0 +1,468 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_util.h" + +#include <time.h> + +#include <algorithm> +#include <cmath> +#include <cwctype> +#include <string> +#include <vector> + +#include "core/fxcrt/fx_extension.h" +#include "fxjs/JS_Define.h" +#include "fxjs/cjs_event_context.h" +#include "fxjs/cjs_eventhandler.h" +#include "fxjs/cjs_object.h" +#include "fxjs/cjs_publicmethods.h" +#include "fxjs/cjs_runtime.h" +#include "fxjs/js_resources.h" + +#if _FX_OS_ == _FX_OS_ANDROID_ +#include <ctype.h> +#endif + +namespace { + +// Map PDF-style directives to equivalent wcsftime directives. Not +// all have direct equivalents, though. +struct TbConvert { + const wchar_t* lpszJSMark; + const wchar_t* lpszCppMark; +}; + +// Map PDF-style directives lacking direct wcsftime directives to +// the value with which they will be replaced. +struct TbConvertAdditional { + const wchar_t* lpszJSMark; + int iValue; +}; + +const TbConvert TbConvertTable[] = { + {L"mmmm", L"%B"}, {L"mmm", L"%b"}, {L"mm", L"%m"}, {L"dddd", L"%A"}, + {L"ddd", L"%a"}, {L"dd", L"%d"}, {L"yyyy", L"%Y"}, {L"yy", L"%y"}, + {L"HH", L"%H"}, {L"hh", L"%I"}, {L"MM", L"%M"}, {L"ss", L"%S"}, + {L"TT", L"%p"}, +#if defined(_WIN32) + {L"tt", L"%p"}, {L"h", L"%#I"}, +#else + {L"tt", L"%P"}, {L"h", L"%l"}, +#endif +}; + +} // namespace + +const JSMethodSpec CJS_Util::MethodSpecs[] = { + {"printd", printd_static}, {"printf", printf_static}, + {"printx", printx_static}, {"scand", scand_static}, + {"byteToChar", byteToChar_static}, {0, 0}}; + +int CJS_Util::ObjDefnID = -1; + +// static +void CJS_Util::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("util", FXJSOBJTYPE_STATIC, + JSConstructor<CJS_Util, util>, JSDestructor<CJS_Util>); + DefineMethods(pEngine, ObjDefnID, MethodSpecs); +} + +util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} + +util::~util() {} + +CJS_Return util::printf(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + const size_t iSize = params.size(); + if (iSize < 1) + return CJS_Return(false); + + std::wstring unsafe_fmt_string(pRuntime->ToWideString(params[0]).c_str()); + std::vector<std::wstring> unsafe_conversion_specifiers; + int iOffset = 0; + int iOffend = 0; + unsafe_fmt_string.insert(unsafe_fmt_string.begin(), L'S'); + while (iOffset != -1) { + iOffend = unsafe_fmt_string.find(L"%", iOffset + 1); + std::wstring strSub; + if (iOffend == -1) + strSub = unsafe_fmt_string.substr(iOffset); + else + strSub = unsafe_fmt_string.substr(iOffset, iOffend - iOffset); + unsafe_conversion_specifiers.push_back(strSub); + iOffset = iOffend; + } + + std::wstring c_strResult; + for (size_t iIndex = 0; iIndex < unsafe_conversion_specifiers.size(); + ++iIndex) { + std::wstring c_strFormat = unsafe_conversion_specifiers[iIndex]; + if (iIndex == 0) { + c_strResult = c_strFormat; + continue; + } + + if (iIndex >= iSize) { + c_strResult += c_strFormat; + continue; + } + + WideString strSegment; + switch (ParseDataType(&c_strFormat)) { + case UTIL_INT: + strSegment.Format(c_strFormat.c_str(), + pRuntime->ToInt32(params[iIndex])); + break; + case UTIL_DOUBLE: + strSegment.Format(c_strFormat.c_str(), + pRuntime->ToDouble(params[iIndex])); + break; + case UTIL_STRING: + strSegment.Format(c_strFormat.c_str(), + pRuntime->ToWideString(params[iIndex]).c_str()); + break; + default: + strSegment.Format(L"%ls", c_strFormat.c_str()); + break; + } + c_strResult += strSegment.c_str(); + } + + c_strResult.erase(c_strResult.begin()); + return CJS_Return(pRuntime->NewString(c_strResult.c_str())); +} + +CJS_Return util::printd(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + const size_t iSize = params.size(); + if (iSize < 2) + return CJS_Return(false); + + if (params[1].IsEmpty() || !params[1]->IsDate()) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPRINT1)); + + v8::Local<v8::Date> v8_date = params[1].As<v8::Date>(); + if (v8_date.IsEmpty() || std::isnan(pRuntime->ToDouble(v8_date))) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPRINT2)); + + double date = JS_LocalTime(pRuntime->ToDouble(v8_date)); + int year = JS_GetYearFromTime(date); + int month = JS_GetMonthFromTime(date) + 1; // One-based. + int day = JS_GetDayFromTime(date); + int hour = JS_GetHourFromTime(date); + int min = JS_GetMinFromTime(date); + int sec = JS_GetSecFromTime(date); + + if (params[0]->IsNumber()) { + WideString swResult; + switch (pRuntime->ToInt32(params[0])) { + case 0: + swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", year, month, day, hour, + min, sec); + break; + case 1: + swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", year, month, day, + hour, min, sec); + break; + case 2: + swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", year, month, day, + hour, min, sec); + break; + default: + return CJS_Return(JSGetStringFromID(IDS_STRING_JSVALUEERROR)); + } + + return CJS_Return(pRuntime->NewString(swResult.c_str())); + } + + if (params[0]->IsString()) { + // We don't support XFAPicture at the moment. + if (iSize > 2 && pRuntime->ToBoolean(params[2])) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOTSUPPORT)); + + // Convert PDF-style format specifiers to wcsftime specifiers. Remove any + // pre-existing %-directives before inserting our own. + std::basic_string<wchar_t> cFormat = + pRuntime->ToWideString(params[0]).c_str(); + cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'), + cFormat.end()); + + for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) { + int iStart = 0; + int iEnd; + while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) != + -1) { + cFormat.replace(iEnd, wcslen(TbConvertTable[i].lpszJSMark), + TbConvertTable[i].lpszCppMark); + iStart = iEnd; + } + } + + if (year < 0) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSVALUEERROR)); + + static const TbConvertAdditional cTableAd[] = { + {L"m", month}, {L"d", day}, + {L"H", hour}, {L"h", hour > 12 ? hour - 12 : hour}, + {L"M", min}, {L"s", sec}, + }; + + for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) { + WideString sValue; + sValue.Format(L"%d", cTableAd[i].iValue); + + int iStart = 0; + int iEnd; + while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) { + if (iEnd > 0) { + if (cFormat[iEnd - 1] == L'%') { + iStart = iEnd + 1; + continue; + } + } + cFormat.replace(iEnd, wcslen(cTableAd[i].lpszJSMark), sValue.c_str()); + iStart = iEnd; + } + } + + struct tm time = {}; + time.tm_year = year - 1900; + time.tm_mon = month - 1; + time.tm_mday = day; + time.tm_hour = hour; + time.tm_min = min; + time.tm_sec = sec; + + wchar_t buf[64] = {}; + FXSYS_wcsftime(buf, 64, cFormat.c_str(), &time); + cFormat = buf; + return CJS_Return(pRuntime->NewString(cFormat.c_str())); + } + + return CJS_Return(JSGetStringFromID(IDS_STRING_JSTYPEERROR)); +} + +CJS_Return util::printx(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() < 2) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + return CJS_Return( + pRuntime->NewString(printx(pRuntime->ToWideString(params[0]), + pRuntime->ToWideString(params[1])) + .c_str())); +} + +enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; + +static wchar_t TranslateCase(wchar_t input, CaseMode eMode) { + if (eMode == kLowerCase && FXSYS_isupper(input)) + return input | 0x20; + if (eMode == kUpperCase && FXSYS_islower(input)) + return input & ~0x20; + return input; +} + +WideString util::printx(const WideString& wsFormat, + const WideString& wsSource) { + WideString wsResult; + size_t iSourceIdx = 0; + size_t iFormatIdx = 0; + CaseMode eCaseMode = kPreserveCase; + bool bEscaped = false; + while (iFormatIdx < wsFormat.GetLength()) { + if (bEscaped) { + bEscaped = false; + wsResult += wsFormat[iFormatIdx]; + ++iFormatIdx; + continue; + } + switch (wsFormat[iFormatIdx]) { + case '\\': { + bEscaped = true; + ++iFormatIdx; + } break; + case '<': { + eCaseMode = kLowerCase; + ++iFormatIdx; + } break; + case '>': { + eCaseMode = kUpperCase; + ++iFormatIdx; + } break; + case '=': { + eCaseMode = kPreserveCase; + ++iFormatIdx; + } break; + case '?': { + if (iSourceIdx < wsSource.GetLength()) { + wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); + ++iSourceIdx; + } + ++iFormatIdx; + } break; + case 'X': { + if (iSourceIdx < wsSource.GetLength()) { + if (FXSYS_iswalnum(wsSource[iSourceIdx])) { + wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); + ++iFormatIdx; + } + ++iSourceIdx; + } else { + ++iFormatIdx; + } + } break; + case 'A': { + if (iSourceIdx < wsSource.GetLength()) { + if (FXSYS_iswalpha(wsSource[iSourceIdx])) { + wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); + ++iFormatIdx; + } + ++iSourceIdx; + } else { + ++iFormatIdx; + } + } break; + case '9': { + if (iSourceIdx < wsSource.GetLength()) { + if (std::iswdigit(wsSource[iSourceIdx])) { + wsResult += wsSource[iSourceIdx]; + ++iFormatIdx; + } + ++iSourceIdx; + } else { + ++iFormatIdx; + } + } break; + case '*': { + if (iSourceIdx < wsSource.GetLength()) { + wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); + ++iSourceIdx; + } else { + ++iFormatIdx; + } + } break; + default: { + wsResult += wsFormat[iFormatIdx]; + ++iFormatIdx; + } break; + } + } + return wsResult; +} + +CJS_Return util::scand(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() < 2) + return CJS_Return(false); + + WideString sFormat = pRuntime->ToWideString(params[0]); + WideString sDate = pRuntime->ToWideString(params[1]); + double dDate = JS_GetDateTime(); + if (sDate.GetLength() > 0) + dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); + + if (std::isnan(dDate)) + return CJS_Return(pRuntime->NewUndefined()); + return CJS_Return(pRuntime->NewDate(dDate)); +} + +CJS_Return util::byteToChar(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params) { + if (params.size() < 1) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + int arg = pRuntime->ToInt32(params[0]); + if (arg < 0 || arg > 255) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSVALUEERROR)); + + WideString wStr(static_cast<wchar_t>(arg)); + return CJS_Return(pRuntime->NewString(wStr.c_str())); +} + +// Ensure that sFormat contains at most one well-understood printf formatting +// directive which is safe to use with a single argument, and return the type +// of argument expected, or -1 otherwise. If -1 is returned, it is NOT safe +// to use sFormat with printf() and it must be copied byte-by-byte. +int util::ParseDataType(std::wstring* sFormat) { + enum State { BEFORE, FLAGS, WIDTH, PRECISION, SPECIFIER, AFTER }; + + int result = -1; + State state = BEFORE; + size_t precision_digits = 0; + size_t i = 0; + while (i < sFormat->length()) { + wchar_t c = (*sFormat)[i]; + switch (state) { + case BEFORE: + if (c == L'%') + state = FLAGS; + break; + case FLAGS: + if (c == L'+' || c == L'-' || c == L'#' || c == L' ') { + // Stay in same state. + } else { + state = WIDTH; + continue; // Re-process same character. + } + break; + case WIDTH: + if (c == L'*') + return -1; + if (std::iswdigit(c)) { + // Stay in same state. + } else if (c == L'.') { + state = PRECISION; + } else { + state = SPECIFIER; + continue; // Re-process same character. + } + break; + case PRECISION: + if (c == L'*') + return -1; + if (std::iswdigit(c)) { + // Stay in same state. + ++precision_digits; + } else { + state = SPECIFIER; + continue; // Re-process same character. + } + break; + case SPECIFIER: + if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' || + c == L'u' || c == L'x' || c == L'X') { + result = UTIL_INT; + } else if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || + c == L'G') { + result = UTIL_DOUBLE; + } else if (c == L's' || c == L'S') { + // Map s to S since we always deal internally with wchar_t strings. + // TODO(tsepez): Probably 100% borked. %S is not a standard + // conversion. + (*sFormat)[i] = L'S'; + result = UTIL_STRING; + } else { + return -1; + } + state = AFTER; + break; + case AFTER: + if (c == L'%') + return -1; + // Stay in same state until string exhausted. + break; + } + ++i; + } + // See https://crbug.com/740166 + if (result == UTIL_INT && precision_digits > 2) + return -1; + + return result; +} diff --git a/fxjs/cjs_util.h b/fxjs/cjs_util.h new file mode 100644 index 0000000000..cc2026dfc7 --- /dev/null +++ b/fxjs/cjs_util.h @@ -0,0 +1,63 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_UTIL_H_ +#define FXJS_CJS_UTIL_H_ + +#include <string> +#include <vector> + +#include "fxjs/JS_Define.h" + +// Return values for ParseDataType() below. +#define UTIL_INT 0 +#define UTIL_DOUBLE 1 +#define UTIL_STRING 2 + +class util : public CJS_EmbedObj { + public: + explicit util(CJS_Object* pJSObject); + ~util() override; + + CJS_Return printd(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return printf(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return printx(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return scand(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + CJS_Return byteToChar(CJS_Runtime* pRuntime, + const std::vector<v8::Local<v8::Value>>& params); + + static WideString printx(const WideString& cFormat, + const WideString& cSource); + + private: + friend class CJS_Util_ParseDataType_Test; + + static int ParseDataType(std::wstring* sFormat); +}; + +class CJS_Util : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Util(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Util() override {} + + JS_STATIC_METHOD(printd, util); + JS_STATIC_METHOD(printf, util); + JS_STATIC_METHOD(printx, util); + JS_STATIC_METHOD(scand, util); + JS_STATIC_METHOD(byteToChar, util); + + private: + static int ObjDefnID; + static const JSMethodSpec MethodSpecs[]; +}; + +#endif // FXJS_CJS_UTIL_H_ diff --git a/fxjs/cjs_util_unittest.cpp b/fxjs/cjs_util_unittest.cpp new file mode 100644 index 0000000000..6f43f0fcb6 --- /dev/null +++ b/fxjs/cjs_util_unittest.cpp @@ -0,0 +1,113 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "fxjs/cjs_util.h" + +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/test_support.h" + +TEST(CJS_Util, ParseDataType) { + struct ParseDataTypeCase { + const wchar_t* const input_string; + const int expected; + }; + + // Commented out tests follow the spec but are not passing. + const ParseDataTypeCase cases[] = { + // Not conversions + {L"", -1}, + {L"d", -1}, + + // Simple cases + {L"%d", UTIL_INT}, + {L"%x", UTIL_INT}, + {L"%f", UTIL_DOUBLE}, + {L"%s", UTIL_STRING}, + + // nDecSep Not implemented + // {L"%,0d", UTIL_INT}, + // {L"%,1d", UTIL_INT}, + // {L"%,2d", UTIL_INT}, + // {L"%,3d", UTIL_INT}, + // {L"%,4d", -1}, + // {L"%,d", -1}, + + // cFlags("+ 0#"") are only valid for numeric conversions. + {L"%+d", UTIL_INT}, + {L"%+x", UTIL_INT}, + {L"%+f", UTIL_DOUBLE}, + // {L"%+s", -1}, + {L"% d", UTIL_INT}, + {L"% x", UTIL_INT}, + {L"% f", UTIL_DOUBLE}, + // {L"% s", -1}, + {L"%0d", UTIL_INT}, + {L"%0x", UTIL_INT}, + {L"%0f", UTIL_DOUBLE}, + // {L"%0s", -1}, + {L"%#d", UTIL_INT}, + {L"%#x", UTIL_INT}, + {L"%#f", UTIL_DOUBLE}, + // {L"%#s", -1}, + + // nWidth should work. for all conversions, can be combined with cFlags=0 + // for numbers. + {L"%5d", UTIL_INT}, + {L"%05d", UTIL_INT}, + {L"%5x", UTIL_INT}, + {L"%05x", UTIL_INT}, + {L"%5f", UTIL_DOUBLE}, + {L"%05f", UTIL_DOUBLE}, + {L"%5s", UTIL_STRING}, + // {L"%05s", -1}, + + // nPrecision should only work for float + // {L"%.5d", -1}, + // {L"%.5x", -1}, + {L"%.5f", UTIL_DOUBLE}, + // {L"%.5s", -1}, + // {L"%.14d", -1}, + // {L"%.14x", -1}, + {L"%.14f", UTIL_DOUBLE}, + // {L"%.14s", -1}, + // {L"%.f", -1}, + + // See https://crbug.com/740166 + // nPrecision too large (> 260) causes crashes in Windows. + // Avoid this by limiting to two digits + {L"%.1d", UTIL_INT}, + {L"%.10d", UTIL_INT}, + {L"%.100d", -1}, + + // Unexpected characters + {L"%ad", -1}, + {L"%bx", -1}, + // {L"%cf", -1}, + // {L"%es", -1}, + // {L"%gd", -1}, + {L"%hx", -1}, + // {L"%if", -1}, + {L"%js", -1}, + {L"%@d", -1}, + {L"%~x", -1}, + {L"%[f", -1}, + {L"%\0s", -1}, + {L"%\nd", -1}, + {L"%\rx", -1}, + // {L"%%f", -1}, + // {L"% s", -1}, + + // Combine multiple valid components + {L"%+6d", UTIL_INT}, + {L"% 7x", UTIL_INT}, + {L"%#9.3f", UTIL_DOUBLE}, + {L"%10s", UTIL_STRING}, + }; + + for (size_t i = 0; i < FX_ArraySize(cases); i++) { + std::wstring input(cases[i].input_string); + EXPECT_EQ(cases[i].expected, util::ParseDataType(&input)) + << cases[i].input_string; + } +} diff --git a/fxjs/cjs_zoomtype.cpp b/fxjs/cjs_zoomtype.cpp new file mode 100644 index 0000000000..1cca58e56d --- /dev/null +++ b/fxjs/cjs_zoomtype.cpp @@ -0,0 +1,26 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/cjs_zoomtype.h" + +const JSConstSpec CJS_Zoomtype::ConstSpecs[] = { + {"none", JSConstSpec::String, 0, "NoVary"}, + {"fitP", JSConstSpec::String, 0, "FitPage"}, + {"fitW", JSConstSpec::String, 0, "FitWidth"}, + {"fitH", JSConstSpec::String, 0, "FitHeight"}, + {"fitV", JSConstSpec::String, 0, "FitVisibleWidth"}, + {"pref", JSConstSpec::String, 0, "Preferred"}, + {"refW", JSConstSpec::String, 0, "ReflowWidth"}, + {0, JSConstSpec::Number, 0, 0}}; + +int CJS_Zoomtype::ObjDefnID = -1; + +// static +void CJS_Zoomtype::DefineJSObjects(CFXJS_Engine* pEngine) { + ObjDefnID = + pEngine->DefineObj("zoomtype", FXJSOBJTYPE_STATIC, nullptr, nullptr); + DefineConsts(pEngine, ObjDefnID, ConstSpecs); +} diff --git a/fxjs/cjs_zoomtype.h b/fxjs/cjs_zoomtype.h new file mode 100644 index 0000000000..14db0a4c96 --- /dev/null +++ b/fxjs/cjs_zoomtype.h @@ -0,0 +1,24 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJS_ZOOMTYPE_H_ +#define FXJS_CJS_ZOOMTYPE_H_ + +#include "fxjs/JS_Define.h" + +class CJS_Zoomtype : public CJS_Object { + public: + static void DefineJSObjects(CFXJS_Engine* pEngine); + + explicit CJS_Zoomtype(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {} + ~CJS_Zoomtype() override {} + + private: + static int ObjDefnID; + static const JSConstSpec ConstSpecs[]; +}; + +#endif // FXJS_CJS_ZOOMTYPE_H_ diff --git a/fxjs/global_timer.cpp b/fxjs/global_timer.cpp new file mode 100644 index 0000000000..6f7f09c673 --- /dev/null +++ b/fxjs/global_timer.cpp @@ -0,0 +1,80 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/global_timer.h" + +GlobalTimer::GlobalTimer(app* pObj, + CPDFSDK_FormFillEnvironment* pFormFillEnv, + CJS_Runtime* pRuntime, + int nType, + const WideString& script, + uint32_t dwElapse, + uint32_t dwTimeOut) + : m_nTimerID(0), + m_pEmbedObj(pObj), + m_bProcessing(false), + m_nType(nType), + m_dwTimeOut(dwTimeOut), + m_swJScript(script), + m_pRuntime(pRuntime), + m_pFormFillEnv(pFormFillEnv) { + CFX_SystemHandler* pHandler = m_pFormFillEnv->GetSysHandler(); + m_nTimerID = pHandler->SetTimer(dwElapse, Trigger); + if (m_nTimerID) + (*GetGlobalTimerMap())[m_nTimerID] = this; +} + +GlobalTimer::~GlobalTimer() { + if (!m_nTimerID) + return; + + if (GetRuntime()) + m_pFormFillEnv->GetSysHandler()->KillTimer(m_nTimerID); + + GetGlobalTimerMap()->erase(m_nTimerID); +} + +// static +void GlobalTimer::Trigger(int nTimerID) { + auto it = GetGlobalTimerMap()->find(nTimerID); + if (it == GetGlobalTimerMap()->end()) + return; + + GlobalTimer* pTimer = it->second; + if (pTimer->m_bProcessing) + return; + + pTimer->m_bProcessing = true; + if (pTimer->m_pEmbedObj) + pTimer->m_pEmbedObj->TimerProc(pTimer); + + // Timer proc may have destroyed timer, find it again. + it = GetGlobalTimerMap()->find(nTimerID); + if (it == GetGlobalTimerMap()->end()) + return; + + pTimer = it->second; + pTimer->m_bProcessing = false; + if (pTimer->IsOneShot()) + pTimer->m_pEmbedObj->CancelProc(pTimer); +} + +// static +void GlobalTimer::Cancel(int nTimerID) { + auto it = GetGlobalTimerMap()->find(nTimerID); + if (it == GetGlobalTimerMap()->end()) + return; + + GlobalTimer* pTimer = it->second; + pTimer->m_pEmbedObj->CancelProc(pTimer); +} + +// static +GlobalTimer::TimerMap* GlobalTimer::GetGlobalTimerMap() { + // Leak the timer array at shutdown. + static auto* s_TimerMap = new TimerMap; + return s_TimerMap; +} diff --git a/fxjs/global_timer.h b/fxjs/global_timer.h new file mode 100644 index 0000000000..ec8806b9a1 --- /dev/null +++ b/fxjs/global_timer.h @@ -0,0 +1,50 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_GLOBAL_TIMER_H_ +#define FXJS_GLOBAL_TIMER_H_ + +#include <map> + +#include "fxjs/cjs_app.h" + +class GlobalTimer { + public: + GlobalTimer(app* pObj, + CPDFSDK_FormFillEnvironment* pFormFillEnv, + CJS_Runtime* pRuntime, + int nType, + const WideString& script, + uint32_t dwElapse, + uint32_t dwTimeOut); + ~GlobalTimer(); + + static void Trigger(int nTimerID); + static void Cancel(int nTimerID); + + bool IsOneShot() const { return m_nType == 1; } + uint32_t GetTimeOut() const { return m_dwTimeOut; } + int GetTimerID() const { return m_nTimerID; } + CJS_Runtime* GetRuntime() const { return m_pRuntime.Get(); } + WideString GetJScript() const { return m_swJScript; } + + private: + using TimerMap = std::map<uint32_t, GlobalTimer*>; + static TimerMap* GetGlobalTimerMap(); + + uint32_t m_nTimerID; + app* const m_pEmbedObj; + bool m_bProcessing; + + // data + const int m_nType; // 0:Interval; 1:TimeOut + const uint32_t m_dwTimeOut; + const WideString m_swJScript; + CJS_Runtime::ObservedPtr m_pRuntime; + CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv; +}; + +#endif // FXJS_GLOBAL_TIMER_H_ diff --git a/fxjs/ijs_event_context.h b/fxjs/ijs_event_context.h new file mode 100644 index 0000000000..9b8dd8eabe --- /dev/null +++ b/fxjs/ijs_event_context.h @@ -0,0 +1,133 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_IJS_EVENT_CONTEXT_H_ +#define FXJS_IJS_EVENT_CONTEXT_H_ + +#include "core/fxcrt/fx_string.h" +#include "core/fxcrt/fx_system.h" + +class CPDF_Bookmark; +class CPDF_FormField; +class CPDFSDK_Annot; +class CPDFSDK_FormFillEnvironment; + +// Records the details of an event and triggers JS execution for it. There +// can be more than one of these at any given time, as JS callbacks to C++ +// may trigger new events on top of one another. +class IJS_EventContext { + public: + virtual bool RunScript(const WideString& script, WideString* info) = 0; + + virtual void OnApp_Init() = 0; + + virtual void OnDoc_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString& strTargetName) = 0; + virtual void OnDoc_WillPrint(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + virtual void OnDoc_DidPrint(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + virtual void OnDoc_WillSave(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + virtual void OnDoc_DidSave(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + virtual void OnDoc_WillClose(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + + virtual void OnPage_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + virtual void OnPage_Close(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + virtual void OnPage_InView(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + virtual void OnPage_OutView(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + + virtual void OnField_MouseDown(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) = 0; + virtual void OnField_MouseEnter(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) = 0; + virtual void OnField_MouseExit(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) = 0; + virtual void OnField_MouseUp(bool bModifier, + bool bShift, + CPDF_FormField* pTarget) = 0; + virtual void OnField_Focus(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value) = 0; + virtual void OnField_Blur(bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + const WideString& Value) = 0; + + virtual void OnField_Calculate(CPDF_FormField* pSource, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc) = 0; + virtual void OnField_Format(CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit) = 0; + virtual void OnField_Keystroke(WideString& strChange, + const WideString& strChangeEx, + bool KeyDown, + bool bModifier, + int& nSelEnd, + int& nSelStart, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool bWillCommit, + bool bFieldFull, + bool& bRc) = 0; + virtual void OnField_Validate(WideString& strChange, + const WideString& strChangeEx, + bool bKeyDown, + bool bModifier, + bool bShift, + CPDF_FormField* pTarget, + WideString& Value, + bool& bRc) = 0; + + virtual void OnScreen_Focus(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) = 0; + virtual void OnScreen_Blur(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) = 0; + virtual void OnScreen_Open(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) = 0; + virtual void OnScreen_Close(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) = 0; + virtual void OnScreen_MouseDown(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) = 0; + virtual void OnScreen_MouseUp(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) = 0; + virtual void OnScreen_MouseEnter(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) = 0; + virtual void OnScreen_MouseExit(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) = 0; + virtual void OnScreen_InView(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) = 0; + virtual void OnScreen_OutView(bool bModifier, + bool bShift, + CPDFSDK_Annot* pScreen) = 0; + + virtual void OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) = 0; + virtual void OnLink_MouseUp(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + + virtual void OnMenu_Exec(CPDFSDK_FormFillEnvironment* pFormFillEnv, + const WideString&) = 0; + virtual void OnBatchExec(CPDFSDK_FormFillEnvironment* pFormFillEnv) = 0; + virtual void OnConsole_Exec() = 0; + virtual void OnExternal_Exec() = 0; + + protected: + virtual ~IJS_EventContext() {} +}; + +#endif // FXJS_IJS_EVENT_CONTEXT_H_ diff --git a/fxjs/ijs_runtime.h b/fxjs/ijs_runtime.h new file mode 100644 index 0000000000..e649aada0c --- /dev/null +++ b/fxjs/ijs_runtime.h @@ -0,0 +1,44 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_IJS_RUNTIME_H_ +#define FXJS_IJS_RUNTIME_H_ + +#include "core/fxcrt/fx_string.h" +#include "core/fxcrt/fx_system.h" + +#ifdef PDF_ENABLE_XFA +#include "fxjs/fxjse.h" +#endif // PDF_ENABLE_XFA + +class CPDFSDK_FormFillEnvironment; +class IJS_EventContext; + +// Owns the FJXS objects needed to actually execute JS. +class IJS_Runtime { + public: + static void Initialize(unsigned int slot, void* isolate); + static void Destroy(); + static IJS_Runtime* Create(CPDFSDK_FormFillEnvironment* pFormFillEnv); + virtual ~IJS_Runtime() {} + + virtual IJS_EventContext* NewEventContext() = 0; + virtual void ReleaseEventContext(IJS_EventContext* pContext) = 0; + virtual CPDFSDK_FormFillEnvironment* GetFormFillEnv() const = 0; + virtual int ExecuteScript(const WideString& script, WideString* info) = 0; + +#ifdef PDF_ENABLE_XFA + virtual bool GetValueByName(const ByteStringView& utf8Name, + CFXJSE_Value* pValue) = 0; + virtual bool SetValueByName(const ByteStringView& utf8Name, + CFXJSE_Value* pValue) = 0; +#endif // PDF_ENABLE_XFA + + protected: + IJS_Runtime() {} +}; + +#endif // FXJS_IJS_RUNTIME_H_ diff --git a/fxjs/js_resources.cpp b/fxjs/js_resources.cpp new file mode 100644 index 0000000000..c7ed06f274 --- /dev/null +++ b/fxjs/js_resources.cpp @@ -0,0 +1,68 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/js_resources.h" + +WideString JSGetStringFromID(uint32_t id) { + switch (id) { + case IDS_STRING_JSALERT: + return L"Alert"; + case IDS_STRING_JSPARAMERROR: + return L"Incorrect number of parameters passed to function."; + case IDS_STRING_JSAFNUMBER_KEYSTROKE: + return L"The input value is invalid."; + case IDS_STRING_JSPARAM_TOOLONG: + return L"The input value is too long."; + case IDS_STRING_JSPARSEDATE: + return L"The input value can't be parsed as a valid date/time (%s)."; + case IDS_STRING_JSRANGE1: + return L"The input value must be greater than or equal to %s" + L" and less than or equal to %s."; + case IDS_STRING_JSRANGE2: + return L"The input value must be greater than or equal to %s."; + case IDS_STRING_JSRANGE3: + return L"The input value must be less than or equal to %s."; + case IDS_STRING_JSNOTSUPPORT: + return L"Operation not supported."; + case IDS_STRING_JSBUSY: + return L"System is busy."; + case IDS_STRING_JSEVENT: + return L"Duplicate formfield event found."; + case IDS_STRING_RUN: + return L"Script ran successfully."; + case IDS_STRING_JSPRINT1: + return L"The second parameter can't be converted to a Date."; + case IDS_STRING_JSPRINT2: + return L"The second parameter is an invalid Date!"; + case IDS_STRING_JSNOGLOBAL: + return L"Global value not found."; + case IDS_STRING_JSREADONLY: + return L"Cannot assign to readonly property."; + case IDS_STRING_JSTYPEERROR: + return L"Incorrect parameter type."; + case IDS_STRING_JSVALUEERROR: + return L"Incorrect parameter value."; + case IDS_STRING_JSNOPERMISSION: + return L"Permission denied."; + case IDS_STRING_JSBADOBJECT: + return L"Object no longer exists."; + default: + return L""; + } +} + +WideString JSFormatErrorString(const char* class_name, + const char* property_name, + const WideString& details) { + WideString result = WideString::FromLocal(class_name); + if (property_name) { + result += L"."; + result += WideString::FromLocal(property_name); + } + result += L": "; + result += details; + return result; +} diff --git a/fxjs/js_resources.h b/fxjs/js_resources.h new file mode 100644 index 0000000000..9f655487ba --- /dev/null +++ b/fxjs/js_resources.h @@ -0,0 +1,38 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_JS_RESOURCES_H_ +#define FXJS_JS_RESOURCES_H_ + +#include "core/fxcrt/widestring.h" + +#define IDS_STRING_JSALERT 25613 +#define IDS_STRING_JSPARAMERROR 25614 +#define IDS_STRING_JSAFNUMBER_KEYSTROKE 25615 +#define IDS_STRING_JSPARAM_TOOLONG 25617 +#define IDS_STRING_JSPARSEDATE 25618 +#define IDS_STRING_JSRANGE1 25619 +#define IDS_STRING_JSRANGE2 25620 +#define IDS_STRING_JSRANGE3 25621 +#define IDS_STRING_JSNOTSUPPORT 25627 +#define IDS_STRING_JSBUSY 25628 +#define IDS_STRING_JSEVENT 25629 +#define IDS_STRING_RUN 25630 +#define IDS_STRING_JSPRINT1 25632 +#define IDS_STRING_JSPRINT2 25633 +#define IDS_STRING_JSNOGLOBAL 25635 +#define IDS_STRING_JSREADONLY 25636 +#define IDS_STRING_JSTYPEERROR 25637 +#define IDS_STRING_JSVALUEERROR 25638 +#define IDS_STRING_JSNOPERMISSION 25639 +#define IDS_STRING_JSBADOBJECT 25640 + +WideString JSGetStringFromID(uint32_t id); +WideString JSFormatErrorString(const char* class_name, + const char* property_name, + const WideString& details); + +#endif // FXJS_JS_RESOURCES_H_ |