Changeset 480 for pkpgcounter/trunk/pkpgpdls/hbp.py
- Timestamp:
- 10/01/07 23:24:10 (17 years ago)
- Files:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/hbp.py
r463 r480 21 21 # 22 22 23 """This modules implements a page counter for DVIdocuments."""23 """This modules implements a page counter for Brother HBP documents.""" 24 24 25 25 import sys … … 31 31 32 32 class Parser(pdlparser.PDLParser) : 33 """A parser for DVI documents.""" 34 totiffcommands = [ 'cat >%(fname)s && dvips -q -o - %(fname)s | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(fname)s" -' ] 33 """A parser for HBP documents.""" 35 34 def isValid(self) : 36 """Returns True if data is DVI, else False.""" 37 try : 38 if (ord(self.firstblock[0]) == 0xf7) and (ord(self.lastblock[-1]) == 0xdf) : 39 self.logdebug("DEBUG: Input file is in the DVI format.") 40 return True 41 else : 42 return False 43 except IndexError : 35 """Returns True if data is HBP, else False.""" 36 if self.firstblock.find("@PJL ENTER LANGUAGE = HBP\n") != -1 : 37 self.logdebug("DEBUG: Input file is in the HBP format.") 38 return True 39 else : 44 40 return False 45 41 46 42 def getJobSize(self) : 47 """Counts pages in a DVIdocument.43 """Counts pages in a HBP document. 48 44 49 45 Algorithm by Jerome Alet. … … 51 47 The documentation used for this was : 52 48 53 http://www.math.umd.edu/~asnowden/comp-cont/dvi.html 49 http://sf.net/projects/hbp-for-brother/ 50 51 IMPORTANT : this may not work since @F should be sufficient, 52 but the documentation really is unclear and I don't know 53 how to skip raster data blocks for now. 54 54 """ 55 55 infileno = self.infile.fileno() 56 56 minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) 57 57 pagecount = 0 58 pos = -1 59 eofchar = chr(0xdf) 60 postchar = chr(0xf8) 58 59 formfeed = "@G" + chr(0) + chr(0) + chr(1) + chr(0xff) + "@F" 60 fflen = len(formfeed) 61 pos = 0 61 62 try : 62 while minfile[pos] == eofchar : 63 pos -= 1 64 idbyte = minfile[pos] 65 if idbyte != minfile[1] : 66 raise IndexError, "Invalid DVI file." 67 pos = unpack(">I", minfile[pos - 4:pos])[0] 68 if minfile[pos] != postchar : 69 raise IndexError, "Invalid DVI file." 70 pagecount = unpack(">H", minfile[pos + 27: pos + 29])[0] 63 while True : 64 if (minfile[pos] == "@") \ 65 and (minfile[pos:pos+fflen] == formfeed) : 66 pagecount += 1 67 pos += fflen 68 else : 69 pos += 1 71 70 except IndexError : # EOF ? 72 71 pass