OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / cnfont_demo.py
1 ###
2 ## * << Haru Free PDF Library 2.0.0 >> -- jpfont_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
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 def main():
44     global pdf
45
46     chinesefonts='''
47 SimSun
48 SimSun,Bold
49 SimSun,Italic
50 SimSun,BoldItalic
51 SimHei
52 SimHei,Bold
53 SimHei,Italic
54 SimHei,BoldItalic
55 '''
56     chinesefonts=chinesefonts.split('\n')
57     chinesefonts=[i for i in chinesefonts if i]
58
59     detail_font=[]
60     PAGE_HEIGHT = 210
61
62     try:
63         f = open ("mbtext/cp936.txt", "rb")
64     except:
65         printf ("error: cannot open 'mbtext/sjis.txt'\n")
66         return 1
67
68     samp_text=f.read(2048)
69     f.close ()
70
71     fname=os.path.realpath(sys.argv[0])
72     fname=fname[:fname.rfind('.')]+'.pdf'
73
74     pdf = HPDF_New (error_handler, NULL)
75     if (not pdf):
76         printf ("error: cannot create PdfDoc object\n")
77         return 1
78
79
80     # configure pdf-document to be compressed.
81     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL)
82
83     # declaration for using Japanese font, encoding.
84     HPDF_UseCNSEncodings (pdf)
85     HPDF_UseCNSFonts (pdf)
86
87
88     for i in chinesefonts:
89         detail_font.append( HPDF_GetFont (pdf, i, "GB-EUC-H"))
90
91     # Set page mode to use outlines.
92     HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE)
93
94     # create outline root.
95     root = HPDF_CreateOutline (pdf, NULL, "JP font demo", NULL)
96     HPDF_Outline_SetOpened (root, HPDF_TRUE)
97
98     for i in detail_font:
99
100         # add a new page object.
101         page = HPDF_AddPage (pdf)
102
103         # create outline entry
104         outline = HPDF_CreateOutline (pdf, root,
105                 HPDF_Font_GetFontName (i), NULL)
106         dst = HPDF_Page_CreateDestination (page)
107         HPDF_Outline_SetDestination(outline, dst)
108
109         title_font = HPDF_GetFont (pdf, "Helvetica", NULL)
110         HPDF_Page_SetFontAndSize (page, title_font, 10)
111
112         HPDF_Page_BeginText (page)
113
114         # move the position of the text to top of the page.
115         HPDF_Page_MoveTextPos(page, 10, 190)
116         HPDF_Page_ShowText (page, HPDF_Font_GetFontName (i))
117
118         HPDF_Page_SetFontAndSize (page, i, 15)
119         HPDF_Page_MoveTextPos (page, 10, -20)
120         HPDF_Page_ShowText (page, "abcdefghijklmnopqrstuvwxyz")
121         HPDF_Page_MoveTextPos (page, 0, -20)
122         HPDF_Page_ShowText (page, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
123         HPDF_Page_MoveTextPos (page, 0, -20)
124         HPDF_Page_ShowText (page, "1234567890")
125         HPDF_Page_MoveTextPos (page, 0, -20)
126
127         HPDF_Page_SetFontAndSize (page, i, 10)
128         HPDF_Page_ShowText (page, samp_text)
129         HPDF_Page_MoveTextPos (page, 0, -18)
130
131         HPDF_Page_SetFontAndSize (page, i, 16)
132         HPDF_Page_ShowText (page, samp_text)
133         HPDF_Page_MoveTextPos (page, 0, -27)
134
135         HPDF_Page_SetFontAndSize (page, i, 23)
136         HPDF_Page_ShowText (page, samp_text)
137         HPDF_Page_MoveTextPos (page, 0, -36)
138
139         HPDF_Page_SetFontAndSize (page, i, 30)
140         HPDF_Page_ShowText (page, samp_text)
141
142         p = HPDF_Page_GetCurrentTextPos (page)
143
144         # finish to print text.
145         HPDF_Page_EndText (page)
146
147         HPDF_Page_SetLineWidth (page, 0.5)
148
149         x_pos = 20
150         for j in range(len (samp_text) // 2):
151             HPDF_Page_MoveTo (page, x_pos, p.y - 10)
152             HPDF_Page_LineTo (page, x_pos, p.y - 12)
153             HPDF_Page_Stroke (page)
154             x_pos = x_pos + 30
155
156         HPDF_Page_SetWidth (page, p.x + 20)
157         HPDF_Page_SetHeight (page, PAGE_HEIGHT)
158
159         HPDF_Page_MoveTo (page, 10, PAGE_HEIGHT - 25)
160         HPDF_Page_LineTo (page, p.x + 10, PAGE_HEIGHT - 25)
161         HPDF_Page_Stroke (page)
162
163         HPDF_Page_MoveTo (page, 10, PAGE_HEIGHT - 85)
164         HPDF_Page_LineTo (page, p.x + 10, PAGE_HEIGHT - 85)
165         HPDF_Page_Stroke (page)
166
167         HPDF_Page_MoveTo (page, 10, p.y - 12)
168         HPDF_Page_LineTo (page, p.x + 10, p.y - 12)
169         HPDF_Page_Stroke (page)
170
171     HPDF_SaveToFile (pdf, fname)
172
173     # clean up
174     HPDF_Free (pdf)
175
176     return 0
177
178 main()