Module: Rough::Renderer
- Defined in:
- lib/rough/renderer.rb
Defined Under Namespace
Classes: RenderHelper
Class Method Summary collapse
- ._offset(min, max, ops, roughness_gain = 1) ⇒ Object
- ._offset_opt(x, ops, roughness_gain = 1) ⇒ Object
-
._random(ops) ⇒ Object
Internal methods (public for RenderHelper access).
- .arc(x, y, width, height, start, stop, closed, rough_closure, o) ⇒ Object
- .curve(input_points, o) ⇒ Object
- .double_line_fill_ops(x1, y1, x2, y2, o) ⇒ Object
- .ellipse(x, y, width, height, o) ⇒ Object
- .ellipse_with_params(x, y, o, ellipse_params) ⇒ Object
- .generate_ellipse_params(width, height, o) ⇒ Object
- .helper ⇒ Object
- .line(x1, y1, x2, y2, o) ⇒ Object
- .linear_path(points, close, o) ⇒ Object
- .pattern_fill_arc(x, y, width, height, start, stop, o) ⇒ Object
- .pattern_fill_polygons(polygon_list, o) ⇒ Object
- .polygon(points, o) ⇒ Object
- .rand_offset(x, o) ⇒ Object
- .rand_offset_with_range(min, max, o) ⇒ Object
- .rectangle(x, y, width, height, o) ⇒ Object
-
.solid_fill_polygon(polygon_list, o) ⇒ Object
Fills.
- .svg_path(path, o) ⇒ Object
Class Method Details
._offset(min, max, ops, roughness_gain = 1) ⇒ Object
256 257 258 |
# File 'lib/rough/renderer.rb', line 256 def self._offset(min, max, ops, roughness_gain = 1) ops.roughness * roughness_gain * ((_random(ops) * (max - min)) + min) end |
._offset_opt(x, ops, roughness_gain = 1) ⇒ Object
260 261 262 |
# File 'lib/rough/renderer.rb', line 260 def self._offset_opt(x, ops, roughness_gain = 1) _offset(-x, x, ops, roughness_gain) end |
._random(ops) ⇒ Object
Internal methods (public for RenderHelper access)
249 250 251 252 253 254 |
# File 'lib/rough/renderer.rb', line 249 def self._random(ops) unless ops.randomizer ops.randomizer = Random.new(ops.seed || 0) end ops.randomizer.next end |
.arc(x, y, width, height, start, stop, closed, rough_closure, o) ⇒ Object
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 132 133 134 |
# File 'lib/rough/renderer.rb', line 100 def arc(x, y, width, height, start, stop, closed, rough_closure, o) cx = x cy = y rx = (width / 2.0).abs ry = (height / 2.0).abs rx += _offset_opt(rx * 0.01, o) ry += _offset_opt(ry * 0.01, o) strt = start stp = stop while strt < 0 strt += Math::PI * 2 stp += Math::PI * 2 end if (stp - strt) > (Math::PI * 2) strt = 0 stp = Math::PI * 2 end ellipse_inc = (Math::PI * 2) / o.curve_step_count arc_inc = [ellipse_inc / 2.0, (stp - strt) / 2.0].min ops = _arc(arc_inc, cx, cy, rx, ry, strt, stp, 1, o) unless o.disable_multi_stroke o2 = _arc(arc_inc, cx, cy, rx, ry, strt, stp, 1.5, o) ops.concat(o2) end if closed if rough_closure ops.concat(_double_line(cx, cy, cx + rx * Math.cos(strt), cy + ry * Math.sin(strt), o)) ops.concat(_double_line(cx, cy, cx + rx * Math.cos(stp), cy + ry * Math.sin(stp), o)) else ops << Op.new(op: :lineTo, data: [cx, cy]) ops << Op.new(op: :lineTo, data: [cx + rx * Math.cos(strt), cy + ry * Math.sin(strt)]) end end OpSet.new(type: :path, ops: ops) end |
.curve(input_points, o) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rough/renderer.rb', line 43 def curve(input_points, o) if input_points.length > 0 p1 = input_points[0] points_list = p1[0].is_a?(Numeric) ? [input_points] : input_points o1 = _curve_with_offset(points_list[0], 1 * (1 + o.roughness * 0.2), o) o2 = o.disable_multi_stroke ? [] : _curve_with_offset(points_list[0], 1.5 * (1 + o.roughness * 0.22), (o)) (1...points_list.length).each do |i| pts = points_list[i] next unless pts.length > 0 underlay = _curve_with_offset(pts, 1 * (1 + o.roughness * 0.2), o) = o.disable_multi_stroke ? [] : _curve_with_offset(pts, 1.5 * (1 + o.roughness * 0.22), (o)) underlay.each { |item| o1 << item if item.op != :move } .each { |item| o2 << item if item.op != :move } end OpSet.new(type: :path, ops: o1 + o2) else OpSet.new(type: :path, ops: []) end end |
.double_line_fill_ops(x1, y1, x2, y2, o) ⇒ Object
220 221 222 |
# File 'lib/rough/renderer.rb', line 220 def double_line_fill_ops(x1, y1, x2, y2, o) _double_line(x1, y1, x2, y2, o, true) end |
.ellipse(x, y, width, height, o) ⇒ Object
66 67 68 69 |
# File 'lib/rough/renderer.rb', line 66 def ellipse(x, y, width, height, o) params = generate_ellipse_params(width, height, o) ellipse_with_params(x, y, o, params)[:opset] end |
.ellipse_with_params(x, y, o, ellipse_params) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rough/renderer.rb', line 83 def ellipse_with_params(x, y, o, ellipse_params) ap1, cp1 = _compute_ellipse_points( ellipse_params[:increment], x, y, ellipse_params[:rx], ellipse_params[:ry], 1, ellipse_params[:increment] * _offset(0.1, _offset(0.4, 1, o), o), o ) o1 = _curve_ops(ap1, nil, o) if !o.disable_multi_stroke && o.roughness != 0 ap2, _ = _compute_ellipse_points(ellipse_params[:increment], x, y, ellipse_params[:rx], ellipse_params[:ry], 1.5, 0, o) o2 = _curve_ops(ap2, nil, o) o1 += o2 end { estimated_points: cp1, opset: OpSet.new(type: :path, ops: o1) } end |
.generate_ellipse_params(width, height, o) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rough/renderer.rb', line 71 def generate_ellipse_params(width, height, o) psq = Math.sqrt(Math::PI * 2 * Math.sqrt(((width / 2.0)**2 + (height / 2.0)**2) / 2.0)) step_count = [o.curve_step_count, (o.curve_step_count / Math.sqrt(200)) * psq].max.ceil increment = (Math::PI * 2) / step_count rx = (width / 2.0).abs ry = (height / 2.0).abs curve_fit_randomness = 1 - o.curve_fitting rx += _offset_opt(rx * curve_fit_randomness, o) ry += _offset_opt(ry * curve_fit_randomness, o) {increment: increment, rx: rx, ry: ry} end |
.helper ⇒ Object
224 225 226 |
# File 'lib/rough/renderer.rb', line 224 def helper @helper ||= RenderHelper.new end |
.line(x1, y1, x2, y2, o) ⇒ Object
12 13 14 |
# File 'lib/rough/renderer.rb', line 12 def line(x1, y1, x2, y2, o) OpSet.new(type: :path, ops: _double_line(x1, y1, x2, y2, o)) end |
.linear_path(points, close, o) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rough/renderer.rb', line 16 def linear_path(points, close, o) len = (points || []).length if len > 2 ops = [] (0...(len - 1)).each do |i| ops.concat(_double_line(points[i][0], points[i][1], points[i + 1][0], points[i + 1][1], o)) end if close ops.concat(_double_line(points[len - 1][0], points[len - 1][1], points[0][0], points[0][1], o)) end OpSet.new(type: :path, ops: ops) elsif len == 2 line(points[0][0], points[0][1], points[1][0], points[1][1], o) else OpSet.new(type: :path, ops: []) end end |
.pattern_fill_arc(x, y, width, height, start, stop, o) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rough/renderer.rb', line 183 def pattern_fill_arc(x, y, width, height, start, stop, o) cx = x cy = y rx = (width / 2.0).abs ry = (height / 2.0).abs rx += _offset_opt(rx * 0.01, o) ry += _offset_opt(ry * 0.01, o) strt = start stp = stop while strt < 0 strt += Math::PI * 2 stp += Math::PI * 2 end if (stp - strt) > (Math::PI * 2) strt = 0 stp = Math::PI * 2 end increment = (stp - strt) / o.curve_step_count points = [] angle = strt while angle <= stp points << [cx + rx * Math.cos(angle), cy + ry * Math.sin(angle)] angle += increment end points << [cx + rx * Math.cos(stp), cy + ry * Math.sin(stp)] points << [cx, cy] pattern_fill_polygons([points], o) end |
.pattern_fill_polygons(polygon_list, o) ⇒ Object
178 179 180 181 |
# File 'lib/rough/renderer.rb', line 178 def pattern_fill_polygons(polygon_list, o) require_relative "fillers/registry" Fillers.get(o, helper).fill_polygons(polygon_list, o) end |
.polygon(points, o) ⇒ Object
34 35 36 |
# File 'lib/rough/renderer.rb', line 34 def polygon(points, o) linear_path(points, true, o) end |
.rand_offset(x, o) ⇒ Object
212 213 214 |
# File 'lib/rough/renderer.rb', line 212 def rand_offset(x, o) _offset_opt(x, o) end |
.rand_offset_with_range(min, max, o) ⇒ Object
216 217 218 |
# File 'lib/rough/renderer.rb', line 216 def rand_offset_with_range(min, max, o) _offset(min, max, o) end |
.rectangle(x, y, width, height, o) ⇒ Object
38 39 40 41 |
# File 'lib/rough/renderer.rb', line 38 def rectangle(x, y, width, height, o) points = [[x, y], [x + width, y], [x + width, y + height], [x, y + height]] polygon(points, o) end |
.solid_fill_polygon(polygon_list, o) ⇒ Object
Fills
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rough/renderer.rb', line 163 def solid_fill_polygon(polygon_list, o) ops = [] polygon_list.each do |points| next unless points.length > 0 offset = o.max_randomness_offset || 0 if points.length > 2 ops << Op.new(op: :move, data: [points[0][0] + _offset_opt(offset, o), points[0][1] + _offset_opt(offset, o)]) (1...points.length).each do |i| ops << Op.new(op: :lineTo, data: [points[i][0] + _offset_opt(offset, o), points[i][1] + _offset_opt(offset, o)]) end end end OpSet.new(type: :fillPath, ops: ops) end |
.svg_path(path, o) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/rough/renderer.rb', line 136 def svg_path(path, o) segments = PathDataParser.normalize(PathDataParser.absolutize(PathDataParser.parse(path))) ops = [] first = [0, 0] current = [0, 0] segments.each do |seg| case seg.key when "M" current = [seg.data[0], seg.data[1]] first = [seg.data[0], seg.data[1]] when "L" ops.concat(_double_line(current[0], current[1], seg.data[0], seg.data[1], o)) current = [seg.data[0], seg.data[1]] when "C" x1, y1, x2, y2, x, y = seg.data ops.concat(_bezier_to(x1, y1, x2, y2, x, y, current, o)) current = [x, y] when "Z" ops.concat(_double_line(current[0], current[1], first[0], first[1], o)) current = [first[0], first[1]] end end OpSet.new(type: :path, ops: ops) end |