xref: /aosp_15_r20/external/pdfium/core/fpdfdoc/cpdf_apsettings.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2016 The PDFium Authors
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 #include <utility>
11 
12 #include "core/fpdfapi/parser/cpdf_array.h"
13 #include "core/fpdfapi/parser/cpdf_dictionary.h"
14 #include "core/fpdfapi/parser/cpdf_stream.h"
15 #include "core/fpdfdoc/cpdf_formcontrol.h"
16 
CPDF_ApSettings(RetainPtr<CPDF_Dictionary> pDict)17 CPDF_ApSettings::CPDF_ApSettings(RetainPtr<CPDF_Dictionary> pDict)
18     : m_pDict(std::move(pDict)) {}
19 
20 CPDF_ApSettings::CPDF_ApSettings(const CPDF_ApSettings& that) = default;
21 
22 CPDF_ApSettings::~CPDF_ApSettings() = default;
23 
HasMKEntry(const ByteString & csEntry) const24 bool CPDF_ApSettings::HasMKEntry(const ByteString& csEntry) const {
25   return m_pDict && m_pDict->KeyExist(csEntry);
26 }
27 
GetRotation() const28 int CPDF_ApSettings::GetRotation() const {
29   return m_pDict ? m_pDict->GetIntegerFor("R") : 0;
30 }
31 
GetColorARGB(const ByteString & csEntry) const32 CFX_Color::TypeAndARGB CPDF_ApSettings::GetColorARGB(
33     const ByteString& csEntry) const {
34   if (!m_pDict)
35     return {CFX_Color::Type::kTransparent, 0};
36 
37   RetainPtr<const CPDF_Array> pEntry = m_pDict->GetArrayFor(csEntry);
38   if (!pEntry)
39     return {CFX_Color::Type::kTransparent, 0};
40 
41   const size_t dwCount = pEntry->size();
42   if (dwCount == 1) {
43     const float g = pEntry->GetFloatAt(0) * 255;
44     return {CFX_Color::Type::kGray, ArgbEncode(255, (int)g, (int)g, (int)g)};
45   }
46   if (dwCount == 3) {
47     float r = pEntry->GetFloatAt(0) * 255;
48     float g = pEntry->GetFloatAt(1) * 255;
49     float b = pEntry->GetFloatAt(2) * 255;
50     return {CFX_Color::Type::kRGB, ArgbEncode(255, (int)r, (int)g, (int)b)};
51   }
52   if (dwCount == 4) {
53     float c = pEntry->GetFloatAt(0);
54     float m = pEntry->GetFloatAt(1);
55     float y = pEntry->GetFloatAt(2);
56     float k = pEntry->GetFloatAt(3);
57     float r = (1.0f - std::min(1.0f, c + k)) * 255;
58     float g = (1.0f - std::min(1.0f, m + k)) * 255;
59     float b = (1.0f - std::min(1.0f, y + k)) * 255;
60     return {CFX_Color::Type::kCMYK, ArgbEncode(255, (int)r, (int)g, (int)b)};
61   }
62   return {CFX_Color::Type::kTransparent, 0};
63 }
64 
GetOriginalColorComponent(int index,const ByteString & csEntry) const65 float CPDF_ApSettings::GetOriginalColorComponent(
66     int index,
67     const ByteString& csEntry) const {
68   if (!m_pDict)
69     return 0;
70 
71   RetainPtr<const CPDF_Array> pEntry = m_pDict->GetArrayFor(csEntry);
72   return pEntry ? pEntry->GetFloatAt(index) : 0;
73 }
74 
GetOriginalColor(const ByteString & csEntry) const75 CFX_Color CPDF_ApSettings::GetOriginalColor(const ByteString& csEntry) const {
76   if (!m_pDict)
77     return CFX_Color();
78 
79   RetainPtr<const CPDF_Array> pEntry = m_pDict->GetArrayFor(csEntry);
80   if (!pEntry)
81     return CFX_Color();
82 
83   size_t dwCount = pEntry->size();
84   if (dwCount == 1) {
85     return CFX_Color(CFX_Color::Type::kGray, pEntry->GetFloatAt(0));
86   }
87   if (dwCount == 3) {
88     return CFX_Color(CFX_Color::Type::kRGB, pEntry->GetFloatAt(0),
89                      pEntry->GetFloatAt(1), pEntry->GetFloatAt(2));
90   }
91   if (dwCount == 4) {
92     return CFX_Color(CFX_Color::Type::kCMYK, pEntry->GetFloatAt(0),
93                      pEntry->GetFloatAt(1), pEntry->GetFloatAt(2),
94                      pEntry->GetFloatAt(3));
95   }
96   return CFX_Color();
97 }
98 
GetCaption(const ByteString & csEntry) const99 WideString CPDF_ApSettings::GetCaption(const ByteString& csEntry) const {
100   return m_pDict ? m_pDict->GetUnicodeTextFor(csEntry) : WideString();
101 }
102 
GetIcon(const ByteString & csEntry) const103 RetainPtr<CPDF_Stream> CPDF_ApSettings::GetIcon(
104     const ByteString& csEntry) const {
105   return m_pDict ? m_pDict->GetMutableStreamFor(csEntry) : nullptr;
106 }
107 
GetIconFit() const108 CPDF_IconFit CPDF_ApSettings::GetIconFit() const {
109   return CPDF_IconFit(m_pDict ? m_pDict->GetDictFor("IF") : nullptr);
110 }
111 
GetTextPosition() const112 int CPDF_ApSettings::GetTextPosition() const {
113   return m_pDict ? m_pDict->GetIntegerFor("TP", TEXTPOS_CAPTION)
114                  : TEXTPOS_CAPTION;
115 }
116