Index: pkpgcounter/trunk/pkpgpdls/newpcl345.py
===================================================================
--- pkpgcounter/trunk/pkpgpdls/newpcl345.py (revision 395)
+++ pkpgcounter/trunk/pkpgpdls/newpcl345.py (revision 396)
@@ -32,6 +32,8 @@
 import pjl
 
-FORMFEED = chr(12)
-ESCAPE = chr(27)
+NUL = chr(0x00)
+FORMFEED = chr(0x0c)
+ESCAPE = chr(0x1b)
+ASCIILIMIT = chr(0x80)
 
 class Parser(pdlparser.PDLParser) :
@@ -99,7 +101,7 @@
             return False
         
-    def setPageDict(self, pages, number, attribute, value) :
+    def setPageDict(self, attribute, value) :
         """Initializes a page dictionnary."""
-        dic = pages.setdefault(number, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : "", "duplex": 0})
+        dic = self.pages.setdefault(self.pagecount, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : "", "duplex": 0})
         dic[attribute] = value
         
@@ -107,5 +109,11 @@
         """Reads a byte from the input stream."""
         tag = ord(self.minfile[self.pos])
-        #self.logdebug("%08x  ===>  %02x" % (self.pos, tag))
+        if tag == 0x0c :
+            self.logdebug("%08x ====> %02x %02x %02x %02x" %
+                 (self.pos,
+                 ord(self.minfile[self.pos-2]),
+                 ord(self.minfile[self.pos-1]),
+                 ord(self.minfile[self.pos-0]),
+                 ord(self.minfile[self.pos+1])))
         self.pos += 1
         return tag
@@ -113,5 +121,5 @@
     def endPage(self) :    
         """Handle the FF marker."""
-        self.logdebug("FORMFEED %i" % self.pagecount)
+        self.logdebug("FORMFEED %i at %08x" % (self.pagecount, self.pos-1))
         self.pagecount += 1
         
@@ -119,6 +127,39 @@
         """Handles the ESC% sequence."""
         if self.minfile[self.pos : self.pos+7] == r"-12345X" :
-            self.logdebug("Generic ESCAPE sequence at %08x" % self.pos)
+            #self.logdebug("Generic ESCAPE sequence at %08x" % self.pos)
             self.pos += 7
+            buffer = []
+            quotes = 0
+            char = chr(self.readByte())
+            while ((char < ASCIILIMIT) or (quotes % 2)) and (char not in (FORMFEED, ESCAPE, NUL)) :  
+                buffer.append(char)
+                if char == '"' :
+                    quotes += 1
+                char = chr(self.readByte())
+            self.setPageDict("escaped", "".join(buffer))
+            #self.logdebug("ESCAPED : %s" % "".join(buffer))
+            self.pos -= 1   # Adjust position
+        else :    
+            while 1 :
+                (value, end) = self.getInteger()
+                if end == 'B' :
+                    self.enterHPGL2()
+                    while self.minfile[self.pos] != ESCAPE :
+                        self.pos += 1
+                    self.pos -= 1    
+                    return 
+                elif end == 'A' :    
+                    self.exitHPGL2()
+                    return
+        
+    def enterHPGL2(self) :    
+        """Enters HPGL2 mode."""
+        self.logdebug("ENTERHPGL2 %08x" % self.pos)
+        self.hpgl2 = True
+        
+    def exitHPGL2(self) :    
+        """Exits HPGL2 mode."""
+        self.logdebug("EXITHPGL2 %08x" % self.pos)
+        self.hpgl2 = False
         
     def handleTag(self, tagtable) :    
@@ -165,20 +206,25 @@
                 mediasource = self.mediasources.get(value, str(value))
                 self.mediasourcesvalues.append(mediasource)
-                self.logdebug("MEDIASOURCE %s" % mediasource)
+                self.setPageDict("mediasource", mediasource)
+                #self.logdebug("MEDIASOURCE %s" % mediasource)
             elif end in ('a', 'A') :
                 mediasize = self.mediasizes.get(value, str(value))
                 self.mediasizesvalues.append(mediasize)
-                self.logdebug("MEDIASIZE %s" % mediasize)
+                self.setPageDict("mediasize", mediasize)
+                #self.logdebug("MEDIASIZE %s" % mediasize)
             elif end in ('o', 'O') :
                 orientation = self.orientations.get(value, str(value))
                 self.orientationsvalues.append(orientation)
-                self.logdebug("ORIENTATION %s" % orientation)
+                self.setPageDict("orientation", orientation)
+                #self.logdebug("ORIENTATION %s" % orientation)
             elif end in ('m', 'M') :
                 mediatype = self.mediatypes.get(value, str(value))
                 self.mediatypesvalues.append(mediatype)
-                self.logdebug("MEDIATYPE %s" % mediatype)
+                self.setPageDict("mediatype", mediatype)
+                #self.logdebug("MEDIATYPE %s" % mediatype)
             elif end == 'X' :
                 self.copies.append(value)
-                self.logdebug("COPIES %i" % value)
+                self.setPageDict("copies", value)
+                #self.logdebug("COPIES %i" % value)
                 
     def escAmpa(self) :    
@@ -189,6 +235,7 @@
                 return
             if end == 'G' :    
-                self.logdebug("BACKSIDES %i" % value)
+                #self.logdebug("BACKSIDES %i" % value)
                 self.backsides.append(value)
+                self.setPageDict("duplex", value)
                 
     def escAmpb(self) :    
@@ -272,8 +319,9 @@
                 elif end in ('B', 'C') :        
                     #self.logdebug("EndGFX")
-                    if not self.startgfx :
-                        self.logdebug("EndGFX found before StartGFX, ignored.")
+                    if self.startgfx :
+                        self.endgfx.append(1)
                     else :    
-                        self.endgfx.append(1)
+                        #self.logdebug("EndGFX found before StartGFX, ignored.")
+                        pass
             if end == 'A' and (0 <= value <= 3) :
                 #self.logdebug("StartGFX %i" % value)
@@ -303,5 +351,5 @@
         while 1 :
             char = chr(self.readByte())
-            if char in (ESCAPE, FORMFEED) :
+            if char in (NUL, ESCAPE, FORMFEED, ASCIILIMIT) :
                 self.pos -= 1 # Adjust position
                 return (None, None)
@@ -315,4 +363,9 @@
             else :    
                 value = ((value or 0) * 10) + int(char)    
+        
+    def skipByte(self) :    
+        """Skips a byte."""
+        #self.logdebug("SKIPBYTE %08x ===> %02x" % (self.pos, ord(self.minfile[self.pos])))
+        self.pos += 1
         
     def getJobSize(self) :     
@@ -336,4 +389,5 @@
         infileno = self.infile.fileno()
         self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
+        self.pages = {}
         self.pagecount = 0
         self.resets = 0
@@ -346,8 +400,10 @@
         self.startgfx = []
         self.endgfx = []
+        self.hpgl2 = False
         
         tags = [ lambda : None] * 256
         tags[ord(FORMFEED)] = self.endPage
         tags[ord(ESCAPE)] = self.escape
+        tags[ord(ASCIILIMIT)] = self.skipByte
         
         self.esctags = [ lambda : None ] * 256
