Fix strings, remove stringify macros, void return types for Consts.h.
[pdfium.git] / fpdfsdk / src / javascript / Consts.cpp
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 "../../include/javascript/JavaScript.h"
8 #include "../../include/javascript/IJavaScript.h"
9 #include "../../include/javascript/JS_Define.h"
10 #include "../../include/javascript/JS_Object.h"
11 #include "../../include/javascript/JS_Value.h"
12 #include "../../include/javascript/Consts.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")
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(v8::Isolate* pIsolate,
133                                     const wchar_t* pConstName,
134                                     const wchar_t* pValue) {
135   JS_DefineGlobalConst(pIsolate, pConstName, JS_NewString(pIsolate, pValue));
136 }
137
138 void CJS_GlobalConsts::DefineJSObjects(v8::Isolate* pIsolate) {
139   DefineGlobalConstString(
140       pIsolate, L"IDS_GREATER_THAN",
141       L"Invalid value: must be greater than or equal to % s.");
142   DefineGlobalConstString(
143       pIsolate, L"IDS_GT_AND_LT",
144       L"Invalid value: must be greater than or equal to % s "
145       L"and less than or equal to % s.");
146   DefineGlobalConstString(pIsolate, L"IDS_LESS_THAN",
147                           L"Invalid value: must be less than or equal to % s.");
148   DefineGlobalConstString(pIsolate, L"IDS_INVALID_MONTH", L"**Invalid**");
149   DefineGlobalConstString(
150       pIsolate, L"IDS_INVALID_DATE",
151       L"Invalid date / time: please ensure that the date / time exists.Field");
152   DefineGlobalConstString(
153       pIsolate, L"IDS_INVALID_VALUE",
154       L"The value entered does not match the format of the field");
155   DefineGlobalConstString(pIsolate, L"IDS_AM", L"am");
156   DefineGlobalConstString(pIsolate, L"IDS_PM", L"pm");
157   DefineGlobalConstString(
158       pIsolate, L"IDS_MONTH_INFO",
159       L"January[1] February[2] March[3] April[4] May[5] "
160       L"June[6] July[7] August[8] September[9] October[10] "
161       L"November[11] December[12] Sept[9] Jan[1] Feb[2] Mar[3] "
162       L"Apr[4] Jun[6] Jul[7] Aug[8] Sep[9] Oct[10] Nov[11] "
163       L"Dec[12]");
164   DefineGlobalConstString(pIsolate, L"IDS_STARTUP_CONSOLE_MSG", L"** ^ _ ^ **");
165 }
166
167 /* ------------------------------ CJS_GlobalArrays  ------------------------ */
168
169 void DefineGlobalConstStringArray(v8::Isolate* pIsolate,
170                                   const wchar_t* sConstName,
171                                   const wchar_t** pValues,
172                                   size_t nValues) {
173   CJS_Array array(pIsolate);
174   for (size_t i = 0; i < nValues; ++i) {
175     array.SetElement(i, CJS_Value(pIsolate, pValues[i]));
176   }
177   CJS_PropValue prop(pIsolate);
178   prop << array;
179   JS_DefineGlobalConst(pIsolate, sConstName, prop.ToV8Value());
180 }
181
182 void CJS_GlobalArrays::DefineJSObjects(v8::Isolate* pIsolate) {
183   {
184     const FX_WCHAR* ArrayName = L"RE_NUMBER_ENTRY_DOT_SEP";
185     const FX_WCHAR* ArrayContent[] = {L"[+-]?\\d*\\.?\\d*"};
186     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
187                                  FX_ArraySize(ArrayContent));
188   }
189
190   {
191     const FX_WCHAR* ArrayName = L"RE_NUMBER_COMMIT_DOT_SEP";
192     const FX_WCHAR* ArrayContent[] = {
193         L"[+-]?\\d+(\\.\\d+)?", /* -1.0 or -1 */
194         L"[+-]?\\.\\d+",        /* -.1 */
195         L"[+-]?\\d+\\."         /* -1. */
196     };
197     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
198                                  FX_ArraySize(ArrayContent));
199   }
200
201   {
202     const FX_WCHAR* ArrayName = L"RE_NUMBER_ENTRY_COMMA_SEP";
203     const FX_WCHAR* ArrayContent[] = {L"[+-]?\\d*,?\\d*"};
204
205     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
206                                  FX_ArraySize(ArrayContent));
207   }
208
209   {
210     const FX_WCHAR* ArrayName = L"RE_NUMBER_COMMIT_COMMA_SEP";
211     const FX_WCHAR* ArrayContent[] = {
212         L"[+-]?\\d+([.,]\\d+)?", /* -1,0 or -1 */
213         L"[+-]?[.,]\\d+",        /* -,1 */
214         L"[+-]?\\d+[.,]"         /* -1, */
215     };
216     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
217                                  FX_ArraySize(ArrayContent));
218   }
219
220   {
221     const FX_WCHAR* ArrayName = L"RE_ZIP_ENTRY";
222     const FX_WCHAR* ArrayContent[] = {L"\\d{0,5}"};
223     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
224                                  FX_ArraySize(ArrayContent));
225   }
226
227   {
228     const FX_WCHAR* ArrayName = L"RE_ZIP_COMMIT";
229     const FX_WCHAR* ArrayContent[] = {L"\\d{5}"};
230     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
231                                  FX_ArraySize(ArrayContent));
232   }
233
234   {
235     const FX_WCHAR* ArrayName = L"RE_ZIP4_ENTRY";
236     const FX_WCHAR* ArrayContent[] = {L"\\d{0,5}(\\.|[- ])?\\d{0,4}"};
237     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
238                                  FX_ArraySize(ArrayContent));
239   }
240
241   {
242     const FX_WCHAR* ArrayName = L"RE_ZIP4_COMMIT";
243     const FX_WCHAR* ArrayContent[] = {L"\\d{5}(\\.|[- ])?\\d{4}"};
244     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
245                                  FX_ArraySize(ArrayContent));
246   }
247
248   {
249     const FX_WCHAR* ArrayName = L"RE_PHONE_ENTRY";
250     const FX_WCHAR* ArrayContent[] = {
251         L"\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", /* 555-1234 or 408
252                                                               555-1234 */
253         L"\\(\\d{0,3}",                                    /* (408 */
254         L"\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", /* (408)
255                                                                     555-1234 */
256         /* (allow the addition of parens as an afterthought) */
257         L"\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", /* (408 555-1234
258                                                                  */
259         L"\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", /* 408) 555-1234
260                                                                  */
261         L"011(\\.|[- \\d])*" /* international */
262     };
263     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
264                                  FX_ArraySize(ArrayContent));
265   }
266
267   {
268     const FX_WCHAR* ArrayName = L"RE_PHONE_COMMIT";
269     const FX_WCHAR* ArrayContent[] = {
270         L"\\d{3}(\\.|[- ])?\\d{4}",                        /* 555-1234 */
271         L"\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}",       /* 408 555-1234 */
272         L"\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", /* (408) 555-1234 */
273         L"011(\\.|[- \\d])*"                               /* international */
274     };
275     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
276                                  FX_ArraySize(ArrayContent));
277   }
278
279   {
280     const FX_WCHAR* ArrayName = L"RE_SSN_ENTRY";
281     const FX_WCHAR* ArrayContent[] = {
282         L"\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}"};
283     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
284                                  FX_ArraySize(ArrayContent));
285   }
286
287   {
288     const FX_WCHAR* ArrayName = L"RE_SSN_COMMIT";
289     const FX_WCHAR* ArrayContent[] = {
290         L"\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}"};
291     DefineGlobalConstStringArray(pIsolate, ArrayName, ArrayContent,
292                                  FX_ArraySize(ArrayContent));
293   }
294 }