Class: GeoPattern::SVG

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_pattern/svg.rb

Instance Method Summary collapse

Constructor Details

#initializeSVG

Returns a new instance of SVG.



3
4
5
6
7
# File 'lib/geo_pattern/svg.rb', line 3

def initialize
  @width      = 100
  @height     = 100
  @svg_string = ""
end

Instance Method Details

#circle(cx, cy, r, args = {}) ⇒ Object



33
34
35
# File 'lib/geo_pattern/svg.rb', line 33

def circle(cx, cy, r, args={})
  @svg_string << %Q{<circle cx="#{cx}" cy="#{cy}" r="#{r}" #{write_args(args)} />}
end

#group(elements, args = {}) ⇒ Object



45
46
47
48
49
# File 'lib/geo_pattern/svg.rb', line 45

def group(elements, args={})
  @svg_string << %Q{<g #{write_args(args)}>}
  elements.each {|e| eval e}
  @svg_string << %Q{</g>}
end

#path(str, args = {}) ⇒ Object



37
38
39
# File 'lib/geo_pattern/svg.rb', line 37

def path(str, args={})
  @svg_string << %Q{<path d="#{str}" #{write_args(args)} />}
end

#polyline(str, args = {}) ⇒ Object



41
42
43
# File 'lib/geo_pattern/svg.rb', line 41

def polyline(str, args={})
  @svg_string << %Q{<polyline points="#{str}" #{write_args(args)} />}
end

#rect(x, y, width, height, args = {}) ⇒ Object



29
30
31
# File 'lib/geo_pattern/svg.rb', line 29

def rect(x, y, width, height, args={})
  @svg_string << %Q{<rect x="#{x}" y="#{y}" width="#{width}" height="#{height}" #{write_args(args)} />}
end

#set_height(height) ⇒ Object



13
14
15
# File 'lib/geo_pattern/svg.rb', line 13

def set_height(height)
  @height = height.floor
end

#set_width(width) ⇒ Object



9
10
11
# File 'lib/geo_pattern/svg.rb', line 9

def set_width(width)
  @width = width.floor
end

#svg_closerObject



21
22
23
# File 'lib/geo_pattern/svg.rb', line 21

def svg_closer
  "</svg>"
end

#svg_headerObject



17
18
19
# File 'lib/geo_pattern/svg.rb', line 17

def svg_header
  %Q{<svg xmlns="http://www.w3.org/2000/svg" width="#{@width}" height="#{@height}">}
end

#to_sObject



25
26
27
# File 'lib/geo_pattern/svg.rb', line 25

def to_s
  svg_header + @svg_string + svg_closer
end

#write_args(args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/geo_pattern/svg.rb', line 51

def write_args(args)
  str = ""
  args.each {|key, value|
    if value.is_a?(Hash)
      str << %Q{#{key}="}
      value.each {|k, v|
        str << %Q{#{k}:#{v};}
      } 
      str << %Q{" }
    else
      str << %Q{#{key}="#{value}" }
    end
  }
  str
end