Module: DYI::SvgElement

Included in:
Drawing::Filter::DropShadow
Defined in:
lib/dyi/svg_element.rb

Overview

Since:

  • 0.0.0

Instance Method Summary collapse

Instance Method Details

#draw_on(canvas) ⇒ Object

Since:

  • 0.0.0



28
29
30
31
32
# File 'lib/dyi/svg_element.rb', line 28

def draw_on(canvas)
  canvas.child_elements.push(self)
  self.root_node = canvas.root_node
  self
end

#puts_as_png(io = $>) ⇒ Object

Since:

  • 0.0.0



44
45
46
# File 'lib/dyi/svg_element.rb', line 44

def puts_as_png(io=$>)
  io.puts(to_png)
end

#puts_as_svg(io = $>) ⇒ Object

Since:

  • 0.0.0



40
41
42
# File 'lib/dyi/svg_element.rb', line 40

def puts_as_svg(io=$>)
  io.puts(to_svg)
end

#save(file_name, format = nil) ⇒ Object

Since:

  • 0.0.0



48
49
50
51
52
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
# File 'lib/dyi/svg_element.rb', line 48

def save(file_name, format=nil)
  if format
    format = format.to_s.downcase
  else
    file_name.scan(/.\.([^\.]+)$/) {|s| format = s[0].downcase}
  end

  format ||= 'svg'

  if format == 'svg'
    save_as_svg(file_name)
  elsif format == 'png'
    begin
      save_as_png(file_name)
    rescue
      tmp_file_name = file_name + '.temp'
      save_as_svg(tmp_file_name)
      system "\"#{INKSCAPE_PATH}\" -z -T -f #{File.expand_path(tmp_file_name)} -e #{File.expand_path(file_name)}"
      File.delete(tmp_file_name)
    end
  else
    tmp_file_name = file_name + '.temp'
    save_as_svg(tmp_file_name)
    opt =
      case format
        when 'ps' then opt = '-P'
        when 'eps' then opt = '-E'
        when 'pdf' then opt = '-A'
        else raise ArgumentError, "Unimplement Format: #{format}"
      end
    system "\"#{INKSCAPE_PATH}\" -z -T -f #{File.expand_path(tmp_file_name)} #{opt} #{File.expand_path(file_name)}"
    File.delete(tmp_file_name)
  end
end

#save_as_svg(file_name) ⇒ Object

Since:

  • 0.0.0



34
35
36
37
38
# File 'lib/dyi/svg_element.rb', line 34

def save_as_svg(file_name)
  open(file_name, "w+b") {|io|
    puts_as_svg(io)
  }
end

#to_pngObject

Since:

  • 0.0.0



106
107
108
109
110
111
112
# File 'lib/dyi/svg_element.rb', line 106

def to_png
  IO.popen('rsvg-convert', 'w+') {|pipe|
    puts_as_svg(pipe)
    pipe.close_write
    pipe.read
  }
end

#to_svg(xml = nil) ⇒ Object

Since:

  • 0.0.0



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dyi/svg_element.rb', line 83

def to_svg(xml=nil)
  unless xml
    xml = Builder::XmlMarkup.new :indent=>2
    xml.instruct!
    xml.declare! :DOCTYPE, :svg, :PUBLIC, "-//W3C//DTD SVG 1.0//EN", "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd"
    xml.svg(root_attributes.merge(:xmlns=>"http://www.w3.org/2000/svg")) {
      to_svg(xml)
    }
  else
    if respond_to?(:svg_tag, true)
      if draw_children?
        xml.tag!(svg_tag, svg_attributes) {
          child_elements_to_svg(xml)
        }
      else
        xml.tag!(svg_tag, svg_attributes)
      end
    else
      child_elements_to_svg(xml)
    end
  end
end