2 ## * << Haru Free PDF Library 2.0.0 >> -- jpfont_demo.c
4 ## * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
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.
15 ## port to python by Li Jun
16 ## http://groups.google.com/group/pythoncia
24 path=os.path.normpath(os.path.split(os.path.realpath(__file__))[0]+'\..'*up)
25 if path not in sys.path:
31 from haru.c_func import *
32 from haru.hpdf_errorcode import *
35 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
36 def error_handler (error_no, detail_no, user_data):
38 printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
56 chinesefonts=chinesefonts.split('\n')
57 chinesefonts=[i for i in chinesefonts if i]
63 f = open ("mbtext/cp936.txt", "rb")
65 printf ("error: cannot open 'mbtext/sjis.txt'\n")
68 samp_text=f.read(2048)
71 fname=os.path.realpath(sys.argv[0])
72 fname=fname[:fname.rfind('.')]+'.pdf'
74 pdf = HPDF_New (error_handler, NULL)
76 printf ("error: cannot create PdfDoc object\n")
80 # configure pdf-document to be compressed.
81 HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL)
83 # declaration for using Japanese font, encoding.
84 HPDF_UseCNSEncodings (pdf)
85 HPDF_UseCNSFonts (pdf)
88 for i in chinesefonts:
89 detail_font.append( HPDF_GetFont (pdf, i, "GB-EUC-H"))
91 # Set page mode to use outlines.
92 HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE)
94 # create outline root.
95 root = HPDF_CreateOutline (pdf, NULL, "JP font demo", NULL)
96 HPDF_Outline_SetOpened (root, HPDF_TRUE)
100 # add a new page object.
101 page = HPDF_AddPage (pdf)
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)
109 title_font = HPDF_GetFont (pdf, "Helvetica", NULL)
110 HPDF_Page_SetFontAndSize (page, title_font, 10)
112 HPDF_Page_BeginText (page)
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))
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)
127 HPDF_Page_SetFontAndSize (page, i, 10)
128 HPDF_Page_ShowText (page, samp_text)
129 HPDF_Page_MoveTextPos (page, 0, -18)
131 HPDF_Page_SetFontAndSize (page, i, 16)
132 HPDF_Page_ShowText (page, samp_text)
133 HPDF_Page_MoveTextPos (page, 0, -27)
135 HPDF_Page_SetFontAndSize (page, i, 23)
136 HPDF_Page_ShowText (page, samp_text)
137 HPDF_Page_MoveTextPos (page, 0, -36)
139 HPDF_Page_SetFontAndSize (page, i, 30)
140 HPDF_Page_ShowText (page, samp_text)
142 p = HPDF_Page_GetCurrentTextPos (page)
144 # finish to print text.
145 HPDF_Page_EndText (page)
147 HPDF_Page_SetLineWidth (page, 0.5)
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)
156 HPDF_Page_SetWidth (page, p.x + 20)
157 HPDF_Page_SetHeight (page, PAGE_HEIGHT)
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)
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)
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)
171 HPDF_SaveToFile (pdf, fname)