File: helloworld-base14.cpp

package info (click to toggle)
libpodofo 0.9.0-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,068 kB
  • sloc: cpp: 53,348; ansic: 339; sh: 96; makefile: 59
file content (380 lines) | stat: -rw-r--r-- 11,277 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/***************************************************************************
 *   Copyright (C) 2006 by Dominik Seichter                                *
 *   domseichter@web.de                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


/*
 * Include the standard headers for cout to write
 * some output to the console.
 */
#include <iostream>

/* Include appropriate system header for standard integer types
*/
#ifdef _WIN32
#include <BaseTsd.h>
#else
#include <stdint.h>
#endif
/*
 * Now include all podofo header files, to have access
 * to all functions of podofo and so that you do not have
 * to care about the order of includes.
 *
 * You should always use podofo.h and not try to include
 * the required headers on your own.
 */
#include <podofo.h>

/*
 * All podofo classes are member of the PoDoFo namespace.
 */
using namespace PoDoFo;



void PrintHelp()
{
    std::cout << "This is a example application for the PoDoFo PDF library." << std::endl
              << "It creates a small PDF file containing the text >Hello World!<" << std::endl
              << "Please see http://podofo.sf.net for more information" << std::endl << std::endl;
    std::cout << "Usage:" << std::endl;
    std::cout << "  examplehelloworld [outputfile.pdf]" << std::endl << std::endl;
}

const char * GetBase14FontName(int i);
void DemoBase14Fonts(PdfPainter& painter, PdfPage* pPage, PdfStreamedDocument& document);

void HelloWorld( const char* pszFilename ) 
{
    /*
     * PdfStreamedDocument is the class that can actually write a PDF file.
     * PdfStreamedDocument is much faster than PdfDocument, but it is only
     * suitable for creating/drawing PDF files and cannot modify existing
     * PDF documents.
     *
     * The document is written directly to pszFilename while being created.
     */
    PdfStreamedDocument document( pszFilename );

	/*
     * PdfPainter is the class which is able to draw text and graphics
     * directly on a PdfPage object.
     */
    PdfPainter painter;

	/*
     * This pointer will hold the page object later. 
     * PdfSimpleWriter can write several PdfPage's to a PDF file.
     */
    PdfPage* pPage;

	/*
     * A PdfFont object is required to draw text on a PdfPage using a PdfPainter.
     * PoDoFo will find the font using fontconfig on your system and embedd truetype
     * fonts automatically in the PDF file.
     */     
    PdfFont* pFont;

	try {
		/*
		 * The PdfDocument object can be used to create new PdfPage objects.
		 * The PdfPage object is owned by the PdfDocument will also be deleted automatically
		 * by the PdfDocument object.
		 *
		 * You have to pass only one argument, i.e. the page size of the page to create.
		 * There are predefined enums for some common page sizes.
		 */
		pPage = document.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) );

		/*
		 * If the page cannot be created because of an error (e.g. ePdfError_OutOfMemory )
		 * a NULL pointer is returned.
		 * We check for a NULL pointer here and throw an exception using the RAISE_ERROR macro.
		 * The raise error macro initializes a PdfError object with a given error code and
		 * the location in the file in which the error ocurred and throws it as an exception.
		 */
		if( !pPage ) 
		{
			PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
		}

		/*
		 * Set the page as drawing target for the PdfPainter.
		 * Before the painter can draw, a page has to be set first.
		 */
		painter.SetPage( pPage );

		/*
		 * Create a PdfFont object using the font "Arial".
		 * The font is found on the system using fontconfig and embedded into the
		 * PDF file. If Arial is not available, a default font will be used.
		 *
		 * The created PdfFont will be deleted by the PdfDocument.
		 */
		pFont = document.CreateFont( "Helvetica" );

		/*
		 * If the PdfFont object cannot be allocated return an error.
		 */
		if( !pFont )
		{
			PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
		}

		/*
		 * Set the font size
		 */
		pFont->SetFontSize( 18.0 );

		/*
		 * Set the font as default font for drawing.
		 * A font has to be set before you can draw text on
		 * a PdfPainter.
		 */
		painter.SetFont( pFont );

		/*
		 * You could set a different color than black to draw
		 * the text.
		 *
		 * SAFE_OP( painter.SetColor( 1.0, 0.0, 0.0 ) );
		 */

		/*
		 * Actually draw the line "Hello World!" on to the PdfPage at
		 * the position 2cm,2cm from the top left corner. 
		 * Please remember that PDF files have their origin at the 
		 * bottom left corner. Therefore we substract the y coordinate 
		 * from the page height.
		 * 
		 * The position specifies the start of the baseline of the text.
		 *
		 * All coordinates in PoDoFo are in PDF units.
		 * You can also use PdfPainterMM which takes coordinates in 1/1000th mm.
		 *
		 */
		painter.DrawText( 56.69, pPage->GetPageSize().GetHeight() - 56.69, "Hello World!" );

		DemoBase14Fonts(painter, pPage, document);

		painter.FinishPage();

		/*
		 * The last step is to close the document.
		 */
		document.Close();

	} catch ( const PdfError & e ) {
		/*
		 * All PoDoFo methods may throw exceptions
		 * make sure that painter.FinishPage() is called
		 * or who will get an assert in its destructor
		 */
		try {
			painter.FinishPage();
		} catch( ... ) {
			/*
			 * Ignore errors this time
			 */
		}

		throw e;
	}
}

