root / pkpgcounter / trunk / pkpgpdls / hbp.py @ 3488

Revision 3474, 2.4 kB (checked in by jerome, 16 years ago)

Changed copyright years.

  • Property svn:keywords set to Author Id Date Revision
Line 
1# -*- coding: utf-8 -*-
2#
3# pkpgcounter : a generic Page Description Language parser
4#
5# (c) 2003-2009 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 Brother HBP documents."""
23
24import sys
25import os
26import mmap
27from struct import unpack
28
29import pdlparser
30
31class Parser(pdlparser.PDLParser) :
32    """A parser for HBP documents."""
33    format = "Brother HBP"
34    def isValid(self) :
35        """Returns True if data is HBP, else False."""
36        if self.firstblock.find("@PJL ENTER LANGUAGE = HBP\n") != -1 :
37            return True
38        else :
39            return False
40
41    def getJobSize(self) :
42        """Counts pages in a HBP document.
43
44           Algorithm by Jerome Alet.
45
46           The documentation used for this was :
47
48           http://sf.net/projects/hbp-for-brother/
49
50           IMPORTANT : this may not work since @F should be sufficient,
51           but the documentation really is unclear and I don't know
52           how to skip raster data blocks for now.
53        """
54        infileno = self.infile.fileno()
55        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
56        pagecount = 0
57
58        formfeed = "@G" + chr(0) + chr(0) + chr(1) + chr(0xff) + "@F"
59        fflen = len(formfeed)
60        pos = 0
61        try :
62            try :
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
70            except IndexError : # EOF ?
71                pass
72        finally :
73            minfile.close() # reached EOF
74        return pagecount
Note: See TracBrowser for help on using the browser.