root / pkpgcounter / trunk / pkpgpdls / pcl345.py @ 3495

Revision 3495, 24.9 kB (checked in by jerome, 15 years ago)

Now correctly handles the <ESC>$b???W sequences.
IMPORTANT : a post-parsing adjustment was done for ImageRunner?
documents, which incremented the page count. This adjustment **might**
have been needed because of this bug. A new adjustment is now done for
ImageRunner? files, which decrements the page count instead. Seems to
work on the set of test files. Fixes #40.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
RevLine 
[3410]1# -*- coding: utf-8 -*-
[392]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
[392]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
[392]9# (at your option) any later version.
[3436]10#
[392]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#
[392]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/>.
[392]18#
19# $Id$
20#
21
22"""This modules implements a page counter for PCL3/4/5 documents."""
23
24import sys
25import os
26import mmap
27from struct import unpack
28
29import pdlparser
30import pjl
31
[396]32NUL = chr(0x00)
[452]33LINEFEED = chr(0x0a)
[396]34FORMFEED = chr(0x0c)
35ESCAPE = chr(0x1b)
36ASCIILIMIT = chr(0x80)
[392]37
38class Parser(pdlparser.PDLParser) :
39    """A parser for PCL3, PCL4, PCL5 documents."""
[3436]40    totiffcommands = [ 'pcl6 -sDEVICE=pdfwrite -r"%(dpi)i" -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -sOutputFile=- "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(outfname)s" -',
[492]41                       'pcl6 -sDEVICE=pswrite -r"%(dpi)i" -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -sOutputFile=- "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(outfname)s" -',
[428]42                     ]
[527]43    required = [ "pcl6", "gs" ]
[555]44    format = "PCL3/4/5"
[392]45    mediasizes = {  # ESC&l####A
46                    0 : "Default",
47                    1 : "Executive",
48                    2 : "Letter",
49                    3 : "Legal",
[3436]50                    6 : "Ledger",
[392]51                    25 : "A5",
52                    26 : "A4",
53                    27 : "A3",
54                    45 : "JB5",
55                    46 : "JB4",
56                    71 : "HagakiPostcard",
57                    72 : "OufukuHagakiPostcard",
58                    80 : "MonarchEnvelope",
59                    81 : "COM10Envelope",
60                    90 : "DLEnvelope",
61                    91 : "C5Envelope",
62                    100 : "B5Envelope",
63                    101 : "Custom",
[3436]64                 }
65
[392]66    mediasources = { # ESC&l####H
67                     0 : "Default",
68                     1 : "Main",
69                     2 : "Manual",
70                     3 : "ManualEnvelope",
71                     4 : "Alternate",
72                     5 : "OptionalLarge",
73                     6 : "EnvelopeFeeder",
74                     7 : "Auto",
75                     8 : "Tray1",
76                   }
[3436]77
[392]78    orientations = { # ESC&l####O
79                     0 : "Portrait",
80                     1 : "Landscape",
81                     2 : "ReversePortrait",
82                     3 : "ReverseLandscape",
83                   }
[3436]84
[392]85    mediatypes = { # ESC&l####M
86                     0 : "Plain",
87                     1 : "Bond",
88                     2 : "Special",
89                     3 : "Glossy",
90                     4 : "Transparent",
91                   }
[3436]92
93    def isValid(self) :
[392]94        """Returns True if data is PCL3/4/5, else False."""
[538]95        try :
96            pos = 0
97            while self.firstblock[pos] == chr(0) :
98                pos += 1
[3436]99        except IndexError :
[538]100            return False
[3436]101        else :
[538]102            firstblock = self.firstblock[pos:]
103            if firstblock.startswith("\033E\033") or \
[541]104               firstblock.startswith("\033%1BBPIN;") or \
[538]105               ((pos == 11000) and firstblock.startswith("\033")) or \
106               (firstblock.startswith("\033*rbC") and (not self.lastblock[-3:] == "\f\033@")) or \
107               firstblock.startswith("\033*rB\033") or \
108               firstblock.startswith("\033%8\033") or \
109               (firstblock.find("\033%-12345X") != -1) or \
110               (firstblock.find("@PJL ENTER LANGUAGE=PCL\012\015\033") != -1) or \
111               (firstblock.startswith(chr(0xcd)+chr(0xca)) and (firstblock.find("\033E\033") != -1)) :
112                return True
[3436]113            else :
[538]114                return False
[3436]115
[396]116    def setPageDict(self, attribute, value) :
[392]117        """Initializes a page dictionnary."""
[452]118        dic = self.pages.setdefault(self.pagecount, { "linescount" : 1,
[3436]119                                                      "copies" : 1,
120                                                      "mediasource" : "Main",
121                                                      "mediasize" : "Default",
122                                                      "mediatype" : "Plain",
123                                                      "orientation" : "Portrait",
124                                                      "escaped" : "",
[452]125                                                      "duplex": 0 })
[392]126        dic[attribute] = value
[3436]127
128    def readByte(self) :
[394]129        """Reads a byte from the input stream."""
130        tag = ord(self.minfile[self.pos])
131        self.pos += 1
132        return tag
[3436]133
134    def endPage(self) :
[392]135        """Handle the FF marker."""
[398]136        #self.logdebug("FORMFEED %i at %08x" % (self.pagecount, self.pos-1))
[401]137        if not self.hpgl2 :
138            # Increments page count only if we are not inside an HPGL2 block
139            self.pagecount += 1
[3436]140
141    def escPercent(self) :
[392]142        """Handles the ESC% sequence."""
143        if self.minfile[self.pos : self.pos+7] == r"-12345X" :
[396]144            #self.logdebug("Generic ESCAPE sequence at %08x" % self.pos)
[392]145            self.pos += 7
[396]146            buffer = []
147            quotes = 0
148            char = chr(self.readByte())
[3436]149            while ((char < ASCIILIMIT) or (quotes % 2)) and (char not in (FORMFEED, ESCAPE, NUL)) :
[396]150                buffer.append(char)
151                if char == '"' :
152                    quotes += 1
153                char = chr(self.readByte())
154            self.setPageDict("escaped", "".join(buffer))
155            #self.logdebug("ESCAPED : %s" % "".join(buffer))
156            self.pos -= 1   # Adjust position
[3436]157        else :
[396]158            while 1 :
159                (value, end) = self.getInteger()
160                if end == 'B' :
161                    self.enterHPGL2()
162                    while self.minfile[self.pos] != ESCAPE :
163                        self.pos += 1
[3436]164                    self.pos -= 1
165                    return
166                elif end == 'A' :
[396]167                    self.exitHPGL2()
168                    return
[3436]169                elif end is None :
[397]170                    return
[3436]171
172    def enterHPGL2(self) :
[396]173        """Enters HPGL2 mode."""
[398]174        #self.logdebug("ENTERHPGL2 %08x" % self.pos)
[396]175        self.hpgl2 = True
[3436]176
177    def exitHPGL2(self) :
[396]178        """Exits HPGL2 mode."""
[398]179        #self.logdebug("EXITHPGL2 %08x" % self.pos)
[396]180        self.hpgl2 = False
[3436]181
182    def handleTag(self, tagtable) :
[392]183        """Handles tags."""
[394]184        tagtable[self.readByte()]()
[3436]185
186    def escape(self) :
[392]187        """Handles the ESC character."""
[395]188        #self.logdebug("ESCAPE")
[392]189        self.handleTag(self.esctags)
[3436]190
191    def escAmp(self) :
[392]192        """Handles the ESC& sequence."""
[395]193        #self.logdebug("AMP")
[392]194        self.handleTag(self.escamptags)
[3436]195
[3495]196    def escDollar(self) :
197        """Handles the ESC$ sequence."""
198        #self.logdebug("DOLLAR")
199        self.handleTag(self.escdollartags)
200
[3436]201    def escStar(self) :
[392]202        """Handles the ESC* sequence."""
[395]203        #self.logdebug("STAR")
[392]204        self.handleTag(self.escstartags)
[3436]205
206    def escLeftPar(self) :
[392]207        """Handles the ESC( sequence."""
[395]208        #self.logdebug("LEFTPAR")
[392]209        self.handleTag(self.escleftpartags)
[3436]210
211    def escRightPar(self) :
[392]212        """Handles the ESC( sequence."""
[395]213        #self.logdebug("RIGHTPAR")
[392]214        self.handleTag(self.escrightpartags)
[3436]215
216    def escE(self) :
[392]217        """Handles the ESCE sequence."""
[395]218        #self.logdebug("RESET")
[392]219        self.resets += 1
[3436]220
221    def escAmpl(self) :
[392]222        """Handles the ESC&l sequence."""
223        while 1 :
224            (value, end) = self.getInteger()
225            if value is None :
226                return
227            if end in ('h', 'H') :
228                mediasource = self.mediasources.get(value, str(value))
229                self.mediasourcesvalues.append(mediasource)
[396]230                self.setPageDict("mediasource", mediasource)
231                #self.logdebug("MEDIASOURCE %s" % mediasource)
[392]232            elif end in ('a', 'A') :
233                mediasize = self.mediasizes.get(value, str(value))
234                self.mediasizesvalues.append(mediasize)
[396]235                self.setPageDict("mediasize", mediasize)
236                #self.logdebug("MEDIASIZE %s" % mediasize)
[392]237            elif end in ('o', 'O') :
238                orientation = self.orientations.get(value, str(value))
239                self.orientationsvalues.append(orientation)
[396]240                self.setPageDict("orientation", orientation)
241                #self.logdebug("ORIENTATION %s" % orientation)
[392]242            elif end in ('m', 'M') :
243                mediatype = self.mediatypes.get(value, str(value))
244                self.mediatypesvalues.append(mediatype)
[396]245                self.setPageDict("mediatype", mediatype)
246                #self.logdebug("MEDIATYPE %s" % mediatype)
[392]247            elif end == 'X' :
248                self.copies.append(value)
[396]249                self.setPageDict("copies", value)
250                #self.logdebug("COPIES %i" % value)
[3436]251            elif end == 'F' :
[452]252                self.linesperpagevalues.append(value)
253                self.linesperpage = value
254                #self.logdebug("LINES PER PAGE : %i" % self.linesperpage)
255            #else :
256            #    self.logdebug("Unexpected end <%s> and value <%s>" % (end, value))
[3436]257
258    def escAmpa(self) :
[395]259        """Handles the ESC&a sequence."""
260        while 1 :
261            (value, end) = self.getInteger()
262            if value is None :
263                return
[3436]264            if end == 'G' :
[396]265                #self.logdebug("BACKSIDES %i" % value)
[395]266                self.backsides.append(value)
[396]267                self.setPageDict("duplex", value)
[3436]268
269    def escAmpp(self) :
[395]270        """Handles the ESC&p sequence."""
271        while 1 :
272            (value, end) = self.getInteger()
273            if value is None :
274                return
[3436]275            if end == 'X' :
[395]276                self.pos += value
277                #self.logdebug("SKIPTO %08x" % self.pos)
[3436]278
279    def escStarb(self) :
[392]280        """Handles the ESC*b sequence."""
281        while 1 :
282            (value, end) = self.getInteger()
283            if (end is None) and (value is None) :
284                return
[3436]285            if end in ('V', 'W', 'v', 'w') :
[392]286                self.pos += (value or 0)
[395]287                #self.logdebug("SKIPTO %08x" % self.pos)
[3436]288
289    def escStarr(self) :
[392]290        """Handles the ESC*r sequence."""
291        while 1 :
292            (value, end) = self.getInteger()
293            if value is None :
294                if end is None :
295                    return
[3436]296                elif end in ('B', 'C') :
[395]297                    #self.logdebug("EndGFX")
[396]298                    if self.startgfx :
299                        self.endgfx.append(1)
[3436]300                    else :
[396]301                        #self.logdebug("EndGFX found before StartGFX, ignored.")
302                        pass
[392]303            if end == 'A' and (0 <= value <= 3) :
[395]304                #self.logdebug("StartGFX %i" % value)
[392]305                self.startgfx.append(value)
[3436]306
307    def escStaroptAmpu(self) :
[406]308        """Handles the ESC*o ESC*p ESC*t and ESC&u sequences."""
[392]309        while 1 :
310            (value, end) = self.getInteger()
311            if value is None :
312                return
[3436]313
314    def escSkipSomethingW(self) :
[406]315        """Handles the ESC???###W sequences."""
[392]316        while 1 :
317            (value, end) = self.getInteger()
318            if value is None :
319                return
[3436]320            if end == 'W' :
[395]321                self.pos += value
322                #self.logdebug("SKIPTO %08x" % self.pos)
[3436]323
324    def newLine(self) :
[452]325        """Handles new lines markers."""
326        if not self.hpgl2 :
327            dic = self.pages.get(self.pagecount, None)
328            if dic is None :
[3436]329                self.setPageDict("linescount", 1)
[452]330                dic = self.pages.get(self.pagecount)
[3436]331            nblines = dic["linescount"]
332            self.setPageDict("linescount", nblines + 1)
[452]333            if (self.linesperpage is not None) \
334               and (dic["linescount"] > self.linesperpage) :
335                self.pagecount += 1
[3436]336
337    def getInteger(self) :
[392]338        """Returns an integer value and the end character."""
339        sign = 1
340        value = None
341        while 1 :
[394]342            char = chr(self.readByte())
[396]343            if char in (NUL, ESCAPE, FORMFEED, ASCIILIMIT) :
[394]344                self.pos -= 1 # Adjust position
[392]345                return (None, None)
346            if char == '-' :
347                sign = -1
348            elif not char.isdigit() :
349                if value is not None :
350                    return (sign*value, char)
351                else :
352                    return (value, char)
[3436]353            else :
354                value = ((value or 0) * 10) + int(char)
355
356    def skipByte(self) :
[396]357        """Skips a byte."""
358        #self.logdebug("SKIPBYTE %08x ===> %02x" % (self.pos, ord(self.minfile[self.pos])))
359        self.pos += 1
[3436]360
361    def handleImageRunner(self) :
[399]362        """Handles Canon ImageRunner tags."""
363        tag = self.readByte()
364        if tag == ord(self.imagerunnermarker1[-1]) :
365            oldpos = self.pos-2
366            codop = self.minfile[self.pos:self.pos+2]
[471]367            length = unpack(">H", self.minfile[self.pos+6:self.pos+8])[0]
[399]368            self.pos += 18
369            if codop != self.imagerunnermarker2 :
370                self.pos += length
[3495]371            self.logdebug("ImageRunner tag : Skip %i bytes from 0x%08x to 0x%08x" % (self.pos-oldpos,
372                                                                                     oldpos,
373                                                                                     self.pos))
[399]374        else :
375            self.pos -= 1 # Adjust position
[3436]376
377    def getJobSize(self) :
[392]378        """Count pages in a PCL5 document.
[3436]379
[392]380           Should also work for PCL3 and PCL4 documents.
[3436]381
[392]382           Algorithm from pclcount
[3436]383           (c) 2003, by Eduardo Gielamo Oliveira & Rodolfo Broco Manin
[392]384           published under the terms of the GNU General Public Licence v2.
[3436]385
[392]386           Backported from C to Python by Jerome Alet, then enhanced
387           with more PCL tags detected. I think all the necessary PCL tags
388           are recognized to correctly handle PCL5 files wrt their number
389           of pages. The documentation used for this was :
[3436]390
[392]391           HP PCL/PJL Reference Set
392           PCL5 Printer Language Technical Quick Reference Guide
[3436]393           http://h20000.www2.hp.com/bc/docs/support/SupportManual/bpl13205/bpl13205.pdf
[392]394        """
395        infileno = self.infile.fileno()
396        self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
[396]397        self.pages = {}
[392]398        self.pagecount = 0
399        self.resets = 0
[395]400        self.backsides = []
[392]401        self.copies = []
402        self.mediasourcesvalues = []
403        self.mediasizesvalues = []
404        self.orientationsvalues = []
405        self.mediatypesvalues = []
[452]406        self.linesperpagevalues = []
407        self.linesperpage = None
[392]408        self.startgfx = []
409        self.endgfx = []
[396]410        self.hpgl2 = False
[399]411        self.imagerunnermarker1 = chr(0xcd) + chr(0xca) # Markers for Canon ImageRunner printers
412        self.imagerunnermarker2 = chr(0x10) + chr(0x02)
413        self.isimagerunner = (minfile[:2] == self.imagerunnermarker1)
[3436]414
[392]415        tags = [ lambda : None] * 256
[452]416        tags[ord(LINEFEED)] = self.newLine
[394]417        tags[ord(FORMFEED)] = self.endPage
418        tags[ord(ESCAPE)] = self.escape
[396]419        tags[ord(ASCIILIMIT)] = self.skipByte
[399]420        tags[ord(self.imagerunnermarker1[0])] = self.handleImageRunner
[3436]421
[392]422        self.esctags = [ lambda : None ] * 256
423        self.esctags[ord('%')] = self.escPercent
424        self.esctags[ord('*')] = self.escStar
[3495]425        self.esctags[ord('$')] = self.escDollar
[392]426        self.esctags[ord('&')] = self.escAmp
427        self.esctags[ord('(')] = self.escLeftPar
428        self.esctags[ord(')')] = self.escRightPar
429        self.esctags[ord('E')] = self.escE
[3436]430
[392]431        self.escamptags = [lambda : None ] * 256
[395]432        self.escamptags[ord('a')] = self.escAmpa
[392]433        self.escamptags[ord('l')] = self.escAmpl
[395]434        self.escamptags[ord('p')] = self.escAmpp
[406]435        self.escamptags[ord('b')] = self.escSkipSomethingW
436        self.escamptags[ord('n')] = self.escSkipSomethingW
437        self.escamptags[ord('u')] = self.escStaroptAmpu
[3436]438
[392]439        self.escstartags = [ lambda : None ] * 256
440        self.escstartags[ord('b')] = self.escStarb
441        self.escstartags[ord('r')] = self.escStarr
[406]442        self.escstartags[ord('o')] = self.escStaroptAmpu
443        self.escstartags[ord('p')] = self.escStaroptAmpu
444        self.escstartags[ord('t')] = self.escStaroptAmpu
445        self.escstartags[ord('c')] = self.escSkipSomethingW
446        self.escstartags[ord('g')] = self.escSkipSomethingW
447        self.escstartags[ord('i')] = self.escSkipSomethingW
448        self.escstartags[ord('l')] = self.escSkipSomethingW
449        self.escstartags[ord('m')] = self.escSkipSomethingW
450        self.escstartags[ord('v')] = self.escSkipSomethingW
[3436]451
[3495]452        self.escdollartags = [ lambda : None ] * 256
453        self.escdollartags[ord('b')] = self.escSkipSomethingW
454
[395]455        self.escleftpartags = [ lambda : None ] * 256
[406]456        self.escleftpartags[ord('s')] = self.escSkipSomethingW
457        self.escleftpartags[ord('f')] = self.escSkipSomethingW
[3436]458
[395]459        self.escrightpartags = [ lambda : None ] * 256
[406]460        self.escrightpartags[ord('s')] = self.escSkipSomethingW
[3436]461
[392]462        self.pos = 0
463        try :
464            try :
465                while 1 :
[395]466                    tags[self.readByte()]()
[3436]467            except IndexError : # EOF ?
[392]468                pass
469        finally :
470            self.minfile.close()
[3436]471
[395]472        self.logdebug("Pagecount : \t\t\t%i" % self.pagecount)
473        self.logdebug("Resets : \t\t\t%i" % self.resets)
474        self.logdebug("Copies : \t\t\t%s" % self.copies)
475        self.logdebug("NbCopiesMarks : \t\t%i" % len(self.copies))
476        self.logdebug("MediaTypes : \t\t\t%s" % self.mediatypesvalues)
477        self.logdebug("NbMediaTypes : \t\t\t%i" % len(self.mediatypesvalues))
478        self.logdebug("MediaSizes : \t\t\t%s" % self.mediasizesvalues)
[3396]479        nbmediasizes = len(self.mediasizesvalues)
480        self.logdebug("NbMediaSizes : \t\t\t%i" % nbmediasizes)
[395]481        self.logdebug("MediaSources : \t\t\t%s" % self.mediasourcesvalues)
482        nbmediasourcesdefault = len([m for m in self.mediasourcesvalues if m == 'Default'])
[543]483        nbmediasourcesnotdefault = len(self.mediasourcesvalues) - nbmediasourcesdefault
[395]484        self.logdebug("MediaSourcesDefault : \t\t%i" % nbmediasourcesdefault)
[543]485        self.logdebug("MediaSourcesNOTDefault : \t%i" % nbmediasourcesnotdefault)
[395]486        self.logdebug("Orientations : \t\t\t%s" % self.orientationsvalues)
[398]487        nborientations = len(self.orientationsvalues)
488        self.logdebug("NbOrientations : \t\t\t%i" % nborientations)
[395]489        self.logdebug("StartGfx : \t\t\t%s" % len(self.startgfx))
490        self.logdebug("EndGfx : \t\t\t%s" % len(self.endgfx))
[3396]491        nbbacksides = len(self.backsides)
[395]492        self.logdebug("BackSides : \t\t\t%s" % self.backsides)
[3396]493        self.logdebug("NbBackSides : \t\t\t%i" % nbbacksides)
[399]494        self.logdebug("IsImageRunner : \t\t\t%s" % self.isimagerunner)
[3436]495
[3495]496#        if self.isimagerunner :
497#            self.logdebug("Adjusting PageCount : +1")
498#            self.pagecount += 1      # ImageRunner adjustment
[399]499        if self.isimagerunner :
[3495]500            self.logdebug("Adjusting PageCount : -1")
501            self.pagecount -= 1      # ImageRunner adjustment
[3436]502        elif self.linesperpage is not None :
[543]503            self.logdebug("Adjusting PageCount : +1")
[452]504            self.pagecount += 1      # Adjusts for incomplete last page
[399]505        elif len(self.startgfx) == len(self.endgfx) == 0 :
[398]506            if self.resets % 2 :
[3396]507                if (not self.pagecount) and (nborientations < nbbacksides) :
508                    self.logdebug("Adjusting PageCount because of backsides : %i" % nbbacksides)
509                    self.pagecount = nbbacksides
510                elif nborientations == self.pagecount + 1 :
[398]511                    self.logdebug("Adjusting PageCount : +1")
512                    self.pagecount += 1
[543]513                elif (self.pagecount > 1) \
514                     and (nborientations == self.pagecount - 1) :
[398]515                    self.logdebug("Adjusting PageCount : -1")
516                    self.pagecount -= 1
[543]517        elif (self.pagecount > 1) \
518             and (self.resets == 2) \
519             and (not nbmediasourcesdefault) \
520             and (nbmediasourcesnotdefault == 1) :
521            self.logdebug("Adjusting PageCount : -1")
522            self.pagecount -= 1
[3436]523
[3400]524        self.pagecount = self.pagecount or nbmediasourcesdefault or nbmediasizes or nborientations or self.resets
[3436]525
[540]526        if not self.pagecount :
527            if self.resets == len(self.startgfx) :
[3436]528                self.pagecount = self.resets
529
530        defaultpjlcopies = 1
[400]531        defaultduplexmode = "Simplex"
532        defaultpapersize = ""
533        oldpjlcopies = -1
534        oldduplexmode = ""
535        oldpapersize = ""
536        for pnum in range(self.pagecount) :
537            # if no number of copies defined, take the preceding one else the one set before any page else 1.
538            page = self.pages.get(pnum, self.pages.get(pnum - 1, self.pages.get(0, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : "", "duplex": 0})))
539            pjlstuff = page["escaped"]
540            if pjlstuff :
541                pjlparser = pjl.PJLParser(pjlstuff)
542                nbdefaultcopies = int(pjlparser.default_variables.get("COPIES", -1))
543                nbcopies = int(pjlparser.environment_variables.get("COPIES", -1))
544                nbdefaultqty = int(pjlparser.default_variables.get("QTY", -1))
545                nbqty = int(pjlparser.environment_variables.get("QTY", -1))
546                if nbdefaultcopies > -1 :
547                    defaultpjlcopies = nbdefaultcopies
548                if nbdefaultqty > -1 :
549                    defaultpjlcopies = nbdefaultqty
550                if nbcopies > -1 :
551                    pjlcopies = nbcopies
552                elif nbqty > -1 :
553                    pjlcopies = nbqty
554                else :
[3436]555                    if oldpjlcopies == -1 :
[400]556                        pjlcopies = defaultpjlcopies
[3436]557                    else :
558                        pjlcopies = oldpjlcopies
559                if page["duplex"] :
[400]560                    duplexmode = "Duplex"
[3436]561                else :
[400]562                    defaultdm = pjlparser.default_variables.get("DUPLEX", "")
563                    if defaultdm :
564                        if defaultdm.upper() == "ON" :
565                            defaultduplexmode = "Duplex"
[3436]566                        else :
[400]567                            defaultduplexmode = "Simplex"
568                    envdm = pjlparser.environment_variables.get("DUPLEX", "")
569                    if envdm :
570                        if envdm.upper() == "ON" :
571                            duplexmode = "Duplex"
[3436]572                        else :
[400]573                            duplexmode = "Simplex"
[3436]574                    else :
[400]575                        duplexmode = oldduplexmode or defaultduplexmode
576                defaultps = pjlparser.default_variables.get("PAPER", "")
577                if defaultps :
578                    defaultpapersize = defaultps
579                envps = pjlparser.environment_variables.get("PAPER", "")
580                if envps :
581                    papersize = envps
[3436]582                else :
[400]583                    if not oldpapersize :
584                        papersize = defaultpapersize
[3436]585                    else :
[400]586                        papersize = oldpapersize
[3436]587            else :
[400]588                if oldpjlcopies == -1 :
589                    pjlcopies = defaultpjlcopies
[3436]590                else :
[400]591                    pjlcopies = oldpjlcopies
[3436]592
[400]593                duplexmode = (page["duplex"] and "Duplex") or oldduplexmode or defaultduplexmode
[3436]594                if not oldpapersize :
[400]595                    papersize = defaultpapersize
[3436]596                else :
[400]597                    papersize = oldpapersize
598                papersize = oldpapersize or page["mediasize"]
599            if page["mediasize"] != "Default" :
600                papersize = page["mediasize"]
[3436]601            if not duplexmode :
[400]602                duplexmode = oldduplexmode or defaultduplexmode
[3436]603            oldpjlcopies = pjlcopies
[400]604            oldduplexmode = duplexmode
605            oldpapersize = papersize
[456]606            copies = max(pjlcopies, page["copies"]) # Was : pjlcopies * page["copies"]
[400]607            self.pagecount += (copies - 1)
608            self.logdebug("%s*%s*%s*%s*%s*%s*BW" % (copies, \
609                                              page["mediatype"], \
610                                              papersize, \
611                                              page["orientation"], \
612                                              page["mediasource"], \
613                                              duplexmode))
[3436]614
[400]615        return self.pagecount
Note: See TracBrowser for help on using the browser.