1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # pkpgcounter : a generic Page Description Language parser |
---|
4 | # |
---|
5 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # |
---|
19 | # $Id$ |
---|
20 | # |
---|
21 | |
---|
22 | """This modules implements a page counter for Structured Fax documents.""" |
---|
23 | |
---|
24 | import struct |
---|
25 | |
---|
26 | import pdlparser |
---|
27 | |
---|
28 | class Parser(pdlparser.PDLParser) : |
---|
29 | """A parser for Structured Fax documents.""" |
---|
30 | format = "Structured Fax" |
---|
31 | def isValid(self) : |
---|
32 | """Returns True if data is Structured Fax, else False.""" |
---|
33 | if self.firstblock.startswith("Sfff") : |
---|
34 | return True |
---|
35 | else : |
---|
36 | return False |
---|
37 | |
---|
38 | def getJobSize(self) : |
---|
39 | """Counts pages in a Structured Fax document. |
---|
40 | |
---|
41 | Algorithm by Jerome Alet. |
---|
42 | |
---|
43 | The documentation used for this was : |
---|
44 | |
---|
45 | http://delphi.pjh2.de/articles/graphic/sff_format.php |
---|
46 | """ |
---|
47 | unpack = struct.unpack |
---|
48 | pagecount = 0 |
---|
49 | docheader = self.infile.read(20) |
---|
50 | try : |
---|
51 | (sffid, |
---|
52 | version, |
---|
53 | reserved, |
---|
54 | userinfo, |
---|
55 | docpagecount, |
---|
56 | offsetfirstpage, |
---|
57 | offsetlastpage, |
---|
58 | offsetdocumentend) = unpack("<4sBBHHHII", docheader) |
---|
59 | self.infile.seek(offsetfirstpage - len(docheader), 1) |
---|
60 | while True : |
---|
61 | headerid = self.infile.read(1) |
---|
62 | if not headerid : |
---|
63 | break |
---|
64 | headerid = ord(headerid) |
---|
65 | if 1 <= headerid <= 216 : # Normal record header |
---|
66 | self.infile.seek(headerid, 1) |
---|
67 | elif headerid == 255 : # Illegal line / Additional user info |
---|
68 | additionalbyte = self.infile.read(1) |
---|
69 | if not additionalbyte : |
---|
70 | break |
---|
71 | additionalbyte = ord(additionalbyte) |
---|
72 | if 1 <= additionalbyte <= 255 : |
---|
73 | # Skip additional user information (reserved) |
---|
74 | self.infile.seek(additionalbyte, 1) |
---|
75 | elif not headerid : |
---|
76 | # Record with more than 216 MH-coded bytes |
---|
77 | recordlen = self.infile.read(2) |
---|
78 | if not recordlen : |
---|
79 | break |
---|
80 | recordlen = unpack("<H", recordlen)[0] |
---|
81 | self.infile.seek(recordlen, 1) |
---|
82 | elif headerid == 254 : # Page header |
---|
83 | pageheader = self.infile.read(17) |
---|
84 | if not pageheader : |
---|
85 | break |
---|
86 | headerlen = ord(pageheader[0]) |
---|
87 | if not headerlen : |
---|
88 | break # End Of Document |
---|
89 | (vres, |
---|
90 | hres, |
---|
91 | coding, |
---|
92 | reserved, |
---|
93 | linelen, |
---|
94 | pagelen, |
---|
95 | offsetpreviouspage, |
---|
96 | offsetnextpage) = unpack("<4BHHII", pageheader[1:]) |
---|
97 | pagecount += 1 |
---|
98 | if (offsetnextpage == 1) or (vres == 255) : |
---|
99 | break # End Of Document |
---|
100 | self.infile.seek(offsetnextpage, 1) |
---|
101 | except struct.error : |
---|
102 | raise pdlparser.PDLParserError, "Invalid Structured Fax datas" |
---|
103 | return max(docpagecount, pagecount) |
---|