Class: RubySVGLight::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_svg.rb,
lib/ruby_svg_light.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Document

Returns a new instance of Document.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby_svg.rb', line 8

def initialize( options={} )
  @options            = options
  @options[:name]   ||= ''
  #@options[:width]  ||= 0
  #@options[:height] ||= 0
  @options[:stroke] ||= '#000000'
  @options[:stroke_width] ||= 1
  @options[:fill]   ||= 'white'
  @options[:font_size] ||= '24px'

  @width  = 0
  @height = 0
  @body   = []
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/ruby_svg.rb', line 6

def body
  @body
end

#fillObject

Returns the value of attribute fill.



5
6
7
# File 'lib/ruby_svg.rb', line 5

def fill
  @fill
end

#font_sizeObject

Returns the value of attribute font_size.



5
6
7
# File 'lib/ruby_svg.rb', line 5

def font_size
  @font_size
end

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/ruby_svg.rb', line 5

def height
  @height
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/ruby_svg.rb', line 5

def name
  @name
end

#strokeObject

Returns the value of attribute stroke.



5
6
7
# File 'lib/ruby_svg.rb', line 5

def stroke
  @stroke
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/ruby_svg.rb', line 5

def width
  @width
end

Instance Method Details

#calc_local_options(opts = {}) ⇒ Object

Private method, for allowing overide options



194
195
196
197
198
199
200
201
202
# File 'lib/ruby_svg_light.rb', line 194

def calc_local_options(opts={})
  local_opts = @options.dup
  opts.each_pair do |key,value|
    local_opts[key] = value
  end

  #return
  local_opts
end

#circle(x = 0, y = 0, radius = 10, opts = {}) ⇒ Object

Drawing methodds



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_svg_light.rb', line 44

def circle(x=0, y=0, radius=10)
  text = %{<circle
  cx="#{x}" 
  cy="#{y}" 
  r="#{radius}" 
  stroke="#{@options[:stroke]}"
  stroke-width="#{@options[:stroke_width]}" 
  fill="#{@options[:fill]}"/>
  }

  update_size(x+radius, y+radius)
  #The call below should be in method missing
  @body << text
end

#components_only_to_sObject



111
112
113
114
115
116
117
# File 'lib/ruby_svg.rb', line 111

def components_only_to_s
  text = ""  
  @body.each do |section|
    text << section
  end
  return text
end

#ellipse(x, y, rx, ry, opts = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby_svg_light.rb', line 88

def ellipse(x,y,rx,ry, opts={})
  local_options = calc_local_options(opts)
  text = %{<ellipse
   cx="#{x}" cy="#{y}"
   rx="#{rx}" ry="#{ry}"
   fill="#{local_options[:fill]}"
   stroke="#{local_options[:stroke]}"
   stroke-width="#{local_options[:stroke_width]}"
    />
  }
  update_size(x+rx, y+ry)
  @body << text
end

#finaliseObject



119
120
121
122
123
124
# File 'lib/ruby_svg.rb', line 119

def finalise
  #Maybe add the initial and closing things here so we are just noramlly dealing drawn components
  @body.insert(0,  header)
  @body.insert(-1, footer)

end


40
41
42
# File 'lib/ruby_svg.rb', line 40

def footer
  text = %{</svg>}
end

#headerObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_svg.rb', line 28

def header
  text = %{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg 
 width="100%"
 height="100%"
 version="1.1" 
 xmlns="http://www.w3.org/2000/svg">
 }
end

#line(x, y, w, h, opts = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby_svg.rb', line 62

def line(x,y,w,h)
  text = %{<path
   style="fill:none;stroke:#{@options[:stroke]};stroke-width:#{@options[:stroke_width]};stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
   d="m #{x},#{y} l #{w},#{h}"
   />
   }

  update_size(x+w, y+h)
  @body << text
end

#options(opts = {}) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/ruby_svg_light.rb', line 124

def options(opts={})
  opts.each_pair do |key,value|
    @options[key] = value
  end

  #return
  @options
end

#rectangle(x, y, width, height, opts = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby_svg.rb', line 73

def rectangle(x,y,width,height)
  text = %{<rect
   style="fill:#{@options[:fill]};stroke:#{@options[:stroke]};stroke-width:#{@options[:stroke_width]};stroke-linecap:butt;stroke-linejoin:miter;"
   width="#{width}"
   height="#{height}"
   x="#{x}"
   y="#{y}" />
   }
  update_size(x+width, y+height)
  @body << text
end

#text(x, y, input_text, opts = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ruby_svg.rb', line 85

def text(x,y, input_text, size=@options[:font_size])
  centre_text = "text-anchor:middle; dominant-baseline:central;"


  #Stroke is outline
  #fill is normal font. ie for text fill gets stroke colour
  text = %{<text
   xml:space="preserve"
   style="font-size:#{size};font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#{@options[:stroke]};fill-opacity:1;stroke:none;font-family:Sans;#{centre_text}"
   <tspan
     x="#{x}"
     y="#{y}">#{input_text}</tspan></text>
}
  #Do not know height or width of text only the anchor 
  update_size(x, y)
  @body << text
end

#to_file(filename) ⇒ Object

to_file includes header and footers for a complete svg file



182
183
184
185
186
187
188
189
190
# File 'lib/ruby_svg_light.rb', line 182

def to_file( filename )
  File.open(filename, 'w') do |f|
    f.puts self.header
    @body.each do |section|
      f.puts section
    end
    f.puts self.footer
  end
end

#to_sObject

To string includes header and footers



162
163
164
165
166
167
168
# File 'lib/ruby_svg_light.rb', line 162

def to_s
  text = ""  
  @body.each do |section|
    text << section
  end
  return text
end

#update_size(x, y) ⇒ Object

Internal helper function for updating dimensions



205
206
207
208
# File 'lib/ruby_svg_light.rb', line 205

def update_size(x,y)
  @width  = x if x > @width
  @height = y if y > @height
end