OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / grid_sheet.py
1 ###
2 ## * << Haru Free PDF Library 2.0.0 >> -- grid_sheet.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 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
35 def error_handler (error_no, detail_no, user_data):
36     global pdf
37     printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
38                 detail_no)
39     HPDF_Free (pdf)
40     sys.exit(1)
41
42
43 def print_grid  (pdf, page):
44     height = HPDF_Page_GetHeight (page)
45     width = HPDF_Page_GetWidth (page)
46     font = HPDF_GetFont (pdf, "Helvetica", NULL)
47
48     HPDF_Page_SetFontAndSize (page, font, 5)
49     HPDF_Page_SetGrayFill (page, 0.5)
50     HPDF_Page_SetGrayStroke (page, 0.8)
51
52     # Draw horizontal lines
53     y = 0
54     while (y < height):
55         if (y % 10 == 0):
56             HPDF_Page_SetLineWidth (page, 0.5)
57         else:
58             if (HPDF_Page_GetLineWidth (page) != 0.25):
59                 HPDF_Page_SetLineWidth (page, 0.25)
60
61
62         HPDF_Page_MoveTo (page, 0, y)
63         HPDF_Page_LineTo (page, width, y)
64         HPDF_Page_Stroke (page)
65
66         if (y % 10 == 0 and y > 0):
67             HPDF_Page_SetGrayStroke (page, 0.5)
68
69             HPDF_Page_MoveTo (page, 0, y)
70             HPDF_Page_LineTo (page, 5, y)
71             HPDF_Page_Stroke (page)
72
73             HPDF_Page_SetGrayStroke (page, 0.8)
74
75
76         y += 5
77
78
79
80     # Draw virtical lines
81     x = 0
82     while (x < width):
83         if (x % 10 == 0):
84             HPDF_Page_SetLineWidth (page, 0.5)
85         else:
86             if (HPDF_Page_GetLineWidth (page) != 0.25):
87                 HPDF_Page_SetLineWidth (page, 0.25)
88
89
90         HPDF_Page_MoveTo (page, x, 0)
91         HPDF_Page_LineTo (page, x, height)
92         HPDF_Page_Stroke (page)
93
94         if (x % 50 == 0 and x > 0):
95             HPDF_Page_SetGrayStroke (page, 0.5)
96
97             HPDF_Page_MoveTo (page, x, 0)
98             HPDF_Page_LineTo (page, x, 5)
99             HPDF_Page_Stroke (page)
100
101             HPDF_Page_MoveTo (page, x, height)
102             HPDF_Page_LineTo (page, x, height - 5)
103             HPDF_Page_Stroke (page)
104
105             HPDF_Page_SetGrayStroke (page, 0.8)
106
107
108         x += 5
109
110
111     # Draw horizontal text
112     y = 0
113     while (y < height):
114         if (y % 10 == 0 and y > 0):
115
116             HPDF_Page_BeginText (page)
117             HPDF_Page_MoveTextPos (page, 5, y - 2)
118
119             buf="%u" % y
120
121             HPDF_Page_ShowText (page, buf)
122             HPDF_Page_EndText (page)
123
124         y += 5
125
126
127     # Draw virtical text
128     x = 0
129     while (x < width):
130         if (x % 50 == 0 and x > 0):
131
132             HPDF_Page_BeginText (page)
133             HPDF_Page_MoveTextPos (page, x, 5)
134
135             buf="%u" % x
136
137             HPDF_Page_ShowText (page, buf)
138             HPDF_Page_EndText (page)
139
140             HPDF_Page_BeginText (page)
141             HPDF_Page_MoveTextPos (page, x, height - 10)
142             HPDF_Page_ShowText (page, buf)
143             HPDF_Page_EndText (page)
144
145         x += 5
146
147     HPDF_Page_SetGrayFill (page, 0)
148     HPDF_Page_SetGrayStroke (page, 0)
149
150
151 def main():
152     global pdf
153
154     fname=os.path.realpath(sys.argv[0])
155     fname=fname[:fname.rfind('.')]+'.pdf'
156
157     pdf = HPDF_New (error_handler, NULL)
158     if (not pdf):
159         printf ("error: cannot create PdfDoc object\n")
160         return 1
161
162     # add a new page object.
163     page = HPDF_AddPage (pdf)
164
165     HPDF_Page_SetHeight (page, 600)
166     HPDF_Page_SetWidth (page, 400)
167
168     print_grid  (pdf, page)
169
170
171     # save the document to a file
172     HPDF_SaveToFile (pdf, fname)
173
174     # clean up
175     HPDF_Free (pdf)
176
177     return 0
178
179 if __name__=='__main__':
180     main()
181
182
183 __all__=['print_grid']