module DYI::SvgElement

@since 0.0.0

Public Instance Methods

draw_on(canvas) click to toggle source
# File lib/dyi/svg_element.rb, line 27
def draw_on(canvas)
  canvas.child_elements.push(self)
  self.root_node = canvas.root_node
  self
end
puts_as_png(io=$>) click to toggle source
# File lib/dyi/svg_element.rb, line 43
def puts_as_png(io=$>)
  io.puts(to_png)
end
puts_as_svg(io=$>) click to toggle source
# File lib/dyi/svg_element.rb, line 39
def puts_as_svg(io=$>)
  io.puts(to_svg)
end
save(file_name, format=nil) click to toggle source
# File lib/dyi/svg_element.rb, line 47
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) click to toggle source
# File lib/dyi/svg_element.rb, line 33
def save_as_svg(file_name)
  open(file_name, "w+b") {|io|
    puts_as_svg(io)
  }
end
to_png() click to toggle source
# File lib/dyi/svg_element.rb, line 105
def to_png
  IO.popen('rsvg-convert', 'w+') {|pipe|
    puts_as_svg(pipe)
    pipe.close_write
    pipe.read
  }
end
to_svg(xml=nil) click to toggle source
# File lib/dyi/svg_element.rb, line 82
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

Private Instance Methods

child_elements_to_svg(xml) click to toggle source
# File lib/dyi/svg_element.rb, line 144
def child_elements_to_svg(xml)
  child_elements.each {|child|
    child.to_svg(xml)
  }
end
draw_children?() click to toggle source
# File lib/dyi/svg_element.rb, line 115
def draw_children?
  not child_elements.empty?
end
name_to_attribute(name) click to toggle source
# File lib/dyi/svg_element.rb, line 136
def name_to_attribute(name)
  name.to_s.gsub(/_/,'-')
end
root_attributes() click to toggle source
# File lib/dyi/svg_element.rb, line 140
def root_attributes
  {}
end
svg_attributes() click to toggle source
# File lib/dyi/svg_element.rb, line 119
def svg_attributes
  attrs =
      attributes.inject({}) do |hash, (key, value)|
        hash[name_to_attribute(key).to_sym] = value if value
        hash
      end
  if respond_to?(:style) && style && !style.empty?
    sty =
        style.inject([]) do |array, (key, value)|
          array << "#{name_to_attribute(key)}:#{value}" if value
          array
        end
    attrs[:style] = sty.join(';')
  end
  attrs
end