1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # pkpgcounter : a generic Page Description Language parser |
---|
5 | # |
---|
6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
7 | # This program is free software: you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation, either version 3 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
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 |
---|
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | |
---|
23 | """This modules implements a page counter for PCLXL (aka PCL6) documents.""" |
---|
24 | |
---|
25 | import sys |
---|
26 | import os |
---|
27 | import mmap |
---|
28 | from struct import unpack |
---|
29 | |
---|
30 | import pdlparser |
---|
31 | import pjl |
---|
32 | |
---|
33 | class Parser(pdlparser.PDLParser) : |
---|
34 | """A parser for PCLXL (aka PCL6) documents.""" |
---|
35 | 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" -', |
---|
36 | '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" -', |
---|
37 | ] |
---|
38 | required = [ "pcl6", "gs" ] |
---|
39 | mediasizes = { |
---|
40 | 0 : "Letter", |
---|
41 | 1 : "Legal", |
---|
42 | 2 : "A4", |
---|
43 | 3 : "Executive", |
---|
44 | 4 : "Ledger", |
---|
45 | 5 : "A3", |
---|
46 | 6 : "COM10Envelope", |
---|
47 | 7 : "MonarchEnvelope", |
---|
48 | 8 : "C5Envelope", |
---|
49 | 9 : "DLEnvelope", |
---|
50 | 10 : "JB4", |
---|
51 | 11 : "JB5", |
---|
52 | 12 : "B5Envelope", |
---|
53 | 12 : "B5", |
---|
54 | 14 : "JPostcard", |
---|
55 | 15 : "JDoublePostcard", |
---|
56 | 16 : "A5", |
---|
57 | 17 : "A6", |
---|
58 | 18 : "JB6", |
---|
59 | 19 : "JIS8K", |
---|
60 | 20 : "JIS16K", |
---|
61 | 21 : "JISExec", |
---|
62 | 96 : "Default", |
---|
63 | } |
---|
64 | |
---|
65 | mediasources = { |
---|
66 | 0 : "Default", |
---|
67 | 1 : "Auto", |
---|
68 | 2 : "Manual", |
---|
69 | 3 : "MultiPurpose", |
---|
70 | 4 : "UpperCassette", |
---|
71 | 5 : "LowerCassette", |
---|
72 | 6 : "EnvelopeTray", |
---|
73 | 7 : "ThirdCassette", |
---|
74 | } |
---|
75 | |
---|
76 | orientations = { |
---|
77 | 0 : "Portrait", |
---|
78 | 1 : "Landscape", |
---|
79 | 2 : "ReversePortrait", |
---|
80 | 3 : "ReverseLandscape", |
---|
81 | 4 : "Default", |
---|
82 | } |
---|
83 | |
---|
84 | def isValid(self) : |
---|
85 | """Returns True if data is PCLXL aka PCL6, else False.""" |
---|
86 | if (((self.firstblock[:128].find("\033%-12345X") != -1) and \ |
---|
87 | (self.firstblock.find(" HP-PCL XL;") != -1) and \ |
---|
88 | ((self.firstblock.find("LANGUAGE=PCLXL") != -1) or \ |
---|
89 | (self.firstblock.find("LANGUAGE = PCLXL") != -1)))) \ |
---|
90 | or ((self.firstblock.startswith(chr(0xcd)+chr(0xca)) and (self.firstblock.find(" HP-PCL XL;") != -1))) : |
---|
91 | self.logdebug("DEBUG: Input file is in the PCLXL (aka PCL6) format.") |
---|
92 | return True |
---|
93 | else : |
---|
94 | return False |
---|
95 | |
---|
96 | def beginPage(self, nextpos) : |
---|
97 | """Indicates the beginning of a new page, and extracts media information.""" |
---|
98 | # self.logdebug("BeginPage at %x" % nextpos) |
---|
99 | self.pagecount += 1 |
---|
100 | |
---|
101 | # Default values |
---|
102 | mediatypelabel = "Plain" |
---|
103 | mediasourcelabel = "Main" |
---|
104 | mediasizelabel = "Default" |
---|
105 | orientationlabel = "Portrait" |
---|
106 | duplexmode = None |
---|
107 | |
---|
108 | # Now go upstream to decode media type, size, source, and orientation |
---|
109 | # this saves time because we don't need a complete parser ! |
---|
110 | minfile = self.minfile |
---|
111 | pos = nextpos - 2 |
---|
112 | while pos > 0 : # safety check : don't go back to far ! |
---|
113 | val = ord(minfile[pos]) |
---|
114 | if val in (0x44, 0x48, 0x41) : # if previous endPage or openDataSource or beginSession (first page) |
---|
115 | break |
---|
116 | if val == 0x26 : |
---|
117 | mediasource = ord(minfile[pos - 2]) |
---|
118 | mediasourcelabel = self.mediasources.get(mediasource, str(mediasource)) |
---|
119 | pos -= 4 |
---|
120 | elif val == 0x25 : |
---|
121 | while (pos > 0) and (ord(minfile[pos]) != 0xc0) : |
---|
122 | # we search the preceding ubyte tag |
---|
123 | pos -= 1 |
---|
124 | if pos > 0 : |
---|
125 | if ord(minfile[pos-1]) == 0xc8 : |
---|
126 | # if we found an ubyte_array then the media |
---|
127 | # size is completely spelled |
---|
128 | arraylength = ord(minfile[pos+1]) |
---|
129 | mediasizelabel = minfile[pos+2:pos+2+arraylength].title() |
---|
130 | pos -= 1 |
---|
131 | else : |
---|
132 | # if we just found an ubyte, then the media |
---|
133 | # size is known by its index |
---|
134 | mediasize = ord(minfile[pos+1]) |
---|
135 | mediasizelabel = self.mediasizes.get(mediasize, str(mediasize)) |
---|
136 | pos -= 1 |
---|
137 | # self.logdebug("Media size : %s" % mediasizelabel) |
---|
138 | elif val == 0x28 : |
---|
139 | orientation = ord(minfile[pos - 2]) |
---|
140 | orientationlabel = self.orientations.get(orientation, str(orientation)) |
---|
141 | pos -= 4 |
---|
142 | elif val == 0x27 : |
---|
143 | savepos = pos |
---|
144 | pos -= 1 |
---|
145 | startpos = size = None |
---|
146 | while pos > 0 : # safety check : don't go back to far ! |
---|
147 | val = ord(minfile[pos]) |
---|
148 | pos -= 1 |
---|
149 | if val == 0xc8 : |
---|
150 | length = self.tags[ord(minfile[pos+2])] # will probably always be a byte or uint16 |
---|
151 | if length == 1 : |
---|
152 | startpos = pos + 4 |
---|
153 | size = unpack("B", self.minfile[pos+3:startpos])[0] |
---|
154 | elif length == 2 : |
---|
155 | startpos = pos + 5 |
---|
156 | size = unpack(self.unpackShort, self.minfile[pos+3:startpos])[0] |
---|
157 | elif length == 4 : |
---|
158 | startpos = pos + 7 |
---|
159 | size = unpack(self.unpackLong, self.minfile[pos+3:startpos])[0] |
---|
160 | else : |
---|
161 | raise pdlparser.PDLParserError, "Error on size at %s : %s" % (pos+2, length) |
---|
162 | break |
---|
163 | mediatypelabel = minfile[startpos:startpos+size] |
---|
164 | # self.logdebug("Media type : %s" % mediatypelabel) |
---|
165 | elif val == 0x34 : |
---|
166 | duplexmode = "Simplex" |
---|
167 | pos -= 2 |
---|
168 | elif val in (0x35, 0x36) : |
---|
169 | duplexmode = "Duplex" |
---|
170 | pos -= 2 |
---|
171 | # else : TODO : CUSTOM MEDIA SIZE AND UNIT ! |
---|
172 | else : |
---|
173 | pos -= 1 # ignored |
---|
174 | self.pages[self.pagecount] = { "copies" : 1, |
---|
175 | "orientation" : orientationlabel, |
---|
176 | "mediatype" : mediatypelabel, |
---|
177 | "mediasize" : mediasizelabel, |
---|
178 | "mediasource" : mediasourcelabel, |
---|
179 | "duplex" : duplexmode, |
---|
180 | } |
---|
181 | return 0 |
---|
182 | |
---|
183 | def endPage(self, nextpos) : |
---|
184 | """Indicates the end of a page.""" |
---|
185 | # self.logdebug("EndPage at %x" % nextpos) |
---|
186 | pos3 = nextpos - 3 |
---|
187 | minfile = self.minfile |
---|
188 | if minfile[pos3:nextpos-1] == self.setNumberOfCopies : |
---|
189 | # The EndPage operator may be preceded by a PageCopies attribute |
---|
190 | # So set number of copies for current page. |
---|
191 | # From what I read in PCLXL documentation, the number |
---|
192 | # of copies is an unsigned 16 bits integer |
---|
193 | try : |
---|
194 | nbcopies = unpack(self.unpackShort, minfile[pos3-2:pos3])[0] |
---|
195 | # self.logdebug("Number of copies : %i" % nbcopies) |
---|
196 | self.pages[self.pagecount]["copies"] = nbcopies |
---|
197 | except KeyError : |
---|
198 | self.logdebug("It looks like this PCLXL file is corrupted.") |
---|
199 | return 0 |
---|
200 | |
---|
201 | def setColorSpace(self, nextpos) : |
---|
202 | """Changes the color space.""" |
---|
203 | if self.minfile[nextpos-4:nextpos-1] == self.RGBColorSpace : # TODO : doesn't seem to handle all cases ! |
---|
204 | self.iscolor = True |
---|
205 | return 0 |
---|
206 | |
---|
207 | def array_Generic(self, nextpos, size) : |
---|
208 | """Handles all arrays.""" |
---|
209 | pos = nextpos |
---|
210 | datatype = ord(self.minfile[pos]) |
---|
211 | pos += 1 |
---|
212 | length = self.tags[datatype] |
---|
213 | if callable(length) : |
---|
214 | length = length(pos) |
---|
215 | try : |
---|
216 | return 1 + length + size * unpack(self.unpackType[length], self.minfile[pos:pos+length])[0] |
---|
217 | except KeyError : |
---|
218 | raise pdlparser.PDLParserError, "Error on array size at %x" % nextpos |
---|
219 | |
---|
220 | def array_8(self, nextpos) : |
---|
221 | """Handles byte arrays.""" |
---|
222 | return self.array_Generic(nextpos, 1) |
---|
223 | |
---|
224 | def array_16(self, nextpos) : |
---|
225 | """Handles 16 bits arrays.""" |
---|
226 | return self.array_Generic(nextpos, 2) |
---|
227 | |
---|
228 | def array_32(self, nextpos) : |
---|
229 | """Handles 32 bits arrays and Canon ImageRunner tags.""" |
---|
230 | minfile = self.minfile |
---|
231 | irtag = minfile[nextpos-1:nextpos+3] |
---|
232 | if irtag in (self.imagerunnermarker1, self.imagerunnermarker2) : |
---|
233 | # This is the beginning of a Canon ImageRunner tag |
---|
234 | # self.logdebug("Canon ImageRunner tag at %x" % (nextpos-1)) |
---|
235 | codop = minfile[nextpos+1:nextpos+3] |
---|
236 | length = unpack(">H", minfile[nextpos+7:nextpos+9])[0] |
---|
237 | # self.logdebug("Canon ImageRunner block length=%04x" % length) |
---|
238 | toskip = 19 |
---|
239 | if irtag != self.imagerunnermarker2 : |
---|
240 | toskip += length |
---|
241 | # self.logdebug("Canon ImageRunner skip until %x" % (nextpos+toskip)) |
---|
242 | return toskip |
---|
243 | else : |
---|
244 | # This is a normal PCLXL array |
---|
245 | return self.array_Generic(nextpos, 4) |
---|
246 | |
---|
247 | def embeddedDataSmall(self, nextpos) : |
---|
248 | """Handle small amounts of data.""" |
---|
249 | return 1 + ord(self.minfile[nextpos]) |
---|
250 | |
---|
251 | def embeddedData(self, nextpos) : |
---|
252 | """Handle normal amounts of data.""" |
---|
253 | return 4 + unpack(self.unpackLong, self.minfile[nextpos:nextpos+4])[0] |
---|
254 | |
---|
255 | def skipHPPCLXL(self, nextpos) : |
---|
256 | """Skip the 'HP-PCL XL' statement if needed.""" |
---|
257 | minfile = self.minfile |
---|
258 | if nextpos and (minfile[nextpos:nextpos+11] == " HP-PCL XL;") : |
---|
259 | pos = nextpos |
---|
260 | while minfile[pos] != '\n' : |
---|
261 | pos += 1 |
---|
262 | length = (pos - nextpos + 1) |
---|
263 | # self.logdebug("Skip HP PCLXL statement until %x" % (nextpos + length)) |
---|
264 | return length |
---|
265 | else : |
---|
266 | return 0 |
---|
267 | |
---|
268 | def littleEndian(self, nextpos) : |
---|
269 | """Toggles to little endianness.""" |
---|
270 | self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" } |
---|
271 | self.unpackShort = self.unpackType[2] |
---|
272 | self.unpackLong = self.unpackType[4] |
---|
273 | # self.logdebug("LittleEndian at %x" % (nextpos - 1)) |
---|
274 | return self.skipHPPCLXL(nextpos) |
---|
275 | |
---|
276 | def bigEndian(self, nextpos) : |
---|
277 | """Toggles to big endianness.""" |
---|
278 | self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" } |
---|
279 | self.unpackShort = self.unpackType[2] |
---|
280 | self.unpackLong = self.unpackType[4] |
---|
281 | # self.logdebug("BigEndian at %x" % (nextpos - 1)) |
---|
282 | return self.skipHPPCLXL(nextpos) |
---|
283 | |
---|
284 | def reservedForFutureUse(self, nextpos) : |
---|
285 | """Outputs something when a reserved byte is encountered.""" |
---|
286 | self.logdebug("Byte at %x is out of the PCLXL Protocol Class 2.0 Specification" % nextpos) |
---|
287 | return 0 |
---|
288 | |
---|
289 | def x46_class3(self, nextpos) : |
---|
290 | """Undocumented tag 0x46 in class 3.0 streams.""" |
---|
291 | pos = nextpos - 3 |
---|
292 | minfile = self.minfile |
---|
293 | val = ord(minfile[pos]) |
---|
294 | while val == 0xf8 : |
---|
295 | funcid = ord(minfile[pos+1]) |
---|
296 | try : |
---|
297 | offset = self.x46_functions[funcid] |
---|
298 | except KeyError : |
---|
299 | self.logdebug("Unexpected subfunction 0x%02x for undocumented tag 0x46 at %x" % (funcid, nextpos)) |
---|
300 | break |
---|
301 | else : |
---|
302 | pos -= offset |
---|
303 | length = self.tags[ord(self.minfile[pos])] |
---|
304 | if callable(length) : |
---|
305 | length = length(pos+1) |
---|
306 | if funcid == 0x92 : # we want to skip these blocks |
---|
307 | try : |
---|
308 | return unpack(self.unpackType[length], self.minfile[pos+1:pos+length+1])[0] |
---|
309 | except KeyError : |
---|
310 | raise pdlparser.PDLParserError, "Error on size '%s' at %x" % (length, pos+1) |
---|
311 | val = ord(minfile[pos]) |
---|
312 | return 0 |
---|
313 | |
---|
314 | def escape(self, nextpos) : |
---|
315 | """Handles the ESC code.""" |
---|
316 | pos = endpos = nextpos |
---|
317 | minfile = self.minfile |
---|
318 | if minfile[pos : pos+8] == r"%-12345X" : |
---|
319 | endpos = pos + 9 |
---|
320 | endmark = chr(0x0c) + chr(0x00) + chr(0x1b) |
---|
321 | asciilimit = chr(0x80) |
---|
322 | quotes = 0 |
---|
323 | while (minfile[endpos] not in endmark) and \ |
---|
324 | ((minfile[endpos] < asciilimit) or (quotes % 2)) : |
---|
325 | if minfile[endpos] == '"' : |
---|
326 | quotes += 1 |
---|
327 | endpos += 1 |
---|
328 | |
---|
329 | # Store this in a per page mapping. |
---|
330 | # NB : First time will be at page 0 (i.e. **before** page 1) ! |
---|
331 | stuff = self.escapedStuff.setdefault(self.pagecount, []) |
---|
332 | stuff.append(minfile[pos : endpos]) |
---|
333 | self.logdebug("Escaped datas : [%s]" % repr(minfile[pos : endpos])) |
---|
334 | return endpos - pos |
---|
335 | |
---|
336 | def skipKyoceraPrescribe(self, nextpos) : |
---|
337 | """Skips Kyocera Prescribe commands.""" |
---|
338 | pos = nextpos - 1 |
---|
339 | minfile = self.minfile |
---|
340 | if minfile[pos:pos+3] == "!R!" : |
---|
341 | while (pos - nextpos) < 1024 : # This is a realistic upper bound, to avoid infinite loops |
---|
342 | if (minfile[pos] == ";") and (minfile[pos-4:pos] == "EXIT") : |
---|
343 | pos += 1 |
---|
344 | prescribe = self.prescribeStuff.setdefault(self.pagecount, []) |
---|
345 | prescribe.append(minfile[nextpos-1:pos]) |
---|
346 | self.logdebug("Prescribe commands : [%s]" % repr(minfile[nextpos-1:pos])) |
---|
347 | break |
---|
348 | pos += 1 |
---|
349 | return (pos - nextpos) |
---|
350 | else : |
---|
351 | return 0 |
---|
352 | |
---|
353 | def getJobSize(self) : |
---|
354 | """Counts pages in a PCLXL (PCL6) document. |
---|
355 | |
---|
356 | Algorithm by Jerome Alet. |
---|
357 | |
---|
358 | The documentation used for this was : |
---|
359 | |
---|
360 | HP PCL XL Feature Reference |
---|
361 | Protocol Class 2.0 |
---|
362 | http://www.hpdevelopersolutions.com/downloads/64/358/xl_ref20r22.pdf |
---|
363 | |
---|
364 | Protocol Class 2.1 Supplement |
---|
365 | xl_ref21.pdf |
---|
366 | |
---|
367 | Protocol Class 3.0 Supplement |
---|
368 | xl_refsup30r089.pdf |
---|
369 | """ |
---|
370 | |
---|
371 | infileno = self.infile.fileno() |
---|
372 | self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
373 | |
---|
374 | self.iscolor = False |
---|
375 | |
---|
376 | found = False |
---|
377 | while not found : |
---|
378 | line = self.infile.readline() |
---|
379 | if not line : |
---|
380 | break |
---|
381 | pos = line.find(" HP-PCL XL;") |
---|
382 | if pos != -1 : |
---|
383 | found = True |
---|
384 | endian = ord(line[pos - 1]) |
---|
385 | if endian == 0x29 : |
---|
386 | self.littleEndian(0) |
---|
387 | elif endian == 0x28 : |
---|
388 | self.bigEndian(0) |
---|
389 | # elif endian == 0x27 : # TODO : This is the ASCII binding code : what does it do exactly ? |
---|
390 | # |
---|
391 | else : |
---|
392 | raise pdlparser.PDLParserError, "Unknown endianness marker 0x%02x at start !" % endian |
---|
393 | if not found : |
---|
394 | raise pdlparser.PDLParserError, "This file doesn't seem to be PCLXL (aka PCL6)" |
---|
395 | |
---|
396 | # Initialize Media Sources |
---|
397 | for i in range(8, 256) : |
---|
398 | self.mediasources[i] = "ExternalTray%03i" % (i - 7) |
---|
399 | |
---|
400 | # Initialize table of tags |
---|
401 | self.tags = [ 0 ] * 256 |
---|
402 | |
---|
403 | self.tags[0x1b] = self.escape # The escape code |
---|
404 | |
---|
405 | self.tags[0x21] = self.skipKyoceraPrescribe # 0x21 is not normally used |
---|
406 | |
---|
407 | # GhostScript's sources tell us that HP printers |
---|
408 | # only accept little endianness, but we can handle both. |
---|
409 | self.tags[0x28] = self.bigEndian # BigEndian |
---|
410 | self.tags[0x29] = self.littleEndian # LittleEndian |
---|
411 | |
---|
412 | self.tags[0x43] = self.beginPage # BeginPage |
---|
413 | self.tags[0x44] = self.endPage # EndPage |
---|
414 | self.tags[0x45] = self.reservedForFutureUse # reserved |
---|
415 | |
---|
416 | self.tags[0x46] = self.x46_class3 |
---|
417 | |
---|
418 | self.tags[0x4a] = self.reservedForFutureUse # reserved |
---|
419 | self.tags[0x4b] = self.reservedForFutureUse # reserved |
---|
420 | self.tags[0x4c] = self.reservedForFutureUse # reserved |
---|
421 | self.tags[0x4d] = self.reservedForFutureUse # reserved |
---|
422 | self.tags[0x4e] = self.reservedForFutureUse # reserved |
---|
423 | |
---|
424 | self.tags[0x56] = self.reservedForFutureUse # TODO : documentation not clear about reserved status |
---|
425 | |
---|
426 | self.tags[0x57] = self.reservedForFutureUse # reserved |
---|
427 | |
---|
428 | self.tags[0x59] = self.reservedForFutureUse # reserved |
---|
429 | self.tags[0x5a] = self.reservedForFutureUse # reserved |
---|
430 | |
---|
431 | self.tags[0x6a] = self.setColorSpace # to detect color/b&w mode |
---|
432 | |
---|
433 | self.tags[0x87] = self.reservedForFutureUse # reserved |
---|
434 | self.tags[0x88] = self.reservedForFutureUse # reserved |
---|
435 | self.tags[0x89] = self.reservedForFutureUse # reserved |
---|
436 | self.tags[0x8a] = self.reservedForFutureUse # reserved |
---|
437 | |
---|
438 | self.tags[0x8b] = self.reservedForFutureUse # reserved |
---|
439 | |
---|
440 | self.tags[0x8c] = self.reservedForFutureUse # reserved |
---|
441 | self.tags[0x8d] = self.reservedForFutureUse # reserved |
---|
442 | self.tags[0x8e] = self.reservedForFutureUse # reserved |
---|
443 | self.tags[0x8f] = self.reservedForFutureUse # reserved |
---|
444 | self.tags[0x90] = self.reservedForFutureUse # reserved |
---|
445 | |
---|
446 | self.tags[0x9a] = self.reservedForFutureUse # reserved |
---|
447 | self.tags[0x9c] = self.reservedForFutureUse # reserved |
---|
448 | |
---|
449 | self.tags[0xa4] = self.reservedForFutureUse # reserved |
---|
450 | self.tags[0xa5] = self.reservedForFutureUse # reserved |
---|
451 | self.tags[0xa6] = self.reservedForFutureUse # reserved |
---|
452 | self.tags[0xa7] = self.reservedForFutureUse # reserved |
---|
453 | |
---|
454 | self.tags[0xaa] = self.reservedForFutureUse # reserved |
---|
455 | self.tags[0xab] = self.reservedForFutureUse # reserved |
---|
456 | self.tags[0xac] = self.reservedForFutureUse # reserved |
---|
457 | self.tags[0xad] = self.reservedForFutureUse # reserved |
---|
458 | self.tags[0xae] = self.reservedForFutureUse # reserved |
---|
459 | self.tags[0xaf] = self.reservedForFutureUse # reserved |
---|
460 | |
---|
461 | self.tags[0xb7] = self.reservedForFutureUse # reserved |
---|
462 | |
---|
463 | self.tags[0xba] = self.reservedForFutureUse # reserved |
---|
464 | self.tags[0xbb] = self.reservedForFutureUse # reserved |
---|
465 | self.tags[0xbc] = self.reservedForFutureUse # reserved |
---|
466 | self.tags[0xbd] = self.reservedForFutureUse # reserved |
---|
467 | self.tags[0xbe] = self.reservedForFutureUse # reserved |
---|
468 | |
---|
469 | # self.tags[0xbf] = self.passThrough # PassThrough mode should already be taken care of automatically |
---|
470 | |
---|
471 | self.tags[0xc0] = 1 # ubyte |
---|
472 | self.tags[0xc1] = 2 # uint16 |
---|
473 | self.tags[0xc2] = 4 # uint32 |
---|
474 | self.tags[0xc3] = 2 # sint16 |
---|
475 | self.tags[0xc4] = 4 # sint32 |
---|
476 | self.tags[0xc5] = 4 # real32 |
---|
477 | |
---|
478 | self.tags[0xc6] = self.reservedForFutureUse # reserved |
---|
479 | self.tags[0xc7] = self.reservedForFutureUse # reserved |
---|
480 | |
---|
481 | self.tags[0xc8] = self.array_8 # ubyte_array |
---|
482 | self.tags[0xc9] = self.array_16 # uint16_array |
---|
483 | self.tags[0xca] = self.array_32 # uint32_array |
---|
484 | self.tags[0xcb] = self.array_16 # sint16_array |
---|
485 | self.tags[0xcc] = self.array_32 # sint32_array |
---|
486 | self.tags[0xcd] = self.array_32 # real32_array and unfortunately Canon ImageRunner |
---|
487 | |
---|
488 | self.tags[0xce] = self.reservedForFutureUse # reserved |
---|
489 | self.tags[0xcf] = self.reservedForFutureUse # reserved |
---|
490 | |
---|
491 | self.tags[0xd0] = 2 # ubyte_xy |
---|
492 | self.tags[0xd1] = 4 # uint16_xy |
---|
493 | self.tags[0xd2] = 8 # uint32_xy |
---|
494 | self.tags[0xd3] = 4 # sint16_xy |
---|
495 | self.tags[0xd4] = 8 # sint32_xy |
---|
496 | self.tags[0xd5] = 8 # real32_xy |
---|
497 | self.tags[0xd6] = self.reservedForFutureUse # reserved |
---|
498 | self.tags[0xd7] = self.reservedForFutureUse # reserved |
---|
499 | self.tags[0xd8] = self.reservedForFutureUse # reserved |
---|
500 | self.tags[0xd9] = self.reservedForFutureUse # reserved |
---|
501 | self.tags[0xda] = self.reservedForFutureUse # reserved |
---|
502 | self.tags[0xdb] = self.reservedForFutureUse # reserved |
---|
503 | self.tags[0xdc] = self.reservedForFutureUse # reserved |
---|
504 | self.tags[0xdd] = self.reservedForFutureUse # reserved |
---|
505 | self.tags[0xde] = self.reservedForFutureUse # reserved |
---|
506 | self.tags[0xdf] = self.reservedForFutureUse # reserved |
---|
507 | |
---|
508 | self.tags[0xe0] = 4 # ubyte_box |
---|
509 | self.tags[0xe1] = 8 # uint16_box |
---|
510 | self.tags[0xe2] = 16 # uint32_box |
---|
511 | self.tags[0xe3] = 8 # sint16_box |
---|
512 | self.tags[0xe4] = 16 # sint32_box |
---|
513 | self.tags[0xe5] = 16 # real32_box |
---|
514 | self.tags[0xe6] = self.reservedForFutureUse # reserved |
---|
515 | self.tags[0xe7] = self.reservedForFutureUse # reserved |
---|
516 | self.tags[0xe8] = self.reservedForFutureUse # reserved |
---|
517 | self.tags[0xe9] = self.reservedForFutureUse # reserved |
---|
518 | self.tags[0xea] = self.reservedForFutureUse # reserved |
---|
519 | self.tags[0xeb] = self.reservedForFutureUse # reserved |
---|
520 | self.tags[0xec] = self.reservedForFutureUse # reserved |
---|
521 | self.tags[0xed] = self.reservedForFutureUse # reserved |
---|
522 | self.tags[0xee] = self.reservedForFutureUse # reserved |
---|
523 | self.tags[0xef] = self.reservedForFutureUse # reserved |
---|
524 | |
---|
525 | self.tags[0xf0] = self.reservedForFutureUse # reserved |
---|
526 | self.tags[0xf1] = self.reservedForFutureUse # reserved |
---|
527 | self.tags[0xf2] = self.reservedForFutureUse # reserved |
---|
528 | self.tags[0xf3] = self.reservedForFutureUse # reserved |
---|
529 | self.tags[0xf4] = self.reservedForFutureUse # reserved |
---|
530 | self.tags[0xf5] = self.reservedForFutureUse # reserved |
---|
531 | self.tags[0xf6] = self.reservedForFutureUse # reserved |
---|
532 | self.tags[0xf7] = self.reservedForFutureUse # reserved |
---|
533 | |
---|
534 | self.tags[0xf8] = 1 # attr_ubyte |
---|
535 | self.tags[0xf9] = 2 # attr_uint16 |
---|
536 | |
---|
537 | self.tags[0xfa] = self.embeddedData # dataLength |
---|
538 | self.tags[0xfb] = self.embeddedDataSmall # dataLengthByte |
---|
539 | |
---|
540 | self.tags[0xfc] = self.reservedForFutureUse # reserved |
---|
541 | self.tags[0xfd] = self.reservedForFutureUse # reserved |
---|
542 | self.tags[0xfe] = self.reservedForFutureUse # reserved |
---|
543 | self.tags[0xff] = self.reservedForFutureUse # reserved |
---|
544 | |
---|
545 | # color spaces |
---|
546 | self.BWColorSpace = "".join([chr(0x00), chr(0xf8), chr(0x03)]) |
---|
547 | self.GrayColorSpace = "".join([chr(0x01), chr(0xf8), chr(0x03)]) |
---|
548 | self.RGBColorSpace = "".join([chr(0x02), chr(0xf8), chr(0x03)]) |
---|
549 | |
---|
550 | # set number of copies |
---|
551 | self.setNumberOfCopies = "".join([chr(0xf8), chr(0x31)]) |
---|
552 | |
---|
553 | # subcodes for undocumented tag 0x46 and the negative |
---|
554 | # offset to grab the value from. |
---|
555 | self.x46_functions = { 0x91 : 5, |
---|
556 | 0x92 : 5, |
---|
557 | 0x93 : 3, |
---|
558 | 0x94 : 3, |
---|
559 | 0x95 : 5, |
---|
560 | 0x96 : 2, |
---|
561 | 0x97 : 2, |
---|
562 | 0x98 : 2, |
---|
563 | } |
---|
564 | |
---|
565 | # Markers for Canon ImageRunner printers |
---|
566 | self.imagerunnermarker1 = chr(0xcd) + chr(0xca) + chr(0x10) + chr(0x00) |
---|
567 | self.imagerunnermarker2 = chr(0xcd) + chr(0xca) + chr(0x10) + chr(0x02) |
---|
568 | |
---|
569 | self.pages = { 0 : { "copies" : 1, |
---|
570 | "orientation" : "Default", |
---|
571 | "mediatype" : "Plain", |
---|
572 | "mediasize" : "Default", |
---|
573 | "mediasource" : "Default", |
---|
574 | "duplex" : None, |
---|
575 | } |
---|
576 | } |
---|
577 | tags = self.tags |
---|
578 | self.pagecount = 0 |
---|
579 | self.escapedStuff = {} # For escaped datas, mostly PJL commands |
---|
580 | self.prescribeStuff = {} # For Kyocera Prescribe commands |
---|
581 | pos = oldpos = 0 |
---|
582 | try : |
---|
583 | try : |
---|
584 | while 1 : |
---|
585 | try : |
---|
586 | tag = ord(minfile[pos]) |
---|
587 | except OverflowError : |
---|
588 | pos = oldpos + 1 |
---|
589 | pos += 1 |
---|
590 | length = tags[tag] |
---|
591 | if length : |
---|
592 | if callable(length) : |
---|
593 | length = length(pos) |
---|
594 | oldpos = pos |
---|
595 | pos += length |
---|
596 | except IndexError : # EOF ? |
---|
597 | pass |
---|
598 | finally : |
---|
599 | self.minfile.close() |
---|
600 | |
---|
601 | # now handle number of copies for each page (may differ). |
---|
602 | if self.iscolor : |
---|
603 | colormode = "Color" |
---|
604 | else : |
---|
605 | colormode = "BW" |
---|
606 | |
---|
607 | defaultduplexmode = "Simplex" |
---|
608 | defaultpapersize = "" |
---|
609 | defaultpjlcopies = 1 |
---|
610 | oldpjlcopies = -1 |
---|
611 | oldduplexmode = "" |
---|
612 | oldpapersize = "" |
---|
613 | for pnum in range(1, self.pagecount + 1) : |
---|
614 | # if no number of copies defined, take 1, as explained |
---|
615 | # in PCLXL documentation. |
---|
616 | # NB : is number of copies is 0, the page won't be output |
---|
617 | # but the formula below is still correct : we want |
---|
618 | # to decrease the total number of pages in this case. |
---|
619 | page = self.pages.get(pnum, self.pages.get(1, { "copies" : 1, "mediasize" : "Default", "duplex" : None })) |
---|
620 | pjlstuff = self.escapedStuff.get(pnum, self.escapedStuff.get(0, [])) |
---|
621 | if pjlstuff : |
---|
622 | pjlparser = pjl.PJLParser("".join(pjlstuff)) |
---|
623 | nbdefaultcopies = int(pjlparser.default_variables.get("COPIES", -1)) |
---|
624 | nbcopies = int(pjlparser.environment_variables.get("COPIES", -1)) |
---|
625 | nbdefaultqty = int(pjlparser.default_variables.get("QTY", -1)) |
---|
626 | nbqty = int(pjlparser.environment_variables.get("QTY", -1)) |
---|
627 | if nbdefaultcopies > -1 : |
---|
628 | defaultpjlcopies = nbdefaultcopies |
---|
629 | if nbdefaultqty > -1 : |
---|
630 | defaultpjlcopies = nbdefaultqty |
---|
631 | if nbcopies > -1 : |
---|
632 | pjlcopies = nbcopies |
---|
633 | elif nbqty > -1 : |
---|
634 | pjlcopies = nbqty |
---|
635 | else : |
---|
636 | if oldpjlcopies == -1 : |
---|
637 | pjlcopies = defaultpjlcopies |
---|
638 | else : |
---|
639 | pjlcopies = oldpjlcopies |
---|
640 | if page["duplex"] : |
---|
641 | duplexmode = page["duplex"] |
---|
642 | else : |
---|
643 | defaultdm = pjlparser.default_variables.get("DUPLEX", "") |
---|
644 | if defaultdm : |
---|
645 | if defaultdm.upper() == "ON" : |
---|
646 | defaultduplexmode = "Duplex" |
---|
647 | else : |
---|
648 | defaultduplexmode = "Simplex" |
---|
649 | envdm = pjlparser.environment_variables.get("DUPLEX", "") |
---|
650 | if envdm : |
---|
651 | if envdm.upper() == "ON" : |
---|
652 | duplexmode = "Duplex" |
---|
653 | else : |
---|
654 | duplexmode = "Simplex" |
---|
655 | else : |
---|
656 | if not oldduplexmode : |
---|
657 | duplexmode = defaultduplexmode |
---|
658 | else : |
---|
659 | duplexmode = oldduplexmode |
---|
660 | defaultps = pjlparser.default_variables.get("PAPER", "") |
---|
661 | if defaultps : |
---|
662 | defaultpapersize = defaultps |
---|
663 | envps = pjlparser.environment_variables.get("PAPER", "") |
---|
664 | if envps : |
---|
665 | papersize = envps |
---|
666 | else : |
---|
667 | if not oldpapersize : |
---|
668 | papersize = defaultpapersize |
---|
669 | else : |
---|
670 | papersize = oldpapersize |
---|
671 | else : |
---|
672 | if oldpjlcopies == -1 : |
---|
673 | pjlcopies = defaultpjlcopies |
---|
674 | else : |
---|
675 | pjlcopies = oldpjlcopies |
---|
676 | if not oldduplexmode : |
---|
677 | duplexmode = defaultduplexmode |
---|
678 | else : |
---|
679 | duplexmode = oldduplexmode |
---|
680 | if not oldpapersize : |
---|
681 | papersize = defaultpapersize |
---|
682 | else : |
---|
683 | papersize = oldpapersize |
---|
684 | duplexmode = oldduplexmode |
---|
685 | papersize = oldpapersize or page["mediasize"] |
---|
686 | if page["mediasize"] != "Default" : |
---|
687 | papersize = page["mediasize"] |
---|
688 | if not duplexmode : |
---|
689 | duplexmode = oldduplexmode or defaultduplexmode |
---|
690 | oldpjlcopies = pjlcopies |
---|
691 | oldduplexmode = duplexmode |
---|
692 | oldpapersize = papersize |
---|
693 | copies = max(pjlcopies, page["copies"]) # Was : pjlcopies * page["copies"] |
---|
694 | self.pagecount += (copies - 1) |
---|
695 | self.logdebug("%s*%s*%s*%s*%s*%s*%s" % (copies, |
---|
696 | page["mediatype"], |
---|
697 | papersize, |
---|
698 | page["orientation"], |
---|
699 | page["mediasource"], |
---|
700 | duplexmode, |
---|
701 | colormode)) |
---|
702 | return self.pagecount |
---|