OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / text_demo2.py
1 ###
2 ## * << Alternative PDF Library 1.0.0 >> -- text_demo2.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
33 from grid_sheet import *
34
35 from math import *
36
37
38 global pdf
39
40 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
41 def error_handler  (error_no, detail_no, user_data):
42     printf ("ERROR: error_no=%04X, detail_no=%u\n", error_no,
43                 detail_no)
44     HPDF_Free (pdf)
45
46
47 global no
48 no = 0
49
50 def PrintText(page):
51     #char buf[512]
52     pos = HPDF_Page_GetCurrentTextPos (page)
53
54     no+=1
55     buf=".[%d]%0.2f %0.2f" %( no, pos.x, pos.y)
56     HPDF_Page_ShowText(page, buf)
57
58
59 def main ():
60     global pdf
61
62     rect=HPDF_Rect()
63
64     SAMP_TXT = "The quick brown fox jumps over the lazy dog. "
65
66     fname=os.path.realpath(sys.argv[0])
67     fname=fname[:fname.rfind('.')]+'.pdf'
68
69     pdf = HPDF_New (error_handler, NULL)
70     if (not pdf):
71         printf ("error: cannot create PdfDoc object\n")
72         return 1
73
74     # add a new page object.
75     page = HPDF_AddPage (pdf)
76     HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_A5, HPDF_PAGE_PORTRAIT)
77
78     print_grid  (pdf, page)
79
80     page_height = HPDF_Page_GetHeight (page)
81
82     font = HPDF_GetFont (pdf, "Helvetica", NULL)
83     HPDF_Page_SetTextLeading (page, 20)
84
85     # text_rect method
86
87     # HPDF_TALIGN_LEFT
88     rect.left = 25
89     rect.top = 545
90     rect.right = 200
91     rect.bottom = rect.top - 40
92
93     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
94                 rect.top - rect.bottom)
95     HPDF_Page_Stroke (page)
96
97     HPDF_Page_BeginText (page)
98
99     HPDF_Page_SetFontAndSize (page, font, 10)
100     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_LEFT")
101
102     HPDF_Page_SetFontAndSize (page, font, 13)
103     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
104                 SAMP_TXT, HPDF_TALIGN_LEFT, NULL)
105
106     HPDF_Page_EndText (page)
107
108     # HPDF_TALIGN_RIGTH
109     rect.left = 220
110     rect.right = 395
111
112     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
113                 rect.top - rect.bottom)
114     HPDF_Page_Stroke (page)
115
116     HPDF_Page_BeginText (page)
117
118     HPDF_Page_SetFontAndSize (page, font, 10)
119     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_RIGTH")
120
121     HPDF_Page_SetFontAndSize (page, font, 13)
122     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
123                 SAMP_TXT, HPDF_TALIGN_RIGHT, NULL)
124
125     HPDF_Page_EndText (page)
126
127     # HPDF_TALIGN_CENTER
128     rect.left = 25
129     rect.top = 475
130     rect.right = 200
131     rect.bottom = rect.top - 40
132
133     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
134                 rect.top - rect.bottom)
135     HPDF_Page_Stroke (page)
136
137     HPDF_Page_BeginText (page)
138
139     HPDF_Page_SetFontAndSize (page, font, 10)
140     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_CENTER")
141
142     HPDF_Page_SetFontAndSize (page, font, 13)
143     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
144                 SAMP_TXT, HPDF_TALIGN_CENTER, NULL)
145
146     HPDF_Page_EndText (page)
147
148     # HPDF_TALIGN_JUSTIFY
149     rect.left = 220
150     rect.right = 395
151
152     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
153                 rect.top - rect.bottom)
154     HPDF_Page_Stroke (page)
155
156     HPDF_Page_BeginText (page)
157
158     HPDF_Page_SetFontAndSize (page, font, 10)
159     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_JUSTIFY")
160
161     HPDF_Page_SetFontAndSize (page, font, 13)
162     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
163                 SAMP_TXT, HPDF_TALIGN_JUSTIFY, NULL)
164
165     HPDF_Page_EndText (page)
166
167
168
169     # Skewed coordinate system
170     HPDF_Page_GSave (page)
171
172     angle1 = 5
173     angle2 = 10
174     rad1 = angle1 / 180 * 3.141592
175     rad2 = angle2 / 180 * 3.141592
176
177     HPDF_Page_Concat (page, 1, tan(rad1), tan(rad2), 1, 25, 350)
178     rect.left = 0
179     rect.top = 40
180     rect.right = 175
181     rect.bottom = 0
182
183     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
184                 rect.top - rect.bottom)
185     HPDF_Page_Stroke (page)
186
187     HPDF_Page_BeginText (page)
188
189     HPDF_Page_SetFontAndSize (page, font, 10)
190     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "Skewed coordinate system")
191
192     HPDF_Page_SetFontAndSize (page, font, 13)
193     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
194                 SAMP_TXT, HPDF_TALIGN_LEFT, NULL)
195
196     HPDF_Page_EndText (page)
197
198     HPDF_Page_GRestore (page)
199
200
201     # Rotated coordinate system
202     HPDF_Page_GSave (page)
203
204     angle1 = 5
205     rad1 = angle1 / 180 * 3.141592
206
207     HPDF_Page_Concat (page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), 220, 350)
208     rect.left = 0
209     rect.top = 40
210     rect.right = 175
211     rect.bottom = 0
212
213     HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
214                 rect.top - rect.bottom)
215     HPDF_Page_Stroke (page)
216
217     HPDF_Page_BeginText (page)
218
219     HPDF_Page_SetFontAndSize (page, font, 10)
220     HPDF_Page_TextOut (page, rect.left, rect.top + 3, "Rotated coordinate system")
221
222     HPDF_Page_SetFontAndSize (page, font, 13)
223     HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
224                 SAMP_TXT, HPDF_TALIGN_LEFT, NULL)
225
226     HPDF_Page_EndText (page)
227
228     HPDF_Page_GRestore (page)
229
230
231     # text along a circle
232     HPDF_Page_SetGrayStroke (page, 0)
233     HPDF_Page_Circle (page, 210, 190, 145)
234     HPDF_Page_Circle (page, 210, 190, 113)
235     HPDF_Page_Stroke (page)
236
237     angle1 = 360.0 / (len (SAMP_TXT))
238     angle2 = 180
239
240     HPDF_Page_BeginText (page)
241     font = HPDF_GetFont (pdf, "Courier-Bold", NULL)
242     HPDF_Page_SetFontAndSize (page, font, 30)
243
244     for i in range(len (SAMP_TXT)):
245         buf=[None, None]
246
247         rad1 = (angle2 - 90) / 180 * 3.141592
248         rad2 = angle2 / 180 * 3.141592
249
250         x = 210 + cos(rad2) * 122
251         y = 190 + sin(rad2) * 122
252
253         HPDF_Page_SetTextMatrix(page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), x, y)
254
255         buf[0] = SAMP_TXT[i]
256         buf[1] = chr(0)
257         HPDF_Page_ShowText (page, buf)
258         angle2 -= angle1
259
260     HPDF_Page_EndText (page)
261
262     # save the document to a file
263     HPDF_SaveToFile (pdf, fname)
264
265     # clean up
266     HPDF_Free (pdf)
267
268     return 0
269
270
271 main()