1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominik Seichter                                *
3  *   domseichter@web.de                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Library General Public License as       *
7  *   published by the Free Software Foundation; either version 2 of the    *
8  *   License, or (at your option) any later version.                       *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU Library General Public     *
16  *   License along with this program; if not, write to the                 *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  *                                                                         *
20  *   In addition, as a special exception, the copyright holders give       *
21  *   permission to link the code of portions of this program with the      *
22  *   OpenSSL library under certain conditions as described in each         *
23  *   individual source file, and distribute linked combinations            *
24  *   including the two.                                                    *
25  *   You must obey the GNU General Public License in all respects          *
26  *   for all of the code used other than OpenSSL.  If you modify           *
27  *   file(s) with this exception, you may extend this exception to your    *
28  *   version of the file(s), but you are not obligated to do so.  If you   *
29  *   do not wish to do so, delete this exception statement from your       *
30  *   version.  If you delete this exception statement from all source      *
31  *   files in the program, then also delete it here.                       *
32  ***************************************************************************/
33 
34 #ifndef _PDF_DEFINES_H_
35 #define _PDF_DEFINES_H_
36 
37 /** \file PdfDefines.h
38  *        This file should be included as the FIRST file in every header of
39  *        PoDoFo lib. It includes all standard files, defines some useful
40  *        macros, some datatypes and all important enumeration types. On
41  *        supporting platforms it will be precompiled to speed compilation.
42  */
43 
44 #include "PdfCompilerCompat.h"
45 
46 /**
47  * PoDoFo version - 24-bit integer representation.
48  * Version is 0xMMmmpp  where M is major, m is minor and p is patch
49  * eg 0.7.0  is represented as 0x000700
50  * eg 0.7.99 is represented as 0x000763
51  *
52  * Note that the PoDoFo version is available in parts as individual 8-bit
53  * integer literals in PODOFO_VERSION_MAJOR, PODOFO_VERSION_MINOR and
54  * PODOFO_VERSION_PATCH .
55  */
56 #define PODOFO_MAKE_VERSION_REAL(M,m,p) ( (M<<16)+(m<<8)+(p) )
57 #define PODOFO_MAKE_VERSION(M,m,p) PODOFO_MAKE_VERSION_REAL(M,m,p)
58 #define PODOFO_VERSION PODOFO_MAKE_VERSION(PODOFO_VERSION_MAJOR, PODOFO_VERSION_MINOR, PODOFO_VERSION_PATCH)
59 
60 /**
61  * PoDoFo version represented as a string literal, eg '0.7.99'
62  */
63 // The \0 is from Win32 example resources and the other values in PoDoFo's one
64 #define PODOFO_MAKE_VERSION_STR_REAL(M,m,p) M ## . ## m ## . ## p
65 #define PODOFO_STR(x) #x "\0"
66 #define PODOFO_XSTR(x) PODOFO_STR(x)
67 #define PODOFO_MAKE_VERSION_STR(M,m,p) PODOFO_XSTR(PODOFO_MAKE_VERSION_STR_REAL(M,m,p))
68 #define PODOFO_VERSION_STR PODOFO_MAKE_VERSION_STR(PODOFO_VERSION_MAJOR, PODOFO_VERSION_MINOR, PODOFO_VERSION_PATCH)
69 
70 #ifndef PODOFO_COMPILE_RC
71 
72 #ifndef PODOFO_UNUSED_PARAM
73 #define PODOFO_UNUSED_PARAM(x)
74 #endif
75 
76 // Include common system files
77 // (most are now pulled in my PdfCompilerCompat.h)
78 #include <wchar.h>
79 
80 // Include common STL files
81 #include <map>
82 #include <string>
83 #include <vector>
84 #include <set>
85 
86 // Include common BOOST settings
87 #ifdef PODOFO_HAVE_BOOST
88 #include <boost/config.hpp>
89 #endif // PODOFO_HAVE_BOOST
90 
91 /** \def PODOFO_VERBOSE_DEBUG
92  *  Debug define. Enable it, if you need
93  *  more debuf output to the commandline from PoDoFo
94  *
95  *  Setting PDF_VERBOSE_DEBUG will make PoDoFo
96  *  EXTREMELY slow and verbose, so it's not practical
97  *  even for regular debuggin.
98  */
99 #ifndef PODOFO_VERBOSE_DEBUG
100 //#define PODOFO_VERBOSE_DEBUG
101 #endif //PODOFO_VERBOSE_DEBUG
102 
103 // Should we do lots of extra (expensive) sanity checking?  You should not
104 // define this on production builds because of the runtime cost and because it
105 // might cause the library to abort() if it notices something nasty.
106 // It may also change the size of some objects, and is thus not binary
107 // compatible.
108 //
109 // If you don't know you need this, avoid it.
110 //
111 #ifndef PODOFO_EXTRA_CHECKS
112 //#define PODOFO_EXTRA_CHECKS
113 #endif //PODOFO_EXTRA_CHECKS
114 
115 // Error Handling Defines
116 #include "PdfError.h"
117 
118 // Memory management
119 #include "PdfMemoryManagement.h"
120 
121 // Include API macro definitions
122 #include "podofoapi.h"
123 
124 #ifdef DEBUG
125 #include <assert.h>
126 #define PODOFO_ASSERT( x ) assert( x );
127 #else
128 #define PODOFO_ASSERT( x ) do { if (!(x)) PODOFO_RAISE_ERROR_INFO(ePdfError_InternalLogic, #x); } while (false)
129 #endif // DEBUG
130 
131 // By default, PoDoFo will use C++ locale support to ensure that
132 // it doesn't write bad PDF data - particularly floating point numbers.
133 // If your standard library does not support locales this won't work, but
134 // your STL probably writes all data in a POSIX-like way irrespective of
135 // locale. If you set this to 0, you MUST use some other method to ensure
136 // that streams used by PoDoFo will write data in a POSIX locale like manner.
137 #ifndef USE_CXX_LOCALE
138 #define USE_CXX_LOCALE 1
139 #endif
140 
141 /**
142  * \namespace PoDoFo
143  *
144  * All classes, functions, types and enums of PoDoFo
145  * are members of these namespace.
146  *
147  * If you use PoDoFo, you might want to add the line:
148  *       using namespace PoDoFo;
149  * to your application.
150  */
151 namespace PoDoFo {
152 
153 /* Explicitly big-endian short, suitable for unicode text */
154 typedef pdf_uint16     pdf_utf16be;
155 /* Typedef to indicate utf-8 encoded data */
156 typedef unsigned char  pdf_utf8;
157 
158 // Enums
159 
160 /**
161  * Enum to identify diferent versions of the PDF file format
162  */
163 enum EPdfVersion {
164     ePdfVersion_1_0 = 0,       /**< PDF 1.0 */
165     ePdfVersion_1_1,           /**< PDF 1.1 */
166     ePdfVersion_1_2,           /**< PDF 1.2 */
167     ePdfVersion_1_3,           /**< PDF 1.3 */
168     ePdfVersion_1_4,           /**< PDF 1.4 */
169     ePdfVersion_1_5,           /**< PDF 1.5 */
170     ePdfVersion_1_6,           /**< PDF 1.6 */
171     ePdfVersion_1_7            /**< PDF 1.7 */
172 };
173 
174 /** The default PDF Version used by new PDF documents
175  *  in PoDoFo.
176  */
177 const EPdfVersion ePdfVersion_Default = ePdfVersion_1_3;
178 
179 /**
180  * Specify additional options for writing the PDF.
181  */
182 enum EPdfWriteMode {
183     ePdfWriteMode_Compact = 0x01, ///< Try to write the PDF as compact as possible (Default)
184     ePdfWriteMode_Clean = 0x02,   ///< Create a PDF that is readable in a text editor, i.e. insert spaces and linebreaks between tokens
185 };
186 
187 const EPdfWriteMode ePdfWriteMode_Default = ePdfWriteMode_Compact;
188 
189 /**
190  * Every PDF datatype that can occur in a PDF file
191  * is referenced by an own enum (e.g. Bool or String).
192  *
193  * \see PdfVariant
194  *
195  * Remember to update PdfVariant::GetDataTypeString() when adding members here.
196  */
197 enum EPdfDataType {
198     ePdfDataType_Bool,                  /**< Boolean datatype: Accepts the values "true" and "false" */
199     ePdfDataType_Number,                /**< Number datatype for integer values */
200     ePdfDataType_Real,                  /**< Real datatype for floating point numbers */
201     ePdfDataType_String,                /**< String datatype in PDF file. Strings have the form (Hallo World!) in PDF files. \see PdfString */
202     ePdfDataType_HexString,             /**< HexString datatype in PDF file. Hex encoded strings have the form &lt;AF00BE&gt; in PDF files. \see PdfString */
203     ePdfDataType_Name,                  /**< Name datatype. Names are used as keys in dictionary to reference values. \see PdfName */
204     ePdfDataType_Array,                 /**< An array of other PDF data types. */
205     ePdfDataType_Dictionary,            /**< A dictionary associates keys with values. A key can have another dictionary as value. */
206     //ePdfDataType_Stream,                /**< A stream can be attached to a dictionary and contain additional data. \see PdfStream */
207     ePdfDataType_Null,                  /**< The null datatype is always null. */
208     ePdfDataType_Reference,             /**< The reference datatype contains references to PDF objects in the PDF file of the form 4 0 R. \see PdfObject */
209     ePdfDataType_RawData,               /**< Raw PDF data */
210 
211     ePdfDataType_Unknown = 0xff         /**< The Datatype is unknown. The value is chosen to enable value storage in 8-bit unsigned integer. */
212 };
213 
214 /**
215  * Every filter that can be used to encode a stream
216  * in a PDF file is referenced by an own enum value.
217  * Common filters are ePdfFilter_FlateDecode (i.e. Zip) or
218  * ePdfFilter_ASCIIHexDecode
219  */
220 enum EPdfFilter {
221     ePdfFilter_None = -1,                 /**< Do not use any filtering */
222     ePdfFilter_ASCIIHexDecode,            /**< Converts data from and to hexadecimal. Increases size of the data by a factor of 2! \see PdfHexFilter */
223     ePdfFilter_ASCII85Decode,             /**< Converts to and from Ascii85 encoding. \see PdfAscii85Filter */
224     ePdfFilter_LZWDecode,
225     ePdfFilter_FlateDecode,               /**< Compress data using the Flate algorithm of ZLib. This filter is recommended to be used always. \see PdfFlateFilter */
226     ePdfFilter_RunLengthDecode,           /**< Run length decode data. \see PdfRLEFilter */
227     ePdfFilter_CCITTFaxDecode,
228     ePdfFilter_JBIG2Decode,
229     ePdfFilter_DCTDecode,
230     ePdfFilter_JPXDecode,
231     ePdfFilter_Crypt
232 };
233 
234 
235 /**
236  * Enum for the different font formats supported by PoDoFo
237  */
238 enum EPdfFontType {
239     ePdfFontType_TrueType,
240     ePdfFontType_Type1Pfa,
241     ePdfFontType_Type1Pfb,
242     ePdfFontType_Type1Base14,
243     ePdfFontType_Type3,
244     ePdfFontType_Unknown = 0xff
245 };
246 
247 /**
248  * Enum for the colorspaces supported
249  * by PDF.
250  */
251 enum EPdfColorSpace {
252     ePdfColorSpace_DeviceGray,        /**< Gray */
253     ePdfColorSpace_DeviceRGB,         /**< RGB  */
254     ePdfColorSpace_DeviceCMYK,        /**< CMYK */
255     ePdfColorSpace_Separation,        /**< Separation */
256     ePdfColorSpace_CieLab,            /**< CIE-Lab */
257     ePdfColorSpace_Indexed,           /**< Indexed */
258     ePdfColorSpace_Unknown = 0xff
259 };
260 
261 /**
262  * Enum for text rendering mode (Tr)
263  */
264 enum EPdfTextRenderingMode {
265     ePdfTextRenderingMode_Fill = 0,                 /**< Default mode, fill text */
266     ePdfTextRenderingMode_Stroke,                   /**< Stroke text */
267     ePdfTextRenderingMode_FillAndStroke,            /**< Fill, then stroke text */
268     ePdfTextRenderingMode_Invisible,                /**< Neither fill nor stroke text (invisible) */
269     ePdfTextRenderingMode_FillToClipPath,           /**< Fill text and add to path for clipping */
270     ePdfTextRenderingMode_StrokeToClipPath,         /**< Stroke text and add to path for clipping */
271     ePdfTextRenderingMode_FillAndStrokeToClipPath,  /**< Fill, then stroke text and add to path for clipping */
272     ePdfTextRenderingMode_ToClipPath,               /**< Add text to path for clipping */
273     ePdfTextRenderingMode_Unknown = 0xff
274 };
275 
276 /**
277  * Enum for the different stroke styles that can be set
278  * when drawing to a PDF file (mostly for line drawing).
279  */
280 enum EPdfStrokeStyle {
281     ePdfStrokeStyle_Solid,
282     ePdfStrokeStyle_Dash,
283     ePdfStrokeStyle_Dot,
284     ePdfStrokeStyle_DashDot,
285     ePdfStrokeStyle_DashDotDot,
286     ePdfStrokeStyle_Custom
287 };
288 
289 /**
290  * Enum for predefined tiling patterns.
291  */
292 enum EPdfTilingPatternType {
293     ePdfTilingPatternType_BDiagonal = 1,
294     ePdfTilingPatternType_Cross,
295     ePdfTilingPatternType_DiagCross,
296     ePdfTilingPatternType_FDiagonal,
297     ePdfTilingPatternType_Horizontal,
298     ePdfTilingPatternType_Vertical,
299     ePdfTilingPatternType_Image
300 };
301 
302 /**
303  * Enum for line cap styles when drawing.
304  */
305 enum EPdfLineCapStyle {
306     ePdfLineCapStyle_Butt    = 0,
307     ePdfLineCapStyle_Round   = 1,
308     ePdfLineCapStyle_Square  = 2
309 };
310 
311 /**
312  * Enum for line join styles when drawing.
313  */
314 enum EPdfLineJoinStyle {
315     ePdfLineJoinStyle_Miter   = 0,
316     ePdfLineJoinStyle_Round   = 1,
317     ePdfLineJoinStyle_Bevel   = 2
318 };
319 
320 /**
321  * Enum for vertical text alignment
322  */
323 enum EPdfVerticalAlignment {
324     ePdfVerticalAlignment_Top    = 0,
325     ePdfVerticalAlignment_Center = 1,
326     ePdfVerticalAlignment_Bottom  = 2
327 };
328 
329 /**
330  * Enum for text alignment
331  */
332 enum EPdfAlignment {
333     ePdfAlignment_Left    = 0,
334     ePdfAlignment_Center  = 1,
335     ePdfAlignment_Right   = 2
336 };
337 
338 
339 /**
340  * List of defined Rendering intents
341  */
342 #define ePdfRenderingIntent_AbsoluteColorimetric	"AbsoluteColorimetric"
343 #define ePdfRenderingIntent_RelativeColorimetric	"RelativeColorimetric"
344 #define ePdfRenderingIntent_Perceptual			"Perceptual"
345 #define ePdfRenderingIntent_Saturation			"Saturation"
346 
347 /**
348  * List of defined transparency blending modes
349  */
350 #define ePdfBlendMode_Normal		"Normal"
351 #define ePdfBlendMode_Multiply		"Multiply"
352 #define ePdfBlendMode_Screen		"Screen"
353 #define ePdfBlendMode_Overlay		"Overlay"
354 #define ePdfBlendMode_Darken		"Darken"
355 #define ePdfBlendMode_Lighten		"Lighten"
356 #define ePdfBlendMode_ColorDodge	"ColorDodge"
357 #define ePdfBlendMode_ColorBurn		"ColorBurn"
358 #define ePdfBlendMode_HardLight		"HardLight"
359 #define ePdfBlendMode_SoftLight		"SoftLight"
360 #define ePdfBlendMode_Difference	"Difference"
361 #define ePdfBlendMode_Exclusion		"Exclusion"
362 #define ePdfBlendMode_Hue		"Hue"
363 #define ePdfBlendMode_Saturation	"Saturation"
364 #define ePdfBlendMode_Color		"Color"
365 #define ePdfBlendMode_Luminosity	"Luminosity"
366 
367 /**
368  * Enum holding the supported page sizes by PoDoFo.
369  * Can be used to construct a PdfRect structure with
370  * measurements of a page object.
371  *
372  * \see PdfPage
373  */
374 enum EPdfPageSize {
375     ePdfPageSize_A0,              /**< DIN A0  */
376     ePdfPageSize_A1,              /**< DIN A1  */
377     ePdfPageSize_A2,              /**< DIN A2  */
378     ePdfPageSize_A3,              /**< DIN A3  */
379     ePdfPageSize_A4,              /**< DIN A4  */
380     ePdfPageSize_A5,              /**< DIN A5  */
381     ePdfPageSize_A6,              /**< DIN A6  */
382     ePdfPageSize_Letter,          /**< Letter  */
383     ePdfPageSize_Legal,           /**< Legal   */
384     ePdfPageSize_Tabloid          /**< Tabloid */
385 };
386 
387 /**
388  * Enum holding the supported of types of "PageModes"
389  * that define which (if any) of the "panels" are opened
390  * in Acrobat when the document is opened.
391  *
392  * \see PdfDocument
393  */
394 enum EPdfPageMode {
395     ePdfPageModeDontCare,
396     ePdfPageModeUseNone,
397     ePdfPageModeUseThumbs,
398     ePdfPageModeUseBookmarks,
399     ePdfPageModeFullScreen,
400     ePdfPageModeUseOC,
401     ePdfPageModeUseAttachments
402 };
403 
404 /**
405  * Enum holding the supported of types of "PageLayouts"
406  * that define how Acrobat will display the pages in
407  * relation to each other
408  *
409  * \see PdfDocument
410  */
411 enum EPdfPageLayout {
412     ePdfPageLayoutIgnore,
413     ePdfPageLayoutDefault,
414     ePdfPageLayoutSinglePage,
415     ePdfPageLayoutOneColumn,
416     ePdfPageLayoutTwoColumnLeft,
417     ePdfPageLayoutTwoColumnRight,
418     ePdfPageLayoutTwoPageLeft,
419     ePdfPageLayoutTwoPageRight
420 };
421 
422 /**
423  */
424 const bool ePdfCreateObject = true;
425 const bool ePdfDontCreateObject = false;
426 
427 // character constants
428 #define MAX_PDF_VERSION_STRING_INDEX  7
429 
430 // We use fixed bounds two dimensional arrays here so that
431 // they go into the const data section of the library.
432 static const char s_szPdfVersions[][9] = {
433     "%PDF-1.0",
434     "%PDF-1.1",
435     "%PDF-1.2",
436     "%PDF-1.3",
437     "%PDF-1.4",
438     "%PDF-1.5",
439     "%PDF-1.6",
440     "%PDF-1.7"
441 };
442 
443 static const char s_szPdfVersionNums[][4] = {
444     "1.0",
445     "1.1",
446     "1.2",
447     "1.3",
448     "1.4",
449     "1.5",
450     "1.6",
451     "1.7"
452 };
453 
454 /// PDF Reference, Section 3.1.1, Table 3.1, White-space characters
455 const int s_nNumWhiteSpaces = 6;
456 const char s_cWhiteSpaces[] = {
457     0x00, // NULL
458     0x09, // TAB
459     0x0A, // Line Feed
460     0x0C, // Form Feed
461     0x0D, // Carriage Return
462     0x20, // White Space
463     0x00  // end marker
464 };
465 
466 /// PDF Reference, Section 3.1.1, Character Set
467 static const int s_nNumDelimiters = 10;
468 static const char s_cDelimiters[] = {
469     '(',
470     ')',
471     '<',
472     '>',
473     '[',
474     ']',
475     '{',
476     '}',
477     '/',
478     '%',
479     '\0' // end marker
480 };
481 
482 /**
483  * PDF_MAX(x,y)
484  *
485  * \returns the maximum of x and y
486  */
487 // Not actually a macro, because function-like macros are evil and
488 // prone to nasty issues with double-evaluation of arguments.
PDF_MAX(const T a,const T b)489 template <typename T> const T PDF_MAX ( const T a, const T b ) {
490   return (b<a)?a:b;
491 }
492 
493 /**
494  * PDF_MIN(x,y)
495  * \returns the minimum of x and y
496  */
497 // Not actually a macro, because function-like macros are evil and
498 // prone to nasty issues with double-evaluation of arguments.
PDF_MIN(const T a,const T b)499 template <typename T> const T PDF_MIN ( const T a, const T b ) {
500   return (a<b)?a:b;
501 }
502 
503 #ifndef PODOFO_CONVERSION_CONSTANT
504 #define PODOFO_CONVERSION_CONSTANT 0.002834645669291339
505 #endif // PODOFO_CONVERSION_CONSTANT
506 
507 }; // end namespace PoDoFo
508 
509 /**
510  * \mainpage
511  *
512  * <b>PoDoFo</b> is a library to work with the PDF file format and includes also a few
513  * tools. The name comes from the first letter of PDF (Portable Document
514  * Format).
515  *
516  * The <b>PoDoFo</b> library is a free portable C++ library which includes
517  * classes to parse a PDF file and modify its contents into memory. The changes
518  * can be written back to disk easily. The parser could also be used to write a
519  * PDF viewer. Besides parsing PoDoFo includes also very simple classes to create
520  * your own PDF files. All classes are documented so it is easy to start writing
521  * your own application using PoDoFo.
522  *
523  * The <b>PoDoFo</b> tools are simple tools build around the <b>PoDoFo</b> library. These tools
524  * are first of all examples on how to use the <b>PoDoFo</b> library in your own
525  * projects. But secondly they offer also features for working with PDF
526  * files. More tools will come with future release and the existing tools will
527  * gain more features. Currently there are two tools: podofoimgextract (which
528  * extracts all jpeg images from a given PDF file) and podofouncompress (which
529  * removes all compression filters from a PDF file - this is useful for debugging
530  * existing PDF files).
531  *
532  * Additionally there is the external tool <b>PoDoFoBrowser</b> which is not included in
533  * this package, but can be downloaded from the <b>PoDoFo</b> webpage. <b>PoDoFoBrowser</b> is
534  * a Qt application for browsing the objects in a PDF file and modifying their
535  * keys easily. It is very useful if you want to look on the internal structure
536  * of PDF files.
537  *
538  * As of now <b>PoDoFo</b> is available for Unix, Mac OS X and Windows platforms.
539  *
540  * More information can be found at: http://podofo.sourceforge.net
541  *
542  * <b>PoDoFo</b> is created by Dominik Seichter <domseichter@web.de>,
543  * Leonard Rosenthol <leonardr@pdfsages.com> and Craig Ringer <craig@postnewspapers.com.au>
544  *
545  * \page Codingstyle (Codingstyle)
546  * \verbinclude CODINGSTYLE.txt
547  *
548  */
549 
550 #endif // !PODOFO_COMPILE_RC
551 
552 #endif // _PDF_DEFINES_H_
553