Line 0
Link Here
|
|
|
1 |
--- gbpdf.orig Fri Jul 5 01:38:22 2002 |
2 |
+++ gbpdf Fri Jul 5 01:39:10 2002 |
3 |
@@ -0,0 +1,299 @@ |
4 |
+#!/usr/bin/env python |
5 |
+ |
6 |
+""" |
7 |
+Bg5pdf is a simple wrapper for wrapping big5 encoding text file into |
8 |
+PDF file by using PDFlib. The output of this program does not contain |
9 |
+embedding fonts. You have to download Acrobat Acroread Asianfont pack |
10 |
+to view and print the output file. This wrapper does not provide any |
11 |
+formatting function except simple line wrapping. If you need |
12 |
+sophisticated formatting, you should try CJK-LaTex or other equivalent |
13 |
+tools. |
14 |
+ |
15 |
+Information about PDFlib can be found at http://www.pdflib.com. |
16 |
+You might need it if you need to compile bg5pdflib module which |
17 |
+is used by gbpdf. |
18 |
+ |
19 |
+You can download Acrobat Acroread Asianfont Pack at |
20 |
+http://www.adobe.com/products/acrobat/acrrasianfontpack.html |
21 |
+ |
22 |
+============================================================================ |
23 |
+ |
24 |
+Copyright (C) 2001 by Chen-Shan Chin |
25 |
+gbpdf ver. 1.0 Date:Jun 3 2001 |
26 |
+ |
27 |
+ This program is free software; you can redistribute it and/or modify |
28 |
+ it under the terms of the GNU General Public License as published by |
29 |
+ the Free Software Foundation; either version 2 of the License, or any |
30 |
+ later version. This program is distributed in the hope that it will |
31 |
+ be useful, but WITHOUT ANY WARRANTY; without even implied warranty of |
32 |
+ MERCHANTABILITY of FITTNESS |
33 |
+ |
34 |
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
35 |
+ details. |
36 |
+ |
37 |
+ You should have received a copy of the GNU General Public License |
38 |
+ along with this program; if not, write to the Free Sofeware |
39 |
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA |
40 |
+ |
41 |
+""" |
42 |
+ |
43 |
+import getopt |
44 |
+from string import * |
45 |
+from sys import * |
46 |
+from bg5pdflib import * |
47 |
+#If you want to use python binding (pdflib_py.so) coming with PDFlib, |
48 |
+#remark the above line and unmark the next line. |
49 |
+#from pdflib_py import * |
50 |
+ |
51 |
+ |
52 |
+False = 0 |
53 |
+True = 1 |
54 |
+ |
55 |
+ASCII = 0 |
56 |
+GB2312 = 1 |
57 |
+ |
58 |
+ |
59 |
+def renderer(doc, fn): |
60 |
+ p = PDF_new() |
61 |
+ if PDF_open_file(p, fn) == -1: |
62 |
+ print "Couldn't open PDF file '%s'\n" % fn |
63 |
+ exit(2) |
64 |
+ PDF_set_info(p, "Author", "gbpdf") |
65 |
+ PDF_set_info(p, "Creator", "gbpdf") |
66 |
+ PDF_set_info(p, "Title", "%s" % fn) |
67 |
+ cfname = doc.fontName |
68 |
+ cEncoding = doc.encoding |
69 |
+ fontSize = doc.fontSize |
70 |
+ topM = 792.0 / 11.0 |
71 |
+ leftM = 792.0 / 11.0 |
72 |
+ |
73 |
+ PDF_begin_page(p, 612, 792) |
74 |
+ font = PDF_findfont(p, cfname, cEncoding, 0) |
75 |
+ PDF_setfont(p, font, fontSize) |
76 |
+ mode = GB2312 |
77 |
+ y = 0 |
78 |
+ pageN = 1 |
79 |
+ for l in doc.parsedLines: |
80 |
+ if len(l) == 0: |
81 |
+ y = y + fontSize |
82 |
+ continue |
83 |
+ x = 0.0 |
84 |
+ PDF_set_text_pos(p, leftM + x, 792 - topM -y) |
85 |
+ curStr = "" |
86 |
+ for pos in range(len(l)): |
87 |
+ ch = l[pos] |
88 |
+ if ch[0] != mode: |
89 |
+ if len(curStr) != 0: |
90 |
+ if mode == GB2312: |
91 |
+ font = PDF_findfont(p, cfname, cEncoding, 0) |
92 |
+ PDF_setfont(p, font, fontSize) |
93 |
+ PDF_show(p, curStr) |
94 |
+ else: |
95 |
+ font = PDF_findfont(p, "Courier", "host", 0) |
96 |
+ PDF_setfont(p, font, fontSize) |
97 |
+ PDF_show(p, curStr) |
98 |
+ mode = ch[0] |
99 |
+ curStr = ch[1] |
100 |
+ else: |
101 |
+ curStr = curStr + ch[1] |
102 |
+ |
103 |
+ if len(curStr) != 0: |
104 |
+ if mode == 1: |
105 |
+ font = PDF_findfont(p, cfname, cEncoding, 0) |
106 |
+ PDF_setfont(p, font, fontSize) |
107 |
+ PDF_show(p, curStr) |
108 |
+ else: |
109 |
+ font = PDF_findfont(p, "Courier", "host", 0) |
110 |
+ PDF_setfont(p, font, fontSize) |
111 |
+ PDF_show(p, curStr) |
112 |
+ |
113 |
+ y = y + fontSize + doc.lineSpacing |
114 |
+ if y > 792 - 2* topM: |
115 |
+ font = PDF_findfont(p, "Helvetica-Oblique", "host", 0) |
116 |
+ PDF_setfont(p, font, 12) |
117 |
+ PDF_show_xy(p,"Converted to PDF by gbpdf", 400, 750) |
118 |
+ PDF_show_xy(p,"file name: %s--Page %d" % (doc.infileName, pageN), 60,750) |
119 |
+ PDF_setlinewidth(p,2) |
120 |
+ PDF_moveto(p, 50, 740) |
121 |
+ PDF_lineto(p, 562, 740) |
122 |
+ PDF_stroke(p) |
123 |
+ PDF_end_page(p) |
124 |
+ pageN = pageN + 1 |
125 |
+ PDF_begin_page(p, 612, 792) |
126 |
+ font = PDF_findfont(p, cfname, cEncoding, 0) |
127 |
+ PDF_setfont(p, font, fontSize) |
128 |
+ mode = GB2312 |
129 |
+ y = 0 |
130 |
+ font = PDF_findfont(p, "Helvetica-Oblique", "host", 0) |
131 |
+ PDF_setfont(p, font, 12) |
132 |
+ PDF_show_xy(p,"Converted to PDF by gbpdf", 400, 750); |
133 |
+ PDF_show_xy(p,"file name: %s--Page %d" % (doc.infileName, pageN), 60,750) |
134 |
+ PDF_setlinewidth(p,2) |
135 |
+ PDF_moveto(p, 50, 740) |
136 |
+ PDF_lineto(p, 562, 740) |
137 |
+ PDF_stroke(p) |
138 |
+ PDF_end_page(p) |
139 |
+ PDF_close(p) |
140 |
+ PDF_delete(p) |
141 |
+ |
142 |
+class document: |
143 |
+ infileName = "" |
144 |
+ fontSize = 12; |
145 |
+ fontNames = ["STSong-Light"] |
146 |
+ fontName = fontNames[0] |
147 |
+ encodings = ["GBK-EUC-H"] |
148 |
+ encoding = encodings[0] |
149 |
+ wrapped = False |
150 |
+ numOfChr = 55 #number of character per line for wrapping text |
151 |
+ parsedLines=[] |
152 |
+ lineSpacing = 2 |
153 |
+ |
154 |
+ def getChr(self, st, i): |
155 |
+ if i + 1 < len(st) and\ |
156 |
+ (ord(st[i]) >= 161 and ord(st[i]) <= 249) and \ |
157 |
+ ((ord(st[i+1]) >=64 and ord(st[i+1]) <=126) or \ |
158 |
+ (ord(st[i+1]) >=161 and ord(st[i+1]) <=254)) : # bg5_c |
159 |
+ cnchr = st[i:i+2] |
160 |
+ return (cnchr, 2) |
161 |
+ else: |
162 |
+ cnchr = st[i] |
163 |
+ return (cnchr, 1) |
164 |
+ |
165 |
+ def setFontSize(self, fs): |
166 |
+ self.fontSize = fs |
167 |
+ |
168 |
+ def setFont(self, fn): |
169 |
+ if not fn in self.fontNames: |
170 |
+ raise FontError |
171 |
+ else: |
172 |
+ self.fontName = fn |
173 |
+ |
174 |
+ def setWrapped(self, yn, nc): |
175 |
+ if yn > 0: |
176 |
+ self.wrapped = True |
177 |
+ self.numOfChr = nc |
178 |
+ |
179 |
+ def setLineSpacing(self, ls): |
180 |
+ self.lineSpacing = ls |
181 |
+ |
182 |
+ def setFontSize(self, fs): |
183 |
+ self.fontSize = fs |
184 |
+ |
185 |
+ def parseFile(self, filename): |
186 |
+ self.infileName = filename |
187 |
+ try: |
188 |
+ f = open(filename,"r"); |
189 |
+ except IOError: |
190 |
+ print 'can not open file: "%s"' % filename |
191 |
+ exit(2) |
192 |
+ for line in f.readlines(): |
193 |
+ line = rstrip(line) |
194 |
+ #if line[-1] == '\n': |
195 |
+ # line = line[:-1] |
196 |
+ if len(line) == 0: |
197 |
+ self.parsedLines.append([]) |
198 |
+ continue |
199 |
+ pline = [] |
200 |
+ pos = 0 |
201 |
+ cpos = 0 |
202 |
+ line_len = len(line) |
203 |
+ while 1: |
204 |
+ (chrx, inc) = self.getChr(line, pos) |
205 |
+ pos = pos + inc |
206 |
+ cpos = cpos + inc |
207 |
+ if inc == 2: |
208 |
+ pline.append([GB2312, chrx]) |
209 |
+ else: |
210 |
+ pline.append([ASCII, chrx]) |
211 |
+ if pos >= line_len: |
212 |
+ break |
213 |
+ if (self.wrapped == True) and ((cpos + 2) >= self.numOfChr): |
214 |
+ cpos = 0 |
215 |
+ self.parsedLines.append(pline) |
216 |
+ pline = [] |
217 |
+ self.parsedLines.append(pline) |
218 |
+ |
219 |
+def usage(): |
220 |
+ print "usage: gbpdf [option] inputfile outfile" |
221 |
+ print "d: integer, s:string" |
222 |
+ print "-w d, --wrap=d: wrapping line up to d ASCII characters (Each GB2312 character" |
223 |
+ print " occupis two ASCII character spaces.)" |
224 |
+ print "-s d, --size=d: set the size of font (default: 11)" |
225 |
+ print "-f s, --font=s: set font (default: 'Msung-Light')" |
226 |
+ print "-l d, --linespacing=d: set spacing between line (default:2)" |
227 |
+ print "--inputfile=s: input file name" |
228 |
+ print "--outputfile=s: outfile name (default: inputfile.pdf)" |
229 |
+ print "--showfonts: show avaiable fonts" |
230 |
+ |
231 |
+getopt.GetoptError = getopt.error |
232 |
+ |
233 |
+def main(): |
234 |
+ try: |
235 |
+ opts, args =\ |
236 |
+ getopt.getopt(argv[1:], |
237 |
+ "w:s:f:l:", |
238 |
+ ["size=", |
239 |
+ "font=", |
240 |
+ "linespacing=", |
241 |
+ "inputfile=", |
242 |
+ "outputfile=", |
243 |
+ "wrap=", |
244 |
+ "showfonts"]) |
245 |
+ except getopt.error: |
246 |
+ usage() |
247 |
+ exit(2) |
248 |
+ |
249 |
+ #print opts, args |
250 |
+ |
251 |
+ doc = document() |
252 |
+ doc.setFont(doc.fontNames[0]) |
253 |
+ doc.setWrapped(False, 0) |
254 |
+ doc.setLineSpacing(2) |
255 |
+ doc.setFontSize(11) |
256 |
+ |
257 |
+ infile = "" |
258 |
+ outfile = "" |
259 |
+ |
260 |
+ for opt in opts: |
261 |
+ if opt[0] == "--size" or opt[0] == "-s": |
262 |
+ doc.setFontSize(atof(opt[1])) |
263 |
+ continue |
264 |
+ if opt[0] == "--font" or opt[0] == "-f": |
265 |
+ doc.setFont(opt[1]) |
266 |
+ continue |
267 |
+ if opt[0] == "--wrap" or opt[0] == "-w": |
268 |
+ doc.setWrapped(True, atoi(opt[1])) |
269 |
+ continue |
270 |
+ if opt[0] == "--linespacing" or opt[0] == "-l": |
271 |
+ doc.setLineSpacing(atof(opt[1])) |
272 |
+ if opt[0] == "--inputfile": |
273 |
+ infile = opt[1] |
274 |
+ continue |
275 |
+ if opt[0] == "--outfile": |
276 |
+ outfile = opt[1] |
277 |
+ continue |
278 |
+ if opt[0] == "--showfonts": |
279 |
+ print "Avaiable Fonts:" |
280 |
+ for f in doc.fontNames: |
281 |
+ print f |
282 |
+ exit(2) |
283 |
+ |
284 |
+ if len(args) == 2: |
285 |
+ infile = args[0] |
286 |
+ outfile = args[1] |
287 |
+ elif len(args) == 1: |
288 |
+ infile = args[0] |
289 |
+ |
290 |
+ if infile == "": |
291 |
+ print "no input file name" |
292 |
+ exit(2) |
293 |
+ |
294 |
+ if outfile == "": |
295 |
+ outfile = infile+".pdf" |
296 |
+ |
297 |
+ doc.parseFile(infile) |
298 |
+ renderer(doc,outfile) |
299 |
+ print "Output File is %s" % outfile |
300 |
+ |
301 |
+if __name__ == "__main__": |
302 |
+ main() |