1 /*
2  * << Haru Free PDF Library 2.0.0 >> -- ttfont_demo.c
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 <setjmp.h>
19 #include "hpdf.h"
20 
21 jmp_buf env;
22 
23 #ifdef HPDF_DLL
24 void  __stdcall
25 #else
26 void
27 #endif
error_handler(HPDF_STATUS error_no,HPDF_STATUS detail_no,void * user_data)28 error_handler (HPDF_STATUS   error_no,
29                HPDF_STATUS   detail_no,
30                void         *user_data)
31 {
32     printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
33                 (HPDF_UINT)detail_no);
34     longjmp(env, 1);
35 }
36 
main(int argc,char ** argv)37 int main (int argc, char **argv)
38 {
39     const char* SAMP_TXT = "The quick brown fox jumps over the lazy dog.";
40 
41     HPDF_Doc  pdf;
42     char fname[256];
43     HPDF_Page page;
44     HPDF_Font title_font;
45     HPDF_Font detail_font;
46     const char *detail_font_name;
47     HPDF_BOOL embed;
48     HPDF_REAL page_height;
49     HPDF_REAL page_width;
50     HPDF_REAL pw;
51 
52     if (argc < 2) {
53         printf("usage: ttfont_demo [path to font file] "
54                 "-E(embedding font).\n");
55         return 1;
56     }
57 
58     strcpy (fname, argv[0]);
59     strcat (fname, ".pdf");
60 
61     pdf = HPDF_New (error_handler, NULL);
62     if (!pdf) {
63         printf ("error: cannot create PdfDoc object\n");
64         return 1;
65     }
66 
67     if (setjmp(env)) {
68         HPDF_Free (pdf);
69         return 1;
70     }
71 
72     /* Add a new page object. */
73     page = HPDF_AddPage (pdf);
74 
75     title_font = HPDF_GetFont (pdf, "Helvetica", NULL);
76 
77     if (argc > 2 && memcmp(argv[2], "-E", 2) == 0)
78         embed = HPDF_TRUE;
79     else
80         embed = HPDF_FALSE;
81 
82     detail_font_name = HPDF_LoadTTFontFromFile (pdf, argv[1], embed);
83 
84     detail_font = HPDF_GetFont (pdf, detail_font_name, NULL);
85 
86     HPDF_Page_SetFontAndSize (page, title_font, 10);
87 
88     HPDF_Page_BeginText (page);
89 
90     /* Move the position of the text to top of the page. */
91     HPDF_Page_MoveTextPos(page, 10, 190);
92     HPDF_Page_ShowText (page, detail_font_name);
93 
94     if (embed)
95         HPDF_Page_ShowText (page, "(Embedded Subset)");
96 
97     HPDF_Page_SetFontAndSize (page, detail_font, 15);
98     HPDF_Page_MoveTextPos (page, 10, -20);
99     HPDF_Page_ShowText (page, "abcdefghijklmnopqrstuvwxyz");
100     HPDF_Page_MoveTextPos (page, 0, -20);
101     HPDF_Page_ShowText (page, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
102     HPDF_Page_MoveTextPos (page, 0, -20);
103     HPDF_Page_ShowText (page, "1234567890");
104     HPDF_Page_MoveTextPos (page, 0, -20);
105 
106     HPDF_Page_SetFontAndSize (page, detail_font, 10);
107     HPDF_Page_ShowText (page, SAMP_TXT);
108     HPDF_Page_MoveTextPos (page, 0, -18);
109 
110     HPDF_Page_SetFontAndSize (page, detail_font, 16);
111     HPDF_Page_ShowText (page, SAMP_TXT);
112     HPDF_Page_MoveTextPos (page, 0, -27);
113 
114     HPDF_Page_SetFontAndSize (page, detail_font, 23);
115     HPDF_Page_ShowText (page, SAMP_TXT);
116     HPDF_Page_MoveTextPos (page, 0, -36);
117 
118     HPDF_Page_SetFontAndSize (page, detail_font, 30);
119     HPDF_Page_ShowText (page, SAMP_TXT);
120     HPDF_Page_MoveTextPos (page, 0, -36);
121 
122     pw = HPDF_Page_TextWidth (page, SAMP_TXT);
123     page_height = 210;
124     page_width = pw + 40;
125 
126     HPDF_Page_SetWidth (page, page_width);
127     HPDF_Page_SetHeight (page, page_height);
128 
129     /* Finish to print text. */
130     HPDF_Page_EndText (page);
131 
132     HPDF_Page_SetLineWidth (page, 0.5);
133 
134     HPDF_Page_MoveTo (page, 10, page_height - 25);
135     HPDF_Page_LineTo (page, page_width - 10, page_height - 25);
136     HPDF_Page_Stroke (page);
137 
138     HPDF_Page_MoveTo (page, 10, page_height - 85);
139     HPDF_Page_LineTo (page, page_width - 10, page_height - 85);
140     HPDF_Page_Stroke (page);
141 
142     HPDF_SaveToFile (pdf, fname);
143 
144     /* clean up */
145     HPDF_Free (pdf);
146 
147     return 0;
148 }
149 
150