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