Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(306)

Side by Side Diff: fpdfsdk/src/javascript/PublicMethods.cpp

Issue 1799773002: Move fpdfsdk/src up to fpdfsdk/. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Rebase to master Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fpdfsdk/src/javascript/PublicMethods.h ('k') | fpdfsdk/src/javascript/app.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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/src/javascript/PublicMethods.h"
8
9 #include <algorithm>
10 #include <string>
11 #include <vector>
12
13 #include "core/include/fxcrt/fx_ext.h"
14 #include "fpdfsdk/include/fsdk_mgr.h" // For CPDFDoc_Environment.
15 #include "fpdfsdk/include/javascript/IJavaScript.h"
16 #include "fpdfsdk/src/javascript/Field.h"
17 #include "fpdfsdk/src/javascript/JS_Context.h"
18 #include "fpdfsdk/src/javascript/JS_Define.h"
19 #include "fpdfsdk/src/javascript/JS_EventHandler.h"
20 #include "fpdfsdk/src/javascript/JS_Object.h"
21 #include "fpdfsdk/src/javascript/JS_Runtime.h"
22 #include "fpdfsdk/src/javascript/JS_Value.h"
23 #include "fpdfsdk/src/javascript/color.h"
24 #include "fpdfsdk/src/javascript/resource.h"
25 #include "fpdfsdk/src/javascript/util.h"
26
27 #define DOUBLE_CORRECT 0.000000000000001
28
29 BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
30 JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format)
31 JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke)
32 JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format)
33 JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke)
34 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx)
35 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx)
36 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format)
37 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke)
38 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx)
39 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx)
40 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format)
41 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke)
42 JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format)
43 JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke)
44 JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx)
45 JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple)
46 JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber)
47 JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate)
48 JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate)
49 JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange)
50 JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx)
51 JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums)
52 END_JS_STATIC_GLOBAL_FUN()
53
54 IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
55
56 static const FX_WCHAR* const months[] = {L"Jan",
57 L"Feb",
58 L"Mar",
59 L"Apr",
60 L"May",
61 L"Jun",
62 L"Jul",
63 L"Aug",
64 L"Sep",
65 L"Oct",
66 L"Nov",
67 L"Dec"};
68
69 static const FX_WCHAR* const fullmonths[] = {L"January",
70 L"February",
71 L"March",
72 L"April",
73 L"May",
74 L"June",
75 L"July",
76 L"August",
77 L"September",
78 L"October",
79 L"November",
80 L"December"};
81
82 FX_BOOL CJS_PublicMethods::IsNumber(const FX_WCHAR* str) {
83 CFX_WideString sTrim = StrTrim(str);
84 const FX_WCHAR* pTrim = sTrim.c_str();
85 const FX_WCHAR* p = pTrim;
86
87 FX_BOOL bDot = FALSE;
88 FX_BOOL bKXJS = FALSE;
89
90 wchar_t c;
91 while ((c = *p)) {
92 if (c == '.' || c == ',') {
93 if (bDot)
94 return FALSE;
95 bDot = TRUE;
96 } else if (c == '-' || c == '+') {
97 if (p != pTrim)
98 return FALSE;
99 } else if (c == 'e' || c == 'E') {
100 if (bKXJS)
101 return FALSE;
102
103 p++;
104 c = *p;
105 if (c == '+' || c == '-') {
106 bKXJS = TRUE;
107 } else {
108 return FALSE;
109 }
110 } else if (!FXSYS_iswdigit(c)) {
111 return FALSE;
112 }
113 p++;
114 }
115
116 return TRUE;
117 }
118
119 FX_BOOL CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
120 switch (c_Mask) {
121 case L'9':
122 return FXSYS_iswdigit(c_Change);
123 case L'A':
124 return FXSYS_iswalpha(c_Change);
125 case L'O':
126 return FXSYS_iswalnum(c_Change);
127 case L'X':
128 return TRUE;
129 default:
130 return (c_Change == c_Mask);
131 }
132 }
133
134 FX_BOOL CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
135 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
136 }
137
138 double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
139 double dValue1,
140 double dValue2) {
141 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
142 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
143 return dValue1 + dValue2;
144 }
145 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
146 return dValue1 * dValue2;
147 }
148 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
149 return std::min(dValue1, dValue2);
150 }
151 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
152 return std::max(dValue1, dValue2);
153 }
154 return dValue1;
155 }
156
157 CFX_WideString CJS_PublicMethods::StrLTrim(const FX_WCHAR* pStr) {
158 while (*pStr && *pStr == L' ')
159 pStr++;
160
161 return pStr;
162 }
163
164 CFX_WideString CJS_PublicMethods::StrRTrim(const FX_WCHAR* pStr) {
165 const FX_WCHAR* p = pStr;
166 while (*p)
167 p++;
168 while (p > pStr && *(p - 1) == L' ')
169 p--;
170
171 return CFX_WideString(pStr, p - pStr);
172 }
173
174 CFX_WideString CJS_PublicMethods::StrTrim(const FX_WCHAR* pStr) {
175 return StrRTrim(StrLTrim(pStr).c_str());
176 }
177
178 CFX_ByteString CJS_PublicMethods::StrLTrim(const FX_CHAR* pStr) {
179 while (*pStr && *pStr == ' ')
180 pStr++;
181
182 return pStr;
183 }
184
185 CFX_ByteString CJS_PublicMethods::StrRTrim(const FX_CHAR* pStr) {
186 const FX_CHAR* p = pStr;
187 while (*p)
188 p++;
189 while (p > pStr && *(p - 1) == L' ')
190 p--;
191
192 return CFX_ByteString(pStr, p - pStr);
193 }
194
195 CFX_ByteString CJS_PublicMethods::StrTrim(const FX_CHAR* pStr) {
196 return StrRTrim(StrLTrim(pStr));
197 }
198
199 CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
200 CJS_Value val) {
201 CJS_Array StrArray(pRuntime);
202 if (val.IsArrayObject()) {
203 val.ConvertToArray(StrArray);
204 return StrArray;
205 }
206 CFX_WideString wsStr = val.ToCFXWideString();
207 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
208 const char* p = (const char*)t;
209
210 int ch = ',';
211 int nIndex = 0;
212
213 while (*p) {
214 const char* pTemp = strchr(p, ch);
215 if (!pTemp) {
216 StrArray.SetElement(nIndex, CJS_Value(pRuntime, StrTrim(p).c_str()));
217 break;
218 }
219
220 char* pSub = new char[pTemp - p + 1];
221 strncpy(pSub, p, pTemp - p);
222 *(pSub + (pTemp - p)) = '\0';
223
224 StrArray.SetElement(nIndex, CJS_Value(pRuntime, StrTrim(pSub).c_str()));
225 delete[] pSub;
226
227 nIndex++;
228 p = ++pTemp;
229 }
230 return StrArray;
231 }
232
233 int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
234 int nStart,
235 int& nSkip,
236 int nMaxStep) {
237 int nRet = 0;
238 nSkip = 0;
239 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
240 if (i - nStart > 10)
241 break;
242
243 FX_WCHAR c = str.GetAt(i);
244 if (!FXSYS_iswdigit(c))
245 break;
246
247 nRet = nRet * 10 + FXSYS_toDecimalDigit(c);
248 nSkip = i - nStart + 1;
249 if (nSkip >= nMaxStep)
250 break;
251 }
252
253 return nRet;
254 }
255
256 CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
257 int nStart,
258 int& nSkip) {
259 CFX_WideString swRet;
260 nSkip = 0;
261 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
262 FX_WCHAR c = str.GetAt(i);
263 if (!FXSYS_iswdigit(c))
264 break;
265
266 swRet += c;
267 nSkip = i - nStart + 1;
268 }
269
270 return swRet;
271 }
272
273 double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
274 bool* bWrongFormat) {
275 double dt = JS_GetDateTime();
276
277 int nYear = JS_GetYearFromTime(dt);
278 int nMonth = JS_GetMonthFromTime(dt) + 1;
279 int nDay = JS_GetDayFromTime(dt);
280 int nHour = JS_GetHourFromTime(dt);
281 int nMin = JS_GetMinFromTime(dt);
282 int nSec = JS_GetSecFromTime(dt);
283
284 int number[3];
285
286 int nSkip = 0;
287 int nLen = value.GetLength();
288 int nIndex = 0;
289 int i = 0;
290 while (i < nLen) {
291 if (nIndex > 2)
292 break;
293
294 FX_WCHAR c = value.GetAt(i);
295 if (FXSYS_iswdigit(c)) {
296 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
297 i += nSkip;
298 } else {
299 i++;
300 }
301 }
302
303 if (nIndex == 2) {
304 // case2: month/day
305 // case3: day/month
306 if ((number[0] >= 1 && number[0] <= 12) &&
307 (number[1] >= 1 && number[1] <= 31)) {
308 nMonth = number[0];
309 nDay = number[1];
310 } else if ((number[0] >= 1 && number[0] <= 31) &&
311 (number[1] >= 1 && number[1] <= 12)) {
312 nDay = number[0];
313 nMonth = number[1];
314 }
315
316 if (bWrongFormat)
317 *bWrongFormat = false;
318 } else if (nIndex == 3) {
319 // case1: year/month/day
320 // case2: month/day/year
321 // case3: day/month/year
322
323 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
324 (number[2] >= 1 && number[2] <= 31)) {
325 nYear = number[0];
326 nMonth = number[1];
327 nDay = number[2];
328 } else if ((number[0] >= 1 && number[0] <= 12) &&
329 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
330 nMonth = number[0];
331 nDay = number[1];
332 nYear = number[2];
333 } else if ((number[0] >= 1 && number[0] <= 31) &&
334 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
335 nDay = number[0];
336 nMonth = number[1];
337 nYear = number[2];
338 }
339
340 if (bWrongFormat)
341 *bWrongFormat = false;
342 } else {
343 if (bWrongFormat)
344 *bWrongFormat = true;
345 return dt;
346 }
347
348 CFX_WideString swTemp;
349 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
350 return JS_DateParse(swTemp.c_str());
351 }
352
353 double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
354 const CFX_WideString& format,
355 bool* bWrongFormat) {
356 double dt = JS_GetDateTime();
357
358 if (format.IsEmpty() || value.IsEmpty())
359 return dt;
360
361 int nYear = JS_GetYearFromTime(dt);
362 int nMonth = JS_GetMonthFromTime(dt) + 1;
363 int nDay = JS_GetDayFromTime(dt);
364 int nHour = JS_GetHourFromTime(dt);
365 int nMin = JS_GetMinFromTime(dt);
366 int nSec = JS_GetSecFromTime(dt);
367
368 int nYearSub = 99; // nYear - 2000;
369
370 FX_BOOL bPm = FALSE;
371 FX_BOOL bExit = FALSE;
372 bool bBadFormat = false;
373
374 int i = 0;
375 int j = 0;
376
377 while (i < format.GetLength()) {
378 if (bExit)
379 break;
380
381 FX_WCHAR c = format.GetAt(i);
382 switch (c) {
383 case ':':
384 case '.':
385 case '-':
386 case '\\':
387 case '/':
388 i++;
389 j++;
390 break;
391
392 case 'y':
393 case 'm':
394 case 'd':
395 case 'H':
396 case 'h':
397 case 'M':
398 case 's':
399 case 't': {
400 int oldj = j;
401 int nSkip = 0;
402 int remaining = format.GetLength() - i - 1;
403
404 if (remaining == 0 || format.GetAt(i + 1) != c) {
405 switch (c) {
406 case 'y':
407 i++;
408 j++;
409 break;
410 case 'm':
411 nMonth = ParseStringInteger(value, j, nSkip, 2);
412 i++;
413 j += nSkip;
414 break;
415 case 'd':
416 nDay = ParseStringInteger(value, j, nSkip, 2);
417 i++;
418 j += nSkip;
419 break;
420 case 'H':
421 nHour = ParseStringInteger(value, j, nSkip, 2);
422 i++;
423 j += nSkip;
424 break;
425 case 'h':
426 nHour = ParseStringInteger(value, j, nSkip, 2);
427 i++;
428 j += nSkip;
429 break;
430 case 'M':
431 nMin = ParseStringInteger(value, j, nSkip, 2);
432 i++;
433 j += nSkip;
434 break;
435 case 's':
436 nSec = ParseStringInteger(value, j, nSkip, 2);
437 i++;
438 j += nSkip;
439 break;
440 case 't':
441 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
442 i++;
443 j++;
444 break;
445 }
446 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
447 switch (c) {
448 case 'y':
449 nYear = ParseStringInteger(value, j, nSkip, 4);
450 i += 2;
451 j += nSkip;
452 break;
453 case 'm':
454 nMonth = ParseStringInteger(value, j, nSkip, 2);
455 i += 2;
456 j += nSkip;
457 break;
458 case 'd':
459 nDay = ParseStringInteger(value, j, nSkip, 2);
460 i += 2;
461 j += nSkip;
462 break;
463 case 'H':
464 nHour = ParseStringInteger(value, j, nSkip, 2);
465 i += 2;
466 j += nSkip;
467 break;
468 case 'h':
469 nHour = ParseStringInteger(value, j, nSkip, 2);
470 i += 2;
471 j += nSkip;
472 break;
473 case 'M':
474 nMin = ParseStringInteger(value, j, nSkip, 2);
475 i += 2;
476 j += nSkip;
477 break;
478 case 's':
479 nSec = ParseStringInteger(value, j, nSkip, 2);
480 i += 2;
481 j += nSkip;
482 break;
483 case 't':
484 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
485 value.GetAt(j + 1) == 'm');
486 i += 2;
487 j += 2;
488 break;
489 }
490 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
491 switch (c) {
492 case 'm': {
493 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
494 FX_BOOL bFind = FALSE;
495 for (int m = 0; m < 12; m++) {
496 if (sMonth.CompareNoCase(months[m]) == 0) {
497 nMonth = m + 1;
498 i += 3;
499 j += nSkip;
500 bFind = TRUE;
501 break;
502 }
503 }
504
505 if (!bFind) {
506 nMonth = ParseStringInteger(value, j, nSkip, 3);
507 i += 3;
508 j += nSkip;
509 }
510 } break;
511 case 'y':
512 break;
513 default:
514 i += 3;
515 j += 3;
516 break;
517 }
518 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
519 switch (c) {
520 case 'y':
521 nYear = ParseStringInteger(value, j, nSkip, 4);
522 j += nSkip;
523 i += 4;
524 break;
525 case 'm': {
526 FX_BOOL bFind = FALSE;
527
528 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
529 sMonth.MakeLower();
530
531 for (int m = 0; m < 12; m++) {
532 CFX_WideString sFullMonths = fullmonths[m];
533 sFullMonths.MakeLower();
534
535 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
536 nMonth = m + 1;
537 i += 4;
538 j += nSkip;
539 bFind = TRUE;
540 break;
541 }
542 }
543
544 if (!bFind) {
545 nMonth = ParseStringInteger(value, j, nSkip, 4);
546 i += 4;
547 j += nSkip;
548 }
549 } break;
550 default:
551 i += 4;
552 j += 4;
553 break;
554 }
555 } else {
556 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
557 bBadFormat = true;
558 bExit = TRUE;
559 }
560 i++;
561 j++;
562 }
563
564 if (oldj == j) {
565 bBadFormat = true;
566 bExit = TRUE;
567 }
568 }
569
570 break;
571 default:
572 if (value.GetLength() <= j) {
573 bExit = TRUE;
574 } else if (format.GetAt(i) != value.GetAt(j)) {
575 bBadFormat = true;
576 bExit = TRUE;
577 }
578
579 i++;
580 j++;
581 break;
582 }
583 }
584
585 if (bPm)
586 nHour += 12;
587
588 if (nYear >= 0 && nYear <= nYearSub)
589 nYear += 2000;
590
591 if (nMonth < 1 || nMonth > 12)
592 bBadFormat = true;
593
594 if (nDay < 1 || nDay > 31)
595 bBadFormat = true;
596
597 if (nHour < 0 || nHour > 24)
598 bBadFormat = true;
599
600 if (nMin < 0 || nMin > 60)
601 bBadFormat = true;
602
603 if (nSec < 0 || nSec > 60)
604 bBadFormat = true;
605
606 double dRet = 0;
607
608 if (bBadFormat) {
609 dRet = ParseNormalDate(value, &bBadFormat);
610 } else {
611 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
612 JS_MakeTime(nHour, nMin, nSec, 0));
613
614 if (JS_PortIsNan(dRet)) {
615 dRet = JS_DateParse(value.c_str());
616 }
617 }
618
619 if (JS_PortIsNan(dRet)) {
620 dRet = ParseNormalDate(value, &bBadFormat);
621 }
622
623 if (bWrongFormat)
624 *bWrongFormat = bBadFormat;
625 return dRet;
626 }
627
628 CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
629 const CFX_WideString& format) {
630 CFX_WideString sRet = L"", sPart = L"";
631
632 int nYear = JS_GetYearFromTime(dDate);
633 int nMonth = JS_GetMonthFromTime(dDate) + 1;
634 int nDay = JS_GetDayFromTime(dDate);
635 int nHour = JS_GetHourFromTime(dDate);
636 int nMin = JS_GetMinFromTime(dDate);
637 int nSec = JS_GetSecFromTime(dDate);
638
639 int i = 0;
640 while (i < format.GetLength()) {
641 FX_WCHAR c = format.GetAt(i);
642 int remaining = format.GetLength() - i - 1;
643 sPart = L"";
644 switch (c) {
645 case 'y':
646 case 'm':
647 case 'd':
648 case 'H':
649 case 'h':
650 case 'M':
651 case 's':
652 case 't':
653 if (remaining == 0 || format.GetAt(i + 1) != c) {
654 switch (c) {
655 case 'y':
656 sPart += c;
657 break;
658 case 'm':
659 sPart.Format(L"%d", nMonth);
660 break;
661 case 'd':
662 sPart.Format(L"%d", nDay);
663 break;
664 case 'H':
665 sPart.Format(L"%d", nHour);
666 break;
667 case 'h':
668 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
669 break;
670 case 'M':
671 sPart.Format(L"%d", nMin);
672 break;
673 case 's':
674 sPart.Format(L"%d", nSec);
675 break;
676 case 't':
677 sPart += nHour > 12 ? 'p' : 'a';
678 break;
679 }
680 i++;
681 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
682 switch (c) {
683 case 'y':
684 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
685 break;
686 case 'm':
687 sPart.Format(L"%02d", nMonth);
688 break;
689 case 'd':
690 sPart.Format(L"%02d", nDay);
691 break;
692 case 'H':
693 sPart.Format(L"%02d", nHour);
694 break;
695 case 'h':
696 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
697 break;
698 case 'M':
699 sPart.Format(L"%02d", nMin);
700 break;
701 case 's':
702 sPart.Format(L"%02d", nSec);
703 break;
704 case 't':
705 sPart = nHour > 12 ? L"pm" : L"am";
706 break;
707 }
708 i += 2;
709 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
710 switch (c) {
711 case 'm':
712 i += 3;
713 if (nMonth > 0 && nMonth <= 12)
714 sPart += months[nMonth - 1];
715 break;
716 default:
717 i += 3;
718 sPart += c;
719 sPart += c;
720 sPart += c;
721 break;
722 }
723 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
724 switch (c) {
725 case 'y':
726 sPart.Format(L"%04d", nYear);
727 i += 4;
728 break;
729 case 'm':
730 i += 4;
731 if (nMonth > 0 && nMonth <= 12)
732 sPart += fullmonths[nMonth - 1];
733 break;
734 default:
735 i += 4;
736 sPart += c;
737 sPart += c;
738 sPart += c;
739 sPart += c;
740 break;
741 }
742 } else {
743 i++;
744 sPart += c;
745 }
746 break;
747 default:
748 i++;
749 sPart += c;
750 break;
751 }
752
753 sRet += sPart;
754 }
755
756 return sRet;
757 }
758
759 /* -------------------------------------------------------------------------- */
760
761 // function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
762 // bCurrencyPrepend)
763 FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
764 const std::vector<CJS_Value>& params,
765 CJS_Value& vRet,
766 CFX_WideString& sError) {
767 #if _FX_OS_ != _FX_ANDROID_
768 CJS_Context* pContext = (CJS_Context*)cc;
769 if (params.size() != 6) {
770 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
771 return FALSE;
772 }
773
774 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
775 CJS_EventHandler* pEvent = pContext->GetEventHandler();
776 if (!pEvent->m_pValue)
777 return FALSE;
778
779 CFX_WideString& Value = pEvent->Value();
780 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
781 if (strValue.IsEmpty())
782 return TRUE;
783
784 int iDec = params[0].ToInt();
785 int iSepStyle = params[1].ToInt();
786 int iNegStyle = params[2].ToInt();
787 // params[3] is iCurrStyle, it's not used.
788 std::wstring wstrCurrency(params[4].ToCFXWideString().c_str());
789 FX_BOOL bCurrencyPrepend = params[5].ToBool();
790
791 if (iDec < 0)
792 iDec = -iDec;
793
794 if (iSepStyle < 0 || iSepStyle > 3)
795 iSepStyle = 0;
796
797 if (iNegStyle < 0 || iNegStyle > 3)
798 iNegStyle = 0;
799
800 //////////////////////////////////////////////////////
801 // for processing decimal places
802 strValue.Replace(",", ".");
803 double dValue = atof(strValue);
804 if (iDec > 0)
805 dValue += DOUBLE_CORRECT;
806
807 int iDec2;
808 int iNegative = 0;
809
810 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
811 if (strValue.IsEmpty()) {
812 dValue = 0;
813 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
814 if (strValue.IsEmpty()) {
815 strValue = "0";
816 iDec2 = 1;
817 }
818 }
819
820 if (iDec2 < 0) {
821 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
822 strValue = "0" + strValue;
823 }
824 iDec2 = 0;
825 }
826 int iMax = strValue.GetLength();
827 if (iDec2 > iMax) {
828 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
829 strValue += "0";
830 }
831 iMax = iDec2 + 1;
832 }
833 ///////////////////////////////////////////////////////
834 // for processing seperator style
835 if (iDec2 < iMax) {
836 if (iSepStyle == 0 || iSepStyle == 1) {
837 strValue.Insert(iDec2, '.');
838 iMax++;
839 } else if (iSepStyle == 2 || iSepStyle == 3) {
840 strValue.Insert(iDec2, ',');
841 iMax++;
842 }
843
844 if (iDec2 == 0)
845 strValue.Insert(iDec2, '0');
846 }
847 if (iSepStyle == 0 || iSepStyle == 2) {
848 char cSeperator;
849 if (iSepStyle == 0)
850 cSeperator = ',';
851 else
852 cSeperator = '.';
853
854 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
855 strValue.Insert(iDecPositive, cSeperator);
856 iMax++;
857 }
858 }
859
860 //////////////////////////////////////////////////////////////////////
861 // for processing currency string
862
863 Value = CFX_WideString::FromLocal(strValue);
864 std::wstring strValue2 = Value.c_str();
865
866 if (bCurrencyPrepend)
867 strValue2 = wstrCurrency + strValue2;
868 else
869 strValue2 = strValue2 + wstrCurrency;
870
871 /////////////////////////////////////////////////////////////////////////
872 // for processing negative style
873 if (iNegative) {
874 if (iNegStyle == 0) {
875 strValue2.insert(0, L"-");
876 }
877 if (iNegStyle == 2 || iNegStyle == 3) {
878 strValue2.insert(0, L"(");
879 strValue2.insert(strValue2.length(), L")");
880 }
881 if (iNegStyle == 1 || iNegStyle == 3) {
882 if (Field* fTarget = pEvent->Target_Field()) {
883 CJS_Array arColor(pRuntime);
884 CJS_Value vColElm(pRuntime);
885 vColElm = L"RGB";
886 arColor.SetElement(0, vColElm);
887 vColElm = 1;
888 arColor.SetElement(1, vColElm);
889 vColElm = 0;
890 arColor.SetElement(2, vColElm);
891
892 arColor.SetElement(3, vColElm);
893
894 CJS_PropValue vProp(pRuntime);
895 vProp.StartGetting();
896 vProp << arColor;
897 vProp.StartSetting();
898 fTarget->textColor(cc, vProp, sError); // red
899 }
900 }
901 } else {
902 if (iNegStyle == 1 || iNegStyle == 3) {
903 if (Field* fTarget = pEvent->Target_Field()) {
904 CJS_Array arColor(pRuntime);
905 CJS_Value vColElm(pRuntime);
906 vColElm = L"RGB";
907 arColor.SetElement(0, vColElm);
908 vColElm = 0;
909 arColor.SetElement(1, vColElm);
910 arColor.SetElement(2, vColElm);
911 arColor.SetElement(3, vColElm);
912
913 CJS_PropValue vProp(pRuntime);
914 vProp.StartGetting();
915 fTarget->textColor(cc, vProp, sError);
916
917 CJS_Array aProp(pRuntime);
918 vProp.ConvertToArray(aProp);
919
920 CPWL_Color crProp;
921 CPWL_Color crColor;
922 color::ConvertArrayToPWLColor(aProp, crProp);
923 color::ConvertArrayToPWLColor(arColor, crColor);
924
925 if (crColor != crProp) {
926 CJS_PropValue vProp2(pRuntime);
927 vProp2.StartGetting();
928 vProp2 << arColor;
929 vProp2.StartSetting();
930 fTarget->textColor(cc, vProp2, sError);
931 }
932 }
933 }
934 }
935 Value = strValue2.c_str();
936 #endif
937 return TRUE;
938 }
939
940 // function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
941 // bCurrencyPrepend)
942 FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(
943 IJS_Context* cc,
944 const std::vector<CJS_Value>& params,
945 CJS_Value& vRet,
946 CFX_WideString& sError) {
947 CJS_Context* pContext = (CJS_Context*)cc;
948 CJS_EventHandler* pEvent = pContext->GetEventHandler();
949
950 if (params.size() < 2)
951 return FALSE;
952 int iSepStyle = params[1].ToInt();
953
954 if (iSepStyle < 0 || iSepStyle > 3)
955 iSepStyle = 0;
956 if (!pEvent->m_pValue)
957 return FALSE;
958 CFX_WideString& val = pEvent->Value();
959 CFX_WideString& w_strChange = pEvent->Change();
960 CFX_WideString w_strValue = val;
961
962 if (pEvent->WillCommit()) {
963 CFX_WideString wstrChange = w_strChange;
964 CFX_WideString wstrValue = StrLTrim(w_strValue.c_str());
965 if (wstrValue.IsEmpty())
966 return TRUE;
967
968 CFX_WideString swTemp = wstrValue;
969 swTemp.Replace(L",", L".");
970 if (!IsNumber(swTemp.c_str())) {
971 pEvent->Rc() = FALSE;
972 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
973 Alert(pContext, sError.c_str());
974 return TRUE;
975 }
976 return TRUE; // it happens after the last keystroke and before validating,
977 }
978
979 std::wstring w_strValue2 = w_strValue.c_str();
980 std::wstring w_strChange2 = w_strChange.c_str();
981 std::wstring w_strSelected;
982 if (-1 != pEvent->SelStart())
983 w_strSelected = w_strValue2.substr(pEvent->SelStart(),
984 (pEvent->SelEnd() - pEvent->SelStart()));
985 bool bHasSign = (w_strValue2.find('-') != std::wstring::npos) &&
986 (w_strSelected.find('-') == std::wstring::npos);
987 if (bHasSign) {
988 // can't insert "change" in front to sign postion.
989 if (pEvent->SelStart() == 0) {
990 FX_BOOL& bRc = pEvent->Rc();
991 bRc = FALSE;
992 return TRUE;
993 }
994 }
995
996 char cSep = L'.';
997
998 switch (iSepStyle) {
999 case 0:
1000 case 1:
1001 cSep = L'.';
1002 break;
1003 case 2:
1004 case 3:
1005 cSep = L',';
1006 break;
1007 }
1008
1009 bool bHasSep = (w_strValue2.find(cSep) != std::wstring::npos);
1010 for (std::wstring::iterator it = w_strChange2.begin();
1011 it != w_strChange2.end(); it++) {
1012 if (*it == cSep) {
1013 if (bHasSep) {
1014 FX_BOOL& bRc = pEvent->Rc();
1015 bRc = FALSE;
1016 return TRUE;
1017 }
1018 bHasSep = TRUE;
1019 continue;
1020 }
1021 if (*it == L'-') {
1022 if (bHasSign) {
1023 FX_BOOL& bRc = pEvent->Rc();
1024 bRc = FALSE;
1025 return TRUE;
1026 }
1027 // sign's position is not correct
1028 if (it != w_strChange2.begin()) {
1029 FX_BOOL& bRc = pEvent->Rc();
1030 bRc = FALSE;
1031 return TRUE;
1032 }
1033 if (pEvent->SelStart() != 0) {
1034 FX_BOOL& bRc = pEvent->Rc();
1035 bRc = FALSE;
1036 return TRUE;
1037 }
1038 bHasSign = TRUE;
1039 continue;
1040 }
1041
1042 if (!FXSYS_iswdigit(*it)) {
1043 FX_BOOL& bRc = pEvent->Rc();
1044 bRc = FALSE;
1045 return TRUE;
1046 }
1047 }
1048
1049 std::wstring w_prefix = w_strValue2.substr(0, pEvent->SelStart());
1050 std::wstring w_postfix;
1051 if (pEvent->SelEnd() < (int)w_strValue2.length())
1052 w_postfix = w_strValue2.substr(pEvent->SelEnd());
1053 w_strValue2 = w_prefix + w_strChange2 + w_postfix;
1054 w_strValue = w_strValue2.c_str();
1055 val = w_strValue;
1056 return TRUE;
1057 }
1058
1059 // function AFPercent_Format(nDec, sepStyle)
1060 FX_BOOL CJS_PublicMethods::AFPercent_Format(
1061 IJS_Context* cc,
1062 const std::vector<CJS_Value>& params,
1063 CJS_Value& vRet,
1064 CFX_WideString& sError) {
1065 #if _FX_OS_ != _FX_ANDROID_
1066 CJS_Context* pContext = (CJS_Context*)cc;
1067 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1068
1069 if (params.size() != 2) {
1070 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1071 return FALSE;
1072 }
1073 if (!pEvent->m_pValue)
1074 return FALSE;
1075
1076 CFX_WideString& Value = pEvent->Value();
1077 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1078 if (strValue.IsEmpty())
1079 return TRUE;
1080
1081 int iDec = params[0].ToInt();
1082 if (iDec < 0)
1083 iDec = -iDec;
1084
1085 int iSepStyle = params[1].ToInt();
1086 if (iSepStyle < 0 || iSepStyle > 3)
1087 iSepStyle = 0;
1088
1089 //////////////////////////////////////////////////////
1090 // for processing decimal places
1091 double dValue = atof(strValue);
1092 dValue *= 100;
1093 if (iDec > 0)
1094 dValue += DOUBLE_CORRECT;
1095
1096 int iDec2;
1097 int iNegative = 0;
1098 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1099 if (strValue.IsEmpty()) {
1100 dValue = 0;
1101 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1102 }
1103
1104 if (iDec2 < 0) {
1105 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1106 strValue = "0" + strValue;
1107 }
1108 iDec2 = 0;
1109 }
1110 int iMax = strValue.GetLength();
1111 if (iDec2 > iMax) {
1112 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1113 strValue += "0";
1114 }
1115 iMax = iDec2 + 1;
1116 }
1117 ///////////////////////////////////////////////////////
1118 // for processing seperator style
1119 if (iDec2 < iMax) {
1120 if (iSepStyle == 0 || iSepStyle == 1) {
1121 strValue.Insert(iDec2, '.');
1122 iMax++;
1123 } else if (iSepStyle == 2 || iSepStyle == 3) {
1124 strValue.Insert(iDec2, ',');
1125 iMax++;
1126 }
1127
1128 if (iDec2 == 0)
1129 strValue.Insert(iDec2, '0');
1130 }
1131 if (iSepStyle == 0 || iSepStyle == 2) {
1132 char cSeperator;
1133 if (iSepStyle == 0)
1134 cSeperator = ',';
1135 else
1136 cSeperator = '.';
1137
1138 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
1139 strValue.Insert(iDecPositive, cSeperator);
1140 iMax++;
1141 }
1142 }
1143 ////////////////////////////////////////////////////////////////////
1144 // negative mark
1145 if (iNegative)
1146 strValue = "-" + strValue;
1147 strValue += "%";
1148 Value = CFX_WideString::FromLocal(strValue);
1149 #endif
1150 return TRUE;
1151 }
1152 // AFPercent_Keystroke(nDec, sepStyle)
1153 FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(
1154 IJS_Context* cc,
1155 const std::vector<CJS_Value>& params,
1156 CJS_Value& vRet,
1157 CFX_WideString& sError) {
1158 return AFNumber_Keystroke(cc, params, vRet, sError);
1159 }
1160
1161 // function AFDate_FormatEx(cFormat)
1162 FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
1163 const std::vector<CJS_Value>& params,
1164 CJS_Value& vRet,
1165 CFX_WideString& sError) {
1166 CJS_Context* pContext = (CJS_Context*)cc;
1167 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1168
1169 if (params.size() != 1) {
1170 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1171 return FALSE;
1172 }
1173 if (!pEvent->m_pValue)
1174 return FALSE;
1175
1176 CFX_WideString& val = pEvent->Value();
1177 CFX_WideString strValue = val;
1178 if (strValue.IsEmpty())
1179 return TRUE;
1180
1181 CFX_WideString sFormat = params[0].ToCFXWideString();
1182 double dDate = 0.0f;
1183
1184 if (strValue.Find(L"GMT") != -1) {
1185 // for GMT format time
1186 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1187 dDate = MakeInterDate(strValue);
1188 } else {
1189 dDate = MakeRegularDate(strValue, sFormat, nullptr);
1190 }
1191
1192 if (JS_PortIsNan(dDate)) {
1193 CFX_WideString swMsg;
1194 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1195 sFormat.c_str());
1196 Alert(pContext, swMsg.c_str());
1197 return FALSE;
1198 }
1199
1200 val = MakeFormatDate(dDate, sFormat);
1201 return TRUE;
1202 }
1203
1204 double CJS_PublicMethods::MakeInterDate(CFX_WideString strValue) {
1205 std::vector<CFX_WideString> wsArray;
1206 CFX_WideString sTemp = L"";
1207 for (int i = 0; i < strValue.GetLength(); ++i) {
1208 FX_WCHAR c = strValue.GetAt(i);
1209 if (c == L' ' || c == L':') {
1210 wsArray.push_back(sTemp);
1211 sTemp = L"";
1212 continue;
1213 }
1214 sTemp += c;
1215 }
1216 wsArray.push_back(sTemp);
1217 if (wsArray.size() != 8)
1218 return 0;
1219
1220 int nMonth = 1;
1221 sTemp = wsArray[1];
1222 if (sTemp.Compare(L"Jan") == 0)
1223 nMonth = 1;
1224 else if (sTemp.Compare(L"Feb") == 0)
1225 nMonth = 2;
1226 else if (sTemp.Compare(L"Mar") == 0)
1227 nMonth = 3;
1228 else if (sTemp.Compare(L"Apr") == 0)
1229 nMonth = 4;
1230 else if (sTemp.Compare(L"May") == 0)
1231 nMonth = 5;
1232 else if (sTemp.Compare(L"Jun") == 0)
1233 nMonth = 6;
1234 else if (sTemp.Compare(L"Jul") == 0)
1235 nMonth = 7;
1236 else if (sTemp.Compare(L"Aug") == 0)
1237 nMonth = 8;
1238 else if (sTemp.Compare(L"Sep") == 0)
1239 nMonth = 9;
1240 else if (sTemp.Compare(L"Oct") == 0)
1241 nMonth = 10;
1242 else if (sTemp.Compare(L"Nov") == 0)
1243 nMonth = 11;
1244 else if (sTemp.Compare(L"Dec") == 0)
1245 nMonth = 12;
1246
1247 int nDay = FX_atof(wsArray[2]);
1248 int nHour = FX_atof(wsArray[3]);
1249 int nMin = FX_atof(wsArray[4]);
1250 int nSec = FX_atof(wsArray[5]);
1251 int nYear = FX_atof(wsArray[7]);
1252 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1253 JS_MakeTime(nHour, nMin, nSec, 0));
1254 if (JS_PortIsNan(dRet))
1255 dRet = JS_DateParse(strValue.c_str());
1256
1257 return dRet;
1258 }
1259
1260 // AFDate_KeystrokeEx(cFormat)
1261 FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(
1262 IJS_Context* cc,
1263 const std::vector<CJS_Value>& params,
1264 CJS_Value& vRet,
1265 CFX_WideString& sError) {
1266 CJS_Context* pContext = (CJS_Context*)cc;
1267 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1268
1269 if (params.size() != 1) {
1270 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1271 return FALSE;
1272 }
1273
1274 if (pEvent->WillCommit()) {
1275 if (!pEvent->m_pValue)
1276 return FALSE;
1277 CFX_WideString strValue = pEvent->Value();
1278 if (strValue.IsEmpty())
1279 return TRUE;
1280
1281 CFX_WideString sFormat = params[0].ToCFXWideString();
1282 bool bWrongFormat = FALSE;
1283 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
1284 if (bWrongFormat || JS_PortIsNan(dRet)) {
1285 CFX_WideString swMsg;
1286 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1287 sFormat.c_str());
1288 Alert(pContext, swMsg.c_str());
1289 pEvent->Rc() = FALSE;
1290 return TRUE;
1291 }
1292 }
1293 return TRUE;
1294 }
1295
1296 FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
1297 const std::vector<CJS_Value>& params,
1298 CJS_Value& vRet,
1299 CFX_WideString& sError) {
1300 CJS_Context* pContext = (CJS_Context*)cc;
1301 if (params.size() != 1) {
1302 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1303 return FALSE;
1304 }
1305
1306 int iIndex = params[0].ToInt();
1307 const FX_WCHAR* cFormats[] = {L"m/d",
1308 L"m/d/yy",
1309 L"mm/dd/yy",
1310 L"mm/yy",
1311 L"d-mmm",
1312 L"d-mmm-yy",
1313 L"dd-mmm-yy",
1314 L"yy-mm-dd",
1315 L"mmm-yy",
1316 L"mmmm-yy",
1317 L"mmm d, yyyy",
1318 L"mmmm d, yyyy",
1319 L"m/d/yy h:MM tt",
1320 L"m/d/yy HH:MM"};
1321
1322 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1323 iIndex = 0;
1324
1325 std::vector<CJS_Value> newParams;
1326 newParams.push_back(
1327 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
1328 return AFDate_FormatEx(cc, newParams, vRet, sError);
1329 }
1330
1331 // AFDate_KeystrokeEx(cFormat)
1332 FX_BOOL CJS_PublicMethods::AFDate_Keystroke(
1333 IJS_Context* cc,
1334 const std::vector<CJS_Value>& params,
1335 CJS_Value& vRet,
1336 CFX_WideString& sError) {
1337 CJS_Context* pContext = (CJS_Context*)cc;
1338 if (params.size() != 1) {
1339 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1340 return FALSE;
1341 }
1342
1343 int iIndex = params[0].ToInt();
1344 const FX_WCHAR* cFormats[] = {L"m/d",
1345 L"m/d/yy",
1346 L"mm/dd/yy",
1347 L"mm/yy",
1348 L"d-mmm",
1349 L"d-mmm-yy",
1350 L"dd-mmm-yy",
1351 L"yy-mm-dd",
1352 L"mmm-yy",
1353 L"mmmm-yy",
1354 L"mmm d, yyyy",
1355 L"mmmm d, yyyy",
1356 L"m/d/yy h:MM tt",
1357 L"m/d/yy HH:MM"};
1358
1359 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1360 iIndex = 0;
1361
1362 std::vector<CJS_Value> newParams;
1363 newParams.push_back(
1364 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
1365 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1366 }
1367
1368 // function AFTime_Format(ptf)
1369 FX_BOOL CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
1370 const std::vector<CJS_Value>& params,
1371 CJS_Value& vRet,
1372 CFX_WideString& sError) {
1373 CJS_Context* pContext = (CJS_Context*)cc;
1374 if (params.size() != 1) {
1375 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1376 return FALSE;
1377 }
1378
1379 int iIndex = params[0].ToInt();
1380 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1381 L"h:MM:ss tt"};
1382
1383 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1384 iIndex = 0;
1385
1386 std::vector<CJS_Value> newParams;
1387 newParams.push_back(
1388 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
1389 return AFDate_FormatEx(cc, newParams, vRet, sError);
1390 }
1391
1392 FX_BOOL CJS_PublicMethods::AFTime_Keystroke(
1393 IJS_Context* cc,
1394 const std::vector<CJS_Value>& params,
1395 CJS_Value& vRet,
1396 CFX_WideString& sError) {
1397 CJS_Context* pContext = (CJS_Context*)cc;
1398 if (params.size() != 1) {
1399 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1400 return FALSE;
1401 }
1402
1403 int iIndex = params[0].ToInt();
1404 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1405 L"h:MM:ss tt"};
1406
1407 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1408 iIndex = 0;
1409
1410 std::vector<CJS_Value> newParams;
1411 newParams.push_back(
1412 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
1413 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1414 }
1415
1416 FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
1417 const std::vector<CJS_Value>& params,
1418 CJS_Value& vRet,
1419 CFX_WideString& sError) {
1420 return AFDate_FormatEx(cc, params, vRet, sError);
1421 }
1422
1423 FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(
1424 IJS_Context* cc,
1425 const std::vector<CJS_Value>& params,
1426 CJS_Value& vRet,
1427 CFX_WideString& sError) {
1428 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1429 }
1430
1431 // function AFSpecial_Format(psf)
1432 FX_BOOL CJS_PublicMethods::AFSpecial_Format(
1433 IJS_Context* cc,
1434 const std::vector<CJS_Value>& params,
1435 CJS_Value& vRet,
1436 CFX_WideString& sError) {
1437 CJS_Context* pContext = (CJS_Context*)cc;
1438
1439 if (params.size() != 1) {
1440 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1441 return FALSE;
1442 }
1443
1444 std::string cFormat;
1445 int iIndex = params[0].ToInt();
1446
1447 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1448 if (!pEvent->m_pValue)
1449 return FALSE;
1450 CFX_WideString& Value = pEvent->Value();
1451 std::string strSrc = CFX_ByteString::FromUnicode(Value).c_str();
1452
1453 switch (iIndex) {
1454 case 0:
1455 cFormat = "99999";
1456 break;
1457 case 1:
1458 cFormat = "99999-9999";
1459 break;
1460 case 2: {
1461 std::string NumberStr;
1462 util::printx("9999999999", strSrc, NumberStr);
1463 if (NumberStr.length() >= 10)
1464 cFormat = "(999) 999-9999";
1465 else
1466 cFormat = "999-9999";
1467 break;
1468 }
1469 case 3:
1470 cFormat = "999-99-9999";
1471 break;
1472 }
1473
1474 std::string strDes;
1475 util::printx(cFormat, strSrc, strDes);
1476 Value = CFX_WideString::FromLocal(strDes.c_str());
1477 return TRUE;
1478 }
1479
1480 // function AFSpecial_KeystrokeEx(mask)
1481 FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(
1482 IJS_Context* cc,
1483 const std::vector<CJS_Value>& params,
1484 CJS_Value& vRet,
1485 CFX_WideString& sError) {
1486 CJS_Context* pContext = (CJS_Context*)cc;
1487 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1488
1489 if (params.size() < 1) {
1490 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1491 return FALSE;
1492 }
1493
1494 if (!pEvent->m_pValue)
1495 return FALSE;
1496 CFX_WideString& valEvent = pEvent->Value();
1497
1498 CFX_WideString wstrMask = params[0].ToCFXWideString();
1499 if (wstrMask.IsEmpty())
1500 return TRUE;
1501
1502 const size_t wstrMaskLen = wstrMask.GetLength();
1503 const std::wstring wstrValue = valEvent.c_str();
1504
1505 if (pEvent->WillCommit()) {
1506 if (wstrValue.empty())
1507 return TRUE;
1508 size_t iIndexMask = 0;
1509 for (const auto& w_Value : wstrValue) {
1510 if (!maskSatisfied(w_Value, wstrMask[iIndexMask]))
1511 break;
1512 iIndexMask++;
1513 }
1514
1515 if (iIndexMask != wstrMaskLen ||
1516 (iIndexMask != wstrValue.size() && wstrMaskLen != 0)) {
1517 Alert(
1518 pContext,
1519 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
1520 pEvent->Rc() = FALSE;
1521 }
1522 return TRUE;
1523 }
1524
1525 CFX_WideString& wideChange = pEvent->Change();
1526 std::wstring wChange = wideChange.c_str();
1527 if (wChange.empty())
1528 return TRUE;
1529
1530 int iIndexMask = pEvent->SelStart();
1531
1532 size_t combined_len = wstrValue.length() + wChange.length() -
1533 (pEvent->SelEnd() - pEvent->SelStart());
1534 if (combined_len > wstrMaskLen) {
1535 Alert(pContext,
1536 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1537 pEvent->Rc() = FALSE;
1538 return TRUE;
1539 }
1540
1541 if (iIndexMask >= wstrMaskLen && (!wChange.empty())) {
1542 Alert(pContext,
1543 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1544 pEvent->Rc() = FALSE;
1545 return TRUE;
1546 }
1547
1548 for (std::wstring::iterator it = wChange.begin(); it != wChange.end(); it++) {
1549 if (iIndexMask >= wstrMaskLen) {
1550 Alert(pContext,
1551 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1552 pEvent->Rc() = FALSE;
1553 return TRUE;
1554 }
1555 wchar_t w_Mask = wstrMask[iIndexMask];
1556 if (!isReservedMaskChar(w_Mask)) {
1557 *it = w_Mask;
1558 }
1559 wchar_t w_Change = *it;
1560 if (!maskSatisfied(w_Change, w_Mask)) {
1561 pEvent->Rc() = FALSE;
1562 return TRUE;
1563 }
1564 iIndexMask++;
1565 }
1566
1567 wideChange = wChange.c_str();
1568 return TRUE;
1569 }
1570
1571 // function AFSpecial_Keystroke(psf)
1572 FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(
1573 IJS_Context* cc,
1574 const std::vector<CJS_Value>& params,
1575 CJS_Value& vRet,
1576 CFX_WideString& sError) {
1577 CJS_Context* pContext = (CJS_Context*)cc;
1578 if (params.size() != 1) {
1579 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1580 return FALSE;
1581 }
1582
1583 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1584 if (!pEvent->m_pValue)
1585 return FALSE;
1586
1587 std::string cFormat;
1588 int iIndex = params[0].ToInt();
1589 CFX_WideString& val = pEvent->Value();
1590 std::string strSrc = CFX_ByteString::FromUnicode(val).c_str();
1591 std::wstring wstrChange = pEvent->Change().c_str();
1592
1593 switch (iIndex) {
1594 case 0:
1595 cFormat = "99999";
1596 break;
1597 case 1:
1598 // cFormat = "99999-9999";
1599 cFormat = "999999999";
1600 break;
1601 case 2: {
1602 std::string NumberStr;
1603 util::printx("9999999999", strSrc, NumberStr);
1604 if (strSrc.length() + wstrChange.length() > 7)
1605 // cFormat = "(999) 999-9999";
1606 cFormat = "9999999999";
1607 else
1608 // cFormat = "999-9999";
1609 cFormat = "9999999";
1610 break;
1611 }
1612 case 3:
1613 // cFormat = "999-99-9999";
1614 cFormat = "999999999";
1615 break;
1616 }
1617
1618 std::vector<CJS_Value> params2;
1619 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat.c_str()));
1620 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
1621 }
1622
1623 FX_BOOL CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
1624 const std::vector<CJS_Value>& params,
1625 CJS_Value& vRet,
1626 CFX_WideString& sError) {
1627 CJS_Context* pContext = (CJS_Context*)cc;
1628 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
1629
1630 if (params.size() != 1) {
1631 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1632 return FALSE;
1633 }
1634
1635 CFX_WideString swValue;
1636 if (pEventHandler->m_pValue)
1637 swValue = pEventHandler->Value();
1638
1639 if (pEventHandler->WillCommit()) {
1640 vRet = swValue.c_str();
1641 return TRUE;
1642 }
1643
1644 CFX_WideString prefix, postfix;
1645
1646 if (pEventHandler->SelStart() >= 0)
1647 prefix = swValue.Mid(0, pEventHandler->SelStart());
1648 else
1649 prefix = L"";
1650
1651 if (pEventHandler->SelEnd() >= 0 &&
1652 pEventHandler->SelEnd() <= swValue.GetLength())
1653 postfix = swValue.Mid(pEventHandler->SelEnd(),
1654 swValue.GetLength() - pEventHandler->SelEnd());
1655 else
1656 postfix = L"";
1657
1658 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1659
1660 return TRUE;
1661 }
1662
1663 FX_BOOL CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
1664 const std::vector<CJS_Value>& params,
1665 CJS_Value& vRet,
1666 CFX_WideString& sError) {
1667 CJS_Context* pContext = (CJS_Context*)cc;
1668 ASSERT(pContext);
1669
1670 if (params.size() != 2) {
1671 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1672 return FALSE;
1673 }
1674
1675 CFX_WideString sValue = params[0].ToCFXWideString();
1676 CFX_WideString sFormat = params[1].ToCFXWideString();
1677
1678 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
1679
1680 if (JS_PortIsNan(dDate)) {
1681 CFX_WideString swMsg;
1682 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1683 sFormat.c_str());
1684 Alert((CJS_Context*)cc, swMsg.c_str());
1685 return FALSE;
1686 }
1687
1688 vRet = dDate;
1689 return TRUE;
1690 }
1691
1692 FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc,
1693 const std::vector<CJS_Value>& params,
1694 CJS_Value& vRet,
1695 CFX_WideString& sError) {
1696 if (params.size() != 3) {
1697 CJS_Context* pContext = (CJS_Context*)cc;
1698 ASSERT(pContext);
1699
1700 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1701 return FALSE;
1702 }
1703
1704 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(),
1705 params[1].ToDouble(), params[2].ToDouble());
1706 return TRUE;
1707 }
1708
1709 FX_BOOL CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
1710 const std::vector<CJS_Value>& params,
1711 CJS_Value& vRet,
1712 CFX_WideString& sError) {
1713 CJS_Context* pContext = (CJS_Context*)cc;
1714 if (params.size() != 1) {
1715 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1716 return FALSE;
1717 }
1718 CFX_WideString ws = params[0].ToCFXWideString();
1719 ws.Replace(L",", L".");
1720 vRet = ws;
1721 vRet.MaybeCoerceToNumber();
1722 if (vRet.GetType() != CJS_Value::VT_number)
1723 vRet = 0;
1724 return TRUE;
1725 }
1726
1727 FX_BOOL CJS_PublicMethods::AFSimple_Calculate(
1728 IJS_Context* cc,
1729 const std::vector<CJS_Value>& params,
1730 CJS_Value& vRet,
1731 CFX_WideString& sError) {
1732 CJS_Context* pContext = (CJS_Context*)cc;
1733 if (params.size() != 2) {
1734 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1735 return FALSE;
1736 }
1737
1738 CJS_Value params1 = params[1];
1739 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
1740 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1741 return FALSE;
1742 }
1743
1744 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
1745 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
1746 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
1747
1748 CFX_WideString sFunction = params[0].ToCFXWideString();
1749 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
1750
1751 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1752 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
1753 int nFieldsCount = 0;
1754
1755 for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
1756 CJS_Value jsValue(pRuntime);
1757 FieldNameArray.GetElement(i, jsValue);
1758 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
1759
1760 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1761 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1762 double dTemp = 0.0;
1763 switch (pFormField->GetFieldType()) {
1764 case FIELDTYPE_TEXTFIELD:
1765 case FIELDTYPE_COMBOBOX: {
1766 CFX_WideString trimmed = pFormField->GetValue();
1767 trimmed.TrimRight();
1768 trimmed.TrimLeft();
1769 dTemp = FX_atof(trimmed);
1770 } break;
1771 case FIELDTYPE_PUSHBUTTON: {
1772 dTemp = 0.0;
1773 } break;
1774 case FIELDTYPE_CHECKBOX:
1775 case FIELDTYPE_RADIOBUTTON: {
1776 dTemp = 0.0;
1777 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1778 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1779 if (pFormCtrl->IsChecked()) {
1780 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1781 trimmed.TrimRight();
1782 trimmed.TrimLeft();
1783 dTemp = FX_atof(trimmed);
1784 break;
1785 }
1786 }
1787 }
1788 } break;
1789 case FIELDTYPE_LISTBOX: {
1790 if (pFormField->CountSelectedItems() <= 1) {
1791 CFX_WideString trimmed = pFormField->GetValue();
1792 trimmed.TrimRight();
1793 trimmed.TrimLeft();
1794 dTemp = FX_atof(trimmed);
1795 }
1796 } break;
1797 default:
1798 break;
1799 }
1800
1801 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1802 wcscmp(sFunction.c_str(), L"MAX") == 0))
1803 dValue = dTemp;
1804
1805 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1806
1807 nFieldsCount++;
1808 }
1809 }
1810 }
1811
1812 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1813 dValue /= nFieldsCount;
1814
1815 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1816 FXSYS_pow((double)10, (double)6);
1817 CJS_Value jsValue(pRuntime, dValue);
1818 if (pContext->GetEventHandler()->m_pValue)
1819 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString();
1820
1821 return TRUE;
1822 }
1823
1824 /* This function validates the current event to ensure that its value is
1825 ** within the specified range. */
1826
1827 FX_BOOL CJS_PublicMethods::AFRange_Validate(
1828 IJS_Context* cc,
1829 const std::vector<CJS_Value>& params,
1830 CJS_Value& vRet,
1831 CFX_WideString& sError) {
1832 CJS_Context* pContext = (CJS_Context*)cc;
1833 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1834
1835 if (params.size() != 4) {
1836 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1837 return FALSE;
1838 }
1839
1840 if (!pEvent->m_pValue)
1841 return FALSE;
1842 if (pEvent->Value().IsEmpty())
1843 return TRUE;
1844 double dEentValue = atof(CFX_ByteString::FromUnicode(pEvent->Value()));
1845 FX_BOOL bGreaterThan = params[0].ToBool();
1846 double dGreaterThan = params[1].ToDouble();
1847 FX_BOOL bLessThan = params[2].ToBool();
1848 double dLessThan = params[3].ToDouble();
1849 CFX_WideString swMsg;
1850
1851 if (bGreaterThan && bLessThan) {
1852 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
1853 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str(),
1854 params[1].ToCFXWideString().c_str(),
1855 params[3].ToCFXWideString().c_str());
1856 } else if (bGreaterThan) {
1857 if (dEentValue < dGreaterThan)
1858 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(),
1859 params[1].ToCFXWideString().c_str());
1860 } else if (bLessThan) {
1861 if (dEentValue > dLessThan)
1862 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(),
1863 params[3].ToCFXWideString().c_str());
1864 }
1865
1866 if (!swMsg.IsEmpty()) {
1867 Alert(pContext, swMsg.c_str());
1868 pEvent->Rc() = FALSE;
1869 }
1870 return TRUE;
1871 }
1872
1873 FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
1874 const std::vector<CJS_Value>& params,
1875 CJS_Value& vRet,
1876 CFX_WideString& sError) {
1877 CJS_Context* pContext = (CJS_Context*)cc;
1878 if (params.size() != 1) {
1879 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1880 return FALSE;
1881 }
1882
1883 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1884 CJS_Array nums(pRuntime);
1885
1886 CFX_WideString str = params[0].ToCFXWideString();
1887 CFX_WideString sPart;
1888
1889 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1890 str = L"0" + str;
1891
1892 int nIndex = 0;
1893 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1894 FX_WCHAR wc = str.GetAt(i);
1895 if (FXSYS_iswdigit(wc)) {
1896 sPart += wc;
1897 } else {
1898 if (sPart.GetLength() > 0) {
1899 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
1900 sPart = L"";
1901 nIndex++;
1902 }
1903 }
1904 }
1905
1906 if (sPart.GetLength() > 0) {
1907 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
1908 }
1909
1910 if (nums.GetLength() > 0)
1911 vRet = nums;
1912 else
1913 vRet.SetNull();
1914
1915 return TRUE;
1916 }
OLDNEW
« no previous file with comments | « fpdfsdk/src/javascript/PublicMethods.h ('k') | fpdfsdk/src/javascript/app.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698