Class: Rough::SVG
- Inherits:
-
Object
- Object
- Rough::SVG
- Defined in:
- lib/rough/svg.rb
Instance Attribute Summary collapse
-
#generator ⇒ Object
readonly
Returns the value of attribute generator.
Class Method Summary collapse
-
.document(width, height, **options) ⇒ Object
Generate a complete SVG document.
Instance Method Summary collapse
- #arc(x, y, width, height, start, stop, closed: false, **options) ⇒ Object
- #circle(x, y, diameter, **options) ⇒ Object
- #curve(points, **options) ⇒ Object
- #draw(drawable) ⇒ Object
- #ellipse(x, y, width, height, **options) ⇒ Object
-
#initialize(**config_options) ⇒ SVG
constructor
A new instance of SVG.
-
#line(x1, y1, x2, y2, **options) ⇒ Object
Convenience shape methods.
- #linear_path(points, **options) ⇒ Object
- #path(d, **options) ⇒ Object
- #polygon(points, **options) ⇒ Object
- #rectangle(x, y, width, height, **options) ⇒ Object
Constructor Details
Instance Attribute Details
#generator ⇒ Object (readonly)
Returns the value of attribute generator.
7 8 9 |
# File 'lib/rough/svg.rb', line 7 def generator @generator end |
Class Method Details
.document(width, height, **options) ⇒ Object
Generate a complete SVG document
108 109 110 111 112 |
# File 'lib/rough/svg.rb', line 108 def self.document(width, height, **) svg = new(**) content = yield svg %(<svg xmlns="http://www.w3.org/2000/svg" width="#{width}" height="#{height}">#{content}</svg>) end |
Instance Method Details
#arc(x, y, width, height, start, stop, closed: false, **options) ⇒ Object
95 96 97 |
# File 'lib/rough/svg.rb', line 95 def arc(x, y, width, height, start, stop, closed: false, **) draw(@generator.arc(x, y, width, height, start, stop, closed: closed, **)) end |
#circle(x, y, diameter, **options) ⇒ Object
83 84 85 |
# File 'lib/rough/svg.rb', line 83 def circle(x, y, diameter, **) draw(@generator.circle(x, y, diameter, **)) end |
#curve(points, **options) ⇒ Object
99 100 101 |
# File 'lib/rough/svg.rb', line 99 def curve(points, **) draw(@generator.curve(points, **)) end |
#draw(drawable) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rough/svg.rb', line 13 def draw(drawable) sets = drawable.sets || [] o = drawable. || @generator. precision = o.fixed_decimal_place_digits parts = [] sets.each do |drawing| case drawing.type when :path attrs = { "d" => @generator.ops_to_path(drawing, precision), "stroke" => o.stroke, "stroke-width" => o.stroke_width.to_s, "fill" => "none" } if o.stroke_line_dash attrs["stroke-dasharray"] = o.stroke_line_dash.join(" ").strip end if o.stroke_line_dash_offset attrs["stroke-dashoffset"] = o.stroke_line_dash_offset.to_s end parts << _path_element(attrs) when :fillPath attrs = { "d" => @generator.ops_to_path(drawing, precision), "stroke" => "none", "stroke-width" => "0", "fill" => o.fill || "" } if drawable.shape == "curve" || drawable.shape == "polygon" attrs["fill-rule"] = "evenodd" end parts << _path_element(attrs) when :fillSketch fweight = o.fill_weight fweight = o.stroke_width / 2.0 if fweight < 0 attrs = { "d" => @generator.ops_to_path(drawing, precision), "stroke" => o.fill || "", "stroke-width" => fweight.to_s, "fill" => "none" } if o.fill_line_dash attrs["stroke-dasharray"] = o.fill_line_dash.join(" ").strip end if o.fill_line_dash_offset attrs["stroke-dashoffset"] = o.fill_line_dash_offset.to_s end parts << _path_element(attrs) end end "<g>#{parts.join}</g>" end |
#ellipse(x, y, width, height, **options) ⇒ Object
79 80 81 |
# File 'lib/rough/svg.rb', line 79 def ellipse(x, y, width, height, **) draw(@generator.ellipse(x, y, width, height, **)) end |
#line(x1, y1, x2, y2, **options) ⇒ Object
Convenience shape methods
71 72 73 |
# File 'lib/rough/svg.rb', line 71 def line(x1, y1, x2, y2, **) draw(@generator.line(x1, y1, x2, y2, **)) end |
#linear_path(points, **options) ⇒ Object
87 88 89 |
# File 'lib/rough/svg.rb', line 87 def linear_path(points, **) draw(@generator.linear_path(points, **)) end |
#path(d, **options) ⇒ Object
103 104 105 |
# File 'lib/rough/svg.rb', line 103 def path(d, **) draw(@generator.path(d, **)) end |
#polygon(points, **options) ⇒ Object
91 92 93 |
# File 'lib/rough/svg.rb', line 91 def polygon(points, **) draw(@generator.polygon(points, **)) end |
#rectangle(x, y, width, height, **options) ⇒ Object
75 76 77 |
# File 'lib/rough/svg.rb', line 75 def rectangle(x, y, width, height, **) draw(@generator.rectangle(x, y, width, height, **)) end |