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