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 "Consts.h"
8
9 #include "JS_Define.h"
10 #include "JS_Object.h"
11 #include "JS_Value.h"
12 #include "fpdfsdk/include/javascript/IJavaScript.h"
13
14 /* ------------------------------ border ------------------------------ */
15
16 BEGIN_JS_STATIC_CONST(CJS_Border)
17 JS_STATIC_CONST_ENTRY_STRING(L"s", L"solid")
18 JS_STATIC_CONST_ENTRY_STRING(L"b", L"beveled")
19 JS_STATIC_CONST_ENTRY_STRING(L"d", L"dashed")
20 JS_STATIC_CONST_ENTRY_STRING(L"i", L"inset")
21 JS_STATIC_CONST_ENTRY_STRING(L"u", L"underline")
END_JS_STATIC_CONST()22 END_JS_STATIC_CONST()
23
24 IMPLEMENT_JS_CLASS_CONST(CJS_Border, border)
25
26 /* ------------------------------ display ------------------------------ */
27
28 BEGIN_JS_STATIC_CONST(CJS_Display)
29 JS_STATIC_CONST_ENTRY_NUMBER(L"visible", 0)
30 JS_STATIC_CONST_ENTRY_NUMBER(L"hidden", 1)
31 JS_STATIC_CONST_ENTRY_NUMBER(L"noPrint", 2)
32 JS_STATIC_CONST_ENTRY_NUMBER(L"noView", 3)
33 END_JS_STATIC_CONST()
34
35 IMPLEMENT_JS_CLASS_CONST(CJS_Display, display)
36
37 /* ------------------------------ font ------------------------------ */
38
39 BEGIN_JS_STATIC_CONST(CJS_Font)
40 JS_STATIC_CONST_ENTRY_STRING(L"Times", L"Times-Roman")
41 JS_STATIC_CONST_ENTRY_STRING(L"TimesB", L"Times-Bold")
42 JS_STATIC_CONST_ENTRY_STRING(L"TimesI", L"Times-Italic")
43 JS_STATIC_CONST_ENTRY_STRING(L"TimesBI", L"Times-BoldItalic")
44 JS_STATIC_CONST_ENTRY_STRING(L"Helv", L"Helvetica")
45 JS_STATIC_CONST_ENTRY_STRING(L"HelvB", L"Helvetica-Bold")
46 JS_STATIC_CONST_ENTRY_STRING(L"HelvI", L"Helvetica-Oblique")
47 JS_STATIC_CONST_ENTRY_STRING(L"HelvBI", L"Helvetica-BoldOblique")
48 JS_STATIC_CONST_ENTRY_STRING(L"Cour", L"Courier")
49 JS_STATIC_CONST_ENTRY_STRING(L"CourB", L"Courier-Bold")
50 JS_STATIC_CONST_ENTRY_STRING(L"CourI", L"Courier-Oblique")
51 JS_STATIC_CONST_ENTRY_STRING(L"CourBI", L"Courier-BoldOblique")
52 JS_STATIC_CONST_ENTRY_STRING(L"Symbol", L"Symbol")
53 JS_STATIC_CONST_ENTRY_STRING(L"ZapfD", L"ZapfDingbats")
54 END_JS_STATIC_CONST()
55
56 IMPLEMENT_JS_CLASS_CONST(CJS_Font, font)
57
58 /* ------------------------------ highlight ------------------------------ */
59
60 BEGIN_JS_STATIC_CONST(CJS_Highlight)
61 JS_STATIC_CONST_ENTRY_STRING(L"n", L"none")
62 JS_STATIC_CONST_ENTRY_STRING(L"i", L"invert")
63 JS_STATIC_CONST_ENTRY_STRING(L"p", L"push")
64 JS_STATIC_CONST_ENTRY_STRING(L"o", L"outline")
65 END_JS_STATIC_CONST()
66
67 IMPLEMENT_JS_CLASS_CONST(CJS_Highlight, highlight)
68
69 /* ------------------------------ position ------------------------------ */
70
71 BEGIN_JS_STATIC_CONST(CJS_Position)
72 JS_STATIC_CONST_ENTRY_NUMBER(L"textOnly", 0)
73 JS_STATIC_CONST_ENTRY_NUMBER(L"iconOnly", 1)
74 JS_STATIC_CONST_ENTRY_NUMBER(L"iconTextV", 2)
75 JS_STATIC_CONST_ENTRY_NUMBER(L"textIconV", 3)
76 JS_STATIC_CONST_ENTRY_NUMBER(L"iconTextH", 4)
77 JS_STATIC_CONST_ENTRY_NUMBER(L"textIconH", 5)
78 JS_STATIC_CONST_ENTRY_NUMBER(L"overlay", 6)
79 END_JS_STATIC_CONST()
80
81 IMPLEMENT_JS_CLASS_CONST(CJS_Position, position)
82
83 /* ------------------------------ scaleHow ------------------------------ */
84
85 BEGIN_JS_STATIC_CONST(CJS_ScaleHow)
86 JS_STATIC_CONST_ENTRY_NUMBER(L"proportional", 0)
87 JS_STATIC_CONST_ENTRY_NUMBER(L"anamorphic", 1)
88 END_JS_STATIC_CONST()
89
90 IMPLEMENT_JS_CLASS_CONST(CJS_ScaleHow, scaleHow)
91
92 /* ------------------------------ scaleWhen ------------------------------ */
93
94 BEGIN_JS_STATIC_CONST(CJS_ScaleWhen)
95 JS_STATIC_CONST_ENTRY_NUMBER(L"always", 0)
96 JS_STATIC_CONST_ENTRY_NUMBER(L"never", 1)
97 JS_STATIC_CONST_ENTRY_NUMBER(L"tooBig", 2)
98 JS_STATIC_CONST_ENTRY_NUMBER(L"tooSmall", 3)
99 END_JS_STATIC_CONST()
100
101 IMPLEMENT_JS_CLASS_CONST(CJS_ScaleWhen, scaleWhen)
102
103 /* ------------------------------ style ------------------------------ */
104
105 BEGIN_JS_STATIC_CONST(CJS_Style)
106 JS_STATIC_CONST_ENTRY_STRING(L"ch", L"check")
107 JS_STATIC_CONST_ENTRY_STRING(L"cr", L"cross")
108 JS_STATIC_CONST_ENTRY_STRING(L"di", L"diamond")
109 JS_STATIC_CONST_ENTRY_STRING(L"ci", L"circle")
110 JS_STATIC_CONST_ENTRY_STRING(L"st", L"star")
111 JS_STATIC_CONST_ENTRY_STRING(L"sq", L"square")
112 END_JS_STATIC_CONST()
113
114 IMPLEMENT_JS_CLASS_CONST(CJS_Style, style)
115
116 /* ------------------------------ zoomtype ------------------------------ */
117
118 BEGIN_JS_STATIC_CONST(CJS_Zoomtype)
119 JS_STATIC_CONST_ENTRY_STRING(L"none", L"NoVary")
120 JS_STATIC_CONST_ENTRY_STRING(L"fitP", L"FitPage")
121 JS_STATIC_CONST_ENTRY_STRING(L"fitW", L"FitWidth")
122 JS_STATIC_CONST_ENTRY_STRING(L"fitH", L"FitHeight")
123 JS_STATIC_CONST_ENTRY_STRING(L"fitV", L"FitVisibleWidth")
124 JS_STATIC_CONST_ENTRY_STRING(L"pref", L"Preferred")
125 JS_STATIC_CONST_ENTRY_STRING(L"refW", L"ReflowWidth")
126 END_JS_STATIC_CONST()
127
128 IMPLEMENT_JS_CLASS_CONST(CJS_Zoomtype, zoomtype)
129
130 /* ------------------------------ CJS_GlobalConsts ------------------------- */
131
132 static void DefineGlobalConstString(CJS_Runtime* pRuntime,
133 const wchar_t* pConstName,
134 const wchar_t* pValue) {
135 FXJS_DefineGlobalConst(pRuntime->GetIsolate(), pConstName,
136 FXJS_NewString(pRuntime->GetIsolate(), pValue));
137 }
138
DefineJSObjects(CJS_Runtime * pRuntime)139 void CJS_GlobalConsts::DefineJSObjects(CJS_Runtime* pRuntime) {
140 DefineGlobalConstString(
141 pRuntime, L"IDS_GREATER_THAN",
142 L"Invalid value: must be greater than or equal to % s.");
143 DefineGlobalConstString(
144 pRuntime, L"IDS_GT_AND_LT",
145 L"Invalid value: must be greater than or equal to % s "
146 L"and less than or equal to % s.");
147 DefineGlobalConstString(pRuntime, L"IDS_LESS_THAN",
148 L"Invalid value: must be less than or equal to % s.");
149 DefineGlobalConstString(pRuntime, L"IDS_INVALID_MONTH", L"**Invalid**");
150 DefineGlobalConstString(
151 pRuntime, L"IDS_INVALID_DATE",
152 L"Invalid date / time: please ensure that the date / time exists.Field");
153 DefineGlobalConstString(
154 pRuntime, L"IDS_INVALID_VALUE",
155 L"The value entered does not match the format of the field");
156 DefineGlobalConstString(pRuntime, L"IDS_AM", L"am");
157 DefineGlobalConstString(pRuntime, L"IDS_PM", L"pm");
158 DefineGlobalConstString(
159 pRuntime, L"IDS_MONTH_INFO",
160 L"January[1] February[2] March[3] April[4] May[5] "
161 L"June[6] July[7] August[8] September[9] October[10] "
162 L"November[11] December[12] Sept[9] Jan[1] Feb[2] Mar[3] "
163 L"Apr[4] Jun[6] Jul[7] Aug[8] Sep[9] Oct[10] Nov[11] "
164 L"Dec[12]");
165 DefineGlobalConstString(pRuntime, L"IDS_STARTUP_CONSOLE_MSG", L"** ^ _ ^ **");
166 }
167
168 /* ------------------------------ CJS_GlobalArrays ------------------------ */
169
DefineGlobalConstStringArray(CJS_Runtime * pRuntime,const wchar_t * sConstName,const wchar_t ** pValues,size_t nValues)170 void DefineGlobalConstStringArray(CJS_Runtime* pRuntime,
171 const wchar_t* sConstName,
172 const wchar_t** pValues,
173 size_t nValues) {
174 CJS_Array array(pRuntime);
175 for (size_t i = 0; i < nValues; ++i) {
176 array.SetElement(i, CJS_Value(pRuntime, pValues[i]));
177 }
178 CJS_PropValue prop(pRuntime);
179 prop << array;
180 FXJS_DefineGlobalConst(pRuntime->GetIsolate(), sConstName, prop.ToV8Value());
181 }
182
DefineJSObjects(CJS_Runtime * pRuntime)183 void CJS_GlobalArrays::DefineJSObjects(CJS_Runtime* pRuntime) {
184 {
185 const FX_WCHAR* ArrayName = L"RE_NUMBER_ENTRY_DOT_SEP";
186 const FX_WCHAR* ArrayContent[] = {L"[+-]?\\d*\\.?\\d*"};
187 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
188 FX_ArraySize(ArrayContent));
189 }
190
191 {
192 const FX_WCHAR* ArrayName = L"RE_NUMBER_COMMIT_DOT_SEP";
193 const FX_WCHAR* ArrayContent[] = {
194 L"[+-]?\\d+(\\.\\d+)?", /* -1.0 or -1 */
195 L"[+-]?\\.\\d+", /* -.1 */
196 L"[+-]?\\d+\\." /* -1. */
197 };
198 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
199 FX_ArraySize(ArrayContent));
200 }
201
202 {
203 const FX_WCHAR* ArrayName = L"RE_NUMBER_ENTRY_COMMA_SEP";
204 const FX_WCHAR* ArrayContent[] = {L"[+-]?\\d*,?\\d*"};
205
206 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
207 FX_ArraySize(ArrayContent));
208 }
209
210 {
211 const FX_WCHAR* ArrayName = L"RE_NUMBER_COMMIT_COMMA_SEP";
212 const FX_WCHAR* ArrayContent[] = {
213 L"[+-]?\\d+([.,]\\d+)?", /* -1,0 or -1 */
214 L"[+-]?[.,]\\d+", /* -,1 */
215 L"[+-]?\\d+[.,]" /* -1, */
216 };
217 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
218 FX_ArraySize(ArrayContent));
219 }
220
221 {
222 const FX_WCHAR* ArrayName = L"RE_ZIP_ENTRY";
223 const FX_WCHAR* ArrayContent[] = {L"\\d{0,5}"};
224 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
225 FX_ArraySize(ArrayContent));
226 }
227
228 {
229 const FX_WCHAR* ArrayName = L"RE_ZIP_COMMIT";
230 const FX_WCHAR* ArrayContent[] = {L"\\d{5}"};
231 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
232 FX_ArraySize(ArrayContent));
233 }
234
235 {
236 const FX_WCHAR* ArrayName = L"RE_ZIP4_ENTRY";
237 const FX_WCHAR* ArrayContent[] = {L"\\d{0,5}(\\.|[- ])?\\d{0,4}"};
238 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
239 FX_ArraySize(ArrayContent));
240 }
241
242 {
243 const FX_WCHAR* ArrayName = L"RE_ZIP4_COMMIT";
244 const FX_WCHAR* ArrayContent[] = {L"\\d{5}(\\.|[- ])?\\d{4}"};
245 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
246 FX_ArraySize(ArrayContent));
247 }
248
249 {
250 const FX_WCHAR* ArrayName = L"RE_PHONE_ENTRY";
251 const FX_WCHAR* ArrayContent[] = {
252 L"\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", /* 555-1234 or 408
253 555-1234 */
254 L"\\(\\d{0,3}", /* (408 */
255 L"\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", /* (408)
256 555-1234 */
257 /* (allow the addition of parens as an afterthought) */
258 L"\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", /* (408 555-1234
259 */
260 L"\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", /* 408) 555-1234
261 */
262 L"011(\\.|[- \\d])*" /* international */
263 };
264 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
265 FX_ArraySize(ArrayContent));
266 }
267
268 {
269 const FX_WCHAR* ArrayName = L"RE_PHONE_COMMIT";
270 const FX_WCHAR* ArrayContent[] = {
271 L"\\d{3}(\\.|[- ])?\\d{4}", /* 555-1234 */
272 L"\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", /* 408 555-1234 */
273 L"\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", /* (408) 555-1234 */
274 L"011(\\.|[- \\d])*" /* international */
275 };
276 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
277 FX_ArraySize(ArrayContent));
278 }
279
280 {
281 const FX_WCHAR* ArrayName = L"RE_SSN_ENTRY";
282 const FX_WCHAR* ArrayContent[] = {
283 L"\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}"};
284 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
285 FX_ArraySize(ArrayContent));
286 }
287
288 {
289 const FX_WCHAR* ArrayName = L"RE_SSN_COMMIT";
290 const FX_WCHAR* ArrayContent[] = {
291 L"\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}"};
292 DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent,
293 FX_ArraySize(ArrayContent));
294 }
295 }
296