root / pkpgcounter / trunk / pkpgpdls / bj.py @ 545

Revision 545, 2.5 kB (checked in by jerome, 16 years ago)

Added support for Canon BJ/BJC file format in page counting mode.

  • Property svn:keywords set to Author Id Date Revision
Line 
1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20# $Id$
21#
22
23"""This modules implements a page counter for Canon BJ documents."""
24
25import sys
26import os
27import mmap
28from struct import unpack
29
30import pdlparser
31
32class Parser(pdlparser.PDLParser) :
33    """A parser for Canon BJ documents."""
34    def isValid(self) :       
35        """Returns True if data is DVI, else False."""
36        if self.firstblock.startswith("\033[K\002\000") :
37            self.logdebug("DEBUG: Input file is in the Canon BJ format.")
38            return True
39        else :   
40            return False
41           
42    def getJobSize(self) :
43        """Counts pages in a Canon BJ document.
44       
45           Algorithm by Jerome Alet.
46           
47           The documentation used for this was :
48         
49           ghostscript-8.60/src/gdevbj*.c
50        """
51        infileno = self.infile.fileno()
52        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
53        pagecount = 0
54        pos = 0
55        try :
56            try :
57                while True :
58                    if minfile[pos] == "\033" :
59                        # Look if we've found an initialization sequence
60                        # through the Set Initial Condition command
61                        pageheader = minfile[pos:pos+7]
62                        if pageheader in ("\033[K\002\000\000\017", 
63                                          "\033[K\002\000\000\044",
64                                          "\033[K\002\000\004\044") :
65                            pagecount += 1
66                            pos += 6
67                    pos += 1
68            except IndexError : # EOF ?
69                pass
70        finally :       
71            minfile.close() # reached EOF
72        return pagecount
Note: See TracBrowser for help on using the browser.