Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpdf_formcontrol.cpp
Go to the documentation of this file.
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
24namespace {
25
26constexpr char kHighlightModes[] = {'N', 'I', 'O', 'P', 'T'};
27
28// Order of |kHighlightModes| must match order of HighlightingMode enum.
29static_assert(kHighlightModes[CPDF_FormControl::kNone] == 'N',
30 "HighlightingMode mismatch");
31static_assert(kHighlightModes[CPDF_FormControl::kInvert] == 'I',
32 "HighlightingMode mismatch");
33static_assert(kHighlightModes[CPDF_FormControl::kOutline] == 'O',
34 "HighlightingMode mismatch");
35static_assert(kHighlightModes[CPDF_FormControl::kPush] == 'P',
36 "HighlightingMode mismatch");
37static_assert(kHighlightModes[CPDF_FormControl::kToggle] == 'T',
38 "HighlightingMode mismatch");
39
40} // namespace
41
43 RetainPtr<CPDF_Dictionary> pWidgetDict,
46 DCHECK(m_pWidgetDict);
47}
48
50
52 return m_pWidgetDict->GetRectFor("Rect");
53}
54
55ByteString 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
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
85WideString 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
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
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
117void 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
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
140CPDF_ApSettings CPDF_FormControl::GetMK() const {
141 return CPDF_ApSettings(m_pWidgetDict->GetMutableDictFor("MK"));
142}
143
144bool CPDF_FormControl::HasMKEntry(const ByteString& csEntry) const {
145 return GetMK().HasMKEntry(csEntry);
146}
147
149 return GetMK().GetRotation();
150}
151
153 const ByteString& csEntry) {
154 return GetMK().GetColorARGB(csEntry);
155}
156
158 const ByteString& csEntry) {
159 return GetMK().GetOriginalColorComponent(index, csEntry);
160}
161
162CFX_Color CPDF_FormControl::GetOriginalColor(const ByteString& csEntry) {
163 return GetMK().GetOriginalColor(csEntry);
164}
165
166WideString CPDF_FormControl::GetCaption(const ByteString& csEntry) const {
167 return GetMK().GetCaption(csEntry);
168}
169
170RetainPtr<CPDF_Stream> CPDF_FormControl::GetIcon(const ByteString& csEntry) {
171 return GetMK().GetIcon(csEntry);
172}
173
175 return GetMK().GetIconFit();
176}
177
179 return GetMK().GetTextPosition();
180}
181
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
196 RetainPtr<CPDF_Font> pFont = GetDefaultControlFont();
197 if (!pFont)
198 return absl::nullopt;
199
200 return WideString::FromDefANSI(pFont->GetBaseFontName().AsStringView());
201}
202
203RetainPtr<CPDF_Font> CPDF_FormControl::GetDefaultControlFont() const {
204 float fFontSize;
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
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}
CFX_Color GetOriginalColor(const ByteString &csEntry) const
bool HasMKEntry(const ByteString &csEntry) const
int GetRotation() const
RetainPtr< CPDF_Stream > GetIcon(const ByteString &csEntry) const
CPDF_IconFit GetIconFit() const
WideString GetCaption(const ByteString &csEntry) const
float GetOriginalColorComponent(int index, const ByteString &csEntry) const
CFX_Color::TypeAndARGB GetColorARGB(const ByteString &csEntry) const
int GetTextPosition() const
WideString GetExportValue() const
int GetControlAlignment() const
bool HasMKEntry(const ByteString &csEntry) const
CFX_FloatRect GetRect() const
HighlightingMode GetHighlightingMode() const
float GetOriginalColorComponent(int index, const ByteString &csEntry)
absl::optional< WideString > GetDefaultControlFontName() const
ByteString GetCheckedAPState() const
ByteString GetOnStateName() const
CPDF_IconFit GetIconFit() const
CPDF_DefaultAppearance GetDefaultAppearance() const
CFX_Color::TypeAndARGB GetColorARGB(const ByteString &csEntry)
void CheckControl(bool bChecked)
bool IsDefaultChecked() const
CPDF_FormControl(CPDF_FormField *pField, RetainPtr< CPDF_Dictionary > pWidgetDict, CPDF_InteractiveForm *pForm)
int GetTextPosition() const
bool operator==(const ByteString &other) const
ByteString & operator=(const char *str)
ByteString & operator=(ByteString &&that) noexcept
bool IsEmpty() const
Definition bytestring.h:119
static WideString FromDefANSI(ByteStringView str)