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 #ifndef _FX_LOCALE_H_
8 #define _FX_LOCALE_H_
9 class CFX_Unitime;
10 class IFX_Locale;
11 class IFX_FormatString;
12 class IFX_LocaleMgr;
13 enum FX_LOCALENUMSYMBOL {
14   FX_LOCALENUMSYMBOL_Decimal,
15   FX_LOCALENUMSYMBOL_Grouping,
16   FX_LOCALENUMSYMBOL_Percent,
17   FX_LOCALENUMSYMBOL_Minus,
18   FX_LOCALENUMSYMBOL_Zero,
19   FX_LOCALENUMSYMBOL_CurrencySymbol,
20   FX_LOCALENUMSYMBOL_CurrencyName,
21 };
22 enum FX_LOCALEDATETIMESUBCATEGORY {
23   FX_LOCALEDATETIMESUBCATEGORY_Default,
24   FX_LOCALEDATETIMESUBCATEGORY_Short,
25   FX_LOCALEDATETIMESUBCATEGORY_Medium,
26   FX_LOCALEDATETIMESUBCATEGORY_Full,
27   FX_LOCALEDATETIMESUBCATEGORY_Long,
28 };
29 enum FX_LOCALENUMSUBCATEGORY {
30   FX_LOCALENUMPATTERN_Percent,
31   FX_LOCALENUMPATTERN_Currency,
32   FX_LOCALENUMPATTERN_Decimal,
33   FX_LOCALENUMPATTERN_Integer,
34 };
35 enum FX_LOCALECATEGORY {
36   FX_LOCALECATEGORY_Unknown,
37   FX_LOCALECATEGORY_Date,
38   FX_LOCALECATEGORY_Time,
39   FX_LOCALECATEGORY_DateTime,
40   FX_LOCALECATEGORY_Num,
41   FX_LOCALECATEGORY_Text,
42   FX_LOCALECATEGORY_Zero,
43   FX_LOCALECATEGORY_Null,
44 };
45 enum FX_DATETIMETYPE {
46   FX_DATETIMETYPE_Unknown,
47   FX_DATETIMETYPE_Date,
48   FX_DATETIMETYPE_Time,
49   FX_DATETIMETYPE_DateTime,
50   FX_DATETIMETYPE_TimeDate,
51 };
52 
53 class IFX_Locale {
54  public:
55   static IFX_Locale* Create(CXML_Element* pLocaleData);
56 
~IFX_Locale()57   virtual ~IFX_Locale() {}
58   virtual void Release() = 0;
59 
60   virtual CFX_WideString GetName() = 0;
61 
62   virtual void GetNumbericSymbol(FX_LOCALENUMSYMBOL eType,
63                                  CFX_WideString& wsNumSymbol) const = 0;
64   virtual void GetDateTimeSymbols(CFX_WideString& wsDtSymbol) const = 0;
65   virtual void GetMonthName(int32_t nMonth,
66                             CFX_WideString& wsMonthName,
67                             FX_BOOL bAbbr = TRUE) const = 0;
68   virtual void GetDayName(int32_t nWeek,
69                           CFX_WideString& wsDayName,
70                           FX_BOOL bAbbr = TRUE) const = 0;
71   virtual void GetMeridiemName(CFX_WideString& wsMeridiemName,
72                                FX_BOOL bAM = TRUE) const = 0;
73   virtual void GetTimeZone(FX_TIMEZONE& tz) const = 0;
74   virtual void GetEraName(CFX_WideString& wsEraName,
75                           FX_BOOL bAD = TRUE) const = 0;
76   virtual void GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY eType,
77                               CFX_WideString& wsPattern) const = 0;
78   virtual void GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY eType,
79                               CFX_WideString& wsPattern) const = 0;
80   virtual void GetNumPattern(FX_LOCALENUMSUBCATEGORY eType,
81                              CFX_WideString& wsPattern) const = 0;
82 };
83 
84 class IFX_LocaleMgr {
85  public:
~IFX_LocaleMgr()86   virtual ~IFX_LocaleMgr() {}
87   virtual void Release() = 0;
88   virtual FX_WORD GetDefLocaleID() = 0;
89   virtual IFX_Locale* GetDefLocale() = 0;
90   virtual IFX_Locale* GetLocale(FX_WORD lcid) = 0;
91   virtual IFX_Locale* GetLocaleByName(const CFX_WideStringC& wsLocaleName) = 0;
92 };
93 IFX_LocaleMgr* FX_LocaleMgr_Create(const FX_WCHAR* pszLocalPath,
94                                    FX_WORD wDefaultLCID);
95 void FX_ParseNumString(const CFX_WideString& wsNum, CFX_WideString& wsResult);
96 FX_BOOL FX_DateFromCanonical(const CFX_WideString& wsDate,
97                              CFX_Unitime& datetime);
98 FX_BOOL FX_TimeFromCanonical(const CFX_WideStringC& wsTime,
99                              CFX_Unitime& datetime,
100                              IFX_Locale* pLocale);
101 class IFX_FormatString {
102  public:
103   static IFX_FormatString* Create(IFX_LocaleMgr* pLocaleMgr, FX_BOOL bUseLCID);
104 
~IFX_FormatString()105   virtual ~IFX_FormatString() {}
106   virtual void Release() = 0;
107   virtual void SplitFormatString(const CFX_WideString& wsFormatString,
108                                  CFX_WideStringArray& wsPatterns) = 0;
109   virtual FX_LOCALECATEGORY GetCategory(const CFX_WideString& wsPattern) = 0;
110   virtual FX_WORD GetLCID(const CFX_WideString& wsPattern) = 0;
111   virtual CFX_WideString GetLocaleName(const CFX_WideString& wsPattern) = 0;
112   virtual FX_BOOL ParseText(const CFX_WideString& wsSrcText,
113                             const CFX_WideString& wsPattern,
114                             CFX_WideString& wsValue) = 0;
115   virtual FX_BOOL ParseNum(const CFX_WideString& wsSrcNum,
116                            const CFX_WideString& wsPattern,
117                            FX_FLOAT& fValue) = 0;
118   virtual FX_BOOL ParseNum(const CFX_WideString& wsSrcNum,
119                            const CFX_WideString& wsPattern,
120                            CFX_WideString& wsValue) = 0;
121   virtual FX_BOOL ParseDateTime(const CFX_WideString& wsSrcDateTime,
122                                 const CFX_WideString& wsPattern,
123                                 FX_DATETIMETYPE eDateTimeType,
124                                 CFX_Unitime& dtValue) = 0;
125   virtual FX_BOOL ParseZero(const CFX_WideString& wsSrcText,
126                             const CFX_WideString& wsPattern) = 0;
127   virtual FX_BOOL ParseNull(const CFX_WideString& wsSrcText,
128                             const CFX_WideString& wsPattern) = 0;
129   virtual FX_BOOL FormatText(const CFX_WideString& wsSrcText,
130                              const CFX_WideString& wsPattern,
131                              CFX_WideString& wsOutput) = 0;
132   virtual FX_BOOL FormatNum(const CFX_WideString& wsSrcNum,
133                             const CFX_WideString& wsPattern,
134                             CFX_WideString& wsOutput) = 0;
135   virtual FX_BOOL FormatNum(FX_FLOAT fNum,
136                             const CFX_WideString& wsPattern,
137                             CFX_WideString& wsOutput) = 0;
138   virtual FX_BOOL FormatDateTime(const CFX_WideString& wsSrcDateTime,
139                                  const CFX_WideString& wsPattern,
140                                  CFX_WideString& wsOutput) = 0;
141   virtual FX_BOOL FormatDateTime(const CFX_WideString& wsSrcDateTime,
142                                  const CFX_WideString& wsPattern,
143                                  CFX_WideString& wsOutput,
144                                  FX_DATETIMETYPE eDateTimeType) = 0;
145   virtual FX_BOOL FormatDateTime(const CFX_Unitime& dt,
146                                  const CFX_WideString& wsPattern,
147                                  CFX_WideString& wsOutput) = 0;
148   virtual FX_BOOL FormatZero(const CFX_WideString& wsPattern,
149                              CFX_WideString& wsOutput) = 0;
150   virtual FX_BOOL FormatNull(const CFX_WideString& wsPattern,
151                              CFX_WideString& wsOutput) = 0;
152 };
153 class CFX_Decimal {
154  public:
155   CFX_Decimal();
156   CFX_Decimal(uint32_t val);
157   CFX_Decimal(uint64_t val);
158   CFX_Decimal(int32_t val);
159   CFX_Decimal(int64_t val);
160   CFX_Decimal(FX_FLOAT val, uint8_t scale = 3);
161   CFX_Decimal(const CFX_WideStringC& str);
162   CFX_Decimal(const CFX_ByteStringC& str);
163   operator CFX_WideString() const;
164   operator double() const;
165   FX_BOOL operator==(const CFX_Decimal& val) const;
166   FX_BOOL operator<=(const CFX_Decimal& val) const;
167   FX_BOOL operator>=(const CFX_Decimal& val) const;
168   FX_BOOL operator!=(const CFX_Decimal& val) const;
169   FX_BOOL operator<(const CFX_Decimal& val) const;
170   FX_BOOL operator>(const CFX_Decimal& val) const;
171   CFX_Decimal operator+(const CFX_Decimal& val) const;
172   CFX_Decimal operator-(const CFX_Decimal& val) const;
173   CFX_Decimal operator*(const CFX_Decimal& val) const;
174   CFX_Decimal operator/(const CFX_Decimal& val) const;
175   CFX_Decimal operator%(const CFX_Decimal& val) const;
176   void SetScale(uint8_t newScale);
177   uint8_t GetScale();
178   void SetAbs();
179   void SetNegate();
180   void SetFloor();
181   void SetCeiling();
182   void SetTruncate();
183 
184  protected:
185   CFX_Decimal(uint32_t hi,
186               uint32_t mid,
187               uint32_t lo,
188               FX_BOOL neg,
189               uint8_t scale);
IsNotZero()190   inline FX_BOOL IsNotZero() const { return m_uHi || m_uMid || m_uLo; }
191   inline int8_t Compare(const CFX_Decimal& val) const;
192   inline void Swap(CFX_Decimal& val);
193   inline void FloorOrCeil(FX_BOOL bFloor);
194   CFX_Decimal AddOrMinus(const CFX_Decimal& val, FX_BOOL isAdding) const;
195   CFX_Decimal Multiply(const CFX_Decimal& val) const;
196   CFX_Decimal Divide(const CFX_Decimal& val) const;
197   CFX_Decimal Modulus(const CFX_Decimal& val) const;
198   uint32_t m_uFlags;
199   uint32_t m_uHi;
200   uint32_t m_uLo;
201   uint32_t m_uMid;
202 };
203 #endif
204