2 ## * << Haru Free PDF Library 2.0.0 >> -- character_map.c
4 ## * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
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.
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
19 ## port to python by Li Jun
20 ## http://groups.google.com/group/pythoncia
28 path=os.path.normpath(os.path.split(os.path.realpath(__file__))[0]+'\..'*up)
29 if path not in sys.path:
35 from haru.c_func import *
36 from haru.hpdf_errorcode import *
39 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
40 def error_handler (error_no, detail_no, user_data):
42 printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
47 def draw_page(pdf, page, title_font, font, h_byte, l_byte):
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
57 HPDF_Page_SetHeight (page, page_height)
58 HPDF_Page_SetWidth (page, PAGE_WIDTH)
60 HPDF_Page_SetFontAndSize (page, title_font, 10)
64 y = (ypos) * CELL_HEIGHT + 40
66 HPDF_Page_MoveTo (page, 40, y)
67 HPDF_Page_LineTo (page, 380, y)
68 HPDF_Page_Stroke (page)
72 buf[0] = 16 - ypos - 1
76 buf[0] += (ord('A') - 10)
78 buf=[i % 256 for i in buf] #because buf is unsigned char *
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)
93 for xpos in range(18):
94 y = (h_count + 1) * CELL_HEIGHT + 40
95 x = xpos * CELL_WIDTH + 40
97 HPDF_Page_MoveTo (page, x, 40)
98 HPDF_Page_LineTo (page, x, y)
99 HPDF_Page_Stroke (page)
101 if (xpos > 0 and xpos <= 16):
108 buf[0] += (ord('A') - 10)
110 buf=[i % 256 for i in buf] #because buf is unsigned char *
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)
120 HPDF_Page_SetFontAndSize (page, font, 15)
124 y = (ypos - 1) * CELL_HEIGHT + 45
126 for xpos in range(16):
127 buf=[None for i in range(3)]
129 x = xpos * CELL_WIDTH + 40 + CELL_WIDTH
132 buf[1] = (16 - ypos) * 16 + xpos
134 buf=[i % 256 for i in buf] #because buf is unsigned char *
136 w = HPDF_Page_TextWidth(page, buf)
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)
153 flg=[HPDF_UINT16(0) for i in range(256)]
155 fname=os.path.realpath(sys.argv[0])
156 fname=fname[:fname.rfind('.')]+'.pdf'
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')
163 pdf = HPDF_New (error_handler, NULL)
165 printf ("error: cannot create PdfDoc object\n")
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)
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)
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])
189 font = HPDF_GetFont (pdf, sys.argv[2], sys.argv[1])
198 for j in range(20, 256):
199 buf=[None, None ,None]
206 btype = HPDF_Encoder_GetByteType (encoder, buf, 0)
207 unicode = HPDF_Encoder_GetUnicode (encoder, code)
209 if (btype == HPDF_BYTE_TYPE_LEAD and
225 printf ("min_h=%04X max_h=%04X min_l=%04X max_l=%04X\n",
226 min_h, max_h, min_l, max_l)
228 # create outline root.
229 root = HPDF_CreateOutline (pdf, NULL, sys.argv[1], NULL)
230 HPDF_Outline_SetOpened (root, HPDF_TRUE)
234 page = HPDF_AddPage (pdf)
235 title_font = HPDF_GetFont (pdf, "Helvetica", NULL)
237 buf="0x%04X-0x%04X" %(
242 outline = HPDF_CreateOutline (pdf, root, buf, NULL)
243 dst = HPDF_Page_CreateDestination (page)
244 HPDF_Outline_SetDestination(outline, dst)
246 draw_page (pdf, page, title_font, font, i, min_l)
248 buf="%s (%s) 0x%04X-0x%04X" %(
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)
260 HPDF_SaveToFile (pdf, fname)