Class: Bio::Graphics::SVGEE

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/graphics/svgee.rb

Overview

The Bio::Graphics::SVGEE class takes argument information in a hash and creates SVG Markup tags, which it will draw

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SVGEE

Creates a new Bio::Graphics::SVGEE object which will contain all the necessary objects to display all the features on a page

args

  • :width = the width of the SVG page (100%)

  • :height = the amount of the page height the svg should take up (100%)

  • :style = the svg style information



13
14
15
16
17
18
19
20
21
# File 'lib/bio/graphics/svgee.rb', line 13

def initialize(args)
  opts = {:width => '100%', :height => '100%'}.merge!(args)
  @width = opts[:width]
  @height = opts[:height]
  @style= opts[:style]
  @primitives = []
  @defs = []
  @supported_primitives = [:circle, :rectangle, :ellipse, :line, :polyline, :text]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(primitive, args = {}) ⇒ Object (private)

only used to dynamically select the primitive type..

Raises:

  • (NoMethodError)


90
91
92
93
# File 'lib/bio/graphics/svgee.rb', line 90

def method_missing(primitive, args={}) #only used to dynamically select the primitive type.. 
  raise NoMethodError if not self.supported_primitives.include?(primitive) #we're only doing the listed primitive types...
  self.send "make_tag", primitive, args 
end

Instance Attribute Details

#defsObject (readonly)

Returns the value of attribute defs.



6
7
8
# File 'lib/bio/graphics/svgee.rb', line 6

def defs
  @defs
end

#primitivesObject (readonly)

Returns the value of attribute primitives.



6
7
8
# File 'lib/bio/graphics/svgee.rb', line 6

def primitives
  @primitives
end

#supported_primitivesObject (readonly)

Returns the value of attribute supported_primitives.



6
7
8
# File 'lib/bio/graphics/svgee.rb', line 6

def supported_primitives
  @supported_primitives
end

Instance Method Details

#add_primitive(primitive_object) ⇒ Object

Adds a Primitive object to the SVGEE object and makes the svg text for that Primitive



97
98
99
100
101
102
# File 'lib/bio/graphics/svgee.rb', line 97

def add_primitive(primitive_object)
  args = {}
  primitive_object.instance_variables.each{|v| args[v.to_s.gsub(/@/,"").to_sym] = primitive_object.instance_variable_get(v)  }
  primitive_string = args.delete(:primitive)
  make_tag(primitive_string, args)
end

#close_tagObject

Produces the closing text for an svg file



29
30
31
# File 'lib/bio/graphics/svgee.rb', line 29

def close_tag
  %{</svg>}
end

#drawObject

Produces the svg text to display all the features on a Page



118
119
120
121
122
123
124
125
# File 'lib/bio/graphics/svgee.rb', line 118

def draw
  head = self.open_tag
  defstring = ""
  defstring = "<defs>\n" + self.defs.join("\n") + "\n </defs>\n" if not defs.empty?
  shapes = self.primitives.join("\n")
  close = self.close_tag
  head + defstring + shapes + close
end

#gradient(a) ⇒ Object

Takes the gradient information from a Glyph, which must be of type ‘radial’ or ‘linear’ and creates the svg text for that gradient

  • a = a gradient (a gradient type, a colour and the parameters for a given type)



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bio/graphics/svgee.rb', line 105

def gradient(a)
  definition_string = case a[:type]
  when :radial
    %{<radialGradient id="#{a[:id]}" cx="#{a[:cx]}%" cy="#{a[:cy]}%" r="#{a[:r]}%" fx="#{a[:fx]}%" fy="#{a[:fy]}%">}
  else
    %{<linearGradient id="#{a[:id]}" x1="#{a[:x1]}%" x2="#{a[:x2]}%" y1="#{a[:y1]}%" y2="#{a[:y2]}%">}
  end
  a[:stops].each do |s|
    definition_string = definition_string + "\n" + %{<stop offset="#{s[:offset]}%" style="stop-color:#{s[:color]};stop-opacity:#{s[:opacity]}" />}
  end
  add_def definition_string + (a[:type] == :linear ? '</linearGradient>' : '</radialGradient>')
end

#open_tagObject

Produces the opening text for an svg file



24
25
26
# File 'lib/bio/graphics/svgee.rb', line 24

def open_tag
  %{<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="#{@width}" height="#{@height}" style="#{@style}" xmlns:xlink="http://www.w3.org/1999/xlink">}
end