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 #include "core/fxge/cfx_color.h"
15
CPDF_ApSettings(CPDF_Dictionary * pDict)16 CPDF_ApSettings::CPDF_ApSettings(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
17
18 CPDF_ApSettings::CPDF_ApSettings(const CPDF_ApSettings& that) = default;
19
~CPDF_ApSettings()20 CPDF_ApSettings::~CPDF_ApSettings() {}
21
HasMKEntry(const ByteString & csEntry) const22 bool CPDF_ApSettings::HasMKEntry(const ByteString& csEntry) const {
23 return m_pDict && m_pDict->KeyExist(csEntry);
24 }
25
GetRotation() const26 int CPDF_ApSettings::GetRotation() const {
27 return m_pDict ? m_pDict->GetIntegerFor("R") : 0;
28 }
29
GetColor(int & iColorType,const ByteString & csEntry) const30 FX_ARGB CPDF_ApSettings::GetColor(int& iColorType,
31 const ByteString& csEntry) const {
32 iColorType = CFX_Color::kTransparent;
33 if (!m_pDict)
34 return 0;
35
36 CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
37 if (!pEntry)
38 return 0;
39
40 FX_ARGB color = 0;
41 size_t dwCount = pEntry->size();
42 if (dwCount == 1) {
43 iColorType = CFX_Color::kGray;
44 float g = pEntry->GetNumberAt(0) * 255;
45 return ArgbEncode(255, (int)g, (int)g, (int)g);
46 }
47 if (dwCount == 3) {
48 iColorType = CFX_Color::kRGB;
49 float r = pEntry->GetNumberAt(0) * 255;
50 float g = pEntry->GetNumberAt(1) * 255;
51 float b = pEntry->GetNumberAt(2) * 255;
52 return ArgbEncode(255, (int)r, (int)g, (int)b);
53 }
54 if (dwCount == 4) {
55 iColorType = CFX_Color::kCMYK;
56 float c = pEntry->GetNumberAt(0);
57 float m = pEntry->GetNumberAt(1);
58 float y = pEntry->GetNumberAt(2);
59 float k = pEntry->GetNumberAt(3);
60 float r = 1.0f - std::min(1.0f, c + k);
61 float g = 1.0f - std::min(1.0f, m + k);
62 float b = 1.0f - std::min(1.0f, y + k);
63 return ArgbEncode(255, (int)(r * 255), (int)(g * 255), (int)(b * 255));
64 }
65 return color;
66 }
67
GetOriginalColor(int index,const ByteString & csEntry) const68 float CPDF_ApSettings::GetOriginalColor(int index,
69 const ByteString& csEntry) const {
70 if (!m_pDict)
71 return 0;
72
73 CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
74 return pEntry ? pEntry->GetNumberAt(index) : 0;
75 }
76
GetOriginalColor(int & iColorType,float fc[4],const ByteString & csEntry) const77 void CPDF_ApSettings::GetOriginalColor(int& iColorType,
78 float fc[4],
79 const ByteString& csEntry) const {
80 iColorType = CFX_Color::kTransparent;
81 for (int i = 0; i < 4; i++)
82 fc[i] = 0;
83
84 if (!m_pDict)
85 return;
86
87 CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
88 if (!pEntry)
89 return;
90
91 size_t dwCount = pEntry->size();
92 if (dwCount == 1) {
93 iColorType = CFX_Color::kGray;
94 fc[0] = pEntry->GetNumberAt(0);
95 } else if (dwCount == 3) {
96 iColorType = CFX_Color::kRGB;
97 fc[0] = pEntry->GetNumberAt(0);
98 fc[1] = pEntry->GetNumberAt(1);
99 fc[2] = pEntry->GetNumberAt(2);
100 } else if (dwCount == 4) {
101 iColorType = CFX_Color::kCMYK;
102 fc[0] = pEntry->GetNumberAt(0);
103 fc[1] = pEntry->GetNumberAt(1);
104 fc[2] = pEntry->GetNumberAt(2);
105 fc[3] = pEntry->GetNumberAt(3);
106 }
107 }
108
GetCaption(const ByteString & csEntry) const109 WideString CPDF_ApSettings::GetCaption(const ByteString& csEntry) const {
110 return m_pDict ? m_pDict->GetUnicodeTextFor(csEntry) : WideString();
111 }
112
GetIcon(const ByteString & csEntry) const113 CPDF_Stream* CPDF_ApSettings::GetIcon(const ByteString& csEntry) const {
114 return m_pDict ? m_pDict->GetStreamFor(csEntry) : nullptr;
115 }
116
GetIconFit() const117 CPDF_IconFit CPDF_ApSettings::GetIconFit() const {
118 return CPDF_IconFit(m_pDict ? m_pDict->GetDictFor("IF") : nullptr);
119 }
120
GetTextPosition() const121 int CPDF_ApSettings::GetTextPosition() const {
122 return m_pDict ? m_pDict->GetIntegerFor("TP", TEXTPOS_CAPTION)
123 : TEXTPOS_CAPTION;
124 }
125