Debian Codegraph @

qt5/src/poppler-annotation.cc

raw
1 /* poppler-annotation.cc: qt interface to poppler
2 * Copyright (C) 2006, 2009, 2012-2015, 2018-2022, 2024, 2025 Albert Astals Cid <[email protected]>
3 * Copyright (C) 2006, 2008, 2010 Pino Toscano <[email protected]>
4 * Copyright (C) 2012, Guillermo A. Amaral B. <[email protected]>
5 * Copyright (C) 2012-2014 Fabio D'Urso <[email protected]>
6 * Copyright (C) 2012, 2015, Tobias Koenig <[email protected]>
7 * Copyright (C) 2018 Adam Reichold <[email protected]>
8 * Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by the LiMux project of the city of Munich
9 * Copyright (C) 2018 Intevation GmbH <[email protected]>
10 * Copyright (C) 2018 Dileep Sankhla <[email protected]>
11 * Copyright (C) 2018, 2019 Tobias Deiminger <[email protected]>
12 * Copyright (C) 2018 Carlos Garcia Campos <[email protected]>
13 * Copyright (C) 2020, 2022 Oliver Sander <[email protected]>
14 * Copyright (C) 2020 Katarina Behrens <[email protected]>
15 * Copyright (C) 2020 Thorsten Behrens <[email protected]>
16 * Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by Technische Universität Dresden
17 * Copyright (C) 2021 Mahmoud Ahmed Khalil <[email protected]>
18 * Copyright (C) 2024 Pratham Gandhi <[email protected]>
19 * Copyright (C) 2024, 2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]>
20 * Adapting code from
21 * Copyright (C) 2004 by Enrico Ros <[email protected]>
22 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2, or (at your option)
26 * any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
36 */
37
38 // qt/kde includes
39 #include <QtCore/QRegExp>
40 #include <QtCore/QtAlgorithms>
41 #include <QtXml/QDomElement>
42 #include <QtGui/QColor>
43 #include <QtGui/QTransform>
44 #include <QImage>
45
46 // local includes
47 #include "poppler-annotation.h"
48 #include "poppler-link.h"
49 #include "poppler-qt5.h"
50 #include "poppler-annotation-helper.h"
51 #include "poppler-annotation-private.h"
52 #include "poppler-page-private.h"
53 #include "poppler-private.h"
54
55 // poppler includes
56 #include <Page.h>
57 #include <Annot.h>
58 #include <Gfx.h>
59 #include <Error.h>
60 #include <FileSpec.h>
61 #include <Link.h>
62 #include <DateInfo.h>
63
64 /* Almost all getters directly query the underlying poppler annotation, with
65 * the exceptions of link, file attachment, sound, movie and screen annotations,
66 * Whose data retrieval logic has not been moved yet. Their getters return
67 * static data set at creation time by findAnnotations
68 */
69
70 template<typename T, typename U>
71 static std::unique_ptr<T> static_pointer_cast(std::unique_ptr<U> &&in)
72 {
73 return std::unique_ptr<T>(static_cast<std::add_pointer_t<T>>(in.release()));
74 }
75
76 namespace Poppler {
77
78 // BEGIN AnnotationUtils implementation
79 Annotation *AnnotationUtils::createAnnotation(const QDomElement &annElement)
80 {
81 // safety check on annotation element
82 if (!annElement.hasAttribute(QStringLiteral("type"))) {
83 return nullptr;
84 }
85
86 // build annotation of given type
87 Annotation *annotation = nullptr;
88 int typeNumber = annElement.attribute(QStringLiteral("type")).toInt();
89 switch (typeNumber) {
90 case Annotation::AText:
91 annotation = new TextAnnotation(annElement);
92 break;
93 case Annotation::ALine:
94 annotation = new LineAnnotation(annElement);
95 break;
96 case Annotation::AGeom:
97 annotation = new GeomAnnotation(annElement);
98 break;
99 case Annotation::AHighlight:
100 annotation = new HighlightAnnotation(annElement);
101 break;
102 case Annotation::AStamp:
103 annotation = new StampAnnotation(annElement);
104 break;
105 case Annotation::AInk:
106 annotation = new InkAnnotation(annElement);
107 break;
108 case Annotation::ACaret:
109 annotation = new CaretAnnotation(annElement);
110 break;
111 }
112
113 // return created annotation
114 return annotation;
115 }
116
117 void AnnotationUtils::storeAnnotation(const Annotation *ann, QDomElement &annElement, QDomDocument &document)
118 {
119 // save annotation's type as element's attribute
120 annElement.setAttribute(QStringLiteral("type"), (uint)ann->subType());
121
122 // append all annotation data as children of this node
123 ann->store(annElement, document);
124 }
125
126 QDomElement AnnotationUtils::findChildElement(const QDomNode &parentNode, const QString &name)
127 {
128 // loop through the whole children and return a 'name' named element
129 QDomNode subNode = parentNode.firstChild();
130 while (subNode.isElement()) {
131 QDomElement element = subNode.toElement();
132 if (element.tagName() == name) {
133 return element;
134 }
135 subNode = subNode.nextSibling();
136 }
137 // if the name can't be found, return a dummy null element
138 return QDomElement();
139 }
140 // END AnnotationUtils implementation
141
142 // BEGIN AnnotationAppearancePrivate implementation
143 AnnotationAppearancePrivate::AnnotationAppearancePrivate(Annot *annot)
144 {
145 if (annot) {
146 appearance = annot->getAppearance();
147 } else {
148 appearance.setToNull();
149 }
150 }
151 // END AnnotationAppearancePrivate implementation
152
153 // BEGIN AnnotationAppearance implementation
154 AnnotationAppearance::AnnotationAppearance(AnnotationAppearancePrivate *annotationAppearancePrivate) : d(annotationAppearancePrivate) { }
155
156 AnnotationAppearance::~AnnotationAppearance()
157 {
158 delete d;
159 }
160 // END AnnotationAppearance implementation
161
162 // BEGIN Annotation implementation
163 AnnotationPrivate::AnnotationPrivate() : flags(0), revisionScope(Annotation::Root), revisionType(Annotation::None), pdfAnnot(nullptr), pdfPage(nullptr), parentDoc(nullptr) { }
164
165 void getRawDataFromQImage(const QImage &qimg, int bitsPerPixel, QByteArray *data, QByteArray *sMaskData)
166 {
167 const int height = qimg.height();
168 const int width = qimg.width();
169
170 switch (bitsPerPixel) {
171 case 1:
172 for (int line = 0; line < height; line++) {
173 const char *lineData = reinterpret_cast<const char *>(qimg.scanLine(line));
174 for (int offset = 0; offset < (width + 7) / 8; offset++) {
175 data->append(lineData[offset]);
176 }
177 }
178 break;
179 case 8:
180 case 24:
181 data->append((const char *)qimg.bits(), static_cast<int>(qimg.sizeInBytes()));
182 break;
183 case 32:
184 for (int line = 0; line < height; line++) {
185 const QRgb *lineData = reinterpret_cast<const QRgb *>(qimg.scanLine(line));
186 for (int offset = 0; offset < width; offset++) {
187 char a = (char)qAlpha(lineData[offset]);
188 char r = (char)qRed(lineData[offset]);
189 char g = (char)qGreen(lineData[offset]);
190 char b = (char)qBlue(lineData[offset]);
191
192 data->append(r);
193 data->append(g);
194 data->append(b);
195
196 sMaskData->append(a);
197 }
198 }
199 break;
200 }
201 }
202
203 void AnnotationPrivate::addRevision(Annotation *ann, Annotation::RevScope scope, Annotation::RevType type)
204 {
205 /* Since ownership stays with the caller, create an alias of ann */
206 revisions.push_back(ann->d_ptr->makeAlias());
207
208 /* Set revision properties */
209 revisionScope = scope;
210 revisionType = type;
211 }
212
213 AnnotationPrivate::~AnnotationPrivate() = default;
214
215 void AnnotationPrivate::tieToNativeAnnot(std::shared_ptr<Annot> ann, ::Page *page, Poppler::DocumentData *doc)
216 {
217 if (pdfAnnot) {
218 error(errIO, -1, "Annotation is already tied");
219 return;
220 }
221
222 pdfAnnot = std::move(ann);
223 pdfPage = page;
224 parentDoc = doc;
225 }
226
227 /* This method is called when a new annotation is created, after pdfAnnot and
228 * pdfPage have been set */
229 void AnnotationPrivate::flushBaseAnnotationProperties()
230 {
231 Q_ASSERT(pdfPage);
232
233 std::unique_ptr<Annotation> q = makeAlias(); // Setters are defined in the public class
234
235 // Since pdfAnnot has been set, this calls will write in the Annot object
236 q->setAuthor(author);
237 q->setContents(contents);
238 q->setUniqueName(uniqueName);
239 q->setModificationDate(modDate);
240 q->setCreationDate(creationDate);
241 q->setFlags(flags);
242 // q->setBoundary(boundary); -- already set by subclass-specific code
243 q->setStyle(style);
244 q->setPopup(popup);
245
246 // Flush revisions
247 revisions.clear();
248
249 // Clear some members to save memory
250 author.clear();
251 contents.clear();
252 uniqueName.clear();
253 revisions.clear();
254 }
255
256 // Returns matrix to convert from user space coords (oriented according to the
257 // specified rotation) to normalized coords
258 static void fillNormalizationMTX(::Page *pdfPage, double MTX[6], int pageRotation)
259 {
260 Q_ASSERT(pdfPage);
261
262 // build a normalized transform matrix for this page at 100% scale
263 GfxState *gfxState = new GfxState(72.0, 72.0, pdfPage->getCropBox(), pageRotation, true);
264 const double *gfxCTM = gfxState->getCTM();
265
266 double w = pdfPage->getCropWidth();
267 double h = pdfPage->getCropHeight();
268
269 // Swap width and height if the page is rotated landscape or seascape
270 if (pageRotation == 90 || pageRotation == 270) {
271 double t = w;
272 w = h;
273 h = t;
274 }
275
276 for (int i = 0; i < 6; i += 2) {
277 MTX[i] = gfxCTM[i] / w;
278 MTX[i + 1] = gfxCTM[i + 1] / h;
279 }
280 delete gfxState;
281 }
282
283 // Returns matrix to convert from user space coords (i.e. those that are stored
284 // in the PDF file) to normalized coords (i.e. those that we expose to clients).
285 // This method also applies a rotation around the top-left corner if the
286 // FixedRotation flag is set.
287 void AnnotationPrivate::fillTransformationMTX(double MTX[6]) const
288 {
289 Q_ASSERT(pdfPage);
290 Q_ASSERT(pdfAnnot);
291
292 const int pageRotate = pdfPage->getRotate();
293
294 if (pageRotate == 0 || (pdfAnnot->getFlags() & Annot::flagNoRotate) == 0) {
295 // Use the normalization matrix for this page's rotation
296 fillNormalizationMTX(pdfPage, MTX, pageRotate);
297 } else {
298 // Clients expect coordinates relative to this page's rotation, but
299 // FixedRotation annotations internally use unrotated coordinates:
300 // construct matrix to both normalize and rotate coordinates using the
301 // top-left corner as rotation pivot
302
303 double MTXnorm[6];
304 fillNormalizationMTX(pdfPage, MTXnorm, pageRotate);
305
306 QTransform transform(MTXnorm[0], MTXnorm[1], MTXnorm[2], MTXnorm[3], MTXnorm[4], MTXnorm[5]);
307 transform.translate(+pdfAnnot->getXMin(), +pdfAnnot->getYMax());
308 transform.rotate(pageRotate);
309 transform.translate(-pdfAnnot->getXMin(), -pdfAnnot->getYMax());
310
311 MTX[0] = transform.m11();
312 MTX[1] = transform.m12();
313 MTX[2] = transform.m21();
314 MTX[3] = transform.m22();
315 MTX[4] = transform.dx();
316 MTX[5] = transform.dy();
317 }
318 }
319
320 QRectF AnnotationPrivate::fromPdfRectangle(const PDFRectangle &r) const
321 {
322 double swp, MTX[6];
323 fillTransformationMTX(MTX);
324
325 QPointF p1, p2;
326 XPDFReader::transform(MTX, r.x1, r.y1, p1);
327 XPDFReader::transform(MTX, r.x2, r.y2, p2);
328
329 double tl_x = p1.x();
330 double tl_y = p1.y();
331 double br_x = p2.x();
332 double br_y = p2.y();
333
334 if (tl_x > br_x) {
335 swp = tl_x;
336 tl_x = br_x;
337 br_x = swp;
338 }
339
340 if (tl_y > br_y) {
341 swp = tl_y;
342 tl_y = br_y;
343 br_y = swp;
344 }
345
346 return QRectF(QPointF(tl_x, tl_y), QPointF(br_x, br_y));
347 }
348
349 // This function converts a boundary QRectF in normalized coords to a
350 // PDFRectangle in user coords. If the FixedRotation flag is set, this function
351 // also applies a rotation around the top-left corner: it's the inverse of
352 // the transformation produced by fillTransformationMTX, but we can't use
353 // fillTransformationMTX here because it relies on the native annotation
354 // object's boundary rect to be already set up.
355 PDFRectangle boundaryToPdfRectangle(::Page *pdfPage, const QRectF &r, int rFlags)
356 {
357 Q_ASSERT(pdfPage);
358
359 const double w = pdfPage->getCropWidth();
360 const double h = pdfPage->getCropHeight();
361
362 if (w == 0 || h == 0) {
363 // page is broken, there's nothing to transform
364 return {};
365 }
366
367 const int pageRotate = pdfPage->getRotate();
368
369 double MTX[6];
370 fillNormalizationMTX(pdfPage, MTX, pageRotate);
371
372 double tl_x, tl_y, br_x, br_y, swp;
373 XPDFReader::invTransform(MTX, r.topLeft(), tl_x, tl_y);
374 XPDFReader::invTransform(MTX, r.bottomRight(), br_x, br_y);
375
376 if (tl_x > br_x) {
377 swp = tl_x;
378 tl_x = br_x;
379 br_x = swp;
380 }
381
382 if (tl_y > br_y) {
383 swp = tl_y;
384 tl_y = br_y;
385 br_y = swp;
386 }
387
388 const int rotationFixUp = (rFlags & Annotation::FixedRotation) ? pageRotate : 0;
389 const double width = br_x - tl_x;
390 const double height = br_y - tl_y;
391
392 if (rotationFixUp == 0) {
393 return PDFRectangle(tl_x, tl_y, br_x, br_y);
394 } else if (rotationFixUp == 90) {
395 return PDFRectangle(tl_x, tl_y - width, tl_x + height, tl_y);
396 } else if (rotationFixUp == 180) {
397 return PDFRectangle(br_x, tl_y - height, br_x + width, tl_y);
398 } else { // rotationFixUp == 270
399 return PDFRectangle(br_x, br_y - width, br_x + height, br_y);
400 }
401 }
402
403 PDFRectangle AnnotationPrivate::boundaryToPdfRectangle(const QRectF &r, int rFlags) const
404 {
405 return Poppler::boundaryToPdfRectangle(pdfPage, r, rFlags);
406 }
407
408 std::unique_ptr<AnnotPath> AnnotationPrivate::toAnnotPath(const QLinkedList<QPointF> &list) const
409 {
410 const int count = list.size();
411 std::vector<AnnotCoord> ac;
412 ac.reserve(count);
413
414 double MTX[6];
415 fillTransformationMTX(MTX);
416
417 Q_FOREACH (const QPointF &p, list) {
418 double x, y;
419 XPDFReader::invTransform(MTX, p, x, y);
420 ac.emplace_back(x, y);
421 }
422
423 return std::make_unique<AnnotPath>(std::move(ac));
424 }
425
426 QList<Annotation *> AnnotationPrivate::findAnnotations(::Page *pdfPage, DocumentData *doc, const QSet<Annotation::SubType> &subtypes, int parentID)
427 {
428 Annots *annots = pdfPage->getAnnots();
429
430 const bool wantTextAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::AText);
431 const bool wantLineAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::ALine);
432 const bool wantGeomAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::AGeom);
433 const bool wantHighlightAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::AHighlight);
434 const bool wantStampAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::AStamp);
435 const bool wantInkAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::AInk);
436 const bool wantLinkAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::ALink);
437 const bool wantCaretAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::ACaret);
438 const bool wantFileAttachmentAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::AFileAttachment);
439 const bool wantSoundAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::ASound);
440 const bool wantMovieAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::AMovie);
441 const bool wantScreenAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::AScreen);
442 const bool wantWidgetAnnotations = subtypes.isEmpty() || subtypes.contains(Annotation::AWidget);
443
444 // Create Annotation objects and tie to their native Annot
445 QList<Annotation *> res;
446 for (const std::shared_ptr<Annot> &ann : annots->getAnnots()) {
447 if (!ann) {
448 error(errInternal, -1, "Annot is null");
449 continue;
450 }
451
452 // Check parent annotation
453 AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(ann.get());
454 if (!markupann) {
455 // Assume it's a root annotation, and skip if user didn't request it
456 if (parentID != -1) {
457 continue;
458 }
459 } else if (markupann->getInReplyToID() != parentID) {
460 continue;
461 }
462
463 /* Create Annotation of the right subclass */
464 Annotation *annotation = nullptr;
465 Annot::AnnotSubtype subType = ann->getType();
466
467 switch (subType) {
468 case Annot::typeText:
469 if (!wantTextAnnotations) {
470 continue;
471 }
472 annotation = new TextAnnotation(TextAnnotation::Linked);
473 break;
474 case Annot::typeFreeText:
475 if (!wantTextAnnotations) {
476 continue;
477 }
478 annotation = new TextAnnotation(TextAnnotation::InPlace);
479 break;
480 case Annot::typeLine:
481 if (!wantLineAnnotations) {
482 continue;
483 }
484 annotation = new LineAnnotation(LineAnnotation::StraightLine);
485 break;
486 case Annot::typePolygon:
487 case Annot::typePolyLine:
488 if (!wantLineAnnotations) {
489 continue;
490 }
491 annotation = new LineAnnotation(LineAnnotation::Polyline);
492 break;
493 case Annot::typeSquare:
494 case Annot::typeCircle:
495 if (!wantGeomAnnotations) {
496 continue;
497 }
498 annotation = new GeomAnnotation();
499 break;
500 case Annot::typeHighlight:
501 case Annot::typeUnderline:
502 case Annot::typeSquiggly:
503 case Annot::typeStrikeOut:
504 if (!wantHighlightAnnotations) {
505 continue;
506 }
507 annotation = new HighlightAnnotation();
508 break;
509 case Annot::typeStamp:
510 if (!wantStampAnnotations) {
511 continue;
512 }
513 annotation = new StampAnnotation();
514 break;
515 case Annot::typeInk:
516 if (!wantInkAnnotations) {
517 continue;
518 }
519 annotation = new InkAnnotation();
520 break;
521 case Annot::typeLink: /* TODO: Move logic to getters */
522 {
523 if (!wantLinkAnnotations) {
524 continue;
525 }
526 // parse Link params
527 AnnotLink *linkann = static_cast<AnnotLink *>(ann.get());
528 LinkAnnotation *l = new LinkAnnotation();
529 annotation = l;
530
531 // -> hlMode
532 l->setLinkHighlightMode((LinkAnnotation::HighlightMode)linkann->getLinkEffect());
533
534 // -> link region
535 // TODO
536
537 // reading link action
538 if (linkann->getAction()) {
539 Link *popplerLink = PageData::convertLinkActionToLink(linkann->getAction(), doc, QRectF());
540 if (popplerLink) {
541 l->setLinkDestination(popplerLink);
542 }
543 }
544 break;
545 }
546 case Annot::typeCaret:
547 if (!wantCaretAnnotations) {
548 continue;
549 }
550 annotation = new CaretAnnotation();
551 break;
552 case Annot::typeFileAttachment: /* TODO: Move logic to getters */
553 {
554 if (!wantFileAttachmentAnnotations) {
555 continue;
556 }
557 AnnotFileAttachment *attachann = static_cast<AnnotFileAttachment *>(ann.get());
558 FileAttachmentAnnotation *f = new FileAttachmentAnnotation();
559 annotation = f;
560 // -> fileIcon
561 f->setFileIconName(QString::fromLatin1(attachann->getName()->c_str()));
562 // -> embeddedFile
563 auto filespec = std::make_unique<FileSpec>(attachann->getFile());
564 f->setEmbeddedFile(new EmbeddedFile(*new EmbeddedFileData(std::move(filespec))));
565 break;
566 }
567 case Annot::typeSound: /* TODO: Move logic to getters */
568 {
569 if (!wantSoundAnnotations) {
570 continue;
571 }
572 AnnotSound *soundann = static_cast<AnnotSound *>(ann.get());
573 SoundAnnotation *s = new SoundAnnotation();
574 annotation = s;
575
576 // -> soundIcon
577 s->setSoundIconName(QString::fromLatin1(soundann->getName()->c_str()));
578 // -> sound
579 s->setSound(new SoundObject(soundann->getSound()));
580 break;
581 }
582 case Annot::typeMovie: /* TODO: Move logic to getters */
583 {
584 if (!wantMovieAnnotations) {
585 continue;
586 }
587 AnnotMovie *movieann = static_cast<AnnotMovie *>(ann.get());
588 MovieAnnotation *m = new MovieAnnotation();
589 annotation = m;
590
591 // -> movie
592 MovieObject *movie = new MovieObject(movieann);
593 m->setMovie(movie);
594 // -> movieTitle
595 const GooString *movietitle = movieann->getTitle();
596 if (movietitle) {
597 m->setMovieTitle(QString::fromLatin1(movietitle->c_str()));
598 }
599 break;
600 }
601 case Annot::typeScreen: {
602 if (!wantScreenAnnotations) {
603 continue;
604 }
605 AnnotScreen *screenann = static_cast<AnnotScreen *>(ann.get());
606 // TODO Support other link types than Link::Rendition in ScreenAnnotation
607 if (!screenann->getAction() || screenann->getAction()->getKind() != actionRendition) {
608 continue;
609 }
610 ScreenAnnotation *s = new ScreenAnnotation();
611 annotation = s;
612
613 // -> screen
614 Link *popplerLink = PageData::convertLinkActionToLink(screenann->getAction(), doc, QRectF());
615 s->setAction(static_cast<Poppler::LinkRendition *>(popplerLink));
616
617 // -> screenTitle
618 const GooString *screentitle = screenann->getTitle();
619 if (screentitle) {
620 s->setScreenTitle(UnicodeParsedString(screentitle));
621 }
622 break;
623 }
624 case Annot::typePopup:
625 continue; // popups are parsed by Annotation's window() getter
626 case Annot::typeUnknown:
627 continue; // special case for ignoring unknown annotations
628 case Annot::typeWidget:
629 if (!wantWidgetAnnotations) {
630 continue;
631 }
632 annotation = new WidgetAnnotation();
633 break;
634 case Annot::typeRichMedia: {
635 const AnnotRichMedia *annotRichMedia = static_cast<AnnotRichMedia *>(ann.get());
636
637 RichMediaAnnotation *richMediaAnnotation = new RichMediaAnnotation;
638
639 const AnnotRichMedia::Settings *annotSettings = annotRichMedia->getSettings();
640 if (annotSettings) {
641 RichMediaAnnotation::Settings *settings = new RichMediaAnnotation::Settings;
642
643 if (annotSettings->getActivation()) {
644 RichMediaAnnotation::Activation *activation = new RichMediaAnnotation::Activation;
645
646 switch (annotSettings->getActivation()->getCondition()) {
647 case AnnotRichMedia::Activation::conditionPageOpened:
648 activation->setCondition(RichMediaAnnotation::Activation::PageOpened);
649 break;
650 case AnnotRichMedia::Activation::conditionPageVisible:
651 activation->setCondition(RichMediaAnnotation::Activation::PageVisible);
652 break;
653 case AnnotRichMedia::Activation::conditionUserAction:
654 activation->setCondition(RichMediaAnnotation::Activation::UserAction);
655 break;
656 }
657
658 settings->setActivation(activation);
659 }
660
661 if (annotSettings->getDeactivation()) {
662 RichMediaAnnotation::Deactivation *deactivation = new RichMediaAnnotation::Deactivation;
663
664 switch (annotSettings->getDeactivation()->getCondition()) {
665 case AnnotRichMedia::Deactivation::conditionPageClosed:
666 deactivation->setCondition(RichMediaAnnotation::Deactivation::PageClosed);
667 break;
668 case AnnotRichMedia::Deactivation::conditionPageInvisible:
669 deactivation->setCondition(RichMediaAnnotation::Deactivation::PageInvisible);
670 break;
671 case AnnotRichMedia::Deactivation::conditionUserAction:
672 deactivation->setCondition(RichMediaAnnotation::Deactivation::UserAction);
673 break;
674 }
675
676 settings->setDeactivation(deactivation);
677 }
678
679 richMediaAnnotation->setSettings(settings);
680 }
681
682 const AnnotRichMedia::Content *annotContent = annotRichMedia->getContent();
683 if (annotContent) {
684 RichMediaAnnotation::Content *content = new RichMediaAnnotation::Content;
685
686 const std::vector<std::unique_ptr<AnnotRichMedia::Configuration>> &annotConfigurations = annotContent->getConfigurations();
687 QList<RichMediaAnnotation::Configuration *> configurations;
688
689 for (const std::unique_ptr<AnnotRichMedia::Configuration> &annotConfiguration : annotConfigurations) {
690 if (!annotConfiguration) {
691 continue;
692 }
693
694 RichMediaAnnotation::Configuration *configuration = new RichMediaAnnotation::Configuration;
695
696 if (annotConfiguration->getName()) {
697 configuration->setName(UnicodeParsedString(annotConfiguration->getName()));
698 }
699
700 switch (annotConfiguration->getType()) {
701 case AnnotRichMedia::Configuration::type3D:
702 configuration->setType(RichMediaAnnotation::Configuration::Type3D);
703 break;
704 case AnnotRichMedia::Configuration::typeFlash:
705 configuration->setType(RichMediaAnnotation::Configuration::TypeFlash);
706 break;
707 case AnnotRichMedia::Configuration::typeSound:
708 configuration->setType(RichMediaAnnotation::Configuration::TypeSound);
709 break;
710 case AnnotRichMedia::Configuration::typeVideo:
711 configuration->setType(RichMediaAnnotation::Configuration::TypeVideo);
712 break;
713 }
714
715 const std::vector<std::unique_ptr<AnnotRichMedia::Instance>> &annotInstances = annotConfiguration->getInstances();
716 QList<RichMediaAnnotation::Instance *> instances;
717
718 for (const std::unique_ptr<AnnotRichMedia::Instance> &annotInstance : annotInstances) {
719 if (!annotInstance) {
720 continue;
721 }
722
723 RichMediaAnnotation::Instance *instance = new RichMediaAnnotation::Instance;
724
725 switch (annotInstance->getType()) {
726 case AnnotRichMedia::Instance::type3D:
727 instance->setType(RichMediaAnnotation::Instance::Type3D);
728 break;
729 case AnnotRichMedia::Instance::typeFlash:
730 instance->setType(RichMediaAnnotation::Instance::TypeFlash);
731 break;
732 case AnnotRichMedia::Instance::typeSound:
733 instance->setType(RichMediaAnnotation::Instance::TypeSound);
734 break;
735 case AnnotRichMedia::Instance::typeVideo:
736 instance->setType(RichMediaAnnotation::Instance::TypeVideo);
737 break;
738 }
739
740 const AnnotRichMedia::Params *annotParams = annotInstance->getParams();
741 if (annotParams) {
742 RichMediaAnnotation::Params *params = new RichMediaAnnotation::Params;
743
744 if (annotParams->getFlashVars()) {
745 params->setFlashVars(UnicodeParsedString(annotParams->getFlashVars()));
746 }
747
748 instance->setParams(params);
749 }
750
751 instances.append(instance);
752 }
753
754 configuration->setInstances(instances);
755
756 configurations.append(configuration);
757 }
758 content->setConfigurations(configurations);
759
760 const std::vector<std::unique_ptr<AnnotRichMedia::Asset>> &annotAssets = annotContent->getAssets();
761 QList<RichMediaAnnotation::Asset *> assets;
762
763 for (const std::unique_ptr<AnnotRichMedia::Asset> &annotAsset : annotAssets) {
764 if (!annotAsset) {
765 continue;
766 }
767
768 RichMediaAnnotation::Asset *asset = new RichMediaAnnotation::Asset;
769
770 if (annotAsset->getName()) {
771 asset->setName(UnicodeParsedString(annotAsset->getName()));
772 }
773
774 auto fileSpec = std::make_unique<FileSpec>(annotAsset->getFileSpec());
775 asset->setEmbeddedFile(new EmbeddedFile(*new EmbeddedFileData(std::move(fileSpec))));
776
777 assets.append(asset);
778 }
779 content->setAssets(assets);
780
781 richMediaAnnotation->setContent(content);
782 }
783
784 annotation = richMediaAnnotation;
785
786 break;
787 }
788 default: {
789 #define CASE_FOR_TYPE(thetype) \
790 case Annot::type##thetype: \
791 error(errUnimplemented, -1, "Annotation " #thetype " not supported"); \
792 break;
793 switch (subType) {
794 CASE_FOR_TYPE(PrinterMark)
795 CASE_FOR_TYPE(TrapNet)
796 CASE_FOR_TYPE(Watermark)
797 CASE_FOR_TYPE(3D)
798 default:
799 error(errUnimplemented, -1, "Annotation {0:d} not supported", subType);
800 }
801 continue;
802 #undef CASE_FOR_TYPE
803 }
804 }
805
806 annotation->d_ptr->tieToNativeAnnot(ann, pdfPage, doc);
807 res.append(annotation);
808 }
809
810 return res;
811 }
812
813 Ref AnnotationPrivate::pdfObjectReference() const
814 {
815 if (pdfAnnot == nullptr) {
816 return Ref::INVALID();
817 }
818
819 return pdfAnnot->getRef();
820 }
821
822 Link *AnnotationPrivate::additionalAction(Annotation::AdditionalActionType type) const
823 {
824 if (pdfAnnot->getType() != Annot::typeScreen && pdfAnnot->getType() != Annot::typeWidget) {
825 return nullptr;
826 }
827
828 const Annot::AdditionalActionsType actionType = toPopplerAdditionalActionType(type);
829
830 std::unique_ptr<::LinkAction> linkAction = nullptr;
831 if (pdfAnnot->getType() == Annot::typeScreen) {
832 linkAction = static_cast<AnnotScreen *>(pdfAnnot.get())->getAdditionalAction(actionType);
833 } else {
834 linkAction = static_cast<AnnotWidget *>(pdfAnnot.get())->getAdditionalAction(actionType);
835 }
836
837 Link *link = nullptr;
838
839 if (linkAction) {
840 link = PageData::convertLinkActionToLink(linkAction.get(), parentDoc, QRectF());
841 }
842
843 return link;
844 }
845
846 void AnnotationPrivate::addAnnotationToPage(::Page *pdfPage, DocumentData *doc, const Annotation *ann)
847 {
848 if (ann->d_ptr->pdfAnnot != nullptr) {
849 error(errIO, -1, "Annotation is already tied");
850 return;
851 }
852
853 // Unimplemented annotations can't be created by the user because their ctor
854 // is private. Therefore, createNativeAnnot will never return 0
855 std::shared_ptr<Annot> nativeAnnot = ann->d_ptr->createNativeAnnot(pdfPage, doc);
856 Q_ASSERT(nativeAnnot);
857
858 if (ann->d_ptr->annotationAppearance.isStream()) {
859 nativeAnnot->setNewAppearance(ann->d_ptr->annotationAppearance.copy());
860 }
861
862 pdfPage->addAnnot(nativeAnnot);
863 }
864
865 void AnnotationPrivate::removeAnnotationFromPage(::Page *pdfPage, const Annotation *ann)
866 {
867 if (ann->d_ptr->pdfAnnot == nullptr) {
868 error(errIO, -1, "Annotation is not tied");
869 return;
870 }
871
872 if (ann->d_ptr->pdfPage != pdfPage) {
873 error(errIO, -1, "Annotation doesn't belong to the specified page");
874 return;
875 }
876
877 // Remove annotation
878 pdfPage->removeAnnot(ann->d_ptr->pdfAnnot);
879
880 // Destroy object
881 delete ann;
882 }
883
884 class TextAnnotationPrivate : public AnnotationPrivate
885 {
886 public:
887 TextAnnotationPrivate();
888 std::unique_ptr<Annotation> makeAlias() override;
889 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
890 void setDefaultAppearanceToNative();
891 std::unique_ptr<DefaultAppearance> getDefaultAppearanceFromNative() const;
892
893 // data fields
894 TextAnnotation::TextType textType;
895 QString textIcon;
896 std::optional<QFont> textFont;
897 QColor textColor = Qt::black;
898 int inplaceAlign; // 0:left, 1:center, 2:right
899 QVector<QPointF> inplaceCallout;
900 TextAnnotation::InplaceIntent inplaceIntent;
901 };
902
903 class Annotation::Style::Private : public QSharedData
904 {
905 public:
906 Private() : opacity(1.0), width(1.0), lineStyle(Solid), xCorners(0.0), yCorners(0.0), lineEffect(NoEffect), effectIntensity(1.0)
907 {
908 dashArray.resize(1);
909 dashArray[0] = 3;
910 }
911
912 QColor color;
913 double opacity;
914 double width;
915 Annotation::LineStyle lineStyle;
916 double xCorners;
917 double yCorners;
918 QVector<double> dashArray;
919 Annotation::LineEffect lineEffect;
920 double effectIntensity;
921 };
922
923 Annotation::Style::Style() : d(new Private) { }
924
925 Annotation::Style::Style(const Style &other) = default;
926
927 Annotation::Style &Annotation::Style::operator=(const Style &other)
928 {
929 if (this != &other) {
930 d = other.d;
931 }
932
933 return *this;
934 }
935
936 Annotation::Style::~Style() = default;
937
938 QColor Annotation::Style::color() const
939 {
940 return d->color;
941 }
942
943 void Annotation::Style::setColor(const QColor &color)
944 {
945 d->color = color;
946 }
947
948 double Annotation::Style::opacity() const
949 {
950 return d->opacity;
951 }
952
953 void Annotation::Style::setOpacity(double opacity)
954 {
955 d->opacity = opacity;
956 }
957
958 double Annotation::Style::width() const
959 {
960 return d->width;
961 }
962
963 void Annotation::Style::setWidth(double width)
964 {
965 d->width = width;
966 }
967
968 Annotation::LineStyle Annotation::Style::lineStyle() const
969 {
970 return d->lineStyle;
971 }
972
973 void Annotation::Style::setLineStyle(Annotation::LineStyle style)
974 {
975 d->lineStyle = style;
976 }
977
978 double Annotation::Style::xCorners() const
979 {
980 return d->xCorners;
981 }
982
983 void Annotation::Style::setXCorners(double radius)
984 {
985 d->xCorners = radius;
986 }
987
988 double Annotation::Style::yCorners() const
989 {
990 return d->yCorners;
991 }
992
993 void Annotation::Style::setYCorners(double radius)
994 {
995 d->yCorners = radius;
996 }
997
998 const QVector<double> &Annotation::Style::dashArray() const
999 {
1000 return d->dashArray;
1001 }
1002
1003 void Annotation::Style::setDashArray(const QVector<double> &array)
1004 {
1005 d->dashArray = array;
1006 }
1007
1008 Annotation::LineEffect Annotation::Style::lineEffect() const
1009 {
1010 return d->lineEffect;
1011 }
1012
1013 void Annotation::Style::setLineEffect(Annotation::LineEffect effect)
1014 {
1015 d->lineEffect = effect;
1016 }
1017
1018 double Annotation::Style::effectIntensity() const
1019 {
1020 return d->effectIntensity;
1021 }
1022
1023 void Annotation::Style::setEffectIntensity(double intens)
1024 {
1025 d->effectIntensity = intens;
1026 }
1027
1028 class Annotation::Popup::Private : public QSharedData
1029 {
1030 public:
1031 Private() : flags(-1) { }
1032
1033 int flags;
1034 QRectF geometry;
1035 QString title;
1036 QString summary;
1037 QString text;
1038 };
1039
1040 Annotation::Popup::Popup() : d(new Private) { }
1041
1042 Annotation::Popup::Popup(const Popup &other) = default;
1043
1044 Annotation::Popup &Annotation::Popup::operator=(const Popup &other)
1045 {
1046 if (this != &other) {
1047 d = other.d;
1048 }
1049
1050 return *this;
1051 }
1052
1053 Annotation::Popup::~Popup() = default;
1054
1055 int Annotation::Popup::flags() const
1056 {
1057 return d->flags;
1058 }
1059
1060 void Annotation::Popup::setFlags(int flags)
1061 {
1062 d->flags = flags;
1063 }
1064
1065 QRectF Annotation::Popup::geometry() const
1066 {
1067 return d->geometry;
1068 }
1069
1070 void Annotation::Popup::setGeometry(const QRectF &geom)
1071 {
1072 d->geometry = geom;
1073 }
1074
1075 QString Annotation::Popup::title() const
1076 {
1077 return d->title;
1078 }
1079
1080 void Annotation::Popup::setTitle(const QString &title)
1081 {
1082 d->title = title;
1083 }
1084
1085 QString Annotation::Popup::summary() const
1086 {
1087 return d->summary;
1088 }
1089
1090 void Annotation::Popup::setSummary(const QString &summary)
1091 {
1092 d->summary = summary;
1093 }
1094
1095 QString Annotation::Popup::text() const
1096 {
1097 return d->text;
1098 }
1099
1100 void Annotation::Popup::setText(const QString &text)
1101 {
1102 d->text = text;
1103 }
1104
1105 Annotation::Annotation(AnnotationPrivate &dd) : d_ptr(&dd) { }
1106
1107 Annotation::~Annotation() = default;
1108
1109 Annotation::Annotation(AnnotationPrivate &dd, const QDomNode &annNode) : d_ptr(&dd)
1110 {
1111 Q_D(Annotation);
1112
1113 // get the [base] element of the annotation node
1114 QDomElement e = AnnotationUtils::findChildElement(annNode, QStringLiteral("base"));
1115 if (e.isNull()) {
1116 return;
1117 }
1118
1119 Style s;
1120 Popup w;
1121
1122 // parse -contents- attributes
1123 if (e.hasAttribute(QStringLiteral("author"))) {
1124 setAuthor(e.attribute(QStringLiteral("author")));
1125 }
1126 if (e.hasAttribute(QStringLiteral("contents"))) {
1127 setContents(e.attribute(QStringLiteral("contents")));
1128 }
1129 if (e.hasAttribute(QStringLiteral("uniqueName"))) {
1130 setUniqueName(e.attribute(QStringLiteral("uniqueName")));
1131 }
1132 if (e.hasAttribute(QStringLiteral("modifyDate"))) {
1133 QDateTime dt = QDateTime::fromString(e.attribute(QStringLiteral("modifyDate")));
1134 if (!dt.isValid()) {
1135 dt = QDateTime::fromString(e.attribute(QStringLiteral("modifyDate")), Qt::ISODate);
1136 }
1137 setModificationDate(dt);
1138 }
1139 if (e.hasAttribute(QStringLiteral("creationDate"))) {
1140 QDateTime dt = QDateTime::fromString(e.attribute(QStringLiteral("creationDate")));
1141 if (!dt.isValid()) {
1142 dt = QDateTime::fromString(e.attribute(QStringLiteral("creationDate")), Qt::ISODate);
1143 }
1144 setCreationDate(dt);
1145 }
1146
1147 // parse -other- attributes
1148 if (e.hasAttribute(QStringLiteral("flags"))) {
1149 setFlags(e.attribute(QStringLiteral("flags")).toInt());
1150 }
1151 if (e.hasAttribute(QStringLiteral("color"))) {
1152 s.setColor(QColor(e.attribute(QStringLiteral("color"))));
1153 }
1154 if (e.hasAttribute(QStringLiteral("opacity"))) {
1155 s.setOpacity(e.attribute(QStringLiteral("opacity")).toDouble());
1156 }
1157
1158 // parse -the-subnodes- (describing Style, Window, Revision(s) structures)
1159 // Note: all subnodes if present must be 'attributes complete'
1160 QDomNode eSubNode = e.firstChild();
1161 while (eSubNode.isElement()) {
1162 QDomElement ee = eSubNode.toElement();
1163 eSubNode = eSubNode.nextSibling();
1164
1165 // parse boundary
1166 if (ee.tagName() == QLatin1String("boundary")) {
1167 QRectF brect;
1168 brect.setLeft(ee.attribute(QStringLiteral("l")).toDouble());
1169 brect.setTop(ee.attribute(QStringLiteral("t")).toDouble());
1170 brect.setRight(ee.attribute(QStringLiteral("r")).toDouble());
1171 brect.setBottom(ee.attribute(QStringLiteral("b")).toDouble());
1172 setBoundary(brect);
1173 }
1174 // parse penStyle if not default
1175 else if (ee.tagName() == QLatin1String("penStyle")) {
1176 s.setWidth(ee.attribute(QStringLiteral("width")).toDouble());
1177 s.setLineStyle((LineStyle)ee.attribute(QStringLiteral("style")).toInt());
1178 s.setXCorners(ee.attribute(QStringLiteral("xcr")).toDouble());
1179 s.setYCorners(ee.attribute(QStringLiteral("ycr")).toDouble());
1180
1181 // Try to parse dash array (new format)
1182 QVector<double> dashArray;
1183
1184 QDomNode eeSubNode = ee.firstChild();
1185 while (eeSubNode.isElement()) {
1186 QDomElement eee = eeSubNode.toElement();
1187 eeSubNode = eeSubNode.nextSibling();
1188
1189 if (eee.tagName() != QLatin1String("dashsegm")) {
1190 continue;
1191 }
1192
1193 dashArray.append(eee.attribute(QStringLiteral("len")).toDouble());
1194 }
1195
1196 // If no segments were found use marks/spaces (old format)
1197 if (dashArray.empty()) {
1198 dashArray.append(ee.attribute(QStringLiteral("marks")).toDouble());
1199 dashArray.append(ee.attribute(QStringLiteral("spaces")).toDouble());
1200 }
1201
1202 s.setDashArray(dashArray);
1203 }
1204 // parse effectStyle if not default
1205 else if (ee.tagName() == QLatin1String("penEffect")) {
1206 s.setLineEffect((LineEffect)ee.attribute(QStringLiteral("effect")).toInt());
1207 s.setEffectIntensity(ee.attribute(QStringLiteral("intensity")).toDouble());
1208 }
1209 // parse window if present
1210 else if (ee.tagName() == QLatin1String("window")) {
1211 QRectF geom;
1212 geom.setX(ee.attribute(QStringLiteral("top")).toDouble());
1213 geom.setY(ee.attribute(QStringLiteral("left")).toDouble());
1214
1215 if (ee.hasAttribute(QStringLiteral("widthDouble"))) {
1216 geom.setWidth(ee.attribute(QStringLiteral("widthDouble")).toDouble());
1217 } else {
1218 geom.setWidth(ee.attribute(QStringLiteral("width")).toDouble());
1219 }
1220
1221 if (ee.hasAttribute(QStringLiteral("widthDouble"))) {
1222 geom.setHeight(ee.attribute(QStringLiteral("heightDouble")).toDouble());
1223 } else {
1224 geom.setHeight(ee.attribute(QStringLiteral("height")).toDouble());
1225 }
1226
1227 w.setGeometry(geom);
1228
1229 w.setFlags(ee.attribute(QStringLiteral("flags")).toInt());
1230 w.setTitle(ee.attribute(QStringLiteral("title")));
1231 w.setSummary(ee.attribute(QStringLiteral("summary")));
1232 // parse window subnodes
1233 QDomNode winNode = ee.firstChild();
1234 for (; winNode.isElement(); winNode = winNode.nextSibling()) {
1235 QDomElement winElement = winNode.toElement();
1236 if (winElement.tagName() == QLatin1String("text")) {
1237 w.setText(winElement.firstChild().toCDATASection().data());
1238 }
1239 }
1240 }
1241 }
1242
1243 setStyle(s); // assign parsed style
1244 setPopup(w); // assign parsed window
1245
1246 // get the [revisions] element of the annotation node
1247 QDomNode revNode = annNode.firstChild();
1248 for (; revNode.isElement(); revNode = revNode.nextSibling()) {
1249 QDomElement revElement = revNode.toElement();
1250 if (revElement.tagName() != QLatin1String("revision")) {
1251 continue;
1252 }
1253
1254 Annotation *reply = AnnotationUtils::createAnnotation(revElement);
1255
1256 if (reply) // if annotation is valid, add as a revision of this annotation
1257 {
1258 RevScope scope = (RevScope)revElement.attribute(QStringLiteral("revScope")).toInt();
1259 RevType type = (RevType)revElement.attribute(QStringLiteral("revType")).toInt();
1260 d->addRevision(reply, scope, type);
1261 delete reply;
1262 }
1263 }
1264 }
1265
1266 void Annotation::storeBaseAnnotationProperties(QDomNode &annNode, QDomDocument &document) const
1267 {
1268 // create [base] element of the annotation node
1269 QDomElement e = document.createElement(QStringLiteral("base"));
1270 annNode.appendChild(e);
1271
1272 const Style s = style();
1273 const Popup w = popup();
1274
1275 // store -contents- attributes
1276 if (!author().isEmpty()) {
1277 e.setAttribute(QStringLiteral("author"), author());
1278 }
1279 if (!contents().isEmpty()) {
1280 e.setAttribute(QStringLiteral("contents"), contents());
1281 }
1282 if (!uniqueName().isEmpty()) {
1283 e.setAttribute(QStringLiteral("uniqueName"), uniqueName());
1284 }
1285 if (modificationDate().isValid()) {
1286 e.setAttribute(QStringLiteral("modifyDate"), modificationDate().toString());
1287 }
1288 if (creationDate().isValid()) {
1289 e.setAttribute(QStringLiteral("creationDate"), creationDate().toString());
1290 }
1291
1292 // store -other- attributes
1293 if (flags()) {
1294 e.setAttribute(QStringLiteral("flags"), flags());
1295 }
1296 if (s.color().isValid()) {
1297 e.setAttribute(QStringLiteral("color"), s.color().name());
1298 }
1299 if (s.opacity() != 1.0) {
1300 e.setAttribute(QStringLiteral("opacity"), QString::number(s.opacity()));
1301 }
1302
1303 // Sub-Node-1 - boundary
1304 const QRectF brect = boundary();
1305 QDomElement bE = document.createElement(QStringLiteral("boundary"));
1306 e.appendChild(bE);
1307 bE.setAttribute(QStringLiteral("l"), QString::number((double)brect.left()));
1308 bE.setAttribute(QStringLiteral("t"), QString::number((double)brect.top()));
1309 bE.setAttribute(QStringLiteral("r"), QString::number((double)brect.right()));
1310 bE.setAttribute(QStringLiteral("b"), QString::number((double)brect.bottom()));
1311
1312 // Sub-Node-2 - penStyle
1313 const QVector<double> &dashArray = s.dashArray();
1314 if (s.width() != 1 || s.lineStyle() != Solid || s.xCorners() != 0 || s.yCorners() != 0.0 || dashArray.size() != 1 || dashArray[0] != 3) {
1315 QDomElement psE = document.createElement(QStringLiteral("penStyle"));
1316 e.appendChild(psE);
1317 psE.setAttribute(QStringLiteral("width"), QString::number(s.width()));
1318 psE.setAttribute(QStringLiteral("style"), (int)s.lineStyle());
1319 psE.setAttribute(QStringLiteral("xcr"), QString::number(s.xCorners()));
1320 psE.setAttribute(QStringLiteral("ycr"), QString::number(s.yCorners()));
1321
1322 int marks = 3, spaces = 0; // Do not break code relying on marks/spaces
1323 if (!dashArray.empty()) {
1324 marks = (int)dashArray[0];
1325 }
1326 if (dashArray.size() > 1) {
1327 spaces = (int)dashArray[1];
1328 }
1329
1330 psE.setAttribute(QStringLiteral("marks"), marks);
1331 psE.setAttribute(QStringLiteral("spaces"), spaces);
1332
1333 Q_FOREACH (double segm, dashArray) {
1334 QDomElement pattE = document.createElement(QStringLiteral("dashsegm"));
1335 pattE.setAttribute(QStringLiteral("len"), QString::number(segm));
1336 psE.appendChild(pattE);
1337 }
1338 }
1339
1340 // Sub-Node-3 - penEffect
1341 if (s.lineEffect() != NoEffect || s.effectIntensity() != 1.0) {
1342 QDomElement peE = document.createElement(QStringLiteral("penEffect"));
1343 e.appendChild(peE);
1344 peE.setAttribute(QStringLiteral("effect"), (int)s.lineEffect());
1345 peE.setAttribute(QStringLiteral("intensity"), QString::number(s.effectIntensity()));
1346 }
1347
1348 // Sub-Node-4 - window
1349 if (w.flags() != -1 || !w.title().isEmpty() || !w.summary().isEmpty() || !w.text().isEmpty()) {
1350 QDomElement wE = document.createElement(QStringLiteral("window"));
1351 const QRectF geom = w.geometry();
1352 e.appendChild(wE);
1353 wE.setAttribute(QStringLiteral("flags"), w.flags());
1354 wE.setAttribute(QStringLiteral("top"), QString::number(geom.x()));
1355 wE.setAttribute(QStringLiteral("left"), QString::number(geom.y()));
1356 wE.setAttribute(QStringLiteral("width"), (int)geom.width());
1357 wE.setAttribute(QStringLiteral("height"), (int)geom.height());
1358 wE.setAttribute(QStringLiteral("widthDouble"), QString::number(geom.width()));
1359 wE.setAttribute(QStringLiteral("heightDouble"), QString::number(geom.height()));
1360 wE.setAttribute(QStringLiteral("title"), w.title());
1361 wE.setAttribute(QStringLiteral("summary"), w.summary());
1362 // store window.text as a subnode, because we need escaped data
1363 if (!w.text().isEmpty()) {
1364 QDomElement escapedText = document.createElement(QStringLiteral("text"));
1365 wE.appendChild(escapedText);
1366 QDomCDATASection textCData = document.createCDATASection(w.text());
1367 escapedText.appendChild(textCData);
1368 }
1369 }
1370
1371 const QList<Annotation *> revs = revisions();
1372
1373 // create [revision] element of the annotation node (if any)
1374 if (revs.isEmpty()) {
1375 return;
1376 }
1377
1378 // add all revisions as children of revisions element
1379 Q_FOREACH (const Annotation *rev, revs) {
1380 QDomElement r = document.createElement(QStringLiteral("revision"));
1381 annNode.appendChild(r);
1382 // set element attributes
1383 r.setAttribute(QStringLiteral("revScope"), (int)rev->revisionScope());
1384 r.setAttribute(QStringLiteral("revType"), (int)rev->revisionType());
1385 // use revision as the annotation element, so fill it up
1386 AnnotationUtils::storeAnnotation(rev, r, document);
1387 delete rev;
1388 }
1389 }
1390
1391 QString Annotation::author() const
1392 {
1393 Q_D(const Annotation);
1394
1395 if (!d->pdfAnnot) {
1396 return d->author;
1397 }
1398
1399 const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
1400 return markupann ? UnicodeParsedString(markupann->getLabel()) : QString();
1401 }
1402
1403 void Annotation::setAuthor(const QString &author)
1404 {
1405 Q_D(Annotation);
1406
1407 if (!d->pdfAnnot) {
1408 d->author = author;
1409 return;
1410 }
1411
1412 AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot.get());
1413 if (markupann) {
1414 markupann->setLabel(std::unique_ptr<GooString>(QStringToUnicodeGooString(author)));
1415 }
1416 }
1417
1418 QString Annotation::contents() const
1419 {
1420 Q_D(const Annotation);
1421
1422 if (!d->pdfAnnot) {
1423 return d->contents;
1424 }
1425
1426 return UnicodeParsedString(d->pdfAnnot->getContents());
1427 }
1428
1429 void Annotation::setContents(const QString &contents)
1430 {
1431 Q_D(Annotation);
1432
1433 if (!d->pdfAnnot) {
1434 d->contents = contents;
1435 return;
1436 }
1437
1438 d->pdfAnnot->setContents(std::unique_ptr<GooString>(QStringToUnicodeGooString(contents)));
1439
1440 TextAnnotationPrivate *textAnnotD = dynamic_cast<TextAnnotationPrivate *>(d);
1441 if (textAnnotD) {
1442 textAnnotD->setDefaultAppearanceToNative();
1443 }
1444 }
1445
1446 QString Annotation::uniqueName() const
1447 {
1448 Q_D(const Annotation);
1449
1450 if (!d->pdfAnnot) {
1451 return d->uniqueName;
1452 }
1453
1454 return UnicodeParsedString(d->pdfAnnot->getName());
1455 }
1456
1457 void Annotation::setUniqueName(const QString &uniqueName)
1458 {
1459 Q_D(Annotation);
1460
1461 if (!d->pdfAnnot) {
1462 d->uniqueName = uniqueName;
1463 return;
1464 }
1465
1466 QByteArray ascii = uniqueName.toLatin1();
1467 GooString s(ascii.constData());
1468 d->pdfAnnot->setName(&s);
1469 }
1470
1471 QDateTime Annotation::modificationDate() const
1472 {
1473 Q_D(const Annotation);
1474
1475 if (!d->pdfAnnot) {
1476 return d->modDate;
1477 }
1478
1479 if (d->pdfAnnot->getModified()) {
1480 return convertDate(d->pdfAnnot->getModified()->c_str());
1481 } else {
1482 return QDateTime();
1483 }
1484 }
1485
1486 void Annotation::setModificationDate(const QDateTime &date)
1487 {
1488 Q_D(Annotation);
1489
1490 if (!d->pdfAnnot) {
1491 d->modDate = date;
1492 return;
1493 }
1494
1495 if (d->pdfAnnot) {
1496 if (date.isValid()) {
1497 const time_t t = date.toSecsSinceEpoch();
1498 std::unique_ptr<GooString> s = timeToDateString(&t);
1499 d->pdfAnnot->setModified(std::move(s));
1500 } else {
1501 d->pdfAnnot->setModified(nullptr);
1502 }
1503 }
1504 }
1505
1506 QDateTime Annotation::creationDate() const
1507 {
1508 Q_D(const Annotation);
1509
1510 if (!d->pdfAnnot) {
1511 return d->creationDate;
1512 }
1513
1514 const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
1515
1516 if (markupann && markupann->getDate()) {
1517 return convertDate(markupann->getDate()->c_str());
1518 }
1519
1520 return modificationDate();
1521 }
1522
1523 void Annotation::setCreationDate(const QDateTime &date)
1524 {
1525 Q_D(Annotation);
1526
1527 if (!d->pdfAnnot) {
1528 d->creationDate = date;
1529 return;
1530 }
1531
1532 AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot.get());
1533 if (markupann) {
1534 if (date.isValid()) {
1535 const time_t t = date.toSecsSinceEpoch();
1536 std::unique_ptr<GooString> s = timeToDateString(&t);
1537 markupann->setDate(std::move(s));
1538 } else {
1539 markupann->setDate(nullptr);
1540 }
1541 }
1542 }
1543
1544 static int fromPdfFlags(int flags)
1545 {
1546 int qtflags = 0;
1547
1548 if (flags & Annot::flagHidden) {
1549 qtflags |= Annotation::Hidden;
1550 }
1551 if (flags & Annot::flagNoZoom) {
1552 qtflags |= Annotation::FixedSize;
1553 }
1554 if (flags & Annot::flagNoRotate) {
1555 qtflags |= Annotation::FixedRotation;
1556 }
1557 if (!(flags & Annot::flagPrint)) {
1558 qtflags |= Annotation::DenyPrint;
1559 }
1560 if (flags & Annot::flagReadOnly) {
1561 qtflags |= (Annotation::DenyWrite | Annotation::DenyDelete);
1562 }
1563 if (flags & Annot::flagLocked) {
1564 qtflags |= Annotation::DenyDelete;
1565 }
1566 if (flags & Annot::flagToggleNoView) {
1567 qtflags |= Annotation::ToggleHidingOnMouse;
1568 }
1569
1570 return qtflags;
1571 }
1572
1573 static int toPdfFlags(int qtflags)
1574 {
1575 int flags = 0;
1576
1577 if (qtflags & Annotation::Hidden) {
1578 flags |= Annot::flagHidden;
1579 }
1580 if (qtflags & Annotation::FixedSize) {
1581 flags |= Annot::flagNoZoom;
1582 }
1583 if (qtflags & Annotation::FixedRotation) {
1584 flags |= Annot::flagNoRotate;
1585 }
1586 if (!(qtflags & Annotation::DenyPrint)) {
1587 flags |= Annot::flagPrint;
1588 }
1589 if (qtflags & Annotation::DenyWrite) {
1590 flags |= Annot::flagReadOnly;
1591 }
1592 if (qtflags & Annotation::DenyDelete) {
1593 flags |= Annot::flagLocked;
1594 }
1595 if (qtflags & Annotation::ToggleHidingOnMouse) {
1596 flags |= Annot::flagToggleNoView;
1597 }
1598
1599 return flags;
1600 }
1601
1602 int Annotation::flags() const
1603 {
1604 Q_D(const Annotation);
1605
1606 if (!d->pdfAnnot) {
1607 return d->flags;
1608 }
1609
1610 return fromPdfFlags(d->pdfAnnot->getFlags());
1611 }
1612
1613 void Annotation::setFlags(int flags)
1614 {
1615 Q_D(Annotation);
1616
1617 if (!d->pdfAnnot) {
1618 d->flags = flags;
1619 return;
1620 }
1621
1622 d->pdfAnnot->setFlags(toPdfFlags(flags));
1623 }
1624
1625 QRectF Annotation::boundary() const
1626 {
1627 Q_D(const Annotation);
1628
1629 if (!d->pdfAnnot) {
1630 return d->boundary;
1631 }
1632
1633 const PDFRectangle &rect = d->pdfAnnot->getRect();
1634 return d->fromPdfRectangle(rect);
1635 }
1636
1637 void Annotation::setBoundary(const QRectF &boundary)
1638 {
1639 Q_D(Annotation);
1640
1641 if (!d->pdfAnnot) {
1642 d->boundary = boundary;
1643 return;
1644 }
1645
1646 const PDFRectangle rect = d->boundaryToPdfRectangle(boundary, flags());
1647 if (rect == d->pdfAnnot->getRect()) {
1648 return;
1649 }
1650 d->pdfAnnot->setRect(rect);
1651 }
1652
1653 Annotation::Style Annotation::style() const
1654 {
1655 Q_D(const Annotation);
1656
1657 if (!d->pdfAnnot) {
1658 return d->style;
1659 }
1660
1661 Style s;
1662 s.setColor(convertAnnotColor(d->pdfAnnot->getColor()));
1663
1664 const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
1665 if (markupann) {
1666 s.setOpacity(markupann->getOpacity());
1667 }
1668
1669 const AnnotBorder *border = d->pdfAnnot->getBorder();
1670 if (border) {
1671 if (border->getType() == AnnotBorder::typeArray) {
1672 const AnnotBorderArray *border_array = static_cast<const AnnotBorderArray *>(border);
1673 s.setXCorners(border_array->getHorizontalCorner());
1674 s.setYCorners(border_array->getVerticalCorner());
1675 }
1676
1677 s.setWidth(border->getWidth());
1678 s.setLineStyle((Annotation::LineStyle)(1 << border->getStyle()));
1679
1680 const std::vector<double> &dashArray = border->getDash();
1681 s.setDashArray(QVector<double>(dashArray.begin(), dashArray.end()));
1682 }
1683
1684 AnnotBorderEffect *border_effect;
1685 switch (d->pdfAnnot->getType()) {
1686 case Annot::typeFreeText:
1687 border_effect = static_cast<AnnotFreeText *>(d->pdfAnnot.get())->getBorderEffect();
1688 break;
1689 case Annot::typeSquare:
1690 case Annot::typeCircle:
1691 border_effect = static_cast<AnnotGeometry *>(d->pdfAnnot.get())->getBorderEffect();
1692 break;
1693 default:
1694 border_effect = nullptr;
1695 }
1696 if (border_effect) {
1697 s.setLineEffect((Annotation::LineEffect)border_effect->getEffectType());
1698 s.setEffectIntensity(border_effect->getIntensity());
1699 }
1700
1701 return s;
1702 }
1703
1704 void Annotation::setStyle(const Annotation::Style &style)
1705 {
1706 Q_D(Annotation);
1707
1708 if (!d->pdfAnnot) {
1709 d->style = style;
1710 return;
1711 }
1712
1713 d->pdfAnnot->setColor(convertQColor(style.color()));
1714
1715 AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot.get());
1716 if (markupann) {
1717 markupann->setOpacity(style.opacity());
1718 }
1719
1720 auto border = std::make_unique<AnnotBorderArray>();
1721 border->setWidth(style.width());
1722 border->setHorizontalCorner(style.xCorners());
1723 border->setVerticalCorner(style.yCorners());
1724 d->pdfAnnot->setBorder(std::move(border));
1725 }
1726
1727 Annotation::Popup Annotation::popup() const
1728 {
1729 Q_D(const Annotation);
1730
1731 if (!d->pdfAnnot) {
1732 return d->popup;
1733 }
1734
1735 Popup w;
1736 AnnotPopup *popup = nullptr;
1737 int flags = -1; // Not initialized
1738
1739 const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
1740 if (markupann) {
1741 popup = markupann->getPopup().get();
1742 w.setSummary(UnicodeParsedString(markupann->getSubject()));
1743 }
1744
1745 if (popup) {
1746 flags = fromPdfFlags(popup->getFlags()) & (Annotation::Hidden | Annotation::FixedSize | Annotation::FixedRotation);
1747
1748 if (!popup->getOpen()) {
1749 flags |= Annotation::Hidden;
1750 }
1751
1752 const PDFRectangle &rect = popup->getRect();
1753 w.setGeometry(d->fromPdfRectangle(rect));
1754 }
1755
1756 if (d->pdfAnnot->getType() == Annot::typeText) {
1757 const AnnotText *textann = static_cast<const AnnotText *>(d->pdfAnnot.get());
1758
1759 // Text annotations default to same rect as annotation
1760 if (flags == -1) {
1761 flags = 0;
1762 w.setGeometry(boundary());
1763 }
1764
1765 // If text is not 'opened', force window hiding. if the window
1766 // was parsed from popup, the flag should already be set
1767 if (!textann->getOpen() && flags != -1) {
1768 flags |= Annotation::Hidden;
1769 }
1770 }
1771
1772 w.setFlags(flags);
1773
1774 return w;
1775 }
1776
1777 void Annotation::setPopup(const Annotation::Popup &popup)
1778 {
1779 Q_D(Annotation);
1780
1781 if (!d->pdfAnnot) {
1782 d->popup = popup;
1783 return;
1784 }
1785
1786 #if 0 /* TODO: Remove old popup and add AnnotPopup to page */
1787 AnnotMarkup *markupann = dynamic_cast<AnnotMarkup*>(d->pdfAnnot);
1788 if (!markupann)
1789 return;
1790
1791 // Create a new AnnotPopup and assign it to pdfAnnot
1792 PDFRectangle rect = d->toPdfRectangle( popup.geometry() );
1793 AnnotPopup * p = new AnnotPopup( d->pdfPage->getDoc(), &rect );
1794 p->setOpen( !(popup.flags() & Annotation::Hidden) );
1795 if (!popup.summary().isEmpty())
1796 {
1797 GooString *s = QStringToUnicodeGooString(popup.summary());
1798 markupann->setLabel(s);
1799 delete s;
1800 }
1801 markupann->setPopup(p);
1802 #endif
1803 }
1804
1805 Annotation::RevScope Annotation::revisionScope() const
1806 {
1807 Q_D(const Annotation);
1808
1809 if (!d->pdfAnnot) {
1810 return d->revisionScope;
1811 }
1812
1813 const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
1814
1815 if (markupann && markupann->isInReplyTo()) {
1816 switch (markupann->getReplyTo()) {
1817 case AnnotMarkup::replyTypeR:
1818 return Annotation::Reply;
1819 case AnnotMarkup::replyTypeGroup:
1820 return Annotation::Group;
1821 }
1822 }
1823
1824 return Annotation::Root; // It's not a revision
1825 }
1826
1827 Annotation::RevType Annotation::revisionType() const
1828 {
1829 Q_D(const Annotation);
1830
1831 if (!d->pdfAnnot) {
1832 return d->revisionType;
1833 }
1834
1835 const AnnotText *textann = dynamic_cast<const AnnotText *>(d->pdfAnnot.get());
1836
1837 if (textann && textann->isInReplyTo()) {
1838 switch (textann->getState()) {
1839 case AnnotText::stateMarked:
1840 return Annotation::Marked;
1841 case AnnotText::stateUnmarked:
1842 return Annotation::Unmarked;
1843 case AnnotText::stateAccepted:
1844 return Annotation::Accepted;
1845 case AnnotText::stateRejected:
1846 return Annotation::Rejected;
1847 case AnnotText::stateCancelled:
1848 return Annotation::Cancelled;
1849 case AnnotText::stateCompleted:
1850 return Annotation::Completed;
1851 default:
1852 break;
1853 }
1854 }
1855
1856 return Annotation::None;
1857 }
1858
1859 QList<Annotation *> Annotation::revisions() const
1860 {
1861 Q_D(const Annotation);
1862
1863 if (!d->pdfAnnot) {
1864 /* Return aliases, whose ownership goes to the caller */
1865 QList<Annotation *> res;
1866 for (const std::unique_ptr<Annotation> &rev : d->revisions) {
1867 res.append(rev->d_ptr->makeAlias().release());
1868 }
1869 return res;
1870 }
1871
1872 /* If the annotation doesn't live in a object on its own (eg bug51361), it
1873 * has no ref, therefore it can't have revisions */
1874 if (!d->pdfAnnot->getHasRef()) {
1875 return QList<Annotation *>();
1876 }
1877
1878 return AnnotationPrivate::findAnnotations(d->pdfPage, d->parentDoc, QSet<Annotation::SubType>(), d->pdfAnnot->getId());
1879 }
1880
1881 std::unique_ptr<AnnotationAppearance> Annotation::annotationAppearance() const
1882 {
1883 Q_D(const Annotation);
1884
1885 return std::make_unique<AnnotationAppearance>(new AnnotationAppearancePrivate(d->pdfAnnot.get()));
1886 }
1887
1888 void Annotation::setAnnotationAppearance(const AnnotationAppearance &annotationAppearance)
1889 {
1890 Q_D(Annotation);
1891
1892 if (!d->pdfAnnot) {
1893 d->annotationAppearance = annotationAppearance.d->appearance.copy();
1894 return;
1895 }
1896
1897 // Moving the appearance object using std::move would result
1898 // in the object being completed moved from the AnnotationAppearancePrivate
1899 // class. So, we'll not be able to retrieve the stamp's original AP stream
1900 d->pdfAnnot->setNewAppearance(annotationAppearance.d->appearance.copy());
1901 }
1902
1903 // END Annotation implementation
1904
1905 /** TextAnnotation [Annotation] */
1906
1907 TextAnnotationPrivate::TextAnnotationPrivate() : textType(TextAnnotation::Linked), textIcon(QStringLiteral("Note")), inplaceAlign(0), inplaceIntent(TextAnnotation::Unknown) { }
1908
1909 std::unique_ptr<Annotation> TextAnnotationPrivate::makeAlias()
1910 {
1911 return std::unique_ptr<Annotation>(new TextAnnotation(*this));
1912 }
1913
1914 std::shared_ptr<Annot> TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
1915 {
1916 // Setters are defined in the public clas
1917 std::unique_ptr<TextAnnotation> q = static_pointer_cast<TextAnnotation>(makeAlias());
1918
1919 // Set page and contents
1920 pdfPage = destPage;
1921 parentDoc = doc;
1922
1923 // Set pdfAnnot
1924 PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
1925 if (textType == TextAnnotation::Linked) {
1926 pdfAnnot = std::make_shared<AnnotText>(destPage->getDoc(), &rect);
1927 } else {
1928 const double pointSize = textFont ? textFont->pointSizeF() : AnnotFreeText::undefinedFontPtSize;
1929 if (pointSize < 0) {
1930 qWarning() << "TextAnnotationPrivate::createNativeAnnot: font pointSize < 0";
1931 }
1932 pdfAnnot = std::make_shared<AnnotFreeText>(destPage->getDoc(), &rect);
1933 }
1934
1935 // Set properties
1936 flushBaseAnnotationProperties();
1937 q->setTextIcon(textIcon);
1938 q->setInplaceAlign(inplaceAlign);
1939 q->setCalloutPoints(inplaceCallout);
1940 q->setInplaceIntent(inplaceIntent);
1941
1942 inplaceCallout.clear(); // Free up memory
1943
1944 setDefaultAppearanceToNative();
1945
1946 return pdfAnnot;
1947 }
1948
1949 void TextAnnotationPrivate::setDefaultAppearanceToNative()
1950 {
1951 if (pdfAnnot && pdfAnnot->getType() == Annot::typeFreeText) {
1952 AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(pdfAnnot.get());
1953 const double pointSize = textFont ? textFont->pointSizeF() : AnnotFreeText::undefinedFontPtSize;
1954 if (pointSize < 0) {
1955 qWarning() << "TextAnnotationPrivate::createNativeAnnot: font pointSize < 0";
1956 }
1957 std::string fontName = "Invalid_font";
1958 if (textFont) {
1959 Form *form = pdfPage->getDoc()->getCatalog()->getCreateForm();
1960 if (form) {
1961 fontName = form->findFontInDefaultResources(textFont->family().toStdString(), textFont->styleName().toStdString());
1962 if (fontName.empty()) {
1963 fontName = form->addFontToDefaultResources(textFont->family().toStdString(), textFont->styleName().toStdString()).fontName;
1964 }
1965
1966 if (!fontName.empty()) {
1967 form->ensureFontsForAllCharacters(pdfAnnot->getContents(), fontName);
1968 } else {
1969 fontName = "Invalid_font";
1970 }
1971 }
1972 }
1973 DefaultAppearance da { { objName, fontName.c_str() }, pointSize, convertQColor(textColor) };
1974 ftextann->setDefaultAppearance(da);
1975 }
1976 }
1977
1978 std::unique_ptr<DefaultAppearance> TextAnnotationPrivate::getDefaultAppearanceFromNative() const
1979 {
1980 if (pdfAnnot && pdfAnnot->getType() == Annot::typeFreeText) {
1981 AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(pdfAnnot.get());
1982 return ftextann->getDefaultAppearance();
1983 } else {
1984 return {};
1985 }
1986 }
1987
1988 TextAnnotation::TextAnnotation(TextAnnotation::TextType type) : Annotation(*new TextAnnotationPrivate())
1989 {
1990 setTextType(type);
1991 }
1992
1993 TextAnnotation::TextAnnotation(TextAnnotationPrivate &dd) : Annotation(dd) { }
1994
1995 TextAnnotation::TextAnnotation(const QDomNode &node) : Annotation(*new TextAnnotationPrivate, node)
1996 {
1997 // loop through the whole children looking for a 'text' element
1998 QDomNode subNode = node.firstChild();
1999 while (subNode.isElement()) {
2000 QDomElement e = subNode.toElement();
2001 subNode = subNode.nextSibling();
2002 if (e.tagName() != QLatin1String("text")) {
2003 continue;
2004 }
2005
2006 // parse the attributes
2007 if (e.hasAttribute(QStringLiteral("type"))) {
2008 setTextType((TextAnnotation::TextType)e.attribute(QStringLiteral("type")).toInt());
2009 }
2010 if (e.hasAttribute(QStringLiteral("icon"))) {
2011 setTextIcon(e.attribute(QStringLiteral("icon")));
2012 }
2013 if (e.hasAttribute(QStringLiteral("font"))) {
2014 QFont font;
2015 font.fromString(e.attribute(QStringLiteral("font")));
2016 setTextFont(font);
2017 if (e.hasAttribute(QStringLiteral("fontColor"))) {
2018 const QColor color = QColor(e.attribute(QStringLiteral("fontColor")));
2019 setTextColor(color);
2020 }
2021 }
2022 if (e.hasAttribute(QStringLiteral("align"))) {
2023 setInplaceAlign(e.attribute(QStringLiteral("align")).toInt());
2024 }
2025 if (e.hasAttribute(QStringLiteral("intent"))) {
2026 setInplaceIntent((TextAnnotation::InplaceIntent)e.attribute(QStringLiteral("intent")).toInt());
2027 }
2028
2029 // parse the subnodes
2030 QDomNode eSubNode = e.firstChild();
2031 while (eSubNode.isElement()) {
2032 QDomElement ee = eSubNode.toElement();
2033 eSubNode = eSubNode.nextSibling();
2034
2035 if (ee.tagName() == QLatin1String("escapedText")) {
2036 setContents(ee.firstChild().toCDATASection().data());
2037 } else if (ee.tagName() == QLatin1String("callout")) {
2038 QVector<QPointF> points(3);
2039 points[0] = QPointF(ee.attribute(QStringLiteral("ax")).toDouble(), ee.attribute(QStringLiteral("ay")).toDouble());
2040 points[1] = QPointF(ee.attribute(QStringLiteral("bx")).toDouble(), ee.attribute(QStringLiteral("by")).toDouble());
2041 points[2] = QPointF(ee.attribute(QStringLiteral("cx")).toDouble(), ee.attribute(QStringLiteral("cy")).toDouble());
2042 setCalloutPoints(points);
2043 }
2044 }
2045
2046 // loading complete
2047 break;
2048 }
2049 }
2050
2051 TextAnnotation::~TextAnnotation() = default;
2052
2053 void TextAnnotation::store(QDomNode &node, QDomDocument &document) const
2054 {
2055 // store base annotation properties
2056 storeBaseAnnotationProperties(node, document);
2057
2058 // create [text] element
2059 QDomElement textElement = document.createElement(QStringLiteral("text"));
2060 node.appendChild(textElement);
2061
2062 // store the optional attributes
2063 if (textType() != Linked) {
2064 textElement.setAttribute(QStringLiteral("type"), (int)textType());
2065 }
2066 if (textIcon() != QLatin1String("Note")) {
2067 textElement.setAttribute(QStringLiteral("icon"), textIcon());
2068 }
2069 if (inplaceAlign()) {
2070 textElement.setAttribute(QStringLiteral("align"), inplaceAlign());
2071 }
2072 if (inplaceIntent() != Unknown) {
2073 textElement.setAttribute(QStringLiteral("intent"), (int)inplaceIntent());
2074 }
2075
2076 textElement.setAttribute(QStringLiteral("font"), textFont().toString());
2077 textElement.setAttribute(QStringLiteral("fontColor"), textColor().name());
2078
2079 // Sub-Node-1 - escapedText
2080 if (!contents().isEmpty()) {
2081 QDomElement escapedText = document.createElement(QStringLiteral("escapedText"));
2082 textElement.appendChild(escapedText);
2083 QDomCDATASection textCData = document.createCDATASection(contents());
2084 escapedText.appendChild(textCData);
2085 }
2086
2087 // Sub-Node-2 - callout
2088 if (calloutPoint(0).x() != 0.0) {
2089 QDomElement calloutElement = document.createElement(QStringLiteral("callout"));
2090 textElement.appendChild(calloutElement);
2091 calloutElement.setAttribute(QStringLiteral("ax"), QString::number(calloutPoint(0).x()));
2092 calloutElement.setAttribute(QStringLiteral("ay"), QString::number(calloutPoint(0).y()));
2093 calloutElement.setAttribute(QStringLiteral("bx"), QString::number(calloutPoint(1).x()));
2094 calloutElement.setAttribute(QStringLiteral("by"), QString::number(calloutPoint(1).y()));
2095 calloutElement.setAttribute(QStringLiteral("cx"), QString::number(calloutPoint(2).x()));
2096 calloutElement.setAttribute(QStringLiteral("cy"), QString::number(calloutPoint(2).y()));
2097 }
2098 }
2099
2100 Annotation::SubType TextAnnotation::subType() const
2101 {
2102 return AText;
2103 }
2104
2105 TextAnnotation::TextType TextAnnotation::textType() const
2106 {
2107 Q_D(const TextAnnotation);
2108
2109 if (!d->pdfAnnot) {
2110 return d->textType;
2111 }
2112
2113 return d->pdfAnnot->getType() == Annot::typeText ? TextAnnotation::Linked : TextAnnotation::InPlace;
2114 }
2115
2116 void TextAnnotation::setTextType(TextAnnotation::TextType type)
2117 {
2118 Q_D(TextAnnotation);
2119
2120 if (!d->pdfAnnot) {
2121 d->textType = type;
2122 return;
2123 }
2124
2125 // Type cannot be changed if annotation is already tied
2126 qWarning() << "You can't change the type of a TextAnnotation that is already in a page";
2127 }
2128
2129 QString TextAnnotation::textIcon() const
2130 {
2131 Q_D(const TextAnnotation);
2132
2133 if (!d->pdfAnnot) {
2134 return d->textIcon;
2135 }
2136
2137 if (d->pdfAnnot->getType() == Annot::typeText) {
2138 const AnnotText *textann = static_cast<const AnnotText *>(d->pdfAnnot.get());
2139 return QString::fromStdString(textann->getIcon());
2140 }
2141
2142 return QString();
2143 }
2144
2145 void TextAnnotation::setTextIcon(const QString &icon)
2146 {
2147 Q_D(TextAnnotation);
2148
2149 if (!d->pdfAnnot) {
2150 d->textIcon = icon;
2151 return;
2152 }
2153
2154 if (d->pdfAnnot->getType() == Annot::typeText) {
2155 AnnotText *textann = static_cast<AnnotText *>(d->pdfAnnot.get());
2156 textann->setIcon(icon.toStdString());
2157 }
2158 }
2159
2160 QFont TextAnnotation::textFont() const
2161 {
2162 Q_D(const TextAnnotation);
2163
2164 if (d->textFont) {
2165 return *d->textFont;
2166 }
2167
2168 double fontSize { AnnotFreeText::undefinedFontPtSize };
2169 if (d->pdfAnnot->getType() == Annot::typeFreeText) {
2170 std::unique_ptr<DefaultAppearance> da { d->getDefaultAppearanceFromNative() };
2171 if (da && da->getFontPtSize() > 0) {
2172 fontSize = da->getFontPtSize();
2173 }
2174 }
2175
2176 QFont font;
2177 font.setPointSizeF(fontSize);
2178 return font;
2179 }
2180
2181 void TextAnnotation::setTextFont(const QFont &font)
2182 {
2183 Q_D(TextAnnotation);
2184 if (font == d->textFont) {
2185 return;
2186 }
2187 d->textFont = font;
2188
2189 d->setDefaultAppearanceToNative();
2190 }
2191
2192 QColor TextAnnotation::textColor() const
2193 {
2194 Q_D(const TextAnnotation);
2195
2196 if (!d->pdfAnnot) {
2197 return d->textColor;
2198 }
2199
2200 if (std::unique_ptr<DefaultAppearance> da { d->getDefaultAppearanceFromNative() }) {
2201 return convertAnnotColor(da->getFontColor());
2202 }
2203
2204 return {};
2205 }
2206
2207 void TextAnnotation::setTextColor(const QColor &color)
2208 {
2209 Q_D(TextAnnotation);
2210 if (color == d->textColor) {
2211 return;
2212 }
2213 d->textColor = color;
2214
2215 d->setDefaultAppearanceToNative();
2216 }
2217
2218 int TextAnnotation::inplaceAlign() const
2219 {
2220 Q_D(const TextAnnotation);
2221
2222 if (!d->pdfAnnot) {
2223 return d->inplaceAlign;
2224 }
2225
2226 if (d->pdfAnnot->getType() == Annot::typeFreeText) {
2227 const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot.get());
2228 return static_cast<int>(ftextann->getQuadding());
2229 }
2230
2231 return 0;
2232 }
2233
2234 void TextAnnotation::setInplaceAlign(int align)
2235 {
2236 Q_D(TextAnnotation);
2237
2238 if (!d->pdfAnnot) {
2239 d->inplaceAlign = align;
2240 return;
2241 }
2242
2243 if (d->pdfAnnot->getType() == Annot::typeFreeText) {
2244 AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot.get());
2245 ftextann->setQuadding((VariableTextQuadding)align);
2246 }
2247 }
2248
2249 QPointF TextAnnotation::calloutPoint(int id) const
2250 {
2251 const QVector<QPointF> points = calloutPoints();
2252 if (id < 0 || id >= points.size()) {
2253 return QPointF();
2254 } else {
2255 return points[id];
2256 }
2257 }
2258
2259 QVector<QPointF> TextAnnotation::calloutPoints() const
2260 {
2261 Q_D(const TextAnnotation);
2262
2263 if (!d->pdfAnnot) {
2264 return d->inplaceCallout;
2265 }
2266
2267 if (d->pdfAnnot->getType() == Annot::typeText) {
2268 return QVector<QPointF>();
2269 }
2270
2271 const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot.get());
2272 const AnnotCalloutLine *callout = ftextann->getCalloutLine();
2273
2274 if (!callout) {
2275 return QVector<QPointF>();
2276 }
2277
2278 double MTX[6];
2279 d->fillTransformationMTX(MTX);
2280
2281 const AnnotCalloutMultiLine *callout_v6 = dynamic_cast<const AnnotCalloutMultiLine *>(callout);
2282 QVector<QPointF> res(callout_v6 ? 3 : 2);
2283 XPDFReader::transform(MTX, callout->getX1(), callout->getY1(), res[0]);
2284 XPDFReader::transform(MTX, callout->getX2(), callout->getY2(), res[1]);
2285 if (callout_v6) {
2286 XPDFReader::transform(MTX, callout_v6->getX3(), callout_v6->getY3(), res[2]);
2287 }
2288 return res;
2289 }
2290
2291 void TextAnnotation::setCalloutPoints(const QVector<QPointF> &points)
2292 {
2293 Q_D(TextAnnotation);
2294 if (!d->pdfAnnot) {
2295 d->inplaceCallout = points;
2296 return;
2297 }
2298
2299 if (d->pdfAnnot->getType() != Annot::typeFreeText) {
2300 return;
2301 }
2302
2303 AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot.get());
2304 const int count = points.size();
2305
2306 if (count == 0) {
2307 ftextann->setCalloutLine(nullptr);
2308 return;
2309 }
2310
2311 if (count != 2 && count != 3) {
2312 error(errSyntaxError, -1, "Expected zero, two or three points for callout");
2313 return;
2314 }
2315
2316 std::unique_ptr<AnnotCalloutLine> callout;
2317 double x1, y1, x2, y2;
2318 double MTX[6];
2319 d->fillTransformationMTX(MTX);
2320
2321 XPDFReader::invTransform(MTX, points[0], x1, y1);
2322 XPDFReader::invTransform(MTX, points[1], x2, y2);
2323 if (count == 3) {
2324 double x3, y3;
2325 XPDFReader::invTransform(MTX, points[2], x3, y3);
2326 callout = std::make_unique<AnnotCalloutMultiLine>(x1, y1, x2, y2, x3, y3);
2327 } else {
2328 callout = std::make_unique<AnnotCalloutLine>(x1, y1, x2, y2);
2329 }
2330
2331 ftextann->setCalloutLine(std::move(callout));
2332 }
2333
2334 TextAnnotation::InplaceIntent TextAnnotation::inplaceIntent() const
2335 {
2336 Q_D(const TextAnnotation);
2337
2338 if (!d->pdfAnnot) {
2339 return d->inplaceIntent;
2340 }
2341
2342 if (d->pdfAnnot->getType() == Annot::typeFreeText) {
2343 const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot.get());
2344 return (TextAnnotation::InplaceIntent)ftextann->getIntent();
2345 }
2346
2347 return TextAnnotation::Unknown;
2348 }
2349
2350 void TextAnnotation::setInplaceIntent(TextAnnotation::InplaceIntent intent)
2351 {
2352 Q_D(TextAnnotation);
2353
2354 if (!d->pdfAnnot) {
2355 d->inplaceIntent = intent;
2356 return;
2357 }
2358
2359 if (d->pdfAnnot->getType() == Annot::typeFreeText) {
2360 AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot.get());
2361 ftextann->setIntent((AnnotFreeText::AnnotFreeTextIntent)intent);
2362 }
2363 }
2364
2365 /** LineAnnotation [Annotation] */
2366 class LineAnnotationPrivate : public AnnotationPrivate
2367 {
2368 public:
2369 LineAnnotationPrivate();
2370 std::unique_ptr<Annotation> makeAlias() override;
2371 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
2372
2373 // data fields (note uses border for rendering style)
2374 QLinkedList<QPointF> linePoints;
2375 LineAnnotation::TermStyle lineStartStyle;
2376 LineAnnotation::TermStyle lineEndStyle;
2377 bool lineClosed : 1; // (if true draw close shape)
2378 bool lineShowCaption : 1;
2379 LineAnnotation::LineType lineType;
2380 QColor lineInnerColor;
2381 double lineLeadingFwdPt;
2382 double lineLeadingBackPt;
2383 LineAnnotation::LineIntent lineIntent;
2384 };
2385
2386 LineAnnotationPrivate::LineAnnotationPrivate()
2387 : lineStartStyle(LineAnnotation::None), lineEndStyle(LineAnnotation::None), lineClosed(false), lineShowCaption(false), lineLeadingFwdPt(0), lineLeadingBackPt(0), lineIntent(LineAnnotation::Unknown)
2388 {
2389 }
2390
2391 std::unique_ptr<Annotation> LineAnnotationPrivate::makeAlias()
2392 {
2393 return std::unique_ptr<LineAnnotation>(new LineAnnotation(*this));
2394 }
2395
2396 std::shared_ptr<Annot> LineAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
2397 {
2398 // Setters are defined in the public class
2399 std::unique_ptr<LineAnnotation> q = static_pointer_cast<LineAnnotation>(makeAlias());
2400
2401 // Set page and document
2402 pdfPage = destPage;
2403 parentDoc = doc;
2404
2405 // Set pdfAnnot
2406 PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
2407 if (lineType == LineAnnotation::StraightLine) {
2408 pdfAnnot = std::make_shared<AnnotLine>(doc->doc, &rect);
2409 } else {
2410 pdfAnnot = std::make_shared<AnnotPolygon>(doc->doc, &rect, lineClosed ? Annot::typePolygon : Annot::typePolyLine);
2411 }
2412
2413 // Set properties
2414 flushBaseAnnotationProperties();
2415 q->setLinePoints(linePoints);
2416 q->setLineStartStyle(lineStartStyle);
2417 q->setLineEndStyle(lineEndStyle);
2418 q->setLineInnerColor(lineInnerColor);
2419 q->setLineLeadingForwardPoint(lineLeadingFwdPt);
2420 q->setLineLeadingBackPoint(lineLeadingBackPt);
2421 q->setLineShowCaption(lineShowCaption);
2422 q->setLineIntent(lineIntent);
2423
2424 linePoints.clear(); // Free up memory
2425
2426 return pdfAnnot;
2427 }
2428
2429 LineAnnotation::LineAnnotation(LineAnnotation::LineType type) : Annotation(*new LineAnnotationPrivate())
2430 {
2431 setLineType(type);
2432 }
2433
2434 LineAnnotation::LineAnnotation(LineAnnotationPrivate &dd) : Annotation(dd) { }
2435
2436 LineAnnotation::LineAnnotation(const QDomNode &node) : Annotation(*new LineAnnotationPrivate(), node)
2437 {
2438 // loop through the whole children looking for a 'line' element
2439 QDomNode subNode = node.firstChild();
2440 while (subNode.isElement()) {
2441 QDomElement e = subNode.toElement();
2442 subNode = subNode.nextSibling();
2443 if (e.tagName() != QLatin1String("line")) {
2444 continue;
2445 }
2446
2447 // parse the attributes
2448 if (e.hasAttribute(QStringLiteral("startStyle"))) {
2449 setLineStartStyle((LineAnnotation::TermStyle)e.attribute(QStringLiteral("startStyle")).toInt());
2450 }
2451 if (e.hasAttribute(QStringLiteral("endStyle"))) {
2452 setLineEndStyle((LineAnnotation::TermStyle)e.attribute(QStringLiteral("endStyle")).toInt());
2453 }
2454 if (e.hasAttribute(QStringLiteral("closed"))) {
2455 setLineClosed(e.attribute(QStringLiteral("closed")).toInt());
2456 }
2457 if (e.hasAttribute(QStringLiteral("innerColor"))) {
2458 setLineInnerColor(QColor(e.attribute(QStringLiteral("innerColor"))));
2459 }
2460 if (e.hasAttribute(QStringLiteral("leadFwd"))) {
2461 setLineLeadingForwardPoint(e.attribute(QStringLiteral("leadFwd")).toDouble());
2462 }
2463 if (e.hasAttribute(QStringLiteral("leadBack"))) {
2464 setLineLeadingBackPoint(e.attribute(QStringLiteral("leadBack")).toDouble());
2465 }
2466 if (e.hasAttribute(QStringLiteral("showCaption"))) {
2467 setLineShowCaption(e.attribute(QStringLiteral("showCaption")).toInt());
2468 }
2469 if (e.hasAttribute(QStringLiteral("intent"))) {
2470 setLineIntent((LineAnnotation::LineIntent)e.attribute(QStringLiteral("intent")).toInt());
2471 }
2472
2473 // parse all 'point' subnodes
2474 QLinkedList<QPointF> points;
2475 QDomNode pointNode = e.firstChild();
2476 while (pointNode.isElement()) {
2477 QDomElement pe = pointNode.toElement();
2478 pointNode = pointNode.nextSibling();
2479
2480 if (pe.tagName() != QLatin1String("point")) {
2481 continue;
2482 }
2483
2484 QPointF p(pe.attribute(QStringLiteral("x"), QStringLiteral("0.0")).toDouble(), pe.attribute(QStringLiteral("y"), QStringLiteral("0.0")).toDouble());
2485 points.append(p);
2486 }
2487 setLinePoints(points);
2488 setLineType(points.size() == 2 ? StraightLine : Polyline);
2489
2490 // loading complete
2491 break;
2492 }
2493 }
2494
2495 LineAnnotation::~LineAnnotation() = default;
2496
2497 void LineAnnotation::store(QDomNode &node, QDomDocument &document) const
2498 {
2499 // store base annotation properties
2500 storeBaseAnnotationProperties(node, document);
2501
2502 // create [line] element
2503 QDomElement lineElement = document.createElement(QStringLiteral("line"));
2504 node.appendChild(lineElement);
2505
2506 // store the attributes
2507 if (lineStartStyle() != None) {
2508 lineElement.setAttribute(QStringLiteral("startStyle"), (int)lineStartStyle());
2509 }
2510 if (lineEndStyle() != None) {
2511 lineElement.setAttribute(QStringLiteral("endStyle"), (int)lineEndStyle());
2512 }
2513 if (isLineClosed()) {
2514 lineElement.setAttribute(QStringLiteral("closed"), isLineClosed());
2515 }
2516 if (lineInnerColor().isValid()) {
2517 lineElement.setAttribute(QStringLiteral("innerColor"), lineInnerColor().name());
2518 }
2519 if (lineLeadingForwardPoint() != 0.0) {
2520 lineElement.setAttribute(QStringLiteral("leadFwd"), QString::number(lineLeadingForwardPoint()));
2521 }
2522 if (lineLeadingBackPoint() != 0.0) {
2523 lineElement.setAttribute(QStringLiteral("leadBack"), QString::number(lineLeadingBackPoint()));
2524 }
2525 if (lineShowCaption()) {
2526 lineElement.setAttribute(QStringLiteral("showCaption"), lineShowCaption());
2527 }
2528 if (lineIntent() != Unknown) {
2529 lineElement.setAttribute(QStringLiteral("intent"), lineIntent());
2530 }
2531
2532 // append the list of points
2533 const QLinkedList<QPointF> points = linePoints();
2534 if (points.count() > 1) {
2535 QLinkedList<QPointF>::const_iterator it = points.begin(), end = points.end();
2536 while (it != end) {
2537 const QPointF &p = *it;
2538 QDomElement pElement = document.createElement(QStringLiteral("point"));
2539 lineElement.appendChild(pElement);
2540 pElement.setAttribute(QStringLiteral("x"), QString::number(p.x()));
2541 pElement.setAttribute(QStringLiteral("y"), QString::number(p.y()));
2542 ++it;
2543 }
2544 }
2545 }
2546
2547 Annotation::SubType LineAnnotation::subType() const
2548 {
2549 return ALine;
2550 }
2551
2552 LineAnnotation::LineType LineAnnotation::lineType() const
2553 {
2554 Q_D(const LineAnnotation);
2555
2556 if (!d->pdfAnnot) {
2557 return d->lineType;
2558 }
2559
2560 return (d->pdfAnnot->getType() == Annot::typeLine) ? LineAnnotation::StraightLine : LineAnnotation::Polyline;
2561 }
2562
2563 void LineAnnotation::setLineType(LineAnnotation::LineType type)
2564 {
2565 Q_D(LineAnnotation);
2566
2567 if (!d->pdfAnnot) {
2568 d->lineType = type;
2569 return;
2570 }
2571
2572 // Type cannot be changed if annotation is already tied
2573 qWarning() << "You can't change the type of a LineAnnotation that is already in a page";
2574 }
2575
2576 QLinkedList<QPointF> LineAnnotation::linePoints() const
2577 {
2578 Q_D(const LineAnnotation);
2579
2580 if (!d->pdfAnnot) {
2581 return d->linePoints;
2582 }
2583
2584 double MTX[6];
2585 d->fillTransformationMTX(MTX);
2586
2587 QLinkedList<QPointF> res;
2588 if (d->pdfAnnot->getType() == Annot::typeLine) {
2589 const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
2590 QPointF p;
2591 XPDFReader::transform(MTX, lineann->getX1(), lineann->getY1(), p);
2592 res.append(p);
2593 XPDFReader::transform(MTX, lineann->getX2(), lineann->getY2(), p);
2594 res.append(p);
2595 } else {
2596 const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
2597 const AnnotPath *vertices = polyann->getVertices();
2598
2599 for (int i = 0; i < vertices->getCoordsLength(); ++i) {
2600 QPointF p;
2601 XPDFReader::transform(MTX, vertices->getX(i), vertices->getY(i), p);
2602 res.append(p);
2603 }
2604 }
2605
2606 return res;
2607 }
2608
2609 void LineAnnotation::setLinePoints(const QLinkedList<QPointF> &points)
2610 {
2611 Q_D(LineAnnotation);
2612
2613 if (!d->pdfAnnot) {
2614 d->linePoints = points;
2615 return;
2616 }
2617
2618 if (d->pdfAnnot->getType() == Annot::typeLine) {
2619 AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
2620 if (points.size() != 2) {
2621 error(errSyntaxError, -1, "Expected two points for a straight line");
2622 return;
2623 }
2624 double x1, y1, x2, y2;
2625 double MTX[6];
2626 d->fillTransformationMTX(MTX);
2627 XPDFReader::invTransform(MTX, points.first(), x1, y1);
2628 XPDFReader::invTransform(MTX, points.last(), x2, y2);
2629 lineann->setVertices(x1, y1, x2, y2);
2630 } else {
2631 AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
2632 const std::unique_ptr<AnnotPath> p = d->toAnnotPath(points);
2633 polyann->setVertices(*p);
2634 }
2635 }
2636
2637 LineAnnotation::TermStyle LineAnnotation::lineStartStyle() const
2638 {
2639 Q_D(const LineAnnotation);
2640
2641 if (!d->pdfAnnot) {
2642 return d->lineStartStyle;
2643 }
2644
2645 if (d->pdfAnnot->getType() == Annot::typeLine) {
2646 const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
2647 return (LineAnnotation::TermStyle)lineann->getStartStyle();
2648 } else {
2649 const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
2650 return (LineAnnotation::TermStyle)polyann->getStartStyle();
2651 }
2652 }
2653
2654 void LineAnnotation::setLineStartStyle(LineAnnotation::TermStyle style)
2655 {
2656 Q_D(LineAnnotation);
2657
2658 if (!d->pdfAnnot) {
2659 d->lineStartStyle = style;
2660 return;
2661 }
2662
2663 if (d->pdfAnnot->getType() == Annot::typeLine) {
2664 AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
2665 lineann->setStartEndStyle((AnnotLineEndingStyle)style, lineann->getEndStyle());
2666 } else {
2667 AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
2668 polyann->setStartEndStyle((AnnotLineEndingStyle)style, polyann->getEndStyle());
2669 }
2670 }
2671
2672 LineAnnotation::TermStyle LineAnnotation::lineEndStyle() const
2673 {
2674 Q_D(const LineAnnotation);
2675
2676 if (!d->pdfAnnot) {
2677 return d->lineEndStyle;
2678 }
2679
2680 if (d->pdfAnnot->getType() == Annot::typeLine) {
2681 const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
2682 return (LineAnnotation::TermStyle)lineann->getEndStyle();
2683 } else {
2684 const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
2685 return (LineAnnotation::TermStyle)polyann->getEndStyle();
2686 }
2687 }
2688
2689 void LineAnnotation::setLineEndStyle(LineAnnotation::TermStyle style)
2690 {
2691 Q_D(LineAnnotation);
2692
2693 if (!d->pdfAnnot) {
2694 d->lineEndStyle = style;
2695 return;
2696 }
2697
2698 if (d->pdfAnnot->getType() == Annot::typeLine) {
2699 AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
2700 lineann->setStartEndStyle(lineann->getStartStyle(), (AnnotLineEndingStyle)style);
2701 } else {
2702 AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
2703 polyann->setStartEndStyle(polyann->getStartStyle(), (AnnotLineEndingStyle)style);
2704 }
2705 }
2706
2707 bool LineAnnotation::isLineClosed() const
2708 {
2709 Q_D(const LineAnnotation);
2710
2711 if (!d->pdfAnnot) {
2712 return d->lineClosed;
2713 }
2714
2715 return d->pdfAnnot->getType() == Annot::typePolygon;
2716 }
2717
2718 void LineAnnotation::setLineClosed(bool closed)
2719 {
2720 Q_D(LineAnnotation);
2721
2722 if (!d->pdfAnnot) {
2723 d->lineClosed = closed;
2724 return;
2725 }
2726
2727 if (d->pdfAnnot->getType() != Annot::typeLine) {
2728 AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
2729
2730 // Set new subtype and switch intent if necessary
2731 if (closed) {
2732 polyann->setType(Annot::typePolygon);
2733 if (polyann->getIntent() == AnnotPolygon::polylineDimension) {
2734 polyann->setIntent(AnnotPolygon::polygonDimension);
2735 }
2736 } else {
2737 polyann->setType(Annot::typePolyLine);
2738 if (polyann->getIntent() == AnnotPolygon::polygonDimension) {
2739 polyann->setIntent(AnnotPolygon::polylineDimension);
2740 }
2741 }
2742 }
2743 }
2744
2745 QColor LineAnnotation::lineInnerColor() const
2746 {
2747 Q_D(const LineAnnotation);
2748
2749 if (!d->pdfAnnot) {
2750 return d->lineInnerColor;
2751 }
2752
2753 AnnotColor *c;
2754
2755 if (d->pdfAnnot->getType() == Annot::typeLine) {
2756 const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
2757 c = lineann->getInteriorColor();
2758 } else {
2759 const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
2760 c = polyann->getInteriorColor();
2761 }
2762
2763 return convertAnnotColor(c);
2764 }
2765
2766 void LineAnnotation::setLineInnerColor(const QColor &color)
2767 {
2768 Q_D(LineAnnotation);
2769
2770 if (!d->pdfAnnot) {
2771 d->lineInnerColor = color;
2772 return;
2773 }
2774
2775 auto c = convertQColor(color);
2776
2777 if (d->pdfAnnot->getType() == Annot::typeLine) {
2778 AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
2779 lineann->setInteriorColor(std::move(c));
2780 } else {
2781 AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
2782 polyann->setInteriorColor(std::move(c));
2783 }
2784 }
2785
2786 double LineAnnotation::lineLeadingForwardPoint() const
2787 {
2788 Q_D(const LineAnnotation);
2789
2790 if (!d->pdfAnnot) {
2791 return d->lineLeadingFwdPt;
2792 }
2793
2794 if (d->pdfAnnot->getType() == Annot::typeLine) {
2795 const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
2796 return lineann->getLeaderLineLength();
2797 }
2798
2799 return 0;
2800 }
2801
2802 void LineAnnotation::setLineLeadingForwardPoint(double point)
2803 {
2804 Q_D(LineAnnotation);
2805
2806 if (!d->pdfAnnot) {
2807 d->lineLeadingFwdPt = point;
2808 return;
2809 }
2810
2811 if (d->pdfAnnot->getType() == Annot::typeLine) {
2812 AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
2813 lineann->setLeaderLineLength(point);
2814 }
2815 }
2816
2817 double LineAnnotation::lineLeadingBackPoint() const
2818 {
2819 Q_D(const LineAnnotation);
2820
2821 if (!d->pdfAnnot) {
2822 return d->lineLeadingBackPt;
2823 }
2824
2825 if (d->pdfAnnot->getType() == Annot::typeLine) {
2826 const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
2827 return lineann->getLeaderLineExtension();
2828 }
2829
2830 return 0;
2831 }
2832
2833 void LineAnnotation::setLineLeadingBackPoint(double point)
2834 {
2835 Q_D(LineAnnotation);
2836
2837 if (!d->pdfAnnot) {
2838 d->lineLeadingBackPt = point;
2839 return;
2840 }
2841
2842 if (d->pdfAnnot->getType() == Annot::typeLine) {
2843 AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
2844 lineann->setLeaderLineExtension(point);
2845 }
2846 }
2847
2848 bool LineAnnotation::lineShowCaption() const
2849 {
2850 Q_D(const LineAnnotation);
2851
2852 if (!d->pdfAnnot) {
2853 return d->lineShowCaption;
2854 }
2855
2856 if (d->pdfAnnot->getType() == Annot::typeLine) {
2857 const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
2858 return lineann->getCaption();
2859 }
2860
2861 return false;
2862 }
2863
2864 void LineAnnotation::setLineShowCaption(bool show)
2865 {
2866 Q_D(LineAnnotation);
2867
2868 if (!d->pdfAnnot) {
2869 d->lineShowCaption = show;
2870 return;
2871 }
2872
2873 if (d->pdfAnnot->getType() == Annot::typeLine) {
2874 AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
2875 lineann->setCaption(show);
2876 }
2877 }
2878
2879 LineAnnotation::LineIntent LineAnnotation::lineIntent() const
2880 {
2881 Q_D(const LineAnnotation);
2882
2883 if (!d->pdfAnnot) {
2884 return d->lineIntent;
2885 }
2886
2887 if (d->pdfAnnot->getType() == Annot::typeLine) {
2888 const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
2889 return (LineAnnotation::LineIntent)(lineann->getIntent() + 1);
2890 } else {
2891 const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
2892 if (polyann->getIntent() == AnnotPolygon::polygonCloud) {
2893 return LineAnnotation::PolygonCloud;
2894 } else { // AnnotPolygon::polylineDimension, AnnotPolygon::polygonDimension
2895 return LineAnnotation::Dimension;
2896 }
2897 }
2898 }
2899
2900 void LineAnnotation::setLineIntent(LineAnnotation::LineIntent intent)
2901 {
2902 Q_D(LineAnnotation);
2903
2904 if (!d->pdfAnnot) {
2905 d->lineIntent = intent;
2906 return;
2907 }
2908
2909 if (intent == LineAnnotation::Unknown) {
2910 return; // Do not set (actually, it should clear the property)
2911 }
2912
2913 if (d->pdfAnnot->getType() == Annot::typeLine) {
2914 AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
2915 lineann->setIntent((AnnotLine::AnnotLineIntent)(intent - 1));
2916 } else {
2917 AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
2918 if (intent == LineAnnotation::PolygonCloud) {
2919 polyann->setIntent(AnnotPolygon::polygonCloud);
2920 } else // LineAnnotation::Dimension
2921 {
2922 if (d->pdfAnnot->getType() == Annot::typePolygon) {
2923 polyann->setIntent(AnnotPolygon::polygonDimension);
2924 } else { // Annot::typePolyLine
2925 polyann->setIntent(AnnotPolygon::polylineDimension);
2926 }
2927 }
2928 }
2929 }
2930
2931 /** GeomAnnotation [Annotation] */
2932 class GeomAnnotationPrivate : public AnnotationPrivate
2933 {
2934 public:
2935 GeomAnnotationPrivate();
2936 std::unique_ptr<Annotation> makeAlias() override;
2937 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
2938
2939 // data fields (note uses border for rendering style)
2940 GeomAnnotation::GeomType geomType;
2941 QColor geomInnerColor;
2942 };
2943
2944 GeomAnnotationPrivate::GeomAnnotationPrivate() : geomType(GeomAnnotation::InscribedSquare) { }
2945
2946 std::unique_ptr<Annotation> GeomAnnotationPrivate::makeAlias()
2947 {
2948 return std::unique_ptr<GeomAnnotation>(new GeomAnnotation(*this));
2949 }
2950
2951 std::shared_ptr<Annot> GeomAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
2952 {
2953 // Setters are defined in the public class
2954 std::unique_ptr<GeomAnnotation> q = static_pointer_cast<GeomAnnotation>(makeAlias());
2955
2956 // Set page and document
2957 pdfPage = destPage;
2958 parentDoc = doc;
2959
2960 Annot::AnnotSubtype type;
2961 if (geomType == GeomAnnotation::InscribedSquare) {
2962 type = Annot::typeSquare;
2963 } else { // GeomAnnotation::InscribedCircle
2964 type = Annot::typeCircle;
2965 }
2966
2967 // Set pdfAnnot
2968 PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
2969 pdfAnnot = std::make_shared<AnnotGeometry>(destPage->getDoc(), &rect, type);
2970
2971 // Set properties
2972 flushBaseAnnotationProperties();
2973 q->setGeomInnerColor(geomInnerColor);
2974
2975 return pdfAnnot;
2976 }
2977
2978 GeomAnnotation::GeomAnnotation() : Annotation(*new GeomAnnotationPrivate()) { }
2979
2980 GeomAnnotation::GeomAnnotation(GeomAnnotationPrivate &dd) : Annotation(dd) { }
2981
2982 GeomAnnotation::GeomAnnotation(const QDomNode &node) : Annotation(*new GeomAnnotationPrivate(), node)
2983 {
2984 // loop through the whole children looking for a 'geom' element
2985 QDomNode subNode = node.firstChild();
2986 while (subNode.isElement()) {
2987 QDomElement e = subNode.toElement();
2988 subNode = subNode.nextSibling();
2989 if (e.tagName() != QLatin1String("geom")) {
2990 continue;
2991 }
2992
2993 // parse the attributes
2994 if (e.hasAttribute(QStringLiteral("type"))) {
2995 setGeomType((GeomAnnotation::GeomType)e.attribute(QStringLiteral("type")).toInt());
2996 }
2997 if (e.hasAttribute(QStringLiteral("color"))) {
2998 setGeomInnerColor(QColor(e.attribute(QStringLiteral("color"))));
2999 }
3000
3001 // loading complete
3002 break;
3003 }
3004 }
3005
3006 GeomAnnotation::~GeomAnnotation() = default;
3007
3008 void GeomAnnotation::store(QDomNode &node, QDomDocument &document) const
3009 {
3010 // store base annotation properties
3011 storeBaseAnnotationProperties(node, document);
3012
3013 // create [geom] element
3014 QDomElement geomElement = document.createElement(QStringLiteral("geom"));
3015 node.appendChild(geomElement);
3016
3017 // append the optional attributes
3018 if (geomType() != InscribedSquare) {
3019 geomElement.setAttribute(QStringLiteral("type"), (int)geomType());
3020 }
3021 if (geomInnerColor().isValid()) {
3022 geomElement.setAttribute(QStringLiteral("color"), geomInnerColor().name());
3023 }
3024 }
3025
3026 Annotation::SubType GeomAnnotation::subType() const
3027 {
3028 return AGeom;
3029 }
3030
3031 GeomAnnotation::GeomType GeomAnnotation::geomType() const
3032 {
3033 Q_D(const GeomAnnotation);
3034
3035 if (!d->pdfAnnot) {
3036 return d->geomType;
3037 }
3038
3039 if (d->pdfAnnot->getType() == Annot::typeSquare) {
3040 return GeomAnnotation::InscribedSquare;
3041 } else { // Annot::typeCircle
3042 return GeomAnnotation::InscribedCircle;
3043 }
3044 }
3045
3046 void GeomAnnotation::setGeomType(GeomAnnotation::GeomType type)
3047 {
3048 Q_D(GeomAnnotation);
3049
3050 if (!d->pdfAnnot) {
3051 d->geomType = type;
3052 return;
3053 }
3054
3055 AnnotGeometry *geomann = static_cast<AnnotGeometry *>(d->pdfAnnot.get());
3056 if (type == GeomAnnotation::InscribedSquare) {
3057 geomann->setType(Annot::typeSquare);
3058 } else { // GeomAnnotation::InscribedCircle
3059 geomann->setType(Annot::typeCircle);
3060 }
3061 }
3062
3063 QColor GeomAnnotation::geomInnerColor() const
3064 {
3065 Q_D(const GeomAnnotation);
3066
3067 if (!d->pdfAnnot) {
3068 return d->geomInnerColor;
3069 }
3070
3071 const AnnotGeometry *geomann = static_cast<const AnnotGeometry *>(d->pdfAnnot.get());
3072 return convertAnnotColor(geomann->getInteriorColor());
3073 }
3074
3075 void GeomAnnotation::setGeomInnerColor(const QColor &color)
3076 {
3077 Q_D(GeomAnnotation);
3078
3079 if (!d->pdfAnnot) {
3080 d->geomInnerColor = color;
3081 return;
3082 }
3083
3084 AnnotGeometry *geomann = static_cast<AnnotGeometry *>(d->pdfAnnot.get());
3085 geomann->setInteriorColor(convertQColor(color));
3086 }
3087
3088 /** HighlightAnnotation [Annotation] */
3089 class HighlightAnnotationPrivate : public AnnotationPrivate
3090 {
3091 public:
3092 HighlightAnnotationPrivate();
3093 std::unique_ptr<Annotation> makeAlias() override;
3094 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
3095
3096 // data fields
3097 HighlightAnnotation::HighlightType highlightType;
3098 QList<HighlightAnnotation::Quad> highlightQuads; // not empty
3099
3100 // helpers
3101 static Annot::AnnotSubtype toAnnotSubType(HighlightAnnotation::HighlightType type);
3102 QList<HighlightAnnotation::Quad> fromQuadrilaterals(AnnotQuadrilaterals *quads) const;
3103 AnnotQuadrilaterals *toQuadrilaterals(const QList<HighlightAnnotation::Quad> &quads) const;
3104 };
3105
3106 HighlightAnnotationPrivate::HighlightAnnotationPrivate() : highlightType(HighlightAnnotation::Highlight) { }
3107
3108 std::unique_ptr<Annotation> HighlightAnnotationPrivate::makeAlias()
3109 {
3110 return std::unique_ptr<HighlightAnnotation>(new HighlightAnnotation(*this));
3111 }
3112
3113 Annot::AnnotSubtype HighlightAnnotationPrivate::toAnnotSubType(HighlightAnnotation::HighlightType type)
3114 {
3115 switch (type) {
3116 default: // HighlightAnnotation::Highlight:
3117 return Annot::typeHighlight;
3118 case HighlightAnnotation::Underline:
3119 return Annot::typeUnderline;
3120 case HighlightAnnotation::Squiggly:
3121 return Annot::typeSquiggly;
3122 case HighlightAnnotation::StrikeOut:
3123 return Annot::typeStrikeOut;
3124 }
3125 }
3126
3127 QList<HighlightAnnotation::Quad> HighlightAnnotationPrivate::fromQuadrilaterals(AnnotQuadrilaterals *hlquads) const
3128 {
3129 QList<HighlightAnnotation::Quad> quads;
3130
3131 if (!hlquads || !hlquads->getQuadrilateralsLength()) {
3132 return quads;
3133 }
3134 const int quadsCount = hlquads->getQuadrilateralsLength();
3135
3136 double MTX[6];
3137 fillTransformationMTX(MTX);
3138
3139 quads.reserve(quadsCount);
3140 for (int q = 0; q < quadsCount; ++q) {
3141 HighlightAnnotation::Quad quad;
3142 XPDFReader::transform(MTX, hlquads->getX1(q), hlquads->getY1(q), quad.points[0]);
3143 XPDFReader::transform(MTX, hlquads->getX2(q), hlquads->getY2(q), quad.points[1]);
3144 XPDFReader::transform(MTX, hlquads->getX3(q), hlquads->getY3(q), quad.points[2]);
3145 XPDFReader::transform(MTX, hlquads->getX4(q), hlquads->getY4(q), quad.points[3]);
3146 // ### PDF1.6 specs says that point are in ccw order, but in fact
3147 // points 3 and 4 are swapped in every PDF around!
3148 QPointF tmpPoint = quad.points[2];
3149 quad.points[2] = quad.points[3];
3150 quad.points[3] = tmpPoint;
3151 // initialize other properties and append quad
3152 quad.capStart = true; // unlinked quads are always capped
3153 quad.capEnd = true; // unlinked quads are always capped
3154 quad.feather = 0.1; // default feather
3155 quads.append(quad);
3156 }
3157
3158 return quads;
3159 }
3160
3161 AnnotQuadrilaterals *HighlightAnnotationPrivate::toQuadrilaterals(const QList<HighlightAnnotation::Quad> &quads) const
3162 {
3163 const int count = quads.size();
3164 auto ac = std::make_unique<AnnotQuadrilaterals::AnnotQuadrilateral[]>(count);
3165
3166 double MTX[6];
3167 fillTransformationMTX(MTX);
3168
3169 int pos = 0;
3170 Q_FOREACH (const HighlightAnnotation::Quad &q, quads) {
3171 double x1, y1, x2, y2, x3, y3, x4, y4;
3172 XPDFReader::invTransform(MTX, q.points[0], x1, y1);
3173 XPDFReader::invTransform(MTX, q.points[1], x2, y2);
3174 // Swap points 3 and 4 (see HighlightAnnotationPrivate::fromQuadrilaterals)
3175 XPDFReader::invTransform(MTX, q.points[3], x3, y3);
3176 XPDFReader::invTransform(MTX, q.points[2], x4, y4);
3177 ac[pos++] = AnnotQuadrilaterals::AnnotQuadrilateral(x1, y1, x2, y2, x3, y3, x4, y4);
3178 }
3179
3180 return new AnnotQuadrilaterals(std::move(ac), count);
3181 }
3182
3183 std::shared_ptr<Annot> HighlightAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
3184 {
3185 // Setters are defined in the public class
3186 std::unique_ptr<HighlightAnnotation> q = static_pointer_cast<HighlightAnnotation>(makeAlias());
3187
3188 // Set page and document
3189 pdfPage = destPage;
3190 parentDoc = doc;
3191
3192 // Set pdfAnnot
3193 PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
3194 pdfAnnot = std::make_shared<AnnotTextMarkup>(destPage->getDoc(), &rect, toAnnotSubType(highlightType));
3195
3196 // Set properties
3197 flushBaseAnnotationProperties();
3198 q->setHighlightQuads(highlightQuads);
3199
3200 highlightQuads.clear(); // Free up memory
3201
3202 return pdfAnnot;
3203 }
3204
3205 HighlightAnnotation::HighlightAnnotation() : Annotation(*new HighlightAnnotationPrivate()) { }
3206
3207 HighlightAnnotation::HighlightAnnotation(HighlightAnnotationPrivate &dd) : Annotation(dd) { }
3208
3209 HighlightAnnotation::HighlightAnnotation(const QDomNode &node) : Annotation(*new HighlightAnnotationPrivate(), node)
3210 {
3211 // loop through the whole children looking for a 'hl' element
3212 QDomNode subNode = node.firstChild();
3213 while (subNode.isElement()) {
3214 QDomElement e = subNode.toElement();
3215 subNode = subNode.nextSibling();
3216 if (e.tagName() != QLatin1String("hl")) {
3217 continue;
3218 }
3219
3220 // parse the attributes
3221 if (e.hasAttribute(QStringLiteral("type"))) {
3222 setHighlightType((HighlightAnnotation::HighlightType)e.attribute(QStringLiteral("type")).toInt());
3223 }
3224
3225 // parse all 'quad' subnodes
3226 QList<HighlightAnnotation::Quad> quads;
3227 QDomNode quadNode = e.firstChild();
3228 for (; quadNode.isElement(); quadNode = quadNode.nextSibling()) {
3229 QDomElement qe = quadNode.toElement();
3230 if (qe.tagName() != QLatin1String("quad")) {
3231 continue;
3232 }
3233
3234 Quad q;
3235 q.points[0].setX(qe.attribute(QStringLiteral("ax"), QStringLiteral("0.0")).toDouble());
3236 q.points[0].setY(qe.attribute(QStringLiteral("ay"), QStringLiteral("0.0")).toDouble());
3237 q.points[1].setX(qe.attribute(QStringLiteral("bx"), QStringLiteral("0.0")).toDouble());
3238 q.points[1].setY(qe.attribute(QStringLiteral("by"), QStringLiteral("0.0")).toDouble());
3239 q.points[2].setX(qe.attribute(QStringLiteral("cx"), QStringLiteral("0.0")).toDouble());
3240 q.points[2].setY(qe.attribute(QStringLiteral("cy"), QStringLiteral("0.0")).toDouble());
3241 q.points[3].setX(qe.attribute(QStringLiteral("dx"), QStringLiteral("0.0")).toDouble());
3242 q.points[3].setY(qe.attribute(QStringLiteral("dy"), QStringLiteral("0.0")).toDouble());
3243 q.capStart = qe.hasAttribute(QStringLiteral("start"));
3244 q.capEnd = qe.hasAttribute(QStringLiteral("end"));
3245 q.feather = qe.attribute(QStringLiteral("feather"), QStringLiteral("0.1")).toDouble();
3246 quads.append(q);
3247 }
3248 setHighlightQuads(quads);
3249
3250 // loading complete
3251 break;
3252 }
3253 }
3254
3255 HighlightAnnotation::~HighlightAnnotation() = default;
3256
3257 void HighlightAnnotation::store(QDomNode &node, QDomDocument &document) const
3258 {
3259 // store base annotation properties
3260 storeBaseAnnotationProperties(node, document);
3261
3262 // create [hl] element
3263 QDomElement hlElement = document.createElement(QStringLiteral("hl"));
3264 node.appendChild(hlElement);
3265
3266 // append the optional attributes
3267 if (highlightType() != Highlight) {
3268 hlElement.setAttribute(QStringLiteral("type"), (int)highlightType());
3269 }
3270
3271 const QList<HighlightAnnotation::Quad> quads = highlightQuads();
3272 if (quads.count() < 1) {
3273 return;
3274 }
3275 // append highlight quads, all children describe quads
3276 QList<HighlightAnnotation::Quad>::const_iterator it = quads.begin(), end = quads.end();
3277 for (; it != end; ++it) {
3278 QDomElement quadElement = document.createElement(QStringLiteral("quad"));
3279 hlElement.appendChild(quadElement);
3280 const Quad &q = *it;
3281 quadElement.setAttribute(QStringLiteral("ax"), QString::number(q.points[0].x()));
3282 quadElement.setAttribute(QStringLiteral("ay"), QString::number(q.points[0].y()));
3283 quadElement.setAttribute(QStringLiteral("bx"), QString::number(q.points[1].x()));
3284 quadElement.setAttribute(QStringLiteral("by"), QString::number(q.points[1].y()));
3285 quadElement.setAttribute(QStringLiteral("cx"), QString::number(q.points[2].x()));
3286 quadElement.setAttribute(QStringLiteral("cy"), QString::number(q.points[2].y()));
3287 quadElement.setAttribute(QStringLiteral("dx"), QString::number(q.points[3].x()));
3288 quadElement.setAttribute(QStringLiteral("dy"), QString::number(q.points[3].y()));
3289 if (q.capStart) {
3290 quadElement.setAttribute(QStringLiteral("start"), 1);
3291 }
3292 if (q.capEnd) {
3293 quadElement.setAttribute(QStringLiteral("end"), 1);
3294 }
3295 quadElement.setAttribute(QStringLiteral("feather"), QString::number(q.feather));
3296 }
3297 }
3298
3299 Annotation::SubType HighlightAnnotation::subType() const
3300 {
3301 return AHighlight;
3302 }
3303
3304 HighlightAnnotation::HighlightType HighlightAnnotation::highlightType() const
3305 {
3306 Q_D(const HighlightAnnotation);
3307
3308 if (!d->pdfAnnot) {
3309 return d->highlightType;
3310 }
3311
3312 Annot::AnnotSubtype subType = d->pdfAnnot->getType();
3313
3314 if (subType == Annot::typeHighlight) {
3315 return HighlightAnnotation::Highlight;
3316 } else if (subType == Annot::typeUnderline) {
3317 return HighlightAnnotation::Underline;
3318 } else if (subType == Annot::typeSquiggly) {
3319 return HighlightAnnotation::Squiggly;
3320 } else { // Annot::typeStrikeOut
3321 return HighlightAnnotation::StrikeOut;
3322 }
3323 }
3324
3325 void HighlightAnnotation::setHighlightType(HighlightAnnotation::HighlightType type)
3326 {
3327 Q_D(HighlightAnnotation);
3328
3329 if (!d->pdfAnnot) {
3330 d->highlightType = type;
3331 return;
3332 }
3333
3334 AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot.get());
3335 hlann->setType(HighlightAnnotationPrivate::toAnnotSubType(type));
3336 }
3337
3338 QList<HighlightAnnotation::Quad> HighlightAnnotation::highlightQuads() const
3339 {
3340 Q_D(const HighlightAnnotation);
3341
3342 if (!d->pdfAnnot) {
3343 return d->highlightQuads;
3344 }
3345
3346 const AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot.get());
3347 return d->fromQuadrilaterals(hlann->getQuadrilaterals());
3348 }
3349
3350 void HighlightAnnotation::setHighlightQuads(const QList<HighlightAnnotation::Quad> &quads)
3351 {
3352 Q_D(HighlightAnnotation);
3353
3354 if (!d->pdfAnnot) {
3355 d->highlightQuads = quads;
3356 return;
3357 }
3358
3359 AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot.get());
3360 AnnotQuadrilaterals *quadrilaterals = d->toQuadrilaterals(quads);
3361 hlann->setQuadrilaterals(*quadrilaterals);
3362 delete quadrilaterals;
3363 }
3364
3365 /** StampAnnotation [Annotation] */
3366 class StampAnnotationPrivate : public AnnotationPrivate
3367 {
3368 public:
3369 StampAnnotationPrivate();
3370 std::unique_ptr<Annotation> makeAlias() override;
3371 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
3372
3373 std::unique_ptr<AnnotStampImageHelper> convertQImageToAnnotStampImageHelper(const QImage &qimg);
3374
3375 // data fields
3376 QString stampIconName;
3377 QImage stampCustomImage;
3378 };
3379
3380 StampAnnotationPrivate::StampAnnotationPrivate() : stampIconName(QStringLiteral("Draft")) { }
3381
3382 std::unique_ptr<Annotation> StampAnnotationPrivate::makeAlias()
3383 {
3384 return std::unique_ptr<StampAnnotation>(new StampAnnotation(*this));
3385 }
3386
3387 std::shared_ptr<Annot> StampAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
3388 {
3389 std::unique_ptr<StampAnnotation> q = static_pointer_cast<StampAnnotation>(makeAlias());
3390
3391 // Set page and document
3392 pdfPage = destPage;
3393 parentDoc = doc;
3394
3395 // Set pdfAnnot
3396 PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
3397 pdfAnnot = std::make_shared<AnnotStamp>(destPage->getDoc(), &rect);
3398
3399 // Set properties
3400 flushBaseAnnotationProperties();
3401 q->setStampIconName(stampIconName);
3402 q->setStampCustomImage(stampCustomImage);
3403
3404 stampIconName.clear(); // Free up memory
3405
3406 return pdfAnnot;
3407 }
3408
3409 std::unique_ptr<AnnotStampImageHelper> StampAnnotationPrivate::convertQImageToAnnotStampImageHelper(const QImage &qimg)
3410 {
3411 QImage convertedQImage = qimg;
3412
3413 QByteArray data;
3414 QByteArray sMaskData;
3415 const int width = convertedQImage.width();
3416 const int height = convertedQImage.height();
3417 int bitsPerComponent = 1;
3418 ColorSpace colorSpace = ColorSpace::DeviceGray;
3419
3420 switch (convertedQImage.format()) {
3421 case QImage::Format_MonoLSB:
3422 if (!convertedQImage.allGray()) {
3423 convertedQImage = convertedQImage.convertToFormat(QImage::Format_RGB888);
3424
3425 colorSpace = ColorSpace::DeviceRGB;
3426 bitsPerComponent = 8;
3427 } else {
3428 convertedQImage = convertedQImage.convertToFormat(QImage::Format_Mono);
3429 }
3430 break;
3431 case QImage::Format_Mono:
3432 if (!convertedQImage.allGray()) {
3433 convertedQImage = convertedQImage.convertToFormat(QImage::Format_RGB888);
3434
3435 colorSpace = ColorSpace::DeviceRGB;
3436 bitsPerComponent = 8;
3437 }
3438 break;
3439 case QImage::Format_RGB32:
3440 case QImage::Format_ARGB32_Premultiplied:
3441 case QImage::Format_ARGB8565_Premultiplied:
3442 case QImage::Format_ARGB6666_Premultiplied:
3443 case QImage::Format_ARGB8555_Premultiplied:
3444 case QImage::Format_ARGB4444_Premultiplied:
3445 case QImage::Format_Alpha8:
3446 convertedQImage = convertedQImage.convertToFormat(QImage::Format_ARGB32);
3447 colorSpace = ColorSpace::DeviceRGB;
3448 bitsPerComponent = 8;
3449 break;
3450 case QImage::Format_RGBA8888:
3451 case QImage::Format_RGBA8888_Premultiplied:
3452 case QImage::Format_RGBX8888:
3453 case QImage::Format_ARGB32:
3454 colorSpace = ColorSpace::DeviceRGB;
3455 bitsPerComponent = 8;
3456 break;
3457 case QImage::Format_Grayscale8:
3458 bitsPerComponent = 8;
3459 break;
3460 case QImage::Format_Grayscale16:
3461 convertedQImage = convertedQImage.convertToFormat(QImage::Format_Grayscale8);
3462
3463 colorSpace = ColorSpace::DeviceGray;
3464 bitsPerComponent = 8;
3465 break;
3466 case QImage::Format_RGB16:
3467 case QImage::Format_RGB666:
3468 case QImage::Format_RGB555:
3469 case QImage::Format_RGB444:
3470 convertedQImage = convertedQImage.convertToFormat(QImage::Format_RGB888);
3471 colorSpace = ColorSpace::DeviceRGB;
3472 bitsPerComponent = 8;
3473 break;
3474 case QImage::Format_RGB888:
3475 colorSpace = ColorSpace::DeviceRGB;
3476 bitsPerComponent = 8;
3477 break;
3478 default:
3479 convertedQImage = convertedQImage.convertToFormat(QImage::Format_ARGB32);
3480
3481 colorSpace = ColorSpace::DeviceRGB;
3482 bitsPerComponent = 8;
3483 break;
3484 }
3485
3486 getRawDataFromQImage(convertedQImage, convertedQImage.depth(), &data, &sMaskData);
3487
3488 std::unique_ptr<AnnotStampImageHelper> annotImg;
3489
3490 if (sMaskData.count() > 0) {
3491 AnnotStampImageHelper sMask(parentDoc->doc, width, height, ColorSpace::DeviceGray, 8, sMaskData.data(), sMaskData.count());
3492 annotImg = std::make_unique<AnnotStampImageHelper>(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data.data(), data.count(), sMask.getRef());
3493 } else {
3494 annotImg = std::make_unique<AnnotStampImageHelper>(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data.data(), data.count());
3495 }
3496
3497 return annotImg;
3498 }
3499
3500 StampAnnotation::StampAnnotation() : Annotation(*new StampAnnotationPrivate()) { }
3501
3502 StampAnnotation::StampAnnotation(StampAnnotationPrivate &dd) : Annotation(dd) { }
3503
3504 StampAnnotation::StampAnnotation(const QDomNode &node) : Annotation(*new StampAnnotationPrivate(), node)
3505 {
3506 // loop through the whole children looking for a 'stamp' element
3507 QDomNode subNode = node.firstChild();
3508 while (subNode.isElement()) {
3509 QDomElement e = subNode.toElement();
3510 subNode = subNode.nextSibling();
3511 if (e.tagName() != QLatin1String("stamp")) {
3512 continue;
3513 }
3514
3515 // parse the attributes
3516 if (e.hasAttribute(QStringLiteral("icon"))) {
3517 setStampIconName(e.attribute(QStringLiteral("icon")));
3518 }
3519
3520 // loading complete
3521 break;
3522 }
3523 }
3524
3525 StampAnnotation::~StampAnnotation() = default;
3526
3527 void StampAnnotation::store(QDomNode &node, QDomDocument &document) const
3528 {
3529 // store base annotation properties
3530 storeBaseAnnotationProperties(node, document);
3531
3532 // create [stamp] element
3533 QDomElement stampElement = document.createElement(QStringLiteral("stamp"));
3534 node.appendChild(stampElement);
3535
3536 // append the optional attributes
3537 if (stampIconName() != QLatin1String("Draft")) {
3538 stampElement.setAttribute(QStringLiteral("icon"), stampIconName());
3539 }
3540 }
3541
3542 Annotation::SubType StampAnnotation::subType() const
3543 {
3544 return AStamp;
3545 }
3546
3547 QString StampAnnotation::stampIconName() const
3548 {
3549 Q_D(const StampAnnotation);
3550
3551 if (!d->pdfAnnot) {
3552 return d->stampIconName;
3553 }
3554
3555 const AnnotStamp *stampann = static_cast<const AnnotStamp *>(d->pdfAnnot.get());
3556 return QString::fromStdString(stampann->getIcon());
3557 }
3558
3559 void StampAnnotation::setStampIconName(const QString &name)
3560 {
3561 Q_D(StampAnnotation);
3562
3563 if (!d->pdfAnnot) {
3564 d->stampIconName = name;
3565 return;
3566 }
3567
3568 AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot.get());
3569 stampann->setIcon(name.toStdString());
3570 }
3571
3572 void StampAnnotation::setStampCustomImage(const QImage &image)
3573 {
3574 if (image.isNull()) {
3575 return;
3576 }
3577
3578 Q_D(StampAnnotation);
3579
3580 if (!d->pdfAnnot) {
3581 d->stampCustomImage = QImage(image);
3582 return;
3583 }
3584
3585 AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot.get());
3586 std::unique_ptr<AnnotStampImageHelper> annotCustomImage = d->convertQImageToAnnotStampImageHelper(image);
3587 stampann->setCustomImage(std::move(annotCustomImage));
3588 }
3589
3590 /** InkAnnotation [Annotation] */
3591 class InkAnnotationPrivate : public AnnotationPrivate
3592 {
3593 public:
3594 InkAnnotationPrivate();
3595 std::unique_ptr<Annotation> makeAlias() override;
3596 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
3597
3598 // data fields
3599 QList<QLinkedList<QPointF>> inkPaths;
3600
3601 // helper
3602 std::vector<std::unique_ptr<AnnotPath>> toAnnotPaths(const QList<QLinkedList<QPointF>> &paths);
3603 };
3604
3605 InkAnnotationPrivate::InkAnnotationPrivate() = default;
3606
3607 std::unique_ptr<Annotation> InkAnnotationPrivate::makeAlias()
3608 {
3609 return std::unique_ptr<InkAnnotation>(new InkAnnotation(*this));
3610 }
3611
3612 // Note: Caller is required to delete array elements and the array itself after use
3613 std::vector<std::unique_ptr<AnnotPath>> InkAnnotationPrivate::toAnnotPaths(const QList<QLinkedList<QPointF>> &paths)
3614 {
3615 std::vector<std::unique_ptr<AnnotPath>> res;
3616 res.reserve(paths.size());
3617 for (const auto &path : paths) {
3618 res.push_back(toAnnotPath(path));
3619 }
3620 return res;
3621 }
3622
3623 std::shared_ptr<Annot> InkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
3624 {
3625 // Setters are defined in the public class
3626 std::unique_ptr<InkAnnotation> q = static_pointer_cast<InkAnnotation>(makeAlias());
3627
3628 // Set page and document
3629 pdfPage = destPage;
3630 parentDoc = doc;
3631
3632 // Set pdfAnnot
3633 PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
3634 pdfAnnot = std::make_shared<AnnotInk>(destPage->getDoc(), &rect);
3635
3636 // Set properties
3637 flushBaseAnnotationProperties();
3638 q->setInkPaths(inkPaths);
3639
3640 inkPaths.clear(); // Free up memory
3641
3642 return pdfAnnot;
3643 }
3644
3645 InkAnnotation::InkAnnotation() : Annotation(*new InkAnnotationPrivate()) { }
3646
3647 InkAnnotation::InkAnnotation(InkAnnotationPrivate &dd) : Annotation(dd) { }
3648
3649 InkAnnotation::InkAnnotation(const QDomNode &node) : Annotation(*new InkAnnotationPrivate(), node)
3650 {
3651 // loop through the whole children looking for a 'ink' element
3652 QDomNode subNode = node.firstChild();
3653 while (subNode.isElement()) {
3654 QDomElement e = subNode.toElement();
3655 subNode = subNode.nextSibling();
3656 if (e.tagName() != QLatin1String("ink")) {
3657 continue;
3658 }
3659
3660 // parse the 'path' subnodes
3661 QList<QLinkedList<QPointF>> paths;
3662 QDomNode pathNode = e.firstChild();
3663 while (pathNode.isElement()) {
3664 QDomElement pathElement = pathNode.toElement();
3665 pathNode = pathNode.nextSibling();
3666
3667 if (pathElement.tagName() != QLatin1String("path")) {
3668 continue;
3669 }
3670
3671 // build each path parsing 'point' subnodes
3672 QLinkedList<QPointF> path;
3673 QDomNode pointNode = pathElement.firstChild();
3674 while (pointNode.isElement()) {
3675 QDomElement pointElement = pointNode.toElement();
3676 pointNode = pointNode.nextSibling();
3677
3678 if (pointElement.tagName() != QLatin1String("point")) {
3679 continue;
3680 }
3681
3682 QPointF p(pointElement.attribute(QStringLiteral("x"), QStringLiteral("0.0")).toDouble(), pointElement.attribute(QStringLiteral("y"), QStringLiteral("0.0")).toDouble());
3683 path.append(p);
3684 }
3685
3686 // add the path to the path list if it contains at least 2 nodes
3687 if (path.count() >= 2) {
3688 paths.append(path);
3689 }
3690 }
3691 setInkPaths(paths);
3692
3693 // loading complete
3694 break;
3695 }
3696 }
3697
3698 InkAnnotation::~InkAnnotation() = default;
3699
3700 void InkAnnotation::store(QDomNode &node, QDomDocument &document) const
3701 {
3702 // store base annotation properties
3703 storeBaseAnnotationProperties(node, document);
3704
3705 // create [ink] element
3706 QDomElement inkElement = document.createElement(QStringLiteral("ink"));
3707 node.appendChild(inkElement);
3708
3709 // append the optional attributes
3710 const QList<QLinkedList<QPointF>> paths = inkPaths();
3711 if (paths.count() < 1) {
3712 return;
3713 }
3714 QList<QLinkedList<QPointF>>::const_iterator pIt = paths.begin(), pEnd = paths.end();
3715 for (; pIt != pEnd; ++pIt) {
3716 QDomElement pathElement = document.createElement(QStringLiteral("path"));
3717 inkElement.appendChild(pathElement);
3718 const QLinkedList<QPointF> &path = *pIt;
3719 QLinkedList<QPointF>::const_iterator iIt = path.begin(), iEnd = path.end();
3720 for (; iIt != iEnd; ++iIt) {
3721 const QPointF &point = *iIt;
3722 QDomElement pointElement = document.createElement(QStringLiteral("point"));
3723 pathElement.appendChild(pointElement);
3724 pointElement.setAttribute(QStringLiteral("x"), QString::number(point.x()));
3725 pointElement.setAttribute(QStringLiteral("y"), QString::number(point.y()));
3726 }
3727 }
3728 }
3729
3730 Annotation::SubType InkAnnotation::subType() const
3731 {
3732 return AInk;
3733 }
3734
3735 QList<QLinkedList<QPointF>> InkAnnotation::inkPaths() const
3736 {
3737 Q_D(const InkAnnotation);
3738
3739 if (!d->pdfAnnot) {
3740 return d->inkPaths;
3741 }
3742
3743 const AnnotInk *inkann = static_cast<const AnnotInk *>(d->pdfAnnot.get());
3744
3745 const std::vector<std::unique_ptr<AnnotPath>> &paths = inkann->getInkList();
3746 if (paths.empty()) {
3747 return QList<QLinkedList<QPointF>>();
3748 }
3749
3750 double MTX[6];
3751 d->fillTransformationMTX(MTX);
3752
3753 QList<QLinkedList<QPointF>> inkPaths;
3754 inkPaths.reserve(paths.size());
3755 for (const auto &path : paths) {
3756 // transform each path in a list of normalized points ..
3757 QLinkedList<QPointF> localList;
3758 const int pointsNumber = path ? path->getCoordsLength() : 0;
3759 for (int n = 0; n < pointsNumber; ++n) {
3760 QPointF point;
3761 XPDFReader::transform(MTX, path->getX(n), path->getY(n), point);
3762 localList.append(point);
3763 }
3764 // ..and add it to the annotation
3765 inkPaths.append(localList);
3766 }
3767 return inkPaths;
3768 }
3769
3770 void InkAnnotation::setInkPaths(const QList<QLinkedList<QPointF>> &paths)
3771 {
3772 Q_D(InkAnnotation);
3773
3774 if (!d->pdfAnnot) {
3775 d->inkPaths = paths;
3776 return;
3777 }
3778
3779 AnnotInk *inkann = static_cast<AnnotInk *>(d->pdfAnnot.get());
3780 const std::vector<std::unique_ptr<AnnotPath>> annotpaths = d->toAnnotPaths(paths);
3781 inkann->setInkList(annotpaths);
3782 }
3783
3784 /** LinkAnnotation [Annotation] */
3785 class LinkAnnotationPrivate : public AnnotationPrivate
3786 {
3787 public:
3788 LinkAnnotationPrivate();
3789 ~LinkAnnotationPrivate() override;
3790 std::unique_ptr<Annotation> makeAlias() override;
3791 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
3792
3793 // data fields
3794 Link *linkDestination;
3795 LinkAnnotation::HighlightMode linkHLMode;
3796 QPointF linkRegion[4];
3797 };
3798
3799 LinkAnnotationPrivate::LinkAnnotationPrivate() : linkDestination(nullptr), linkHLMode(LinkAnnotation::Invert) { }
3800
3801 LinkAnnotationPrivate::~LinkAnnotationPrivate()
3802 {
3803 delete linkDestination;
3804 }
3805
3806 std::unique_ptr<Annotation> LinkAnnotationPrivate::makeAlias()
3807 {
3808 return std::unique_ptr<LinkAnnotation>(new LinkAnnotation(*this));
3809 }
3810
3811 std::shared_ptr<Annot> LinkAnnotationPrivate::createNativeAnnot(::Page * /*destPage*/, DocumentData * /*doc*/)
3812 {
3813 return nullptr; // Not implemented
3814 }
3815
3816 LinkAnnotation::LinkAnnotation() : Annotation(*new LinkAnnotationPrivate()) { }
3817
3818 LinkAnnotation::LinkAnnotation(LinkAnnotationPrivate &dd) : Annotation(dd) { }
3819
3820 LinkAnnotation::LinkAnnotation(const QDomNode &node) : Annotation(*new LinkAnnotationPrivate(), node)
3821 {
3822 // loop through the whole children looking for a 'link' element
3823 QDomNode subNode = node.firstChild();
3824 while (subNode.isElement()) {
3825 QDomElement e = subNode.toElement();
3826 subNode = subNode.nextSibling();
3827 if (e.tagName() != QLatin1String("link")) {
3828 continue;
3829 }
3830
3831 // parse the attributes
3832 if (e.hasAttribute(QStringLiteral("hlmode"))) {
3833 setLinkHighlightMode((LinkAnnotation::HighlightMode)e.attribute(QStringLiteral("hlmode")).toInt());
3834 }
3835
3836 // parse all 'quad' subnodes
3837 QDomNode quadNode = e.firstChild();
3838 for (; quadNode.isElement(); quadNode = quadNode.nextSibling()) {
3839 QDomElement qe = quadNode.toElement();
3840 if (qe.tagName() == QLatin1String("quad")) {
3841 setLinkRegionPoint(0, QPointF(qe.attribute(QStringLiteral("ax"), QStringLiteral("0.0")).toDouble(), qe.attribute(QStringLiteral("ay"), QStringLiteral("0.0")).toDouble()));
3842 setLinkRegionPoint(1, QPointF(qe.attribute(QStringLiteral("bx"), QStringLiteral("0.0")).toDouble(), qe.attribute(QStringLiteral("by"), QStringLiteral("0.0")).toDouble()));
3843 setLinkRegionPoint(2, QPointF(qe.attribute(QStringLiteral("cx"), QStringLiteral("0.0")).toDouble(), qe.attribute(QStringLiteral("cy"), QStringLiteral("0.0")).toDouble()));
3844 setLinkRegionPoint(3, QPointF(qe.attribute(QStringLiteral("dx"), QStringLiteral("0.0")).toDouble(), qe.attribute(QStringLiteral("dy"), QStringLiteral("0.0")).toDouble()));
3845 } else if (qe.tagName() == QLatin1String("link")) {
3846 QString type = qe.attribute(QStringLiteral("type"));
3847 if (type == QLatin1String("GoTo")) {
3848 Poppler::LinkGoto *go = new Poppler::LinkGoto(QRect(), qe.attribute(QStringLiteral("filename")), LinkDestination(qe.attribute(QStringLiteral("destination"))));
3849 setLinkDestination(go);
3850 } else if (type == QLatin1String("Exec")) {
3851 Poppler::LinkExecute *exec = new Poppler::LinkExecute(QRect(), qe.attribute(QStringLiteral("filename")), qe.attribute(QStringLiteral("parameters")));
3852 setLinkDestination(exec);
3853 } else if (type == QLatin1String("Browse")) {
3854 Poppler::LinkBrowse *browse = new Poppler::LinkBrowse(QRect(), qe.attribute(QStringLiteral("url")));
3855 setLinkDestination(browse);
3856 } else if (type == QLatin1String("Action")) {
3857 Poppler::LinkAction::ActionType act;
3858 QString actString = qe.attribute(QStringLiteral("action"));
3859 bool found = true;
3860 if (actString == QLatin1String("PageFirst")) {
3861 act = Poppler::LinkAction::PageFirst;
3862 } else if (actString == QLatin1String("PagePrev")) {
3863 act = Poppler::LinkAction::PagePrev;
3864 } else if (actString == QLatin1String("PageNext")) {
3865 act = Poppler::LinkAction::PageNext;
3866 } else if (actString == QLatin1String("PageLast")) {
3867 act = Poppler::LinkAction::PageLast;
3868 } else if (actString == QLatin1String("HistoryBack")) {
3869 act = Poppler::LinkAction::HistoryBack;
3870 } else if (actString == QLatin1String("HistoryForward")) {
3871 act = Poppler::LinkAction::HistoryForward;
3872 } else if (actString == QLatin1String("Quit")) {
3873 act = Poppler::LinkAction::Quit;
3874 } else if (actString == QLatin1String("Presentation")) {
3875 act = Poppler::LinkAction::Presentation;
3876 } else if (actString == QLatin1String("EndPresentation")) {
3877 act = Poppler::LinkAction::EndPresentation;
3878 } else if (actString == QLatin1String("Find")) {
3879 act = Poppler::LinkAction::Find;
3880 } else if (actString == QLatin1String("GoToPage")) {
3881 act = Poppler::LinkAction::GoToPage;
3882 } else if (actString == QLatin1String("Close")) {
3883 act = Poppler::LinkAction::Close;
3884 } else if (actString == QLatin1String("Print")) {
3885 act = Poppler::LinkAction::Print;
3886 } else {
3887 found = false;
3888 }
3889 if (found) {
3890 Poppler::LinkAction *action = new Poppler::LinkAction(QRect(), act);
3891 setLinkDestination(action);
3892 }
3893 } else {
3894 qWarning("Loading annotations of type %s from DOM nodes is not yet implemented.", type.toLocal8Bit().constData());
3895 }
3896 }
3897 }
3898
3899 // loading complete
3900 break;
3901 }
3902 }
3903
3904 LinkAnnotation::~LinkAnnotation() = default;
3905
3906 void LinkAnnotation::store(QDomNode &node, QDomDocument &document) const
3907 {
3908 // store base annotation properties
3909 storeBaseAnnotationProperties(node, document);
3910
3911 // create [hl] element
3912 QDomElement linkElement = document.createElement(QStringLiteral("link"));
3913 node.appendChild(linkElement);
3914
3915 // append the optional attributes
3916 if (linkHighlightMode() != Invert) {
3917 linkElement.setAttribute(QStringLiteral("hlmode"), (int)linkHighlightMode());
3918 }
3919
3920 // saving region
3921 QDomElement quadElement = document.createElement(QStringLiteral("quad"));
3922 linkElement.appendChild(quadElement);
3923 quadElement.setAttribute(QStringLiteral("ax"), QString::number(linkRegionPoint(0).x()));
3924 quadElement.setAttribute(QStringLiteral("ay"), QString::number(linkRegionPoint(0).y()));
3925 quadElement.setAttribute(QStringLiteral("bx"), QString::number(linkRegionPoint(1).x()));
3926 quadElement.setAttribute(QStringLiteral("by"), QString::number(linkRegionPoint(1).y()));
3927 quadElement.setAttribute(QStringLiteral("cx"), QString::number(linkRegionPoint(2).x()));
3928 quadElement.setAttribute(QStringLiteral("cy"), QString::number(linkRegionPoint(2).y()));
3929 quadElement.setAttribute(QStringLiteral("dx"), QString::number(linkRegionPoint(3).x()));
3930 quadElement.setAttribute(QStringLiteral("dy"), QString::number(linkRegionPoint(3).y()));
3931
3932 // saving link
3933 QDomElement hyperlinkElement = document.createElement(QStringLiteral("link"));
3934 linkElement.appendChild(hyperlinkElement);
3935 if (linkDestination()) {
3936 switch (linkDestination()->linkType()) {
3937 case Poppler::Link::Goto: {
3938 Poppler::LinkGoto *go = static_cast<Poppler::LinkGoto *>(linkDestination());
3939 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("GoTo"));
3940 hyperlinkElement.setAttribute(QStringLiteral("filename"), go->fileName());
3941 hyperlinkElement.setAttribute(QStringLiteral("destionation"), go->destination().toString()); // TODO remove for poppler 0.28
3942 hyperlinkElement.setAttribute(QStringLiteral("destination"), go->destination().toString());
3943 break;
3944 }
3945 case Poppler::Link::Execute: {
3946 Poppler::LinkExecute *exec = static_cast<Poppler::LinkExecute *>(linkDestination());
3947 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("Exec"));
3948 hyperlinkElement.setAttribute(QStringLiteral("filename"), exec->fileName());
3949 hyperlinkElement.setAttribute(QStringLiteral("parameters"), exec->parameters());
3950 break;
3951 }
3952 case Poppler::Link::Browse: {
3953 Poppler::LinkBrowse *browse = static_cast<Poppler::LinkBrowse *>(linkDestination());
3954 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("Browse"));
3955 hyperlinkElement.setAttribute(QStringLiteral("url"), browse->url());
3956 break;
3957 }
3958 case Poppler::Link::Action: {
3959 Poppler::LinkAction *action = static_cast<Poppler::LinkAction *>(linkDestination());
3960 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("Action"));
3961 switch (action->actionType()) {
3962 case Poppler::LinkAction::PageFirst:
3963 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("PageFirst"));
3964 break;
3965 case Poppler::LinkAction::PagePrev:
3966 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("PagePrev"));
3967 break;
3968 case Poppler::LinkAction::PageNext:
3969 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("PageNext"));
3970 break;
3971 case Poppler::LinkAction::PageLast:
3972 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("PageLast"));
3973 break;
3974 case Poppler::LinkAction::HistoryBack:
3975 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("HistoryBack"));
3976 break;
3977 case Poppler::LinkAction::HistoryForward:
3978 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("HistoryForward"));
3979 break;
3980 case Poppler::LinkAction::Quit:
3981 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("Quit"));
3982 break;
3983 case Poppler::LinkAction::Presentation:
3984 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("Presentation"));
3985 break;
3986 case Poppler::LinkAction::EndPresentation:
3987 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("EndPresentation"));
3988 break;
3989 case Poppler::LinkAction::Find:
3990 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("Find"));
3991 break;
3992 case Poppler::LinkAction::GoToPage:
3993 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("GoToPage"));
3994 break;
3995 case Poppler::LinkAction::Close:
3996 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("Close"));
3997 break;
3998 case Poppler::LinkAction::Print:
3999 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("Print"));
4000 break;
4001 case Poppler::LinkAction::SaveAs:
4002 hyperlinkElement.setAttribute(QStringLiteral("action"), QStringLiteral("SaveAs"));
4003 break;
4004 }
4005 break;
4006 }
4007 case Poppler::Link::Movie: {
4008 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("Movie"));
4009 break;
4010 }
4011 case Poppler::Link::Rendition: {
4012 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("Rendition"));
4013 break;
4014 }
4015 case Poppler::Link::Sound: {
4016 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("Sound"));
4017 break;
4018 }
4019 case Poppler::Link::JavaScript: {
4020 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("JavaScript"));
4021 break;
4022 }
4023 case Poppler::Link::OCGState: {
4024 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("OCGState"));
4025 break;
4026 }
4027 case Poppler::Link::Hide: {
4028 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("Hide"));
4029 break;
4030 }
4031 case Poppler::Link::ResetForm: {
4032 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("ResetForm"));
4033 break;
4034 }
4035 case Poppler::Link::SubmitForm: {
4036 hyperlinkElement.setAttribute(QStringLiteral("type"), QStringLiteral("SubmitForm"));
4037 }
4038 case Poppler::Link::None:
4039 break;
4040 }
4041 }
4042 }
4043
4044 Annotation::SubType LinkAnnotation::subType() const
4045 {
4046 return ALink;
4047 }
4048
4049 Link *LinkAnnotation::linkDestination() const
4050 {
4051 Q_D(const LinkAnnotation);
4052 return d->linkDestination;
4053 }
4054
4055 void LinkAnnotation::setLinkDestination(Link *link)
4056 {
4057 Q_D(LinkAnnotation);
4058 delete d->linkDestination;
4059 d->linkDestination = link;
4060 }
4061
4062 LinkAnnotation::HighlightMode LinkAnnotation::linkHighlightMode() const
4063 {
4064 Q_D(const LinkAnnotation);
4065 return d->linkHLMode;
4066 }
4067
4068 void LinkAnnotation::setLinkHighlightMode(LinkAnnotation::HighlightMode mode)
4069 {
4070 Q_D(LinkAnnotation);
4071 d->linkHLMode = mode;
4072 }
4073
4074 QPointF LinkAnnotation::linkRegionPoint(int id) const
4075 {
4076 if (id < 0 || id >= 4) {
4077 return QPointF();
4078 }
4079
4080 Q_D(const LinkAnnotation);
4081 return d->linkRegion[id];
4082 }
4083
4084 void LinkAnnotation::setLinkRegionPoint(int id, const QPointF &point) // clazy:exclude=function-args-by-value
4085 {
4086 if (id < 0 || id >= 4) {
4087 return;
4088 }
4089
4090 Q_D(LinkAnnotation);
4091 d->linkRegion[id] = point;
4092 }
4093
4094 /** CaretAnnotation [Annotation] */
4095 class CaretAnnotationPrivate : public AnnotationPrivate
4096 {
4097 public:
4098 CaretAnnotationPrivate();
4099 std::unique_ptr<Annotation> makeAlias() override;
4100 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
4101
4102 // data fields
4103 CaretAnnotation::CaretSymbol symbol;
4104 };
4105
4106 static QString caretSymbolToString(CaretAnnotation::CaretSymbol symbol)
4107 {
4108 switch (symbol) {
4109 case CaretAnnotation::None:
4110 return QStringLiteral("None");
4111 case CaretAnnotation::P:
4112 return QStringLiteral("P");
4113 }
4114 return QString();
4115 }
4116
4117 static CaretAnnotation::CaretSymbol caretSymbolFromString(const QString &symbol)
4118 {
4119 if (symbol == QLatin1String("None")) {
4120 return CaretAnnotation::None;
4121 } else if (symbol == QLatin1String("P")) {
4122 return CaretAnnotation::P;
4123 }
4124 return CaretAnnotation::None;
4125 }
4126
4127 CaretAnnotationPrivate::CaretAnnotationPrivate() : symbol(CaretAnnotation::None) { }
4128
4129 std::unique_ptr<Annotation> CaretAnnotationPrivate::makeAlias()
4130 {
4131 return std::unique_ptr<CaretAnnotation>(new CaretAnnotation(*this));
4132 }
4133
4134 std::shared_ptr<Annot> CaretAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
4135 {
4136 // Setters are defined in the public class
4137 std::unique_ptr<CaretAnnotation> q = static_pointer_cast<CaretAnnotation>(makeAlias());
4138
4139 // Set page and document
4140 pdfPage = destPage;
4141 parentDoc = doc;
4142
4143 // Set pdfAnnot
4144 PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
4145 pdfAnnot = std::make_shared<AnnotCaret>(destPage->getDoc(), &rect);
4146
4147 // Set properties
4148 flushBaseAnnotationProperties();
4149 q->setCaretSymbol(symbol);
4150
4151 return pdfAnnot;
4152 }
4153
4154 CaretAnnotation::CaretAnnotation() : Annotation(*new CaretAnnotationPrivate()) { }
4155
4156 CaretAnnotation::CaretAnnotation(CaretAnnotationPrivate &dd) : Annotation(dd) { }
4157
4158 CaretAnnotation::CaretAnnotation(const QDomNode &node) : Annotation(*new CaretAnnotationPrivate(), node)
4159 {
4160 // loop through the whole children looking for a 'caret' element
4161 QDomNode subNode = node.firstChild();
4162 while (subNode.isElement()) {
4163 QDomElement e = subNode.toElement();
4164 subNode = subNode.nextSibling();
4165 if (e.tagName() != QLatin1String("caret")) {
4166 continue;
4167 }
4168
4169 // parse the attributes
4170 if (e.hasAttribute(QStringLiteral("symbol"))) {
4171 setCaretSymbol(caretSymbolFromString(e.attribute(QStringLiteral("symbol"))));
4172 }
4173
4174 // loading complete
4175 break;
4176 }
4177 }
4178
4179 CaretAnnotation::~CaretAnnotation() = default;
4180
4181 void CaretAnnotation::store(QDomNode &node, QDomDocument &document) const
4182 {
4183 // store base annotation properties
4184 storeBaseAnnotationProperties(node, document);
4185
4186 // create [caret] element
4187 QDomElement caretElement = document.createElement(QStringLiteral("caret"));
4188 node.appendChild(caretElement);
4189
4190 // append the optional attributes
4191 if (caretSymbol() != CaretAnnotation::None) {
4192 caretElement.setAttribute(QStringLiteral("symbol"), caretSymbolToString(caretSymbol()));
4193 }
4194 }
4195
4196 Annotation::SubType CaretAnnotation::subType() const
4197 {
4198 return ACaret;
4199 }
4200
4201 CaretAnnotation::CaretSymbol CaretAnnotation::caretSymbol() const
4202 {
4203 Q_D(const CaretAnnotation);
4204
4205 if (!d->pdfAnnot) {
4206 return d->symbol;
4207 }
4208
4209 const AnnotCaret *caretann = static_cast<const AnnotCaret *>(d->pdfAnnot.get());
4210 return (CaretAnnotation::CaretSymbol)caretann->getSymbol();
4211 }
4212
4213 void CaretAnnotation::setCaretSymbol(CaretAnnotation::CaretSymbol symbol)
4214 {
4215 Q_D(CaretAnnotation);
4216
4217 if (!d->pdfAnnot) {
4218 d->symbol = symbol;
4219 return;
4220 }
4221
4222 AnnotCaret *caretann = static_cast<AnnotCaret *>(d->pdfAnnot.get());
4223 caretann->setSymbol((AnnotCaret::AnnotCaretSymbol)symbol);
4224 }
4225
4226 /** FileAttachmentAnnotation [Annotation] */
4227 class FileAttachmentAnnotationPrivate : public AnnotationPrivate
4228 {
4229 public:
4230 FileAttachmentAnnotationPrivate();
4231 ~FileAttachmentAnnotationPrivate() override;
4232 std::unique_ptr<Annotation> makeAlias() override;
4233 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
4234
4235 // data fields
4236 QString icon;
4237 EmbeddedFile *embfile;
4238 };
4239
4240 FileAttachmentAnnotationPrivate::FileAttachmentAnnotationPrivate() : icon(QStringLiteral("PushPin")), embfile(nullptr) { }
4241
4242 FileAttachmentAnnotationPrivate::~FileAttachmentAnnotationPrivate()
4243 {
4244 delete embfile;
4245 }
4246
4247 std::unique_ptr<Annotation> FileAttachmentAnnotationPrivate::makeAlias()
4248 {
4249 return std::unique_ptr<FileAttachmentAnnotation>(new FileAttachmentAnnotation(*this));
4250 }
4251
4252 std::shared_ptr<Annot> FileAttachmentAnnotationPrivate::createNativeAnnot(::Page * /*destPage*/, DocumentData * /*doc*/)
4253 {
4254 return nullptr; // Not implemented
4255 }
4256
4257 FileAttachmentAnnotation::FileAttachmentAnnotation() : Annotation(*new FileAttachmentAnnotationPrivate()) { }
4258
4259 FileAttachmentAnnotation::FileAttachmentAnnotation(FileAttachmentAnnotationPrivate &dd) : Annotation(dd) { }
4260
4261 FileAttachmentAnnotation::FileAttachmentAnnotation(const QDomNode &node) : Annotation(*new FileAttachmentAnnotationPrivate(), node)
4262 {
4263 // loop through the whole children looking for a 'fileattachment' element
4264 QDomNode subNode = node.firstChild();
4265 while (subNode.isElement()) {
4266 QDomElement e = subNode.toElement();
4267 subNode = subNode.nextSibling();
4268 if (e.tagName() != QLatin1String("fileattachment")) {
4269 continue;
4270 }
4271
4272 // loading complete
4273 break;
4274 }
4275 }
4276
4277 FileAttachmentAnnotation::~FileAttachmentAnnotation() = default;
4278
4279 void FileAttachmentAnnotation::store(QDomNode &node, QDomDocument &document) const
4280 {
4281 // store base annotation properties
4282 storeBaseAnnotationProperties(node, document);
4283
4284 // create [fileattachment] element
4285 QDomElement fileAttachmentElement = document.createElement(QStringLiteral("fileattachment"));
4286 node.appendChild(fileAttachmentElement);
4287 }
4288
4289 Annotation::SubType FileAttachmentAnnotation::subType() const
4290 {
4291 return AFileAttachment;
4292 }
4293
4294 QString FileAttachmentAnnotation::fileIconName() const
4295 {
4296 Q_D(const FileAttachmentAnnotation);
4297 return d->icon;
4298 }
4299
4300 void FileAttachmentAnnotation::setFileIconName(const QString &icon)
4301 {
4302 Q_D(FileAttachmentAnnotation);
4303 d->icon = icon;
4304 }
4305
4306 EmbeddedFile *FileAttachmentAnnotation::embeddedFile() const
4307 {
4308 Q_D(const FileAttachmentAnnotation);
4309 return d->embfile;
4310 }
4311
4312 void FileAttachmentAnnotation::setEmbeddedFile(EmbeddedFile *ef)
4313 {
4314 Q_D(FileAttachmentAnnotation);
4315 d->embfile = ef;
4316 }
4317
4318 /** SoundAnnotation [Annotation] */
4319 class SoundAnnotationPrivate : public AnnotationPrivate
4320 {
4321 public:
4322 SoundAnnotationPrivate();
4323 ~SoundAnnotationPrivate() override;
4324 std::unique_ptr<Annotation> makeAlias() override;
4325 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
4326
4327 // data fields
4328 QString icon;
4329 SoundObject *sound;
4330 };
4331
4332 SoundAnnotationPrivate::SoundAnnotationPrivate() : icon(QStringLiteral("Speaker")), sound(nullptr) { }
4333
4334 SoundAnnotationPrivate::~SoundAnnotationPrivate()
4335 {
4336 delete sound;
4337 }
4338
4339 std::unique_ptr<Annotation> SoundAnnotationPrivate::makeAlias()
4340 {
4341 return std::unique_ptr<SoundAnnotation>(new SoundAnnotation(*this));
4342 }
4343
4344 std::shared_ptr<Annot> SoundAnnotationPrivate::createNativeAnnot(::Page * /*destPage*/, DocumentData * /*doc*/)
4345 {
4346 return nullptr; // Not implemented
4347 }
4348
4349 SoundAnnotation::SoundAnnotation() : Annotation(*new SoundAnnotationPrivate()) { }
4350
4351 SoundAnnotation::SoundAnnotation(SoundAnnotationPrivate &dd) : Annotation(dd) { }
4352
4353 SoundAnnotation::SoundAnnotation(const QDomNode &node) : Annotation(*new SoundAnnotationPrivate(), node)
4354 {
4355 // loop through the whole children looking for a 'sound' element
4356 QDomNode subNode = node.firstChild();
4357 while (subNode.isElement()) {
4358 QDomElement e = subNode.toElement();
4359 subNode = subNode.nextSibling();
4360 if (e.tagName() != QLatin1String("sound")) {
4361 continue;
4362 }
4363
4364 // loading complete
4365 break;
4366 }
4367 }
4368
4369 SoundAnnotation::~SoundAnnotation() = default;
4370
4371 void SoundAnnotation::store(QDomNode &node, QDomDocument &document) const
4372 {
4373 // store base annotation properties
4374 storeBaseAnnotationProperties(node, document);
4375
4376 // create [sound] element
4377 QDomElement soundElement = document.createElement(QStringLiteral("sound"));
4378 node.appendChild(soundElement);
4379 }
4380
4381 Annotation::SubType SoundAnnotation::subType() const
4382 {
4383 return ASound;
4384 }
4385
4386 QString SoundAnnotation::soundIconName() const
4387 {
4388 Q_D(const SoundAnnotation);
4389 return d->icon;
4390 }
4391
4392 void SoundAnnotation::setSoundIconName(const QString &icon)
4393 {
4394 Q_D(SoundAnnotation);
4395 d->icon = icon;
4396 }
4397
4398 SoundObject *SoundAnnotation::sound() const
4399 {
4400 Q_D(const SoundAnnotation);
4401 return d->sound;
4402 }
4403
4404 void SoundAnnotation::setSound(SoundObject *s)
4405 {
4406 Q_D(SoundAnnotation);
4407 d->sound = s;
4408 }
4409
4410 /** MovieAnnotation [Annotation] */
4411 class MovieAnnotationPrivate : public AnnotationPrivate
4412 {
4413 public:
4414 MovieAnnotationPrivate();
4415 ~MovieAnnotationPrivate() override;
4416 std::unique_ptr<Annotation> makeAlias() override;
4417 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
4418
4419 // data fields
4420 MovieObject *movie;
4421 QString title;
4422 };
4423
4424 MovieAnnotationPrivate::MovieAnnotationPrivate() : movie(nullptr) { }
4425
4426 MovieAnnotationPrivate::~MovieAnnotationPrivate()
4427 {
4428 delete movie;
4429 }
4430
4431 std::unique_ptr<Annotation> MovieAnnotationPrivate::makeAlias()
4432 {
4433 return std::unique_ptr<Annotation>(new MovieAnnotation(*this));
4434 }
4435
4436 std::shared_ptr<Annot> MovieAnnotationPrivate::createNativeAnnot(::Page * /*destPage*/, DocumentData * /*doc*/)
4437 {
4438 return nullptr; // Not implemented
4439 }
4440
4441 MovieAnnotation::MovieAnnotation() : Annotation(*new MovieAnnotationPrivate()) { }
4442
4443 MovieAnnotation::MovieAnnotation(MovieAnnotationPrivate &dd) : Annotation(dd) { }
4444
4445 MovieAnnotation::MovieAnnotation(const QDomNode &node) : Annotation(*new MovieAnnotationPrivate(), node)
4446 {
4447 // loop through the whole children looking for a 'movie' element
4448 QDomNode subNode = node.firstChild();
4449 while (subNode.isElement()) {
4450 QDomElement e = subNode.toElement();
4451 subNode = subNode.nextSibling();
4452 if (e.tagName() != QLatin1String("movie")) {
4453 continue;
4454 }
4455
4456 // loading complete
4457 break;
4458 }
4459 }
4460
4461 MovieAnnotation::~MovieAnnotation() = default;
4462
4463 void MovieAnnotation::store(QDomNode &node, QDomDocument &document) const
4464 {
4465 // store base annotation properties
4466 storeBaseAnnotationProperties(node, document);
4467
4468 // create [movie] element
4469 QDomElement movieElement = document.createElement(QStringLiteral("movie"));
4470 node.appendChild(movieElement);
4471 }
4472
4473 Annotation::SubType MovieAnnotation::subType() const
4474 {
4475 return AMovie;
4476 }
4477
4478 MovieObject *MovieAnnotation::movie() const
4479 {
4480 Q_D(const MovieAnnotation);
4481 return d->movie;
4482 }
4483
4484 void MovieAnnotation::setMovie(MovieObject *movie)
4485 {
4486 Q_D(MovieAnnotation);
4487 d->movie = movie;
4488 }
4489
4490 QString MovieAnnotation::movieTitle() const
4491 {
4492 Q_D(const MovieAnnotation);
4493 return d->title;
4494 }
4495
4496 void MovieAnnotation::setMovieTitle(const QString &title)
4497 {
4498 Q_D(MovieAnnotation);
4499 d->title = title;
4500 }
4501
4502 /** ScreenAnnotation [Annotation] */
4503 class ScreenAnnotationPrivate : public AnnotationPrivate
4504 {
4505 public:
4506 ScreenAnnotationPrivate();
4507 ~ScreenAnnotationPrivate() override;
4508 std::unique_ptr<Annotation> makeAlias() override;
4509 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
4510
4511 // data fields
4512 LinkRendition *action;
4513 QString title;
4514 };
4515
4516 ScreenAnnotationPrivate::ScreenAnnotationPrivate() : action(nullptr) { }
4517
4518 ScreenAnnotationPrivate::~ScreenAnnotationPrivate()
4519 {
4520 delete action;
4521 }
4522
4523 ScreenAnnotation::ScreenAnnotation(ScreenAnnotationPrivate &dd) : Annotation(dd) { }
4524
4525 std::unique_ptr<Annotation> ScreenAnnotationPrivate::makeAlias()
4526 {
4527 return std::unique_ptr<ScreenAnnotation>(new ScreenAnnotation(*this));
4528 }
4529
4530 std::shared_ptr<Annot> ScreenAnnotationPrivate::createNativeAnnot(::Page * /*destPage*/, DocumentData * /*doc*/)
4531 {
4532 return nullptr; // Not implemented
4533 }
4534
4535 ScreenAnnotation::ScreenAnnotation() : Annotation(*new ScreenAnnotationPrivate()) { }
4536
4537 ScreenAnnotation::~ScreenAnnotation() = default;
4538
4539 void ScreenAnnotation::store(QDomNode &node, QDomDocument &document) const
4540 {
4541 // store base annotation properties
4542 storeBaseAnnotationProperties(node, document);
4543
4544 // create [screen] element
4545 QDomElement screenElement = document.createElement(QStringLiteral("screen"));
4546 node.appendChild(screenElement);
4547 }
4548
4549 Annotation::SubType ScreenAnnotation::subType() const
4550 {
4551 return AScreen;
4552 }
4553
4554 LinkRendition *ScreenAnnotation::action() const
4555 {
4556 Q_D(const ScreenAnnotation);
4557 return d->action;
4558 }
4559
4560 void ScreenAnnotation::setAction(LinkRendition *action)
4561 {
4562 Q_D(ScreenAnnotation);
4563 d->action = action;
4564 }
4565
4566 QString ScreenAnnotation::screenTitle() const
4567 {
4568 Q_D(const ScreenAnnotation);
4569 return d->title;
4570 }
4571
4572 void ScreenAnnotation::setScreenTitle(const QString &title)
4573 {
4574 Q_D(ScreenAnnotation);
4575 d->title = title;
4576 }
4577
4578 Link *ScreenAnnotation::additionalAction(AdditionalActionType type) const
4579 {
4580 Q_D(const ScreenAnnotation);
4581 return d->additionalAction(type);
4582 }
4583
4584 /** WidgetAnnotation [Annotation] */
4585 class WidgetAnnotationPrivate : public AnnotationPrivate
4586 {
4587 public:
4588 std::unique_ptr<Annotation> makeAlias() override;
4589 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
4590 };
4591
4592 std::unique_ptr<Annotation> WidgetAnnotationPrivate::makeAlias()
4593 {
4594 return std::unique_ptr<WidgetAnnotation>(new WidgetAnnotation(*this));
4595 }
4596
4597 std::shared_ptr<Annot> WidgetAnnotationPrivate::createNativeAnnot(::Page * /*destPage*/, DocumentData * /*doc*/)
4598 {
4599 return nullptr; // Not implemented
4600 }
4601
4602 WidgetAnnotation::WidgetAnnotation(WidgetAnnotationPrivate &dd) : Annotation(dd) { }
4603
4604 WidgetAnnotation::WidgetAnnotation() : Annotation(*new WidgetAnnotationPrivate()) { }
4605
4606 WidgetAnnotation::~WidgetAnnotation() = default;
4607
4608 void WidgetAnnotation::store(QDomNode &node, QDomDocument &document) const
4609 {
4610 // store base annotation properties
4611 storeBaseAnnotationProperties(node, document);
4612
4613 // create [widget] element
4614 QDomElement widgetElement = document.createElement(QStringLiteral("widget"));
4615 node.appendChild(widgetElement);
4616 }
4617
4618 Annotation::SubType WidgetAnnotation::subType() const
4619 {
4620 return AWidget;
4621 }
4622
4623 Link *WidgetAnnotation::additionalAction(AdditionalActionType type) const
4624 {
4625 Q_D(const WidgetAnnotation);
4626 return d->additionalAction(type);
4627 }
4628
4629 /** RichMediaAnnotation [Annotation] */
4630 class RichMediaAnnotation::Params::Private
4631 {
4632 public:
4633 Private() = default;
4634
4635 QString flashVars;
4636 };
4637
4638 RichMediaAnnotation::Params::Params() : d(new Private) { }
4639
4640 RichMediaAnnotation::Params::~Params() = default;
4641
4642 void RichMediaAnnotation::Params::setFlashVars(const QString &flashVars)
4643 {
4644 d->flashVars = flashVars;
4645 }
4646
4647 QString RichMediaAnnotation::Params::flashVars() const
4648 {
4649 return d->flashVars;
4650 }
4651
4652 class RichMediaAnnotation::Instance::Private
4653 {
4654 public:
4655 Private() : params(nullptr) { }
4656
4657 ~Private() { delete params; }
4658
4659 Private(const Private &) = delete;
4660 Private &operator=(const Private &) = delete;
4661
4662 RichMediaAnnotation::Instance::Type type;
4663 RichMediaAnnotation::Params *params;
4664 };
4665
4666 RichMediaAnnotation::Instance::Instance() : d(new Private) { }
4667
4668 RichMediaAnnotation::Instance::~Instance() = default;
4669
4670 void RichMediaAnnotation::Instance::setType(Type type)
4671 {
4672 d->type = type;
4673 }
4674
4675 RichMediaAnnotation::Instance::Type RichMediaAnnotation::Instance::type() const
4676 {
4677 return d->type;
4678 }
4679
4680 void RichMediaAnnotation::Instance::setParams(RichMediaAnnotation::Params *params)
4681 {
4682 delete d->params;
4683 d->params = params;
4684 }
4685
4686 RichMediaAnnotation::Params *RichMediaAnnotation::Instance::params() const
4687 {
4688 return d->params;
4689 }
4690
4691 class RichMediaAnnotation::Configuration::Private
4692 {
4693 public:
4694 Private() = default;
4695 ~Private()
4696 {
4697 qDeleteAll(instances);
4698 instances.clear();
4699 }
4700
4701 Private(const Private &) = delete;
4702 Private &operator=(const Private &) = delete;
4703
4704 RichMediaAnnotation::Configuration::Type type;
4705 QString name;
4706 QList<RichMediaAnnotation::Instance *> instances;
4707 };
4708
4709 RichMediaAnnotation::Configuration::Configuration() : d(new Private) { }
4710
4711 RichMediaAnnotation::Configuration::~Configuration() = default;
4712
4713 void RichMediaAnnotation::Configuration::setType(Type type)
4714 {
4715 d->type = type;
4716 }
4717
4718 RichMediaAnnotation::Configuration::Type RichMediaAnnotation::Configuration::type() const
4719 {
4720 return d->type;
4721 }
4722
4723 void RichMediaAnnotation::Configuration::setName(const QString &name)
4724 {
4725 d->name = name;
4726 }
4727
4728 QString RichMediaAnnotation::Configuration::name() const
4729 {
4730 return d->name;
4731 }
4732
4733 void RichMediaAnnotation::Configuration::setInstances(const QList<RichMediaAnnotation::Instance *> &instances)
4734 {
4735 qDeleteAll(d->instances);
4736 d->instances.clear();
4737
4738 d->instances = instances;
4739 }
4740
4741 QList<RichMediaAnnotation::Instance *> RichMediaAnnotation::Configuration::instances() const
4742 {
4743 return d->instances;
4744 }
4745
4746 class RichMediaAnnotation::Asset::Private
4747 {
4748 public:
4749 Private() : embeddedFile(nullptr) { }
4750
4751 ~Private() { delete embeddedFile; }
4752
4753 Private(const Private &) = delete;
4754 Private &operator=(const Private &) = delete;
4755
4756 QString name;
4757 EmbeddedFile *embeddedFile;
4758 };
4759
4760 RichMediaAnnotation::Asset::Asset() : d(new Private) { }
4761
4762 RichMediaAnnotation::Asset::~Asset() = default;
4763
4764 void RichMediaAnnotation::Asset::setName(const QString &name)
4765 {
4766 d->name = name;
4767 }
4768
4769 QString RichMediaAnnotation::Asset::name() const
4770 {
4771 return d->name;
4772 }
4773
4774 void RichMediaAnnotation::Asset::setEmbeddedFile(EmbeddedFile *embeddedFile)
4775 {
4776 delete d->embeddedFile;
4777 d->embeddedFile = embeddedFile;
4778 }
4779
4780 EmbeddedFile *RichMediaAnnotation::Asset::embeddedFile() const
4781 {
4782 return d->embeddedFile;
4783 }
4784
4785 class RichMediaAnnotation::Content::Private
4786 {
4787 public:
4788 Private() = default;
4789 ~Private()
4790 {
4791 qDeleteAll(configurations);
4792 configurations.clear();
4793
4794 qDeleteAll(assets);
4795 assets.clear();
4796 }
4797
4798 Private(const Private &) = delete;
4799 Private &operator=(const Private &) = delete;
4800
4801 QList<RichMediaAnnotation::Configuration *> configurations;
4802 QList<RichMediaAnnotation::Asset *> assets;
4803 };
4804
4805 RichMediaAnnotation::Content::Content() : d(new Private) { }
4806
4807 RichMediaAnnotation::Content::~Content() = default;
4808
4809 void RichMediaAnnotation::Content::setConfigurations(const QList<RichMediaAnnotation::Configuration *> &configurations)
4810 {
4811 qDeleteAll(d->configurations);
4812 d->configurations.clear();
4813
4814 d->configurations = configurations;
4815 }
4816
4817 QList<RichMediaAnnotation::Configuration *> RichMediaAnnotation::Content::configurations() const
4818 {
4819 return d->configurations;
4820 }
4821
4822 void RichMediaAnnotation::Content::setAssets(const QList<RichMediaAnnotation::Asset *> &assets)
4823 {
4824 qDeleteAll(d->assets);
4825 d->assets.clear();
4826
4827 d->assets = assets;
4828 }
4829
4830 QList<RichMediaAnnotation::Asset *> RichMediaAnnotation::Content::assets() const
4831 {
4832 return d->assets;
4833 }
4834
4835 class RichMediaAnnotation::Activation::Private
4836 {
4837 public:
4838 Private() : condition(RichMediaAnnotation::Activation::UserAction) { }
4839
4840 RichMediaAnnotation::Activation::Condition condition;
4841 };
4842
4843 RichMediaAnnotation::Activation::Activation() : d(new Private) { }
4844
4845 RichMediaAnnotation::Activation::~Activation() = default;
4846
4847 void RichMediaAnnotation::Activation::setCondition(Condition condition)
4848 {
4849 d->condition = condition;
4850 }
4851
4852 RichMediaAnnotation::Activation::Condition RichMediaAnnotation::Activation::condition() const
4853 {
4854 return d->condition;
4855 }
4856
4857 class RichMediaAnnotation::Deactivation::Private : public QSharedData
4858 {
4859 public:
4860 Private() : condition(RichMediaAnnotation::Deactivation::UserAction) { }
4861
4862 RichMediaAnnotation::Deactivation::Condition condition;
4863 };
4864
4865 RichMediaAnnotation::Deactivation::Deactivation() : d(new Private) { }
4866
4867 RichMediaAnnotation::Deactivation::~Deactivation() = default;
4868
4869 void RichMediaAnnotation::Deactivation::setCondition(Condition condition)
4870 {
4871 d->condition = condition;
4872 }
4873
4874 RichMediaAnnotation::Deactivation::Condition RichMediaAnnotation::Deactivation::condition() const
4875 {
4876 return d->condition;
4877 }
4878
4879 class RichMediaAnnotation::Settings::Private : public QSharedData
4880 {
4881 public:
4882 Private() : activation(nullptr), deactivation(nullptr) { }
4883
4884 RichMediaAnnotation::Activation *activation;
4885 RichMediaAnnotation::Deactivation *deactivation;
4886 };
4887
4888 RichMediaAnnotation::Settings::Settings() : d(new Private) { }
4889
4890 RichMediaAnnotation::Settings::~Settings() = default;
4891
4892 void RichMediaAnnotation::Settings::setActivation(RichMediaAnnotation::Activation *activation)
4893 {
4894 delete d->activation;
4895 d->activation = activation;
4896 }
4897
4898 RichMediaAnnotation::Activation *RichMediaAnnotation::Settings::activation() const
4899 {
4900 return d->activation;
4901 }
4902
4903 void RichMediaAnnotation::Settings::setDeactivation(RichMediaAnnotation::Deactivation *deactivation)
4904 {
4905 delete d->deactivation;
4906 d->deactivation = deactivation;
4907 }
4908
4909 RichMediaAnnotation::Deactivation *RichMediaAnnotation::Settings::deactivation() const
4910 {
4911 return d->deactivation;
4912 }
4913
4914 class RichMediaAnnotationPrivate : public AnnotationPrivate
4915 {
4916 public:
4917 RichMediaAnnotationPrivate() : settings(nullptr), content(nullptr) { }
4918
4919 ~RichMediaAnnotationPrivate() override;
4920
4921 std::unique_ptr<Annotation> makeAlias() override { return std::unique_ptr<RichMediaAnnotation>(new RichMediaAnnotation(*this)); }
4922
4923 std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override
4924 {
4925 Q_UNUSED(destPage);
4926 Q_UNUSED(doc);
4927
4928 return nullptr;
4929 }
4930
4931 RichMediaAnnotation::Settings *settings;
4932 RichMediaAnnotation::Content *content;
4933 };
4934
4935 RichMediaAnnotationPrivate::~RichMediaAnnotationPrivate()
4936 {
4937 delete settings;
4938 delete content;
4939 }
4940
4941 RichMediaAnnotation::RichMediaAnnotation() : Annotation(*new RichMediaAnnotationPrivate()) { }
4942
4943 RichMediaAnnotation::RichMediaAnnotation(RichMediaAnnotationPrivate &dd) : Annotation(dd) { }
4944
4945 RichMediaAnnotation::RichMediaAnnotation(const QDomNode &node) : Annotation(*new RichMediaAnnotationPrivate(), node)
4946 {
4947 // loop through the whole children looking for a 'richMedia' element
4948 QDomNode subNode = node.firstChild();
4949 while (subNode.isElement()) {
4950 QDomElement e = subNode.toElement();
4951 subNode = subNode.nextSibling();
4952 if (e.tagName() != QLatin1String("richMedia")) {
4953 continue;
4954 }
4955
4956 // loading complete
4957 break;
4958 }
4959 }
4960
4961 RichMediaAnnotation::~RichMediaAnnotation() = default;
4962
4963 void RichMediaAnnotation::store(QDomNode &node, QDomDocument &document) const
4964 {
4965 // store base annotation properties
4966 storeBaseAnnotationProperties(node, document);
4967
4968 // create [richMedia] element
4969 QDomElement richMediaElement = document.createElement(QStringLiteral("richMedia"));
4970 node.appendChild(richMediaElement);
4971 }
4972
4973 Annotation::SubType RichMediaAnnotation::subType() const
4974 {
4975 return ARichMedia;
4976 }
4977
4978 void RichMediaAnnotation::setSettings(RichMediaAnnotation::Settings *settings)
4979 {
4980 Q_D(RichMediaAnnotation);
4981
4982 delete d->settings;
4983 d->settings = settings;
4984 }
4985
4986 RichMediaAnnotation::Settings *RichMediaAnnotation::settings() const
4987 {
4988 Q_D(const RichMediaAnnotation);
4989
4990 return d->settings;
4991 }
4992
4993 void RichMediaAnnotation::setContent(RichMediaAnnotation::Content *content)
4994 {
4995 Q_D(RichMediaAnnotation);
4996
4997 delete d->content;
4998 d->content = content;
4999 }
5000
5001 RichMediaAnnotation::Content *RichMediaAnnotation::content() const
5002 {
5003 Q_D(const RichMediaAnnotation);
5004
5005 return d->content;
5006 }
5007
5008 // BEGIN utility annotation functions
5009 QColor convertAnnotColor(const AnnotColor *color)
5010 {
5011 if (!color) {
5012 return QColor();
5013 }
5014
5015 QColor newcolor;
5016 const std::array<double, 4> &color_data = color->getValues();
5017 switch (color->getSpace()) {
5018 case AnnotColor::colorTransparent: // = 0,
5019 newcolor = Qt::transparent;
5020 break;
5021 case AnnotColor::colorGray: // = 1,
5022 newcolor.setRgbF(color_data[0], color_data[0], color_data[0]);
5023 break;
5024 case AnnotColor::colorRGB: // = 3,
5025 newcolor.setRgbF(color_data[0], color_data[1], color_data[2]);
5026 break;
5027 case AnnotColor::colorCMYK: // = 4
5028 newcolor.setCmykF(color_data[0], color_data[1], color_data[2], color_data[3]);
5029 break;
5030 }
5031 return newcolor;
5032 }
5033
5034 std::unique_ptr<AnnotColor> convertQColor(const QColor &c)
5035 {
5036 if (c.alpha() == 0) {
5037 return {}; // Transparent
5038 }
5039
5040 switch (c.spec()) {
5041 case QColor::Rgb:
5042 case QColor::Hsl:
5043 case QColor::Hsv:
5044 return std::make_unique<AnnotColor>(c.redF(), c.greenF(), c.blueF());
5045 case QColor::Cmyk:
5046 return std::make_unique<AnnotColor>(c.cyanF(), c.magentaF(), c.yellowF(), c.blackF());
5047 case QColor::Invalid:
5048 default:
5049 return {};
5050 }
5051 }
5052 // END utility annotation functions
5053
5054 }