root / pkpgcounter / trunk / pkpgpdls / dvi.py @ 373

Revision 373, 3.4 kB (checked in by jerome, 18 years ago)

The computation of ink coverage now works for the DVI file format.

  • Property svn:keywords set to Author Id Date Revision
Line 
1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
6# (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com>
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
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
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# $Id$
22#
23
24"""This modules implements a page counter for DVI documents."""
25
26import sys
27import os
28import mmap
29from struct import unpack
30
31import pdlparser
32
33class Parser(pdlparser.PDLParser) :
34    """A parser for DVI documents."""
35    totiffcommand = 'cat >%(fname)s && dvips -q -o - %(fname)s | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(fname)s" -'
36    def isValid(self) :       
37        """Returns 1 if data is DVI, else 0."""
38        try :
39            if (ord(self.firstblock[0]) == 0xf7) and (ord(self.lastblock[-1]) == 0xdf) :
40                self.logdebug("DEBUG: Input file is in the DVI format.")
41                return 1
42            else :   
43                return 0
44        except IndexError :         
45            return 0
46           
47    def getJobSize(self) :
48        """Counts pages in a DVI document.
49       
50           Algorithm by Jerome Alet.
51           
52           The documentation used for this was :
53         
54           http://www.math.umd.edu/~asnowden/comp-cont/dvi.html
55        """
56        infileno = self.infile.fileno()
57        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
58        pagecount = 0
59        pos = -1
60        eofchar = chr(0xdf)
61        postchar = chr(0xf8)
62        try :
63            while minfile[pos] == eofchar :
64                pos -= 1
65            idbyte = minfile[pos]   
66            if idbyte != minfile[1] :
67                raise IndexError, "Invalid DVI file."
68            pos = unpack(">I", minfile[pos - 4:pos])[0]
69            if minfile[pos] != postchar :
70                raise IndexError, "Invalid DVI file."
71            pagecount = unpack(">H", minfile[pos + 27: pos + 29])[0]
72        except IndexError : # EOF ?
73            pass
74        minfile.close() # reached EOF
75        return pagecount
76       
77def test() :       
78    """Test function."""
79    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
80        sys.argv.append("-")
81    totalsize = 0   
82    for arg in sys.argv[1:] :
83        if arg == "-" :
84            infile = sys.stdin
85            mustclose = 0
86        else :   
87            infile = open(arg, "rb")
88            mustclose = 1
89        try :
90            parser = Parser(infile, debug=1)
91            totalsize += parser.getJobSize()
92        except pdlparser.PDLParserError, msg :   
93            sys.stderr.write("ERROR: %s\n" % msg)
94            sys.stderr.flush()
95        if mustclose :   
96            infile.close()
97    print "%s" % totalsize
98   
99if __name__ == "__main__" :   
100    test()
Note: See TracBrowser for help on using the browser.