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
RevLine 
[216]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
[443]6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[463]7# This program is free software: you can redistribute it and/or modify
[216]8# it under the terms of the GNU General Public License as published by
[463]9# the Free Software Foundation, either version 3 of the License, or
[216]10# (at your option) any later version.
[463]11#
[216]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
[463]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[216]19#
20# $Id$
21#
22
[545]23"""This modules implements a page counter for Canon BJ documents."""
[356]24
[216]25import sys
26import os
27import mmap
28from struct import unpack
29
[235]30import pdlparser
[216]31
[220]32class Parser(pdlparser.PDLParser) :
[545]33    """A parser for Canon BJ documents."""
[220]34    def isValid(self) :       
[387]35        """Returns True if data is DVI, else False."""
[545]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 :   
[387]40            return False
[220]41           
[216]42    def getJobSize(self) :
[545]43        """Counts pages in a Canon BJ document.
[216]44       
45           Algorithm by Jerome Alet.
46           
47           The documentation used for this was :
48         
[545]49           ghostscript-8.60/src/gdevbj*.c
[216]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
[545]54        pos = 0
[216]55        try :
[489]56            try :
[545]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
[489]68            except IndexError : # EOF ?
69                pass
70        finally :       
71            minfile.close() # reached EOF
[216]72        return pagecount
Note: See TracBrowser for help on using the browser.