OLD | NEW |
| (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 "xfa/src/fxfa/parser/xfa_utils.h" | |
8 | |
9 #include "core/include/fxcrt/fx_ext.h" | |
10 #include "xfa/src/fxfa/fm2js/xfa_fm2jsapi.h" | |
11 #include "xfa/src/fxfa/parser/xfa_docdata.h" | |
12 #include "xfa/src/fxfa/parser/xfa_doclayout.h" | |
13 #include "xfa/src/fxfa/parser/xfa_document.h" | |
14 #include "xfa/src/fxfa/parser/xfa_localemgr.h" | |
15 #include "xfa/src/fxfa/parser/xfa_localevalue.h" | |
16 #include "xfa/src/fxfa/parser/xfa_object.h" | |
17 #include "xfa/src/fxfa/parser/xfa_parser.h" | |
18 #include "xfa/src/fxfa/parser/xfa_script.h" | |
19 | |
20 CXFA_Node* XFA_CreateUIChild(CXFA_Node* pNode, XFA_ELEMENT& eWidgetType) { | |
21 XFA_ELEMENT eType = pNode->GetClassID(); | |
22 eWidgetType = eType; | |
23 if (eType != XFA_ELEMENT_Field && eType != XFA_ELEMENT_Draw) { | |
24 return NULL; | |
25 } | |
26 eWidgetType = XFA_ELEMENT_UNKNOWN; | |
27 XFA_ELEMENT eUIType = XFA_ELEMENT_UNKNOWN; | |
28 CXFA_Value defValue(pNode->GetProperty(0, XFA_ELEMENT_Value, TRUE)); | |
29 XFA_ELEMENT eValueType = (XFA_ELEMENT)defValue.GetChildValueClassID(); | |
30 switch (eValueType) { | |
31 case XFA_ELEMENT_Boolean: | |
32 eUIType = XFA_ELEMENT_CheckButton; | |
33 break; | |
34 case XFA_ELEMENT_Integer: | |
35 case XFA_ELEMENT_Decimal: | |
36 case XFA_ELEMENT_Float: | |
37 eUIType = XFA_ELEMENT_NumericEdit; | |
38 break; | |
39 case XFA_ELEMENT_ExData: | |
40 case XFA_ELEMENT_Text: | |
41 eUIType = XFA_ELEMENT_TextEdit; | |
42 eWidgetType = XFA_ELEMENT_Text; | |
43 break; | |
44 case XFA_ELEMENT_Date: | |
45 case XFA_ELEMENT_Time: | |
46 case XFA_ELEMENT_DateTime: | |
47 eUIType = XFA_ELEMENT_DateTimeEdit; | |
48 break; | |
49 case XFA_ELEMENT_Image: | |
50 eUIType = XFA_ELEMENT_ImageEdit; | |
51 eWidgetType = XFA_ELEMENT_Image; | |
52 break; | |
53 case XFA_ELEMENT_Arc: | |
54 case XFA_ELEMENT_Line: | |
55 case XFA_ELEMENT_Rectangle: | |
56 eUIType = XFA_ELEMENT_DefaultUi; | |
57 eWidgetType = eValueType; | |
58 break; | |
59 default: | |
60 break; | |
61 } | |
62 CXFA_Node* pUIChild = NULL; | |
63 CXFA_Node* pUI = pNode->GetProperty(0, XFA_ELEMENT_Ui, TRUE); | |
64 CXFA_Node* pChild = pUI->GetNodeItem(XFA_NODEITEM_FirstChild); | |
65 for (; pChild; pChild = pChild->GetNodeItem(XFA_NODEITEM_NextSibling)) { | |
66 XFA_ELEMENT eChild = pChild->GetClassID(); | |
67 if (eChild == XFA_ELEMENT_Extras || eChild == XFA_ELEMENT_Picture) { | |
68 continue; | |
69 } | |
70 const XFA_PROPERTY* pProperty = | |
71 XFA_GetPropertyOfElement(XFA_ELEMENT_Ui, eChild, XFA_XDPPACKET_Form); | |
72 if (pProperty && (pProperty->uFlags & XFA_PROPERTYFLAG_OneOf)) { | |
73 pUIChild = pChild; | |
74 break; | |
75 } | |
76 } | |
77 if (eType == XFA_ELEMENT_Draw) { | |
78 XFA_ELEMENT eDraw = pUIChild ? pUIChild->GetClassID() : XFA_ELEMENT_UNKNOWN; | |
79 switch (eDraw) { | |
80 case XFA_ELEMENT_TextEdit: | |
81 eWidgetType = XFA_ELEMENT_Text; | |
82 break; | |
83 case XFA_ELEMENT_ImageEdit: | |
84 eWidgetType = XFA_ELEMENT_Image; | |
85 break; | |
86 default: | |
87 eWidgetType = | |
88 eWidgetType == XFA_ELEMENT_UNKNOWN ? XFA_ELEMENT_Text : eWidgetType; | |
89 break; | |
90 } | |
91 } else { | |
92 if (pUIChild && pUIChild->GetClassID() == XFA_ELEMENT_DefaultUi) { | |
93 eWidgetType = XFA_ELEMENT_TextEdit; | |
94 } else { | |
95 eWidgetType = pUIChild | |
96 ? pUIChild->GetClassID() | |
97 : (eUIType == XFA_ELEMENT_UNKNOWN ? XFA_ELEMENT_TextEdit | |
98 : eUIType); | |
99 } | |
100 } | |
101 if (!pUIChild) { | |
102 if (eUIType == XFA_ELEMENT_UNKNOWN) { | |
103 eUIType = XFA_ELEMENT_TextEdit; | |
104 defValue.GetNode()->GetProperty(0, XFA_ELEMENT_Text, TRUE); | |
105 } | |
106 pUIChild = pUI->GetProperty(0, eUIType, TRUE); | |
107 } else if (eUIType == XFA_ELEMENT_UNKNOWN) { | |
108 switch (pUIChild->GetClassID()) { | |
109 case XFA_ELEMENT_CheckButton: { | |
110 eValueType = XFA_ELEMENT_Text; | |
111 if (CXFA_Node* pItems = pNode->GetChild(0, XFA_ELEMENT_Items)) { | |
112 if (CXFA_Node* pItem = pItems->GetChild(0, XFA_ELEMENT_UNKNOWN)) { | |
113 eValueType = pItem->GetClassID(); | |
114 } | |
115 } | |
116 } break; | |
117 case XFA_ELEMENT_DateTimeEdit: | |
118 eValueType = XFA_ELEMENT_DateTime; | |
119 break; | |
120 case XFA_ELEMENT_ImageEdit: | |
121 eValueType = XFA_ELEMENT_Image; | |
122 break; | |
123 case XFA_ELEMENT_NumericEdit: | |
124 eValueType = XFA_ELEMENT_Float; | |
125 break; | |
126 case XFA_ELEMENT_ChoiceList: { | |
127 eValueType = (pUIChild->GetEnum(XFA_ATTRIBUTE_Open) == | |
128 XFA_ATTRIBUTEENUM_MultiSelect) | |
129 ? XFA_ELEMENT_ExData | |
130 : XFA_ELEMENT_Text; | |
131 } break; | |
132 case XFA_ELEMENT_Barcode: | |
133 case XFA_ELEMENT_Button: | |
134 case XFA_ELEMENT_PasswordEdit: | |
135 case XFA_ELEMENT_Signature: | |
136 case XFA_ELEMENT_TextEdit: | |
137 default: | |
138 eValueType = XFA_ELEMENT_Text; | |
139 break; | |
140 } | |
141 defValue.GetNode()->GetProperty(0, eValueType, TRUE); | |
142 } | |
143 return pUIChild; | |
144 } | |
145 CXFA_LocaleValue XFA_GetLocaleValue(CXFA_WidgetData* pWidgetData) { | |
146 CXFA_Node* pNodeValue = | |
147 pWidgetData->GetNode()->GetChild(0, XFA_ELEMENT_Value); | |
148 if (!pNodeValue) { | |
149 return CXFA_LocaleValue(); | |
150 } | |
151 CXFA_Node* pValueChild = pNodeValue->GetNodeItem(XFA_NODEITEM_FirstChild); | |
152 if (!pValueChild) { | |
153 return CXFA_LocaleValue(); | |
154 } | |
155 int32_t iVTType = XFA_VT_NULL; | |
156 XFA_ELEMENT eType = pValueChild->GetClassID(); | |
157 switch (eType) { | |
158 case XFA_ELEMENT_Decimal: | |
159 iVTType = XFA_VT_DECIMAL; | |
160 break; | |
161 case XFA_ELEMENT_Float: | |
162 iVTType = XFA_VT_FLOAT; | |
163 break; | |
164 case XFA_ELEMENT_Date: | |
165 iVTType = XFA_VT_DATE; | |
166 break; | |
167 case XFA_ELEMENT_Time: | |
168 iVTType = XFA_VT_TIME; | |
169 break; | |
170 case XFA_ELEMENT_DateTime: | |
171 iVTType = XFA_VT_DATETIME; | |
172 break; | |
173 case XFA_ELEMENT_Boolean: | |
174 iVTType = XFA_VT_BOOLEAN; | |
175 break; | |
176 case XFA_ELEMENT_Integer: | |
177 iVTType = XFA_VT_INTEGER; | |
178 break; | |
179 case XFA_ELEMENT_Text: | |
180 iVTType = XFA_VT_TEXT; | |
181 break; | |
182 default: | |
183 iVTType = XFA_VT_NULL; | |
184 break; | |
185 } | |
186 return CXFA_LocaleValue(iVTType, pWidgetData->GetRawValue(), | |
187 pWidgetData->GetNode()->GetDocument()->GetLocalMgr()); | |
188 } | |
189 void XFA_GetPlainTextFromRichText(IFDE_XMLNode* pXMLNode, | |
190 CFX_WideString& wsPlainText) { | |
191 if (pXMLNode == NULL) { | |
192 return; | |
193 } | |
194 switch (pXMLNode->GetType()) { | |
195 case FDE_XMLNODE_Element: { | |
196 IFDE_XMLElement* pXMLElement = (IFDE_XMLElement*)pXMLNode; | |
197 CFX_WideString wsTag; | |
198 pXMLElement->GetLocalTagName(wsTag); | |
199 uint32_t uTag = FX_HashCode_String_GetW(wsTag, wsTag.GetLength(), TRUE); | |
200 if (uTag == 0x0001f714) { | |
201 wsPlainText += L"\n"; | |
202 } else if (uTag == 0x00000070) { | |
203 if (!wsPlainText.IsEmpty()) { | |
204 wsPlainText += L"\n"; | |
205 } | |
206 } else if (uTag == 0xa48ac63) { | |
207 if (!wsPlainText.IsEmpty() && | |
208 wsPlainText[wsPlainText.GetLength() - 1] != '\n') { | |
209 wsPlainText += L"\n"; | |
210 } | |
211 } | |
212 } break; | |
213 case FDE_XMLNODE_Text: { | |
214 CFX_WideString wsContent; | |
215 ((IFDE_XMLText*)pXMLNode)->GetText(wsContent); | |
216 wsPlainText += wsContent; | |
217 } break; | |
218 case FDE_XMLNODE_CharData: { | |
219 CFX_WideString wsCharData; | |
220 ((IFDE_XMLCharData*)pXMLNode)->GetCharData(wsCharData); | |
221 wsPlainText += wsCharData; | |
222 } break; | |
223 default: | |
224 break; | |
225 } | |
226 for (IFDE_XMLNode* pChildXML = | |
227 pXMLNode->GetNodeItem(IFDE_XMLNode::FirstChild); | |
228 pChildXML; | |
229 pChildXML = pChildXML->GetNodeItem(IFDE_XMLNode::NextSibling)) { | |
230 XFA_GetPlainTextFromRichText(pChildXML, wsPlainText); | |
231 } | |
232 } | |
233 FX_BOOL XFA_FieldIsMultiListBox(CXFA_Node* pFieldNode) { | |
234 FX_BOOL bRet = FALSE; | |
235 if (!pFieldNode) { | |
236 return bRet; | |
237 } | |
238 CXFA_Node* pUIChild = pFieldNode->GetChild(0, XFA_ELEMENT_Ui); | |
239 if (pUIChild) { | |
240 CXFA_Node* pFirstChild = pUIChild->GetNodeItem(XFA_NODEITEM_FirstChild); | |
241 if (pFirstChild && pFirstChild->GetClassID() == XFA_ELEMENT_ChoiceList) { | |
242 bRet = pFirstChild->GetEnum(XFA_ATTRIBUTE_Open) == | |
243 XFA_ATTRIBUTEENUM_MultiSelect; | |
244 } | |
245 } | |
246 return bRet; | |
247 } | |
248 FX_BOOL XFA_IsLayoutElement(XFA_ELEMENT eElement, FX_BOOL bLayoutContainer) { | |
249 switch (eElement) { | |
250 case XFA_ELEMENT_Draw: | |
251 case XFA_ELEMENT_Field: | |
252 case XFA_ELEMENT_InstanceManager: | |
253 return !bLayoutContainer; | |
254 case XFA_ELEMENT_Area: | |
255 case XFA_ELEMENT_Subform: | |
256 case XFA_ELEMENT_ExclGroup: | |
257 case XFA_ELEMENT_SubformSet: | |
258 return TRUE; | |
259 case XFA_ELEMENT_PageArea: | |
260 case XFA_ELEMENT_Form: | |
261 return TRUE; | |
262 default: | |
263 return FALSE; | |
264 } | |
265 return FALSE; | |
266 } | |
267 FX_BOOL XFA_IsTakingupSpace(XFA_ATTRIBUTEENUM ePresence) { | |
268 switch (ePresence) { | |
269 case XFA_ATTRIBUTEENUM_Visible: | |
270 case XFA_ATTRIBUTEENUM_Invisible: | |
271 return TRUE; | |
272 default: | |
273 return FALSE; | |
274 } | |
275 return FALSE; | |
276 } | |
277 FX_BOOL XFA_IsFlowingLayout(XFA_ATTRIBUTEENUM eLayout) { | |
278 switch (eLayout) { | |
279 case XFA_ATTRIBUTEENUM_Tb: | |
280 case XFA_ATTRIBUTEENUM_Lr_tb: | |
281 case XFA_ATTRIBUTEENUM_Rl_tb: | |
282 return TRUE; | |
283 default: | |
284 return FALSE; | |
285 } | |
286 return FALSE; | |
287 } | |
288 FX_BOOL XFA_IsHorizontalFlow(XFA_ATTRIBUTEENUM eLayout) { | |
289 switch (eLayout) { | |
290 case XFA_ATTRIBUTEENUM_Lr_tb: | |
291 case XFA_ATTRIBUTEENUM_Rl_tb: | |
292 return TRUE; | |
293 default: | |
294 return FALSE; | |
295 } | |
296 return FALSE; | |
297 } | |
298 static const FX_DOUBLE fraction_scales[] = {0.1, | |
299 0.01, | |
300 0.001, | |
301 0.0001, | |
302 0.00001, | |
303 0.000001, | |
304 0.0000001, | |
305 0.00000001, | |
306 0.000000001, | |
307 0.0000000001, | |
308 0.00000000001, | |
309 0.000000000001, | |
310 0.0000000000001, | |
311 0.00000000000001, | |
312 0.000000000000001, | |
313 0.0000000000000001}; | |
314 FX_DOUBLE XFA_WideStringToDouble(const CFX_WideString& wsStringVal) { | |
315 CFX_WideString wsValue = wsStringVal; | |
316 wsValue.TrimLeft(); | |
317 wsValue.TrimRight(); | |
318 int64_t nIntegral = 0; | |
319 FX_DWORD dwFractional = 0; | |
320 int32_t nExponent = 0; | |
321 int32_t cc = 0; | |
322 FX_BOOL bNegative = FALSE, bExpSign = FALSE; | |
323 const FX_WCHAR* str = (const FX_WCHAR*)wsValue; | |
324 int32_t len = wsValue.GetLength(); | |
325 if (str[0] == '+') { | |
326 cc++; | |
327 } else if (str[0] == '-') { | |
328 bNegative = TRUE; | |
329 cc++; | |
330 } | |
331 int32_t nIntegralLen = 0; | |
332 while (cc < len) { | |
333 if (str[cc] == '.' || str[cc] == 'E' || str[cc] == 'e' || | |
334 nIntegralLen > 17) { | |
335 break; | |
336 } | |
337 if (!XFA_IsDigit(str[cc])) { | |
338 return 0; | |
339 } | |
340 nIntegral = nIntegral * 10 + str[cc] - '0'; | |
341 cc++; | |
342 nIntegralLen++; | |
343 } | |
344 nIntegral = bNegative ? -nIntegral : nIntegral; | |
345 int32_t scale = 0; | |
346 FX_DOUBLE fraction = 0.0; | |
347 if (cc < len && str[cc] == '.') { | |
348 cc++; | |
349 while (cc < len) { | |
350 fraction += fraction_scales[scale] * (str[cc] - '0'); | |
351 scale++; | |
352 cc++; | |
353 if (cc == len) { | |
354 break; | |
355 } | |
356 if (scale == sizeof(fraction_scales) / sizeof(FX_DOUBLE) || | |
357 str[cc] == 'E' || str[cc] == 'e') { | |
358 break; | |
359 } | |
360 if (!XFA_IsDigit(str[cc])) { | |
361 return 0; | |
362 } | |
363 } | |
364 dwFractional = (FX_DWORD)(fraction * 4294967296.0); | |
365 } | |
366 if (cc < len && (str[cc] == 'E' || str[cc] == 'e')) { | |
367 cc++; | |
368 if (cc < len) { | |
369 if (str[cc] == '+') { | |
370 cc++; | |
371 } else if (str[cc] == '-') { | |
372 bExpSign = TRUE; | |
373 cc++; | |
374 } | |
375 } | |
376 while (cc < len) { | |
377 if (str[cc] == '.' || !XFA_IsDigit(str[cc])) { | |
378 return 0; | |
379 } | |
380 nExponent = nExponent * 10 + str[cc] - '0'; | |
381 cc++; | |
382 } | |
383 nExponent = bExpSign ? -nExponent : nExponent; | |
384 } | |
385 FX_DOUBLE dValue = (dwFractional / 4294967296.0); | |
386 dValue = nIntegral + (nIntegral >= 0 ? dValue : -dValue); | |
387 if (nExponent != 0) { | |
388 dValue *= FXSYS_pow(10, (FX_FLOAT)nExponent); | |
389 } | |
390 return dValue; | |
391 } | |
392 | |
393 FX_DOUBLE XFA_ByteStringToDouble(const CFX_ByteStringC& szStringVal) { | |
394 CFX_WideString wsValue = | |
395 CFX_WideString::FromUTF8(szStringVal.GetCStr(), szStringVal.GetLength()); | |
396 return XFA_WideStringToDouble(wsValue); | |
397 } | |
398 | |
399 int32_t XFA_MapRotation(int32_t nRotation) { | |
400 nRotation = nRotation % 360; | |
401 nRotation = nRotation < 0 ? nRotation + 360 : nRotation; | |
402 return nRotation; | |
403 } | |
OLD | NEW |