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