[3410] | 1 | # -*- coding: utf-8 -*- |
---|
[216] | 2 | # |
---|
| 3 | # pkpgcounter : a generic Page Description Language parser |
---|
| 4 | # |
---|
[3578] | 5 | # (c) 2003-2019 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 | |
---|
[480] | 22 | """This modules implements a page counter for Brother HBP documents.""" |
---|
[356] | 23 | |
---|
[216] | 24 | import sys |
---|
| 25 | import os |
---|
| 26 | import mmap |
---|
| 27 | from struct import unpack |
---|
| 28 | |
---|
[235] | 29 | import pdlparser |
---|
[216] | 30 | |
---|
[220] | 31 | class Parser(pdlparser.PDLParser) : |
---|
[480] | 32 | """A parser for HBP documents.""" |
---|
[555] | 33 | format = "Brother HBP" |
---|
[3436] | 34 | def isValid(self) : |
---|
[480] | 35 | """Returns True if data is HBP, else False.""" |
---|
[522] | 36 | if self.firstblock.find("@PJL ENTER LANGUAGE = HBP\n") != -1 : |
---|
[480] | 37 | return True |
---|
[3436] | 38 | else : |
---|
[387] | 39 | return False |
---|
[3436] | 40 | |
---|
[216] | 41 | def getJobSize(self) : |
---|
[480] | 42 | """Counts pages in a HBP document. |
---|
[3436] | 43 | |
---|
[216] | 44 | Algorithm by Jerome Alet. |
---|
[3436] | 45 | |
---|
[216] | 46 | The documentation used for this was : |
---|
[3436] | 47 | |
---|
[480] | 48 | http://sf.net/projects/hbp-for-brother/ |
---|
[3436] | 49 | |
---|
[480] | 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. |
---|
[216] | 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 |
---|
[3436] | 57 | |
---|
[480] | 58 | formfeed = "@G" + chr(0) + chr(0) + chr(1) + chr(0xff) + "@F" |
---|
| 59 | fflen = len(formfeed) |
---|
| 60 | pos = 0 |
---|
[216] | 61 | try : |
---|
[489] | 62 | try : |
---|
| 63 | while True : |
---|
| 64 | if (minfile[pos] == "@") \ |
---|
| 65 | and (minfile[pos:pos+fflen] == formfeed) : |
---|
| 66 | pagecount += 1 |
---|
| 67 | pos += fflen |
---|
[3436] | 68 | else : |
---|
[489] | 69 | pos += 1 |
---|
| 70 | except IndexError : # EOF ? |
---|
| 71 | pass |
---|
[3436] | 72 | finally : |
---|
[489] | 73 | minfile.close() # reached EOF |
---|
[216] | 74 | return pagecount |
---|