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