Class: Inkmake::InkscapeRemote

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInkscapeRemote

Returns a new instance of InkscapeRemote.



116
117
118
119
120
121
122
# File 'lib/inkmake.rb', line 116

def initialize
  open
  probe_decimal_symbol
  yield self
ensure
  quit
end

Class Method Details

.escape(s) ⇒ Object



264
265
266
# File 'lib/inkmake.rb', line 264

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

.pathObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/inkmake.rb', line 268

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

  # try to figure out inkscape path
  p = (
    (["/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"
      "#{OSX::NSWorkspace.sharedWorkspace.fullPathForApplication:"Inkscape"}/Contents/Resources/bin/inkscape"
    rescue NameError, LoadError
      nil
    end
  end
end

Instance Method Details

#command(args) ⇒ Object



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

def command(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

#drawing_area(file) ⇒ Object



254
255
256
# File 'lib/inkmake.rb', line 254

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

#export(opts) ⇒ Object



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
231
232
233
# File 'lib/inkmake.rb', line 198

def export(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
  command(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

#ids(file) ⇒ Object



250
251
252
# File 'lib/inkmake.rb', line 250

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

#is_windowsObject



124
125
126
# File 'lib/inkmake.rb', line 124

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

#openObject



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

def open
  if is_windows
    # Inkscape on Windows for some reason needs to run from its binary dir
    @in, @out, @err = Open3.popen3(*[File.basename(self.class.path), "--shell"],
                                   :chdir => File.dirname(self.class.path))
  else
    @in, @out, @err = Open3.popen3(*[self.class.path, "--shell"])
  end
  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?)



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
# File 'lib/inkmake.rb', line 171

def probe_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")
  f.write(svg)
  f.flush
  begin
    command({
      "--file" => f.path,
      "--export-png" => Tempfile.new("inkmake").path,
      "--export-area" => "0.0:0.0:1.0:1.0",
    })
    loop do
      case response
      when :prompt then break
      end
    end
    @decimal_symbol = "."
  rescue EOFError
    @decimal_symbol = ","
    # restart inkscape
    open
  end
end

#query_all(file) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/inkmake.rb', line 235

def query_all(file)
  ids = []
  command({
    "--file" => file,
    "--query-all" => nil,
  })
  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
  ids
end

#quitObject



258
259
260
261
262
# File 'lib/inkmake.rb', line 258

def quit
  command({"quit" => nil})
  @out.read
  nil
end

#responseObject



156
157
158
159
160
161
162
163
164
165
# File 'lib/inkmake.rb', line 156

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