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_annot.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_annot.h"
8
9#include <algorithm>
10#include <utility>
11
12#include "constants/annotation_common.h"
13#include "constants/annotation_flags.h"
14#include "core/fpdfapi/page/cpdf_form.h"
15#include "core/fpdfapi/page/cpdf_page.h"
16#include "core/fpdfapi/page/cpdf_pageimagecache.h"
17#include "core/fpdfapi/parser/cpdf_array.h"
18#include "core/fpdfapi/parser/cpdf_boolean.h"
19#include "core/fpdfapi/parser/cpdf_dictionary.h"
20#include "core/fpdfapi/parser/cpdf_document.h"
21#include "core/fpdfapi/parser/cpdf_stream.h"
22#include "core/fpdfapi/parser/fpdf_parser_utility.h"
23#include "core/fpdfapi/render/cpdf_rendercontext.h"
24#include "core/fpdfapi/render/cpdf_renderoptions.h"
25#include "core/fpdfdoc/cpdf_generateap.h"
26#include "core/fxge/cfx_fillrenderoptions.h"
27#include "core/fxge/cfx_graphstatedata.h"
28#include "core/fxge/cfx_path.h"
29#include "core/fxge/cfx_renderdevice.h"
30#include "third_party/base/check.h"
31
32namespace {
33
34const char kPDFiumKey_HasGeneratedAP[] = "PDFIUM_HasGeneratedAP";
35
36bool IsTextMarkupAnnotation(CPDF_Annot::Subtype type) {
37 return type == CPDF_Annot::Subtype::HIGHLIGHT ||
41}
42
43CPDF_Form* AnnotGetMatrix(CPDF_Page* pPage,
44 CPDF_Annot* pAnnot,
46 const CFX_Matrix& mtUser2Device,
47 CFX_Matrix* matrix) {
48 CPDF_Form* pForm = pAnnot->GetAPForm(pPage, mode);
49 if (!pForm)
50 return nullptr;
51
52 CFX_Matrix form_matrix = pForm->GetDict()->GetMatrixFor("Matrix");
53 CFX_FloatRect form_bbox =
54 form_matrix.TransformRect(pForm->GetDict()->GetRectFor("BBox"));
55 matrix->MatchRect(pAnnot->GetRect(), form_bbox);
56
57 // Compensate for page rotation.
58 if ((pAnnot->GetFlags() & pdfium::annotation_flags::kNoRotate) &&
59 pPage->GetPageRotation() != 0) {
60 // Rotate annotation rect around top-left angle (according to the
61 // specification).
62 const float offset_x = pAnnot->GetRect().Left();
63 const float offset_y = pAnnot->GetRect().Top();
64 matrix->Concat({1, 0, 0, 1, -offset_x, -offset_y});
65 // GetPageRotation returns value in fractions of pi/2.
66 const float angle = FXSYS_PI / 2 * pPage->GetPageRotation();
67 matrix->Rotate(angle);
68 matrix->Concat({1, 0, 0, 1, offset_x, offset_y});
69 }
70
71 matrix->Concat(mtUser2Device);
72 return pForm;
73}
74
75RetainPtr<CPDF_Stream> GetAnnotAPInternal(CPDF_Dictionary* pAnnotDict,
77 bool bFallbackToNormal) {
78 RetainPtr<CPDF_Dictionary> pAP =
79 pAnnotDict->GetMutableDictFor(pdfium::annotation::kAP);
80 if (!pAP)
81 return nullptr;
82
83 const char* ap_entry = "N";
85 ap_entry = "D";
87 ap_entry = "R";
88 if (bFallbackToNormal && !pAP->KeyExist(ap_entry))
89 ap_entry = "N";
90
91 RetainPtr<CPDF_Object> psub = pAP->GetMutableDirectObjectFor(ap_entry);
92 if (!psub)
93 return nullptr;
94
95 RetainPtr<CPDF_Stream> pStream(psub->AsMutableStream());
96 if (pStream)
97 return pStream;
98
99 CPDF_Dictionary* pDict = psub->AsMutableDictionary();
100 if (!pDict)
101 return nullptr;
102
103 ByteString as = pAnnotDict->GetByteStringFor(pdfium::annotation::kAS);
104 if (as.IsEmpty()) {
105 ByteString value = pAnnotDict->GetByteStringFor("V");
106 if (value.IsEmpty()) {
107 RetainPtr<const CPDF_Dictionary> pParentDict =
108 pAnnotDict->GetDictFor("Parent");
109 value = pParentDict ? pParentDict->GetByteStringFor("V") : ByteString();
110 }
111 as = (!value.IsEmpty() && pDict->KeyExist(value)) ? value : "Off";
112 }
113 return pDict->GetMutableStreamFor(as);
114}
115
116} // namespace
117
118CPDF_Annot::CPDF_Annot(RetainPtr<CPDF_Dictionary> pDict,
119 CPDF_Document* pDocument)
124 m_bIsTextMarkupAnnotation(IsTextMarkupAnnotation(m_nSubtype)),
127 GenerateAPIfNeeded();
128}
129
133
134void CPDF_Annot::GenerateAPIfNeeded() {
135 if (!ShouldGenerateAP())
136 return;
137 if (!CPDF_GenerateAP::GenerateAnnotAP(m_pDocument, m_pAnnotDict.Get(),
138 m_nSubtype)) {
139 return;
140 }
141
142 m_pAnnotDict->SetNewFor<CPDF_Boolean>(kPDFiumKey_HasGeneratedAP, true);
143 m_bHasGeneratedAP = true;
144}
145
146bool CPDF_Annot::ShouldGenerateAP() const {
147 // If AP dictionary exists and defines an appearance for normal mode, we use
148 // the appearance defined in the existing AP dictionary.
149 RetainPtr<const CPDF_Dictionary> pAP =
150 m_pAnnotDict->GetDictFor(pdfium::annotation::kAP);
151 if (pAP && pAP->GetDictFor("N"))
152 return false;
153
154 return !IsHidden();
155}
156
157bool CPDF_Annot::ShouldDrawAnnotation() const {
158 if (IsHidden())
159 return false;
160 return m_bOpenState || m_nSubtype != CPDF_Annot::Subtype::POPUP;
161}
162
164 m_APMap.clear();
165}
166
168 return m_nSubtype;
169}
170
171CFX_FloatRect CPDF_Annot::RectForDrawing() const {
172 bool bShouldUseQuadPointsCoords =
173 m_bIsTextMarkupAnnotation && m_bHasGeneratedAP;
174 if (bShouldUseQuadPointsCoords)
175 return BoundingRectFromQuadPoints(m_pAnnotDict.Get());
176 return m_pAnnotDict->GetRectFor(pdfium::annotation::kRect);
177}
178
180 CFX_FloatRect rect = RectForDrawing();
181 rect.Normalize();
182 return rect;
183}
184
185uint32_t CPDF_Annot::GetFlags() const {
186 return m_pAnnotDict->GetIntegerFor(pdfium::annotation::kF);
187}
188
189bool CPDF_Annot::IsHidden() const {
190 return !!(GetFlags() & pdfium::annotation_flags::kHidden);
191}
192
193RetainPtr<CPDF_Stream> GetAnnotAP(CPDF_Dictionary* pAnnotDict,
194 CPDF_Annot::AppearanceMode eMode) {
195 DCHECK(pAnnotDict);
196 return GetAnnotAPInternal(pAnnotDict, eMode, true);
197}
198
199RetainPtr<CPDF_Stream> GetAnnotAPNoFallback(CPDF_Dictionary* pAnnotDict,
200 CPDF_Annot::AppearanceMode eMode) {
201 DCHECK(pAnnotDict);
202 return GetAnnotAPInternal(pAnnotDict, eMode, false);
203}
204
205CPDF_Form* CPDF_Annot::GetAPForm(CPDF_Page* pPage, AppearanceMode mode) {
206 RetainPtr<CPDF_Stream> pStream = GetAnnotAP(m_pAnnotDict.Get(), mode);
207 if (!pStream)
208 return nullptr;
209
210 auto it = m_APMap.find(pStream);
211 if (it != m_APMap.end())
212 return it->second.get();
213
214 auto pNewForm = std::make_unique<CPDF_Form>(
215 m_pDocument, pPage->GetMutableResources(), pStream);
216 pNewForm->ParseContent();
217
218 CPDF_Form* pResult = pNewForm.get();
219 m_APMap[pStream] = std::move(pNewForm);
220 return pResult;
221}
222
223void CPDF_Annot::SetPopupAnnotOpenState(bool bOpenState) {
224 if (m_pPopupAnnot)
225 m_pPopupAnnot->SetOpenState(bOpenState);
226}
227
229 if (!m_pPopupAnnot)
230 return absl::nullopt;
231 return m_pPopupAnnot->GetRect();
232}
233
234// static
235CFX_FloatRect CPDF_Annot::RectFromQuadPointsArray(const CPDF_Array* pArray,
236 size_t nIndex) {
237 DCHECK(pArray);
238 DCHECK(nIndex < pArray->size() / 8);
239
240 // QuadPoints are defined with 4 pairs of numbers
241 // ([ pair0, pair1, pair2, pair3 ]), where
242 // pair0 = top_left
243 // pair1 = top_right
244 // pair2 = bottom_left
245 // pair3 = bottom_right
246 //
247 // On the other hand, /Rect is defined as 2 pairs [pair0, pair1] where:
248 // pair0 = bottom_left
249 // pair1 = top_right.
250
251 return CFX_FloatRect(
252 pArray->GetFloatAt(4 + nIndex * 8), pArray->GetFloatAt(5 + nIndex * 8),
253 pArray->GetFloatAt(2 + nIndex * 8), pArray->GetFloatAt(3 + nIndex * 8));
254}
255
256// static
258 const CPDF_Dictionary* pAnnotDict) {
259 CFX_FloatRect ret;
260 RetainPtr<const CPDF_Array> pArray = pAnnotDict->GetArrayFor("QuadPoints");
261 size_t nQuadPointCount = pArray ? QuadPointCount(pArray.Get()) : 0;
262 if (nQuadPointCount == 0)
263 return ret;
264
265 ret = RectFromQuadPointsArray(pArray.Get(), 0);
266 for (size_t i = 1; i < nQuadPointCount; ++i) {
267 CFX_FloatRect rect = RectFromQuadPointsArray(pArray.Get(), i);
268 ret.Union(rect);
269 }
270 return ret;
271}
272
273// static
274CFX_FloatRect CPDF_Annot::RectFromQuadPoints(const CPDF_Dictionary* pAnnotDict,
275 size_t nIndex) {
276 RetainPtr<const CPDF_Array> pArray = pAnnotDict->GetArrayFor("QuadPoints");
277 size_t nQuadPointCount = pArray ? QuadPointCount(pArray.Get()) : 0;
278 if (nIndex >= nQuadPointCount)
279 return CFX_FloatRect();
280 return RectFromQuadPointsArray(pArray.Get(), nIndex);
281}
282
283// static
285 const ByteString& sSubtype) {
286 if (sSubtype == "Text")
288 if (sSubtype == "Link")
290 if (sSubtype == "FreeText")
292 if (sSubtype == "Line")
294 if (sSubtype == "Square")
296 if (sSubtype == "Circle")
298 if (sSubtype == "Polygon")
300 if (sSubtype == "PolyLine")
302 if (sSubtype == "Highlight")
304 if (sSubtype == "Underline")
306 if (sSubtype == "Squiggly")
308 if (sSubtype == "StrikeOut")
310 if (sSubtype == "Stamp")
312 if (sSubtype == "Caret")
314 if (sSubtype == "Ink")
316 if (sSubtype == "Popup")
318 if (sSubtype == "FileAttachment")
320 if (sSubtype == "Sound")
322 if (sSubtype == "Movie")
324 if (sSubtype == "Widget")
326 if (sSubtype == "Screen")
328 if (sSubtype == "PrinterMark")
330 if (sSubtype == "TrapNet")
332 if (sSubtype == "Watermark")
334 if (sSubtype == "3D")
336 if (sSubtype == "RichMedia")
338 if (sSubtype == "XFAWidget")
340 if (sSubtype == "Redact")
343}
344
345// static
347 if (nSubtype == CPDF_Annot::Subtype::TEXT)
348 return "Text";
349 if (nSubtype == CPDF_Annot::Subtype::LINK)
350 return "Link";
351 if (nSubtype == CPDF_Annot::Subtype::FREETEXT)
352 return "FreeText";
353 if (nSubtype == CPDF_Annot::Subtype::LINE)
354 return "Line";
355 if (nSubtype == CPDF_Annot::Subtype::SQUARE)
356 return "Square";
357 if (nSubtype == CPDF_Annot::Subtype::CIRCLE)
358 return "Circle";
359 if (nSubtype == CPDF_Annot::Subtype::POLYGON)
360 return "Polygon";
361 if (nSubtype == CPDF_Annot::Subtype::POLYLINE)
362 return "PolyLine";
363 if (nSubtype == CPDF_Annot::Subtype::HIGHLIGHT)
364 return "Highlight";
365 if (nSubtype == CPDF_Annot::Subtype::UNDERLINE)
366 return "Underline";
367 if (nSubtype == CPDF_Annot::Subtype::SQUIGGLY)
368 return "Squiggly";
369 if (nSubtype == CPDF_Annot::Subtype::STRIKEOUT)
370 return "StrikeOut";
371 if (nSubtype == CPDF_Annot::Subtype::STAMP)
372 return "Stamp";
373 if (nSubtype == CPDF_Annot::Subtype::CARET)
374 return "Caret";
375 if (nSubtype == CPDF_Annot::Subtype::INK)
376 return "Ink";
377 if (nSubtype == CPDF_Annot::Subtype::POPUP)
378 return "Popup";
380 return "FileAttachment";
381 if (nSubtype == CPDF_Annot::Subtype::SOUND)
382 return "Sound";
383 if (nSubtype == CPDF_Annot::Subtype::MOVIE)
384 return "Movie";
385 if (nSubtype == CPDF_Annot::Subtype::WIDGET)
386 return "Widget";
387 if (nSubtype == CPDF_Annot::Subtype::SCREEN)
388 return "Screen";
389 if (nSubtype == CPDF_Annot::Subtype::PRINTERMARK)
390 return "PrinterMark";
391 if (nSubtype == CPDF_Annot::Subtype::TRAPNET)
392 return "TrapNet";
393 if (nSubtype == CPDF_Annot::Subtype::WATERMARK)
394 return "Watermark";
395 if (nSubtype == CPDF_Annot::Subtype::THREED)
396 return "3D";
397 if (nSubtype == CPDF_Annot::Subtype::RICHMEDIA)
398 return "RichMedia";
399 if (nSubtype == CPDF_Annot::Subtype::XFAWIDGET)
400 return "XFAWidget";
401 if (nSubtype == CPDF_Annot::Subtype::REDACT)
402 return "Redact";
403 return ByteString();
404}
405
406// static
407size_t CPDF_Annot::QuadPointCount(const CPDF_Array* pArray) {
408 return pArray->size() / 8;
409}
410
411bool CPDF_Annot::DrawAppearance(CPDF_Page* pPage,
412 CFX_RenderDevice* pDevice,
413 const CFX_Matrix& mtUser2Device,
414 AppearanceMode mode) {
415 if (!ShouldDrawAnnotation())
416 return false;
417
418 // It might happen that by the time this annotation instance was created,
419 // it was flagged as "hidden" (e.g. /F 2), and hence CPDF_GenerateAP decided
420 // to not "generate" its AP.
421 // If for a reason the object is no longer hidden, but still does not have
422 // its "AP" generated, generate it now.
423 GenerateAPIfNeeded();
424
425 CFX_Matrix matrix;
426 CPDF_Form* pForm = AnnotGetMatrix(pPage, this, mode, mtUser2Device, &matrix);
427 if (!pForm)
428 return false;
429
431 pPage->GetMutablePageResources(),
433 context.AppendLayer(pForm, matrix);
434 context.Render(pDevice, nullptr, nullptr, nullptr);
435 return true;
436}
437
438bool CPDF_Annot::DrawInContext(CPDF_Page* pPage,
439 CPDF_RenderContext* pContext,
440 const CFX_Matrix& mtUser2Device,
441 AppearanceMode mode) {
442 if (!ShouldDrawAnnotation())
443 return false;
444
445 // It might happen that by the time this annotation instance was created,
446 // it was flagged as "hidden" (e.g. /F 2), and hence CPDF_GenerateAP decided
447 // to not "generate" its AP.
448 // If for a reason the object is no longer hidden, but still does not have
449 // its "AP" generated, generate it now.
450 GenerateAPIfNeeded();
451
452 CFX_Matrix matrix;
453 CPDF_Form* pForm = AnnotGetMatrix(pPage, this, mode, mtUser2Device, &matrix);
454 if (!pForm)
455 return false;
456
457 pContext->AppendLayer(pForm, matrix);
458 return true;
459}
460
462 const CFX_Matrix* pUser2Device) {
464 return;
465
466 uint32_t annot_flags = GetFlags();
467 if (annot_flags & pdfium::annotation_flags::kHidden)
468 return;
469
470 bool bPrinting = pDevice->GetDeviceType() == DeviceType::kPrinter;
471 if (bPrinting && (annot_flags & pdfium::annotation_flags::kPrint) == 0) {
472 return;
473 }
474 if (!bPrinting && (annot_flags & pdfium::annotation_flags::kNoView)) {
475 return;
476 }
477 RetainPtr<const CPDF_Dictionary> pBS = m_pAnnotDict->GetDictFor("BS");
478 char style_char;
479 float width;
480 RetainPtr<const CPDF_Array> pDashArray;
481 if (!pBS) {
482 RetainPtr<const CPDF_Array> pBorderArray =
483 m_pAnnotDict->GetArrayFor(pdfium::annotation::kBorder);
484 style_char = 'S';
485 if (pBorderArray) {
486 width = pBorderArray->GetFloatAt(2);
487 if (pBorderArray->size() == 4) {
488 pDashArray = pBorderArray->GetArrayAt(3);
489 if (!pDashArray) {
490 return;
491 }
492 size_t nLen = pDashArray->size();
493 size_t i = 0;
494 for (; i < nLen; ++i) {
495 RetainPtr<const CPDF_Object> pObj = pDashArray->GetDirectObjectAt(i);
496 if (pObj && pObj->GetInteger()) {
497 break;
498 }
499 }
500 if (i == nLen) {
501 return;
502 }
503 style_char = 'D';
504 }
505 } else {
506 width = 1;
507 }
508 } else {
509 ByteString style = pBS->GetByteStringFor("S");
510 pDashArray = pBS->GetArrayFor("D");
511 style_char = style[0];
512 width = pBS->GetFloatFor("W");
513 }
514 if (width <= 0) {
515 return;
516 }
517 RetainPtr<const CPDF_Array> pColor =
518 m_pAnnotDict->GetArrayFor(pdfium::annotation::kC);
519 uint32_t argb = 0xff000000;
520 if (pColor) {
521 int R = static_cast<int32_t>(pColor->GetFloatAt(0) * 255);
522 int G = static_cast<int32_t>(pColor->GetFloatAt(1) * 255);
523 int B = static_cast<int32_t>(pColor->GetFloatAt(2) * 255);
524 argb = ArgbEncode(0xff, R, G, B);
525 }
526 CFX_GraphStateData graph_state;
527 graph_state.m_LineWidth = width;
528 if (style_char == 'U') {
529 // TODO(https://crbug.com/237527): Handle the "Underline" border style
530 // instead of drawing the rectangle border.
531 return;
532 }
533
534 if (style_char == 'D') {
535 if (pDashArray) {
536 graph_state.m_DashArray =
537 ReadArrayElementsToVector(pDashArray.Get(), pDashArray->size());
538 if (graph_state.m_DashArray.size() % 2)
539 graph_state.m_DashArray.push_back(graph_state.m_DashArray.back());
540 } else {
541 graph_state.m_DashArray = {3.0f, 3.0f};
542 }
543 }
544
546 rect.Deflate(width / 2, width / 2);
547
548 CFX_Path path;
549 path.AppendFloatRect(rect);
550 pDevice->DrawPath(path, pUser2Device, &graph_state, argb, argb,
552}
float Left() const
constexpr CFX_FloatRect()=default
float Top() const
void Deflate(float x, float y)
void Union(const CFX_FloatRect &other_rect)
CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
void MatchRect(const CFX_FloatRect &dest, const CFX_FloatRect &src)
void Rotate(float fRadian)
void Concat(const CFX_Matrix &right)
void AppendFloatRect(const CFX_FloatRect &rect)
Definition cfx_path.cpp:305
bool DrawPath(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState, uint32_t fill_color, uint32_t stroke_color, const CFX_FillRenderOptions &fill_options)
DeviceType GetDeviceType() const
uint32_t GetFlags() const
CPDF_Form * GetAPForm(CPDF_Page *pPage, AppearanceMode mode)
bool DrawInContext(CPDF_Page *pPage, CPDF_RenderContext *pContext, const CFX_Matrix &mtUser2Device, AppearanceMode mode)
static ByteString AnnotSubtypeToString(Subtype nSubtype)
absl::optional< CFX_FloatRect > GetPopupAnnotRect() const
Subtype GetSubtype() const
void ClearCachedAP()
static Subtype StringToAnnotSubtype(const ByteString &sSubtype)
CFX_FloatRect GetRect() const
CPDF_Annot(RetainPtr< CPDF_Dictionary > pDict, CPDF_Document *pDocument)
bool IsHidden() const
bool DrawAppearance(CPDF_Page *pPage, CFX_RenderDevice *pDevice, const CFX_Matrix &mtUser2Device, AppearanceMode mode)
void DrawBorder(CFX_RenderDevice *pDevice, const CFX_Matrix *pUser2Device)
static CFX_FloatRect BoundingRectFromQuadPoints(const CPDF_Dictionary *pAnnotDict)
void SetPopupAnnotOpenState(bool bOpenState)
bool KeyExist(const ByteString &key) const
RetainPtr< CPDF_Stream > GetMutableStreamFor(const ByteString &key)
ByteString GetByteStringFor(const ByteString &key) const
CPDF_Document * GetDocument() const override
Definition cpdf_page.cpp:51
int GetPageRotation() const
CPDF_PageImageCache * GetPageImageCache()
Definition cpdf_page.h:83
void AppendLayer(CPDF_PageObjectHolder *pObjectHolder, const CFX_Matrix &mtObject2Device)
void Render(CFX_RenderDevice *pDevice, const CPDF_PageObject *pStopObj, const CPDF_RenderOptions *pOptions, const CFX_Matrix *pLastMatrix)
bool operator==(const char *ptr) const
ByteString & operator=(ByteString &&that) noexcept
bool IsEmpty() const
Definition bytestring.h:119
CharType operator[](const size_t index) const
Definition bytestring.h:150
RetainPtr< CPDF_Stream > GetAnnotAPNoFallback(CPDF_Dictionary *pAnnotDict, CPDF_Annot::AppearanceMode eMode)
RetainPtr< CPDF_Stream > GetAnnotAP(CPDF_Dictionary *pAnnotDict, CPDF_Annot::AppearanceMode eMode)
constexpr FX_ARGB ArgbEncode(uint32_t a, uint32_t r, uint32_t g, uint32_t b)
Definition fx_dib.h:118
#define FXSYS_PI
Definition fx_system.h:43
constexpr uint32_t kNoView
constexpr uint32_t kHidden
constexpr uint32_t kNoRotate
constexpr uint32_t kPrint