1 // Copyright 2016 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 "core/fpdfdoc/cpdf_apsettings.h"
8
9 #include <algorithm>
10
11 #include "core/fpdfapi/parser/cpdf_array.h"
12 #include "core/fpdfapi/parser/cpdf_dictionary.h"
13 #include "core/fpdfdoc/cpdf_formcontrol.h"
14
CPDF_ApSettings(CPDF_Dictionary * pDict)15 CPDF_ApSettings::CPDF_ApSettings(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
16
HasMKEntry(const CFX_ByteString & csEntry) const17 bool CPDF_ApSettings::HasMKEntry(const CFX_ByteString& csEntry) const {
18 return m_pDict && m_pDict->KeyExist(csEntry);
19 }
20
GetRotation() const21 int CPDF_ApSettings::GetRotation() const {
22 return m_pDict ? m_pDict->GetIntegerFor("R") : 0;
23 }
24
GetColor(int & iColorType,const CFX_ByteString & csEntry) const25 FX_ARGB CPDF_ApSettings::GetColor(int& iColorType,
26 const CFX_ByteString& csEntry) const {
27 iColorType = COLORTYPE_TRANSPARENT;
28 if (!m_pDict)
29 return 0;
30
31 CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
32 if (!pEntry)
33 return 0;
34
35 FX_ARGB color = 0;
36 size_t dwCount = pEntry->GetCount();
37 if (dwCount == 1) {
38 iColorType = COLORTYPE_GRAY;
39 FX_FLOAT g = pEntry->GetNumberAt(0) * 255;
40 return ArgbEncode(255, (int)g, (int)g, (int)g);
41 }
42 if (dwCount == 3) {
43 iColorType = COLORTYPE_RGB;
44 FX_FLOAT r = pEntry->GetNumberAt(0) * 255;
45 FX_FLOAT g = pEntry->GetNumberAt(1) * 255;
46 FX_FLOAT b = pEntry->GetNumberAt(2) * 255;
47 return ArgbEncode(255, (int)r, (int)g, (int)b);
48 }
49 if (dwCount == 4) {
50 iColorType = COLORTYPE_CMYK;
51 FX_FLOAT c = pEntry->GetNumberAt(0);
52 FX_FLOAT m = pEntry->GetNumberAt(1);
53 FX_FLOAT y = pEntry->GetNumberAt(2);
54 FX_FLOAT k = pEntry->GetNumberAt(3);
55 FX_FLOAT r = 1.0f - std::min(1.0f, c + k);
56 FX_FLOAT g = 1.0f - std::min(1.0f, m + k);
57 FX_FLOAT b = 1.0f - std::min(1.0f, y + k);
58 return ArgbEncode(255, (int)(r * 255), (int)(g * 255), (int)(b * 255));
59 }
60 return color;
61 }
62
GetOriginalColor(int index,const CFX_ByteString & csEntry) const63 FX_FLOAT CPDF_ApSettings::GetOriginalColor(
64 int index,
65 const CFX_ByteString& csEntry) const {
66 if (!m_pDict)
67 return 0;
68
69 CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
70 return pEntry ? pEntry->GetNumberAt(index) : 0;
71 }
72
GetOriginalColor(int & iColorType,FX_FLOAT fc[4],const CFX_ByteString & csEntry) const73 void CPDF_ApSettings::GetOriginalColor(int& iColorType,
74 FX_FLOAT fc[4],
75 const CFX_ByteString& csEntry) const {
76 iColorType = COLORTYPE_TRANSPARENT;
77 for (int i = 0; i < 4; i++)
78 fc[i] = 0;
79
80 if (!m_pDict)
81 return;
82
83 CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
84 if (!pEntry)
85 return;
86
87 size_t dwCount = pEntry->GetCount();
88 if (dwCount == 1) {
89 iColorType = COLORTYPE_GRAY;
90 fc[0] = pEntry->GetNumberAt(0);
91 } else if (dwCount == 3) {
92 iColorType = COLORTYPE_RGB;
93 fc[0] = pEntry->GetNumberAt(0);
94 fc[1] = pEntry->GetNumberAt(1);
95 fc[2] = pEntry->GetNumberAt(2);
96 } else if (dwCount == 4) {
97 iColorType = COLORTYPE_CMYK;
98 fc[0] = pEntry->GetNumberAt(0);
99 fc[1] = pEntry->GetNumberAt(1);
100 fc[2] = pEntry->GetNumberAt(2);
101 fc[3] = pEntry->GetNumberAt(3);
102 }
103 }
104
GetCaption(const CFX_ByteString & csEntry) const105 CFX_WideString CPDF_ApSettings::GetCaption(
106 const CFX_ByteString& csEntry) const {
107 return m_pDict ? m_pDict->GetUnicodeTextFor(csEntry) : CFX_WideString();
108 }
109
GetIcon(const CFX_ByteString & csEntry) const110 CPDF_Stream* CPDF_ApSettings::GetIcon(const CFX_ByteString& csEntry) const {
111 return m_pDict ? m_pDict->GetStreamFor(csEntry) : nullptr;
112 }
113
GetIconFit() const114 CPDF_IconFit CPDF_ApSettings::GetIconFit() const {
115 return CPDF_IconFit(m_pDict ? m_pDict->GetDictFor("IF") : nullptr);
116 }
117
GetTextPosition() const118 int CPDF_ApSettings::GetTextPosition() const {
119 return m_pDict ? m_pDict->GetIntegerFor("TP", TEXTPOS_CAPTION)
120 : TEXTPOS_CAPTION;
121 }
122