PoDoFo 1.0.0-dev
Loading...
Searching...
No Matches
PdfObject.h
1
7#ifndef PDF_OBJECT_H
8#define PDF_OBJECT_H
9
10#include "PdfVariant.h"
11#include "PdfObjectStream.h"
12
13namespace PoDoFo {
14
15class PdfEncrypt;
16class PdfIndirectObjectList;
17class PdfDictionary;
18class PdfArray;
19class PdfDocument;
20class PdfDataContainer;
21
34class PODOFO_API PdfObject
35{
36 friend class PdfIndirectObjectList;
37 friend class PdfArray;
38 friend class PdfDictionary;
39 friend class PdfDocument;
40 friend class PdfObjectStream;
41 friend class PdfObjectOutputStream;
42 friend class PdfDataContainer;
43 friend class PdfDictionaryElement;
44 friend class PdfArrayElement;
45 friend class PdfTokenizer;
46 PODOFO_PRIVATE_FRIEND(class PdfStreamedObjectStream);
47 PODOFO_PRIVATE_FRIEND(class PdfObjectStreamParser);
48 PODOFO_PRIVATE_FRIEND(class PdfParser);
49 PODOFO_PRIVATE_FRIEND(class PdfParserObject);
50 PODOFO_PRIVATE_FRIEND(class PdfWriter);
51 PODOFO_PRIVATE_FRIEND(class PdfImmediateWriter);
52 PODOFO_PRIVATE_FRIEND(class PdfXRef);
53 PODOFO_PRIVATE_FRIEND(class PdfXRefStream);
54
55public:
56 static const PdfObject Null;
57
58public:
59
62 PdfObject();
63
66 PdfObject(std::nullptr_t);
67
68 virtual ~PdfObject();
69
74 PdfObject(const PdfVariant& var);
75 PdfObject(PdfVariant&& var) noexcept;
76
81 PdfObject(bool b);
82
88
93 PdfObject(double d);
94
99 PdfObject(const PdfString& str);
100
105 PdfObject(const PdfName& name);
106
111 PdfObject(const PdfReference& ref);
112
117 PdfObject(const PdfArray& arr);
118 PdfObject(PdfArray&& arr) noexcept;
119
125 PdfObject(PdfDictionary&& dict) noexcept;
126
131 PdfObject(const PdfObject& rhs);
132 PdfObject(PdfObject&& rhs) noexcept;
133
134public:
138 PdfDataType GetDataType() const;
139
143 std::string_view GetDataTypeString() const;
144
147 bool IsBool() const;
148
151 bool IsNumber() const;
152
157 bool IsRealStrict() const;
158
161 bool IsNumberOrReal() const;
162
165 bool IsString() const;
166
169 bool IsName() const;
170
173 bool IsArray() const;
174
177 bool IsDictionary() const;
178
181 bool IsRawData() const;
182
185 bool IsNull() const;
186
189 bool IsReference() const;
190
195 std::string ToString(PdfWriteFlags writeFlags = PdfWriteFlags::None) const;
196 void ToString(std::string& str, PdfWriteFlags writeFlags = PdfWriteFlags::None) const;
197
201 bool GetBool() const;
202 bool TryGetBool(bool& value) const;
203
209 int64_t GetNumberLenient() const;
210 bool TryGetNumberLenient(int64_t& value) const;
211
217 int64_t GetNumber() const;
218 bool TryGetNumber(int64_t& value) const;
219
225 double GetReal() const;
226 bool TryGetReal(double& value) const;
227
233 double GetRealStrict() const;
234 bool TryGetRealStrict(double& value) const;
235
238 const PdfString& GetString() const;
239 bool TryGetString(PdfString& str) const;
240 bool TryGetString(const PdfString*& str) const;
241
244 const PdfName& GetName() const;
245 bool TryGetName(PdfName& name) const;
246 bool TryGetName(const PdfName*& name) const;
247
251 PdfReference GetReference() const;
252 bool TryGetReference(PdfReference& ref) const;
253
257 const PdfArray& GetArray() const;
258 PdfArray& GetArray();
259 bool TryGetArray(const PdfArray*& arr) const;
260 bool TryGetArray(PdfArray*& arr);
261
265 const PdfDictionary& GetDictionary() const;
266 PdfDictionary& GetDictionary();
267 bool TryGetDictionary(const PdfDictionary*& dict) const;
268 bool TryGetDictionary(PdfDictionary*& dict);
269
276 void SetBool(bool b);
277
284 void SetNumber(int64_t l);
285
292 void SetReal(double d);
293
300 void SetName(const PdfName& name);
301
308 void SetString(const PdfString& str);
309
310 void SetReference(const PdfReference& ref);
311
312 void ForceCreateStream();
313
323 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
324
330 PdfObjectStream& GetOrCreateStream();
331
332 /* Remove the object stream, if it has one
333 */
334 void RemoveStream();
335
339 const PdfObjectStream& MustGetStream() const;
340
344 PdfObjectStream& MustGetStream();
345
353 virtual bool TryUnload();
354
360 bool HasStream() const;
361
362 bool IsIndirect() const;
363
364 const PdfVariant& GetVariant() const;
365
366public:
371 bool operator<(const PdfObject& rhs) const;
372
376 bool operator==(const PdfObject& rhs) const;
377
381 bool operator!=(const PdfObject& rhs) const;
382
385 bool operator==(const PdfVariant& rhs) const;
386
389 bool operator!=(const PdfVariant& rhs) const;
390
396 PdfObject& operator=(const PdfObject& rhs);
397 PdfObject& operator=(PdfObject&& rhs) noexcept;
398
399 operator const PdfVariant& () const;
400
401public:
414 inline bool IsDirty() const { return m_IsDirty; }
415
419 inline PdfDocument* GetDocument() const { return m_Document; }
420
424 PdfDocument& MustGetDocument() const;
425
429 inline const PdfReference& GetIndirectReference() const { return m_IndirectReference; }
430
431 inline const PdfDataContainer* GetParent() const { return m_Parent; }
432
438 inline bool IsDelayedLoadDone() const { return m_IsDelayedLoadDone; }
439
440 inline bool IsDelayedLoadStreamDone() const { return m_IsDelayedLoadStreamDone; }
441
442 const PdfObjectStream* GetStream() const;
443 PdfObjectStream* GetStream();
444
445private:
446 PdfObject(PdfVariant&& var, const PdfReference& indirectReference, bool isDirty);
447
448 PdfObject(PdfArray* arr);
449
450protected:
459 void DelayedLoad() const;
460
475 virtual void delayedLoad();
476
477 virtual void delayedLoadStream();
478
482 virtual bool removeStream();
483
484 virtual bool HasStreamToParse() const;
485
490 void SetDirty();
491
492 void resetDirty();
493
499 void SetDocument(PdfDocument* document);
500
501 void SetVariantOwner();
502
503 void FreeStream();
504
505 PdfObjectStream& getOrCreateStream();
506
507 void forceCreateStream();
508
509 PdfObjectStream* getStream();
510
511 void DelayedLoadStream() const;
512
513 void delayedLoadStream() const;
514
515 void EnableDelayedLoadingStream();
516
517 inline void SetIndirectReference(const PdfReference& reference) { m_IndirectReference = reference; }
518
527 void EnableDelayedLoading();
528
532 virtual void SetRevised();
533
534private:
535 // To be called privately by various classes
536 PdfVariant& GetVariantUnsafe() { return m_Variant; }
537 PdfReference GetReferenceUnsafe() const { return m_Variant.GetReferenceUnsafe(); }
538 const PdfDictionary& GetDictionaryUnsafe() const { return m_Variant.GetDictionaryUnsafe(); }
539 const PdfArray& GetArrayUnsafe() const { return m_Variant.GetArrayUnsafe(); }
540 PdfDictionary& GetDictionaryUnsafe() { return m_Variant.GetDictionaryUnsafe(); }
541 PdfArray& GetArrayUnsafe() { return m_Variant.GetArrayUnsafe(); }
542 void WriteFinal(OutputStream& stream, PdfWriteFlags writeMode,
543 const PdfStatefulEncrypt* encrypt, charbuff& buffer);
544
545 // To be called by PdfStreamedObjectStream
546 void SetNumberNoDirtySet(int64_t l);
547
548 // To be called by PdfImmediateWriter
549 void SetImmutable();
550 void WriteHeader(OutputStream& stream, PdfWriteFlags writeMode, charbuff& buffer) const;
551
552 // To be called by PdfDataContainer
553 bool IsImmutable() const { return m_IsImmutable; }
554
555 // NOTE: It also doesn't dirty set the moved "obj"
556 void AssignNoDirtySet(PdfObject&& rhs);
557 void AssignNoDirtySet(PdfVariant&& rhs);
558 void AssignNoDirtySet(const PdfObject& rhs);
559
560 void SetParent(PdfDataContainer& parent);
561
562private:
563 void write(OutputStream& stream, bool skipLengthFix,
564 PdfWriteFlags writeMode, const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
565
566 void assertMutable() const;
567
568 void assign(const PdfObject& rhs);
569
570 void moveFrom(PdfObject&& rhs);
571
572 void ResetDirty();
573
574 void setDirty();
575
576 // See PdfVariant.h for a detailed explanation of this member, which is
577 // here to prevent accidental construction of a PdfObject of integer type
578 // when passing a pointer. */
579 template<typename T>
580 PdfObject(T*) = delete;
581
582 void copyStreamFrom(const PdfObject& obj);
583
584 void moveStreamFrom(PdfObject& obj);
585
586 // Shared initialization between all the ctors
587 void initObject();
588
589protected:
590 PdfVariant m_Variant;
591
592private:
593 PdfReference m_IndirectReference;
594 PdfDocument* m_Document;
595 PdfDataContainer* m_Parent;
596 std::unique_ptr<PdfObjectStream> m_Stream;
597 bool m_IsDirty; // Indicates if this object was modified after construction
598 bool m_IsImmutable;
599 mutable bool m_IsDelayedLoadDone;
600 mutable bool m_IsDelayedLoadStreamDone;
601 // Tracks whether deferred loading is still pending (in which case it'll be
602 // false). If true, deferred loading is not required or has been completed.
603};
604
607 template <typename T>
608 struct Object
609 {
610 static T Get(const PdfObject& obj)
611 {
612 (void)obj;
613 static_assert(always_false<T>, "Unsupported type");
614 return T{ };
615 }
616
617 static bool TryGet(const PdfObject& obj, T& value)
618 {
619 (void)obj;
620 (void)value;
621 static_assert(always_false<T>, "Unsupported type");
622 return false;
623 }
624 };
625
626 template <>
627 struct Object<bool>
628 {
629 static bool Get(const PdfObject& obj)
630 {
631 return obj.GetBool();
632 }
633
634 static bool TryGet(const PdfObject& obj, bool& value)
635 {
636 return obj.TryGetBool(value);
637 }
638 };
639
640 template <>
641 struct Object<int64_t>
642 {
643 static int64_t Get(const PdfObject& obj)
644 {
645 return obj.GetNumber();
646 }
647
648 static bool TryGet(const PdfObject& obj, int64_t& value)
649 {
650 return obj.TryGetNumber(value);
651 }
652 };
653
654 template <>
655 struct Object<double>
656 {
657 static double Get(const PdfObject& obj)
658 {
659 return obj.GetReal();
660 }
661
662 static bool TryGet(const PdfObject& obj, double& value)
663 {
664 return obj.TryGetReal(value);
665 }
666 };
667
668 template <>
669 struct Object<PdfReference>
670 {
671 static PdfReference Get(const PdfObject& obj)
672 {
673 return obj.GetReference();
674 }
675
676 static bool TryGet(const PdfObject& obj, PdfReference& value)
677 {
678 return obj.TryGetReference(value);
679 }
680 };
681
682 template <>
683 struct Object<PdfName>
684 {
685 static PdfName Get(const PdfObject& obj)
686 {
687 return obj.GetName();
688 }
689
690 static bool TryGet(const PdfObject& obj, PdfName& value)
691 {
692 return obj.TryGetName(value);
693 }
694 };
695
696 template <>
697 struct Object<const PdfName*>
698 {
699 static const PdfName* Get(const PdfObject& obj)
700 {
701 return &obj.GetName();
702 }
703
704 static bool TryGet(const PdfObject& obj, const PdfName*& value)
705 {
706 return obj.TryGetName(value);
707 }
708 };
709
710 template <>
711 struct Object<PdfString>
712 {
713 static PdfString Get(const PdfObject& obj)
714 {
715 return obj.GetString();
716 }
717
718 static bool TryGet(const PdfObject& obj, PdfString& value)
719 {
720 return obj.TryGetString(value);
721 }
722 };
723
724 template <>
725 struct Object<const PdfString*>
726 {
727 static const PdfString* Get(const PdfObject& obj)
728 {
729 return &obj.GetString();
730 }
731
732 static bool TryGet(const PdfObject& obj, const PdfString*& value)
733 {
734 return obj.TryGetString(value);
735 }
736 };
737
738 template <>
739 struct Object<const PdfDictionary*>
740 {
741 static const PdfDictionary* Get(const PdfObject& obj)
742 {
743 return &obj.GetDictionary();
744 }
745
746 static bool TryGet(const PdfObject& obj, const PdfDictionary*& value)
747 {
748 return obj.TryGetDictionary(value);
749 }
750 };
751
752 template <>
753 struct Object<PdfDictionary*>
754 {
755 static PdfDictionary* Get(PdfObject& obj)
756 {
757 return &obj.GetDictionary();
758 }
759
760 static bool TryGet(PdfObject& obj, PdfDictionary*& value)
761 {
762 return obj.TryGetDictionary(value);
763 }
764 };
765
766 template <>
767 struct Object<const PdfArray*>
768 {
769 static const PdfArray* Get(const PdfObject& obj)
770 {
771 return &obj.GetArray();
772 }
773
774 static bool TryGet(const PdfObject& obj, const PdfArray*& value)
775 {
776 return obj.TryGetArray(value);
777 }
778 };
779
780 template <>
781 struct Object<PdfArray*>
782 {
783 static PdfArray* Get(PdfObject& obj)
784 {
785 return &obj.GetArray();
786 }
787
788 static bool TryGet(PdfObject& obj, PdfArray*& value)
789 {
790 return obj.TryGetArray(value);
791 }
792 };
793
794 // Comparator to enable heterogeneous lookup with
795 // both objects and references
796 // See https://stackoverflow.com/a/31924435/213871
797 struct PODOFO_API PdfObjectInequality final
798 {
799 using is_transparent = std::true_type;
800
801 bool operator()(const PdfObject* lhs, const PdfObject* rhs) const
802 {
803 return lhs->GetIndirectReference() < rhs->GetIndirectReference();
804 }
805 bool operator()(const PdfObject* lhs, const PdfReference& rhs) const
806 {
807 return lhs->GetIndirectReference() < rhs;
808 }
809 bool operator()(const PdfReference& lhs, const PdfObject* rhs) const
810 {
811 return lhs < rhs->GetIndirectReference();
812 }
813 bool operator()(const PdfObject& lhs, const PdfObject& rhs) const
814 {
815 return lhs.GetIndirectReference() < rhs.GetIndirectReference();
816 }
817 bool operator()(const PdfObject& lhs, const PdfReference& rhs) const
818 {
819 return lhs.GetIndirectReference() < rhs;
820 }
821 bool operator()(const PdfReference& lhs, const PdfObject& rhs) const
822 {
823 return lhs < rhs.GetIndirectReference();
824 }
825 };
826}
827
828#endif // PDF_OBJECT_H
An interface for writing blocks of data to a data source.
Definition OutputStream.h:18
This class represents a PdfArray Use it for all arrays that are written to a PDF file.
Definition PdfArray.h:81
A PdfDataProvider object with a PdfObject owner, specialized in holding objects.
Definition PdfDataContainer.h:22
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:82
PdfDocument is the core interface for working with PDF documents.
Definition PdfDocument.h:111
A list of PdfObjects that constitutes the indirect object list of the document The PdfParser will rea...
Definition PdfIndirectObjectList.h:30
This class represents a PdfName.
Definition PdfName.h:24
A PDF stream can be appended to any PdfObject and can contain arbitrary data.
Definition PdfObjectStream.h:87
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:35
bool IsDelayedLoadDone() const
Returns true if delayed loading is disabled, or if it is enabled and loading has completed.
Definition PdfObject.h:438
PdfDocument * GetDocument() const
Get the document of this object.
Definition PdfObject.h:419
PdfObject(std::nullptr_t)
Create a "null" PDF object.
bool IsDirty() const
The dirty flag is set if this variant has been modified after construction.
Definition PdfObject.h:414
const PdfReference & GetIndirectReference() const
Get an indirect reference to this object.
Definition PdfObject.h:429
A reference is a pointer to a object in the PDF file of the form "4 0 R", where 4 is the object numbe...
Definition PdfReference.h:24
A string that can be written to a PDF document.
Definition PdfString.h:24
A simple tokenizer for PDF files and PDF content streams.
Definition PdfTokenizer.h:36
A variant data type which supports all data types supported by the PDF standard.
Definition PdfVariant.h:33
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:38
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition basetypes.h:16
PdfDataType
Every PDF datatype that can occur in a PDF file is referenced by an own enum (e.g.
Definition PdfDeclarations.h:167
@ Null
The null datatype is always null.
PdfWriteFlags
Specify additional options for writing the PDF.
Definition PdfDeclarations.h:149
Templatized object type getter helper.
Definition PdfObject.h:609