OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / demo / chfont_demo.c
1 /*
2  * << Haru Free PDF Library 2.0.0 >> -- chfont_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 #include "grid_sheet.h"
21
22 jmp_buf env;
23
24 #ifdef HPDF_DLL
25 void  __stdcall
26 #else
27 void
28 #endif
29 error_handler  (HPDF_STATUS   error_no,
30                 HPDF_STATUS   detail_no,
31                 void         *user_data)
32 {
33     printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
34                 (HPDF_UINT)detail_no);
35     longjmp(env, 1);
36 }
37
38 #ifdef __WIN32__
39 const char* FILE_SEPARATOR = "\\";
40 #else
41 const char* FILE_SEPARATOR = "/";
42 #endif
43
44 int
45 main (int argc, char **argv)
46 {
47     HPDF_Doc  pdf;
48     HPDF_Page page;
49     char fname[256];
50     char buf[1024];
51     FILE *cp932;
52     FILE *cp936;
53     const char *fcp936_name;
54     const char *fcp932_name;
55     HPDF_Font fcp936;
56     HPDF_Font fcp932;
57     int y;
58
59     if (argc < 4) {
60         printf ("chfont_demo <cp936-ttc-font-file-name> "
61                 "<cp936-index> <cp932-ttc-font-file-name> <cp932-index>\n");
62         return 1;
63     }
64
65     strcpy (fname, "mbtext");
66     strcat (fname, FILE_SEPARATOR);
67     strcat (fname, "cp932.txt");
68     cp932 = fopen (fname, "rb");
69     if (!cp932) {
70         printf ("error: cannot open cp932.txt\n");
71         return 1;
72     }
73
74     strcpy (fname, "mbtext");
75     strcat (fname, FILE_SEPARATOR);
76     strcat (fname, "cp936.txt");
77     cp936 = fopen (fname, "rb");
78     if (!cp936) {
79         printf ("error: cannot open cp936.txt\n");
80         return 1;
81     }
82
83     strcpy (fname, argv[0]);
84     strcat (fname, ".pdf");
85
86     pdf = HPDF_New (error_handler, NULL);
87     if (!pdf) {
88         printf ("error: cannot create PdfDoc object\n");
89         return 1;
90     }
91
92     if (setjmp(env)) {
93         HPDF_Free (pdf);
94         return 1;
95     }
96
97     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
98     HPDF_UseJPEncodings (pdf);
99     HPDF_UseCNSEncodings (pdf);
100
101     fcp936_name = HPDF_LoadTTFontFromFile2 (pdf, argv[1], atoi(argv[2]),
102             HPDF_TRUE);
103     fcp932_name = HPDF_LoadTTFontFromFile2 (pdf, argv[3], atoi(argv[4]),
104             HPDF_TRUE);
105
106     /* add a new page object. */
107     page = HPDF_AddPage (pdf);
108
109     HPDF_Page_SetHeight (page, 300);
110     HPDF_Page_SetWidth (page, 550);
111
112     fcp936 = HPDF_GetFont (pdf, fcp936_name, "GBK-EUC-H");
113     fcp932 = HPDF_GetFont (pdf, fcp932_name, "90ms-RKSJ-H");
114
115     print_grid  (pdf, page);
116
117     HPDF_Page_SetTextLeading (page, 20);
118
119     HPDF_Page_BeginText (page);
120     HPDF_Page_MoveTextPos (page, 50, 250);
121     HPDF_Page_SetTextLeading (page, 25);
122
123     while (fgets (buf, 1024, cp936)) {
124         HPDF_Page_SetFontAndSize (page, fcp936, 18);
125         buf [strlen (buf)] = 0;
126         HPDF_Page_ShowText (page, buf);
127
128         if (fgets (buf, 1024, cp932)) {
129             HPDF_Page_SetFontAndSize (page, fcp932, 18);
130             buf [strlen (buf)] = 0;
131             HPDF_Page_ShowText (page, buf);
132         }
133
134         HPDF_Page_MoveToNextLine (page);
135     }
136
137     /* save the document to a file */
138     HPDF_SaveToFile (pdf, fname);
139
140     /* clean up */
141     HPDF_Free (pdf);
142
143     fclose (cp936);
144     fclose (cp932);
145
146     return 0;
147 }
148