PoDoFo  1.0.0-dev
PdfXObject.h
1 
7 #ifndef PDF_XOBJECT_H
8 #define PDF_XOBJECT_H
9 
10 #include "PdfElement.h"
11 #include "PdfArray.h"
12 #include <podofo/auxiliary/Matrix.h>
13 #include <podofo/auxiliary/Rect.h>
14 
15 namespace PoDoFo {
16 
17 class PdfImage;
18 class PdfXObjectForm;
19 class PdfXObjectPostScript;
20 
29 class PODOFO_API PdfXObject : public PdfDictionaryElement
30 {
31  friend class PdfXObjectForm;
32  friend class PdfImage;
33  friend class PdfXObjectPostScript;
34  friend class PdfContentStreamReader;
35 
36 private:
37  PdfXObject(PdfDocument& doc, PdfXObjectType subType);
38  PdfXObject(PdfObject& obj, PdfXObjectType subType);
39 
40 public:
41  static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<PdfXObject>& xobj);
42 
43  static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const PdfXObject>& xobj);
44 
45  template <typename XObjectT>
46  static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<XObjectT>& xobj);
47 
48  template <typename XObjectT>
49  static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const XObjectT>& xobj);
50 
51  virtual Rect GetRect() const = 0;
52 
53  Matrix GetMatrix() const;
54 
55  inline PdfXObjectType GetType() const { return m_Type; }
56 
57 private:
58  // To be called from PdfContentStreamReader
59  static std::unique_ptr<PdfXObject> CreateFromObject(const PdfObject& obj, PdfXObjectType reqType, PdfXObjectType& detectedType);
60 
61  static PdfXObject* createFromObject(const PdfObject& obj, PdfXObjectType reqType, PdfXObjectType& detectedType);
62  static PdfXObjectType getPdfXObjectType(const PdfObject& obj);
63  template <typename TXObject>
64  static constexpr PdfXObjectType GetXObjectType();
65 
66 private:
67  PdfXObjectType m_Type;
68 };
69 
70 template<typename XObjectT>
71 inline bool PdfXObject::TryCreateFromObject(PdfObject& obj, std::unique_ptr<XObjectT>& xobj)
72 {
73  PdfXObjectType detectedType;
74  xobj.reset((XObjectT*)createFromObject(obj, GetXObjectType<XObjectT>(), detectedType));
75  return xobj != nullptr;
76 }
77 
78 template<typename XObjectT>
79 inline bool PdfXObject::TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const XObjectT>& xobj)
80 {
81  PdfXObjectType detectedType;
82  xobj.reset((const XObjectT*)createFromObject(obj, GetXObjectType<XObjectT>(), detectedType));
83  return xobj != nullptr;
84 }
85 
86 template<typename TXObject>
87 constexpr PdfXObjectType PdfXObject::GetXObjectType()
88 {
89  if (std::is_same_v<TXObject, PdfXObjectForm>)
90  return PdfXObjectType::Form;
91  else if (std::is_same_v<TXObject, PdfImage>)
92  return PdfXObjectType::Image;
93  else if (std::is_same_v<TXObject, PdfXObjectPostScript>)
94  return PdfXObjectType::PostScript;
95  else
96  return PdfXObjectType::Unknown;
97 }
98 
99 };
100 
101 #endif // PDF_XOBJECT_H
102 
103 
Reader class to read content streams.
Definition: PdfContentStreamReader.h:82
PdfDocument is the core interface for working with PDF documents.
Definition: PdfDocument.h:108
A PdfImage object is needed when ever you want to embed an image file into a PDF document.
Definition: PdfImage.h:44
This class represents a PDF indirect Object in memory.
Definition: PdfObject.h:35
A XObject is a content stream with several drawing commands and data which can be used throughout a P...
Definition: PdfXObject.h:30
A rectangle defined by position and size.
Definition: Rect.h:20
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16