Index: pkpgcounter/trunk/pkpgpdls/pcl345.py
===================================================================
--- pkpgcounter/trunk/pkpgpdls/pcl345.py (revision 249)
+++ pkpgcounter/trunk/pkpgpdls/pcl345.py (revision 252)
@@ -84,6 +84,5 @@
            self.firstblock.startswith("\033%8\033") or \
            (self.firstblock.find("\033%-12345X") != -1) :
-            if self.debug :  
-                sys.stderr.write("DEBUG: Input file is in the PCL3/4/5 format.\n")
+            self.logdebug("DEBUG: Input file is in the PCL3/4/5 format.")
             return 1
         else :    
@@ -92,6 +91,6 @@
     def setPageDict(self, pages, number, attribute, value) :
         """Initializes a page dictionnary."""
-        dict = pages.setdefault(number, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait"})
-        dict[attribute] = value
+        dic = pages.setdefault(number, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : []})
+        dic[attribute] = value
         
     def getJobSize(self) :     
@@ -137,4 +136,6 @@
         mediasourcecount = mediasizecount = orientationcount = mediatypecount = 0
         tag = None
+        endmark = chr(0x1b) + chr(0x0c) + chr(0x00) 
+        asciilimit = chr(0x80)
         pages = {}
         pos = 0
@@ -145,93 +146,101 @@
                     pagecount += 1
                 elif char == "\033" :    
