1###
2## * << Haru Free PDF Library 2.0.0 >> -- arc_demo.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
18import os, sys
19
20from ctypes import *
21up=2
22def 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
28setlibpath(up)
29
30from haru import *
31from haru.c_func import *
32from haru.hpdf_errorcode import *
33from grid_sheet import *
34
35
36@HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
37def error_handler (error_no, detail_no, user_data):
38    global pdf
39    printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
40                detail_no)
41    HPDF_Free (pdf)
42    sys.exit(1)
43
44
45def main ():
46    global pdf
47
48    fname=os.path.realpath(sys.argv[0])
49    fname=fname[:fname.rfind('.')]+'.pdf'
50
51    pdf = HPDF_New (error_handler, NULL)
52    if (not pdf):
53        printf ("error: cannot create PdfDoc object\n")
54        return 1
55
56    # add a new page object.
57    page = HPDF_AddPage (pdf)
58
59    HPDF_Page_SetHeight (page, 220)
60    HPDF_Page_SetWidth (page, 200)
61
62    # draw grid to the page
63    print_grid  (pdf, page)
64
65    # draw pie chart
66    # *
67    # *   A: 45% Red
68    # *   B: 25% Blue
69    # *   C: 15% green
70    # *   D: other yellow
71
72
73    # A
74    HPDF_Page_SetRGBFill (page, 1.0, 0, 0)
75    HPDF_Page_MoveTo (page, 100, 100)
76    HPDF_Page_LineTo (page, 100, 180)
77    HPDF_Page_Arc (page, 100, 100, 80, 0, 360 * 0.45)
78    pos = HPDF_Page_GetCurrentPos (page)
79    HPDF_Page_LineTo (page, 100, 100)
80    HPDF_Page_Fill (page)
81
82    # B
83    HPDF_Page_SetRGBFill (page, 0, 0, 1.0)
84    HPDF_Page_MoveTo (page, 100, 100)
85    HPDF_Page_LineTo (page, pos.x, pos.y)
86    HPDF_Page_Arc (page, 100, 100, 80, 360 * 0.45, 360 * 0.7)
87    pos = HPDF_Page_GetCurrentPos (page)
88    HPDF_Page_LineTo (page, 100, 100)
89    HPDF_Page_Fill (page)
90
91    # C
92    HPDF_Page_SetRGBFill (page, 0, 1.0, 0)
93    HPDF_Page_MoveTo (page, 100, 100)
94    HPDF_Page_LineTo (page, pos.x, pos.y)
95    HPDF_Page_Arc (page, 100, 100, 80, 360 * 0.7, 360 * 0.85)
96    pos = HPDF_Page_GetCurrentPos (page)
97    HPDF_Page_LineTo (page, 100, 100)
98    HPDF_Page_Fill (page)
99
100    # D
101    HPDF_Page_SetRGBFill (page, 1.0, 1.0, 0)
102    HPDF_Page_MoveTo (page, 100, 100)
103    HPDF_Page_LineTo (page, pos.x, pos.y)
104    HPDF_Page_Arc (page, 100, 100, 80, 360 * 0.85, 360)
105    pos = HPDF_Page_GetCurrentPos (page)
106    HPDF_Page_LineTo (page, 100, 100)
107    HPDF_Page_Fill (page)
108
109    # draw center circle
110    HPDF_Page_SetGrayStroke (page, 0)
111    HPDF_Page_SetGrayFill (page, 1)
112    HPDF_Page_Circle (page, 100, 100, 30)
113    HPDF_Page_Fill (page)
114
115    # save the document to a file
116    HPDF_SaveToFile (pdf, fname)
117
118    # clean up
119    HPDF_Free (pdf)
120
121    return 0
122
123main()