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 Canon BJ documents.""" |
---|
23 | |
---|
24 | import os |
---|
25 | import mmap |
---|
26 | |
---|
27 | import pdlparser |
---|
28 | |
---|
29 | class Parser(pdlparser.PDLParser) : |
---|
30 | """A parser for Canon BJ documents.""" |
---|
31 | format = "Canon BJ/BJC" |
---|
32 | def isValid(self) : |
---|
33 | """Returns True if data is BJ/BJC, else False.""" |
---|
34 | if self.firstblock.startswith("\033[K\002\000") : |
---|
35 | return True |
---|
36 | else : |
---|
37 | return False |
---|
38 | |
---|
39 | def getJobSize(self) : |
---|
40 | """Counts pages in a Canon BJ document. |
---|
41 | |
---|
42 | Algorithm by Jerome Alet. |
---|
43 | |
---|
44 | The documentation used for this was : |
---|
45 | |
---|
46 | ghostscript-8.60/src/gdevbj*.c |
---|
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 |
---|
51 | pos = 0 |
---|
52 | try : |
---|
53 | try : |
---|
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] |
---|
59 | if pageheader in ("\033[K\002\000\000\017", |
---|
60 | "\033[K\002\000\000\044", |
---|
61 | "\033[K\002\000\004\044") : |
---|
62 | pagecount += 1 |
---|
63 | pos += 6 |
---|
64 | pos += 1 |
---|
65 | except IndexError : # EOF ? |
---|
66 | pass |
---|
67 | finally : |
---|
68 | minfile.close() # reached EOF |
---|
69 | return pagecount |
---|