class Rasem::SVGImage
Public Class Methods
new(params = {}, output=nil, &block)
click to toggle source
Calls superclass method
Rasem::SVGTagWithParent::new
# File lib/rasem/svg_image.rb, line 422 def initialize(params = {}, output=nil, &block) @defs = nil @defs_ids = {} params[:"version"] = "1.1" unless params[:"version"] params[:"xmlns"] = "http://www.w3.org/2000/svg" unless params[:"xmlns"] params[:"xmlns:xlink"] = "http://www.w3.org/1999/xlink" unless params[:"xmlns:xlink"] super(self, "svg", params, &block) @output = (output or "") validate_output(@output) if output if block write(@output) end end
Public Instance Methods
add_def(id, child, if_exists = :skip, &block)
click to toggle source
# File lib/rasem/svg_image.rb, line 440 def add_def(id, child, if_exists = :skip, &block) #init on the fly if needed @defs = Rasem::SVGTagWithParent.new(@img, "defs") if @defs.nil? #raise an error if id is already present and if_exists is :fail raise "Definition '#{id}' already exists" if @defs_ids.has_key? id and if_exists == :fail #return the existing element if id is already present and if_exists is :skip return @defs_ids[id] if if_exists == :skip and @defs_ids.has_key? id #search for the existing element if @defs_ids[id] old_idx = nil @defs.children.each_with_index { |c,i| if c.attributes[:id] == id then old_idx = i ; break end } end #force the id, append the child to definitions and call the given block to fill the group child.attributes[:id] = id @defs.append_child child @defs_ids[id] = child child.instance_exec &block #remove the old element if present @defs.children.delete_at old_idx if old_idx return child end
def_group(id, if_exists = :skip, &block)
click to toggle source
# File lib/rasem/svg_image.rb, line 469 def def_group(id, if_exists = :skip, &block) g = Rasem::SVGTagWithParent.new(@img, "g", :id => id) return add_def(id, g, if_exists, &block) end
write(output)
click to toggle source
def text(x, y, text, style=DefaultStyles)
@output << %Q{<text x="#{x}" y="#{y}"} style = fix_style(default_style.merge(style)) @output << %Q{ font-family="#{style.delete "font-family"}"} if style["font-family"] @output << %Q{ font-size="#{style.delete "font-size"}"} if style["font-size"] write_style style @output << ">" dy = 0 # First line should not be shifted text.each_line do |line| @output << %Q{<tspan x="#{x}" dy="#{dy}em">} dy = 1 # Next lines should be shifted @output << line.rstrip @output << "</tspan>" end @output << "</text>"
end
Calls superclass method
Rasem::SVGTag#write
# File lib/rasem/svg_image.rb, line 494 def write(output) validate_output(output) write_header(output) @children.unshift @defs if @defs super(output) @children.shift if @defs end
Private Instance Methods
validate_output(output)
click to toggle source
how to define output << image ?
def <<(output)
write(output)
end
# File lib/rasem/svg_image.rb, line 512 def validate_output(output) raise "Illegal output object: #{output.inspect}" unless output.respond_to?(:<<) end
write_header(output)
click to toggle source
Writes file header
# File lib/rasem/svg_image.rb, line 518 def write_header(output) output << <<-HEADER <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> HEADER end