1 : /******************************************************************************
2 : * $Id: pdfobject.h 25536 2013-01-20 14:04:39Z 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 23616 : {
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 : GDALPDFObject* LookupObject(const char* pszPath);
116 :
117 : void Serialize(CPLString& osStr);
118 520 : CPLString Serialize() { CPLString osStr; Serialize(osStr); return osStr; }
119 : GDALPDFObjectRW* Clone();
120 : };
121 :
122 : class GDALPDFDictionary
123 5042 : {
124 : public:
125 : virtual ~GDALPDFDictionary();
126 :
127 : virtual GDALPDFObject* Get(const char* pszKey) = 0;
128 : virtual std::map<CPLString, GDALPDFObject*>& GetValues() = 0;
129 :
130 : GDALPDFObject* LookupObject(const char* pszPath);
131 :
132 : void Serialize(CPLString& osStr);
133 791 : CPLString Serialize() { CPLString osStr; Serialize(osStr); return osStr; }
134 : GDALPDFDictionaryRW* Clone();
135 : };
136 :
137 : class GDALPDFArray
138 1778 : {
139 : public:
140 : virtual ~GDALPDFArray();
141 :
142 : virtual int GetLength() = 0;
143 : virtual GDALPDFObject* Get(int nIndex) = 0;
144 :
145 : void Serialize(CPLString& osStr);
146 54 : CPLString Serialize() { CPLString osStr; Serialize(osStr); return osStr; }
147 : GDALPDFArrayRW* Clone();
148 : };
149 :
150 : class GDALPDFStream
151 401 : {
152 : public:
153 : virtual ~GDALPDFStream();
154 :
155 : virtual int GetLength() = 0;
156 : virtual char* GetBytes() = 0;
157 : };
158 :
159 : class GDALPDFObjectRW : public GDALPDFObject
160 : {
161 : private:
162 : GDALPDFObjectType m_eType;
163 : int m_nVal;
164 : double m_dfVal;
165 : CPLString m_osVal;
166 : GDALPDFDictionaryRW *m_poDict;
167 : GDALPDFArrayRW *m_poArray;
168 : int m_nNum;
169 : int m_nGen;
170 : int m_bCanRepresentRealAsString;
171 :
172 : GDALPDFObjectRW(GDALPDFObjectType eType);
173 :
174 : protected:
175 : virtual const char* GetTypeNameNative();
176 :
177 : public:
178 :
179 : static GDALPDFObjectRW* CreateIndirect(int nNum, int nGen);
180 : static GDALPDFObjectRW* CreateNull();
181 : static GDALPDFObjectRW* CreateBool(int bVal);
182 : static GDALPDFObjectRW* CreateInt(int nVal);
183 : static GDALPDFObjectRW* CreateReal(double dfVal, int bCanRepresentRealAsString = FALSE);
184 : static GDALPDFObjectRW* CreateString(const char* pszStr);
185 : static GDALPDFObjectRW* CreateName(const char* pszName);
186 : static GDALPDFObjectRW* CreateDictionary(GDALPDFDictionaryRW* poDict);
187 : static GDALPDFObjectRW* CreateArray(GDALPDFArrayRW* poArray);
188 : virtual ~GDALPDFObjectRW();
189 :
190 : virtual GDALPDFObjectType GetType();
191 : virtual int GetBool();
192 : virtual int GetInt();
193 : virtual double GetReal();
194 438 : virtual int CanRepresentRealAsString() { return m_bCanRepresentRealAsString; }
195 : virtual const CPLString& GetString();
196 : virtual const CPLString& GetName();
197 : virtual GDALPDFDictionary* GetDictionary();
198 : virtual GDALPDFArray* GetArray();
199 : virtual GDALPDFStream* GetStream();
200 : virtual int GetRefNum();
201 : virtual int GetRefGen();
202 : };
203 :
204 : class GDALPDFDictionaryRW : public GDALPDFDictionary
205 : {
206 : private:
207 : std::map<CPLString, GDALPDFObject*> m_map;
208 :
209 : public:
210 : GDALPDFDictionaryRW();
211 : virtual ~GDALPDFDictionaryRW();
212 :
213 : virtual GDALPDFObject* Get(const char* pszKey);
214 : virtual std::map<CPLString, GDALPDFObject*>& GetValues();
215 :
216 : GDALPDFDictionaryRW& Add(const char* pszKey, GDALPDFObject* poVal);
217 : GDALPDFDictionaryRW& Remove(const char* pszKey);
218 :
219 440 : GDALPDFDictionaryRW& Add(const char* pszKey, GDALPDFArrayRW* poArray) { return Add(pszKey, GDALPDFObjectRW::CreateArray(poArray)); }
220 254 : GDALPDFDictionaryRW& Add(const char* pszKey, GDALPDFDictionaryRW* poDict) { return Add(pszKey, GDALPDFObjectRW::CreateDictionary(poDict)); }
221 214 : GDALPDFDictionaryRW& Add(const char* pszKey, const char* pszVal) { return Add(pszKey, GDALPDFObjectRW::CreateString(pszVal)); }
222 817 : GDALPDFDictionaryRW& Add(const char* pszKey, int nVal) { return Add(pszKey, GDALPDFObjectRW::CreateInt(nVal)); }
223 93 : GDALPDFDictionaryRW& Add(const char* pszKey, double dfVal, int bCanRepresentRealAsString = FALSE) { return Add(pszKey, GDALPDFObjectRW::CreateReal(dfVal, bCanRepresentRealAsString)); }
224 1043 : GDALPDFDictionaryRW& Add(const char* pszKey, int nNum, int nGen) { return Add(pszKey, GDALPDFObjectRW::CreateIndirect(nNum, nGen)); }
225 : };
226 :
227 : class GDALPDFArrayRW : public GDALPDFArray
228 : {
229 : private:
230 : std::vector<GDALPDFObject*> m_array;
231 :
232 : public:
233 : GDALPDFArrayRW();
234 : virtual ~GDALPDFArrayRW();
235 :
236 : virtual int GetLength();
237 : virtual GDALPDFObject* Get(int nIndex);
238 :
239 : GDALPDFArrayRW& Add(GDALPDFObject* poObj);
240 :
241 10 : GDALPDFArrayRW& Add(GDALPDFArrayRW* poArray) { return Add(GDALPDFObjectRW::CreateArray(poArray)); }
242 21 : GDALPDFArrayRW& Add(GDALPDFDictionaryRW* poDict) { return Add(GDALPDFObjectRW::CreateDictionary(poDict)); }
243 1 : GDALPDFArrayRW& Add(const char* pszVal) { return Add(GDALPDFObjectRW::CreateString(pszVal)); }
244 1032 : GDALPDFArrayRW& Add(int nVal) { return Add(GDALPDFObjectRW::CreateInt(nVal)); }
245 748 : GDALPDFArrayRW& Add(double dfVal, int bCanRepresentRealAsString = FALSE) { return Add(GDALPDFObjectRW::CreateReal(dfVal, bCanRepresentRealAsString)); }
246 : GDALPDFArrayRW& Add(double* padfVal, int nCount, int bCanRepresentRealAsString = FALSE);
247 151 : GDALPDFArrayRW& Add(int nNum, int nGen) { return Add(GDALPDFObjectRW::CreateIndirect(nNum, nGen)); }
248 : };
249 :
250 : #ifdef HAVE_POPPLER
251 :
252 : class GDALPDFObjectPoppler : public GDALPDFObject
253 : {
254 : private:
255 : Object* m_po;
256 : int m_bDestroy;
257 : GDALPDFDictionary* m_poDict;
258 : GDALPDFArray* m_poArray;
259 : GDALPDFStream* m_poStream;
260 : CPLString osStr;
261 : int m_nRefNum;
262 : int m_nRefGen;
263 :
264 : protected:
265 : virtual const char* GetTypeNameNative();
266 :
267 : public:
268 12842 : GDALPDFObjectPoppler(Object* po, int bDestroy) :
269 : m_po(po), m_bDestroy(bDestroy),
270 : m_poDict(NULL), m_poArray(NULL), m_poStream(NULL),
271 12842 : m_nRefNum(0), m_nRefGen(0) {}
272 :
273 : void SetRefNumAndGen(int nNum, int nGen);
274 :
275 : virtual ~GDALPDFObjectPoppler();
276 :
277 : virtual GDALPDFObjectType GetType();
278 : virtual int GetBool();
279 : virtual int GetInt();
280 : virtual double GetReal();
281 : virtual const CPLString& GetString();
282 : virtual const CPLString& GetName();
283 : virtual GDALPDFDictionary* GetDictionary();
284 : virtual GDALPDFArray* GetArray();
285 : virtual GDALPDFStream* GetStream();
286 : virtual int GetRefNum();
287 : virtual int GetRefGen();
288 : };
289 :
290 : GDALPDFArray* GDALPDFCreateArray(Array* array);
291 :
292 : #endif // HAVE_POPPLER
293 :
294 : #ifdef HAVE_PODOFO
295 :
296 : class GDALPDFObjectPodofo : public GDALPDFObject
297 : {
298 : private:
299 : PoDoFo::PdfObject* m_po;
300 : PoDoFo::PdfVecObjects& m_poObjects;
301 : GDALPDFDictionary* m_poDict;
302 : GDALPDFArray* m_poArray;
303 : GDALPDFStream* m_poStream;
304 : CPLString osStr;
305 :
306 : protected:
307 : virtual const char* GetTypeNameNative();
308 :
309 : public:
310 : GDALPDFObjectPodofo(PoDoFo::PdfObject* po, PoDoFo::PdfVecObjects& poObjects);
311 :
312 : virtual ~GDALPDFObjectPodofo();
313 :
314 : virtual GDALPDFObjectType GetType();
315 : virtual int GetBool();
316 : virtual int GetInt();
317 : virtual double GetReal();
318 : virtual const CPLString& GetString();
319 : virtual const CPLString& GetName();
320 : virtual GDALPDFDictionary* GetDictionary();
321 : virtual GDALPDFArray* GetArray();
322 : virtual GDALPDFStream* GetStream();
323 : virtual int GetRefNum();
324 : virtual int GetRefGen();
325 : };
326 :
327 : #endif // HAVE_PODOFO
328 :
329 : #endif // PDFOBJECT_H_INCLUDED
|