Debian Codegraph @

podofo/examples/helloworld-base14/helloworld-base14.cpp

raw symbols SCIP
1 /**
2 * Copyright (C) 2006 by Dominik Seichter <[email protected]>
3 *
4 * Licensed under GNU General Public License 2.0 or later.
5 * Some rights reserved. See COPYING, AUTHORS.
6 */
7
8 // Include the standard headers for cout to write
9 // some output to the console.
10 #include <iostream>
11
12 // Now include all PoDoFo header files, to have access
13 // to all functions of PoDoFo and so that you do not have
14 // to care about the order of includes.
15 //
16 // You should always use podofo.h and not try to include
17 // the required headers on your own.
18 #include <podofo/podofo.h>
19
20 // All PoDoFo classes are member of the PoDoFo namespace.
21 using namespace std;
22 using namespace PoDoFo;
23
24 void PrintHelp()
25 {
26 std::cout << "This is a example application for the PoDoFo PDF library." << std::endl
27 << "It creates a small PDF file containing the text >Hello World!<" << std::endl
28 << "Please see https://github.com/podofo/podofo for more information" << std::endl << std::endl;
29 std::cout << "Usage:" << std::endl;
30 std::cout << " helloworld-base14 [outputfile.pdf]" << std::endl << std::endl;
31 }
32
33 const char* GetBase14FontName(unsigned i);
34 void DemoBase14Fonts(PdfPainter& painter, PdfPage& page, PdfDocument& document);
35
36 void HelloWorld(const string_view& filename)
37 {
38 PdfMemDocument document;
39
40 // PdfPainter is the class which is able to draw text and graphics
41 // directly on a PdfPage object.
42 PdfPainter painter;
43
44 // A PdfFont object is required to draw text on a PdfPage using a PdfPainter.
45 // PoDoFo will find the font using fontconfig on your system and embedd truetype
46 // fonts automatically in the PDF file.
47 PdfFont* font;
48
49 try
50 {
51 // The PdfDocument object can be used to create new PdfPage objects.
52 // The PdfPage object is owned by the PdfDocument will also be deleted automatically
53 // by the PdfDocument object.
54 //
55 // You have to pass only one argument, i.e. the page size of the page to create.
56 // There are predefined enums for some common page sizes.
57 auto& page = document.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
58
59 // If the page cannot be created because of an error (e.g. ePdfError_OutOfMemory )
60 // a nullptr pointer is returned.
61 // We check for a nullptr pointer here and throw an exception using the RAISE_ERROR macro.
62 // The raise error macro initializes a PdfError object with a given error code and
63 // the location in the file in which the error ocurred and throws it as an exception.
64
65 // Set the page as drawing target for the PdfPainter.
66 // Before the painter can draw, a page has to be set first.
67 painter.SetCanvas(page);
68
69 // Create a PdfFont object using the font "Arial".
70 // The font is found on the system using fontconfig and embedded into the
71 // PDF file. If Arial is not available, a default font will be used.
72 //
73 // The created PdfFont will be deleted by the PdfDocument.
74 font = document.GetFonts().SearchFont("Arial");
75
76 // If the PdfFont object cannot be allocated return an error.
77 if (font == nullptr)
78 throw runtime_error("Invalid handle");
79
80 auto& metrics = font->GetMetrics();
81 cout << "The font name is " << metrics.GetFontName() << endl;
82 cout << "The family font name is " << metrics.GetFontFamilyName() << endl;
83 cout << "The font file path is " << metrics.GetFilePath() << endl;
84 cout << "The font face index is " << metrics.GetFaceIndex() << endl;
85
86 // Set the font as default font for drawing.
87 // A font has to be set before you can draw text on
88 // a PdfPainter.
89 painter.TextState.SetFont(*font, 18.0);
90
91 // You could set a different color than black to draw
92 // the text.
93 //
94 // painter.SetColor(1.0, 0.0, 0.0);
95
96 // Actually draw the line "Hello World!" on to the PdfPage at
97 // the position 2cm,2cm from the top left corner.
98 // Please remember that PDF files have their origin at the
99 // bottom left corner. Therefore we substract the y coordinate
100 // from the page height.
101 //
102 // The position specifies the start of the baseline of the text.
103 //
104 // All coordinates in PoDoFo are in PDF units.
105 painter.DrawText("Hello World!", 56.69, page.GetRect().Height - 56.69);
106
107
108 DemoBase14Fonts(painter, page, document);
109
110 painter.FinishDrawing();
111
112 // The last step is to close the document.
113 document.Save(filename);
114
115 }
116 catch (PdfError& e)
117 {
118 // All PoDoFo methods may throw exceptions
119 // make sure that painter.FinishPage() is called
120 // or who will get an assert in its destructor
121 try
122 {
123 painter.FinishDrawing();
124 }
125 catch (...)
126 {
127 // Ignore errors this time
128 }
129
130 throw e;
131 }
132 }
133
134 int main(int argc, char* argv[])
135 {
136 // Check if a filename was passed as commandline argument.
137 // If more than 1 argument or no argument is passed,
138 // a help message is displayed and the example application
139 // will quit.
140 if (argc != 2)
141 {
142 PrintHelp();
143 return -1;
144 }
145
146 // All PoDoFo functions will throw an exception in case of an error.
147 //
148 // You should catch the exception to either fix it or report
149 // back to the user.
150 //
151 // All exceptions PoDoFo throws are objects of the class PdfError.
152 // Thats why we simply catch PdfError objects.
153 try
154 {
155 // Call the drawing routing which will create a PDF file
156 // with the filename of the output file as argument.
157 HelloWorld(argv[1]);
158 }
159 catch (PdfError& err)
160 {
161 // We have to check if an error has occurred.
162 // If yes, we return and print an error message
163 // to the commandline.
164 err.PrintErrorMsg();
165 return (int)err.GetCode();
166 }
167
168 // The PDF was created sucessfully.
169 std::cout << std::endl
170 << "Created a PDF file containing the line \"Hello World!\": " << argv[1] << std::endl << std::endl;
171
172 return 0;
173 }
174
175 // Base14 + other non-Base14 fonts for comparison
176
177 static const char* s_base14fonts[] =
178 {
179 "Times-Roman",
180 "Times-Italic",
181 "Times-Bold",
182 "Times-BoldItalic",
183 "Helvetica",
184 "Helvetica-Oblique",
185 "Helvetica-Bold",
186 "Helvetica-BoldOblique",
187 "Courier",
188 "Courier-Oblique",
189 "Courier-Bold",
190 "Courier-BoldOblique",
191 "Symbol",
192 "ZapfDingbats",
193 "Arial",
194 "Verdana"
195 };
196
197 const char* GetBase14FontName(unsigned i)
198 {
199 if (i >= std::size(s_base14fonts))
200 return nullptr;
201
202 return s_base14fonts[i];
203 }
204
205 void DrawRedFrame(PdfPainter& painter, double x, double y, double width, double height)
206 {
207 // draw red box
208 painter.GraphicsState.SetFillColor(PdfColor(1.0f, 0.0f, 0.0f));
209 painter.GraphicsState.SetStrokeColor(PdfColor(1.0f, 0.0f, 0.0f));
210 painter.DrawLine(x, y, x + width, y);
211 if (height > 0.0f)
212 {
213 painter.DrawLine(x, y, x, y + height);
214 painter.DrawLine(x + width, y, x + width, y + height);
215 painter.DrawLine(x, y + height, x + width, y + height);
216 }
217 // restore to black
218 painter.GraphicsState.SetFillColor(PdfColor(0.0f, 0.0f, 0.0f));
219 painter.GraphicsState.SetStrokeColor(PdfColor(0.0f, 0.0f, 0.0f));
220 }
221
222 void DemoBase14Fonts(PdfPainter& painter, PdfPage& page, PdfDocument& document)
223 {
224 PdfFontSearchParams params;
225 params.AutoSelect = PdfFontAutoSelectBehavior::Standard14;
226
227 double x = 56, y = page.GetRect().Height - 56.69;
228 string_view demo_text = "abcdefgABCDEFG12345!#$%&+-@? ";
229 double height = 0.0f, width = 0.0f;
230
231 // draw sample of all types
232 for (unsigned i = 0; i < std::size(s_base14fonts); i++)
233 {
234 x = 56; y = y - 25;
235 string text;
236 if (i == 12)
237 {
238 // Or u8"♠♣♥♦": Symbol font doesn't support regular characters
239 text = u8"\u2660\u2663\u2665\u2666";
240 }
241 else if (i == 13)
242 {
243 // Or u8"❏❑▲▼": ZapfDingbats font doesn't support regular characters
244 text = u8"\u274f\u2751\u25b2\u25bc";
245 }
246 else
247 {
248 text = demo_text;
249 text.append(GetBase14FontName(i));
250 }
251
252 PdfFont* font = document.GetFonts().SearchFont(GetBase14FontName(i), params);
253 if (font == nullptr)
254 throw runtime_error("Font not found");
255
256 painter.TextState.SetFont(*font, 12.0);
257
258 width = font->GetStringLength(text, painter.TextState);
259 height = font->GetLineSpacing(painter.TextState);
260
261 std::cout << GetBase14FontName(i) << " Width = " << width << " Height = " << height << std::endl;
262
263 // draw red box
264 DrawRedFrame(painter, x, y, width, height);
265
266 // draw text
267 painter.DrawText(text, x, y);
268 }
269
270 // draw some individual characters:
271 string_view demo_text2 = " @_1jiPlg .;";
272
273 auto helveticaStd14 = document.GetFonts().SearchFont("Helvetica", params);
274 auto arialImported = document.GetFonts().SearchFont("Arial");
275 auto& metrics = arialImported->GetMetrics();
276 cout << "Non base 14 font characteristics" << endl;
277 cout << "Font name: " << metrics.GetFontName() << endl;
278 cout << "Family font name: " << metrics.GetFontFamilyName() << endl;
279 cout << "Font file path: " << metrics.GetFilePath() << endl;
280 cout << "Font face index: " << metrics.GetFaceIndex() << endl;
281
282 // draw individuals
283 for (unsigned i = 0; i < demo_text2.length(); i++)
284 {
285 x = 56; y = y - 25;
286 string text;
287 if (i == 0)
288 text = "Helvetica / Arial Comparison:";
289 else
290 text = (string)demo_text2.substr(i, 1);
291
292
293 painter.TextState.SetFont(*helveticaStd14, 12);
294 height = helveticaStd14->GetLineSpacing(painter.TextState);
295 width = helveticaStd14->GetStringLength(text, painter.TextState);
296
297 // draw red box
298 DrawRedFrame(painter, x, y, width, height);
299
300 // draw text
301 painter.DrawText(text, x, y);
302
303 if (i > 0)
304 {
305 // draw again, with non-Base14 font
306 painter.TextState.SetFont(*arialImported, 12);
307 height = arialImported->GetLineSpacing(painter.TextState);
308 width = arialImported->GetStringLength((string_view)text, painter.TextState);
309
310 // draw red box
311 DrawRedFrame(painter, x + 100, y, width, height);
312
313 // draw text
314 painter.DrawText(text, x + 100, y);
315 }
316 }
317 }