xref: /aosp_15_r20/external/pdfium/core/fpdfdoc/cpdf_annotlist.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_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/fxge/cfx_renderdevice.h"
33 
34 namespace {
35 
PopupAppearsForAnnotType(CPDF_Annot::Subtype subtype)36 bool PopupAppearsForAnnotType(CPDF_Annot::Subtype subtype) {
37   switch (subtype) {
38     case CPDF_Annot::Subtype::TEXT:
39     case CPDF_Annot::Subtype::LINE:
40     case CPDF_Annot::Subtype::SQUARE:
41     case CPDF_Annot::Subtype::CIRCLE:
42     case CPDF_Annot::Subtype::POLYGON:
43     case CPDF_Annot::Subtype::POLYLINE:
44     case CPDF_Annot::Subtype::HIGHLIGHT:
45     case CPDF_Annot::Subtype::UNDERLINE:
46     case CPDF_Annot::Subtype::SQUIGGLY:
47     case CPDF_Annot::Subtype::STRIKEOUT:
48     case CPDF_Annot::Subtype::STAMP:
49     case CPDF_Annot::Subtype::CARET:
50     case CPDF_Annot::Subtype::INK:
51     case CPDF_Annot::Subtype::FILEATTACHMENT:
52     case CPDF_Annot::Subtype::REDACT:
53       return true;
54     case CPDF_Annot::Subtype::UNKNOWN:
55     case CPDF_Annot::Subtype::LINK:
56     case CPDF_Annot::Subtype::FREETEXT:
57     case CPDF_Annot::Subtype::POPUP:
58     case CPDF_Annot::Subtype::SOUND:
59     case CPDF_Annot::Subtype::MOVIE:
60     case CPDF_Annot::Subtype::WIDGET:
61     case CPDF_Annot::Subtype::SCREEN:
62     case CPDF_Annot::Subtype::PRINTERMARK:
63     case CPDF_Annot::Subtype::TRAPNET:
64     case CPDF_Annot::Subtype::WATERMARK:
65     case CPDF_Annot::Subtype::THREED:
66     case CPDF_Annot::Subtype::RICHMEDIA:
67     case CPDF_Annot::Subtype::XFAWIDGET:
68       return false;
69   }
70 }
71 
CreatePopupAnnot(CPDF_Document * pDocument,CPDF_Page * pPage,CPDF_Annot * pAnnot)72 std::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 =
85       pParentDict->GetByteStringFor(pdfium::annotation::kContents);
86   if (PDF_DecodeText(contents.raw_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>(
94       pdfium::form_fields::kT,
95       pParentDict->GetByteStringFor(pdfium::form_fields::kT), false);
96   pAnnotDict->SetNewFor<CPDF_String>(pdfium::annotation::kContents, contents,
97                                      /*bHex=*/false);
98 
99   CFX_FloatRect rect = pParentDict->GetRectFor(pdfium::annotation::kRect);
100   rect.Normalize();
101   CFX_FloatRect popupRect(0, 0, 200, 200);
102   // Note that if the popup can set its own dimensions, then we will need to
103   // make sure that it isn't larger than the page size.
104   if (rect.left + popupRect.Width() > pPage->GetPageWidth() &&
105       rect.bottom - popupRect.Height() < 0) {
106     // If the annotation is on the bottom-right corner of the page, then place
107     // the popup above and to the left of the annotation.
108     popupRect.Translate(rect.right - popupRect.Width(), rect.top);
109   } else {
110     // Place the popup below and to the right of the annotation without getting
111     // clipped by page edges.
112     popupRect.Translate(
113         std::min(rect.left, pPage->GetPageWidth() - popupRect.Width()),
114         std::max(rect.bottom - popupRect.Height(), 0.f));
115   }
116 
117   pAnnotDict->SetRectFor(pdfium::annotation::kRect, popupRect);
118   pAnnotDict->SetNewFor<CPDF_Number>(pdfium::annotation::kF, 0);
119 
120   auto pPopupAnnot =
121       std::make_unique<CPDF_Annot>(std::move(pAnnotDict), pDocument);
122   pAnnot->SetPopupAnnot(pPopupAnnot.get());
123   return pPopupAnnot;
124 }
125 
GenerateAP(CPDF_Document * pDoc,CPDF_Dictionary * pAnnotDict)126 void GenerateAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict) {
127   if (!pAnnotDict ||
128       pAnnotDict->GetByteStringFor(pdfium::annotation::kSubtype) != "Widget") {
129     return;
130   }
131 
132   RetainPtr<const CPDF_Object> pFieldTypeObj =
133       CPDF_FormField::GetFieldAttrForDict(pAnnotDict, pdfium::form_fields::kFT);
134   if (!pFieldTypeObj)
135     return;
136 
137   ByteString field_type = pFieldTypeObj->GetString();
138   if (field_type == pdfium::form_fields::kTx) {
139     CPDF_GenerateAP::GenerateFormAP(pDoc, pAnnotDict,
140                                     CPDF_GenerateAP::kTextField);
141     return;
142   }
143 
144   RetainPtr<const CPDF_Object> pFieldFlagsObj =
145       CPDF_FormField::GetFieldAttrForDict(pAnnotDict, pdfium::form_fields::kFf);
146   uint32_t flags = pFieldFlagsObj ? pFieldFlagsObj->GetInteger() : 0;
147   if (field_type == pdfium::form_fields::kCh) {
148     auto type = (flags & pdfium::form_flags::kChoiceCombo)
149                     ? CPDF_GenerateAP::kComboBox
150                     : CPDF_GenerateAP::kListBox;
151     CPDF_GenerateAP::GenerateFormAP(pDoc, pAnnotDict, type);
152     return;
153   }
154 
155   if (field_type != pdfium::form_fields::kBtn)
156     return;
157   if (flags & pdfium::form_flags::kButtonPushbutton)
158     return;
159   if (pAnnotDict->KeyExist(pdfium::annotation::kAS))
160     return;
161 
162   RetainPtr<const CPDF_Dictionary> pParentDict =
163       pAnnotDict->GetDictFor(pdfium::form_fields::kParent);
164   if (!pParentDict || !pParentDict->KeyExist(pdfium::annotation::kAS))
165     return;
166 
167   pAnnotDict->SetNewFor<CPDF_String>(
168       pdfium::annotation::kAS,
169       pParentDict->GetByteStringFor(pdfium::annotation::kAS), false);
170 }
171 
172 }  // namespace
173 
CPDF_AnnotList(CPDF_Page * pPage)174 CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage)
175     : m_pDocument(pPage->GetDocument()) {
176   RetainPtr<CPDF_Array> pAnnots = pPage->GetMutableAnnotsArray();
177   if (!pAnnots)
178     return;
179 
180   const CPDF_Dictionary* pRoot = m_pDocument->GetRoot();
181   RetainPtr<const CPDF_Dictionary> pAcroForm = pRoot->GetDictFor("AcroForm");
182   bool bRegenerateAP =
183       pAcroForm && pAcroForm->GetBooleanFor("NeedAppearances", false);
184   for (size_t i = 0; i < pAnnots->size(); ++i) {
185     RetainPtr<CPDF_Dictionary> pDict =
186         ToDictionary(pAnnots->GetMutableDirectObjectAt(i));
187     if (!pDict)
188       continue;
189     const ByteString subtype =
190         pDict->GetByteStringFor(pdfium::annotation::kSubtype);
191     if (subtype == "Popup") {
192       // Skip creating Popup annotations in the PDF document since PDFium
193       // provides its own Popup annotations.
194       continue;
195     }
196     pAnnots->ConvertToIndirectObjectAt(i, m_pDocument);
197     m_AnnotList.push_back(std::make_unique<CPDF_Annot>(pDict, m_pDocument));
198     if (bRegenerateAP && subtype == "Widget" &&
199         CPDF_InteractiveForm::IsUpdateAPEnabled() &&
200         !pDict->GetDictFor(pdfium::annotation::kAP)) {
201       GenerateAP(m_pDocument, pDict.Get());
202     }
203   }
204 
205   m_nAnnotCount = m_AnnotList.size();
206   for (size_t i = 0; i < m_nAnnotCount; ++i) {
207     std::unique_ptr<CPDF_Annot> pPopupAnnot =
208         CreatePopupAnnot(m_pDocument, pPage, m_AnnotList[i].get());
209     if (pPopupAnnot)
210       m_AnnotList.push_back(std::move(pPopupAnnot));
211   }
212 }
213 
~CPDF_AnnotList()214 CPDF_AnnotList::~CPDF_AnnotList() {
215   // Move the pop-up annotations out of |m_AnnotList| into |popups|. Then
216   // destroy |m_AnnotList| first. This prevents dangling pointers to the pop-up
217   // annotations.
218   size_t nPopupCount = m_AnnotList.size() - m_nAnnotCount;
219   std::vector<std::unique_ptr<CPDF_Annot>> popups(nPopupCount);
220   for (size_t i = 0; i < nPopupCount; ++i)
221     popups[i] = std::move(m_AnnotList[m_nAnnotCount + i]);
222   m_AnnotList.clear();
223 }
224 
Contains(const CPDF_Annot * pAnnot) const225 bool CPDF_AnnotList::Contains(const CPDF_Annot* pAnnot) const {
226   auto it = std::find_if(m_AnnotList.begin(), m_AnnotList.end(),
227                          [pAnnot](const std::unique_ptr<CPDF_Annot>& annot) {
228                            return annot.get() == pAnnot;
229                          });
230   return it != m_AnnotList.end();
231 }
232 
DisplayPass(CPDF_Page * pPage,CFX_RenderDevice * pDevice,CPDF_RenderContext * pContext,bool bPrinting,const CFX_Matrix & mtMatrix,bool bWidgetPass)233 void CPDF_AnnotList::DisplayPass(CPDF_Page* pPage,
234                                  CFX_RenderDevice* pDevice,
235                                  CPDF_RenderContext* pContext,
236                                  bool bPrinting,
237                                  const CFX_Matrix& mtMatrix,
238                                  bool bWidgetPass) {
239   for (const auto& pAnnot : m_AnnotList) {
240     bool bWidget = pAnnot->GetSubtype() == CPDF_Annot::Subtype::WIDGET;
241     if ((bWidgetPass && !bWidget) || (!bWidgetPass && bWidget))
242       continue;
243 
244     uint32_t annot_flags = pAnnot->GetFlags();
245     if (annot_flags & pdfium::annotation_flags::kHidden)
246       continue;
247 
248     if (bPrinting && (annot_flags & pdfium::annotation_flags::kPrint) == 0)
249       continue;
250 
251     if (!bPrinting && (annot_flags & pdfium::annotation_flags::kNoView))
252       continue;
253 
254     if (pContext) {
255       pAnnot->DrawInContext(pPage, pContext, mtMatrix,
256                             CPDF_Annot::AppearanceMode::kNormal);
257     } else if (!pAnnot->DrawAppearance(pPage, pDevice, mtMatrix,
258                                        CPDF_Annot::AppearanceMode::kNormal)) {
259       pAnnot->DrawBorder(pDevice, &mtMatrix);
260     }
261   }
262 }
263 
DisplayAnnots(CPDF_Page * pPage,CFX_RenderDevice * pDevice,CPDF_RenderContext * pContext,bool bPrinting,const CFX_Matrix & mtUser2Device,bool bShowWidget)264 void CPDF_AnnotList::DisplayAnnots(CPDF_Page* pPage,
265                                    CFX_RenderDevice* pDevice,
266                                    CPDF_RenderContext* pContext,
267                                    bool bPrinting,
268                                    const CFX_Matrix& mtUser2Device,
269                                    bool bShowWidget) {
270   DisplayPass(pPage, pDevice, pContext, bPrinting, mtUser2Device, false);
271   if (bShowWidget)
272     DisplayPass(pPage, pDevice, pContext, bPrinting, mtUser2Device, true);
273 }
274