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

Revision 562, 8.2 kB (checked in by jerome, 16 years ago)

Fixed a small problem with the parser's rewrite.

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