Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "fpdfsdk/javascript/PublicMethods.h" | 7 #include "fpdfsdk/javascript/PublicMethods.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <iomanip> | |
| 11 #include <string> | |
| 12 #include <sstream> | |
| 10 #include <vector> | 13 #include <vector> |
| 11 | 14 |
| 12 #include "core/fpdfdoc/cpdf_interform.h" | 15 #include "core/fpdfdoc/cpdf_interform.h" |
| 13 #include "core/fxcrt/fx_ext.h" | 16 #include "core/fxcrt/fx_ext.h" |
| 14 #include "fpdfsdk/cpdfsdk_formfillenvironment.h" | 17 #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
| 15 #include "fpdfsdk/cpdfsdk_interform.h" | 18 #include "fpdfsdk/cpdfsdk_interform.h" |
| 16 #include "fpdfsdk/javascript/Field.h" | 19 #include "fpdfsdk/javascript/Field.h" |
| 17 #include "fpdfsdk/javascript/JS_Define.h" | 20 #include "fpdfsdk/javascript/JS_Define.h" |
| 18 #include "fpdfsdk/javascript/JS_EventHandler.h" | 21 #include "fpdfsdk/javascript/JS_EventHandler.h" |
| 19 #include "fpdfsdk/javascript/JS_Object.h" | 22 #include "fpdfsdk/javascript/JS_Object.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 result.TrimRight(' '); | 80 result.TrimRight(' '); |
| 78 return result; | 81 return result; |
| 79 } | 82 } |
| 80 | 83 |
| 81 void AlertIfPossible(CJS_Context* pContext, const FX_WCHAR* swMsg) { | 84 void AlertIfPossible(CJS_Context* pContext, const FX_WCHAR* swMsg) { |
| 82 CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv(); | 85 CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv(); |
| 83 if (pFormFillEnv) | 86 if (pFormFillEnv) |
| 84 pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3); | 87 pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3); |
| 85 } | 88 } |
| 86 | 89 |
| 90 #if _FX_OS_ != _FX_ANDROID_ | |
| 91 CFX_ByteString CalculateString(double dValue, | |
| 92 int iDec, | |
| 93 int* iDec2, | |
| 94 int* iNegative) { | |
|
Tom Sepez
2016/11/15 22:37:30
nit: maybe iNegative should be a bool*.
npm
2016/11/15 22:54:13
Done.
| |
| 95 *iNegative = dValue < 0 ? 1 : 0; | |
|
Tom Sepez
2016/11/15 22:37:30
then its just *iNegative = dvalue < 0; if bool use
npm
2016/11/15 22:54:13
Done.
| |
| 96 if (*iNegative) | |
| 97 dValue = -dValue; | |
| 98 std::stringstream ss; | |
| 99 ss << std::fixed << std::setprecision(iDec) << dValue; | |
| 100 std::string stringValue = ss.str(); | |
| 101 size_t iDecimalPos = stringValue.find("."); | |
| 102 *iDec2 = iDecimalPos == std::string::npos ? stringValue.size() | |
| 103 : static_cast<int>(iDecimalPos); | |
| 104 return CFX_ByteString(stringValue.c_str()); | |
| 105 } | |
| 106 #endif | |
| 107 | |
| 87 } // namespace | 108 } // namespace |
| 88 | 109 |
| 89 bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) { | 110 bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) { |
| 90 CFX_WideString sTrim = StrTrim(str); | 111 CFX_WideString sTrim = StrTrim(str); |
| 91 const FX_WCHAR* pTrim = sTrim.c_str(); | 112 const FX_WCHAR* pTrim = sTrim.c_str(); |
| 92 const FX_WCHAR* p = pTrim; | 113 const FX_WCHAR* p = pTrim; |
| 93 bool bDot = false; | 114 bool bDot = false; |
| 94 bool bKXJS = false; | 115 bool bKXJS = false; |
| 95 | 116 |
| 96 wchar_t c; | 117 wchar_t c; |
| (...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 753 | 774 |
| 754 if (iDec < 0) | 775 if (iDec < 0) |
| 755 iDec = -iDec; | 776 iDec = -iDec; |
| 756 | 777 |
| 757 if (iSepStyle < 0 || iSepStyle > 3) | 778 if (iSepStyle < 0 || iSepStyle > 3) |
| 758 iSepStyle = 0; | 779 iSepStyle = 0; |
| 759 | 780 |
| 760 if (iNegStyle < 0 || iNegStyle > 3) | 781 if (iNegStyle < 0 || iNegStyle > 3) |
| 761 iNegStyle = 0; | 782 iNegStyle = 0; |
| 762 | 783 |
| 763 // for processing decimal places | 784 // Processing decimal places |
| 764 strValue.Replace(",", "."); | 785 strValue.Replace(",", "."); |
| 765 double dValue = atof(strValue.c_str()); | 786 double dValue = atof(strValue.c_str()); |
| 766 if (iDec > 0) | 787 if (iDec > 0) |
| 767 dValue += DOUBLE_CORRECT; | 788 dValue += DOUBLE_CORRECT; |
| 768 | 789 |
| 790 // Calculating number string | |
| 791 int iNegative; | |
| 769 int iDec2; | 792 int iDec2; |
| 770 int iNegative = 0; | 793 strValue = CalculateString(dValue, iDec, &iDec2, &iNegative); |
| 771 | |
| 772 strValue = fcvt(dValue, iDec, &iDec2, &iNegative); | |
| 773 if (strValue.IsEmpty()) { | 794 if (strValue.IsEmpty()) { |
| 774 dValue = 0; | 795 dValue = 0; |
| 775 strValue = fcvt(dValue, iDec, &iDec2, &iNegative); | 796 strValue = CalculateString(dValue, iDec, &iDec2, &iNegative); |
| 776 if (strValue.IsEmpty()) { | 797 if (strValue.IsEmpty()) { |
| 777 strValue = "0"; | 798 strValue = "0"; |
| 778 iDec2 = 1; | 799 iDec2 = 1; |
| 779 } | 800 } |
| 780 } | 801 } |
| 781 | 802 |
| 782 if (iDec2 < 0) { | 803 // Processing separator style |
| 783 for (int iNum = 0; iNum < abs(iDec2); iNum++) { | 804 if (iDec2 < strValue.GetLength()) { |
| 784 strValue = "0" + strValue; | 805 if (iSepStyle == 2 || iSepStyle == 3) |
| 785 } | 806 strValue.Replace(".", ","); |
| 786 iDec2 = 0; | |
| 787 } | |
| 788 int iMax = strValue.GetLength(); | |
| 789 if (iDec2 > iMax) { | |
| 790 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) { | |
| 791 strValue += "0"; | |
| 792 } | |
| 793 iMax = iDec2 + 1; | |
| 794 } | |
| 795 | |
| 796 // for processing seperator style | |
| 797 if (iDec2 < iMax) { | |
| 798 if (iSepStyle == 0 || iSepStyle == 1) { | |
| 799 strValue.Insert(iDec2, '.'); | |
| 800 iMax++; | |
| 801 } else if (iSepStyle == 2 || iSepStyle == 3) { | |
| 802 strValue.Insert(iDec2, ','); | |
| 803 iMax++; | |
| 804 } | |
| 805 | 807 |
| 806 if (iDec2 == 0) | 808 if (iDec2 == 0) |
| 807 strValue.Insert(iDec2, '0'); | 809 strValue.Insert(iDec2, '0'); |
| 808 } | 810 } |
| 809 if (iSepStyle == 0 || iSepStyle == 2) { | 811 if (iSepStyle == 0 || iSepStyle == 2) { |
| 810 char cSeperator; | 812 char cSeparator; |
| 811 if (iSepStyle == 0) | 813 if (iSepStyle == 0) |
| 812 cSeperator = ','; | 814 cSeparator = ','; |
| 813 else | 815 else |
| 814 cSeperator = '.'; | 816 cSeparator = '.'; |
| 815 | 817 |
| 816 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) { | 818 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) |
| 817 strValue.Insert(iDecPositive, cSeperator); | 819 strValue.Insert(iDecPositive, cSeparator); |
| 818 iMax++; | |
| 819 } | |
| 820 } | 820 } |
| 821 | 821 |
| 822 // for processing currency string | 822 // Processing currency string |
| 823 Value = CFX_WideString::FromLocal(strValue.AsStringC()); | 823 Value = CFX_WideString::FromLocal(strValue.AsStringC()); |
| 824 | 824 |
| 825 if (bCurrencyPrepend) | 825 if (bCurrencyPrepend) |
| 826 Value = wstrCurrency + Value; | 826 Value = wstrCurrency + Value; |
| 827 else | 827 else |
| 828 Value = Value + wstrCurrency; | 828 Value = Value + wstrCurrency; |
| 829 | 829 |
| 830 // for processing negative style | 830 // Processing negative style |
| 831 if (iNegative) { | 831 if (iNegative) { |
| 832 if (iNegStyle == 0) { | 832 if (iNegStyle == 0) |
| 833 Value = L"-" + Value; | 833 Value = L"-" + Value; |
| 834 } else if (iNegStyle == 2 || iNegStyle == 3) { | 834 else if (iNegStyle == 2 || iNegStyle == 3) |
| 835 Value = L"(" + Value + L")"; | 835 Value = L"(" + Value + L")"; |
| 836 } | |
| 837 if (iNegStyle == 1 || iNegStyle == 3) { | 836 if (iNegStyle == 1 || iNegStyle == 3) { |
| 838 if (Field* fTarget = pEvent->Target_Field()) { | 837 if (Field* fTarget = pEvent->Target_Field()) { |
| 839 CJS_Array arColor; | 838 CJS_Array arColor; |
| 840 CJS_Value vColElm(pRuntime); | 839 CJS_Value vColElm(pRuntime); |
| 841 vColElm = CJS_Value(pRuntime, L"RGB"); | 840 vColElm = CJS_Value(pRuntime, L"RGB"); |
| 842 arColor.SetElement(pRuntime, 0, vColElm); | 841 arColor.SetElement(pRuntime, 0, vColElm); |
| 843 vColElm = CJS_Value(pRuntime, 1); | 842 vColElm = CJS_Value(pRuntime, 1); |
| 844 arColor.SetElement(pRuntime, 1, vColElm); | 843 arColor.SetElement(pRuntime, 1, vColElm); |
| 845 vColElm = CJS_Value(pRuntime, 0); | 844 vColElm = CJS_Value(pRuntime, 0); |
| 846 arColor.SetElement(pRuntime, 2, vColElm); | 845 arColor.SetElement(pRuntime, 2, vColElm); |
| (...skipping 967 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1814 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str())); | 1813 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str())); |
| 1815 } | 1814 } |
| 1816 | 1815 |
| 1817 if (nums.GetLength(pRuntime) > 0) | 1816 if (nums.GetLength(pRuntime) > 0) |
| 1818 vRet = CJS_Value(pRuntime, nums); | 1817 vRet = CJS_Value(pRuntime, nums); |
| 1819 else | 1818 else |
| 1820 vRet.SetNull(pRuntime); | 1819 vRet.SetNull(pRuntime); |
| 1821 | 1820 |
| 1822 return true; | 1821 return true; |
| 1823 } | 1822 } |
| OLD | NEW |