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
cpdfsdk_baannot.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 "fpdfsdk/cpdfsdk_baannot.h"
8
9#include <vector>
10
11#include "constants/annotation_common.h"
12#include "constants/annotation_flags.h"
13#include "constants/form_fields.h"
14#include "core/fpdfapi/parser/cpdf_array.h"
15#include "core/fpdfapi/parser/cpdf_dictionary.h"
16#include "core/fpdfapi/parser/cpdf_name.h"
17#include "core/fpdfapi/parser/cpdf_number.h"
18#include "core/fpdfapi/parser/cpdf_reference.h"
19#include "core/fpdfapi/parser/cpdf_stream.h"
20#include "core/fpdfapi/parser/cpdf_string.h"
21#include "core/fpdfapi/parser/fpdf_parser_decode.h"
22#include "core/fpdfapi/parser/fpdf_parser_utility.h"
23#include "core/fxge/cfx_drawutils.h"
24#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
25#include "fpdfsdk/cpdfsdk_pageview.h"
26#include "third_party/abseil-cpp/absl/types/optional.h"
27#include "third_party/base/check.h"
28#include "third_party/base/containers/contains.h"
29
31 CPDFSDK_PageView* pPageView)
32 : CPDFSDK_Annot(pPageView), m_pAnnot(pAnnot) {}
33
35
37 return this;
38}
39
43
45 return m_pAnnot;
46}
47
48const CPDF_Dictionary* CPDFSDK_BAAnnot::GetAnnotDict() const {
49 return m_pAnnot->GetAnnotDict();
50}
51
53 return m_pAnnot->GetMutableAnnotDict();
54}
55
56RetainPtr<CPDF_Dictionary> CPDFSDK_BAAnnot::GetAPDict() {
57 return GetMutableAnnotDict()->GetOrCreateDictFor(pdfium::annotation::kAP);
58}
59
61 m_pAnnot->ClearCachedAP();
62}
63
65 const CPDF_Annot::Subtype& annot_type) const {
66 return pdfium::Contains(
67 GetPageView()->GetFormFillEnv()->GetFocusableAnnotSubtypes(), annot_type);
68}
69
71 return m_pAnnot->GetRect();
72}
73
75 return m_pAnnot->GetSubtype();
76}
77
79 const CFX_Matrix& mtUser2Device,
81 m_pAnnot->DrawAppearance(GetPageView()->GetPDFPage(), pDevice, mtUser2Device,
82 mode);
83}
84
88
89void CPDFSDK_BAAnnot::SetAnnotName(const WideString& sName) {
90 RetainPtr<CPDF_Dictionary> pDict = GetMutableAnnotDict();
91 if (sName.IsEmpty()) {
92 pDict->RemoveFor(pdfium::annotation::kNM);
93 return;
94 }
95 pDict->SetNewFor<CPDF_String>(pdfium::annotation::kNM, sName.AsStringView());
96}
97
101
102void CPDFSDK_BAAnnot::SetFlags(uint32_t nFlags) {
103 GetMutableAnnotDict()->SetNewFor<CPDF_Number>(pdfium::annotation::kF,
104 static_cast<int>(nFlags));
105}
106
110
112 RetainPtr<CPDF_Dictionary> pDict = GetMutableAnnotDict();
113 pDict->SetNewFor<CPDF_String>(pdfium::annotation::kAS, "Off", false);
114}
115
119
121 RetainPtr<CPDF_Dictionary> pAnnotDict = GetMutableAnnotDict();
122 RetainPtr<CPDF_Array> pBorder =
123 pAnnotDict->GetMutableArrayFor(pdfium::annotation::kBorder);
124 if (pBorder) {
125 pBorder->SetNewAt<CPDF_Number>(2, nWidth);
126 return;
127 }
128 pAnnotDict->GetOrCreateDictFor("BS")->SetNewFor<CPDF_Number>("W", nWidth);
129}
130
132 RetainPtr<const CPDF_Array> pBorder =
133 GetAnnotDict()->GetArrayFor(pdfium::annotation::kBorder);
134 if (pBorder)
135 return pBorder->GetIntegerAt(2);
136
137 RetainPtr<const CPDF_Dictionary> pBSDict = GetAnnotDict()->GetDictFor("BS");
138 if (pBSDict)
139 return pBSDict->GetIntegerFor("W", 1);
140
141 return 1;
142}
143
145 RetainPtr<CPDF_Dictionary> pBSDict =
146 GetMutableAnnotDict()->GetOrCreateDictFor("BS");
147 const char* name = nullptr;
148 switch (nStyle) {
150 name = "S";
151 break;
153 name = "D";
154 break;
156 name = "B";
157 break;
159 name = "I";
160 break;
162 name = "U";
163 break;
164 }
165 pBSDict->SetNewFor<CPDF_Name>("S", name);
166}
167
169 RetainPtr<const CPDF_Dictionary> pBSDict = GetAnnotDict()->GetDictFor("BS");
170 if (pBSDict) {
171 ByteString sBorderStyle = pBSDict->GetByteStringFor("S", "S");
172 if (sBorderStyle == "S")
173 return BorderStyle::kSolid;
174 if (sBorderStyle == "D")
175 return BorderStyle::kDash;
176 if (sBorderStyle == "B")
178 if (sBorderStyle == "I")
179 return BorderStyle::kInset;
180 if (sBorderStyle == "U")
182 }
183
184 RetainPtr<const CPDF_Array> pBorder =
185 GetAnnotDict()->GetArrayFor(pdfium::annotation::kBorder);
186 if (pBorder) {
187 if (pBorder->size() >= 4) {
188 RetainPtr<const CPDF_Array> pDP = pBorder->GetArrayAt(3);
189 if (pDP && pDP->size() > 0)
190 return BorderStyle::kDash;
191 }
192 }
193
194 return BorderStyle::kSolid;
195}
196
198 uint32_t nFlags = GetFlags();
199 return !((nFlags & pdfium::annotation_flags::kInvisible) ||
200 (nFlags & pdfium::annotation_flags::kHidden) ||
201 (nFlags & pdfium::annotation_flags::kNoView));
202}
203
207
211
213 CPDF_AAction AAction = GetAAction();
214 if (AAction.ActionExist(eAAT))
215 return AAction.GetAction(eAAT);
216
218 return GetAction();
219
220 return CPDF_Action(nullptr);
221}
222
223void CPDFSDK_BAAnnot::SetOpenState(bool bOpenState) {
224 m_pAnnot->SetPopupAnnotOpenState(bOpenState);
225}
226
227void CPDFSDK_BAAnnot::UpdateAnnotRects() {
228 std::vector<CFX_FloatRect> rects;
229 rects.push_back(GetRect());
230
231 absl::optional<CFX_FloatRect> annot_rect = m_pAnnot->GetPopupAnnotRect();
232 if (annot_rect.has_value())
233 rects.push_back(annot_rect.value());
234
235 // Make the rects round up to avoid https://crbug.com/662804
236 for (CFX_FloatRect& rect : rects)
237 rect.Inflate(1, 1);
238
239 GetPageView()->UpdateRects(rects);
240}
241
242void CPDFSDK_BAAnnot::InvalidateRect() {
243 CFX_FloatRect view_bounding_box = GetViewBBox();
244 if (view_bounding_box.IsEmpty())
245 return;
246
247 view_bounding_box.Inflate(1, 1);
248 view_bounding_box.Normalize();
249 FX_RECT rect = view_bounding_box.GetOuterRect();
250 GetPageView()->GetFormFillEnv()->Invalidate(GetPage(), rect);
251}
252
254 if (m_pAnnot->GetSubtype() == CPDF_Annot::Subtype::POPUP)
255 return 1;
256
258}
259
261 const CFX_Matrix& mtUser2Device,
262 bool bDrawAnnots) {
263 if (!IsVisible())
264 return;
265
266 const CPDF_Annot::Subtype annot_type = GetAnnotSubtype();
267 if (bDrawAnnots && annot_type == CPDF_Annot::Subtype::POPUP) {
269 return;
270 }
271
272 if (!is_focused_ || !IsFocusableAnnot(annot_type) ||
273 this != GetPageView()->GetFormFillEnv()->GetFocusAnnot()) {
274 return;
275 }
276
277 CFX_FloatRect view_bounding_box = GetViewBBox();
278 if (view_bounding_box.IsEmpty())
279 return;
280
281 view_bounding_box.Normalize();
282 CFX_DrawUtils::DrawFocusRect(pDevice, mtUser2Device, view_bounding_box);
283}
284
285bool CPDFSDK_BAAnnot::DoHitTest(const CFX_PointF& point) {
286 return false;
287}
288
292
293void CPDFSDK_BAAnnot::OnMouseEnter(Mask<FWL_EVENTFLAG> nFlags) {
294 SetOpenState(true);
295 UpdateAnnotRects();
296}
297
298void CPDFSDK_BAAnnot::OnMouseExit(Mask<FWL_EVENTFLAG> nFlags) {
299 SetOpenState(false);
300 UpdateAnnotRects();
301}
302
303bool CPDFSDK_BAAnnot::OnLButtonDown(Mask<FWL_EVENTFLAG> nFlags,
304 const CFX_PointF& point) {
305 return false;
306}
307
308bool CPDFSDK_BAAnnot::OnLButtonUp(Mask<FWL_EVENTFLAG> nFlags,
309 const CFX_PointF& point) {
310 return false;
311}
312
313bool CPDFSDK_BAAnnot::OnLButtonDblClk(Mask<FWL_EVENTFLAG> nFlags,
314 const CFX_PointF& point) {
315 return false;
316}
317
318bool CPDFSDK_BAAnnot::OnMouseMove(Mask<FWL_EVENTFLAG> nFlags,
319 const CFX_PointF& point) {
320 return false;
321}
322
323bool CPDFSDK_BAAnnot::OnMouseWheel(Mask<FWL_EVENTFLAG> nFlags,
324 const CFX_PointF& point,
325 const CFX_Vector& delta) {
326 return false;
327}
328
329bool CPDFSDK_BAAnnot::OnRButtonDown(Mask<FWL_EVENTFLAG> nFlags,
330 const CFX_PointF& point) {
331 return false;
332}
333
334bool CPDFSDK_BAAnnot::OnRButtonUp(Mask<FWL_EVENTFLAG> nFlags,
335 const CFX_PointF& point) {
336 return false;
337}
338
339bool CPDFSDK_BAAnnot::OnChar(uint32_t nChar, Mask<FWL_EVENTFLAG> nFlags) {
340 return false;
341}
342
343bool CPDFSDK_BAAnnot::OnKeyDown(FWL_VKEYCODE nKeyCode,
344 Mask<FWL_EVENTFLAG> nFlags) {
345 // OnKeyDown() is implemented only for link annotations for now. As
346 // OnKeyDown() is implemented for other subtypes, following check should be
347 // modified.
348 if (nKeyCode != FWL_VKEY_Return ||
350 return false;
351 }
352
354 CPDFSDK_FormFillEnvironment* env = GetPageView()->GetFormFillEnv();
355 if (action.HasDict()) {
356 return env->DoActionLink(action, CPDF_AAction::kKeyStroke, nFlags);
357 }
358
360}
361
362bool CPDFSDK_BAAnnot::OnSetFocus(Mask<FWL_EVENTFLAG> nFlags) {
364 return false;
365
366 is_focused_ = true;
367 InvalidateRect();
368 return true;
369}
370
371bool CPDFSDK_BAAnnot::OnKillFocus(Mask<FWL_EVENTFLAG> nFlags) {
373 return false;
374
375 is_focused_ = false;
376 InvalidateRect();
377 return true;
378}
379
381 return false;
382}
383
385 return false;
386}
387
389 return false;
390}
391
393 return false;
394}
395
397 return WideString();
398}
399
401 return WideString();
402}
403
404void CPDFSDK_BAAnnot::ReplaceAndKeepSelection(const WideString& text) {}
405
406void CPDFSDK_BAAnnot::ReplaceSelection(const WideString& text) {}
407
409 return false;
410}
411
412bool CPDFSDK_BAAnnot::SetIndexSelected(int index, bool selected) {
413 return false;
414}
415
417 return false;
418}
419
421 if (m_pAnnot->GetSubtype() != CPDF_Annot::Subtype::LINK)
422 return CPDF_Dest(nullptr);
423
424 // Link annotations can have "Dest" entry defined as an explicit array.
425 // See ISO 32000-1:2008 spec, section 12.3.2.1.
426 return CPDF_Dest::Create(GetPageView()->GetPDFDocument(),
428}
BorderStyle
static void DrawFocusRect(CFX_RenderDevice *render_device, const CFX_Matrix &user_to_device, const CFX_FloatRect &view_bounding_box)
bool IsEmpty() const
void Inflate(float x, float y)
FX_RECT GetOuterRect() const
IPDF_Page * GetPage()
virtual int GetLayoutOrder() const
CPDFSDK_Annot(CPDFSDK_PageView *pPageView)
CPDFSDK_PageView * GetPageView() const
const CPDF_Dictionary * GetAnnotDict() const
CPDF_Annot * GetPDFAnnot() const override
CFX_FloatRect GetViewBBox() override
WideString GetAnnotName() const
void SetAnnotName(const WideString &sName)
void ReplaceSelection(const WideString &text) override
bool SetIndexSelected(int index, bool selected) override
bool OnChar(uint32_t nChar, Mask< FWL_EVENTFLAG > nFlags) override
CPDF_Annot::Subtype GetAnnotSubtype() const override
bool CanUndo() override
bool OnMouseWheel(Mask< FWL_EVENTFLAG > nFlags, const CFX_PointF &point, const CFX_Vector &delta) override
bool OnMouseMove(Mask< FWL_EVENTFLAG > nFlags, const CFX_PointF &point) override
bool IsIndexSelected(int index) override
bool SelectAllText() override
bool IsVisible() const
virtual void DrawAppearance(CFX_RenderDevice *pDevice, const CFX_Matrix &mtUser2Device, CPDF_Annot::AppearanceMode mode)
bool DoHitTest(const CFX_PointF &point) override
int GetLayoutOrder() const override
CPDFSDK_BAAnnot(CPDF_Annot *pAnnot, CPDFSDK_PageView *pPageView)
bool OnLButtonDown(Mask< FWL_EVENTFLAG > nFlags, const CFX_PointF &point) override
void ReplaceAndKeepSelection(const WideString &text) override
BorderStyle GetBorderStyle() const
bool OnSetFocus(Mask< FWL_EVENTFLAG > nFlags) override
int GetBorderWidth() const
void SetBorderStyle(BorderStyle nStyle)
CPDF_Action GetAction() const
void SetFlags(uint32_t nFlags)
CPDFSDK_BAAnnot * AsBAAnnot() override
virtual bool IsAppearanceValid()
WideString GetSelectedText() override
CFX_FloatRect GetRect() const override
bool OnLButtonDblClk(Mask< FWL_EVENTFLAG > nFlags, const CFX_PointF &point) override
CPDF_AAction GetAAction() const
bool OnRButtonUp(Mask< FWL_EVENTFLAG > nFlags, const CFX_PointF &point) override
bool Undo() override
bool OnLButtonUp(Mask< FWL_EVENTFLAG > nFlags, const CFX_PointF &point) override
~CPDFSDK_BAAnnot() override
WideString GetText() override
void OnDraw(CFX_RenderDevice *pDevice, const CFX_Matrix &mtUser2Device, bool bDrawAnnots) override
bool Redo() override
bool OnRButtonDown(Mask< FWL_EVENTFLAG > nFlags, const CFX_PointF &point) override
CPDF_Dest GetDestination() const
bool OnKeyDown(FWL_VKEYCODE nKeyCode, Mask< FWL_EVENTFLAG > nFlags) override
ByteString GetAppState() const
bool IsFocusableAnnot(const CPDF_Annot::Subtype &annot_type) const
uint32_t GetFlags() const
bool CanRedo() override
void SetBorderWidth(int nWidth)
bool OnKillFocus(Mask< FWL_EVENTFLAG > nFlags) override
RetainPtr< CPDF_Dictionary > GetMutableAnnotDict()
RetainPtr< CPDF_Dictionary > GetAPDict()
virtual CPDF_Action GetAAction(CPDF_AAction::AActionType eAAT)
void OnMouseEnter(Mask< FWL_EVENTFLAG > nFlags) override
CPDFSDK_Annot::UnsafeInputHandlers * GetUnsafeInputHandlers() override
void OnMouseExit(Mask< FWL_EVENTFLAG > nFlags) override
bool DoActionDestination(const CPDF_Dest &dest)
bool DoActionLink(const CPDF_Action &action, CPDF_AAction::AActionType type, Mask< FWL_EVENTFLAG > modifiers)
bool ActionExist(AActionType eType) const
CPDF_Action GetAction(AActionType eType) const
bool HasDict() const
Definition cpdf_action.h:50
static CPDF_Dest Create(CPDF_Document *pDoc, RetainPtr< const CPDF_Object > pDest)
Definition cpdf_dest.cpp:42
int GetIntegerFor(const ByteString &key) const
RetainPtr< const CPDF_Dictionary > GetDictFor(const ByteString &key) const
WideString GetUnicodeTextFor(const ByteString &key) const
ByteString GetByteStringFor(const ByteString &key) const
RetainPtr< const CPDF_Object > GetDirectObjectFor(const ByteString &key) const
bool operator==(const char *ptr) const
bool IsEmpty() const
Definition widestring.h:118
@ FWL_VKEY_Return
constexpr uint32_t kNoView
constexpr uint32_t kHidden
constexpr uint32_t kInvisible