OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / demo / encoding_list.c
1 /*
2  * << Haru Free PDF Library 2.0.0 >> -- encoding_list.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
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
37 static const int PAGE_WIDTH = 420;
38 static const int PAGE_HEIGHT = 400;
39 static const int CELL_WIDTH = 20;
40 static const int CELL_HEIGHT = 20;
41 static const int CELL_HEADER = 10;
42
43 void
44 draw_graph (HPDF_Page   page);
45
46
47 void
48 draw_fonts (HPDF_Page   page);
49
50
51 void
52 draw_graph (HPDF_Page   page)
53 {
54     char buf[50];
55     int i;
56
57     /* Draw 16 X 15 cells */
58
59     /* Draw vertical lines. */
60     HPDF_Page_SetLineWidth (page, 0.5);
61
62     for (i = 0; i <= 17; i++) {
63         int x = i * CELL_WIDTH + 40;
64
65         HPDF_Page_MoveTo (page, x, PAGE_HEIGHT - 60);
66         HPDF_Page_LineTo (page, x, 40);
67         HPDF_Page_Stroke (page);
68
69         if (i > 0 && i <= 16) {
70             HPDF_Page_BeginText (page);
71             HPDF_Page_MoveTextPos (page, x + 5, PAGE_HEIGHT - 75);
72 #ifdef __WIN32__
73             _snprintf(buf, 5, "%X", i - 1);
74 #else
75             snprintf(buf, 5, "%X", i - 1);
76 #endif
77             HPDF_Page_ShowText (page, buf);
78             HPDF_Page_EndText (page);
79         }
80     }
81
82     /* Draw horizontal lines. */
83     for (i = 0; i <= 15; i++) {
84        int y = i * CELL_HEIGHT + 40;
85
86         HPDF_Page_MoveTo (page, 40, y);
87         HPDF_Page_LineTo (page, PAGE_WIDTH - 40, y);
88         HPDF_Page_Stroke (page);
89
90         if (i < 14) {
91             HPDF_Page_BeginText (page);
92             HPDF_Page_MoveTextPos (page, 45, y + 5);
93 #ifdef __WIN32__
94             _snprintf(buf, 5, "%X", 15 - i);
95 #else
96             snprintf(buf, 5, "%X", 15 - i);
97 #endif
98             HPDF_Page_ShowText (page, buf);
99             HPDF_Page_EndText (page);
100         }
101     }
102 }
103
104
105 void
106 draw_fonts (HPDF_Page   page)
107 {
108     int i;
109     int j;
110
111     HPDF_Page_BeginText (page);
112
113     /* Draw all character from 0x20 to 0xFF to the canvas. */
114     for (i = 1; i < 17; i++) {
115         for (j = 1; j < 17; j++) {
116             unsigned char buf[2];
117             int y = PAGE_HEIGHT - 55 - ((i - 1) * CELL_HEIGHT);
118             int x = j * CELL_WIDTH + 50;
119
120             buf[1] = 0x00;
121
122             buf[0] = (i - 1) * 16 + (j - 1);
123             if (buf[0] >= 32) {
124                 double d;
125
126                 d  = x - HPDF_Page_TextWidth (page, (char*)buf) / 2;
127                 HPDF_Page_TextOut (page, d, y, (char*)buf);
128
129             }
130         }
131     }
132
133     HPDF_Page_EndText (page);
134 }
135
136
137 int main (int argc, char **argv)
138 {
139     HPDF_Doc  pdf;
140     char fname[256];
141     HPDF_Font font;
142     const char *font_name;
143     int i = 0;
144     HPDF_Outline root;
145
146     const char *encodings[] = {
147             "StandardEncoding",
148             "MacRomanEncoding",
149             "WinAnsiEncoding",
150             "ISO8859-2",
151             "ISO8859-3",
152             "ISO8859-4",
153             "ISO8859-5",
154             "ISO8859-9",
155             "ISO8859-10",
156             "ISO8859-13",
157             "ISO8859-14",
158             "ISO8859-15",
159             "ISO8859-16",
160             "CP1250",
161             "CP1251",
162             "CP1252",
163             "CP1254",
164             "CP1257",
165             "KOI8-R",
166             "Symbol-Set",
167             "ZapfDingbats-Set",
168             NULL
169     };
170
171     pdf = HPDF_NewEx (error_handler, NULL, NULL, 0, NULL);
172     if (!pdf) {
173         printf ("error: cannot create PdfDoc object\n");
174         return 1;
175     }
176
177     if (setjmp(env)) {
178         HPDF_Free (pdf);
179         return 1;
180     }
181
182     strcpy (fname, argv[0]);
183     strcat (fname, ".pdf");
184
185     /* set compression mode */
186     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
187
188     /* Set page mode to use outlines. */
189     HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE);
190
191     /* get default font */
192     font = HPDF_GetFont (pdf, "Helvetica", NULL);
193
194     /* load font object */
195     #ifdef __WIN32__
196     font_name = HPDF_LoadType1FontFromFile (pdf, "type1\\a010013l.afm",
197             "type1\\a010013l.pfb");
198     #else
199     font_name = HPDF_LoadType1FontFromFile (pdf, "type1/a010013l.afm",
200             "type1/a010013l.pfb");
201     #endif
202
203     /* create outline root. */
204     root = HPDF_CreateOutline (pdf, NULL, "Encoding list", NULL);
205     HPDF_Outline_SetOpened (root, HPDF_TRUE);
206
207     while (encodings[i]) {
208         HPDF_Page page = HPDF_AddPage (pdf);
209         HPDF_Outline outline;
210         HPDF_Destination dst;
211         HPDF_Font font2;
212
213         HPDF_Page_SetWidth (page, PAGE_WIDTH);
214         HPDF_Page_SetHeight (page, PAGE_HEIGHT);
215
216         outline = HPDF_CreateOutline (pdf, root, encodings[i], NULL);
217         dst = HPDF_Page_CreateDestination (page);
218         HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page), 1);
219         /* HPDF_Destination_SetFitB(dst); */
220         HPDF_Outline_SetDestination(outline, dst);
221
222         HPDF_Page_SetFontAndSize (page, font, 15);
223         draw_graph (page);
224
225         HPDF_Page_BeginText (page);
226         HPDF_Page_SetFontAndSize (page, font, 20);
227         HPDF_Page_MoveTextPos (page, 40, PAGE_HEIGHT - 50);
228         HPDF_Page_ShowText (page, encodings[i]);
229         HPDF_Page_ShowText (page, " Encoding");
230         HPDF_Page_EndText (page);
231
232         if (strcmp (encodings[i], "Symbol-Set") == 0)
233             font2 = HPDF_GetFont (pdf, "Symbol", NULL);
234         else if (strcmp (encodings[i], "ZapfDingbats-Set") == 0)
235             font2 = HPDF_GetFont (pdf, "ZapfDingbats", NULL);
236         else
237             font2 = HPDF_GetFont (pdf, font_name, encodings[i]);
238
239         HPDF_Page_SetFontAndSize (page, font2, 14);
240         draw_fonts (page);
241
242         i++;
243     }
244
245     /* save the document to a file */
246     HPDF_SaveToFile (pdf, fname);
247
248     /* clean up */
249     HPDF_Free (pdf);
250
251     return 0;
252 }