Changeset 545 for pkpgcounter/trunk/pkpgpdls/bj.py
- Timestamp:
- 12/09/07 13:01:15 (17 years ago)
- Files:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/bj.py
r527 r545 21 21 # 22 22 23 """This modules implements a page counter for DVIdocuments."""23 """This modules implements a page counter for Canon BJ documents.""" 24 24 25 25 import sys … … 31 31 32 32 class Parser(pdlparser.PDLParser) : 33 """A parser for DVI documents.""" 34 totiffcommands = [ 'dvips -q -o - "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r"%(dpi)i" -sOutputFile="%(outfname)s" -' ] 35 required = [ "dvips", "gs" ] 33 """A parser for Canon BJ documents.""" 36 34 def isValid(self) : 37 35 """Returns True if data is DVI, else False.""" 38 try : 39 if (ord(self.firstblock[0]) == 0xf7) \ 40 and (ord(self.lastblock[-1]) == 0xdf) : 41 self.logdebug("DEBUG: Input file is in the DVI format.") 42 return True 43 else : 44 return False 45 except IndexError : 36 if self.firstblock.startswith("\033[K\002\000") : 37 self.logdebug("DEBUG: Input file is in the Canon BJ format.") 38 return True 39 else : 46 40 return False 47 41 48 42 def getJobSize(self) : 49 """Counts pages in a DVIdocument.43 """Counts pages in a Canon BJ document. 50 44 51 45 Algorithm by Jerome Alet. … … 53 47 The documentation used for this was : 54 48 55 http://www.math.umd.edu/~asnowden/comp-cont/dvi.html49 ghostscript-8.60/src/gdevbj*.c 56 50 """ 57 51 infileno = self.infile.fileno() 58 52 minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) 59 53 pagecount = 0 60 pos = -1 61 eofchar = chr(0xdf) 62 postchar = chr(0xf8) 54 pos = 0 63 55 try : 64 56 try : 65 while minfile[pos] == eofchar : 66 pos -= 1 67 idbyte = minfile[pos] 68 if idbyte != minfile[1] : 69 raise IndexError, "Invalid DVI file." 70 pos = unpack(">I", minfile[pos - 4:pos])[0] 71 if minfile[pos] != postchar : 72 raise IndexError, "Invalid DVI file." 73 pagecount = unpack(">H", minfile[pos + 27: pos + 29])[0] 57 while True : 58 if minfile[pos] == "\033" : 59 # Look if we've found an initialization sequence 60 # through the Set Initial Condition command 61 pageheader = minfile[pos:pos+7] 62 if pageheader in ("\033[K\002\000\000\017", 63 "\033[K\002\000\000\044", 64 "\033[K\002\000\004\044") : 65 pagecount += 1 66 pos += 6 67 pos += 1 74 68 except IndexError : # EOF ? 75 69 pass