1###
2## * << Haru Free PDF Library 2.0.0 >> -- ttfont_demo_jp.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 *
33
34
35@HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
36def 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
44def main ():
45    global  pdf
46
47    if (len(sys.argv) < 2):
48        printf ("ttfont_jp_demo <ttf-font-filename> [-E]\n")
49        printf ("ttfont_jp_demo <ttc-font-filename> <index> [-E]\n")
50        return 1
51
52    try:
53        f = open ("mbtext/sjis.txt", "rb")
54    except:
55        printf ("error: cannot open 'mbtext/sjis.txt'\n")
56        return 1
57
58
59    SAMP_TXT=f.read(2048)
60    f.close()
61
62    fname=os.path.realpath(sys.argv[0])
63    fname=fname[:fname.rfind('.')]+'.pdf'
64
65    pdf = HPDF_New (error_handler, NULL)
66    if (not pdf):
67        printf ("error: cannot create PdfDoc object\n")
68        return 1
69
70
71    # declaration for using Japanese encoding.
72    HPDF_UseJPEncodings (pdf)
73
74    HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL)
75
76    # load ttc file
77    if len(sys.argv) == 4 and sys.argv[3]=="-E":
78        detail_font_name = HPDF_LoadTTFontFromFile2 (pdf, sys.argv[1],
79                int (sys.argv[2]), HPDF_TRUE)
80    elif len(sys.argv) == 3 and sys.argv[2]=="-E":
81        detail_font_name = HPDF_LoadTTFontFromFile (pdf, sys.argv[1], HPDF_TRUE)
82    elif len(sys.argv) == 3:
83        detail_font_name = HPDF_LoadTTFontFromFile2 (pdf, sys.argv[1],
84                int (sys.argv[2]), HPDF_FALSE)
85    else:
86        detail_font_name = HPDF_LoadTTFontFromFile (pdf, sys.argv[1], HPDF_FALSE)
87
88    # add a new page object.
89    page = HPDF_AddPage (pdf)
90
91    title_font = HPDF_GetFont (pdf, "Helvetica", NULL)
92
93    detail_font = HPDF_GetFont (pdf, detail_font_name, "90msp-RKSJ-H")
94
95    HPDF_Page_SetFontAndSize (page, title_font, 10)
96
97    HPDF_Page_BeginText (page)
98
99    # move the position of the text to top of the page.
100    HPDF_Page_MoveTextPos(page, 10, 190)
101    HPDF_Page_ShowText (page, detail_font_name)
102    HPDF_Page_ShowText (page, " (")
103    HPDF_Page_ShowText (page, HPDF_Font_GetEncodingName (detail_font))
104    HPDF_Page_ShowText (page, ")")
105
106    HPDF_Page_SetFontAndSize (page, detail_font, 15)
107    HPDF_Page_MoveTextPos (page, 10, -20)
108    HPDF_Page_ShowText (page, "abcdefghijklmnopqrstuvwxyz")
109    HPDF_Page_MoveTextPos (page, 0, -20)
110    HPDF_Page_ShowText (page, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
111    HPDF_Page_MoveTextPos (page, 0, -20)
112    HPDF_Page_ShowText (page, "1234567890")
113    HPDF_Page_MoveTextPos (page, 0, -20)
114
115    HPDF_Page_SetFontAndSize (page, detail_font, 10)
116    HPDF_Page_ShowText (page, SAMP_TXT)
117    HPDF_Page_MoveTextPos (page, 0, -18)
118
119    HPDF_Page_SetFontAndSize (page, detail_font, 16)
120    HPDF_Page_ShowText (page, SAMP_TXT)
121    HPDF_Page_MoveTextPos (page, 0, -27)
122
123    HPDF_Page_SetFontAndSize (page, detail_font, 23)
124    HPDF_Page_ShowText (page, SAMP_TXT)
125    HPDF_Page_MoveTextPos (page, 0, -36)
126
127    HPDF_Page_SetFontAndSize (page, detail_font, 30)
128    HPDF_Page_ShowText (page, SAMP_TXT)
129    HPDF_Page_MoveTextPos (page, 0, -36)
130
131    pw = HPDF_Page_TextWidth (page, SAMP_TXT)
132    page_height = 210
133    page_width = pw + 40
134
135    HPDF_Page_SetWidth (page, page_width)
136    HPDF_Page_SetHeight (page, page_height)
137
138    # finish to print text.
139    HPDF_Page_EndText (page)
140
141    HPDF_Page_SetLineWidth (page, 0.5)
142
143    HPDF_Page_MoveTo (page, 10, page_height - 25)
144    HPDF_Page_LineTo (page, page_width - 10, page_height - 25)
145    HPDF_Page_Stroke (page)
146
147    HPDF_Page_MoveTo (page, 10, page_height - 85)
148    HPDF_Page_LineTo (page, page_width - 10, page_height - 85)
149    HPDF_Page_Stroke (page)
150
151    HPDF_SaveToFile (pdf, fname)
152
153    # clean up
154    HPDF_Free (pdf)
155
156    return 0
157
158main()