-                    starb = ampl = 0
-                    #
-                    #     <ESC>*b###y#m###v###w... -> PCL3 raster graphics
-                    #     <ESC>*b###W -> Start of a raster data row/block
-                    #     <ESC>*b###V -> Start of a raster data plane
-                    #     <ESC>*c###W -> Start of a user defined pattern
-                    #     <ESC>*i###W -> Start of a viewing illuminant block
-                    #     <ESC>*l###W -> Start of a color lookup table
-                    #     <ESC>*m###W -> Start of a download dither matrix block
-                    #     <ESC>*v###W -> Start of a configure image data block
-                    #     <ESC>*r1A -> Start Gfx 
-                    #     <ESC>(s###W -> Start of a characters description block
-                    #     <ESC>)s###W -> Start of a fonts description block
-                    #     <ESC>(f###W -> Start of a symbol set block
-                    #     <ESC>&b###W -> Start of configuration data block
-                    #     <ESC>&l###X -> Number of copies for current page
-                    #     <ESC>&n###W -> Starts an alphanumeric string ID block
-                    #     <ESC>&p###X -> Start of a non printable characters block
-                    #     <ESC>&a2G -> Back side when duplex mode as generated by rastertohp
-                    #     <ESC>*g###W -> Needed for planes in PCL3 output
-                    #     <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource
-                    #     <ESC>&l###A -> mediasize
-                    #     <ESC>&l###O -> orientation
-                    #     <ESC>&l###M -> mediatype
-                    #     <ESC>*t###R -> gfx resolution
-                    #
-                    tagstart = minfile[pos] ; pos += 1
-                    if tagstart in "E9=YZ" : # one byte PCL tag
-                        if tagstart == "E" :
-                            resets += 1
-                        continue             # skip to next tag
-                    tag = tagstart + minfile[pos] ; pos += 1
-                    if tag == "*b" : 
-                        starb = 1
-                        tagend = "VW"
-                    elif tag == "&l" :    
-                        ampl = 1
-                        tagend = "XHAOM"
-                    else :    
-                        try :
-                            tagend = tagsends[tag]
-                        except KeyError :    
-                            continue # Unsupported PCL tag
-                    # Now read the numeric argument
-                    size = 0
-                    while 1 :
-                        char = minfile[pos] ; pos += 1
-                        if not char.isdigit() :
-                            break
-                        size = (size * 10) + int(char)    
-                    if char in tagend :    
-                        if tag == "&l" :
-                            if char == "X" : 
-                                self.setPageDict(pages, pagecount, "copies", size)
-                            elif char == "H" :
-                                self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size)))
-                                mediasourcecount += 1
-                                ejects += 1 
-                            elif char == "A" :
-                                self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size)))
-                                mediasizecount += 1
-                            elif char == "O" :
-                                self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size)))
-                                orientationcount += 1
-                            elif char == "M" :
-                                self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size)))
-                                mediatypecount += 1
-                        elif tag == "*r" :
-                            # Special tests for PCL3
-                            if (char == "s") and size :
-                                while 1 :
-                                    char = minfile[pos] ; pos += 1
-                                    if char == "A" :
-                                        break
-                            elif (char == "b") and (minfile[pos] == "C") and not size :
-                                ispcl3 = 1 # Certainely a PCL3 file
-                            startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx
-                            endgfx += (not size) and (char in ("C", "B")) # End Gfx
-                        elif tag == "*t" :    
-                            escstart += 1
-                        elif (tag == "&a") and (size == 2) :
-                            backsides += 1      # Back side in duplex mode
+                    if minfile[pos : pos+8] == r"%-12345X" :
+                        endpos = pos + 9
+                        while (minfile[endpos] not in endmark) and (minfile[endpos] < asciilimit) :
+                            endpos += 1
+                        self.setPageDict(pages, pagecount, "escaped", minfile[pos : endpos].replace('\r\n', '\n').split('\n'))
+                        self.logdebug("Escaped datas : [%s]" % repr(minfile[pos : endpos]))
+                        pos += (endpos - pos)
+                    else :
+                        starb = ampl = 0
+                        #
+                        #     <ESC>*b###y#m###v###w... -> PCL3 raster graphics
+                        #     <ESC>*b###W -> Start of a raster data row/block
+                        #     <ESC>*b###V -> Start of a raster data plane
+                        #     <ESC>*c###W -> Start of a user defined pattern
+                        #     <ESC>*i###W -> Start of a viewing illuminant block
+                        #     <ESC>*l###W -> Start of a color lookup table
+                        #     <ESC>*m###W -> Start of a download dither matrix block
+                        #     <ESC>*v###W -> Start of a configure image data block
+                        #     <ESC>*r1A -> Start Gfx 
+                        #     <ESC>(s###W -> Start of a characters description block
+                        #     <ESC>)s###W -> Start of a fonts description block
+                        #     <ESC>(f###W -> Start of a symbol set block
+                        #     <ESC>&b###W -> Start of configuration data block
+                        #     <ESC>&l###X -> Number of copies for current page
+                        #     <ESC>&n###W -> Starts an alphanumeric string ID block
+                        #     <ESC>&p###X -> Start of a non printable characters block
+                        #     <ESC>&a2G -> Back side when duplex mode as generated by rastertohp
+                        #     <ESC>*g###W -> Needed for planes in PCL3 output
+                        #     <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource
+                        #     <ESC>&l###A -> mediasize
+                        #     <ESC>&l###O -> orientation
+                        #     <ESC>&l###M -> mediatype
+                        #     <ESC>*t###R -> gfx resolution
+                        #
+                        tagstart = minfile[pos] ; pos += 1
+                        if tagstart in "E9=YZ" : # one byte PCL tag
+                            if tagstart == "E" :
+                                resets += 1
+                            continue             # skip to next tag
+                        tag = tagstart + minfile[pos] ; pos += 1
+                        if tag == "*b" : 
+                            starb = 1
+                            tagend = "VW"
+                        elif tag == "&l" :    
+                            ampl = 1
+                            tagend = "XHAOM"
                         else :    
