| 23 | |
| 24 | import sys |
| 25 | |
| 26 | from pdlanalyzer.pdlparser import PDLParser |
| 27 | |
| 28 | class ESCP2Parser(PDLParser) : |
| 29 | """A parser for ESC/P2 documents.""" |
| 30 | def getJobSize(self) : |
| 31 | """Counts pages in an ESC/P2 document.""" |
| 32 | # with Gimpprint, at least, for each page there |
| 33 | # are two Reset Printer sequences (ESC + @) |
| 34 | marker1 = "\033@" |
| 35 | |
| 36 | # with other software or printer driver, we |
| 37 | # may prefer to search for "\r\n\fESCAPE" |
| 38 | # or "\r\fESCAPE" |
| 39 | marker2r = "\r\f\033" |
| 40 | marker2rn = "\r\n\f\033" |
| 41 | |
| 42 | # and ghostscript's stcolor for example seems to |
| 43 | # output ESC + @ + \f for each page plus one |
| 44 | marker3 = "\033@\f" |
| 45 | |
| 46 | # while ghostscript's escp driver outputs instead |
| 47 | # \f + ESC + @ |
| 48 | marker4 = "\f\033@" |
| 49 | |
| 50 | data = self.infile.read() |
| 51 | pagecount1 = data.count(marker1) |
| 52 | pagecount2 = max(data.count(marker2r), data.count(marker2rn)) |
| 53 | pagecount3 = data.count(marker3) |
| 54 | pagecount4 = data.count(marker4) |
| 55 | |
| 56 | if pagecount2 : |
| 57 | return pagecount2 |
| 58 | elif pagecount3 > 1 : |
| 59 | return pagecount3 - 1 |
| 60 | elif pagecount4 : |
| 61 | return pagecount4 |
| 62 | else : |
| 63 | return int(pagecount1 / 2) |
| 64 | |
| 65 | def test() : |
| 66 | """Test function.""" |
| 67 | raise RuntimeError, "Not implemented !" |
| 68 | |
| 69 | if __name__ == "__main__" : |
| 70 | test() |