OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / demo / font_demo.cpp
1 /*
2  * << Haru Free PDF Library 2.0.0 >> -- font_demo.cpp
3  *
4  * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
5  *
6  * Permission to use, copy, modify, distribute and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.
11  * It is provided "as is" without express or implied warranty.
12  *
13  */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <exception>
19 #include "hpdf.h"
20
21 #ifdef HPDF_DLL
22 void  __stdcall
23 #else
24 void
25 #endif
26 error_handler (HPDF_STATUS   error_no,
27                HPDF_STATUS   detail_no,
28                void         *user_data)
29 {
30     printf ("ERROR: error_no=%04X, detail_no=%u\n",
31       (HPDF_UINT)error_no, (HPDF_UINT)detail_no);
32
33     throw std::exception ();
34 }
35
36 const char *font_list[] = {
37     "Courier",
38     "Courier-Bold",
39     "Courier-Oblique",
40     "Courier-BoldOblique",
41     "Helvetica",
42     "Helvetica-Bold",
43     "Helvetica-Oblique",
44     "Helvetica-BoldOblique",
45     "Times-Roman",
46     "Times-Bold",
47     "Times-Italic",
48     "Times-BoldItalic",
49     "Symbol",
50     "ZapfDingbats",
51     NULL
52 };
53
54 int main (int argc, char **argv)
55 {
56     const char *page_title = "Font Demo";
57     HPDF_Doc  pdf;
58     char fname[256];
59     HPDF_Page page;
60     HPDF_Font def_font;
61     HPDF_REAL tw;
62     HPDF_REAL height;
63     HPDF_REAL width;
64     HPDF_UINT i;
65
66     strcpy (fname, argv[0]);
67     strcat (fname, ".pdf");
68
69     pdf = HPDF_New (error_handler, NULL);
70     if (!pdf) {
71         printf ("error: cannot create PdfDoc object\n");
72         return 1;
73     }
74
75     try {
76         /* Add a new page object. */
77         page = HPDF_AddPage (pdf);
78
79         height = HPDF_Page_GetHeight (page);
80         width = HPDF_Page_GetWidth (page);
81
82         /* Print the lines of the page. */
83         HPDF_Page_SetLineWidth (page, 1);
84         HPDF_Page_Rectangle (page, 50, 50, width - 100, height - 110);
85         HPDF_Page_Stroke (page);
86
87         /* Print the title of the page (with positioning center). */
88         def_font = HPDF_GetFont (pdf, "Helvetica", NULL);
89         HPDF_Page_SetFontAndSize (page, def_font, 24);
90
91         tw = HPDF_Page_TextWidth (page, page_title);
92         HPDF_Page_BeginText (page);
93         HPDF_Page_MoveTextPos (page, (width - tw) / 2, height - 50);
94         HPDF_Page_ShowText (page, page_title);
95         HPDF_Page_EndText (page);
96
97         /* output subtitle. */
98         HPDF_Page_BeginText (page);
99         HPDF_Page_MoveTextPos (page, 60, height - 80);
100         HPDF_Page_SetFontAndSize (page, def_font, 16);
101         HPDF_Page_ShowText (page, "<Standerd Type1 fonts samples>");
102         HPDF_Page_EndText (page);
103
104         HPDF_Page_BeginText (page);
105         HPDF_Page_MoveTextPos (page, 60, height - 105);
106
107         i = 0;
108         while (font_list[i]) {
109             const char* samp_text = "abcdefgABCDEFG12345!#$%&+-@?";
110             HPDF_Font font = HPDF_GetFont (pdf, font_list[i], NULL);
111
112             /* print a label of text */
113             HPDF_Page_SetFontAndSize (page, def_font, 9);
114             HPDF_Page_ShowText (page, font_list[i]);
115             HPDF_Page_MoveTextPos (page, 0, -18);
116
117             /* print a sample text. */
118             HPDF_Page_SetFontAndSize (page, font, 20);
119             HPDF_Page_ShowText (page, samp_text);
120                 HPDF_Page_MoveTextPos (page, 0, -20);
121
122             i++;
123         }
124
125         HPDF_Page_EndText (page);
126
127         HPDF_SaveToFile (pdf, fname);
128
129     } catch (...) {
130         HPDF_Free (pdf);
131         return 1;
132     }
133
134     /* clean up */
135     HPDF_Free (pdf);
136
137     return 0;
138 }
139