int main( int argc, char* argv[] )
{
    /*
     * Check if a filename was passed as commandline argument.
     * If more than 1 argument or no argument is passed,
     * a help message is displayed and the example application
     * will quit.
     */
    if( argc != 2 )
    {
        PrintHelp();
        return -1;
    }

    /*
     * All podofo functions will throw an exception in case of an error.
     *
     * You should catch the exception to either fix it or report
     * back to the user.
     *
     * All exceptions podofo throws are objects of the class PdfError.
     * Thats why we simply catch PdfError objects.
     */
    try {
        /*
         * Call the drawing routing which will create a PDF file
         * with the filename of the output file as argument.
         */
         HelloWorld( argv[1] );
    } catch( const PdfError & eCode ) {
        /*
         * We have to check if an error has occurred.
         * If yes, we return and print an error message
         * to the commandline.
         */
        eCode.PrintErrorMsg();
        return eCode.GetError();
    }

    /*
     * The PDF was created sucessfully.
     */
    std::cout << std::endl
              << "Created a PDF file containing the line \"Hello World!\": " << argv[1] << std::endl << std::endl;
    
    return 0;
}

// Base14 + other non-Base14 fonts for comparison
#define NUM_BASE14_FONTS 17

const char * GetBase14FontName(int i)
{
	const char *base14fonts[NUM_BASE14_FONTS] = {
							"Courier",
							"Courier-Bold",
							"Courier-Oblique",
							"Courier-BoldOblique",
							"Helvetica",
							"Helvetica-Bold",
							"Helvetica-Oblique",
							"Helvetica-BoldOblique",
							"Times-Roman",
							"Times-Bold",
							"Times-Italic",
							"Times-BoldItalic",
							"Symbol",
							"ZapfDingbats",

							"Arial",
							"Times New Roman",
							"Verdana"
							};

	if (i >= 0 && i < NUM_BASE14_FONTS)
		return base14fonts[i]; 
	else
		return NULL;

}

void DrawRedFrame(PdfPainter& painter, double x, double y, double width, double height)
{
        // draw red box
        painter.SetColor(1.0f, 0.0f, 0.0f);
        painter.SetStrokingColor(1.0f, 0.0f, 0.0f);
        painter.DrawLine(x, y, x+width, y);
        if ( height > 0.0f )
        {
            painter.DrawLine(x, y, x, y+height);
            painter.DrawLine(x+width, y, x+width, y+height);
            painter.DrawLine(x, y+height, x+width, y+height);
        }
        // restore to black
        painter.SetColor(0.0f, 0.0f, 0.0f);
        painter.SetStrokingColor(0.0f, 0.0f, 0.0f);
}


void DemoBase14Fonts(PdfPainter& painter, PdfPage* pPage, PdfStreamedDocument& document)
{
	double x = 56,y = pPage->GetPageSize().GetHeight() - 56.69;
	char text[255];
	const char *demo_text = "abcdefgABCDEFG12345!#$%&+-@?        ";
	double height=0.0f, width=0.0f;
    int i;

    // draw sample of all types
	for(i = 0; i < NUM_BASE14_FONTS; ++i)
	{
		x = 56; y = y - 25;
		strcpy(text, demo_text);
		strcat(text, GetBase14FontName(i));

		PdfFont *pFont = document.CreateFont( GetBase14FontName(i) );
		pFont->SetFontSize( 12.0 );
		painter.SetFont( pFont );
		
        width  = pFont->GetFontMetrics()->StringWidth(text);
        height = pFont->GetFontMetrics()->GetLineSpacing();


		std::cout <<  GetBase14FontName(i) << " Width = " << width << " Height = " << height << std::endl;

        // draw red box
        DrawRedFrame( painter, x, y, width, height);

        // draw text
		painter.DrawText( x, y , text);
	}

    // draw some individual characters:
    const char *demo_text2 = " @_1jiPlg .;";
    int nChars = strlen(demo_text2);

    // draw  individuals

	for(i = 0; i < nChars; i++)
	{
		x = 56; y = y - 25;
        if ( i==0 )
        {
		    strcpy(text, "Helvetica / Arial Comparison:");

        } else {
		    text[0] = demo_text2[i];
		    text[1] = '\0';
        }

        PdfFont *pFont = document.CreateFont("Helvetica");
        painter.SetFont( pFont );
        height = pFont->GetFontMetrics()->GetLineSpacing();
        width  = pFont->GetFontMetrics()->StringWidth(text);

        // draw red box
        DrawRedFrame( painter, x, y, width, height);

        // draw text
		painter.DrawText( x, y , text);

        if ( i>0 )
        {
            // draw again, with non-Base14 font
            PdfFont *pFont2 = document.CreateFont("Arial");
            painter.SetFont( pFont2 );
            height = pFont2->GetFontMetrics()->GetLineSpacing();
            width  = pFont2->GetFontMetrics()->StringWidth(text);

            // draw red box
            DrawRedFrame( painter, x+100, y, width, height);

            // draw text
		    painter.DrawText( x+100, y , text);
	    }
	}


}