root / pkpgcounter / trunk / pkpgpdls / postscript.py @ 521

Revision 521, 8.7 kB (checked in by jerome, 16 years ago)

More code cleaning : let ghostscript do the job of reading the input
file directly instead of from a pipe.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Auth Date Id Rev
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, 2007 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 3 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, see <http://www.gnu.org/licenses/>.
19#
20# $Id$
21#
22
23"""This modules implements a page counter for PostScript documents."""
24
25import sys
26import os
27import tempfile
28import popen2
29
30import pdlparser
31import inkcoverage
32
33class Parser(pdlparser.PDLParser) :
34    """A parser for PostScript documents."""
35    totiffcommands = [ 'gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r"%(dpi)i" -sOutputFile="%(outfname)s" "%(infname)s"' ]
36    openmode = "rU"
37    def isValid(self) :   
38        """Returns True if data is PostScript, else False."""
39        if self.parent.firstblock.startswith("%!") or \
40           self.parent.firstblock.startswith("\004%!") or \
41           self.parent.firstblock.startswith("\033%-12345X%!PS") or \
42           ((self.parent.firstblock[:128].find("\033%-12345X") != -1) and \
43             ((self.parent.firstblock.find("LANGUAGE=POSTSCRIPT") != -1) or \
44              (self.parent.firstblock.find("LANGUAGE = POSTSCRIPT") != -1) or \
45              (self.parent.firstblock.find("LANGUAGE = Postscript") != -1))) or \
46              (self.parent.firstblock.find("%!PS-Adobe") != -1) :
47            self.logdebug("DEBUG: Input file is in the PostScript format.")
48            return True
49        else :   
50            return False
51       
52    def throughGhostScript(self) :
53        """Get the count through GhostScript, useful for non-DSC compliant PS files."""
54        self.logdebug("Internal parser sucks, using GhostScript instead...")
55        infname = self.parent.filename
56        command = 'gs -sDEVICE=bbox -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET "%(infname)s" 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null'
57        pagecount = 0
58        fromchild = os.popen(command % locals(), "r")
59        try :
60            try :
61                pagecount = int(fromchild.readline().strip())
62            except (IOError, OSError, AttributeError, ValueError), msg :
63                raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg
64        finally :       
65            if fromchild.close() is not None :
66                raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document"
67        self.logdebug("GhostScript said : %s pages" % pagecount)   
68        return pagecount * self.copies
69       
70    def natively(self) :
71        """Count pages in a DSC compliant PostScript document."""
72        pagecount = 0
73        self.pages = { 0 : { "copies" : 1 } }
74        oldpagenum = None
75        previousline = ""
76        notrust = False
77        prescribe = False # Kyocera's Prescribe commands
78        acrobatmarker = False
79        pagescomment = None
80        for line in self.infile :
81            line = line.strip()
82            if (not prescribe) and line.startswith(r"%%BeginResource: procset pdf") \
83               and not acrobatmarker :
84                notrust = True # Let this stuff be managed by GhostScript, but we still extract number of copies
85            elif line.startswith(r"%ADOPrintSettings: L") :
86                acrobatmarker = True
87            elif line.startswith("!R!") :
88                prescribe = True
89            elif line.startswith(r"%%Pages: ") :
90                try :
91                    pagescomment = max(pagescomment or 0, int(line.split()[1]))
92                except ValueError :
93                    pass # strange, to say the least
94            elif line.startswith(r"%%Page: ") or line.startswith(r"(%%[Page: ") :
95                proceed = 1
96                try :
97                    # treats both "%%Page: x x" and "%%Page: (x-y) z" (probably N-up mode)
98                    newpagenum = int(line.split(']')[0].split()[-1])
99                except :   
100                    notinteger = 1 # It seems that sometimes it's not an integer but an EPS file name
101                else :   
102                    notinteger = 0
103                    if newpagenum == oldpagenum :
104                        proceed = 0
105                    else :
106                        oldpagenum = newpagenum
107                if proceed and not notinteger :       
108                    pagecount += 1
109                    self.pages[pagecount] = { "copies" : self.pages[pagecount-1]["copies"] }
110            elif line.startswith(r"%%Requirements: numcopies(") :   
111                try :
112                    number = int(line.split('(')[1].split(')')[0])
113                except :     
114                    pass
115                else :   
116                    if number > self.pages[pagecount]["copies"] :
117                        self.pages[pagecount]["copies"] = number
118            elif line.startswith(r"%%BeginNonPPDFeature: NumCopies ") :
119                # handle # of copies set by some Windows printer driver
120                try :
121                    number = int(line.split()[2])
122                except :     
123                    pass
124                else :   
125                    if number > self.pages[pagecount]["copies"] :
126                        self.pages[pagecount]["copies"] = number
127            elif line.startswith("1 dict dup /NumCopies ") :
128                # handle # of copies set by mozilla/kprinter
129                try :
130                    number = int(line.split()[4])
131                except :     
132                    pass
133                else :   
134                    if number > self.pages[pagecount]["copies"] :
135                        self.pages[pagecount]["copies"] = number
136            elif line.startswith("{ pop 1 dict dup /NumCopies ") :
137                # handle # of copies set by firefox/kprinter/cups (alternate syntax)
138                try :
139                    number = int(line.split()[6])
140                except :
141                    pass
142                else :
143                    if number > self.pages[pagecount]["copies"] :
144                        self.pages[pagecount]["copies"] = number
145            elif line.startswith("/languagelevel where{pop languagelevel}{1}ifelse 2 ge{1 dict dup/NumCopies") :
146                try :
147                    number = int(previousline[2:])
148                except :
149                    pass
150                else :
151                    if number > self.pages[pagecount]["copies"] :
152                        self.pages[pagecount]["copies"] = number
153            elif line.startswith("/#copies ") :
154                try :
155                    number = int(line.split()[1])
156                except :     
157                    pass
158                else :   
159                    if number > self.pages[pagecount]["copies"] :
160                        self.pages[pagecount]["copies"] = number
161            elif line.startswith(r"%RBINumCopies: ") :   
162                try :
163                    number = int(line.split()[1])
164                except :     
165                    pass
166                else :   
167                    if number > self.pages[pagecount]["copies"] :
168                        self.pages[pagecount]["copies"] = number
169            previousline = line
170           
171        # extract max number of copies to please the ghostscript parser, just   
172        # in case we will use it later
173        self.copies = max([ v["copies"] for (k, v) in self.pages.items() ])
174       
175        # now apply the number of copies to each page
176        if not pagecount and pagescomment :   
177            pagecount = pagescomment
178        for pnum in range(1, pagecount + 1) :
179            page = self.pages.get(pnum, self.pages.get(1, self.pages.get(0, { "copies" : 1 })))
180            copies = page["copies"]
181            pagecount += (copies - 1)
182            self.logdebug("%s * page #%s" % (copies, pnum))
183           
184        self.logdebug("Internal parser said : %s pages" % pagecount)
185        return (pagecount, notrust)
186       
187    def getJobSize(self) :   
188        """Count pages in PostScript document."""
189        self.copies = 1
190        (nbpages, notrust) = self.natively()
191        newnbpages = nbpages
192        if notrust or not nbpages :
193            try :
194                newnbpages = self.throughGhostScript()
195            except pdlparser.PDLParserError, msg :
196                self.logdebug(msg)
197        return max(nbpages, newnbpages)   
Note: See TracBrowser for help on using the browser.