26 |
|
|
27 |
dbg = 0 |
dbg = 0 |
28 |
|
|
29 |
|
def p(*s): |
30 |
|
print 'dscutil.py::', s |
31 |
|
|
32 |
def reliablePS2DSC(infile, outfile): |
def reliablePS2DSC(infile, outfile): |
33 |
"""Convert a postscript file to a .dsc file reliably. |
"""Convert a postscript file to a .dsc file reliably. |
34 |
Because several files have broken dsc comments, we |
Because several files have broken dsc comments, we |
93 |
|
|
94 |
class SinglePageInfo: |
class SinglePageInfo: |
95 |
def parseLine(self, str): |
def parseLine(self, str): |
96 |
if str.startswith("%%PageMedia:"): |
if str.startswith("%%PageMedia:"): |
97 |
self.media = str[12:].strip() |
self.media = str[12:].strip() |
98 |
elif str.startswith("%%PageOrientation:"): |
elif str.startswith("%%PageOrientation:"): |
99 |
self.orient = str[19:].strip() |
self.orient = str[19:].strip() |
100 |
|
|
101 |
def dsc2pageinfo(infile): |
def dsc2pageinfo(infile): |
102 |
f = open(infile) |
f = open(infile) |
110 |
i-=1 |
i-=1 |
111 |
# Look for DocumentMedia |
# Look for DocumentMedia |
112 |
documentmedia = [l for l in lines if l.startswith("%%DocumentMedia:")] |
documentmedia = [l for l in lines if l.startswith("%%DocumentMedia:")] |
113 |
|
if len(documentmedia) == 0: |
114 |
|
documentmedia=['%%DocumentMedia: A4 595 842 0 () ()'] |
115 |
|
p('No DocumentMedia found: using defaults.') |
116 |
assert len(documentmedia) == 1 |
assert len(documentmedia) == 1 |
117 |
media = _dsctokens(documentmedia[0][16:]) |
media = _dsctokens(documentmedia[0][16:]) |
118 |
assert len(media) % 6 == 0 |
assert len(media) % 6 == 0 |
119 |
mtypes = {} |
mtypes = {} |
120 |
|
dbg = 1 |
121 |
for i in range(0, len(media)/6): |
for i in range(0, len(media)/6): |
122 |
(name, w, h, weight, color, type) = media[i*6:(i+1)*6] |
(name, w, h, weight, color, type) = media[i*6:(i+1)*6] |
123 |
if dbg: |
if dbg: |
124 |
print "Docmedia '%(name)s' %(w)s %(h)s %(weight)s %(color)s %(type)s" \ |
p('Docmedia "%(name)s" %(w)s %(h)s %(weight)s %(color)s %(type)s' \ |
125 |
% locals() |
% locals()) |
126 |
mtypes[name] = (float(w),float(h)) |
mtypes[name] = (float(w),float(h)) |
127 |
|
|
128 |
# find the dsc defaults |
# find the dsc defaults |
155 |
spi.parseLine(l) |
spi.parseLine(l) |
156 |
pages.append(spi) |
pages.append(spi) |
157 |
|
|
158 |
pagemedias = [ mtypes[p.media] for p in pages ] |
for page in pages: |
159 |
|
if not hasattr(p, 'media'): |
160 |
|
p('No PageMedia found: using "%s".' % (mtypes.keys()[0])) |
161 |
|
page.media = mtypes.keys()[0] |
162 |
|
if not hasattr(p, 'orientation'): |
163 |
|
p('No PageOrientation found: using "portrait".') |
164 |
|
page.orientation = 'portrait' |
165 |
|
|
166 |
|
pagemedias = [ mtypes[page.media] for page in pages ] |
167 |
|
|
168 |
w = jarray.array([ m[0] for m in pagemedias ], 'f') |
w = jarray.array([ m[0] for m in pagemedias ], 'f') |
169 |
h = jarray.array([ m[1] for m in pagemedias ], 'f') |
h = jarray.array([ m[1] for m in pagemedias ], 'f') |