1 // Copyright 2016 PDFium Authors. All rights reserved.
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/render/cpdf_renderoptions.h"
27 #include "core/fpdfdoc/cpdf_annot.h"
28 #include "core/fpdfdoc/cpdf_formfield.h"
29 #include "core/fpdfdoc/cpdf_interactiveform.h"
30 #include "core/fpdfdoc/cpvt_generateap.h"
31 #include "core/fxge/cfx_renderdevice.h"
32 #include "third_party/base/ptr_util.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 return true;
53 case CPDF_Annot::Subtype::UNKNOWN:
54 case CPDF_Annot::Subtype::LINK:
55 case CPDF_Annot::Subtype::FREETEXT:
56 case CPDF_Annot::Subtype::POPUP:
57 case CPDF_Annot::Subtype::SOUND:
58 case CPDF_Annot::Subtype::MOVIE:
59 case CPDF_Annot::Subtype::WIDGET:
60 case CPDF_Annot::Subtype::SCREEN:
61 case CPDF_Annot::Subtype::PRINTERMARK:
62 case CPDF_Annot::Subtype::TRAPNET:
63 case CPDF_Annot::Subtype::WATERMARK:
64 case CPDF_Annot::Subtype::THREED:
65 case CPDF_Annot::Subtype::RICHMEDIA:
66 case CPDF_Annot::Subtype::XFAWIDGET:
67 default:
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(jaepark): We shouldn't strip BOM for some strings and not for others.
83 // See pdfium:593.
84 WideString sContents =
85 pParentDict->GetUnicodeTextFor(pdfium::annotation::kContents);
86 if (sContents.IsEmpty())
87 return nullptr;
88
89 auto pAnnotDict = pDocument->New<CPDF_Dictionary>();
90 pAnnotDict->SetNewFor<CPDF_Name>(pdfium::annotation::kType, "Annot");
91 pAnnotDict->SetNewFor<CPDF_Name>(pdfium::annotation::kSubtype, "Popup");
92 pAnnotDict->SetNewFor<CPDF_String>(
93 pdfium::form_fields::kT,
94 pParentDict->GetStringFor(pdfium::form_fields::kT), false);
95 pAnnotDict->SetNewFor<CPDF_String>(pdfium::annotation::kContents,
96 sContents.ToUTF8(), false);
97
98 CFX_FloatRect rect = pParentDict->GetRectFor(pdfium::annotation::kRect);
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 pdfium::MakeUnique<CPDF_Annot>(std::move(pAnnotDict), pDocument);
121 pAnnot->SetPopupAnnot(pPopupAnnot.get());
122 return pPopupAnnot;
123 }
124
GenerateAP(CPDF_Document * pDoc,CPDF_Dictionary * pAnnotDict)125 void GenerateAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict) {
126 if (!pAnnotDict ||
127 pAnnotDict->GetStringFor(pdfium::annotation::kSubtype) != "Widget") {
128 return;
129 }
130
131 CPDF_Object* pFieldTypeObj =
132 CPDF_FormField::GetFieldAttr(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) {
138 CPVT_GenerateAP::GenerateFormAP(pDoc, pAnnotDict,
139 CPVT_GenerateAP::kTextField);
140 return;
141 }
142
143 CPDF_Object* pFieldFlagsObj =
144 CPDF_FormField::GetFieldAttr(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)
148 ? CPVT_GenerateAP::kComboBox
149 : CPVT_GenerateAP::kListBox;
150 CPVT_GenerateAP::GenerateFormAP(pDoc, pAnnotDict, type);
151 return;
152 }
153
154 if (field_type != pdfium::form_fields::kBtn)
155 return;
156 if (flags & pdfium::form_flags::kButtonPushbutton)
157 return;
158 if (pAnnotDict->KeyExist(pdfium::annotation::kAS))
159 return;
160
161 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>(
167 pdfium::annotation::kAS,
168 pParentDict->GetStringFor(pdfium::annotation::kAS), false);
169 }
170
171 } // namespace
172
CPDF_AnnotList(CPDF_Page * pPage)173 CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage)
174 : m_pDocument(pPage->GetDocument()) {
175 CPDF_Array* pAnnots = pPage->GetDict()->GetArrayFor("Annots");
176 if (!pAnnots)
177 return;
178
179 const CPDF_Dictionary* pRoot = m_pDocument->GetRoot();
180 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) {
184 CPDF_Dictionary* pDict = ToDictionary(pAnnots->GetDirectObjectAt(i));
185 if (!pDict)
186 continue;
187 const ByteString subtype =
188 pDict->GetStringFor(pdfium::annotation::kSubtype);
189 if (subtype == "Popup") {
190 // Skip creating Popup annotations in the PDF document since PDFium
191 // provides its own Popup annotations.
192 continue;
193 }
194 pAnnots->ConvertToIndirectObjectAt(i, m_pDocument.Get());
195 m_AnnotList.push_back(
196 pdfium::MakeUnique<CPDF_Annot>(pDict, m_pDocument.Get()));
197 if (bRegenerateAP && subtype == "Widget" &&
198 CPDF_InteractiveForm::IsUpdateAPEnabled() &&
199 !pDict->GetDictFor(pdfium::annotation::kAP)) {
200 GenerateAP(m_pDocument.Get(), pDict);
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.Get(), pPage, m_AnnotList[i].get());
208 if (pPopupAnnot)
209 m_AnnotList.push_back(std::move(pPopupAnnot));
210 }
211 }
212
~CPDF_AnnotList()213 CPDF_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
DisplayPass(CPDF_Page * pPage,CFX_RenderDevice * pDevice,CPDF_RenderContext * pContext,bool bPrinting,const CFX_Matrix * pMatrix,bool bWidgetPass,CPDF_RenderOptions * pOptions,FX_RECT * clip_rect)224 void CPDF_AnnotList::DisplayPass(CPDF_Page* pPage,
225 CFX_RenderDevice* pDevice,
226 CPDF_RenderContext* pContext,
227 bool bPrinting,
228 const CFX_Matrix* pMatrix,
229 bool bWidgetPass,
230 CPDF_RenderOptions* pOptions,
231 FX_RECT* clip_rect) {
232 for (const auto& pAnnot : m_AnnotList) {
233 bool bWidget = pAnnot->GetSubtype() == CPDF_Annot::Subtype::WIDGET;
234 if ((bWidgetPass && !bWidget) || (!bWidgetPass && bWidget))
235 continue;
236
237 uint32_t annot_flags = pAnnot->GetFlags();
238 if (annot_flags & pdfium::annotation_flags::kHidden)
239 continue;
240
241 if (bPrinting && (annot_flags & pdfium::annotation_flags::kPrint) == 0)
242 continue;
243
244 if (!bPrinting && (annot_flags & pdfium::annotation_flags::kNoView))
245 continue;
246
247 if (pOptions) {
248 const CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
249 const CPDF_OCContext* pOCContext = pOptions->GetOCContext();
250 if (pAnnotDict && pOCContext &&
251 !pOCContext->CheckOCGVisible(
252 pAnnotDict->GetDictFor(pdfium::annotation::kOC))) {
253 continue;
254 }
255 }
256
257 CFX_Matrix matrix = *pMatrix;
258 if (clip_rect) {
259 FX_RECT annot_rect =
260 matrix.TransformRect(pAnnot->GetRect()).GetOuterRect();
261 annot_rect.Intersect(*clip_rect);
262 if (annot_rect.IsEmpty())
263 continue;
264 }
265 if (pContext) {
266 pAnnot->DrawInContext(pPage, pContext, &matrix, CPDF_Annot::Normal);
267 } else if (!pAnnot->DrawAppearance(pPage, pDevice, matrix,
268 CPDF_Annot::Normal, pOptions)) {
269 pAnnot->DrawBorder(pDevice, &matrix, pOptions);
270 }
271 }
272 }
273
DisplayAnnots(CPDF_Page * pPage,CFX_RenderDevice * pDevice,CPDF_RenderContext * pContext,bool bPrinting,const CFX_Matrix * pUser2Device,uint32_t dwAnnotFlags,CPDF_RenderOptions * pOptions,FX_RECT * pClipRect)274 void CPDF_AnnotList::DisplayAnnots(CPDF_Page* pPage,
275 CFX_RenderDevice* pDevice,
276 CPDF_RenderContext* pContext,
277 bool bPrinting,
278 const CFX_Matrix* pUser2Device,
279 uint32_t dwAnnotFlags,
280 CPDF_RenderOptions* pOptions,
281 FX_RECT* pClipRect) {
282 if (dwAnnotFlags & pdfium::annotation_flags::kInvisible) {
283 DisplayPass(pPage, pDevice, pContext, bPrinting, pUser2Device, false,
284 pOptions, pClipRect);
285 }
286 if (dwAnnotFlags & pdfium::annotation_flags::kHidden) {
287 DisplayPass(pPage, pDevice, pContext, bPrinting, pUser2Device, true,
288 pOptions, pClipRect);
289 }
290 }
291
DisplayAnnots(CPDF_Page * pPage,CPDF_RenderContext * pContext,bool bPrinting,const CFX_Matrix * pMatrix,bool bShowWidget,CPDF_RenderOptions * pOptions)292 void CPDF_AnnotList::DisplayAnnots(CPDF_Page* pPage,
293 CPDF_RenderContext* pContext,
294 bool bPrinting,
295 const CFX_Matrix* pMatrix,
296 bool bShowWidget,
297 CPDF_RenderOptions* pOptions) {
298 uint32_t dwAnnotFlags = bShowWidget ? pdfium::annotation_flags::kInvisible |
299 pdfium::annotation_flags::kHidden
300 : pdfium::annotation_flags::kInvisible;
301 DisplayAnnots(pPage, nullptr, pContext, bPrinting, pMatrix, dwAnnotFlags,
302 pOptions, nullptr);
303 }
304