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

Revision 3474, 3.8 kB (checked in by jerome, 15 years ago)

Changed copyright years.

  • Property svn:keywords set to Author Id Date Revision
RevLine 
[3410]1# -*- coding: utf-8 -*-
[216]2#
3# pkpgcounter : a generic Page Description Language parser
4#
[3474]5# (c) 2003-2009 Jerome Alet <alet@librelogiciel.com>
[463]6# This program is free software: you can redistribute it and/or modify
[216]7# it under the terms of the GNU General Public License as published by
[463]8# the Free Software Foundation, either version 3 of the License, or
[216]9# (at your option) any later version.
[3436]10#
[216]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.
[3436]15#
[216]16# You should have received a copy of the GNU General Public License
[463]17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[216]18#
19# $Id$
20#
21
[539]22"""This modules implements a page counter for Structured Fax documents."""
[356]23
[539]24import struct
[216]25
[235]26import pdlparser
[216]27
[220]28class Parser(pdlparser.PDLParser) :
[539]29    """A parser for Structured Fax documents."""
[555]30    format = "Structured Fax"
[3436]31    def isValid(self) :
[539]32        """Returns True if data is Structured Fax, else False."""
33        if self.firstblock.startswith("Sfff") :
34            return True
[3436]35        else :
[387]36            return False
[3436]37
[216]38    def getJobSize(self) :
[539]39        """Counts pages in a Structured Fax document.
[3436]40
[216]41           Algorithm by Jerome Alet.
[3436]42
[216]43           The documentation used for this was :
[3436]44
[539]45           http://delphi.pjh2.de/articles/graphic/sff_format.php
[216]46        """
[539]47        unpack = struct.unpack
[216]48        pagecount = 0
[539]49        docheader = self.infile.read(20)
[216]50        try :
[539]51            (sffid,
52             version,
53             reserved,
54             userinfo,
55             docpagecount,
56             offsetfirstpage,
57             offsetlastpage,
58             offsetdocumentend) = unpack("<4sBBHHHII", docheader)
[3436]59            self.infile.seek(offsetfirstpage - len(docheader), 1)
[539]60            while True :
61                headerid = self.infile.read(1)
62                if not headerid :
63                    break
[3436]64                headerid = ord(headerid)
[539]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
[3436]71                    additionalbyte = ord(additionalbyte)
[539]72                    if 1 <= additionalbyte <= 255 :
73                        # Skip additional user information (reserved)
74                        self.infile.seek(additionalbyte, 1)
[3436]75                elif not headerid :
[539]76                    # Record with more than 216 MH-coded bytes
77                    recordlen = self.infile.read(2)
78                    if not recordlen :
79                        break
[3436]80                    recordlen = unpack("<H", recordlen)[0]
[539]81                    self.infile.seek(recordlen, 1)
82                elif headerid == 254 : # Page header
83                    pageheader = self.infile.read(17)
[3436]84                    if not pageheader :
[539]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:])
[3436]97                    pagecount += 1
[539]98                    if (offsetnextpage == 1) or (vres == 255) :
99                        break # End Of Document
[3436]100                    self.infile.seek(offsetnextpage, 1)
101        except struct.error :
[539]102             raise pdlparser.PDLParserError, "Invalid Structured Fax datas"
[3436]103        return max(docpagecount, pagecount)
Note: See TracBrowser for help on using the browser.