root / pkpgcounter / trunk / pkpgpdls / escpages03.py @ 3578

Revision 3578, 3.5 kB (checked in by jerome, 5 years ago)

Clarified dependency wrt PIL/Pillow.
Updated copyright years.
Regenerated manual page.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
RevLine 
[3410]1# -*- coding: utf-8 -*-
[473]2#
3# pkpgcounter : a generic Page Description Language parser
4#
[3578]5# (c) 2003-2019 Jerome Alet <alet@librelogiciel.com>
[473]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.
[3436]10#
[473]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#
[473]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 TIFF documents."""
23
24import sys
25import os
26import mmap
27from struct import unpack
28
29import pdlparser
30import pjl
31
32class Parser(pdlparser.PDLParser) :
[475]33    """A parser for ESC/PageS03 documents."""
[555]34    format = "ESC/PageS03"
[3436]35    def isValid(self) :
[473]36        """Returns True if data is TIFF, else False."""
[522]37        if self.firstblock.startswith("\033\1@EJL") and \
38            (self.firstblock.find("=ESC/PAGES03\n") != -1) :
[473]39            return True
[3436]40        else :
[473]41            return False
[3436]42
[473]43    def getJobSize(self) :
[475]44        """Counts pages in an ESC/PageS03 document.
[3436]45
[473]46           Algorithm by Jerome Alet.
47           Reverse engineered the file format.
48        """
49        infileno = self.infile.fileno()
50        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
51        pagecount = 0
52        marker = "=ESC/PAGES03\n"
53        startpos = minfile.find(marker)
54        startsequence = chr(0x1d)
55        if startpos == -1 :
[475]56            raise pdlparser.PDLParserError, "Invalid ESC/PageS03 file."
[473]57        startpos += len(marker)
58        if minfile[startpos] != startsequence :
[475]59            raise pdlparser.PDLParserError, "Invalid ESC/PageS03 file."
[473]60        endsequence = "eps{I"
61        lgendsequence = len(endsequence)
62        try :
[3436]63            try :
[473]64                while True :
65                    if minfile[startpos] == startsequence :
66                        skiplen = 0
67                        while True :
68                            startpos += 1
69                            c = minfile[startpos]
70                            if not c.isdigit() :
71                                break
72                            else :
73                                skiplen = (skiplen * 10) + int(c)
74                        if minfile[startpos:startpos+lgendsequence] == endsequence :
[475]75                            startpos += (skiplen + lgendsequence)
[473]76                    else :
77                        if minfile[startpos:startpos+6] == "\033\1@EJL" :
[475]78                            # Probably near the end of the file.
79                            # Test suite was too small to be sure.
[473]80                            ejlparser = pjl.EJLParser(minfile[startpos:])
81                            pagecount = ejlparser.environment_variables.get("PAGES", "1")
82                            if pagecount.startswith('"') and pagecount.endswith('"') :
83                                pagecount = pagecount[1:-1]
[3436]84                            pagecount = int(pagecount)
[473]85                            if pagecount <= 0 :
[475]86                                pagecount = 1 # TODO : 0 or 1000000 ??? ;-)
[473]87                            break
[3436]88                        startpos += 1
89            except IndexError :
[473]90                pass
[3436]91        finally :
[473]92            minfile.close()
93        return pagecount
Note: See TracBrowser for help on using the browser.