Consts.cpp revision 33357cad1fd1321a2b38d2963e2585f27ce980a2
1// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "fpdfsdk/javascript/Consts.h"
8
9#include "fpdfsdk/javascript/JS_Define.h"
10#include "fpdfsdk/javascript/JS_Object.h"
11#include "fpdfsdk/javascript/JS_Value.h"
12
13JSConstSpec CJS_Border::ConstSpecs[] = {
14    {"s", JSConstSpec::String, 0, "solid"},
15    {"b", JSConstSpec::String, 0, "beveled"},
16    {"d", JSConstSpec::String, 0, "dashed"},
17    {"i", JSConstSpec::String, 0, "inset"},
18    {"u", JSConstSpec::String, 0, "underline"},
19    {0, JSConstSpec::Number, 0, 0}};
20IMPLEMENT_JS_CLASS_CONST(CJS_Border, border)
21
22JSConstSpec CJS_Display::ConstSpecs[] = {{"visible", JSConstSpec::Number, 0, 0},
23                                         {"hidden", JSConstSpec::Number, 1, 0},
24                                         {"noPrint", JSConstSpec::Number, 2, 0},
25                                         {"noView", JSConstSpec::Number, 3, 0},
26                                         {0, JSConstSpec::Number, 0, 0}};
27IMPLEMENT_JS_CLASS_CONST(CJS_Display, display)
28
29JSConstSpec CJS_Font::ConstSpecs[] = {
30    {"Times", JSConstSpec::String, 0, "Times-Roman"},
31    {"TimesB", JSConstSpec::String, 0, "Times-Bold"},
32    {"TimesI", JSConstSpec::String, 0, "Times-Italic"},
33    {"TimesBI", JSConstSpec::String, 0, "Times-BoldItalic"},
34    {"Helv", JSConstSpec::String, 0, "Helvetica"},
35    {"HelvB", JSConstSpec::String, 0, "Helvetica-Bold"},
36    {"HelvI", JSConstSpec::String, 0, "Helvetica-Oblique"},
37    {"HelvBI", JSConstSpec::String, 0, "Helvetica-BoldOblique"},
38    {"Cour", JSConstSpec::String, 0, "Courier"},
39    {"CourB", JSConstSpec::String, 0, "Courier-Bold"},
40    {"CourI", JSConstSpec::String, 0, "Courier-Oblique"},
41    {"CourBI", JSConstSpec::String, 0, "Courier-BoldOblique"},
42    {"Symbol", JSConstSpec::String, 0, "Symbol"},
43    {"ZapfD", JSConstSpec::String, 0, "ZapfDingbats"},
44    {0, JSConstSpec::Number, 0, 0}};
45IMPLEMENT_JS_CLASS_CONST(CJS_Font, font)
46
47JSConstSpec CJS_Highlight::ConstSpecs[] = {
48    {"n", JSConstSpec::String, 0, "none"},
49    {"i", JSConstSpec::String, 0, "invert"},
50    {"p", JSConstSpec::String, 0, "push"},
51    {"o", JSConstSpec::String, 0, "outline"},
52    {0, JSConstSpec::Number, 0, 0}};
53IMPLEMENT_JS_CLASS_CONST(CJS_Highlight, highlight)
54
55JSConstSpec CJS_Position::ConstSpecs[] = {
56    {"textOnly", JSConstSpec::Number, 0, 0},
57    {"iconOnly", JSConstSpec::Number, 1, 0},
58    {"iconTextV", JSConstSpec::Number, 2, 0},
59    {"textIconV", JSConstSpec::Number, 3, 0},
60    {"iconTextH", JSConstSpec::Number, 4, 0},
61    {"textIconH", JSConstSpec::Number, 5, 0},
62    {"overlay", JSConstSpec::Number, 6, 0},
63    {0, JSConstSpec::Number, 0, 0}};
64IMPLEMENT_JS_CLASS_CONST(CJS_Position, position)
65
66JSConstSpec CJS_ScaleHow::ConstSpecs[] = {
67    {"proportional", JSConstSpec::Number, 0, 0},
68    {"anamorphic", JSConstSpec::Number, 1, 0},
69    {0, JSConstSpec::Number, 0, 0}};
70IMPLEMENT_JS_CLASS_CONST(CJS_ScaleHow, scaleHow)
71
72JSConstSpec CJS_ScaleWhen::ConstSpecs[] = {
73    {"always", JSConstSpec::Number, 0, 0},
74    {"never", JSConstSpec::Number, 1, 0},
75    {"tooBig", JSConstSpec::Number, 2, 0},
76    {"tooSmall", JSConstSpec::Number, 3, 0},
77    {0, JSConstSpec::Number, 0, 0}};
78IMPLEMENT_JS_CLASS_CONST(CJS_ScaleWhen, scaleWhen)
79
80JSConstSpec CJS_Style::ConstSpecs[] = {
81    {"ch", JSConstSpec::String, 0, "check"},
82    {"cr", JSConstSpec::String, 0, "cross"},
83    {"di", JSConstSpec::String, 0, "diamond"},
84    {"ci", JSConstSpec::String, 0, "circle"},
85    {"st", JSConstSpec::String, 0, "star"},
86    {"sq", JSConstSpec::String, 0, "square"},
87    {0, JSConstSpec::Number, 0, 0}};
88IMPLEMENT_JS_CLASS_CONST(CJS_Style, style)
89
90JSConstSpec CJS_Zoomtype::ConstSpecs[] = {
91    {"none", JSConstSpec::String, 0, "NoVary"},
92    {"fitP", JSConstSpec::String, 0, "FitPage"},
93    {"fitW", JSConstSpec::String, 0, "FitWidth"},
94    {"fitH", JSConstSpec::String, 0, "FitHeight"},
95    {"fitV", JSConstSpec::String, 0, "FitVisibleWidth"},
96    {"pref", JSConstSpec::String, 0, "Preferred"},
97    {"refW", JSConstSpec::String, 0, "ReflowWidth"},
98    {0, JSConstSpec::Number, 0, 0}};
99IMPLEMENT_JS_CLASS_CONST(CJS_Zoomtype, zoomtype)
100
101#define GLOBAL_STRING(rt, name, value)                                \
102  (rt)->DefineGlobalConst(                                            \
103      (name), [](const v8::FunctionCallbackInfo<v8::Value>& info) {   \
104        info.GetReturnValue().Set(                                    \
105            CFXJS_Engine::CurrentEngineFromIsolate(info.GetIsolate()) \
106                ->NewString(value));                                  \
107      })
108
109void CJS_GlobalConsts::DefineJSObjects(CJS_Runtime* pRuntime) {
110  GLOBAL_STRING(pRuntime, L"IDS_GREATER_THAN",
111                L"Invalid value: must be greater than or equal to % s.");
112
113  GLOBAL_STRING(pRuntime, L"IDS_GT_AND_LT",
114                L"Invalid value: must be greater than or equal to % s "
115                L"and less than or equal to % s.");
116
117  GLOBAL_STRING(pRuntime, L"IDS_LESS_THAN",
118                L"Invalid value: must be less than or equal to % s.");
119
120  GLOBAL_STRING(pRuntime, L"IDS_INVALID_MONTH", L"**Invalid**");
121  GLOBAL_STRING(
122      pRuntime, L"IDS_INVALID_DATE",
123      L"Invalid date / time: please ensure that the date / time exists.Field");
124
125  GLOBAL_STRING(pRuntime, L"IDS_INVALID_VALUE",
126                L"The value entered does not match the format of the field");
127
128  GLOBAL_STRING(pRuntime, L"IDS_AM", L"am");
129  GLOBAL_STRING(pRuntime, L"IDS_PM", L"pm");
130  GLOBAL_STRING(pRuntime, L"IDS_MONTH_INFO",
131                L"January[1] February[2] March[3] April[4] May[5] "
132                L"June[6] July[7] August[8] September[9] October[10] "
133                L"November[11] December[12] Sept[9] Jan[1] Feb[2] Mar[3] "
134                L"Apr[4] Jun[6] Jul[7] Aug[8] Sep[9] Oct[10] Nov[11] "
135                L"Dec[12]");
136
137  GLOBAL_STRING(pRuntime, L"IDS_STARTUP_CONSOLE_MSG", L"** ^ _ ^ **");
138}
139
140#define GLOBAL_ARRAY(rt, name, ...)                                          \
141  {                                                                          \
142    const FX_WCHAR* values[] = {__VA_ARGS__};                                \
143    v8::Local<v8::Array> array = (rt)->NewArray();                           \
144    for (size_t i = 0; i < FX_ArraySize(values); ++i)                        \
145      array->Set(i, (rt)->NewString(values[i]));                             \
146    (rt)->SetConstArray((name), array);                                      \
147    (rt)->DefineGlobalConst(                                                 \
148        (name), [](const v8::FunctionCallbackInfo<v8::Value>& info) {        \
149          CJS_Runtime* pCurrentRuntime =                                     \
150              CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate());     \
151          if (pCurrentRuntime)                                               \
152            info.GetReturnValue().Set(pCurrentRuntime->GetConstArray(name)); \
153        });                                                                  \
154  }
155
156void CJS_GlobalArrays::DefineJSObjects(CJS_Runtime* pRuntime) {
157  GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_ENTRY_DOT_SEP", L"[+-]?\\d*\\.?\\d*");
158  GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_COMMIT_DOT_SEP",
159               L"[+-]?\\d+(\\.\\d+)?",  // -1.0 or -1
160               L"[+-]?\\.\\d+",         // -.1
161               L"[+-]?\\d+\\.");        // -1.
162
163  GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_ENTRY_COMMA_SEP", L"[+-]?\\d*,?\\d*");
164  GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_COMMIT_COMMA_SEP",
165               L"[+-]?\\d+([.,]\\d+)?",  // -1,0 or -1
166               L"[+-]?[.,]\\d+",         // -,1
167               L"[+-]?\\d+[.,]");        // -1,
168
169  GLOBAL_ARRAY(pRuntime, L"RE_ZIP_ENTRY", L"\\d{0,5}");
170  GLOBAL_ARRAY(pRuntime, L"RE_ZIP_COMMIT", L"\\d{5}");
171  GLOBAL_ARRAY(pRuntime, L"RE_ZIP4_ENTRY", L"\\d{0,5}(\\.|[- ])?\\d{0,4}");
172  GLOBAL_ARRAY(pRuntime, L"RE_ZIP4_COMMIT", L"\\d{5}(\\.|[- ])?\\d{4}");
173  GLOBAL_ARRAY(pRuntime, L"RE_PHONE_ENTRY",
174               // 555-1234 or 408 555-1234
175               L"\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
176
177               // (408
178               L"\\(\\d{0,3}",
179
180               // (408) 555-1234
181               // (allow the addition of parens as an afterthought)
182               L"\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
183
184               // (408 555-1234
185               L"\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
186
187               // 408) 555-1234
188               L"\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
189
190               // international
191               L"011(\\.|[- \\d])*");
192
193  GLOBAL_ARRAY(
194      pRuntime, L"RE_PHONE_COMMIT", L"\\d{3}(\\.|[- ])?\\d{4}",  // 555-1234
195      L"\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}",               // 408 555-1234
196      L"\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}",  // (408) 555-1234
197      L"011(\\.|[- \\d])*");                              // international
198
199  GLOBAL_ARRAY(pRuntime, L"RE_SSN_ENTRY",
200               L"\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}");
201
202  GLOBAL_ARRAY(pRuntime, L"RE_SSN_COMMIT",
203               L"\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}");
204}
205