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