53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/deplate/ps2ppm.rb', line 53
def run(file, opt={})
syntax unless file
level = 2
format = @default
margin = 0
overwrite = 0
orient = 'Portrait'
res = 72
gab = ''
tab = ''
values = []
level = true if opt['1']
format = opt['f'] if opt['f']
margin = Float(opt['m']) if opt['m']
overwrite = true if opt['o']
res = Integer(opt['r']) if opt['r']
gab = '-dGraphicsAlphaBits=4' if opt['g']
tab = '-dTextAlphaBits=4' if opt['t']
case format
when "jpeg"
values << "-dJPEGQ=100"
end
out_file1 = "#{file}.01.#{format}"
infile = file
file = file.sub( /\.[eE]?[Pp][Ss]$/, '' )
unless infile =~ /\.[eE]?[Pp][Ss]$/
infile += '.ps'
end
if FileTest.file?(out_file1) && (! overwrite)
raise "File exists: #{out_file1}";
end
found = false
level = 0
x1 = nil
y1 = nil
x2 = nil
y2 = nil
found = nil
postscript = nil
File.open( infile, 'rb' ) do |io|
postscript = io.readlines
end
postscript.each do |line|
if level == 0 and line =~ /^%%BoundingBox:\s*(-?\d+)\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)/
x1 = Integer($1)
y1 = Integer($2)
x2 = Integer($3)
y2 = Integer($4)
found = true
elsif level == 0 and line =~ /^%%Orientation:\s*(\S+)/
o = $1
if o =~ /^portrait$/i
orient = 'Portrait';
elsif o =~ /^landscape$/i
orient = 'Landscape'
elsif o =~ /^upside-?down$/i
orient = 'Upside-Down'
elsif o =~ /^seascape$/i
orient = 'Seascape'
end
elsif line =~ /^%%BeginDocument\b/
level += 1
elsif line =~ /^%%EndDocument\b/
level -= 1
end
end
found or raise "BoundingBox not found in #{file}.ps"
if opt['O']
case opt['O']
when /^portrait/i
orient = 'Portrait'
when /^landscape$/i
orient = 'Landscape'
when /^upside-?down$/i
orient = 'Upside-Down'
when /^seascape$/i
orient = 'Seascape'
else
raise 'Illegal orientation'
end
end
trans = ''
if margin != 0
trans << "#{margin} dup translate "
end
case orient
when 'Portrait'
w = x2 - x1
h = y2 - y1
when 'Landscape'
w = y2 - y1
h = x2 - x1
t = -h
trans << "-90 rotate #{t} 0 translate "
when 'Upside-Down'
w = x2 - x1
h = y2 - y1
ww = -w
hh = -h
trans << "180 rotate #{ww} #{hh} translate "
when 'Seascape'
w = y2 - y1
h = x2 - x1
t = -w
trans << "90 rotate 0 #{t} translate "
else
raise "Unknown orientation: %s" % orient
end
w = ((2.0 * margin + w) * res / 72.0).to_i
h = ((2.0 * margin + h) * res / 72.0).to_i
x = -x1
y = -y1
trans << "#{x} #{y} translate"
puts "#{file}.ps -> #{file}.%02d.#{format}"
if defined?(Deplate::External.get_app)
gsname = Deplate::External.get_app('gs', @gsname)
else
gsname = @gsname
end
cmdline = "#{gsname} -dDOINTERPOLATE #{values.join(" ")} #{gab} #{tab} -g#{w}x#{h} -r#{res} -sDEVICE=#{format} -sOutputFile=#{file}.%02d.#{format} -dNOPAUSE -"
IO.popen(cmdline, 'w') do |gs|
if level == 1
gs.puts <<LEV1HEAD
/showpage {
showpage
#{trans}
(.) print flush
} bind def
#{trans}
LEV1HEAD
else
gs.puts <<LEV2HEAD
<<
/BeginPage {
pop
#{trans}
}
/EndPage {
dup 0 eq {
pop
1 add 4 string cvs print ( ) print flush
true
} {
1 eq
exch pop
} ifelse
}
>> setpagedevice
LEV2HEAD
end
gs.puts postscript
gs.puts "\nquit\n"
return File.exist?(out_file1)
end
end
|