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