xref: /aosp_15_r20/external/pdfium/core/fpdfdoc/cpdf_formcontrol.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_formcontrol.h"
8 
9 #include <iterator>
10 #include <utility>
11 
12 #include "constants/form_fields.h"
13 #include "core/fpdfapi/font/cpdf_font.h"
14 #include "core/fpdfapi/page/cpdf_docpagedata.h"
15 #include "core/fpdfapi/parser/cpdf_array.h"
16 #include "core/fpdfapi/parser/cpdf_dictionary.h"
17 #include "core/fpdfapi/parser/cpdf_name.h"
18 #include "core/fpdfapi/parser/cpdf_stream.h"
19 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
20 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
21 #include "core/fpdfdoc/cpdf_interactiveform.h"
22 #include "third_party/base/check.h"
23 
24 namespace {
25 
26 constexpr char kHighlightModes[] = {'N', 'I', 'O', 'P', 'T'};
27 
28 // Order of |kHighlightModes| must match order of HighlightingMode enum.
29 static_assert(kHighlightModes[CPDF_FormControl::kNone] == 'N',
30               "HighlightingMode mismatch");
31 static_assert(kHighlightModes[CPDF_FormControl::kInvert] == 'I',
32               "HighlightingMode mismatch");
33 static_assert(kHighlightModes[CPDF_FormControl::kOutline] == 'O',
34               "HighlightingMode mismatch");
35 static_assert(kHighlightModes[CPDF_FormControl::kPush] == 'P',
36               "HighlightingMode mismatch");
37 static_assert(kHighlightModes[CPDF_FormControl::kToggle] == 'T',
38               "HighlightingMode mismatch");
39 
40 }  // namespace
41 
CPDF_FormControl(CPDF_FormField * pField,RetainPtr<CPDF_Dictionary> pWidgetDict,CPDF_InteractiveForm * pForm)42 CPDF_FormControl::CPDF_FormControl(CPDF_FormField* pField,
43                                    RetainPtr<CPDF_Dictionary> pWidgetDict,
44                                    CPDF_InteractiveForm* pForm)
45     : m_pField(pField), m_pWidgetDict(std::move(pWidgetDict)), m_pForm(pForm) {
46   DCHECK(m_pWidgetDict);
47 }
48 
49 CPDF_FormControl::~CPDF_FormControl() = default;
50 
GetRect() const51 CFX_FloatRect CPDF_FormControl::GetRect() const {
52   return m_pWidgetDict->GetRectFor("Rect");
53 }
54 
GetOnStateName() const55 ByteString CPDF_FormControl::GetOnStateName() const {
56   DCHECK(GetType() == CPDF_FormField::kCheckBox ||
57          GetType() == CPDF_FormField::kRadioButton);
58   RetainPtr<const CPDF_Dictionary> pAP = m_pWidgetDict->GetDictFor("AP");
59   if (!pAP)
60     return ByteString();
61 
62   RetainPtr<const CPDF_Dictionary> pN = pAP->GetDictFor("N");
63   if (!pN)
64     return ByteString();
65 
66   CPDF_DictionaryLocker locker(pN);
67   for (const auto& it : locker) {
68     if (it.first != "Off")
69       return it.first;
70   }
71   return ByteString();
72 }
73 
GetCheckedAPState() const74 ByteString CPDF_FormControl::GetCheckedAPState() const {
75   DCHECK(GetType() == CPDF_FormField::kCheckBox ||
76          GetType() == CPDF_FormField::kRadioButton);
77   ByteString csOn = GetOnStateName();
78   if (ToArray(m_pField->GetFieldAttr("Opt")))
79     csOn = ByteString::FormatInteger(m_pField->GetControlIndex(this));
80   if (csOn.IsEmpty())
81     csOn = "Yes";
82   return csOn;
83 }
84 
GetExportValue() const85 WideString CPDF_FormControl::GetExportValue() const {
86   DCHECK(GetType() == CPDF_FormField::kCheckBox ||
87          GetType() == CPDF_FormField::kRadioButton);
88   ByteString csOn = GetOnStateName();
89   RetainPtr<const CPDF_Array> pArray = ToArray(m_pField->GetFieldAttr("Opt"));
90   if (pArray)
91     csOn = pArray->GetByteStringAt(m_pField->GetControlIndex(this));
92   if (csOn.IsEmpty())
93     csOn = "Yes";
94   return PDF_DecodeText(csOn.raw_span());
95 }
96 
IsChecked() const97 bool CPDF_FormControl::IsChecked() const {
98   DCHECK(GetType() == CPDF_FormField::kCheckBox ||
99          GetType() == CPDF_FormField::kRadioButton);
100   ByteString csOn = GetOnStateName();
101   ByteString csAS = m_pWidgetDict->GetByteStringFor("AS");
102   return csAS == csOn;
103 }
104 
IsDefaultChecked() const105 bool CPDF_FormControl::IsDefaultChecked() const {
106   DCHECK(GetType() == CPDF_FormField::kCheckBox ||
107          GetType() == CPDF_FormField::kRadioButton);
108   RetainPtr<const CPDF_Object> pDV = m_pField->GetFieldAttr("DV");
109   if (!pDV)
110     return false;
111 
112   ByteString csDV = pDV->GetString();
113   ByteString csOn = GetOnStateName();
114   return (csDV == csOn);
115 }
116 
CheckControl(bool bChecked)117 void CPDF_FormControl::CheckControl(bool bChecked) {
118   DCHECK(GetType() == CPDF_FormField::kCheckBox ||
119          GetType() == CPDF_FormField::kRadioButton);
120   ByteString csOldAS = m_pWidgetDict->GetByteStringFor("AS", "Off");
121   ByteString csAS = "Off";
122   if (bChecked)
123     csAS = GetOnStateName();
124   if (csOldAS == csAS)
125     return;
126   m_pWidgetDict->SetNewFor<CPDF_Name>("AS", csAS);
127 }
128 
GetHighlightingMode() const129 CPDF_FormControl::HighlightingMode CPDF_FormControl::GetHighlightingMode()
130     const {
131   ByteString csH = m_pWidgetDict->GetByteStringFor("H", "I");
132   for (size_t i = 0; i < std::size(kHighlightModes); ++i) {
133     // TODO(tsepez): disambiguate string ctors.
134     if (csH == ByteStringView(kHighlightModes[i]))
135       return static_cast<HighlightingMode>(i);
136   }
137   return kInvert;
138 }
139 
GetMK() const140 CPDF_ApSettings CPDF_FormControl::GetMK() const {
141   return CPDF_ApSettings(m_pWidgetDict->GetMutableDictFor("MK"));
142 }
143 
HasMKEntry(const ByteString & csEntry) const144 bool CPDF_FormControl::HasMKEntry(const ByteString& csEntry) const {
145   return GetMK().HasMKEntry(csEntry);
146 }
147 
GetRotation() const148 int CPDF_FormControl::GetRotation() const {
149   return GetMK().GetRotation();
150 }
151 
GetColorARGB(const ByteString & csEntry)152 CFX_Color::TypeAndARGB CPDF_FormControl::GetColorARGB(
153     const ByteString& csEntry) {
154   return GetMK().GetColorARGB(csEntry);
155 }
156 
GetOriginalColorComponent(int index,const ByteString & csEntry)157 float CPDF_FormControl::GetOriginalColorComponent(int index,
158                                                   const ByteString& csEntry) {
159   return GetMK().GetOriginalColorComponent(index, csEntry);
160 }
161 
GetOriginalColor(const ByteString & csEntry)162 CFX_Color CPDF_FormControl::GetOriginalColor(const ByteString& csEntry) {
163   return GetMK().GetOriginalColor(csEntry);
164 }
165 
GetCaption(const ByteString & csEntry) const166 WideString CPDF_FormControl::GetCaption(const ByteString& csEntry) const {
167   return GetMK().GetCaption(csEntry);
168 }
169 
GetIcon(const ByteString & csEntry)170 RetainPtr<CPDF_Stream> CPDF_FormControl::GetIcon(const ByteString& csEntry) {
171   return GetMK().GetIcon(csEntry);
172 }
173 
GetIconFit() const174 CPDF_IconFit CPDF_FormControl::GetIconFit() const {
175   return GetMK().GetIconFit();
176 }
177 
GetTextPosition() const178 int CPDF_FormControl::GetTextPosition() const {
179   return GetMK().GetTextPosition();
180 }
181 
GetDefaultAppearance() const182 CPDF_DefaultAppearance CPDF_FormControl::GetDefaultAppearance() const {
183   if (m_pWidgetDict->KeyExist(pdfium::form_fields::kDA)) {
184     return CPDF_DefaultAppearance(
185         m_pWidgetDict->GetByteStringFor(pdfium::form_fields::kDA));
186   }
187   RetainPtr<const CPDF_Object> pObj =
188       m_pField->GetFieldAttr(pdfium::form_fields::kDA);
189   if (pObj)
190     return CPDF_DefaultAppearance(pObj->GetString());
191 
192   return m_pForm->GetDefaultAppearance();
193 }
194 
GetDefaultControlFontName() const195 absl::optional<WideString> CPDF_FormControl::GetDefaultControlFontName() const {
196   RetainPtr<CPDF_Font> pFont = GetDefaultControlFont();
197   if (!pFont)
198     return absl::nullopt;
199 
200   return WideString::FromDefANSI(pFont->GetBaseFontName().AsStringView());
201 }
202 
GetDefaultControlFont() const203 RetainPtr<CPDF_Font> CPDF_FormControl::GetDefaultControlFont() const {
204   float fFontSize;
205   CPDF_DefaultAppearance cDA = GetDefaultAppearance();
206   absl::optional<ByteString> csFontNameTag = cDA.GetFont(&fFontSize);
207   if (!csFontNameTag.has_value() || csFontNameTag->IsEmpty())
208     return nullptr;
209 
210   RetainPtr<CPDF_Dictionary> pDRDict = ToDictionary(
211       CPDF_FormField::GetMutableFieldAttrForDict(m_pWidgetDict.Get(), "DR"));
212   if (pDRDict) {
213     RetainPtr<CPDF_Dictionary> pFonts = pDRDict->GetMutableDictFor("Font");
214     if (ValidateFontResourceDict(pFonts.Get())) {
215       RetainPtr<CPDF_Dictionary> pElement =
216           pFonts->GetMutableDictFor(csFontNameTag.value());
217       if (pElement) {
218         RetainPtr<CPDF_Font> pFont =
219             m_pForm->GetFontForElement(std::move(pElement));
220         if (pFont)
221           return pFont;
222       }
223     }
224   }
225   RetainPtr<CPDF_Font> pFormFont = m_pForm->GetFormFont(csFontNameTag.value());
226   if (pFormFont)
227     return pFormFont;
228 
229   RetainPtr<CPDF_Dictionary> pPageDict = m_pWidgetDict->GetMutableDictFor("P");
230   RetainPtr<CPDF_Dictionary> pDict = ToDictionary(
231       CPDF_FormField::GetMutableFieldAttrForDict(pPageDict.Get(), "Resources"));
232   if (!pDict)
233     return nullptr;
234 
235   RetainPtr<CPDF_Dictionary> pFonts = pDict->GetMutableDictFor("Font");
236   if (!ValidateFontResourceDict(pFonts.Get()))
237     return nullptr;
238 
239   RetainPtr<CPDF_Dictionary> pElement =
240       pFonts->GetMutableDictFor(csFontNameTag.value());
241   if (!pElement)
242     return nullptr;
243 
244   return m_pForm->GetFontForElement(std::move(pElement));
245 }
246 
GetControlAlignment() const247 int CPDF_FormControl::GetControlAlignment() const {
248   if (m_pWidgetDict->KeyExist(pdfium::form_fields::kQ))
249     return m_pWidgetDict->GetIntegerFor(pdfium::form_fields::kQ, 0);
250 
251   RetainPtr<const CPDF_Object> pObj =
252       m_pField->GetFieldAttr(pdfium::form_fields::kQ);
253   if (pObj)
254     return pObj->GetInteger();
255 
256   return m_pForm->GetFormAlignment();
257 }
258