[392] | 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 |
---|
[392] | 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 |
---|
[392] | 10 | # (at your option) any later version. |
---|
[463] | 11 | # |
---|
[392] | 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/>. |
---|
[392] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
| 22 | |
---|
| 23 | """This modules implements a page counter for PCL3/4/5 documents.""" |
---|
| 24 | |
---|
| 25 | import sys |
---|
| 26 | import os |
---|
| 27 | import mmap |
---|
| 28 | from struct import unpack |
---|
| 29 | |
---|
| 30 | import pdlparser |
---|
| 31 | import pjl |
---|
| 32 | |
---|
[396] | 33 | NUL = chr(0x00) |
---|
[452] | 34 | LINEFEED = chr(0x0a) |
---|
[396] | 35 | FORMFEED = chr(0x0c) |
---|
| 36 | ESCAPE = chr(0x1b) |
---|
| 37 | ASCIILIMIT = chr(0x80) |
---|
[392] | 38 | |
---|
| 39 | class Parser(pdlparser.PDLParser) : |
---|
| 40 | """A parser for PCL3, PCL4, PCL5 documents.""" |
---|
[492] | 41 | totiffcommands = [ 'pcl6 -sDEVICE=pdfwrite -r"%(dpi)i" -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -sOutputFile=- "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(outfname)s" -', |
---|
| 42 | 'pcl6 -sDEVICE=pswrite -r"%(dpi)i" -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -sOutputFile=- "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(outfname)s" -', |
---|
[428] | 43 | ] |
---|
[527] | 44 | required = [ "pcl6", "gs" ] |
---|
[392] | 45 | mediasizes = { # ESC&l####A |
---|
| 46 | 0 : "Default", |
---|
| 47 | 1 : "Executive", |
---|
| 48 | 2 : "Letter", |
---|
| 49 | 3 : "Legal", |
---|
| 50 | 6 : "Ledger", |
---|
| 51 | 25 : "A5", |
---|
| 52 | 26 : "A4", |
---|
| 53 | 27 : "A3", |
---|
| 54 | 45 : "JB5", |
---|
| 55 | 46 : "JB4", |
---|
| 56 | 71 : "HagakiPostcard", |
---|
| 57 | 72 : "OufukuHagakiPostcard", |
---|
| 58 | 80 : "MonarchEnvelope", |
---|
| 59 | 81 : "COM10Envelope", |
---|
| 60 | 90 : "DLEnvelope", |
---|
| 61 | 91 : "C5Envelope", |
---|
| 62 | 100 : "B5Envelope", |
---|
| 63 | 101 : "Custom", |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | mediasources = { # ESC&l####H |
---|
| 67 | 0 : "Default", |
---|
| 68 | 1 : "Main", |
---|
| 69 | 2 : "Manual", |
---|
| 70 | 3 : "ManualEnvelope", |
---|
| 71 | 4 : "Alternate", |
---|
| 72 | 5 : "OptionalLarge", |
---|
| 73 | 6 : "EnvelopeFeeder", |
---|
| 74 | 7 : "Auto", |
---|
| 75 | 8 : "Tray1", |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | orientations = { # ESC&l####O |
---|
| 79 | 0 : "Portrait", |
---|
| 80 | 1 : "Landscape", |
---|
| 81 | 2 : "ReversePortrait", |
---|
| 82 | 3 : "ReverseLandscape", |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | mediatypes = { # ESC&l####M |
---|
| 86 | 0 : "Plain", |
---|
| 87 | 1 : "Bond", |
---|
| 88 | 2 : "Special", |
---|
| 89 | 3 : "Glossy", |
---|
| 90 | 4 : "Transparent", |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | def isValid(self) : |
---|
| 94 | """Returns True if data is PCL3/4/5, else False.""" |
---|
[522] | 95 | if self.firstblock.startswith("\033E\033") or \ |
---|
| 96 | (self.firstblock.startswith("\033*rbC") and (not self.lastblock[-3:] == "\f\033@")) or \ |
---|
| 97 | self.firstblock.startswith("\033*rB\033") or \ |
---|
| 98 | self.firstblock.startswith("\033%8\033") or \ |
---|
| 99 | (self.firstblock.find("\033%-12345X") != -1) or \ |
---|
| 100 | (self.firstblock.find("@PJL ENTER LANGUAGE=PCL\012\015\033") != -1) or \ |
---|
| 101 | (self.firstblock.startswith(chr(0xcd)+chr(0xca)) and (self.firstblock.find("\033E\033") != -1)) : |
---|
[392] | 102 | self.logdebug("DEBUG: Input file is in the PCL3/4/5 format.") |
---|
| 103 | return True |
---|
| 104 | else : |
---|
| 105 | return False |
---|
| 106 | |
---|
[396] | 107 | def setPageDict(self, attribute, value) : |
---|
[392] | 108 | """Initializes a page dictionnary.""" |
---|
[452] | 109 | dic = self.pages.setdefault(self.pagecount, { "linescount" : 1, |
---|
| 110 | "copies" : 1, |
---|
| 111 | "mediasource" : "Main", |
---|
| 112 | "mediasize" : "Default", |
---|
| 113 | "mediatype" : "Plain", |
---|
| 114 | "orientation" : "Portrait", |
---|
| 115 | "escaped" : "", |
---|
| 116 | "duplex": 0 }) |
---|
[392] | 117 | dic[attribute] = value |
---|
| 118 | |
---|
[394] | 119 | def readByte(self) : |
---|
| 120 | """Reads a byte from the input stream.""" |
---|
| 121 | tag = ord(self.minfile[self.pos]) |
---|
| 122 | self.pos += 1 |
---|
| 123 | return tag |
---|
| 124 | |
---|
[392] | 125 | def endPage(self) : |
---|
| 126 | """Handle the FF marker.""" |
---|
[398] | 127 | #self.logdebug("FORMFEED %i at %08x" % (self.pagecount, self.pos-1)) |
---|
[401] | 128 | if not self.hpgl2 : |
---|
| 129 | # Increments page count only if we are not inside an HPGL2 block |
---|
| 130 | self.pagecount += 1 |
---|
[392] | 131 | |
---|
| 132 | def escPercent(self) : |
---|
| 133 | """Handles the ESC% sequence.""" |
---|
| 134 | if self.minfile[self.pos : self.pos+7] == r"-12345X" : |
---|
[396] | 135 | #self.logdebug("Generic ESCAPE sequence at %08x" % self.pos) |
---|
[392] | 136 | self.pos += 7 |
---|
[396] | 137 | buffer = [] |
---|
| 138 | quotes = 0 |
---|
| 139 | char = chr(self.readByte()) |
---|
| 140 | while ((char < ASCIILIMIT) or (quotes % 2)) and (char not in (FORMFEED, ESCAPE, NUL)) : |
---|
| 141 | buffer.append(char) |
---|
| 142 | if char == '"' : |
---|
| 143 | quotes += 1 |
---|
| 144 | char = chr(self.readByte()) |
---|
| 145 | self.setPageDict("escaped", "".join(buffer)) |
---|
| 146 | #self.logdebug("ESCAPED : %s" % "".join(buffer)) |
---|
| 147 | self.pos -= 1 # Adjust position |
---|
| 148 | else : |
---|
| 149 | while 1 : |
---|
| 150 | (value, end) = self.getInteger() |
---|
| 151 | if end == 'B' : |
---|
| 152 | self.enterHPGL2() |
---|
| 153 | while self.minfile[self.pos] != ESCAPE : |
---|
| 154 | self.pos += 1 |
---|
| 155 | self.pos -= 1 |
---|
| 156 | return |
---|
| 157 | elif end == 'A' : |
---|
| 158 | self.exitHPGL2() |
---|
| 159 | return |
---|
[397] | 160 | elif end is None : |
---|
| 161 | return |
---|
[392] | 162 | |
---|
[396] | 163 | def enterHPGL2(self) : |
---|
| 164 | """Enters HPGL2 mode.""" |
---|
[398] | 165 | #self.logdebug("ENTERHPGL2 %08x" % self.pos) |
---|
[396] | 166 | self.hpgl2 = True |
---|
| 167 | |
---|
| 168 | def exitHPGL2(self) : |
---|
| 169 | """Exits HPGL2 mode.""" |
---|
[398] | 170 | #self.logdebug("EXITHPGL2 %08x" % self.pos) |
---|
[396] | 171 | self.hpgl2 = False |
---|
| 172 | |
---|
[392] | 173 | def handleTag(self, tagtable) : |
---|
| 174 | """Handles tags.""" |
---|
[394] | 175 | tagtable[self.readByte()]() |
---|
[392] | 176 | |
---|
| 177 | def escape(self) : |
---|
| 178 | """Handles the ESC character.""" |
---|
[395] | 179 | #self.logdebug("ESCAPE") |
---|
[392] | 180 | self.handleTag(self.esctags) |
---|
| 181 | |
---|
| 182 | def escAmp(self) : |
---|
| 183 | """Handles the ESC& sequence.""" |
---|
[395] | 184 | #self.logdebug("AMP") |
---|
[392] | 185 | self.handleTag(self.escamptags) |
---|
| 186 | |
---|
| 187 | def escStar(self) : |
---|
| 188 | """Handles the ESC* sequence.""" |
---|
[395] | 189 | #self.logdebug("STAR") |
---|
[392] | 190 | self.handleTag(self.escstartags) |
---|
| 191 | |
---|
| 192 | def escLeftPar(self) : |
---|
| 193 | """Handles the ESC( sequence.""" |
---|
[395] | 194 | #self.logdebug("LEFTPAR") |
---|
[392] | 195 | self.handleTag(self.escleftpartags) |
---|
| 196 | |
---|
| 197 | def escRightPar(self) : |
---|
| 198 | """Handles the ESC( sequence.""" |
---|
[395] | 199 | #self.logdebug("RIGHTPAR") |
---|
[392] | 200 | self.handleTag(self.escrightpartags) |
---|
| 201 | |
---|
| 202 | def escE(self) : |
---|
| 203 | """Handles the ESCE sequence.""" |
---|
[395] | 204 | #self.logdebug("RESET") |
---|
[392] | 205 | self.resets += 1 |
---|
| 206 | |
---|
| 207 | def escAmpl(self) : |
---|
| 208 | """Handles the ESC&l sequence.""" |
---|
| 209 | while 1 : |
---|
| 210 | (value, end) = self.getInteger() |
---|
| 211 | if value is None : |
---|
| 212 | return |
---|
| 213 | if end in ('h', 'H') : |
---|
| 214 | mediasource = self.mediasources.get(value, str(value)) |
---|
| 215 | self.mediasourcesvalues.append(mediasource) |
---|
[396] | 216 | self.setPageDict("mediasource", mediasource) |
---|
| 217 | #self.logdebug("MEDIASOURCE %s" % mediasource) |
---|
[392] | 218 | elif end in ('a', 'A') : |
---|
| 219 | mediasize = self.mediasizes.get(value, str(value)) |
---|
| 220 | self.mediasizesvalues.append(mediasize) |
---|
[396] | 221 | self.setPageDict("mediasize", mediasize) |
---|
| 222 | #self.logdebug("MEDIASIZE %s" % mediasize) |
---|
[392] | 223 | elif end in ('o', 'O') : |
---|
| 224 | orientation = self.orientations.get(value, str(value)) |
---|
| 225 | self.orientationsvalues.append(orientation) |
---|
[396] | 226 | self.setPageDict("orientation", orientation) |
---|
| 227 | #self.logdebug("ORIENTATION %s" % orientation) |
---|
[392] | 228 | elif end in ('m', 'M') : |
---|
| 229 | mediatype = self.mediatypes.get(value, str(value)) |
---|
| 230 | self.mediatypesvalues.append(mediatype) |
---|
[396] | 231 | self.setPageDict("mediatype", mediatype) |
---|
| 232 | #self.logdebug("MEDIATYPE %s" % mediatype) |
---|
[392] | 233 | elif end == 'X' : |
---|
| 234 | self.copies.append(value) |
---|
[396] | 235 | self.setPageDict("copies", value) |
---|
| 236 | #self.logdebug("COPIES %i" % value) |
---|
[452] | 237 | elif end == 'F' : |
---|
| 238 | self.linesperpagevalues.append(value) |
---|
| 239 | self.linesperpage = value |
---|
| 240 | #self.logdebug("LINES PER PAGE : %i" % self.linesperpage) |
---|
| 241 | #else : |
---|
| 242 | # self.logdebug("Unexpected end <%s> and value <%s>" % (end, value)) |
---|
[392] | 243 | |
---|
[395] | 244 | def escAmpa(self) : |
---|
| 245 | """Handles the ESC&a sequence.""" |
---|
| 246 | while 1 : |
---|
| 247 | (value, end) = self.getInteger() |
---|
| 248 | if value is None : |
---|
| 249 | return |
---|
| 250 | if end == 'G' : |
---|
[396] | 251 | #self.logdebug("BACKSIDES %i" % value) |
---|
[395] | 252 | self.backsides.append(value) |
---|
[396] | 253 | self.setPageDict("duplex", value) |
---|
[395] | 254 | |
---|
| 255 | def escAmpp(self) : |
---|
| 256 | """Handles the ESC&p sequence.""" |
---|
| 257 | while 1 : |
---|
| 258 | (value, end) = self.getInteger() |
---|
| 259 | if value is None : |
---|
| 260 | return |
---|
| 261 | if end == 'X' : |
---|
| 262 | self.pos += value |
---|
| 263 | #self.logdebug("SKIPTO %08x" % self.pos) |
---|
| 264 | |
---|
[392] | 265 | def escStarb(self) : |
---|
| 266 | """Handles the ESC*b sequence.""" |
---|
| 267 | while 1 : |
---|
| 268 | (value, end) = self.getInteger() |
---|
| 269 | if (end is None) and (value is None) : |
---|
| 270 | return |
---|
| 271 | if end in ('V', 'W', 'v', 'w') : |
---|
| 272 | self.pos += (value or 0) |
---|
[395] | 273 | #self.logdebug("SKIPTO %08x" % self.pos) |
---|
[392] | 274 | |
---|
| 275 | def escStarr(self) : |
---|
| 276 | """Handles the ESC*r sequence.""" |
---|
| 277 | while 1 : |
---|
| 278 | (value, end) = self.getInteger() |
---|
| 279 | if value is None : |
---|
| 280 | if end is None : |
---|
| 281 | return |
---|
[395] | 282 | elif end in ('B', 'C') : |
---|
| 283 | #self.logdebug("EndGFX") |
---|
[396] | 284 | if self.startgfx : |
---|
| 285 | self.endgfx.append(1) |
---|
[392] | 286 | else : |
---|
[396] | 287 | #self.logdebug("EndGFX found before StartGFX, ignored.") |
---|
| 288 | pass |
---|
[392] | 289 | if end == 'A' and (0 <= value <= 3) : |
---|
[395] | 290 | #self.logdebug("StartGFX %i" % value) |
---|
[392] | 291 | self.startgfx.append(value) |
---|
| 292 | |
---|
[406] | 293 | def escStaroptAmpu(self) : |
---|
| 294 | """Handles the ESC*o ESC*p ESC*t and ESC&u sequences.""" |
---|
[392] | 295 | while 1 : |
---|
| 296 | (value, end) = self.getInteger() |
---|
| 297 | if value is None : |
---|
| 298 | return |
---|
[395] | 299 | |
---|
[406] | 300 | def escSkipSomethingW(self) : |
---|
| 301 | """Handles the ESC???###W sequences.""" |
---|
[392] | 302 | while 1 : |
---|
| 303 | (value, end) = self.getInteger() |
---|
| 304 | if value is None : |
---|
| 305 | return |
---|
[395] | 306 | if end == 'W' : |
---|
| 307 | self.pos += value |
---|
| 308 | #self.logdebug("SKIPTO %08x" % self.pos) |
---|
[392] | 309 | |
---|
[452] | 310 | def newLine(self) : |
---|
| 311 | """Handles new lines markers.""" |
---|
| 312 | if not self.hpgl2 : |
---|
| 313 | dic = self.pages.get(self.pagecount, None) |
---|
| 314 | if dic is None : |
---|
| 315 | self.setPageDict("linescount", 1) |
---|
| 316 | dic = self.pages.get(self.pagecount) |
---|
| 317 | nblines = dic["linescount"] |
---|
| 318 | self.setPageDict("linescount", nblines + 1) |
---|
| 319 | if (self.linesperpage is not None) \ |
---|
| 320 | and (dic["linescount"] > self.linesperpage) : |
---|
| 321 | self.pagecount += 1 |
---|
| 322 | |
---|
[392] | 323 | def getInteger(self) : |
---|
| 324 | """Returns an integer value and the end character.""" |
---|
| 325 | sign = 1 |
---|
| 326 | value = None |
---|
| 327 | while 1 : |
---|
[394] | 328 | char = chr(self.readByte()) |
---|
[396] | 329 | if char in (NUL, ESCAPE, FORMFEED, ASCIILIMIT) : |
---|
[394] | 330 | self.pos -= 1 # Adjust position |
---|
[392] | 331 | return (None, None) |
---|
| 332 | if char == '-' : |
---|
| 333 | sign = -1 |
---|
| 334 | elif not char.isdigit() : |
---|
| 335 | if value is not None : |
---|
| 336 | return (sign*value, char) |
---|
| 337 | else : |
---|
| 338 | return (value, char) |
---|
| 339 | else : |
---|
| 340 | value = ((value or 0) * 10) + int(char) |
---|
| 341 | |
---|
[396] | 342 | def skipByte(self) : |
---|
| 343 | """Skips a byte.""" |
---|
| 344 | #self.logdebug("SKIPBYTE %08x ===> %02x" % (self.pos, ord(self.minfile[self.pos]))) |
---|
| 345 | self.pos += 1 |
---|
| 346 | |
---|
[399] | 347 | def handleImageRunner(self) : |
---|
| 348 | """Handles Canon ImageRunner tags.""" |
---|
| 349 | tag = self.readByte() |
---|
| 350 | if tag == ord(self.imagerunnermarker1[-1]) : |
---|
| 351 | oldpos = self.pos-2 |
---|
| 352 | codop = self.minfile[self.pos:self.pos+2] |
---|
[471] | 353 | length = unpack(">H", self.minfile[self.pos+6:self.pos+8])[0] |
---|
[399] | 354 | self.pos += 18 |
---|
| 355 | if codop != self.imagerunnermarker2 : |
---|
| 356 | self.pos += length |
---|
| 357 | self.logdebug("IMAGERUNNERTAG SKIP %i AT %08x" % (self.pos-oldpos, self.pos)) |
---|
| 358 | else : |
---|
| 359 | self.pos -= 1 # Adjust position |
---|
| 360 | |
---|
[392] | 361 | def getJobSize(self) : |
---|
| 362 | """Count pages in a PCL5 document. |
---|
| 363 | |
---|
| 364 | Should also work for PCL3 and PCL4 documents. |
---|
| 365 | |
---|
| 366 | Algorithm from pclcount |
---|
| 367 | (c) 2003, by Eduardo Gielamo Oliveira & Rodolfo Broco Manin |
---|
| 368 | published under the terms of the GNU General Public Licence v2. |
---|
| 369 | |
---|
| 370 | Backported from C to Python by Jerome Alet, then enhanced |
---|
| 371 | with more PCL tags detected. I think all the necessary PCL tags |
---|
| 372 | are recognized to correctly handle PCL5 files wrt their number |
---|
| 373 | of pages. The documentation used for this was : |
---|
| 374 | |
---|
| 375 | HP PCL/PJL Reference Set |
---|
| 376 | PCL5 Printer Language Technical Quick Reference Guide |
---|
| 377 | http://h20000.www2.hp.com/bc/docs/support/SupportManual/bpl13205/bpl13205.pdf |
---|
| 378 | """ |
---|
| 379 | infileno = self.infile.fileno() |
---|
| 380 | self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
[396] | 381 | self.pages = {} |
---|
[392] | 382 | self.pagecount = 0 |
---|
| 383 | self.resets = 0 |
---|
[395] | 384 | self.backsides = [] |
---|
[392] | 385 | self.copies = [] |
---|
| 386 | self.mediasourcesvalues = [] |
---|
| 387 | self.mediasizesvalues = [] |
---|
| 388 | self.orientationsvalues = [] |
---|
| 389 | self.mediatypesvalues = [] |
---|
[452] | 390 | self.linesperpagevalues = [] |
---|
| 391 | self.linesperpage = None |
---|
[392] | 392 | self.startgfx = [] |
---|
| 393 | self.endgfx = [] |
---|
[396] | 394 | self.hpgl2 = False |
---|
[399] | 395 | self.imagerunnermarker1 = chr(0xcd) + chr(0xca) # Markers for Canon ImageRunner printers |
---|
| 396 | self.imagerunnermarker2 = chr(0x10) + chr(0x02) |
---|
| 397 | self.isimagerunner = (minfile[:2] == self.imagerunnermarker1) |
---|
[392] | 398 | |
---|
| 399 | tags = [ lambda : None] * 256 |
---|
[452] | 400 | tags[ord(LINEFEED)] = self.newLine |
---|
[394] | 401 | tags[ord(FORMFEED)] = self.endPage |
---|
| 402 | tags[ord(ESCAPE)] = self.escape |
---|
[396] | 403 | tags[ord(ASCIILIMIT)] = self.skipByte |
---|
[399] | 404 | tags[ord(self.imagerunnermarker1[0])] = self.handleImageRunner |
---|
[392] | 405 | |
---|
| 406 | self.esctags = [ lambda : None ] * 256 |
---|
| 407 | self.esctags[ord('%')] = self.escPercent |
---|
| 408 | self.esctags[ord('*')] = self.escStar |
---|
| 409 | self.esctags[ord('&')] = self.escAmp |
---|
| 410 | self.esctags[ord('(')] = self.escLeftPar |
---|
| 411 | self.esctags[ord(')')] = self.escRightPar |
---|
| 412 | self.esctags[ord('E')] = self.escE |
---|
| 413 | |
---|
| 414 | self.escamptags = [lambda : None ] * 256 |
---|
[395] | 415 | self.escamptags[ord('a')] = self.escAmpa |
---|
[392] | 416 | self.escamptags[ord('l')] = self.escAmpl |
---|
[395] | 417 | self.escamptags[ord('p')] = self.escAmpp |
---|
[406] | 418 | self.escamptags[ord('b')] = self.escSkipSomethingW |
---|
| 419 | self.escamptags[ord('n')] = self.escSkipSomethingW |
---|
| 420 | self.escamptags[ord('u')] = self.escStaroptAmpu |
---|
[392] | 421 | |
---|
| 422 | self.escstartags = [ lambda : None ] * 256 |
---|
| 423 | self.escstartags[ord('b')] = self.escStarb |
---|
| 424 | self.escstartags[ord('r')] = self.escStarr |
---|
[406] | 425 | self.escstartags[ord('o')] = self.escStaroptAmpu |
---|
| 426 | self.escstartags[ord('p')] = self.escStaroptAmpu |
---|
| 427 | self.escstartags[ord('t')] = self.escStaroptAmpu |
---|
| 428 | self.escstartags[ord('c')] = self.escSkipSomethingW |
---|
| 429 | self.escstartags[ord('g')] = self.escSkipSomethingW |
---|
| 430 | self.escstartags[ord('i')] = self.escSkipSomethingW |
---|
| 431 | self.escstartags[ord('l')] = self.escSkipSomethingW |
---|
| 432 | self.escstartags[ord('m')] = self.escSkipSomethingW |
---|
| 433 | self.escstartags[ord('v')] = self.escSkipSomethingW |
---|
[392] | 434 | |
---|
[395] | 435 | self.escleftpartags = [ lambda : None ] * 256 |
---|
[406] | 436 | self.escleftpartags[ord('s')] = self.escSkipSomethingW |
---|
| 437 | self.escleftpartags[ord('f')] = self.escSkipSomethingW |
---|
[395] | 438 | |
---|
| 439 | self.escrightpartags = [ lambda : None ] * 256 |
---|
[406] | 440 | self.escrightpartags[ord('s')] = self.escSkipSomethingW |
---|
[395] | 441 | |
---|
[392] | 442 | self.pos = 0 |
---|
| 443 | try : |
---|
| 444 | try : |
---|
| 445 | while 1 : |
---|
[395] | 446 | tags[self.readByte()]() |
---|
[392] | 447 | except IndexError : # EOF ? |
---|
| 448 | pass |
---|
| 449 | finally : |
---|
| 450 | self.minfile.close() |
---|
| 451 | |
---|
[395] | 452 | self.logdebug("Pagecount : \t\t\t%i" % self.pagecount) |
---|
| 453 | self.logdebug("Resets : \t\t\t%i" % self.resets) |
---|
| 454 | self.logdebug("Copies : \t\t\t%s" % self.copies) |
---|
| 455 | self.logdebug("NbCopiesMarks : \t\t%i" % len(self.copies)) |
---|
| 456 | self.logdebug("MediaTypes : \t\t\t%s" % self.mediatypesvalues) |
---|
| 457 | self.logdebug("NbMediaTypes : \t\t\t%i" % len(self.mediatypesvalues)) |
---|
| 458 | self.logdebug("MediaSizes : \t\t\t%s" % self.mediasizesvalues) |
---|
| 459 | self.logdebug("NbMediaSizes : \t\t\t%i" % len(self.mediasizesvalues)) |
---|
| 460 | self.logdebug("MediaSources : \t\t\t%s" % self.mediasourcesvalues) |
---|
| 461 | nbmediasourcesdefault = len([m for m in self.mediasourcesvalues if m == 'Default']) |
---|
| 462 | self.logdebug("MediaSourcesDefault : \t\t%i" % nbmediasourcesdefault) |
---|
| 463 | self.logdebug("MediaSourcesNOTDefault : \t%i" % (len(self.mediasourcesvalues) - nbmediasourcesdefault)) |
---|
| 464 | self.logdebug("Orientations : \t\t\t%s" % self.orientationsvalues) |
---|
[398] | 465 | nborientations = len(self.orientationsvalues) |
---|
| 466 | self.logdebug("NbOrientations : \t\t\t%i" % nborientations) |
---|
[395] | 467 | self.logdebug("StartGfx : \t\t\t%s" % len(self.startgfx)) |
---|
| 468 | self.logdebug("EndGfx : \t\t\t%s" % len(self.endgfx)) |
---|
| 469 | self.logdebug("BackSides : \t\t\t%s" % self.backsides) |
---|
| 470 | self.logdebug("NbBackSides : \t\t\t%i" % len(self.backsides)) |
---|
[399] | 471 | self.logdebug("IsImageRunner : \t\t\t%s" % self.isimagerunner) |
---|
[392] | 472 | |
---|
[399] | 473 | if self.isimagerunner : |
---|
| 474 | self.pagecount += 1 # ImageRunner adjustment |
---|
[452] | 475 | elif self.linesperpage is not None : |
---|
| 476 | self.pagecount += 1 # Adjusts for incomplete last page |
---|
[399] | 477 | elif len(self.startgfx) == len(self.endgfx) == 0 : |
---|
[398] | 478 | if self.resets % 2 : |
---|
| 479 | if nborientations == self.pagecount + 1 : |
---|
| 480 | self.logdebug("Adjusting PageCount : +1") |
---|
| 481 | self.pagecount += 1 |
---|
| 482 | elif nborientations == self.pagecount - 1 : |
---|
| 483 | self.logdebug("Adjusting PageCount : -1") |
---|
| 484 | self.pagecount -= 1 |
---|
[400] | 485 | |
---|
| 486 | self.pagecount = self.pagecount or nbmediasourcesdefault |
---|
[392] | 487 | |
---|
[400] | 488 | |
---|
| 489 | defaultpjlcopies = 1 |
---|
| 490 | defaultduplexmode = "Simplex" |
---|
| 491 | defaultpapersize = "" |
---|
| 492 | oldpjlcopies = -1 |
---|
| 493 | oldduplexmode = "" |
---|
| 494 | oldpapersize = "" |
---|
| 495 | for pnum in range(self.pagecount) : |
---|
| 496 | # if no number of copies defined, take the preceding one else the one set before any page else 1. |
---|
| 497 | page = self.pages.get(pnum, self.pages.get(pnum - 1, self.pages.get(0, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : "", "duplex": 0}))) |
---|
| 498 | pjlstuff = page["escaped"] |
---|
| 499 | if pjlstuff : |
---|
| 500 | pjlparser = pjl.PJLParser(pjlstuff) |
---|
| 501 | nbdefaultcopies = int(pjlparser.default_variables.get("COPIES", -1)) |
---|
| 502 | nbcopies = int(pjlparser.environment_variables.get("COPIES", -1)) |
---|
| 503 | nbdefaultqty = int(pjlparser.default_variables.get("QTY", -1)) |
---|
| 504 | nbqty = int(pjlparser.environment_variables.get("QTY", -1)) |
---|
| 505 | if nbdefaultcopies > -1 : |
---|
| 506 | defaultpjlcopies = nbdefaultcopies |
---|
| 507 | if nbdefaultqty > -1 : |
---|
| 508 | defaultpjlcopies = nbdefaultqty |
---|
| 509 | if nbcopies > -1 : |
---|
| 510 | pjlcopies = nbcopies |
---|
| 511 | elif nbqty > -1 : |
---|
| 512 | pjlcopies = nbqty |
---|
| 513 | else : |
---|
| 514 | if oldpjlcopies == -1 : |
---|
| 515 | pjlcopies = defaultpjlcopies |
---|
| 516 | else : |
---|
| 517 | pjlcopies = oldpjlcopies |
---|
| 518 | if page["duplex"] : |
---|
| 519 | duplexmode = "Duplex" |
---|
| 520 | else : |
---|
| 521 | defaultdm = pjlparser.default_variables.get("DUPLEX", "") |
---|
| 522 | if defaultdm : |
---|
| 523 | if defaultdm.upper() == "ON" : |
---|
| 524 | defaultduplexmode = "Duplex" |
---|
| 525 | else : |
---|
| 526 | defaultduplexmode = "Simplex" |
---|
| 527 | envdm = pjlparser.environment_variables.get("DUPLEX", "") |
---|
| 528 | if envdm : |
---|
| 529 | if envdm.upper() == "ON" : |
---|
| 530 | duplexmode = "Duplex" |
---|
| 531 | else : |
---|
| 532 | duplexmode = "Simplex" |
---|
| 533 | else : |
---|
| 534 | duplexmode = oldduplexmode or defaultduplexmode |
---|
| 535 | defaultps = pjlparser.default_variables.get("PAPER", "") |
---|
| 536 | if defaultps : |
---|
| 537 | defaultpapersize = defaultps |
---|
| 538 | envps = pjlparser.environment_variables.get("PAPER", "") |
---|
| 539 | if envps : |
---|
| 540 | papersize = envps |
---|
| 541 | else : |
---|
| 542 | if not oldpapersize : |
---|
| 543 | papersize = defaultpapersize |
---|
| 544 | else : |
---|
| 545 | papersize = oldpapersize |
---|
| 546 | else : |
---|
| 547 | if oldpjlcopies == -1 : |
---|
| 548 | pjlcopies = defaultpjlcopies |
---|
| 549 | else : |
---|
| 550 | pjlcopies = oldpjlcopies |
---|
| 551 | |
---|
| 552 | duplexmode = (page["duplex"] and "Duplex") or oldduplexmode or defaultduplexmode |
---|
| 553 | if not oldpapersize : |
---|
| 554 | papersize = defaultpapersize |
---|
| 555 | else : |
---|
| 556 | papersize = oldpapersize |
---|
| 557 | papersize = oldpapersize or page["mediasize"] |
---|
| 558 | if page["mediasize"] != "Default" : |
---|
| 559 | papersize = page["mediasize"] |
---|
| 560 | if not duplexmode : |
---|
| 561 | duplexmode = oldduplexmode or defaultduplexmode |
---|
| 562 | oldpjlcopies = pjlcopies |
---|
| 563 | oldduplexmode = duplexmode |
---|
| 564 | oldpapersize = papersize |
---|
[456] | 565 | copies = max(pjlcopies, page["copies"]) # Was : pjlcopies * page["copies"] |
---|
[400] | 566 | self.pagecount += (copies - 1) |
---|
| 567 | self.logdebug("%s*%s*%s*%s*%s*%s*BW" % (copies, \ |
---|
| 568 | page["mediatype"], \ |
---|
| 569 | papersize, \ |
---|
| 570 | page["orientation"], \ |
---|
| 571 | page["mediasource"], \ |
---|
| 572 | duplexmode)) |
---|
| 573 | |
---|
| 574 | return self.pagecount |
---|