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_annotlist.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_annotlist.h"
8
9#include <algorithm>
10#include <memory>
11#include <utility>
12
13#include "constants/annotation_common.h"
14#include "constants/annotation_flags.h"
15#include "constants/form_fields.h"
16#include "constants/form_flags.h"
17#include "core/fpdfapi/page/cpdf_occontext.h"
18#include "core/fpdfapi/page/cpdf_page.h"
19#include "core/fpdfapi/parser/cpdf_array.h"
20#include "core/fpdfapi/parser/cpdf_dictionary.h"
21#include "core/fpdfapi/parser/cpdf_document.h"
22#include "core/fpdfapi/parser/cpdf_name.h"
23#include "core/fpdfapi/parser/cpdf_number.h"
24#include "core/fpdfapi/parser/cpdf_reference.h"
25#include "core/fpdfapi/parser/cpdf_string.h"
26#include "core/fpdfapi/parser/fpdf_parser_decode.h"
27#include "core/fpdfapi/render/cpdf_renderoptions.h"
28#include "core/fpdfdoc/cpdf_annot.h"
29#include "core/fpdfdoc/cpdf_formfield.h"
30#include "core/fpdfdoc/cpdf_generateap.h"
31#include "core/fpdfdoc/cpdf_interactiveform.h"
32#include "core/fxcrt/check.h"
33
34namespace {
35
36bool PopupAppearsForAnnotType(CPDF_Annot::Subtype subtype) {
37 switch (subtype) {
53 return true;
68 return false;
69 }
70}
71
72std::unique_ptr<CPDF_Annot> CreatePopupAnnot(CPDF_Document* pDocument,
73 CPDF_Page* pPage,
74 CPDF_Annot* pAnnot) {
75 if (!PopupAppearsForAnnotType(pAnnot->GetSubtype()))
76 return nullptr;
77
78 const CPDF_Dictionary* pParentDict = pAnnot->GetAnnotDict();
79 if (!pParentDict)
80 return nullptr;
81
82 // TODO(crbug.com/pdfium/1098): Determine if we really need to check if
83 // /Contents is empty or not. If so, optimize decoding for empty check.
84 ByteString contents =
86 if (PDF_DecodeText(contents.unsigned_span()).IsEmpty()) {
87 return nullptr;
88 }
89
90 auto pAnnotDict = pDocument->New<CPDF_Dictionary>();
91 pAnnotDict->SetNewFor<CPDF_Name>(pdfium::annotation::kType, "Annot");
92 pAnnotDict->SetNewFor<CPDF_Name>(pdfium::annotation::kSubtype, "Popup");
93 pAnnotDict->SetNewFor<CPDF_String>(
96 pAnnotDict->SetNewFor<CPDF_String>(pdfium::annotation::kContents, contents);
97
99 rect.Normalize();
100 CFX_FloatRect popupRect(0, 0, 200, 200);
101 // Note that if the popup can set its own dimensions, then we will need to
102 // make sure that it isn't larger than the page size.
103 if (rect.left + popupRect.Width() > pPage->GetPageWidth() &&
104 rect.bottom - popupRect.Height() < 0) {
105 // If the annotation is on the bottom-right corner of the page, then place
106 // the popup above and to the left of the annotation.
107 popupRect.Translate(rect.right - popupRect.Width(), rect.top);
108 } else {
109 // Place the popup below and to the right of the annotation without getting
110 // clipped by page edges.
111 popupRect.Translate(
112 std::min(rect.left, pPage->GetPageWidth() - popupRect.Width()),
113 std::max(rect.bottom - popupRect.Height(), 0.f));
114 }
115
116 pAnnotDict->SetRectFor(pdfium::annotation::kRect, popupRect);
117 pAnnotDict->SetNewFor<CPDF_Number>(pdfium::annotation::kF, 0);
118
119 auto pPopupAnnot =
120 std::make_unique<CPDF_Annot>(std::move(pAnnotDict), pDocument);
121 pAnnot->SetPopupAnnot(pPopupAnnot.get());
122 return pPopupAnnot;
123}
124
125void GenerateAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict) {
126 if (!pAnnotDict ||
128 return;
129 }
130
131 RetainPtr<const CPDF_Object> pFieldTypeObj =
132 CPDF_FormField::GetFieldAttrForDict(pAnnotDict, pdfium::form_fields::kFT);
133 if (!pFieldTypeObj)
134 return;
135
136 ByteString field_type = pFieldTypeObj->GetString();
137 if (field_type == pdfium::form_fields::kTx) {
140 return;
141 }
142
143 RetainPtr<const CPDF_Object> pFieldFlagsObj =
144 CPDF_FormField::GetFieldAttrForDict(pAnnotDict, pdfium::form_fields::kFf);
145 uint32_t flags = pFieldFlagsObj ? pFieldFlagsObj->GetInteger() : 0;
146 if (field_type == pdfium::form_fields::kCh) {
147 auto type = (flags & pdfium::form_flags::kChoiceCombo)
150 CPDF_GenerateAP::GenerateFormAP(pDoc, pAnnotDict, type);
151 return;
152 }
153
154 if (field_type != pdfium::form_fields::kBtn)
155 return;
157 return;
159 return;
160
161 RetainPtr<const CPDF_Dictionary> pParentDict =
162 pAnnotDict->GetDictFor(pdfium::form_fields::kParent);
163 if (!pParentDict || !pParentDict->KeyExist(pdfium::annotation::kAS))
164 return;
165
166 pAnnotDict->SetNewFor<CPDF_String>(
168 pParentDict->GetByteStringFor(pdfium::annotation::kAS));
169}
170
171} // namespace
172
173CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage)
175 RetainPtr<CPDF_Array> pAnnots = m_pPage->GetMutableAnnotsArray();
176 if (!pAnnots)
177 return;
178
179 const CPDF_Dictionary* pRoot = m_pDocument->GetRoot();
180 RetainPtr<const CPDF_Dictionary> pAcroForm = pRoot->GetDictFor("AcroForm");
181 bool bRegenerateAP =
182 pAcroForm && pAcroForm->GetBooleanFor("NeedAppearances", false);
183 for (size_t i = 0; i < pAnnots->size(); ++i) {
185 ToDictionary(pAnnots->GetMutableDirectObjectAt(i));
186 if (!pDict)
187 continue;
188 const ByteString subtype =
189 pDict->GetByteStringFor(pdfium::annotation::kSubtype);
190 if (subtype == "Popup") {
191 // Skip creating Popup annotations in the PDF document since PDFium
192 // provides its own Popup annotations.
193 continue;
194 }
195 pAnnots->ConvertToIndirectObjectAt(i, m_pDocument);
196 m_AnnotList.push_back(std::make_unique<CPDF_Annot>(pDict, m_pDocument));
197 if (bRegenerateAP && subtype == "Widget" &&
199 !pDict->GetDictFor(pdfium::annotation::kAP)) {
200 GenerateAP(m_pDocument, pDict.Get());
201 }
202 }
203
204 m_nAnnotCount = m_AnnotList.size();
205 for (size_t i = 0; i < m_nAnnotCount; ++i) {
206 std::unique_ptr<CPDF_Annot> pPopupAnnot =
207 CreatePopupAnnot(m_pDocument, m_pPage, m_AnnotList[i].get());
208 if (pPopupAnnot)
209 m_AnnotList.push_back(std::move(pPopupAnnot));
210 }
211}
212
213CPDF_AnnotList::~CPDF_AnnotList() {
214 // Move the pop-up annotations out of |m_AnnotList| into |popups|. Then
215 // destroy |m_AnnotList| first. This prevents dangling pointers to the pop-up
216 // annotations.
217 size_t nPopupCount = m_AnnotList.size() - m_nAnnotCount;
218 std::vector<std::unique_ptr<CPDF_Annot>> popups(nPopupCount);
219 for (size_t i = 0; i < nPopupCount; ++i)
220 popups[i] = std::move(m_AnnotList[m_nAnnotCount + i]);
221 m_AnnotList.clear();
222}
223
224bool CPDF_AnnotList::Contains(const CPDF_Annot* pAnnot) const {
225 auto it = std::find_if(m_AnnotList.begin(), m_AnnotList.end(),
226 [pAnnot](const std::unique_ptr<CPDF_Annot>& annot) {
227 return annot.get() == pAnnot;
228 });
229 return it != m_AnnotList.end();
230}
231
232void CPDF_AnnotList::DisplayPass(CPDF_RenderContext* pContext,
233 bool bPrinting,
234 const CFX_Matrix& mtMatrix,
235 bool bWidgetPass) {
236 CHECK(pContext);
237 for (const auto& pAnnot : m_AnnotList) {
238 bool bWidget = pAnnot->GetSubtype() == CPDF_Annot::Subtype::WIDGET;
239 if ((bWidgetPass && !bWidget) || (!bWidgetPass && bWidget))
240 continue;
241
242 uint32_t annot_flags = pAnnot->GetFlags();
243 if (annot_flags & pdfium::annotation_flags::kHidden)
244 continue;
245
246 if (bPrinting && (annot_flags & pdfium::annotation_flags::kPrint) == 0)
247 continue;
248
249 if (!bPrinting && (annot_flags & pdfium::annotation_flags::kNoView))
250 continue;
251
252 pAnnot->DrawInContext(m_pPage, pContext, mtMatrix,
253 CPDF_Annot::AppearanceMode::kNormal);
254 }
255}
256
257void CPDF_AnnotList::DisplayAnnots(CPDF_RenderContext* pContext,
258 bool bPrinting,
259 const CFX_Matrix& mtUser2Device,
260 bool bShowWidget) {
261 CHECK(pContext);
262 DisplayPass(pContext, bPrinting, mtUser2Device, false);
263 if (bShowWidget)
264 DisplayPass(pContext, bPrinting, mtUser2Device, true);
265}
fxcrt::ByteString ByteString
Definition bytestring.h:180
constexpr CFX_FloatRect(float l, float b, float r, float t)
float Width() const
float Height() const
void Translate(float e, float f)
void DisplayAnnots(CPDF_RenderContext *pContext, bool bPrinting, const CFX_Matrix &mtUser2Device, bool bShowWidget)
bool Contains(const CPDF_Annot *pAnnot) const
~CPDF_AnnotList() override
CPDF_AnnotList(CPDF_Page *pPage)
Subtype GetSubtype() const
const CPDF_Dictionary * GetAnnotDict() const
Definition cpdf_annot.h:83
void SetPopupAnnot(CPDF_Annot *pAnnot)
Definition cpdf_annot.h:103
std::vector< RetainPtr< CPDF_Object > >::const_iterator const_iterator
Definition cpdf_array.h:29
bool KeyExist(const ByteString &key) const
ByteString GetByteStringFor(const ByteString &key) const
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
CFX_FloatRect GetRectFor(const ByteString &key) const
static void GenerateFormAP(CPDF_Document *pDoc, CPDF_Dictionary *pAnnotDict, FormType type)
float GetPageWidth() const override
Definition cpdf_page.cpp:55
bool operator==(const char *ptr) const
bool operator!=(const char *ptr) const
Definition bytestring.h:65
constexpr uint32_t kButtonPushbutton
Definition form_flags.h:21
constexpr uint32_t kChoiceCombo
Definition form_flags.h:36
#define CHECK(cvref)