[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 | |
---|
[356] | 23 | """This modules implements a page counter for DVI documents.""" |
---|
| 24 | |
---|
[216] | 25 | import sys |
---|
| 26 | import os |
---|
| 27 | import mmap |
---|
| 28 | from struct import unpack |
---|
| 29 | |
---|
[235] | 30 | import pdlparser |
---|
[216] | 31 | |
---|
[220] | 32 | class Parser(pdlparser.PDLParser) : |
---|
[216] | 33 | """A parser for DVI documents.""" |
---|
[492] | 34 | totiffcommands = [ 'dvips -q -o - "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r"%(dpi)i" -sOutputFile="%(outfname)s" -' ] |
---|
[527] | 35 | required = [ "dvips", "gs" ] |
---|
[555] | 36 | format = "DVI" |
---|
[220] | 37 | def isValid(self) : |
---|
[387] | 38 | """Returns True if data is DVI, else False.""" |
---|
[220] | 39 | try : |
---|
[522] | 40 | if (ord(self.firstblock[0]) == 0xf7) \ |
---|
| 41 | and (ord(self.lastblock[-1]) == 0xdf) : |
---|
[387] | 42 | return True |
---|
[220] | 43 | else : |
---|
[387] | 44 | return False |
---|
[220] | 45 | except IndexError : |
---|
[387] | 46 | return False |
---|
[220] | 47 | |
---|
[216] | 48 | def getJobSize(self) : |
---|
| 49 | """Counts pages in a DVI document. |
---|
| 50 | |
---|
| 51 | Algorithm by Jerome Alet. |
---|
| 52 | |
---|
| 53 | The documentation used for this was : |
---|
| 54 | |
---|
| 55 | http://www.math.umd.edu/~asnowden/comp-cont/dvi.html |
---|
| 56 | """ |
---|
| 57 | infileno = self.infile.fileno() |
---|
| 58 | minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
| 59 | pagecount = 0 |
---|
| 60 | pos = -1 |
---|
| 61 | eofchar = chr(0xdf) |
---|
| 62 | postchar = chr(0xf8) |
---|
| 63 | try : |
---|
[489] | 64 | try : |
---|
| 65 | while minfile[pos] == eofchar : |
---|
| 66 | pos -= 1 |
---|
| 67 | idbyte = minfile[pos] |
---|
| 68 | if idbyte != minfile[1] : |
---|
| 69 | raise IndexError, "Invalid DVI file." |
---|
| 70 | pos = unpack(">I", minfile[pos - 4:pos])[0] |
---|
| 71 | if minfile[pos] != postchar : |
---|
| 72 | raise IndexError, "Invalid DVI file." |
---|
| 73 | pagecount = unpack(">H", minfile[pos + 27: pos + 29])[0] |
---|
| 74 | except IndexError : # EOF ? |
---|
| 75 | pass |
---|
| 76 | finally : |
---|
| 77 | minfile.close() # reached EOF |
---|
[216] | 78 | return pagecount |
---|