OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / character_map.py
1 ###
2 ## * << Haru Free PDF Library 2.0.0 >> -- character_map.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 ## * usage character_map <encoding-name> <low-range-from> <low-range-to>
14 ## *              <high-range-from> <high-range-to>
15 ## * ex. character_map 90ms-RKSJ-V 0x80 0x
16 ## *
17 ##
18
19 ## port to python by Li Jun
20 ## http://groups.google.com/group/pythoncia
21
22 import os, sys
23
24 from ctypes import *
25 up=2
26 def setlibpath(up):
27     import sys
28     path=os.path.normpath(os.path.split(os.path.realpath(__file__))[0]+'\..'*up)
29     if path not in sys.path:
30         sys.path.append(path)
31
32 setlibpath(up)
33
34 from haru import *
35 from haru.c_func import *
36 from haru.hpdf_errorcode import *
37
38
39 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
40 def error_handler (error_no, detail_no, user_data):
41     global pdf
42     printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
43                 detail_no)
44     HPDF_Free (pdf)
45     sys.exit(1)
46
47 def draw_page(pdf, page, title_font, font, h_byte, l_byte):
48     PAGE_WIDTH = 420
49     CELL_HEIGHT = 20
50     CELL_WIDTH = 20
51
52
53     l_byte = int(l_byte / 16) * 16
54     h_count = 16 - (l_byte / 16)
55     page_height = 40 + 40 + (h_count + 1) * CELL_HEIGHT
56
57     HPDF_Page_SetHeight (page, page_height)
58     HPDF_Page_SetWidth (page, PAGE_WIDTH)
59
60     HPDF_Page_SetFontAndSize (page, title_font, 10)
61
62     ypos = h_count + 1
63     while True:
64         y = (ypos) * CELL_HEIGHT + 40
65
66         HPDF_Page_MoveTo (page, 40, y)
67         HPDF_Page_LineTo (page, 380, y)
68         HPDF_Page_Stroke (page)
69         if (ypos < h_count):
70             buf=[None,None]
71
72             buf[0] = 16 - ypos - 1
73             if (buf[0] < 10):
74                 buf[0] += ord('0')
75             else:
76                 buf[0] += (ord('A') - 10)
77             buf[1] = 0
78             buf=[i % 256 for i in buf]      #because buf is unsigned char *
79
80             w = HPDF_Page_TextWidth (page, buf)
81             HPDF_Page_BeginText (page)
82             HPDF_Page_MoveTextPos (page, 40 + (20 - w) / 2, y + 5)
83             HPDF_Page_ShowText (page, buf)
84             HPDF_Page_EndText (page)
85
86
87         if (ypos == 0):
88             break
89
90         ypos-=1
91
92
93     for xpos in range(18):
94         y = (h_count + 1) * CELL_HEIGHT + 40
95         x = xpos * CELL_WIDTH + 40
96
97         HPDF_Page_MoveTo (page, x, 40)
98         HPDF_Page_LineTo (page, x, y)
99         HPDF_Page_Stroke (page)
100
101         if (xpos > 0 and xpos <= 16):
102             buf=[None,None]
103
104             buf[0] = xpos - 1
105             if (buf[0] < 10):
106                 buf[0] += ord('0')
107             else:
108                 buf[0] += (ord('A') - 10)
109             buf[1] = 0
110             buf=[i % 256 for i in buf]      #because buf is unsigned char *
111
112             w = HPDF_Page_TextWidth(page, buf)
113             HPDF_Page_BeginText(page)
114             HPDF_Page_MoveTextPos(page, x + (20 - w) / 2,
115                         h_count * CELL_HEIGHT + 45)
116             HPDF_Page_ShowText(page, buf)
117             HPDF_Page_EndText(page)
118
119
120     HPDF_Page_SetFontAndSize (page, font, 15)
121
122     ypos = h_count
123     while True:
124         y = (ypos - 1) * CELL_HEIGHT + 45
125
126         for xpos in range(16):
127             buf=[None for i in range(3)]
128
129             x = xpos * CELL_WIDTH + 40 + CELL_WIDTH
130
131             buf[0] = h_byte
132             buf[1] = (16 - ypos) * 16 + xpos
133             buf[2] = 0x00
134             buf=[i % 256 for i in buf]      #because buf is unsigned char *
135
136             w = HPDF_Page_TextWidth(page, buf)
137             if (w > 0):
138                 HPDF_Page_BeginText(page)
139                 HPDF_Page_MoveTextPos(page, x + (20 - w) / 2, y)
140                 HPDF_Page_ShowText(page, buf)
141                 HPDF_Page_EndText(page)
142
143
144         if (ypos == 0):
145             break
146
147         ypos-=1
148
149
150 def main ():
151     global  pdf
152
153     flg=[HPDF_UINT16(0) for i in range(256)]
154
155     fname=os.path.realpath(sys.argv[0])
156     fname=fname[:fname.rfind('.')]+'.pdf'
157
158     if (len(sys.argv) < 3):
159         printf ("usage: character_map <encoding-name> <font-name>\n")
160         printf ('for example, character_map.py GBK-EUC-H SimHei,Bold')
161         return 1
162
163     pdf = HPDF_New (error_handler, NULL)
164     if (not pdf):
165         printf ("error: cannot create PdfDoc object\n")
166         return 1
167
168
169     # configure pdf-document (showing outline, compression enabled)
170     HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE)
171     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL)
172     HPDF_SetPagesConfiguration (pdf, 10)
173
174     HPDF_UseJPEncodings (pdf)
175     HPDF_UseJPFonts (pdf)
176     HPDF_UseKREncodings (pdf)
177     HPDF_UseKRFonts (pdf)
178     HPDF_UseCNSEncodings (pdf)
179     HPDF_UseCNSFonts (pdf)
180     HPDF_UseCNTEncodings (pdf)
181     HPDF_UseCNTFonts (pdf)
182
183     encoder = HPDF_GetEncoder (pdf, sys.argv[1])
184     if (HPDF_Encoder_GetType (encoder) != HPDF_ENCODER_TYPE_DOUBLE_BYTE):
185         printf ("error: %s is not cmap-encoder\n", sys.argv[1])
186         HPDF_Free (pdf)
187         return 1
188
189     font = HPDF_GetFont (pdf, sys.argv[2], sys.argv[1])
190
191     min_l = 255
192     min_h = 256
193     max_l = 0
194     max_h = 0
195
196
197     for i in range(256):
198         for j in range(20, 256):
199             buf=[None, None ,None]
200             code = i * 256 + j
201
202             buf[0] = i
203             buf[1] = j
204             buf[2] = 0
205
206             btype = HPDF_Encoder_GetByteType (encoder, buf, 0)
207             unicode = HPDF_Encoder_GetUnicode (encoder, code)
208
209             if (btype == HPDF_BYTE_TYPE_LEAD and
210                     unicode != 0x25A1):
211                 if (min_l > j):
212                     min_l = j
213
214                 if (max_l < j):
215                     max_l = j
216
217                 if (min_h > i):
218                     min_h = i
219
220                 if (max_h < i):
221                     max_h = i
222
223                 flg[i] = 1
224
225     printf ("min_h=%04X max_h=%04X min_l=%04X max_l=%04X\n",
226             min_h, max_h, min_l, max_l)
227
228     # create outline root.
229     root = HPDF_CreateOutline (pdf, NULL, sys.argv[1], NULL)
230     HPDF_Outline_SetOpened (root, HPDF_TRUE)
231
232     for i in range(256):
233         if (flg[i]):
234             page = HPDF_AddPage (pdf)
235             title_font = HPDF_GetFont (pdf, "Helvetica", NULL)
236
237             buf="0x%04X-0x%04X" %(
238                                   (i * 256 + min_l),
239                                   (i * 256 + max_l)
240                                  )
241
242             outline = HPDF_CreateOutline (pdf, root, buf, NULL)
243             dst = HPDF_Page_CreateDestination (page)
244             HPDF_Outline_SetDestination(outline, dst)
245
246             draw_page (pdf, page, title_font, font, i, min_l)
247
248             buf="%s (%s) 0x%04X-0x%04X" %(
249                         sys.argv[1],
250                         sys.argv[2],
251                         (i * 256 + min_l),
252                         (i * 256 + max_l)
253                         )
254             HPDF_Page_SetFontAndSize (page, title_font, 10)
255             HPDF_Page_BeginText (page)
256             HPDF_Page_MoveTextPos (page, 40, HPDF_Page_GetHeight (page) - 35)
257             HPDF_Page_ShowText (page, buf)
258             HPDF_Page_EndText (page)
259
260     HPDF_SaveToFile (pdf, fname)
261     HPDF_Free (pdf)
262
263     return 0
264
265 main()