-                            # we just ignore the block.
-                            if tag == "&n" : 
-                                # we have to take care of the operation id byte
-                                # which is before the string itself
-                                size += 1
-                            pos += size    
+                            try :
+                                tagend = tagsends[tag]
+                            except KeyError :    
+                                continue # Unsupported PCL tag
+                        # Now read the numeric argument
+                        size = 0
+                        while 1 :
+                            char = minfile[pos] ; pos += 1
+                            if not char.isdigit() :
+                                break
+                            size = (size * 10) + int(char)    
+                        if char in tagend :    
+                            if tag == "&l" :
+                                if char == "X" : 
+                                    self.setPageDict(pages, pagecount, "copies", size)
+                                elif char == "H" :
+                                    self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size)))
+                                    mediasourcecount += 1
+                                    ejects += 1 
+                                elif char == "A" :
+                                    self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size)))
+                                    mediasizecount += 1
+                                elif char == "O" :
+                                    self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size)))
+                                    orientationcount += 1
+                                elif char == "M" :
+                                    self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size)))
+                                    mediatypecount += 1
+                            elif tag == "*r" :
+                                # Special tests for PCL3
+                                if (char == "s") and size :
+                                    while 1 :
+                                        char = minfile[pos] ; pos += 1
+                                        if char == "A" :
+                                            break
+                                elif (char == "b") and (minfile[pos] == "C") and not size :
+                                    ispcl3 = 1 # Certainely a PCL3 file
+                                startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx
+                                endgfx += (not size) and (char in ("C", "B")) # End Gfx
+                            elif tag == "*t" :    
+                                escstart += 1
+                            elif (tag == "&a") and (size == 2) :
+                                backsides += 1      # Back side in duplex mode
+                            else :    
+                                # we just ignore the block.
+                                if tag == "&n" : 
+                                    # we have to take care of the operation id byte
+                                    # which is before the string itself
+                                    size += 1
+                                pos += size    
                 else :                            
                     if starb :
@@ -329,5 +338,6 @@
             pass        # should be OK.
         elif (not startgfx) and (not endgfx) :
-            pagecount = ejects or pagecount
+            if ejects :
+                pagecount = ejects
         elif startgfx == endgfx :    
             pagecount = startgfx
@@ -339,9 +349,38 @@
             pagecount = abs(startgfx - endgfx)
             
-        if self.debug :        
-            for pnum in range(pagecount) :
-                # if no number of copies defined, take the preceding one else the one set before any page else 1.
-                page = pages.get(pnum, pages.get(pnum - 1, pages.get(0, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait"})))
-                sys.stderr.write("%s*%s*%s*%s*%s\n" % (page["copies"], page["mediatype"], page["mediasize"], page["orientation"], page["mediasource"]))
+        pjlstatements = []
+        for pnum in range(pagecount) :
+            # if no number of copies defined, take the preceding one else the one set before any page else 1.
+            page = pages.get(pnum, pages.get(pnum - 1, pages.get(0, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : []})))
+            pjlstuff = page["escaped"]
+            if not pjlstuff :
+                pjlcopies = 1
+            else :    
+                pjlstatements.extend(pjlstuff)
+                newpjl = "\n".join(pjlstatements)
+                copiesstatement = newpjl.rfind("@PJL SET COPIES=")
+                qtystatement = newpjl.rfind("@PJL SET QTY=")
+                if copiesstatement > qtystatement :
+                    # we use the COPIES= statement
+                    try :
+                        pjlcopies = int(newpjl[copiesstatement+16:].split()[0].strip())
+                    except :    
+                        pjlcopies = 1
+                elif qtystatement > copiesstatement :    
+                    # we use the QTY= statement
+                    try :
+                        pjlcopies = int(newpjl[qtystatement+13:].split()[0].strip())
+                    except :    
+                        pjlcopies = 1
+                else :
+                    # both can't be equal unless they both equal -1 (not found)
+                    pjlcopies = 1
+            copies = pjlcopies * page["copies"]        
+            pagecount += (copies - 1)
+            self.logdebug("%s*%s*%s*%s*%s" % (copies, \
+                                              page["mediatype"], \
+                                              page["mediasize"], \
+                                              page["orientation"], \
+                                              page["mediasource"]))
                 
         return pagecount
