Class: Rasem::SVGImage

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

Instance Attribute Summary collapse

Attributes inherited from SVGTagWithParent

#img

Attributes inherited from SVGTag

#attributes, #children, #tag

Instance Method Summary collapse

Methods inherited from SVGTag

#append_child, #linearGradient, #lines, #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


434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/rasem/svg_image.rb', line 434

def initialize(params = {}, output=nil, &block)
  @defs = nil
  @defs_ids = {}
  @options = {lean: false}

  if !params[:lean].nil?
    @options[:lean] = params[:lean]
    params.delete(:lean)
  end
  params[:"version"] = "1.1" unless params[:"version"]
  if !options[:lean]
    params[:"xmlns"] = "http://www.w3.org/2000/svg" unless params[:"xmlns"]
    params[:"xmlns:xlink"] = "http://www.w3.org/1999/xlink" unless params[:"xmlns:xlink"]
  end
  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 Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


432
433
434
# File 'lib/rasem/svg_image.rb', line 432

def options
  @options
end

Instance Method Details

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


459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/rasem/svg_image.rb', line 459

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


488
489
490
491
# File 'lib/rasem/svg_image.rb', line 488

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


513
514
515
516
517
518
519
# File 'lib/rasem/svg_image.rb', line 513

def write(output)
  validate_output(output)
  write_header(output)
  @children.unshift @defs if @defs
  super(output)
  @children.shift if @defs
end