Class: Rough::SVG

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**config_options) ⇒ SVG

Returns a new instance of SVG.



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

def initialize(**config_options)
  @generator = Generator.new(**config_options)
end

Instance Attribute Details

#generatorObject (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, **options)
  svg = new(**options)
  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, **options)
  draw(@generator.arc(x, y, width, height, start, stop, closed: closed, **options))
end

#circle(x, y, diameter, **options) ⇒ Object



83
84
85
# File 'lib/rough/svg.rb', line 83

def circle(x, y, diameter, **options)
  draw(@generator.circle(x, y, diameter, **options))
end

#curve(points, **options) ⇒ Object



99
100
101
# File 'lib/rough/svg.rb', line 99

def curve(points, **options)
  draw(@generator.curve(points, **options))
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.options || @generator.default_options
  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, **options)
  draw(@generator.ellipse(x, y, width, height, **options))
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, **options)
  draw(@generator.line(x1, y1, x2, y2, **options))
end

#linear_path(points, **options) ⇒ Object



87
88
89
# File 'lib/rough/svg.rb', line 87

def linear_path(points, **options)
  draw(@generator.linear_path(points, **options))
end

#path(d, **options) ⇒ Object



103
104
105
# File 'lib/rough/svg.rb', line 103

def path(d, **options)
  draw(@generator.path(d, **options))
end

#polygon(points, **options) ⇒ Object



91
92
93
# File 'lib/rough/svg.rb', line 91

def polygon(points, **options)
  draw(@generator.polygon(points, **options))
end

#rectangle(x, y, width, height, **options) ⇒ Object



75
76
77
# File 'lib/rough/svg.rb', line 75

def rectangle(x, y, width, height, **options)
  draw(@generator.rectangle(x, y, width, height, **options))
end