Show
Ignore:
Timestamp:
01/20/06 20:47:05 (18 years ago)
Author:
jerome
Message:

Added preliminary support for Canon ImageRunner?'s "extension" to PCL.

Location:
pkpgcounter/trunk/pkpgpdls
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • pkpgcounter/trunk/pkpgpdls/pcl345.py

    r303 r305  
    2525import os 
    2626import mmap 
     27from struct import unpack 
    2728 
    2829import pdlparser 
     
    8485           (self.firstblock.startswith("\033*rbC") and (not self.lastblock[-3:] == "\f\033@")) or \ 
    8586           self.firstblock.startswith("\033%8\033") or \ 
    86            (self.firstblock.find("\033%-12345X") != -1) : 
     87           (self.firstblock.find("\033%-12345X") != -1) or \ 
     88           (self.firstblock.startswith(chr(0xcd)+chr(0xca)) and self.firstblock.find("\033E\033")) : 
    8789            self.logdebug("DEBUG: Input file is in the PCL3/4/5 format.") 
    8890            return 1 
     
    133135                     # "*b" : "VW", # treated specially because it occurs very often 
    134136                   }   
     137        irmarker = chr(0xcd) + chr(0xca) # Marker for Canon ImageRunner printers 
     138        irmarker2 = chr(0x10) + chr(0x02) 
     139        wasirmarker = 0 
     140        hasirmarker = (minfile[:2] == (irmarker)) 
    135141        pagecount = resets = ejects = backsides = startgfx = endgfx = 0 
    136142        starb = ampl = ispcl3 = escstart = 0 
     
    143149        try : 
    144150            while 1 : 
    145                 char = minfile[pos] ; pos += 1 
    146                 if char == "\014" :     
    147                     pagecount += 1 
    148                 elif char == "\033" :     
    149                     starb = ampl = 0 
    150                     if minfile[pos : pos+8] == r"%-12345X" : 
    151                         endpos = pos + 9 
    152                         quotes = 0 
    153                         while (minfile[endpos] not in endmark) and \ 
    154                               ((minfile[endpos] < asciilimit) or (quotes % 2)) : 
    155                             if minfile[endpos] == '"' : 
    156                                 quotes += 1 
    157                             endpos += 1 
    158                         self.setPageDict(pages, pagecount, "escaped", minfile[pos : endpos]) 
    159                         pos += (endpos - pos) 
    160                     else : 
    161                         # 
    162                         #     <ESC>*b###y#m###v###w... -> PCL3 raster graphics 
    163                         #     <ESC>*b###W -> Start of a raster data row/block 
    164                         #     <ESC>*b###V -> Start of a raster data plane 
    165                         #     <ESC>*c###W -> Start of a user defined pattern 
    166                         #     <ESC>*i###W -> Start of a viewing illuminant block 
    167                         #     <ESC>*l###W -> Start of a color lookup table 
    168                         #     <ESC>*m###W -> Start of a download dither matrix block 
    169                         #     <ESC>*v###W -> Start of a configure image data block 
    170                         #     <ESC>*r1A -> Start Gfx  
    171                         #     <ESC>(s###W -> Start of a characters description block 
    172                         #     <ESC>)s###W -> Start of a fonts description block 
    173                         #     <ESC>(f###W -> Start of a symbol set block 
    174                         #     <ESC>&b###W -> Start of configuration data block 
    175                         #     <ESC>&l###X -> Number of copies for current page 
    176                         #     <ESC>&n###W -> Starts an alphanumeric string ID block 
    177                         #     <ESC>&p###X -> Start of a non printable characters block 
    178                         #     <ESC>&a2G -> Back side when duplex mode as generated by rastertohp 
    179                         #     <ESC>*g###W -> Needed for planes in PCL3 output 
    180                         #     <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource 
    181                         #     <ESC>&l###A -> mediasize 
    182                         #     <ESC>&l###O -> orientation 
    183                         #     <ESC>&l###M -> mediatype 
    184                         #     <ESC>*t###R -> gfx resolution 
    185                         # 
    186                         tagstart = minfile[pos] ; pos += 1 
    187                         if tagstart in "E9=YZ" : # one byte PCL tag 
    188                             if tagstart == "E" : 
    189                                 resets += 1 
    190                             continue             # skip to next tag 
    191                         tag = tagstart + minfile[pos] ; pos += 1 
    192                         if tag == "*b" :  
    193                             starb = 1 
    194                             tagend = "VW" 
    195                         elif tag == "&l" :     
    196                             ampl = 1 
    197                             tagend = "XHAOM" 
    198                         else :     
    199                             try : 
    200                                 tagend = tagsends[tag] 
    201                             except KeyError :     
    202                                 continue # Unsupported PCL tag 
    203                         # Now read the numeric argument 
    204                         size = 0 
    205                         while 1 : 
    206                             char = minfile[pos] ; pos += 1 
    207                             if not char.isdigit() : 
    208                                 break 
    209                             size = (size * 10) + int(char)     
    210                         if char in tagend :     
    211                             if tag == "&l" : 
    212                                 if char == "X" :  
    213                                     self.setPageDict(pages, pagecount, "copies", size) 
    214                                 elif char == "H" : 
     151                if hasirmarker and (minfile[pos:pos+2] == irmarker) : 
     152                    codop = minfile[pos+2:pos+4] 
     153                    # self.logdebug("Marker at 0x%08x     (%s)" % (pos, wasirmarker)) 
     154                    length = unpack(">H", minfile[pos+8:pos+10])[0] 
     155                    pos += 20 
     156                    if codop != irmarker2 : 
     157                        pos += length 
     158                    wasirmarker = 1     
     159                else :         
     160                    wasirmarker = 0 
     161                    char = minfile[pos] ; pos += 1 
     162                    if char == "\014" :     
     163                        pagecount += 1 
     164                    elif char == "\033" :     
     165                        starb = ampl = 0 
     166                        if minfile[pos : pos+8] == r"%-12345X" : 
     167                            endpos = pos + 9 
     168                            quotes = 0 
     169                            while (minfile[endpos] not in endmark) and \ 
     170                                  ((minfile[endpos] < asciilimit) or (quotes % 2)) : 
     171                                if minfile[endpos] == '"' : 
     172                                    quotes += 1 
     173                                endpos += 1 
     174                            self.setPageDict(pages, pagecount, "escaped", minfile[pos : endpos]) 
     175                            pos += (endpos - pos) 
     176                        else : 
     177                            # 
     178                            #     <ESC>*b###y#m###v###w... -> PCL3 raster graphics 
     179                            #     <ESC>*b###W -> Start of a raster data row/block 
     180                            #     <ESC>*b###V -> Start of a raster data plane 
     181                            #     <ESC>*c###W -> Start of a user defined pattern 
     182                            #     <ESC>*i###W -> Start of a viewing illuminant block 
     183                            #     <ESC>*l###W -> Start of a color lookup table 
     184                            #     <ESC>*m###W -> Start of a download dither matrix block 
     185                            #     <ESC>*v###W -> Start of a configure image data block 
     186                            #     <ESC>*r1A -> Start Gfx  
     187                            #     <ESC>(s###W -> Start of a characters description block 
     188                            #     <ESC>)s###W -> Start of a fonts description block 
     189                            #     <ESC>(f###W -> Start of a symbol set block 
     190                            #     <ESC>&b###W -> Start of configuration data block 
     191                            #     <ESC>&l###X -> Number of copies for current page 
     192                            #     <ESC>&n###W -> Starts an alphanumeric string ID block 
     193                            #     <ESC>&p###X -> Start of a non printable characters block 
     194                            #     <ESC>&a2G -> Back side when duplex mode as generated by rastertohp 
     195                            #     <ESC>*g###W -> Needed for planes in PCL3 output 
     196                            #     <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource 
     197                            #     <ESC>&l###A -> mediasize 
     198                            #     <ESC>&l###O -> orientation 
     199                            #     <ESC>&l###M -> mediatype 
     200                            #     <ESC>*t###R -> gfx resolution 
     201                            # 
     202                            tagstart = minfile[pos] ; pos += 1 
     203                            if tagstart in "E9=YZ" : # one byte PCL tag 
     204                                if tagstart == "E" : 
     205                                    resets += 1 
     206                                continue             # skip to next tag 
     207                            tag = tagstart + minfile[pos] ; pos += 1 
     208                            if tag == "*b" :  
     209                                starb = 1 
     210                                tagend = "VW" 
     211                            elif tag == "&l" :     
     212                                ampl = 1 
     213                                tagend = "XHAOM" 
     214                            else :     
     215                                try : 
     216                                    tagend = tagsends[tag] 
     217                                except KeyError :     
     218                                    continue # Unsupported PCL tag 
     219                            # Now read the numeric argument 
     220                            size = 0 
     221                            while 1 : 
     222                                char = minfile[pos] ; pos += 1 
     223                                if not char.isdigit() : 
     224                                    break 
     225                                size = (size * 10) + int(char)     
     226                            if char in tagend :     
     227                                if tag == "&l" : 
     228                                    if char == "X" :  
     229                                        self.setPageDict(pages, pagecount, "copies", size) 
     230                                    elif char == "H" : 
     231                                        self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 
     232                                        mediasourcecount += 1 
     233                                        ejects += 1  
     234                                    elif char == "A" : 
     235                                        self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 
     236                                        mediasizecount += 1 
     237                                    elif char == "O" : 
     238                                        self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 
     239                                        orientationcount += 1 
     240                                    elif char == "M" : 
     241                                        self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 
     242                                        mediatypecount += 1 
     243                                elif tag == "*r" : 
     244                                    # Special tests for PCL3 
     245                                    if (char == "s") and size : 
     246                                        while 1 : 
     247                                            char = minfile[pos] ; pos += 1 
     248                                            if char == "A" : 
     249                                                break 
     250                                    elif (char == "b") and (minfile[pos] == "C") and not size : 
     251                                        ispcl3 = 1 # Certainely a PCL3 file 
     252                                    startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx 
     253                                    endgfx += (not size) and (char in ("C", "B")) # End Gfx 
     254                                elif tag == "*t" :     
     255                                    escstart += 1 
     256                                elif (tag == "&a") and (size == 2) : 
     257                                    # We are on the backside, so mark current page as duplex 
     258                                    self.setPageDict(pages, pagecount, "duplex", 1) 
     259                                    backsides += 1      # Back side in duplex mode 
     260                                else :     
     261                                    # we just ignore the block. 
     262                                    if tag == "&n" :  
     263                                        # we have to take care of the operation id byte 
     264                                        # which is before the string itself 
     265                                        size += 1 
     266                                    pos += size     
     267                    else :                             
     268                        if starb : 
     269                            # special handling of PCL3 in which  
     270                            # *b introduces combined ESCape sequences 
     271                            size = 0 
     272                            while 1 : 
     273                                char = minfile[pos] ; pos += 1 
     274                                if not char.isdigit() : 
     275                                    break 
     276                                size = (size * 10) + int(char)     
     277                            if char in ("w", "v") :     
     278                                ispcl3 = 1  # certainely a PCL3 document 
     279                                pos += size - 1 
     280                            elif char in ("y", "m") :     
     281                                ispcl3 = 1  # certainely a PCL3 document 
     282                                pos -= 1    # fix position : we were ahead 
     283                        elif ampl :         
     284                            # special handling of PCL3 in which  
     285                            # &l introduces combined ESCape sequences 
     286                            size = 0 
     287                            while 1 : 
     288                                char = minfile[pos] ; pos += 1 
     289                                if not char.isdigit() : 
     290                                    break 
     291                                size = (size * 10) + int(char)     
     292                            if char in ("a", "o", "h", "m") :     
     293                                ispcl3 = 1  # certainely a PCL3 document 
     294                                pos -= 1    # fix position : we were ahead 
     295                                if char == "h" : 
    215296                                    self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 
    216297                                    mediasourcecount += 1 
    217                                     ejects += 1  
    218                                 elif char == "A" : 
     298                                elif char == "a" : 
    219299                                    self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 
    220300                                    mediasizecount += 1 
    221                                 elif char == "O" : 
     301                                elif char == "o" : 
    222302                                    self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 
    223303                                    orientationcount += 1 
    224                                 elif char == "M" : 
     304                                elif char == "m" : 
    225305                                    self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 
    226306                                    mediatypecount += 1 
    227                             elif tag == "*r" : 
    228                                 # Special tests for PCL3 
    229                                 if (char == "s") and size : 
    230                                     while 1 : 
    231                                         char = minfile[pos] ; pos += 1 
    232                                         if char == "A" : 
    233                                             break 
    234                                 elif (char == "b") and (minfile[pos] == "C") and not size : 
    235                                     ispcl3 = 1 # Certainely a PCL3 file 
    236                                 startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx 
    237                                 endgfx += (not size) and (char in ("C", "B")) # End Gfx 
    238                             elif tag == "*t" :     
    239                                 escstart += 1 
    240                             elif (tag == "&a") and (size == 2) : 
    241                                 # We are on the backside, so mark current page as duplex 
    242                                 self.setPageDict(pages, pagecount, "duplex", 1) 
    243                                 backsides += 1      # Back side in duplex mode 
    244                             else :     
    245                                 # we just ignore the block. 
    246                                 if tag == "&n" :  
    247                                     # we have to take care of the operation id byte 
    248                                     # which is before the string itself 
    249                                     size += 1 
    250                                 pos += size     
    251                 else :                             
    252                     if starb : 
    253                         # special handling of PCL3 in which  
    254                         # *b introduces combined ESCape sequences 
    255                         size = 0 
    256                         while 1 : 
    257                             char = minfile[pos] ; pos += 1 
    258                             if not char.isdigit() : 
    259                                 break 
    260                             size = (size * 10) + int(char)     
    261                         if char in ("w", "v") :     
    262                             ispcl3 = 1  # certainely a PCL3 document 
    263                             pos += size - 1 
    264                         elif char in ("y", "m") :     
    265                             ispcl3 = 1  # certainely a PCL3 document 
    266                             pos -= 1    # fix position : we were ahead 
    267                     elif ampl :         
    268                         # special handling of PCL3 in which  
    269                         # &l introduces combined ESCape sequences 
    270                         size = 0 
    271                         while 1 : 
    272                             char = minfile[pos] ; pos += 1 
    273                             if not char.isdigit() : 
    274                                 break 
    275                             size = (size * 10) + int(char)     
    276                         if char in ("a", "o", "h", "m") :     
    277                             ispcl3 = 1  # certainely a PCL3 document 
    278                             pos -= 1    # fix position : we were ahead 
    279                             if char == "h" : 
    280                                 self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 
    281                                 mediasourcecount += 1 
    282                             elif char == "a" : 
    283                                 self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 
    284                                 mediasizecount += 1 
    285                             elif char == "o" : 
    286                                 self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 
    287                                 orientationcount += 1 
    288                             elif char == "m" : 
    289                                 self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 
    290                                 mediatypecount += 1 
    291307        except IndexError : # EOF ? 
    292308            minfile.close() # reached EOF 
     
    314330            sys.stderr.write("mediatypecount : %s\n" % mediatypecount) 
    315331            sys.stderr.write("escstart : %s\n" % escstart) 
     332            sys.stderr.write("hasirmarker : %s\n" % hasirmarker) 
    316333         
    317         if (orientationcount == (pagecount - 1)) and (resets == 1) : 
     334        if hasirmarker : 
     335            self.logdebug("Rule #20 (probably a Canon ImageRunner)") 
     336            pagecount += 1 
     337        elif (orientationcount == (pagecount - 1)) and (resets == 1) : 
    318338            if resets == ejects == startgfx == mediasourcecount == escstart == 1 : 
    319339                self.logdebug("Rule #19") 
  • pkpgcounter/trunk/pkpgpdls/version.py

    r303 r305  
    2020# 
    2121 
    22 __version__ = "1.74" 
     22__version__ = "1.75" 
    2323 
    2424__doc__ = """pkpgcounter : a generic Page Description Languages parser."""