1 : /******************************************************************************
2 : * $Id: pdfobject.h 24069 2012-03-04 18:12:24Z rouault $
3 : *
4 : * Project: PDF driver
5 : * Purpose: GDALDataset driver for PDF dataset.
6 : * Author: Even Rouault, <even dot rouault at mines dash paris dot org>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2011, Even Rouault
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifndef PDFOBJECT_H_INCLUDED
31 : #define PDFOBJECT_H_INCLUDED
32 :
33 : #include "cpl_string.h"
34 : #include <map>
35 : #include <vector>
36 :
37 : #ifdef HAVE_POPPLER
38 :
39 : /* begin of poppler xpdf includes */
40 : #include <poppler/Object.h>
41 :
42 : #define private public /* Ugly! Page::pageObj is private but we need it... */
43 : #include <poppler/Page.h>
44 : #undef private
45 :
46 : #include <poppler/Dict.h>
47 :
48 : #define private public /* Ugly! Catalog::optContent is private but we need it... */
49 : #include <poppler/Catalog.h>
50 : #undef private
51 :
52 : #define private public /* Ugly! PDFDoc::str is private but we need it... */
53 : #include <poppler/PDFDoc.h>
54 : #undef private
55 :
56 : #include <poppler/splash/SplashBitmap.h>
57 : #include <poppler/splash/Splash.h>
58 : #include <poppler/SplashOutputDev.h>
59 : #include <poppler/GlobalParams.h>
60 : #include <poppler/ErrorCodes.h>
61 : /* end of poppler xpdf includes */
62 :
63 : #endif // HAVE_POPPLER
64 :
65 : #ifdef HAVE_PODOFO
66 : #include "podofo.h"
67 : #endif // HAVE_PODOFO
68 :
69 :
70 : double ROUND_TO_INT_IF_CLOSE(double x, double eps = 0);
71 :
72 : typedef enum
73 : {
74 : PDFObjectType_Unknown,
75 : PDFObjectType_Null,
76 : PDFObjectType_Bool,
77 : PDFObjectType_Int,
78 : PDFObjectType_Real,
79 : PDFObjectType_String,
80 : PDFObjectType_Name,
81 : PDFObjectType_Array,
82 : PDFObjectType_Dictionary
83 : } GDALPDFObjectType;
84 :
85 : class GDALPDFDictionary;
86 : class GDALPDFArray;
87 : class GDALPDFStream;
88 :
89 : class GDALPDFObjectRW;
90 : class GDALPDFDictionaryRW;
91 : class GDALPDFArrayRW;
92 :
93 : class GDALPDFObject
94 32594 : {
95 : protected:
96 : virtual const char* GetTypeNameNative() = 0;
97 :
98 : public:
99 : virtual ~GDALPDFObject();
100 :
101 : virtual GDALPDFObjectType GetType() = 0;
102 : virtual const char* GetTypeName();
103 : virtual int GetBool() = 0;
104 : virtual int GetInt() = 0;
105 : virtual double GetReal() = 0;
106 0 : virtual int CanRepresentRealAsString() { return FALSE; }
107 : virtual const CPLString& GetString() = 0;
108 : virtual const CPLString& GetName() = 0;
109 : virtual GDALPDFDictionary* GetDictionary() = 0;
110 : virtual GDALPDFArray* GetArray() = 0;
111 : virtual GDALPDFStream* GetStream() = 0;
112 : virtual int GetRefNum() = 0;
113 : virtual int GetRefGen() = 0;
114 :
115 : void Serialize(CPLString& osStr);
116 512 : CPLString Serialize() { CPLString osStr; Serialize(osStr); return osStr; }
117 : GDALPDFObjectRW* Clone();
118 : };
119 :
120 : class GDALPDFDictionary
121 6992 : {
122 : public:
123 : virtual ~GDALPDFDictionary();
124 :
125 : virtual GDALPDFObject* Get(const char* pszKey) = 0;
126 : virtual std::map<CPLString, GDALPDFObject*>& GetValues() = 0;
127 :
128 : void Serialize(CPLString& osStr);
129 1090 : CPLString Serialize() { CPLString osStr; Serialize(osStr); return osStr; }
130 : GDALPDFDictionaryRW* Clone();
131 : };
132 :
133 : class GDALPDFArray
134 3018 : {
135 : public:
136 : virtual ~GDALPDFArray();
137 :
138 : virtual int GetLength() = 0;
139 : virtual GDALPDFObject* Get(int nIndex) = 0;
140 :
141 : void Serialize(CPLString& osStr);
142 2 : CPLString Serialize() { CPLString osStr; Serialize(osStr); return osStr; }
143 : GDALPDFArrayRW* Clone();
144 : };
145 :
146 : class GDALPDFStream
147 32 : {
148 : public:
149 : virtual ~GDALPDFStream();
150 :
151 : virtual int GetLength() = 0;
152 : virtual char* GetBytes() = 0;
153 : };
154 :
155 : class GDALPDFObjectRW : public GDALPDFObject
156 : {
157 : private:
158 : GDALPDFObjectType m_eType;
159 : int m_nVal;
160 : double m_dfVal;
161 : CPLString m_osVal;
162 : GDALPDFDictionaryRW *m_poDict;
163 : GDALPDFArrayRW *m_poArray;
164 : int m_nNum;
165 : int m_nGen;
166 : int m_bCanRepresentRealAsString;
167 :
168 : GDALPDFObjectRW(GDALPDFObjectType eType);
169 :
170 : protected:
171 : virtual const char* GetTypeNameNative();
172 :
173 : public:
174 :
175 : static GDALPDFObjectRW* CreateIndirect(int nNum, int nGen);
176 : static GDALPDFObjectRW* CreateNull();
177 : static GDALPDFObjectRW* CreateBool(int bVal);
178 : static GDALPDFObjectRW* CreateInt(int nVal);
179 : static GDALPDFObjectRW* CreateReal(double dfVal, int bCanRepresentRealAsString = FALSE);
180 : static GDALPDFObjectRW* CreateString(const char* pszStr);
181 : static GDALPDFObjectRW* CreateName(const char* pszName);
182 : static GDALPDFObjectRW* CreateDictionary(GDALPDFDictionaryRW* poDict);
183 : static GDALPDFObjectRW* CreateArray(GDALPDFArrayRW* poArray);
184 : virtual ~GDALPDFObjectRW();
185 :
186 : virtual GDALPDFObjectType GetType();
187 : virtual int GetBool();
188 : virtual int GetInt();
189 : virtual double GetReal();
190 756 : virtual int CanRepresentRealAsString() { return m_bCanRepresentRealAsString; }
191 : virtual const CPLString& GetString();
192 : virtual const CPLString& GetName();
193 : virtual GDALPDFDictionary* GetDictionary();
194 : virtual GDALPDFArray* GetArray();
195 : virtual GDALPDFStream* GetStream();
196 : virtual int GetRefNum();
197 : virtual int GetRefGen();
198 : };
199 :
200 : class GDALPDFDictionaryRW : public GDALPDFDictionary
201 : {
202 : private:
203 : std::map<CPLString, GDALPDFObject*> m_map;
204 :
205 : public:
206 : GDALPDFDictionaryRW();
207 : virtual ~GDALPDFDictionaryRW();
208 :
209 : virtual GDALPDFObject* Get(const char* pszKey);
210 : virtual std::map<CPLString, GDALPDFObject*>& GetValues();
211 :
212 : GDALPDFDictionaryRW& Add(const char* pszKey, GDALPDFObject* poVal);
213 : GDALPDFDictionaryRW& Remove(const char* pszKey);
214 :
215 668 : GDALPDFDictionaryRW& Add(const char* pszKey, GDALPDFArrayRW* poArray) { return Add(pszKey, GDALPDFObjectRW::CreateArray(poArray)); }
216 212 : GDALPDFDictionaryRW& Add(const char* pszKey, GDALPDFDictionaryRW* poDict) { return Add(pszKey, GDALPDFObjectRW::CreateDictionary(poDict)); }
217 330 : GDALPDFDictionaryRW& Add(const char* pszKey, const char* pszVal) { return Add(pszKey, GDALPDFObjectRW::CreateString(pszVal)); }
218 818 : GDALPDFDictionaryRW& Add(const char* pszKey, int nVal) { return Add(pszKey, GDALPDFObjectRW::CreateInt(nVal)); }
219 110 : GDALPDFDictionaryRW& Add(const char* pszKey, double dfVal, int bCanRepresentRealAsString = FALSE) { return Add(pszKey, GDALPDFObjectRW::CreateReal(dfVal, bCanRepresentRealAsString)); }
220 1146 : GDALPDFDictionaryRW& Add(const char* pszKey, int nNum, int nGen) { return Add(pszKey, GDALPDFObjectRW::CreateIndirect(nNum, nGen)); }
221 : };
222 :
223 : class GDALPDFArrayRW : public GDALPDFArray
224 : {
225 : private:
226 : std::vector<GDALPDFObject*> m_array;
227 :
228 : public:
229 : GDALPDFArrayRW();
230 : virtual ~GDALPDFArrayRW();
231 :
232 : virtual int GetLength();
233 : virtual GDALPDFObject* Get(int nIndex);
234 :
235 : GDALPDFArrayRW& Add(GDALPDFObject* poObj);
236 :
237 14 : GDALPDFArrayRW& Add(GDALPDFArrayRW* poArray) { return Add(GDALPDFObjectRW::CreateArray(poArray)); }
238 24 : GDALPDFArrayRW& Add(GDALPDFDictionaryRW* poDict) { return Add(GDALPDFObjectRW::CreateDictionary(poDict)); }
239 : GDALPDFArrayRW& Add(const char* pszVal) { return Add(GDALPDFObjectRW::CreateString(pszVal)); }
240 1558 : GDALPDFArrayRW& Add(int nVal) { return Add(GDALPDFObjectRW::CreateInt(nVal)); }
241 1316 : GDALPDFArrayRW& Add(double dfVal, int bCanRepresentRealAsString = FALSE) { return Add(GDALPDFObjectRW::CreateReal(dfVal, bCanRepresentRealAsString)); }
242 : GDALPDFArrayRW& Add(double* padfVal, int nCount, int bCanRepresentRealAsString = FALSE);
243 210 : GDALPDFArrayRW& Add(int nNum, int nGen) { return Add(GDALPDFObjectRW::CreateIndirect(nNum, nGen)); }
244 : };
245 :
246 : #ifdef HAVE_POPPLER
247 :
248 : class GDALPDFObjectPoppler : public GDALPDFObject
249 : {
250 : private:
251 : Object* m_po;
252 : int m_bDestroy;
253 : GDALPDFDictionary* m_poDict;
254 : GDALPDFArray* m_poArray;
255 : GDALPDFStream* m_poStream;
256 : CPLString osStr;
257 : int m_nRefNum;
258 : int m_nRefGen;
259 :
260 : protected:
261 : virtual const char* GetTypeNameNative();
262 :
263 : public:
264 19606 : GDALPDFObjectPoppler(Object* po, int bDestroy) :
265 : m_po(po), m_bDestroy(bDestroy),
266 : m_poDict(NULL), m_poArray(NULL), m_poStream(NULL),
267 19606 : m_nRefNum(0), m_nRefGen(0) {}
268 :
269 : void SetRefNumAndGen(int nNum, int nGen);
270 :
271 : virtual ~GDALPDFObjectPoppler();
272 :
273 : virtual GDALPDFObjectType GetType();
274 : virtual int GetBool();
275 : virtual int GetInt();
276 : virtual double GetReal();
277 : virtual const CPLString& GetString();
278 : virtual const CPLString& GetName();
279 : virtual GDALPDFDictionary* GetDictionary();
280 : virtual GDALPDFArray* GetArray();
281 : virtual GDALPDFStream* GetStream();
282 : virtual int GetRefNum();
283 : virtual int GetRefGen();
284 : };
285 :
286 : GDALPDFArray* GDALPDFCreateArray(Array* array);
287 :
288 : #endif // HAVE_POPPLER
289 :
290 : #ifdef HAVE_PODOFO
291 :
292 : class GDALPDFObjectPodofo : public GDALPDFObject
293 : {
294 : private:
295 : PoDoFo::PdfObject* m_po;
296 : PoDoFo::PdfVecObjects& m_poObjects;
297 : GDALPDFDictionary* m_poDict;
298 : GDALPDFArray* m_poArray;
299 : GDALPDFStream* m_poStream;
300 : CPLString osStr;
301 :
302 : protected:
303 : virtual const char* GetTypeNameNative();
304 :
305 : public:
306 : GDALPDFObjectPodofo(PoDoFo::PdfObject* po, PoDoFo::PdfVecObjects& poObjects);
307 :
308 : virtual ~GDALPDFObjectPodofo();
309 :
310 : virtual GDALPDFObjectType GetType();
311 : virtual int GetBool();
312 : virtual int GetInt();
313 : virtual double GetReal();
314 : virtual const CPLString& GetString();
315 : virtual const CPLString& GetName();
316 : virtual GDALPDFDictionary* GetDictionary();
317 : virtual GDALPDFArray* GetArray();
318 : virtual GDALPDFStream* GetStream();
319 : virtual int GetRefNum();
320 : virtual int GetRefGen();
321 : };
322 :
323 : #endif // HAVE_PODOFO
324 :
325 : #endif // PDFOBJECT_H_INCLUDED
|