Class: Rasem::SVGImage
- Inherits:
-
Object
- Object
- Rasem::SVGImage
- Defined in:
- lib/rasem/svg_image.rb
Constant Summary collapse
- DefaultStyle =
{:stroke=>"black", :fill=>"black"}
Instance Method Summary collapse
-
#circle(cx, cy, r, style = {}) ⇒ Object
Draw a circle given a center and a radius.
-
#close ⇒ Object
Closes the file.
- #closed? ⇒ Boolean
-
#ellipse(cx, cy, rx, ry, style = {}) ⇒ Object
Draw an circle given a center and two radii.
- #group(style = {}, &proc) ⇒ Object
-
#initialize(width, height, output = nil, &block) ⇒ SVGImage
constructor
A new instance of SVGImage.
-
#line(x1, y1, x2, y2, style = {}) ⇒ Object
Draw a straight line between the two end points.
- #output ⇒ Object
- #polygon(*args) ⇒ Object
- #polyline(*args) ⇒ Object
-
#rectangle(x, y, width, height, *args) ⇒ Object
Draw a rectangle or rounded rectangle.
- #set_height(new_height) ⇒ Object
- #set_width(new_width) ⇒ Object
- #with_style(style = {}, &proc) ⇒ Object
Constructor Details
#initialize(width, height, output = nil, &block) ⇒ SVGImage
Returns a new instance of SVGImage.
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/rasem/svg_image.rb', line 4 def initialize(width, height, output=nil, &block) @output = create_output(output) # Initialize a stack of default styles @default_styles = [DefaultStyle] write_header(width, height) if block self.instance_exec(&block) self.close end end |
Instance Method Details
#circle(cx, cy, r, style = {}) ⇒ Object
Draw a circle given a center and a radius
41 42 43 44 45 |
# File 'lib/rasem/svg_image.rb', line 41 def circle(cx, cy, r, style={}) @output << %Q{<circle cx="#{cx}" cy="#{cy}" r="#{r}"} write_style(style) @output << %Q{/>} end |
#close ⇒ Object
Closes the file. No more drawing is possible after this
82 83 84 85 |
# File 'lib/rasem/svg_image.rb', line 82 def close write_close @closed = true end |
#closed? ⇒ Boolean
91 92 93 |
# File 'lib/rasem/svg_image.rb', line 91 def closed? @closed end |
#ellipse(cx, cy, rx, ry, style = {}) ⇒ Object
Draw an circle given a center and two radii
67 68 69 70 71 |
# File 'lib/rasem/svg_image.rb', line 67 def ellipse(cx, cy, rx, ry, style={}) @output << %Q{<ellipse cx="#{cx}" cy="#{cy}" rx="#{rx}" ry="#{ry}"} write_style(style) @output << %Q{/>} end |
#group(style = {}, &proc) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rasem/svg_image.rb', line 106 def group(style={}, &proc) # Open the group @output << "<g" write_style(style) @output << ">" # Call the block self.instance_exec(&proc) # Close the group @output << "</g>" end |
#line(x1, y1, x2, y2, style = {}) ⇒ Object
Draw a straight line between the two end points
34 35 36 37 38 |
# File 'lib/rasem/svg_image.rb', line 34 def line(x1, y1, x2, y2, style={}) @output << %Q{<line x1="#{x1}" y1="#{y1}" x2="#{x2}" y2="#{y2}"} write_style(style) @output << %Q{/>} end |
#output ⇒ Object
87 88 89 |
# File 'lib/rasem/svg_image.rb', line 87 def output @output.to_s end |
#polygon(*args) ⇒ Object
73 74 75 |
# File 'lib/rasem/svg_image.rb', line 73 def polygon(*args) polything("polygon", *args) end |
#polyline(*args) ⇒ Object
77 78 79 |
# File 'lib/rasem/svg_image.rb', line 77 def polyline(*args) polything("polyline", *args) end |
#rectangle(x, y, width, height, *args) ⇒ Object
Draw a rectangle or rounded rectangle
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rasem/svg_image.rb', line 48 def rectangle(x, y, width, height, *args) style = (!args.empty? && args.last.is_a?(Hash)) ? args.pop : {} if args.length == 0 rx = ry = 0 elsif args.length == 1 rx = ry = args.pop elsif args.length == 2 rx, ry = args else raise "Illegal number of arguments to rectangle" end @output << %Q{<rect x="#{x}" y="#{y}" width="#{width}" height="#{height}"} @output << %Q{ rx="#{rx}" ry="#{ry}"} if rx && ry write_style(style) @output << %Q{/>} end |
#set_height(new_height) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/rasem/svg_image.rb', line 25 def set_height(new_height) if @output.respond_to?(:sub!) @output.sub!(/<svg width="([^"]+)" height="[^"]+"/, %Q{<svg width="\\1" height="#{new_height}"}) else raise "Cannot change width after initialization for this output" end end |
#set_width(new_width) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/rasem/svg_image.rb', line 17 def set_width(new_width) if @output.respond_to?(:sub!) @output.sub!(/<svg width="[^"]+"/, %Q{<svg width="#{new_width}"}) else raise "Cannot change width after initialization for this output" end end |
#with_style(style = {}, &proc) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rasem/svg_image.rb', line 95 def with_style(style={}, &proc) # Merge passed style with current default style updated_style = default_style.merge(style) # Push updated style to the stack @default_styles.push(updated_style) # Call the block self.instance_exec(&proc) # Pop style again to revert changes @default_styles.pop end |