root / pkpgcounter / trunk / pkpgpdls / cfax.py @ 539

Revision 539, 4.0 kB (checked in by jerome, 16 years ago)

Added support for Structured Fax documents.

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