OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / demo / text_demo2.c
1 /*
2  * << Alternative PDF Library 1.0.0 >> -- text_demo2.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 <math.h>
19 #include <setjmp.h>
20 #include "hpdf.h"
21 #include "grid_sheet.h"
22
23 jmp_buf env;
24
25 #ifdef HPDF_DLL
26 void __stdcall
27 #else
28 void
29 #endif
30 error_handler  (HPDF_STATUS   error_no,
31                 HPDF_STATUS   detail_no,
32                 void         *user_data)
33 {
34     printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
35                 (HPDF_UINT)detail_no);
36     longjmp(env, 1);
37 }
38
39 int no = 0;
40
41 void
42 PrintText(HPDF_Page page)
43 {
44     char buf[512];
45     HPDF_Point pos = HPDF_Page_GetCurrentTextPos (page);
46
47     no++;
48     #ifdef __WIN32__
49     _snprintf (buf, 512, ".[%d]%0.2f %0.2f", no, pos.x, pos.y);
50     #else
51     snprintf (buf, 512, ".[%d]%0.2f %0.2f", no, pos.x, pos.y);
52     #endif
53     HPDF_Page_ShowText(page, buf);
54 }
55
56
57 int
58 main (int argc, char **argv)
59 {
60     HPDF_Doc  pdf;
61     HPDF_Page page;
62     char fname[256];
63     HPDF_Font font;
64     float angle1;
65     float angle2;
66     float rad1;
67     float rad2;
68     HPDF_REAL page_height;
69     HPDF_Rect rect;
70     int i;
71
72     const char* SAMP_TXT = "The quick brown fox jumps over the lazy dog. ";
73
74     strcpy (fname, argv[0]);
75     strcat (fname, ".pdf");
76
77     pdf = HPDF_New (error_handler, NULL);
78     if (!pdf) {
79         printf ("error: cannot create PdfDoc object\n");
80         return 1;
81     }
82
83     if (setjmp(env)) {
84         HPDF_Free (pdf);
85         return 1;
86     }
87
88     /* add a new page object. */
89     page = HPDF_AddPage (pdf);
90     HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_A5, HPDF_PAGE_PORTRAIT);
91
92     print_grid  (pdf, page);
93
94     page_height = HPDF_Page_GetHeight (page);
95
96     font = HPDF_GetFont (pdf, "Helvetica", NULL);
97     HPDF_Page_SetTextLeading (page, 20);
98
99     /* text_rect method */
100
101     /* HPDF_TALIGN_LEFT */
102     rect.left = 25;
103     rect.top = 545;
104     rect.right = 200;
105     rect.bottom = rect.top - 40;
106
107     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
108                 rect.top - rect.bottom);
109     HPDF_Page_Stroke (page);
110
111     HPDF_Page_BeginText (page);
112
113     HPDF_Page_SetFontAndSize (page, font, 10);
114     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_LEFT");
115
116     HPDF_Page_SetFontAndSize (page, font, 13);
117     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
118                 SAMP_TXT, HPDF_TALIGN_LEFT, NULL);
119
120     HPDF_Page_EndText (page);
121
122     /* HPDF_TALIGN_RIGTH */
123     rect.left = 220;
124     rect.right = 395;
125
126     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
127                 rect.top - rect.bottom);
128     HPDF_Page_Stroke (page);
129
130     HPDF_Page_BeginText (page);
131
132     HPDF_Page_SetFontAndSize (page, font, 10);
133     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_RIGTH");
134
135     HPDF_Page_SetFontAndSize (page, font, 13);
136     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
137                 SAMP_TXT, HPDF_TALIGN_RIGHT, NULL);
138
139     HPDF_Page_EndText (page);
140
141     /* HPDF_TALIGN_CENTER */
142     rect.left = 25;
143     rect.top = 475;
144     rect.right = 200;
145     rect.bottom = rect.top - 40;
146
147     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
148                 rect.top - rect.bottom);
149     HPDF_Page_Stroke (page);
150
151     HPDF_Page_BeginText (page);
152
153     HPDF_Page_SetFontAndSize (page, font, 10);
154     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_CENTER");
155
156     HPDF_Page_SetFontAndSize (page, font, 13);
157     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
158                 SAMP_TXT, HPDF_TALIGN_CENTER, NULL);
159
160     HPDF_Page_EndText (page);
161
162     /* HPDF_TALIGN_JUSTIFY */
163     rect.left = 220;
164     rect.right = 395;
165
166     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
167                 rect.top - rect.bottom);
168     HPDF_Page_Stroke (page);
169
170     HPDF_Page_BeginText (page);
171
172     HPDF_Page_SetFontAndSize (page, font, 10);
173     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_JUSTIFY");
174
175     HPDF_Page_SetFontAndSize (page, font, 13);
176     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
177                 SAMP_TXT, HPDF_TALIGN_JUSTIFY, NULL);
178
179     HPDF_Page_EndText (page);
180
181
182
183     /* Skewed coordinate system */
184     HPDF_Page_GSave (page);
185
186     angle1 = 5;
187     angle2 = 10;
188     rad1 = angle1 / 180 * 3.141592;
189     rad2 = angle2 / 180 * 3.141592;
190
191     HPDF_Page_Concat (page, 1, tan(rad1), tan(rad2), 1, 25, 350);
192     rect.left = 0;
193     rect.top = 40;
194     rect.right = 175;
195     rect.bottom = 0;
196
197     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
198                 rect.top - rect.bottom);
199     HPDF_Page_Stroke (page);
200
201     HPDF_Page_BeginText (page);
202
203     HPDF_Page_SetFontAndSize (page, font, 10);
204     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "Skewed coordinate system");
205
206     HPDF_Page_SetFontAndSize (page, font, 13);
207     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
208                 SAMP_TXT, HPDF_TALIGN_LEFT, NULL);
209
210     HPDF_Page_EndText (page);
211
212     HPDF_Page_GRestore (page);
213
214
215     /* Rotated coordinate system */
216     HPDF_Page_GSave (page);
217
218     angle1 = 5;
219     rad1 = angle1 / 180 * 3.141592;
220
221     HPDF_Page_Concat (page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), 220, 350);
222     rect.left = 0;
223     rect.top = 40;
224     rect.right = 175;
225     rect.bottom = 0;
226
227     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
228                 rect.top - rect.bottom);
229     HPDF_Page_Stroke (page);
230
231     HPDF_Page_BeginText (page);
232
233     HPDF_Page_SetFontAndSize (page, font, 10);
234     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "Rotated coordinate system");
235
236     HPDF_Page_SetFontAndSize (page, font, 13);
237     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
238                 SAMP_TXT, HPDF_TALIGN_LEFT, NULL);
239
240     HPDF_Page_EndText (page);
241
242     HPDF_Page_GRestore (page);
243
244
245     /* text along a circle */
246     HPDF_Page_SetGrayStroke (page, 0);
247     HPDF_Page_Circle (page, 210, 190, 145);
248     HPDF_Page_Circle (page, 210, 190, 113);
249     HPDF_Page_Stroke (page);
250
251     angle1 = 360 / (strlen (SAMP_TXT));
252     angle2 = 180;
253
254     HPDF_Page_BeginText (page);
255     font = HPDF_GetFont (pdf, "Courier-Bold", NULL);
256     HPDF_Page_SetFontAndSize (page, font, 30);
257
258     for (i = 0; i < strlen (SAMP_TXT); i++) {
259         char buf[2];
260         float x;
261         float y;
262
263         rad1 = (angle2 - 90) / 180 * 3.141592;
264         rad2 = angle2 / 180 * 3.141592;
265
266         x = 210 + cos(rad2) * 122;
267         y = 190 + sin(rad2) * 122;
268
269         HPDF_Page_SetTextMatrix(page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), x, y);
270
271         buf[0] = SAMP_TXT[i];
272         buf[1] = 0;
273         HPDF_Page_ShowText (page, buf);
274         angle2 -= angle1;
275     }
276
277     HPDF_Page_EndText (page);
278
279     /* save the document to a file */
280     HPDF_SaveToFile (pdf, fname);
281
282     /* clean up */
283     HPDF_Free (pdf);
284
285     return 0;
286 }
287
288
289