OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / encoding_list.py
1 ###
2 ## * << Haru Free PDF Library 2.0.0 >> -- encoding_list.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 ## port to python by Li Jun
16 ## http://groups.google.com/group/pythoncia
17
18 import os, sys
19
20 from ctypes import *
21 up=2
22 def setlibpath(up):
23     import sys
24     path=os.path.normpath(os.path.split(os.path.realpath(__file__))[0]+'\..'*up)
25     if path not in sys.path:
26         sys.path.append(path)
27
28 setlibpath(up)
29
30 from haru import *
31 from haru.c_func import *
32 from haru.hpdf_errorcode import *
33
34
35 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
36 def error_handler (error_no, detail_no, user_data):
37     global pdf
38     printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
39                 detail_no)
40     HPDF_Free (pdf)
41     sys.exit(1)
42
43
44 PAGE_WIDTH = 420
45 PAGE_HEIGHT = 400
46 CELL_WIDTH = 20
47 CELL_HEIGHT = 20
48 CELL_HEADER = 10
49
50 def draw_graph (page):
51
52     # Draw 16 X 15 cells
53
54     # Draw vertical lines.
55     HPDF_Page_SetLineWidth (page, 0.5)
56
57     for i in range(18):
58         x = i * CELL_WIDTH + 40
59
60         HPDF_Page_MoveTo (page, x, PAGE_HEIGHT - 60)
61         HPDF_Page_LineTo (page, x, 40)
62         HPDF_Page_Stroke (page)
63
64         if (i > 0 and i <= 16):
65             HPDF_Page_BeginText (page)
66             HPDF_Page_MoveTextPos (page, x + 5, PAGE_HEIGHT - 75)
67             buf="%X" %(i - 1)
68             HPDF_Page_ShowText (page, buf)
69             HPDF_Page_EndText (page)
70
71     # Draw horizontal lines.
72     for i in range(16):
73         y = i * CELL_HEIGHT + 40
74
75         HPDF_Page_MoveTo (page, 40, y)
76         HPDF_Page_LineTo (page, PAGE_WIDTH - 40, y)
77         HPDF_Page_Stroke (page)
78
79         if (i < 14):
80             HPDF_Page_BeginText (page)
81             HPDF_Page_MoveTextPos (page, 45, y + 5)
82
83             buf="%X" %( 15 - i)
84             HPDF_Page_ShowText (page, buf)
85             HPDF_Page_EndText (page)
86
87
88 def draw_fonts (page):
89     HPDF_Page_BeginText (page)
90
91     # Draw all character from 0x20 to 0xFF to the canvas.
92     for i in range(1,17):
93         for j in range(1,17):
94             buf=[None, None]
95             y = PAGE_HEIGHT - 55 - ((i - 1) * CELL_HEIGHT)
96             x = j * CELL_WIDTH + 50
97
98             buf[1] = 0x00
99
100             buf[0] = (i - 1) * 16 + (j - 1)
101             if (buf[0] >= 32):
102                 d  = x - HPDF_Page_TextWidth (page, buf) / 2
103                 HPDF_Page_TextOut (page, d, y, buf)
104
105
106     HPDF_Page_EndText (page)
107
108
109
110 def main ():
111
112     encodings=[
113             "StandardEncoding",
114             "MacRomanEncoding",
115             "WinAnsiEncoding",
116             "ISO8859-2",
117             "ISO8859-3",
118             "ISO8859-4",
119             "ISO8859-5",
120             "ISO8859-9",
121             "ISO8859-10",
122             "ISO8859-13",
123             "ISO8859-14",
124             "ISO8859-15",
125             "ISO8859-16",
126             "CP1250",
127             "CP1251",
128             "CP1252",
129             "CP1254",
130             "CP1257",
131             "KOI8-R",
132             "Symbol-Set",
133             "ZapfDingbats-Set",
134             NULL
135     ]
136
137     pdf = HPDF_NewEx (error_handler, NULL, NULL, 0, NULL)
138     if (not pdf):
139         printf ("error: cannot create PdfDoc object\n")
140         return 1
141
142     fname=os.path.realpath(sys.argv[0])
143     fname=fname[:fname.rfind('.')]+'.pdf'
144
145     # set compression mode
146     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL)
147
148     # Set page mode to use outlines.
149     HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE)
150
151     # get default font
152     font = HPDF_GetFont (pdf, "Helvetica", NULL)
153
154     # load font object
155     font_name = HPDF_LoadType1FontFromFile (pdf, "type1/a010013l.afm",
156             "type1/a010013l.pfb")
157
158     # create outline root.
159     root = HPDF_CreateOutline (pdf, NULL, "Encoding list", NULL)
160     HPDF_Outline_SetOpened (root, HPDF_TRUE)
161
162     i=0
163     while (encodings[i]):
164         page = HPDF_AddPage (pdf)
165
166         HPDF_Page_SetWidth (page, PAGE_WIDTH)
167         HPDF_Page_SetHeight (page, PAGE_HEIGHT)
168
169         outline = HPDF_CreateOutline (pdf, root, encodings[i], NULL)
170         dst = HPDF_Page_CreateDestination (page)
171         HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page), 1)
172         # HPDF_Destination_SetFitB(dst);
173         HPDF_Outline_SetDestination(outline, dst)
174
175         HPDF_Page_SetFontAndSize (page, font, 15)
176         draw_graph (page)
177
178         HPDF_Page_BeginText (page)
179         HPDF_Page_SetFontAndSize (page, font, 20)
180         HPDF_Page_MoveTextPos (page, 40, PAGE_HEIGHT - 50)
181         HPDF_Page_ShowText (page, encodings[i])
182         HPDF_Page_ShowText (page, " Encoding")
183         HPDF_Page_EndText (page)
184
185         if encodings[i]=="Symbol-Set":
186             font2 = HPDF_GetFont (pdf, "Symbol", NULL)
187         elif encodings[i]=="ZapfDingbats-Set":
188             font2 = HPDF_GetFont (pdf, "ZapfDingbats", NULL)
189         else:
190             font2 = HPDF_GetFont (pdf, font_name, encodings[i])
191
192         HPDF_Page_SetFontAndSize (page, font2, 14)
193         draw_fonts (page)
194
195         i+=1
196
197
198     # save the document to a file
199     HPDF_SaveToFile (pdf, fname)
200
201     # clean up
202     HPDF_Free (pdf)
203
204     return 0
205
206 main()