OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / demo / ttfont_demo_jp.c
1 /*
2  * << Haru Free PDF Library 2.0.0 >> -- ttfont_demo_jp.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_Page page;
42     HPDF_Font title_font;
43     HPDF_Font detail_font;
44     HPDF_UINT page_height;
45     HPDF_UINT page_width;
46     HPDF_REAL pw;
47     char SAMP_TXT[2048];
48     const char *detail_font_name;
49     FILE *f;
50
51     if (argc < 2) {
52         printf ("ttfont_jp_demo <ttf-font-filename> [-E]\n");
53         printf ("ttfont_jp_demo <ttc-font-filename> <index> [-E]\n");
54         return 1;
55     }
56
57 #ifdef __WIN32__
58     f = fopen ("mbtext\\sjis.txt", "rb");
59 #else
60     f = fopen ("mbtext/sjis.txt", "rb");
61 #endif
62
63     if (!f) {
64         printf ("error: cannot open 'mbtext/sjis.txt'\n");
65         return 1;
66     }
67
68     fgets (SAMP_TXT, 2048, f);
69     fclose (f);
70
71     strcpy (fname, argv[0]);
72     strcat (fname, ".pdf");
73
74     pdf = HPDF_New (error_handler, NULL);
75     if (!pdf) {
76         printf ("error: cannot create PdfDoc object\n");
77         return 1;
78     }
79
80     if (setjmp(env)) {
81         HPDF_Free (pdf);
82         return 1;
83     }
84
85     /* declaration for using Japanese encoding. */
86     HPDF_UseJPEncodings (pdf);
87
88     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
89
90     /* load ttc file */
91     if (argc == 4 && strcmp (argv[3], "-E") == 0)
92         detail_font_name = HPDF_LoadTTFontFromFile2 (pdf, argv[1],
93                 atoi (argv[2]), HPDF_TRUE);
94     else if (argc == 3 && strcmp (argv[2], "-E") == 0)
95         detail_font_name = HPDF_LoadTTFontFromFile (pdf, argv[1], HPDF_TRUE);
96     else if (argc == 3)
97         detail_font_name = HPDF_LoadTTFontFromFile2 (pdf, argv[1],
98                 atoi (argv[2]), HPDF_FALSE);
99     else
100         detail_font_name = HPDF_LoadTTFontFromFile (pdf, argv[1], HPDF_FALSE);
101
102     /* add a new page object. */
103     page = HPDF_AddPage (pdf);
104
105     title_font = HPDF_GetFont (pdf, "Helvetica", NULL);
106
107     detail_font = HPDF_GetFont (pdf, detail_font_name, "90msp-RKSJ-H");
108
109     HPDF_Page_SetFontAndSize (page, title_font, 10);
110
111     HPDF_Page_BeginText (page);
112
113     /* move the position of the text to top of the page. */
114     HPDF_Page_MoveTextPos(page, 10, 190);
115     HPDF_Page_ShowText (page, detail_font_name);
116     HPDF_Page_ShowText (page, " (");
117     HPDF_Page_ShowText (page, HPDF_Font_GetEncodingName (detail_font));
118     HPDF_Page_ShowText (page, ")");
119
120     HPDF_Page_SetFontAndSize (page, detail_font, 15);
121     HPDF_Page_MoveTextPos (page, 10, -20);
122     HPDF_Page_ShowText (page, "abcdefghijklmnopqrstuvwxyz");
123     HPDF_Page_MoveTextPos (page, 0, -20);
124     HPDF_Page_ShowText (page, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
125     HPDF_Page_MoveTextPos (page, 0, -20);
126     HPDF_Page_ShowText (page, "1234567890");
127     HPDF_Page_MoveTextPos (page, 0, -20);
128
129     HPDF_Page_SetFontAndSize (page, detail_font, 10);
130     HPDF_Page_ShowText (page, SAMP_TXT);
131     HPDF_Page_MoveTextPos (page, 0, -18);
132
133     HPDF_Page_SetFontAndSize (page, detail_font, 16);
134     HPDF_Page_ShowText (page, SAMP_TXT);
135     HPDF_Page_MoveTextPos (page, 0, -27);
136
137     HPDF_Page_SetFontAndSize (page, detail_font, 23);
138     HPDF_Page_ShowText (page, SAMP_TXT);
139     HPDF_Page_MoveTextPos (page, 0, -36);
140
141     HPDF_Page_SetFontAndSize (page, detail_font, 30);
142     HPDF_Page_ShowText (page, SAMP_TXT);
143     HPDF_Page_MoveTextPos (page, 0, -36);
144
145     pw = HPDF_Page_TextWidth (page, SAMP_TXT);
146     page_height = 210;
147     page_width = pw + 40;
148
149     HPDF_Page_SetWidth (page, page_width);
150     HPDF_Page_SetHeight (page, page_height);
151
152     /* finish to print text. */
153     HPDF_Page_EndText (page);
154
155     HPDF_Page_SetLineWidth (page, 0.5);
156
157     HPDF_Page_MoveTo (page, 10, page_height - 25);
158     HPDF_Page_LineTo (page, page_width - 10, page_height - 25);
159     HPDF_Page_Stroke (page);
160
161     HPDF_Page_MoveTo (page, 10, page_height - 85);
162     HPDF_Page_LineTo (page, page_width - 10, page_height - 85);
163     HPDF_Page_Stroke (page);
164
165     HPDF_SaveToFile (pdf, fname);
166
167     /* clean up */
168     HPDF_Free (pdf);
169
170     return 0;
171 }
172