Class: Rasem::SVGImage

Inherits:
SVGTagWithParent show all
Defined in:
lib/rasem/svg_image.rb

Instance Attribute Summary

Attributes inherited from SVGTagWithParent

#img

Attributes inherited from SVGTag

#attributes, #children, #tag

Instance Method Summary collapse

Methods inherited from SVGTag

#append_child, #linearGradient, #matrix, #merge_defaults, #method_missing, #path, #pop_defaults, #push_defaults, #radialGradient, #raw, #rotate, #scale, #skewX, #skewY, #spawn_child, #to_s, #translate, #use, #validate_attribute, #validate_attributes, #validate_child_name, #validate_tag, #with_style, #write_points, #write_styles, #write_transforms

Constructor Details

#initialize(params = {}, output = nil, &block) ⇒ SVGImage

Returns a new instance of SVGImage.



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rasem::SVGTag

Instance Method Details

#add_def(id, child, if_exists = :skip, &block) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# 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) ⇒ Object



469
470
471
472
# 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) ⇒ Object



494
495
496
497
498
499
500
501
# 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