Class: Rough::Generator
- Inherits:
-
Object
- Object
- Rough::Generator
- Defined in:
- lib/rough/generator.rb
Instance Attribute Summary collapse
-
#default_options ⇒ Object
readonly
Returns the value of attribute default_options.
Class Method Summary collapse
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
- #ellipse(x, y, width, height, **options) ⇒ Object
-
#initialize(**config_options) ⇒ Generator
constructor
A new instance of Generator.
- #line(x1, y1, x2, y2, **options) ⇒ Object
- #linear_path(points, **options) ⇒ Object
- #ops_to_path(drawing, fixed_decimals = nil) ⇒ Object
- #path(d, **options) ⇒ Object
- #polygon(points, **options) ⇒ Object
- #rectangle(x, y, width, height, **options) ⇒ Object
- #to_paths(drawable) ⇒ Object
Constructor Details
#initialize(**config_options) ⇒ Generator
Returns a new instance of Generator.
14 15 16 |
# File 'lib/rough/generator.rb', line 14 def initialize(**) @default_options = ResolvedOptions.new(**) end |
Instance Attribute Details
#default_options ⇒ Object (readonly)
Returns the value of attribute default_options.
12 13 14 |
# File 'lib/rough/generator.rb', line 12 def @default_options end |
Class Method Details
Instance Method Details
#arc(x, y, width, height, start, stop, closed: false, **options) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rough/generator.rb', line 72 def arc(x, y, width, height, start, stop, closed: false, **) o = _o() paths = [] outline = Renderer.arc(x, y, width, height, start, stop, closed, true, o) if closed && o.fill if o.fill_style == "solid" = o.merge(disable_multi_stroke: true) shape = Renderer.arc(x, y, width, height, start, stop, true, false, ) shape.type = :fillPath paths << shape else paths << Renderer.pattern_fill_arc(x, y, width, height, start, stop, o) end end paths << outline if o.stroke != "none" _d("arc", paths, o) end |
#circle(x, y, diameter, **options) ⇒ Object
61 62 63 64 65 |
# File 'lib/rough/generator.rb', line 61 def circle(x, y, diameter, **) ret = ellipse(x, y, diameter, diameter, **) ret.shape = "circle" ret end |
#curve(points, **options) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rough/generator.rb', line 90 def curve(points, **) o = _o() paths = [] outline = Renderer.curve(points, o) if o.fill && o.fill != "none" if o.fill_style == "solid" fill_shape = Renderer.curve(points, o.merge( disable_multi_stroke: true, roughness: (o.roughness != 0) ? (o.roughness + o.fill_shape_roughness_gain) : 0 )) paths << OpSet.new(type: :fillPath, ops: _merged_shape(fill_shape.ops)) else poly_points = [] input_points = points if input_points.length > 0 p1 = input_points[0] points_list = p1[0].is_a?(Numeric) ? [input_points] : input_points points_list.each do |pts| if pts.length < 3 poly_points.concat(pts) elsif pts.length == 3 poly_points.concat( PointsOnCurve.points_on_bezier_curves( PointsOnCurve.curve_to_bezier([pts[0], pts[0], pts[1], pts[2]]), 10, (1 + o.roughness) / 2.0 ) ) else poly_points.concat( PointsOnCurve.points_on_bezier_curves( PointsOnCurve.curve_to_bezier(pts), 10, (1 + o.roughness) / 2.0 ) ) end end end paths << Renderer.pattern_fill_polygons([poly_points], o) if poly_points.length > 0 end end paths << outline if o.stroke != "none" _d("curve", paths, o) end |
#ellipse(x, y, width, height, **options) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rough/generator.rb', line 43 def ellipse(x, y, width, height, **) o = _o() paths = [] ellipse_params = Renderer.generate_ellipse_params(width, height, o) ellipse_response = Renderer.ellipse_with_params(x, y, o, ellipse_params) if o.fill if o.fill_style == "solid" shape = Renderer.ellipse_with_params(x, y, o, ellipse_params)[:opset] shape.type = :fillPath paths << shape else paths << Renderer.pattern_fill_polygons([ellipse_response[:estimated_points]], o) end end paths << ellipse_response[:opset] if o.stroke != "none" _d("ellipse", paths, o) end |
#line(x1, y1, x2, y2, **options) ⇒ Object
22 23 24 25 |
# File 'lib/rough/generator.rb', line 22 def line(x1, y1, x2, y2, **) o = _o() _d("line", [Renderer.line(x1, y1, x2, y2, o)], o) end |
#linear_path(points, **options) ⇒ Object
67 68 69 70 |
# File 'lib/rough/generator.rb', line 67 def linear_path(points, **) o = _o() _d("linearPath", [Renderer.linear_path(points, false, o)], o) end |
#ops_to_path(drawing, fixed_decimals = nil) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rough/generator.rb', line 189 def ops_to_path(drawing, fixed_decimals = nil) path = "" drawing.ops.each do |item| data = if fixed_decimals.is_a?(Integer) && fixed_decimals >= 0 item.data.map { |d| d.round(fixed_decimals) } else item.data end case item.op when :move path += "M#{data[0]} #{data[1]} " when :bcurveTo path += "C#{data[0]} #{data[1]}, #{data[2]} #{data[3]}, #{data[4]} #{data[5]} " when :lineTo path += "L#{data[0]} #{data[1]} " end end path.strip end |
#path(d, **options) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rough/generator.rb', line 148 def path(d, **) o = _o() paths = [] return _d("path", paths, o) unless d d = (d || "").tr("\n", " ").gsub(/-\s/, "-").gsub(/\s\s/, " ") has_fill = o.fill && o.fill != "transparent" && o.fill != "none" has_stroke = o.stroke != "none" simplified = !!(o.simplification && o.simplification < 1) distance = simplified ? (4 - 4 * (o.simplification || 1)) : ((1 + o.roughness) / 2.0) sets = PointsOnPath.points_on_path(d, 1, distance) shape = Renderer.svg_path(d, o) if has_fill if o.fill_style == "solid" if sets.length == 1 fill_shape = Renderer.svg_path(d, o.merge( disable_multi_stroke: true, roughness: (o.roughness != 0) ? (o.roughness + o.fill_shape_roughness_gain) : 0 )) paths << OpSet.new(type: :fillPath, ops: _merged_shape(fill_shape.ops)) else paths << Renderer.solid_fill_polygon(sets, o) end else paths << Renderer.pattern_fill_polygons(sets, o) end end if has_stroke if simplified sets.each { |set| paths << Renderer.linear_path(set, false, o) } else paths << shape end end _d("path", paths, o) end |
#polygon(points, **options) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rough/generator.rb', line 133 def polygon(points, **) o = _o() paths = [] outline = Renderer.linear_path(points, true, o) if o.fill paths << if o.fill_style == "solid" Renderer.solid_fill_polygon([points], o) else Renderer.pattern_fill_polygons([points], o) end end paths << outline if o.stroke != "none" _d("polygon", paths, o) end |
#rectangle(x, y, width, height, **options) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rough/generator.rb', line 27 def rectangle(x, y, width, height, **) o = _o() paths = [] outline = Renderer.rectangle(x, y, width, height, o) if o.fill points = [[x, y], [x + width, y], [x + width, y + height], [x, y + height]] paths << if o.fill_style == "solid" Renderer.solid_fill_polygon([points], o) else Renderer.pattern_fill_polygons([points], o) end end paths << outline if o.stroke != "none" _d("rectangle", paths, o) end |
#to_paths(drawable) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/rough/generator.rb', line 209 def to_paths(drawable) sets = drawable.sets || [] o = drawable. || @default_options paths = [] sets.each do |drawing| pi = case drawing.type when :path PathInfo.new( d: ops_to_path(drawing), stroke: o.stroke, stroke_width: o.stroke_width, fill: "none" ) when :fillPath PathInfo.new( d: ops_to_path(drawing), stroke: "none", stroke_width: 0, fill: o.fill || "none" ) when :fillSketch _fill_sketch_path(drawing, o) end paths << pi if pi end paths end |