OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2016 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 "core/fpdfdoc/include/cpdf_formcontrol.h" | 7 #include "core/fpdfdoc/include/cpdf_formcontrol.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "core/fpdfapi/fpdf_page/include/cpdf_form.h" | 11 #include "core/fpdfapi/fpdf_page/include/cpdf_form.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 if (it.first != "Off") | 46 if (it.first != "Off") |
47 return it.first; | 47 return it.first; |
48 } | 48 } |
49 return CFX_ByteString(); | 49 return CFX_ByteString(); |
50 } | 50 } |
51 | 51 |
52 void CPDF_FormControl::SetOnStateName(const CFX_ByteString& csOn) { | 52 void CPDF_FormControl::SetOnStateName(const CFX_ByteString& csOn) { |
53 ASSERT(GetType() == CPDF_FormField::CheckBox || | 53 ASSERT(GetType() == CPDF_FormField::CheckBox || |
54 GetType() == CPDF_FormField::RadioButton); | 54 GetType() == CPDF_FormField::RadioButton); |
55 CFX_ByteString csValue = csOn; | 55 CFX_ByteString csValue = csOn; |
56 if (csValue.IsEmpty()) { | 56 if (csValue.IsEmpty()) |
57 csValue = "Yes"; | 57 csValue = "Yes"; |
58 } | 58 else if (csValue == "Off") |
59 if (csValue == "Off") { | |
60 csValue = "Yes"; | 59 csValue = "Yes"; |
61 } | 60 |
62 CFX_ByteString csAS = m_pWidgetDict->GetStringBy("AS", "Off"); | 61 CFX_ByteString csAS = m_pWidgetDict->GetStringBy("AS", "Off"); |
63 if (csAS != "Off") { | 62 if (csAS != "Off") |
64 m_pWidgetDict->SetAtName("AS", csValue); | 63 m_pWidgetDict->SetAtName("AS", csValue); |
65 } | 64 |
66 CPDF_Dictionary* pAP = m_pWidgetDict->GetDictBy("AP"); | 65 CPDF_Dictionary* pAP = m_pWidgetDict->GetDictBy("AP"); |
67 if (!pAP) { | 66 if (!pAP) |
68 return; | 67 return; |
69 } | 68 |
70 for (const auto& it : *pAP) { | 69 for (const auto& it : *pAP) { |
71 CPDF_Object* pObj1 = it.second; | 70 CPDF_Object* pObj1 = it.second; |
72 if (!pObj1) { | 71 if (!pObj1) |
73 continue; | 72 continue; |
74 } | 73 |
75 CPDF_Object* pObjDirect1 = pObj1->GetDirect(); | 74 CPDF_Object* pObjDirect1 = pObj1->GetDirect(); |
76 CPDF_Dictionary* pSubDict = pObjDirect1->AsDictionary(); | 75 CPDF_Dictionary* pSubDict = pObjDirect1->AsDictionary(); |
77 if (!pSubDict) | 76 if (!pSubDict) |
78 continue; | 77 continue; |
79 | 78 |
80 auto subdict_it = pSubDict->begin(); | 79 auto subdict_it = pSubDict->begin(); |
81 while (subdict_it != pSubDict->end()) { | 80 while (subdict_it != pSubDict->end()) { |
82 const CFX_ByteString& csKey2 = subdict_it->first; | 81 const CFX_ByteString& csKey2 = subdict_it->first; |
83 CPDF_Object* pObj2 = subdict_it->second; | 82 CPDF_Object* pObj2 = subdict_it->second; |
84 ++subdict_it; | 83 ++subdict_it; |
85 if (!pObj2) { | 84 if (!pObj2) |
86 continue; | 85 continue; |
87 } | |
88 if (csKey2 != "Off") { | 86 if (csKey2 != "Off") { |
89 pSubDict->ReplaceKey(csKey2, csValue); | 87 pSubDict->ReplaceKey(csKey2, csValue); |
90 break; | 88 break; |
91 } | 89 } |
92 } | 90 } |
93 } | 91 } |
94 } | 92 } |
| 93 |
95 CFX_ByteString CPDF_FormControl::GetCheckedAPState() { | 94 CFX_ByteString CPDF_FormControl::GetCheckedAPState() { |
96 ASSERT(GetType() == CPDF_FormField::CheckBox || | 95 ASSERT(GetType() == CPDF_FormField::CheckBox || |
97 GetType() == CPDF_FormField::RadioButton); | 96 GetType() == CPDF_FormField::RadioButton); |
98 CFX_ByteString csOn = GetOnStateName(); | 97 CFX_ByteString csOn = GetOnStateName(); |
99 if (GetType() == CPDF_FormField::RadioButton || | 98 if (GetType() == CPDF_FormField::RadioButton || |
100 GetType() == CPDF_FormField::CheckBox) { | 99 GetType() == CPDF_FormField::CheckBox) { |
101 if (ToArray(FPDF_GetFieldAttr(m_pField->m_pDict, "Opt"))) { | 100 if (ToArray(FPDF_GetFieldAttr(m_pField->m_pDict, "Opt"))) { |
102 int iIndex = m_pField->GetControlIndex(this); | 101 int iIndex = m_pField->GetControlIndex(this); |
103 csOn.Format("%d", iIndex); | 102 csOn.Format("%d", iIndex); |
104 } | 103 } |
(...skipping 25 matching lines...) Expand all Loading... |
130 GetType() == CPDF_FormField::RadioButton); | 129 GetType() == CPDF_FormField::RadioButton); |
131 CFX_ByteString csOn = GetOnStateName(); | 130 CFX_ByteString csOn = GetOnStateName(); |
132 CFX_ByteString csAS = m_pWidgetDict->GetStringBy("AS"); | 131 CFX_ByteString csAS = m_pWidgetDict->GetStringBy("AS"); |
133 return csAS == csOn; | 132 return csAS == csOn; |
134 } | 133 } |
135 | 134 |
136 bool CPDF_FormControl::IsDefaultChecked() const { | 135 bool CPDF_FormControl::IsDefaultChecked() const { |
137 ASSERT(GetType() == CPDF_FormField::CheckBox || | 136 ASSERT(GetType() == CPDF_FormField::CheckBox || |
138 GetType() == CPDF_FormField::RadioButton); | 137 GetType() == CPDF_FormField::RadioButton); |
139 CPDF_Object* pDV = FPDF_GetFieldAttr(m_pField->m_pDict, "DV"); | 138 CPDF_Object* pDV = FPDF_GetFieldAttr(m_pField->m_pDict, "DV"); |
140 if (!pDV) { | 139 if (!pDV) |
141 return FALSE; | 140 return FALSE; |
142 } | 141 |
143 CFX_ByteString csDV = pDV->GetString(); | 142 CFX_ByteString csDV = pDV->GetString(); |
144 CFX_ByteString csOn = GetOnStateName(); | 143 CFX_ByteString csOn = GetOnStateName(); |
145 return (csDV == csOn); | 144 return (csDV == csOn); |
146 } | 145 } |
147 | 146 |
148 void CPDF_FormControl::CheckControl(FX_BOOL bChecked) { | 147 void CPDF_FormControl::CheckControl(FX_BOOL bChecked) { |
149 ASSERT(GetType() == CPDF_FormField::CheckBox || | 148 ASSERT(GetType() == CPDF_FormField::CheckBox || |
150 GetType() == CPDF_FormField::RadioButton); | 149 GetType() == CPDF_FormField::RadioButton); |
151 CFX_ByteString csOn = GetOnStateName(); | 150 CFX_ByteString csOn = GetOnStateName(); |
152 CFX_ByteString csOldAS = m_pWidgetDict->GetStringBy("AS", "Off"); | 151 CFX_ByteString csOldAS = m_pWidgetDict->GetStringBy("AS", "Off"); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 FX_FLOAT CPDF_FormControl::GetOriginalColor(int index, | 217 FX_FLOAT CPDF_FormControl::GetOriginalColor(int index, |
219 const CFX_ByteString& csEntry) { | 218 const CFX_ByteString& csEntry) { |
220 return GetMK().GetOriginalColor(index, csEntry); | 219 return GetMK().GetOriginalColor(index, csEntry); |
221 } | 220 } |
222 | 221 |
223 void CPDF_FormControl::GetOriginalColor(int& iColorType, | 222 void CPDF_FormControl::GetOriginalColor(int& iColorType, |
224 FX_FLOAT fc[4], | 223 FX_FLOAT fc[4], |
225 const CFX_ByteString& csEntry) { | 224 const CFX_ByteString& csEntry) { |
226 GetMK().GetOriginalColor(iColorType, fc, csEntry); | 225 GetMK().GetOriginalColor(iColorType, fc, csEntry); |
227 } | 226 } |
| 227 |
228 CFX_WideString CPDF_FormControl::GetCaption(const CFX_ByteString& csEntry) { | 228 CFX_WideString CPDF_FormControl::GetCaption(const CFX_ByteString& csEntry) { |
229 return GetMK().GetCaption(csEntry); | 229 return GetMK().GetCaption(csEntry); |
230 } | 230 } |
231 | 231 |
232 CPDF_Stream* CPDF_FormControl::GetIcon(const CFX_ByteString& csEntry) { | 232 CPDF_Stream* CPDF_FormControl::GetIcon(const CFX_ByteString& csEntry) { |
233 return GetMK().GetIcon(csEntry); | 233 return GetMK().GetIcon(csEntry); |
234 } | 234 } |
235 | 235 |
236 CPDF_IconFit CPDF_FormControl::GetIconFit() { | 236 CPDF_IconFit CPDF_FormControl::GetIconFit() { |
237 return GetMK().GetIconFit(); | 237 return GetMK().GetIconFit(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 if (csFontNameTag.IsEmpty()) | 285 if (csFontNameTag.IsEmpty()) |
286 return nullptr; | 286 return nullptr; |
287 | 287 |
288 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pWidgetDict, "DR"); | 288 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pWidgetDict, "DR"); |
289 if (CPDF_Dictionary* pDict = ToDictionary(pObj)) { | 289 if (CPDF_Dictionary* pDict = ToDictionary(pObj)) { |
290 CPDF_Dictionary* pFonts = pDict->GetDictBy("Font"); | 290 CPDF_Dictionary* pFonts = pDict->GetDictBy("Font"); |
291 if (pFonts) { | 291 if (pFonts) { |
292 CPDF_Dictionary* pElement = pFonts->GetDictBy(csFontNameTag); | 292 CPDF_Dictionary* pElement = pFonts->GetDictBy(csFontNameTag); |
293 if (pElement) { | 293 if (pElement) { |
294 CPDF_Font* pFont = m_pField->m_pForm->m_pDocument->LoadFont(pElement); | 294 CPDF_Font* pFont = m_pField->m_pForm->m_pDocument->LoadFont(pElement); |
295 if (pFont) { | 295 if (pFont) |
296 return pFont; | 296 return pFont; |
297 } | |
298 } | 297 } |
299 } | 298 } |
300 } | 299 } |
301 if (CPDF_Font* pFormFont = m_pField->m_pForm->GetFormFont(csFontNameTag)) | 300 if (CPDF_Font* pFormFont = m_pField->m_pForm->GetFormFont(csFontNameTag)) |
302 return pFormFont; | 301 return pFormFont; |
303 | 302 |
304 CPDF_Dictionary* pPageDict = m_pWidgetDict->GetDictBy("P"); | 303 CPDF_Dictionary* pPageDict = m_pWidgetDict->GetDictBy("P"); |
305 pObj = FPDF_GetFieldAttr(pPageDict, "Resources"); | 304 pObj = FPDF_GetFieldAttr(pPageDict, "Resources"); |
306 if (CPDF_Dictionary* pDict = ToDictionary(pObj)) { | 305 if (CPDF_Dictionary* pDict = ToDictionary(pObj)) { |
307 CPDF_Dictionary* pFonts = pDict->GetDictBy("Font"); | 306 CPDF_Dictionary* pFonts = pDict->GetDictBy("Font"); |
308 if (pFonts) { | 307 if (pFonts) { |
309 CPDF_Dictionary* pElement = pFonts->GetDictBy(csFontNameTag); | 308 CPDF_Dictionary* pElement = pFonts->GetDictBy(csFontNameTag); |
310 if (pElement) { | 309 if (pElement) { |
311 CPDF_Font* pFont = m_pField->m_pForm->m_pDocument->LoadFont(pElement); | 310 CPDF_Font* pFont = m_pField->m_pForm->m_pDocument->LoadFont(pElement); |
312 if (pFont) { | 311 if (pFont) |
313 return pFont; | 312 return pFont; |
314 } | |
315 } | 313 } |
316 } | 314 } |
317 } | 315 } |
318 return nullptr; | 316 return nullptr; |
319 } | 317 } |
320 | 318 |
321 int CPDF_FormControl::GetControlAlignment() { | 319 int CPDF_FormControl::GetControlAlignment() { |
322 if (!m_pWidgetDict) { | 320 if (!m_pWidgetDict) |
323 return 0; | 321 return 0; |
324 } | 322 if (m_pWidgetDict->KeyExist("Q")) |
325 if (m_pWidgetDict->KeyExist("Q")) { | |
326 return m_pWidgetDict->GetIntegerBy("Q", 0); | 323 return m_pWidgetDict->GetIntegerBy("Q", 0); |
327 } | 324 |
328 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pField->m_pDict, "Q"); | 325 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pField->m_pDict, "Q"); |
329 if (pObj) | 326 if (pObj) |
330 return pObj->GetInteger(); | 327 return pObj->GetInteger(); |
331 return m_pField->m_pForm->GetFormAlignment(); | 328 return m_pField->m_pForm->GetFormAlignment(); |
332 } | 329 } |
333 | |
334 CPDF_ApSettings::CPDF_ApSettings(CPDF_Dictionary* pDict) : m_pDict(pDict) {} | |
335 | |
336 bool CPDF_ApSettings::HasMKEntry(const CFX_ByteString& csEntry) const { | |
337 return m_pDict && m_pDict->KeyExist(csEntry); | |
338 } | |
339 | |
340 int CPDF_ApSettings::GetRotation() const { | |
341 return m_pDict ? m_pDict->GetIntegerBy("R") : 0; | |
342 } | |
343 | |
344 FX_ARGB CPDF_ApSettings::GetColor(int& iColorType, | |
345 const CFX_ByteString& csEntry) const { | |
346 iColorType = COLORTYPE_TRANSPARENT; | |
347 if (!m_pDict) | |
348 return 0; | |
349 | |
350 CPDF_Array* pEntry = m_pDict->GetArrayBy(csEntry); | |
351 if (!pEntry) | |
352 return 0; | |
353 | |
354 FX_ARGB color = 0; | |
355 size_t dwCount = pEntry->GetCount(); | |
356 if (dwCount == 1) { | |
357 iColorType = COLORTYPE_GRAY; | |
358 FX_FLOAT g = pEntry->GetNumberAt(0) * 255; | |
359 color = ArgbEncode(255, (int)g, (int)g, (int)g); | |
360 } else if (dwCount == 3) { | |
361 iColorType = COLORTYPE_RGB; | |
362 FX_FLOAT r = pEntry->GetNumberAt(0) * 255; | |
363 FX_FLOAT g = pEntry->GetNumberAt(1) * 255; | |
364 FX_FLOAT b = pEntry->GetNumberAt(2) * 255; | |
365 color = ArgbEncode(255, (int)r, (int)g, (int)b); | |
366 } else if (dwCount == 4) { | |
367 iColorType = COLORTYPE_CMYK; | |
368 FX_FLOAT c = pEntry->GetNumberAt(0); | |
369 FX_FLOAT m = pEntry->GetNumberAt(1); | |
370 FX_FLOAT y = pEntry->GetNumberAt(2); | |
371 FX_FLOAT k = pEntry->GetNumberAt(3); | |
372 FX_FLOAT r = 1.0f - std::min(1.0f, c + k); | |
373 FX_FLOAT g = 1.0f - std::min(1.0f, m + k); | |
374 FX_FLOAT b = 1.0f - std::min(1.0f, y + k); | |
375 color = ArgbEncode(255, (int)(r * 255), (int)(g * 255), (int)(b * 255)); | |
376 } | |
377 return color; | |
378 } | |
379 | |
380 FX_FLOAT CPDF_ApSettings::GetOriginalColor( | |
381 int index, | |
382 const CFX_ByteString& csEntry) const { | |
383 if (!m_pDict) | |
384 return 0; | |
385 | |
386 CPDF_Array* pEntry = m_pDict->GetArrayBy(csEntry); | |
387 return pEntry ? pEntry->GetNumberAt(index) : 0; | |
388 } | |
389 | |
390 void CPDF_ApSettings::GetOriginalColor(int& iColorType, | |
391 FX_FLOAT fc[4], | |
392 const CFX_ByteString& csEntry) const { | |
393 iColorType = COLORTYPE_TRANSPARENT; | |
394 for (int i = 0; i < 4; i++) { | |
395 fc[i] = 0; | |
396 } | |
397 if (!m_pDict) { | |
398 return; | |
399 } | |
400 CPDF_Array* pEntry = m_pDict->GetArrayBy(csEntry); | |
401 if (!pEntry) { | |
402 return; | |
403 } | |
404 size_t dwCount = pEntry->GetCount(); | |
405 if (dwCount == 1) { | |
406 iColorType = COLORTYPE_GRAY; | |
407 fc[0] = pEntry->GetNumberAt(0); | |
408 } else if (dwCount == 3) { | |
409 iColorType = COLORTYPE_RGB; | |
410 fc[0] = pEntry->GetNumberAt(0); | |
411 fc[1] = pEntry->GetNumberAt(1); | |
412 fc[2] = pEntry->GetNumberAt(2); | |
413 } else if (dwCount == 4) { | |
414 iColorType = COLORTYPE_CMYK; | |
415 fc[0] = pEntry->GetNumberAt(0); | |
416 fc[1] = pEntry->GetNumberAt(1); | |
417 fc[2] = pEntry->GetNumberAt(2); | |
418 fc[3] = pEntry->GetNumberAt(3); | |
419 } | |
420 } | |
421 | |
422 CFX_WideString CPDF_ApSettings::GetCaption( | |
423 const CFX_ByteString& csEntry) const { | |
424 return m_pDict ? m_pDict->GetUnicodeTextBy(csEntry) : CFX_WideString(); | |
425 } | |
426 | |
427 CPDF_Stream* CPDF_ApSettings::GetIcon(const CFX_ByteString& csEntry) const { | |
428 return m_pDict ? m_pDict->GetStreamBy(csEntry) : nullptr; | |
429 } | |
430 | |
431 CPDF_IconFit CPDF_ApSettings::GetIconFit() const { | |
432 return CPDF_IconFit(m_pDict ? m_pDict->GetDictBy("IF") : nullptr); | |
433 } | |
434 | |
435 int CPDF_ApSettings::GetTextPosition() const { | |
436 return m_pDict ? m_pDict->GetIntegerBy("TP", TEXTPOS_CAPTION) | |
437 : TEXTPOS_CAPTION; | |
438 } | |
OLD | NEW |