root / pkpgcounter / trunk / pkpgpdls / escp2.py @ 1978

Revision 564, 2.6 kB (checked in by jerome, 17 years ago)

Changed copyright years.
Removed unnecessary shebang lines.
Changed default encoding to UTF-8 from ISO-8859-15 (only
ascii is used anyway).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Auth Date Id Rev
Line 
1# -*- coding: UTF-8 -*-
2#
3# pkpgcounter : a generic Page Description Language parser
4#
5# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
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.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19# $Id$
20#
21
22"""This modules implements a page counter for ESC/P2 documents."""
23
24import sys
25
26import pdlparser
27
28class Parser(pdlparser.PDLParser) :
29    """A parser for ESC/P2 documents."""
30    format = "ESC/P2"
31    def isValid(self) :       
32        """Returns True if data is ESC/P2, else False."""
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
37            return True
38        else :   
39            return False
40           
41    def getJobSize(self) :   
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@"
46       
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"
52       
53        # and ghostscript's stcolor for example seems to
54        # output ESC + @ + \f for each page plus one
55        marker3 = "\033@\f"
56       
57        # while ghostscript's escp driver outputs instead
58        # \f + ESC + @
59        marker4 = "\f\033@"
60       
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)
66           
67        if pagecount2 :   
68            return pagecount2
69        elif pagecount3 > 1 :     
70            return pagecount3 - 1
71        elif pagecount4 :   
72            return pagecount4
73        else :   
74            return int(pagecount1 / 2)       
Note: See TracBrowser for help on using the browser.