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

Revision 3578, 24.9 kB (checked in by jerome, 5 years ago)

Clarified dependency wrt PIL/Pillow.
Updated copyright years.
Regenerated manual page.

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