[3410] | 1 | # -*- coding: utf-8 -*- |
---|
[191] | 2 | # |
---|
| 3 | # pkpgcounter : a generic Page Description Language parser |
---|
| 4 | # |
---|
[3474] | 5 | # (c) 2003-2009 Jerome Alet <alet@librelogiciel.com> |
---|
[463] | 6 | # This program is free software: you can redistribute it and/or modify |
---|
[191] | 7 | # it under the terms of the GNU General Public License as published by |
---|
[463] | 8 | # the Free Software Foundation, either version 3 of the License, or |
---|
[191] | 9 | # (at your option) any later version. |
---|
[3436] | 10 | # |
---|
[191] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
[3436] | 15 | # |
---|
[191] | 16 | # You should have received a copy of the GNU General Public License |
---|
[463] | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[191] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
[193] | 21 | |
---|
[357] | 22 | """This modules implements a page counter for ESC/P2 documents.""" |
---|
| 23 | |
---|
[193] | 24 | import sys |
---|
| 25 | |
---|
[235] | 26 | import pdlparser |
---|
[193] | 27 | |
---|
[220] | 28 | class Parser(pdlparser.PDLParser) : |
---|
[193] | 29 | """A parser for ESC/P2 documents.""" |
---|
[555] | 30 | format = "ESC/P2" |
---|
[3436] | 31 | def isValid(self) : |
---|
[387] | 32 | """Returns True if data is ESC/P2, else False.""" |
---|
[522] | 33 | if self.firstblock.startswith("\033@") or \ |
---|
| 34 | self.firstblock.startswith("\033*") or \ |
---|
| 35 | self.firstblock.startswith("\n\033@") or \ |
---|
| 36 | self.firstblock.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 1284 |
---|
[387] | 37 | return True |
---|
[3436] | 38 | else : |
---|
[387] | 39 | return False |
---|
[3436] | 40 | |
---|
| 41 | def getJobSize(self) : |
---|
[193] | 42 | """Counts pages in an ESC/P2 document.""" |
---|
| 43 | # with Gimpprint, at least, for each page there |
---|
| 44 | # are two Reset Printer sequences (ESC + @) |
---|
| 45 | marker1 = "\033@" |
---|
[3436] | 46 | |
---|
[193] | 47 | # with other software or printer driver, we |
---|
| 48 | # may prefer to search for "\r\n\fESCAPE" |
---|
| 49 | # or "\r\fESCAPE" |
---|
| 50 | marker2r = "\r\f\033" |
---|
| 51 | marker2rn = "\r\n\f\033" |
---|
[3436] | 52 | |
---|
[193] | 53 | # and ghostscript's stcolor for example seems to |
---|
| 54 | # output ESC + @ + \f for each page plus one |
---|
| 55 | marker3 = "\033@\f" |
---|
[3436] | 56 | |
---|
[193] | 57 | # while ghostscript's escp driver outputs instead |
---|
| 58 | # \f + ESC + @ |
---|
| 59 | marker4 = "\f\033@" |
---|
[3436] | 60 | |
---|
[193] | 61 | data = self.infile.read() |
---|
| 62 | pagecount1 = data.count(marker1) |
---|
| 63 | pagecount2 = max(data.count(marker2r), data.count(marker2rn)) |
---|
| 64 | pagecount3 = data.count(marker3) |
---|
| 65 | pagecount4 = data.count(marker4) |
---|
[3436] | 66 | |
---|
| 67 | if pagecount2 : |
---|
[193] | 68 | return pagecount2 |
---|
[3436] | 69 | elif pagecount3 > 1 : |
---|
[193] | 70 | return pagecount3 - 1 |
---|
[3436] | 71 | elif pagecount4 : |
---|
[193] | 72 | return pagecount4 |
---|
[3436] | 73 | else : |
---|
| 74 | return int(pagecount1 / 2) |
---|