Class: FreshVectorSalad::Fresh

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

Instance Method Summary collapse

Constructor Details

#initialize(options, callbacks) ⇒ Fresh

Returns a new instance of Fresh.



7
8
9
10
# File 'lib/fresh_vector_salad.rb', line 7

def initialize(options, callbacks)
  @o = options
  @callbacks = callbacks
end

Instance Method Details

#autoObject



12
13
14
15
# File 'lib/fresh_vector_salad.rb', line 12

def auto
  auto_compile
  auto_render
end

#auto_compileObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fresh_vector_salad.rb', line 17

def auto_compile
  puts "Compiling..."
  start = Time.now

  begin
    File.write "#{@o.naked_file}.svg",
      VectorSalad::ExportWithMagic.new(@o).export
    puts "Compiled in #{Time.now - start} seconds."
    auto_render
  rescue Exception => e
    puts e.inspect
    e.backtrace.each { |l| puts l }
    @callbacks.error
  end
end

#auto_renderObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fresh_vector_salad.rb', line 33

def auto_render
  puts "Rendering..."
  start = Time.now
  if @ink_i.nil?
    puts "Starting Inkscape"
    @ink_o, @ink_i, _x = start_inkscape
  end

  begin
    @ink_o.expect(">") do |result|
      result.each { |r| puts r }
      @ink_i.puts("\"#{@o.naked_file}.svg\" --export-background=white --export-dpi=#{@o.dpi} --export-png=\"#{@o.naked_file}.png\"")
    end
    @ink_o.expect("Bitmap saved") do |result|
      result.each { |r| puts r }
    end
    @callbacks.success
    puts "Rendered in #{Time.now - start} seconds."
  rescue Exception => e
    puts e.inspect
    e.backtrace.each { |l| puts l }
    @callbacks.error
  end
end

#start_inkscapeObject



58
59
60
61
# File 'lib/fresh_vector_salad.rb', line 58

def start_inkscape
  ink = "inkscape -z --shell"
  PTY.spawn(ink)
end