OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / demo / jpfont_demo.c
1 /*
2  * << Haru Free PDF Library 2.0.0 >> -- jpfont_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
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 int main (int argc, char **argv)
38 {
39     HPDF_Doc  pdf;
40     char fname[256];
41     HPDF_Font title_font;
42     HPDF_Font detail_font[16];
43     char samp_text[2048];
44     HPDF_Outline root;
45     HPDF_UINT i;
46     const HPDF_UINT PAGE_HEIGHT = 210;
47
48 #ifdef __WIN32__
49     FILE* f = fopen ("mbtext\\sjis.txt", "rb");
50 #else
51     FILE* f = fopen ("mbtext/sjis.txt", "rb");
52 #endif
53
54     if (!f) {
55         printf ("error: cannot open 'mbtext/sjis.txt'\n");
56         return 1;
57     }
58
59     fgets (samp_text, 2048, f);
60     fclose (f);
61
62     strcpy (fname, argv[0]);
63     strcat (fname, ".pdf");
64
65     pdf = HPDF_New (error_handler, NULL);
66     if (!pdf) {
67         printf ("error: cannot create PdfDoc object\n");
68         return 1;
69     }
70
71     if (setjmp(env)) {
72     HPDF_Free (pdf);
73         return 1;
74     }
75
76     /* configure pdf-document to be compressed. */
77     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
78
79     /* declaration for using Japanese font, encoding. */
80     HPDF_UseJPEncodings (pdf);
81     HPDF_UseJPFonts (pdf);
82
83     detail_font[0] = HPDF_GetFont (pdf, "MS-Mincyo", "90ms-RKSJ-H");
84     detail_font[1] = HPDF_GetFont (pdf, "MS-Mincyo,Bold", "90ms-RKSJ-H");
85     detail_font[2] = HPDF_GetFont (pdf, "MS-Mincyo,Italic", "90ms-RKSJ-H");
86     detail_font[3] = HPDF_GetFont (pdf, "MS-Mincyo,BoldItalic", "90ms-RKSJ-H");
87     detail_font[4] = HPDF_GetFont (pdf, "MS-PMincyo", "90msp-RKSJ-H");
88     detail_font[5] = HPDF_GetFont (pdf, "MS-PMincyo,Bold", "90msp-RKSJ-H");
89     detail_font[6] = HPDF_GetFont (pdf, "MS-PMincyo,Italic", "90msp-RKSJ-H");
90     detail_font[7] = HPDF_GetFont (pdf, "MS-PMincyo,BoldItalic",
91             "90msp-RKSJ-H");
92     detail_font[8] = HPDF_GetFont (pdf, "MS-Gothic", "90ms-RKSJ-H");
93     detail_font[9] = HPDF_GetFont (pdf, "MS-Gothic,Bold", "90ms-RKSJ-H");
94     detail_font[10] = HPDF_GetFont (pdf, "MS-Gothic,Italic", "90ms-RKSJ-H");
95     detail_font[11] = HPDF_GetFont (pdf, "MS-Gothic,BoldItalic", "90ms-RKSJ-H");
96     detail_font[12] = HPDF_GetFont (pdf, "MS-PGothic", "90msp-RKSJ-H");
97     detail_font[13] = HPDF_GetFont (pdf, "MS-PGothic,Bold", "90msp-RKSJ-H");
98     detail_font[14] = HPDF_GetFont (pdf, "MS-PGothic,Italic", "90msp-RKSJ-H");
99     detail_font[15] = HPDF_GetFont (pdf, "MS-PGothic,BoldItalic",
100             "90msp-RKSJ-H");
101
102     /* Set page mode to use outlines. */
103     HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE);
104
105     /* create outline root. */
106     root = HPDF_CreateOutline (pdf, NULL, "JP font demo", NULL);
107     HPDF_Outline_SetOpened (root, HPDF_TRUE);
108
109     for (i = 0; i <= 15; i++) {
110         HPDF_Page page;
111         HPDF_Outline outline;
112         HPDF_Destination dst;
113         HPDF_REAL x_pos;
114         HPDF_Point p;
115         HPDF_UINT j;
116
117         /* add a new page object. */
118         page = HPDF_AddPage (pdf);
119
120         /* create outline entry */
121         outline = HPDF_CreateOutline (pdf, root,
122                 HPDF_Font_GetFontName (detail_font[i]), NULL);
123         dst = HPDF_Page_CreateDestination (page);
124         HPDF_Outline_SetDestination(outline, dst);
125
126         title_font = HPDF_GetFont (pdf, "Helvetica", NULL);
127         HPDF_Page_SetFontAndSize (page, title_font, 10);
128
129         HPDF_Page_BeginText (page);
130
131         /* move the position of the text to top of the page. */
132         HPDF_Page_MoveTextPos(page, 10, 190);
133         HPDF_Page_ShowText (page, HPDF_Font_GetFontName (detail_font[i]));
134
135         HPDF_Page_SetFontAndSize (page, detail_font[i], 15);
136         HPDF_Page_MoveTextPos (page, 10, -20);
137         HPDF_Page_ShowText (page, "abcdefghijklmnopqrstuvwxyz");
138         HPDF_Page_MoveTextPos (page, 0, -20);
139         HPDF_Page_ShowText (page, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
140         HPDF_Page_MoveTextPos (page, 0, -20);
141         HPDF_Page_ShowText (page, "1234567890");
142         HPDF_Page_MoveTextPos (page, 0, -20);
143
144         HPDF_Page_SetFontAndSize (page, detail_font[i], 10);
145         HPDF_Page_ShowText (page, samp_text);
146         HPDF_Page_MoveTextPos (page, 0, -18);
147
148         HPDF_Page_SetFontAndSize (page, detail_font[i], 16);
149         HPDF_Page_ShowText (page, samp_text);
150         HPDF_Page_MoveTextPos (page, 0, -27);
151
152         HPDF_Page_SetFontAndSize (page, detail_font[i], 23);
153         HPDF_Page_ShowText (page, samp_text);
154         HPDF_Page_MoveTextPos (page, 0, -36);
155
156         HPDF_Page_SetFontAndSize (page, detail_font[i], 30);
157         HPDF_Page_ShowText (page, samp_text);
158
159         p = HPDF_Page_GetCurrentTextPos (page);
160
161         /* finish to print text. */
162         HPDF_Page_EndText (page);
163
164         HPDF_Page_SetLineWidth (page, 0.5);
165
166         x_pos = 20;
167         for (j = 0; j <= strlen (samp_text) / 2; j++) {
168             HPDF_Page_MoveTo (page, x_pos, p.y - 10);
169             HPDF_Page_LineTo (page, x_pos, p.y - 12);
170             HPDF_Page_Stroke (page);
171             x_pos = x_pos + 30;
172         }
173
174         HPDF_Page_SetWidth (page, p.x + 20);
175         HPDF_Page_SetHeight (page, PAGE_HEIGHT);
176
177         HPDF_Page_MoveTo (page, 10, PAGE_HEIGHT - 25);
178         HPDF_Page_LineTo (page, p.x + 10, PAGE_HEIGHT - 25);
179         HPDF_Page_Stroke (page);
180
181         HPDF_Page_MoveTo (page, 10, PAGE_HEIGHT - 85);
182         HPDF_Page_LineTo (page, p.x + 10, PAGE_HEIGHT - 85);
183         HPDF_Page_Stroke (page);
184
185         HPDF_Page_MoveTo (page, 10, p.y - 12);
186         HPDF_Page_LineTo (page, p.x + 10, p.y - 12);
187         HPDF_Page_Stroke (page);
188     }
189
190     HPDF_SaveToFile (pdf, fname);
191
192     /* clean up */
193     HPDF_Free (pdf);
194
195     return 0;
196 }
197