[3410] | 1 | # -*- coding: utf-8 -*- |
---|
[216] | 2 | # |
---|
| 3 | # pkpgcounter : a generic Page Description Language parser |
---|
| 4 | # |
---|
[564] | 5 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[463] | 6 | # This program is free software: you can redistribute it and/or modify |
---|
[216] | 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 |
---|
[216] | 9 | # (at your option) any later version. |
---|
[3436] | 10 | # |
---|
[216] | 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 | # |
---|
[216] | 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/>. |
---|
[216] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
| 21 | |
---|
[545] | 22 | """This modules implements a page counter for Canon BJ documents.""" |
---|
[356] | 23 | |
---|
[216] | 24 | import os |
---|
| 25 | import mmap |
---|
| 26 | |
---|
[235] | 27 | import pdlparser |
---|
[216] | 28 | |
---|
[220] | 29 | class Parser(pdlparser.PDLParser) : |
---|
[545] | 30 | """A parser for Canon BJ documents.""" |
---|
[555] | 31 | format = "Canon BJ/BJC" |
---|
[3436] | 32 | def isValid(self) : |
---|
[552] | 33 | """Returns True if data is BJ/BJC, else False.""" |
---|
[545] | 34 | if self.firstblock.startswith("\033[K\002\000") : |
---|
| 35 | return True |
---|
[3436] | 36 | else : |
---|
[387] | 37 | return False |
---|
[3436] | 38 | |
---|
[216] | 39 | def getJobSize(self) : |
---|
[545] | 40 | """Counts pages in a Canon BJ document. |
---|
[3436] | 41 | |
---|
[216] | 42 | Algorithm by Jerome Alet. |
---|
[3436] | 43 | |
---|
[216] | 44 | The documentation used for this was : |
---|
[3436] | 45 | |
---|
[545] | 46 | ghostscript-8.60/src/gdevbj*.c |
---|
[216] | 47 | """ |
---|
| 48 | infileno = self.infile.fileno() |
---|
| 49 | minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
| 50 | pagecount = 0 |
---|
[545] | 51 | pos = 0 |
---|
[216] | 52 | try : |
---|
[489] | 53 | try : |
---|
[545] | 54 | while True : |
---|
| 55 | if minfile[pos] == "\033" : |
---|
| 56 | # Look if we've found an initialization sequence |
---|
| 57 | # through the Set Initial Condition command |
---|
| 58 | pageheader = minfile[pos:pos+7] |
---|
[3436] | 59 | if pageheader in ("\033[K\002\000\000\017", |
---|
[545] | 60 | "\033[K\002\000\000\044", |
---|
| 61 | "\033[K\002\000\004\044") : |
---|
| 62 | pagecount += 1 |
---|
| 63 | pos += 6 |
---|
| 64 | pos += 1 |
---|
[489] | 65 | except IndexError : # EOF ? |
---|
| 66 | pass |
---|
[3436] | 67 | finally : |
---|
[489] | 68 | minfile.close() # reached EOF |
---|
[216] | 69 | return pagecount |
---|