PoDoFo 1.1.0
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 bool TryGetArray(PdfArray& arr) const;
262
266 const PdfDictionary& GetDictionary() const;
267 PdfDictionary& GetDictionary();
268 bool TryGetDictionary(const PdfDictionary*& dict) const;
269 bool TryGetDictionary(PdfDictionary*& dict);
270 bool TryGetDictionary(PdfDictionary& dict) const;
271
278 void SetBool(bool b);
279
286 void SetNumber(int64_t l);
287
294 void SetReal(double d);
295
302 void SetName(const PdfName& name);
303
310 void SetString(const PdfString& str);
311
312 void SetReference(const PdfReference& ref);
313
314 void ForceCreateStream();
315
325 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
326
332 PdfObjectStream& GetOrCreateStream();
333
334 /* Remove the object stream, if it has one
335 */
336 void RemoveStream();
337
341 const PdfObjectStream& MustGetStream() const;
342
346 PdfObjectStream& MustGetStream();
347
355 virtual bool TryUnload();
356
362 bool HasStream() const;
363
364 bool IsIndirect() const;
365
366 const PdfVariant& GetVariant() const;
367
368public:
373 bool operator<(const PdfObject& rhs) const;
374
378 bool operator==(const PdfObject& rhs) const;
379
383 bool operator!=(const PdfObject& rhs) const;
384
387 bool operator==(const PdfVariant& rhs) const;
388
391 bool operator!=(const PdfVariant& rhs) const;
392
398 PdfObject& operator=(const PdfObject& rhs);
399 PdfObject& operator=(PdfObject&& rhs) noexcept;
400
401 operator const PdfVariant& () const;
402
403public:
416 inline bool IsDirty() const { return m_IsDirty; }
417
421 inline PdfDocument* GetDocument() const { return m_Document; }
422
426 PdfDocument& MustGetDocument() const;
427
431 inline const PdfReference& GetIndirectReference() const { return m_IndirectReference; }
432
433 inline const PdfDataContainer* GetParent() const { return m_Parent; }
434
440 inline bool IsDelayedLoadDone() const { return m_IsDelayedLoadDone; }
441
442 inline bool IsDelayedLoadStreamDone() const { return m_IsDelayedLoadStreamDone; }
443
444 const PdfObjectStream* GetStream() const;
445 PdfObjectStream* GetStream();
446
447private:
448 PdfObject(PdfVariant&& var, const PdfReference& indirectReference, bool isDirty);
449
450 PdfObject(PdfArray* arr);
451
452protected:
461 void DelayedLoad() const;
462
477 virtual void delayedLoad();
478
479 virtual void delayedLoadStream();
480
484 virtual bool removeStream();
485
486 virtual bool HasStreamToParse() const;
487
492 void SetDirty();
493
494 void resetDirty();
495
501 void SetDocument(PdfDocument* document);
502
503 void SetVariantOwner();
504
505 void FreeStream();
506
507 PdfObjectStream& getOrCreateStream();
508
509 void forceCreateStream();
510
511 PdfObjectStream* getStream();
512
513 void DelayedLoadStream() const;
514
515 void EnableDelayedLoadingStream();
516
517 void MakeDelayedLoadingStreamDone();
518
519 inline void SetIndirectReference(const PdfReference& reference) { m_IndirectReference = reference; }
520
529 void EnableDelayedLoading();
530
534 virtual void SetRevised();
535
536private:
537 // To be called privately by various classes
538 PdfVariant& GetVariantUnsafe() { return m_Variant; }
539 PdfReference GetReferenceUnsafe() const { return m_Variant.GetReferenceUnsafe(); }
540 const PdfDictionary& GetDictionaryUnsafe() const { return m_Variant.GetDictionaryUnsafe(); }
541 const PdfArray& GetArrayUnsafe() const { return m_Variant.GetArrayUnsafe(); }
542 PdfDictionary& GetDictionaryUnsafe() { return m_Variant.GetDictionaryUnsafe(); }
543 PdfArray& GetArrayUnsafe() { return m_Variant.GetArrayUnsafe(); }
544 void WriteFinal(OutputStream& stream, PdfWriteFlags writeMode,
545 const PdfStatefulEncrypt* encrypt, charbuff& buffer);
546
547 // To be called by PdfStreamedObjectStream
548 void SetNumberNoDirtySet(int64_t l);
549
550 // To be called by PdfImmediateWriter
551 void SetImmutable();
552 void WriteHeader(OutputStream& stream, PdfWriteFlags writeMode, charbuff& buffer) const;
553
554 // To be called by PdfDataContainer
555 bool IsImmutable() const { return m_IsImmutable; }
556
557 // NOTE: It also doesn't dirty set the moved "obj"
558 void AssignNoDirtySet(PdfObject&& rhs);
559 void AssignNoDirtySet(PdfVariant&& rhs);
560 void AssignNoDirtySet(const PdfObject& rhs);
561
562 void SetParent(PdfDataContainer& parent);
563
564private:
565 void write(OutputStream& stream, bool skipLengthFix,
566 PdfWriteFlags writeMode, const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
567
568 void assertMutable() const;
569
570 void assign(const PdfObject& rhs);
571
572 void moveFrom(PdfObject&& rhs);
573
574 void ResetDirty();
575
576 void setDirty();
577
578 // See PdfVariant.h for a detailed explanation of this member, which is
579 // here to prevent accidental construction of a PdfObject of integer type
580 // when passing a pointer. */
581 template<typename T>
582 PdfObject(T*) = delete;
583
584 void copyStreamFrom(const 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>
609 {
610 using TRet = T;
611
612 static TRet Get(const PdfObject& obj)
613 {
614 (void)obj;
615 static_assert(always_false<T>, "Unsupported type");
616 return TRet{ };
617 }
618
619 static TRet Get(const PdfObject& obj, const T& defVal)
620 {
621 (void)obj;
622 (void)defVal;
623 static_assert(always_false<T>, "Unsupported type");
624 return TRet{ };
625 }
626
627 static bool TryGet(const PdfObject& obj, T& value)
628 {
629 (void)obj;
630 (void)value;
631 static_assert(always_false<T>, "Unsupported type");
632 return false;
633 }
634 };
635
636 template <>
637 struct ObjectAdapter<bool>
638 {
639 using TRet = bool;
640
641 static bool Get(const PdfObject& obj)
642 {
643 return obj.GetBool();
644 }
645
646 static bool Get(const PdfObject& obj, bool fallback)
647 {
648 bool ret;
649 if (obj.TryGetBool(ret))
650 return ret;
651 else
652 return fallback;
653 }
654
655 static bool TryGet(const PdfObject& obj, bool& value)
656 {
657 return obj.TryGetBool(value);
658 }
659 };
660
661 template <>
662 struct ObjectAdapter<int64_t>
663 {
664 using TRet = int64_t;
665
666 static int64_t Get(const PdfObject& obj)
667 {
668 return obj.GetNumber();
669 }
670
671 static int64_t Get(const PdfObject& obj, int64_t fallback)
672 {
673 int64_t ret;
674 if (obj.TryGetNumber(ret))
675 return ret;
676 else
677 return fallback;
678 }
679
680 static bool TryGet(const PdfObject& obj, int64_t& value)
681 {
682 return obj.TryGetNumber(value);
683 }
684 };
685
686 template <>
687 struct ObjectAdapter<double>
688 {
689 using TRet = double;
690
691 static double Get(const PdfObject& obj)
692 {
693 return obj.GetReal();
694 }
695
696 static double Get(const PdfObject& obj, double fallback)
697 {
698 double ret;
699 if (obj.TryGetReal(ret))
700 return ret;
701 else
702 return fallback;
703 }
704
705 static bool TryGet(const PdfObject& obj, double& value)
706 {
707 return obj.TryGetReal(value);
708 }
709 };
710
711 template <>
712 struct ObjectAdapter<PdfReference>
713 {
714 using TRet = PdfReference;
715
716 static PdfReference Get(const PdfObject& obj)
717 {
718 return obj.GetReference();
719 }
720
721 static PdfReference Get(const PdfObject& obj, const PdfReference& fallback)
722 {
723 PdfReference ret;
724 if (obj.TryGetReference(ret))
725 return ret;
726 else
727 return fallback;
728 }
729
730 static bool TryGet(const PdfObject& obj, PdfReference& value)
731 {
732 return obj.TryGetReference(value);
733 }
734 };
735
736 template <>
737 struct ObjectAdapter<PdfName>
738 {
739 using TRet = const PdfName&;
740
741 static const PdfName& Get(const PdfObject& obj)
742 {
743 return obj.GetName();
744 }
745
746 static const PdfName& Get(const PdfObject& obj, const PdfName& fallback)
747 {
748 const PdfName* ret;
749 if (obj.TryGetName(ret))
750 return *ret;
751 else
752 return fallback;
753 }
754
755 static bool TryGet(const PdfObject& obj, PdfName& value)
756 {
757 return obj.TryGetName(value);
758 }
759 };
760
761 template <>
762 struct ObjectAdapter<const PdfName*>
763 {
764 using TRet = const PdfName*;
765
766 static const PdfName* Get(const PdfObject& obj)
767 {
768 const PdfName* ret;
769 (void)obj.TryGetName(ret);
770 return ret;
771 }
772
773 static const PdfName* Get(const PdfObject& obj, const PdfName* fallback)
774 {
775 const PdfName* ret;
776 if (obj.TryGetName(ret))
777 return ret;
778 else
779 return fallback;
780 }
781
782 static bool TryGet(const PdfObject& obj, const PdfName*& value)
783 {
784 return obj.TryGetName(value);
785 }
786 };
787
788 template <>
789 struct ObjectAdapter<PdfString>
790 {
791 using TRet = const PdfString&;
792
793 static const PdfString& Get(const PdfObject& obj)
794 {
795 return obj.GetString();
796 }
797
798 static const PdfString& Get(const PdfObject& obj, const PdfString& fallback)
799 {
800 const PdfString* ret;
801 if (obj.TryGetString(ret))
802 return *ret;
803 else
804 return fallback;
805 }
806
807 static bool TryGet(const PdfObject& obj, PdfString& value)
808 {
809 return obj.TryGetString(value);
810 }
811 };
812
813 template <>
814 struct ObjectAdapter<const PdfString*>
815 {
816 using TRet = const PdfString*;
817
818 static const PdfString* Get(PdfObject& obj)
819 {
820 const PdfString* ret;
821 (void)obj.TryGetString(ret);
822 return ret;
823 }
824
825 static const PdfString* Get(const PdfObject& obj, const PdfString* fallback)
826 {
827 const PdfString* ret;
828 if (obj.TryGetString(ret))
829 return ret;
830 else
831 return fallback;
832 }
833
834 static bool TryGet(const PdfObject& obj, const PdfString*& value)
835 {
836 return obj.TryGetString(value);
837 }
838 };
839
840 template <>
841 struct ObjectAdapter<PdfDictionary*>
842 {
843 using TRet = PdfDictionary*;
844
845 static PdfDictionary* Get(PdfObject& obj)
846 {
847 PdfDictionary* ret;
848 (void)obj.TryGetDictionary(ret);
849 return ret;
850 }
851
852 static PdfDictionary* Get(PdfObject& obj, PdfDictionary* fallback)
853 {
854 PdfDictionary* ret;
855 if (obj.TryGetDictionary(ret))
856 return ret;
857 else
858 return fallback;
859 }
860
861 static bool TryGet(PdfObject& obj, PdfDictionary*& value)
862 {
863 return obj.TryGetDictionary(value);
864 }
865 };
866
867 template <>
868 struct ObjectAdapter<const PdfDictionary*>
869 {
870 using TRet = const PdfDictionary*;
871
872 static const PdfDictionary* Get(const PdfObject& obj)
873 {
874 const PdfDictionary* ret;
875 (void)obj.TryGetDictionary(ret);
876 return ret;
877 }
878
879 static const PdfDictionary* Get(const PdfObject& obj, const PdfDictionary* fallback)
880 {
881 const PdfDictionary* ret;
882 if (obj.TryGetDictionary(ret))
883 return ret;
884 else
885 return fallback;
886 }
887
888 static bool TryGet(const PdfObject& obj, const PdfDictionary*& value)
889 {
890 return obj.TryGetDictionary(value);
891 }
892 };
893
894 template <>
895 struct ObjectAdapter<PdfDictionary>
896 {
897 using TRet = PdfDictionary&;
898
899 static PdfDictionary& Get(PdfObject& obj)
900 {
901 return obj.GetDictionary();
902 }
903
904 static PdfDictionary& Get(PdfObject& obj, PdfDictionary& fallback)
905 {
906 PdfDictionary* ret;
907 if (obj.TryGetDictionary(ret))
908 return *ret;
909 else
910 return fallback;
911 }
912
913 static bool TryGet(const PdfObject& obj, PdfDictionary& value)
914 {
915 return obj.TryGetDictionary(value);
916 }
917 };
918
919 template <>
920 struct ObjectAdapter<PdfArray*>
921 {
922 using TRet = PdfArray*;
923
924 static PdfArray* Get(PdfObject& obj)
925 {
926 PdfArray* ret;
927 (void)obj.TryGetArray(ret);
928 return ret;
929 }
930
931 static PdfArray* Get(PdfObject& obj, PdfArray* fallback)
932 {
933 PdfArray* ret;
934 if (obj.TryGetArray(ret))
935 return ret;
936 else
937 return fallback;
938 }
939
940 static bool TryGet(PdfObject& obj, PdfArray*& value)
941 {
942 return obj.TryGetArray(value);
943 }
944 };
945
946 template <>
947 struct ObjectAdapter<const PdfArray*>
948 {
949 using TRet = const PdfArray*;
950
951 static const PdfArray* Get(const PdfObject& obj)
952 {
953 const PdfArray* ret;
954 (void)obj.TryGetArray(ret);
955 return ret;
956 }
957
958 static const PdfArray* Get(PdfObject& obj, const PdfArray* fallback)
959 {
960 const PdfArray* ret;
961 if (obj.TryGetArray(ret))
962 return ret;
963 else
964 return fallback;
965 }
966
967 static bool TryGet(const PdfObject& obj, const PdfArray*& value)
968 {
969 return obj.TryGetArray(value);
970 }
971 };
972
973 template <>
974 struct ObjectAdapter<PdfArray>
975 {
976 using TRet = PdfArray&;
977
978 static PdfArray& Get(PdfObject& obj)
979 {
980 return obj.GetArray();
981 }
982
983 static PdfArray& Get(PdfObject& obj, PdfArray& fallback)
984 {
985 PdfArray* ret;
986 if (obj.TryGetArray(ret))
987 return *ret;
988 else
989 return fallback;
990 }
991
992 static bool TryGet(const PdfObject& obj, PdfArray& value)
993 {
994 return obj.TryGetArray(value);
995 }
996 };
997
998 // Comparator to enable heterogeneous lookup with
999 // both objects and references
1000 // See https://stackoverflow.com/a/31924435/213871
1001 struct PODOFO_API PdfObjectInequality final
1002 {
1003 using is_transparent = std::true_type;
1004
1005 bool operator()(const PdfObject* lhs, const PdfObject* rhs) const
1006 {
1007 return lhs->GetIndirectReference() < rhs->GetIndirectReference();
1008 }
1009 bool operator()(const PdfObject* lhs, const PdfReference& rhs) const
1010 {
1011 return lhs->GetIndirectReference() < rhs;
1012 }
1013 bool operator()(const PdfReference& lhs, const PdfObject* rhs) const
1014 {
1015 return lhs < rhs->GetIndirectReference();
1016 }
1017 bool operator()(const PdfObject& lhs, const PdfObject& rhs) const
1018 {
1019 return lhs.GetIndirectReference() < rhs.GetIndirectReference();
1020 }
1021 bool operator()(const PdfObject& lhs, const PdfReference& rhs) const
1022 {
1023 return lhs.GetIndirectReference() < rhs;
1024 }
1025 bool operator()(const PdfReference& lhs, const PdfObject& rhs) const
1026 {
1027 return lhs < rhs.GetIndirectReference();
1028 }
1029 };
1030}
1031
1032#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:440
PdfDocument * GetDocument() const
Get the document of this object.
Definition PdfObject.h:421
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:416
const PdfReference & GetIndirectReference() const
Get an indirect reference to this object.
Definition PdfObject.h:431
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