Class: Inkmake::InkscapeRemote

Inherits:
Object
  • Object
show all
Defined in:
lib/inkmake.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInkscapeRemote

Returns a new instance of InkscapeRemote.



118
119
120
121
122
123
124
125
# File 'lib/inkmake.rb', line 118

def initialize
  @inkscape_version = probe_inkscape_version
  open_shell
  probe_decimal_symbol if @inkscape_version == 0
  yield self
ensure
  quit
end

Instance Attribute Details

#inkscape_versionObject (readonly)

Returns the value of attribute inkscape_version.



116
117
118
# File 'lib/inkmake.rb', line 116

def inkscape_version
  @inkscape_version
end

Class Method Details

.escape(s) ⇒ Object



388
389
390
# File 'lib/inkmake.rb', line 388

def self.escape(s)
  s.gsub(/([\\"'])/, '\\\\\1')
end

.pathObject



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/inkmake.rb', line 392

def self.path
  return Inkmake.inkscape_path if Inkmake.inkscape_path

  # try to figure out inkscape path
  p = ( 
    (["/Applications/Inkscape.app/Contents/MacOS/inkscape",
      "/Applications/Inkscape.app/Contents/Resources/bin/inkscape",
      'c:\Program Files\Inkscape\inkscape.exe',
      'c:\Program Files (x86)\Inkscape\inkscape.exe'] +
      (ENV['PATH'].split(':').map {|p| File.join(p, "inkscape")}))
    .select do |path|
      File.exists? path
    end)
  .first
  if p
    p
  else
    begin
      require "osx/cocoa"
      app_path = OSX::NSWorkspace.sharedWorkspace.fullPathForApplication:"Inkscape"
      ["#{app_path}/Contents/MacOS/inkscape",
       "#{app_path}/Contents/Resources/bin/inkscape"]
      .select do |path|
        File.exists? path
      end
    rescue NameError, LoadError
      nil
    end
  end
end

Instance Method Details

#command0(args) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/inkmake.rb', line 152

def command0(args)
  c = args.collect do |key, value|
    if value
      "\"#{key}=#{self.class.escape value.to_s}\""
    else
      key
    end
  end.join(" ")
  puts "< #{c}" if Inkmake.verbose
  @in.write "#{c}\n"
  @in.flush
end

#command1(args) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/inkmake.rb', line 165

def command1(args)
  c = args.collect do |key, value|
    if value
      "#{key}:#{value.to_s}"
    else
      "#{key}:"
    end
  end.join("\n")
  puts "< #{c}" if Inkmake.verbose
  @in.write "#{c}\n"
  @in.flush
end

#drawing_area(file) ⇒ Object



374
375
376
# File 'lib/inkmake.rb', line 374

def drawing_area(file)
  query_all(file).first[1..-1]
end

#export(opts) ⇒ Object



337
338
339
340
341
342
343
# File 'lib/inkmake.rb', line 337

def export(opts)
  if @inkscape_version == 0 then
    export0(opts)
  else
    export1(opts)
  end
end

#export0(opts) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/inkmake.rb', line 247

def export0(opts)
  c = {
    "--file" => opts[:svg_path],
    "--export-#{opts[:format]}" => opts[:out_path]
  }
  if opts[:res]
    s = opts[:rotate_scale_hack] ? 2 : 1
    c["--export-width"] = opts[:res].width.to_pixels(opts[:dpi] || 90) * s
    c["--export-height"] = opts[:res].height.to_pixels(opts[:dpi] || 90) * s
  end
  if opts[:dpi]
    c["--export-dpi"] = opts[:dpi]
  end
  if opts[:area].kind_of? Array
    c["--export-area"] = ("%f:%f:%f:%f" % opts[:area]).gsub(".", @decimal_symbol)
  elsif opts[:area] == :drawing
    c["--export-area-drawing"] = nil
  elsif opts[:area].kind_of? String
    c["--export-id"] = opts[:area]
  end
  command0(c)
  width, height = [0, 0]
  #out = nil
  loop do
    case response
    # when /^Bitmap saved as: (.*)$/ then
    #   out = $1
    when /^Area .* exported to (\d+) x (\d+) pixels.*$/ then
      width = $1
      height = $2
    when :prompt then break
    end
  end

  [width, height]
end

#export1(opts) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/inkmake.rb', line 284

def export1(opts)
  c = [
    ["file-open", opts[:svg_path]],
    ["export-type", opts[:format]],
    ["export-filename", opts[:out_path]]
  ]
  if opts[:res]
    s = opts[:rotate_scale_hack] ? 2 : 1
    c += [["export-width", opts[:res].width.to_pixels(opts[:dpi] || 90) * s]]
    c += [["export-height", opts[:res].height.to_pixels(opts[:dpi] || 90) * s]]
  else
    c += [["export-width", ""]]
    c += [["export-height", ""]]
  end
  if opts[:dpi]
    c += [["export-dpi", opts[:dpi]]]
  end

  c += [["export-area", ""]]
  c += [["export-area-drawing", "false"]]
  c += [["export-id", ""]]
  c += [["export-area-page", "false"]]

  if opts[:area].kind_of? Array
    c += [["export-area", ("%f:%f:%f:%f" % opts[:area]).gsub(".", @decimal_symbol)]]
  elsif opts[:area] == :drawing
    c += [["export-area-drawing", "true"]]
  elsif opts[:area].kind_of? String
    c += [["export-id", opts[:area]]]
  else
    c += [["export-area-page", "true"]]
  end
  c.each do |a|
    command1([a])
    response
  end

  command1([["export-do"]])
  width, height = [0, 0]
  loop do
    case response
    when /^Area .* exported to (\d+) x (\d+) pixels.*$/ then
      width = $1
      height = $2
    when :prompt then break
    end
  end
  command1([["file-close"]])
  response

  [width, height]
end

#ids(file) ⇒ Object



370
371
372
# File 'lib/inkmake.rb', line 370

def ids(file)
  Hash[query_all(file).map {|l| [l[0], l[1..-1]]}]
end

#is_windowsObject



127
128
129
# File 'lib/inkmake.rb', line 127

def is_windows
  @is_windows ||= (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) != nil
end

#open(args) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/inkmake.rb', line 131

def open(args)
  if is_windows
    # Inkscape on Windows for some reason needs to run from its binary dir.
    # popen2e so get stdout and stderr in one pipe. inkscape 1 shell seems to
    # use both as output and we need to read to not block it.
    Open3.popen2e(*[File.basename(self.class.path)] + args,
                                   :chdir => File.dirname(self.class.path))
  else
    Open3.popen2e(*[self.class.path] + args)
  end
end

#open_shellObject



143
144
145
146
147
148
149
150
# File 'lib/inkmake.rb', line 143

def open_shell
  @in, @out = open(["--shell"])
  loop do
    case response
    when :prompt then break
    end
  end
end

#probe_decimal_symbolObject

this is weird but is the least weird and most protable way i could come up with to figuring out what decimal symbol to use. forcing LC_NUMERIC=C seems hard to do in a portable way trying to use env inkmake is running in is also not so portable (windows?)



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/inkmake.rb', line 218

def probe_decimal_symbol
  @decimal_symbol = "."
  svg =
    "<?xml version=\"1.0\"?>" +
    "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"1\" height=\"1\">" +
    "</svg>"
  f = Tempfile.new(["inkmake", ".svg"])
  f.write(svg)
  f.flush
  begin
    # this will try export with "." as symbol in area
    export({
      :svg_path => f.path,
      :out_path => Tempfile.new(["inkmake", ".png"]).path,
      :format => "png",
      :area => [0.0, 0.0, 1.0, 1.0]
    })
    loop do
      case response
      when :prompt then break
      end
    end
  rescue EOFError
    @decimal_symbol = ","
    # restart inkscape
    open_shell
  end
end

#probe_inkscape_versionObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/inkmake.rb', line 197

def probe_inkscape_version
  _in, out = open(["--version"])
  version = 0
  begin
    loop do
      case out.readline()
      when /^\s*Inkscape 1\..*$/ then
        version = 1
      when /^\s*Inkscape 0\..*$/ then
        version = 0
      end
    end
  rescue EOFError
  end
  version
end

#query_all(file) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/inkmake.rb', line 345

def query_all(file)
  ids = []
  if @inkscape_version == 0 then
    command0({
      "--file" => file,
      "--query-all" => nil,
    })
  else
    command1([["file-open", file]])
    response
    command1([["query-all", file]])
  end
  loop do
    case response
    when /^(.*),(.*),(.*),(.*),(.*)$/ then ids << [$1, $2.to_f, $3.to_f, $4.to_f, $5.to_f]
    when :prompt then break
    end
  end
  if @inkscape_version == 1 then
    command1([["file-close", file]])
    response
  end
  ids
end

#quitObject



378
379
380
381
382
383
384
385
386
# File 'lib/inkmake.rb', line 378

def quit
  if @inkscape_version == 0 then
    command0({"quit" => nil})
  else
    @in.close
  end
  @out.read
  nil
end

#responseObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/inkmake.rb', line 178

def response
  if @inkscape_version == 0
    o = @out.read(1)
    if o == ">"
      puts "1> #{o}" if Inkmake.verbose
      return :prompt;
    end
  else
    o = @out.read(2)
    if o == "> "
      puts "1> #{o}" if Inkmake.verbose
      return :prompt;
    end
  end
  o = o + @out.readline
  puts "2> #{o}" if Inkmake.verbose
  o
end