Class: RailroadDiagrams::AlternatingSequence
- Inherits:
-
DiagramMultiContainer
- Object
- DiagramMultiContainer
- RailroadDiagrams::AlternatingSequence
- Defined in:
- lib/railroad_diagrams/alternating_sequence.rb,
sig/generated/railroad_diagrams/alternating_sequence.rbs
Overview
Draws two paths that cross between the entry and exit.
Class Method Summary collapse
Instance Method Summary collapse
- #add_crossovers(group, context, x, y, width) ⇒ Object
- #child_nodes ⇒ Array[DiagramItem]
- #expand_text(diagram, left, right, line) ⇒ Object
- #format(x, y, width) ⇒ AlternatingSequence
-
#initialize(*items, id: nil, cls: nil, attrs: {}) ⇒ AlternatingSequence
constructor
A new instance of AlternatingSequence.
- #measure(context) ⇒ Metrics
- #render_svg(context, x, y, width) ⇒ Svg::Element
- #render_text(context) ⇒ TextDiagram
- #svg_path(data) ⇒ Object
- #text_diagram ⇒ TextDiagram
- #text_gaps(context, outer, inner) ⇒ Object
- #to_s ⇒ String
Constructor Details
#initialize(*items, id: nil, cls: nil, attrs: {}) ⇒ AlternatingSequence
Returns a new instance of AlternatingSequence.
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 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 19 def initialize(*items, id: nil, cls: nil, attrs: {}) super('g', items, nil, nil, id: id, cls: cls, data_attrs: attrs) @needs_space = false arc = AR vert = VS first, second = @items arc_x = 1 / Math.sqrt(2) * arc * 2 arc_y = (1 - (1 / Math.sqrt(2))) * arc * 2 cross_y = [arc, vert].max cross_x = (cross_y - arc_y) + arc_x first_out = [ arc + arc, (cross_y / 2) + arc + arc, (cross_y / 2) + vert + first.down ].max @up = first_out + first.height + first.up second_in = [ arc + arc, (cross_y / 2) + arc + arc, (cross_y / 2) + vert + second.up ].max @down = second_in + second.height + second.down @height = 0 first_width = (first.needs_space ? 20 : 0) + first.width second_width = (second.needs_space ? 20 : 0) + second.width @width = (2 * arc) + [first_width, cross_x, second_width].max + (2 * arc) end |
Class Method Details
.new(*items, id: nil, cls: nil, attrs: {}) ⇒ AlternatingSequence
11 12 13 14 15 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 11 def self.new(*items, id: nil, cls: nil, attrs: {}) raise InvalidArgument, "AlternatingSequence takes exactly two arguments, but got #{items.size} arguments." unless items.size == 2 super end |
Instance Method Details
#add_crossovers(group, context, x, y, width) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 296 def add_crossovers(group, context, x, y, width) arc = context..arc_radius arc_x = 1 / Math.sqrt(2) * arc * 2 arc_y = (1 - (1 / Math.sqrt(2))) * arc * 2 cross_y = [arc, context..vertical_separation].max cross_x = (cross_y - arc_y) + arc_x = (width - (4 * arc) - cross_x) / 2 path = Svg::PathData.new(x + arc, y - (cross_y / 2) - arc, arc_radius: arc) path.arc('ws').h([0, ].max).arc_8('n', 'cw') .l(cross_x - arc_x, cross_y - arc_y).arc_8('sw', 'ccw').h([0, ].max).arc('ne') group << svg_path(path) path = Svg::PathData.new(x + arc, y + (cross_y / 2) + arc, arc_radius: arc) path.arc('wn').h([0, ].max).arc_8('s', 'ccw') .l(cross_x - arc_x, -(cross_y - arc_y)).arc_8('nw', 'cw').h([0, ].max).arc('se') group << svg_path(path) end |
#child_nodes ⇒ Array[DiagramItem]
290 291 292 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 290 def child_nodes @items.dup end |
#expand_text(diagram, left, right, line) ⇒ Object
318 319 320 321 322 323 324 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 318 def (diagram, left, right, line) rows = diagram.lines.each_with_index.map do |text, index| "#{index == diagram.entry ? line * left : ' ' * left}#{text}" \ "#{index == diagram.exit ? line * right : ' ' * right}" end TextDiagram.new(diagram.entry, diagram.exit, rows) end |
#format(x, y, width) ⇒ AlternatingSequence
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 59 def format(x, y, width) arc = AR gaps = determine_gaps(width, @width) Path.new(x, y).right(gaps[0]).add(self) x += gaps[0] Path.new(x + @width, y + @height).right(gaps[1]).add(self) # bounding box # Path(x+gaps[0], y).up(@up).right(@width).down(@up+@down).left(@width).up(@up).add(self) first, second = @items # top first_in = @up - first.up first_out = @up - first.up - first.height Path.new(x, y).arc('se').up(first_in - (2 * arc)).arc('wn').add(self) first.format(x + (2 * arc), y - first_in, @width - (4 * arc)).add(self) Path.new(x + @width - (2 * arc), y - first_out) .arc('ne').down(first_out - (2 * arc)).arc('ws').add(self) # bottom second_in = @down - second.down - second.height second_out = @down - second.down Path.new(x, y) .arc('ne') .down(second_in - (2 * arc)) .arc('ws') .add(self) second.format(x + (2 * arc), y + second_in, @width - (4 * arc)).add(self) Path.new(x + @width - (2 * arc), y + second_out) .arc('se').up(second_out - (2 * arc)).arc('wn').add(self) # crossover arc_x = 1 / Math.sqrt(2) * arc * 2 arc_y = (1 - (1 / Math.sqrt(2))) * arc * 2 cross_y = [arc, VS].max cross_x = (cross_y - arc_y) + arc_x = (@width - (4 * arc) - cross_x) / 2 Path.new(x + arc, y - (cross_y / 2) - arc) .arc('ws') .right() .arc_8('n', 'cw') .l(cross_x - arc_x, cross_y - arc_y) .arc_8('sw', 'ccw') .right() .arc('ne') .add(self) Path.new(x + arc, y + (cross_y / 2) + arc) .arc('wn') .right() .arc_8('s', 'ccw') .l(cross_x - arc_x, -(cross_y - arc_y)) .arc_8('nw', 'cw') .right() .arc('se') .add(self) self end |
#measure(context) ⇒ Metrics
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 185 def measure(context) arc = context..arc_radius vert = context..vertical_separation first, second = @items.map { |item| context.metrics(item) } arc_x = 1 / Math.sqrt(2) * arc * 2 arc_y = (1 - (1 / Math.sqrt(2))) * arc * 2 cross_y = [arc, vert].max cross_x = (cross_y - arc_y) + arc_x first_out = [arc + arc, (cross_y / 2) + arc + arc, (cross_y / 2) + vert + first.down].max up = first_out + first.height + first.up second_in = [arc + arc, (cross_y / 2) + arc + arc, (cross_y / 2) + vert + second.up].max down = second_in + second.height + second.down first_width = (first.needs_space ? 20 : 0) + first.width second_width = (second.needs_space ? 20 : 0) + second.width width = (2 * arc) + [first_width, cross_x, second_width].max + (2 * arc) Metrics.new(width: width, up: up, height: 0, down: down, needs_space: false) end |
#render_svg(context, x, y, width) ⇒ Svg::Element
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 236 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 209 def render_svg(context, x, y, width) metrics = context.metrics(self) arc = context..arc_radius gaps = context.gaps(width, metrics.width) group = Svg::Element.new('g', @attrs.dup) group << svg_path(Svg::PathData.new(x, y).h([0, gaps[0]].max)) x += gaps[0] group << svg_path(Svg::PathData.new(x + metrics.width, y + metrics.height).h([0, gaps[1]].max)) first, second = @items first_metrics, second_metrics = @items.map { |item| context.metrics(item) } first_in = metrics.up - first_metrics.up first_out = metrics.up - first_metrics.up - first_metrics.height group << svg_path(Svg::PathData.new(x, y, arc_radius: arc).arc('se').v(-[0, first_in - (2 * arc)].max).arc('wn')) group << context.render_svg(first, x + (2 * arc), y - first_in, metrics.width - (4 * arc)) path = Svg::PathData.new(x + metrics.width - (2 * arc), y - first_out, arc_radius: arc) group << svg_path(path.arc('ne').v([0, first_out - (2 * arc)].max).arc('ws')) second_in = metrics.down - second_metrics.down - second_metrics.height second_out = metrics.down - second_metrics.down group << svg_path(Svg::PathData.new(x, y, arc_radius: arc).arc('ne').v([0, second_in - (2 * arc)].max).arc('ws')) group << context.render_svg(second, x + (2 * arc), y + second_in, metrics.width - (4 * arc)) path = Svg::PathData.new(x + metrics.width - (2 * arc), y + second_out, arc_radius: arc) group << svg_path(path.arc('se').v(-[0, second_out - (2 * arc)].max).arc('wn')) add_crossovers(group, context, x, y, metrics.width) group end |
#render_text(context) ⇒ TextDiagram
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 240 def render_text(context) cross, bottom_left, bottom_right, top_left, top_right, line, vertical, tee_left, tee_right = context.parts.values_at('cross_diag', 'roundcorner_bot_left', 'roundcorner_bot_right', 'roundcorner_top_left', 'roundcorner_top_right', 'line', 'line_vertical', 'tee_left', 'tee_right') first = @items[0].render_text(context) second = @items[1].render_text(context) max_width = [TextDiagram.max_width(first, second), 4].max left_width, right_width = text_gaps(context, max_width, 0) left_lines = [] right_lines = [] separator = [] left_size, right_size = text_gaps(context, first.width, 0) diagram = (first, left_width - left_size, right_width - right_size, line) left_lines += [' ' * 2] * diagram.entry left_lines << (top_left + line) left_lines += ["#{vertical} "] * (diagram.height - diagram.entry - 1) left_lines << (bottom_left + line) right_lines += [' ' * 2] * diagram.entry right_lines << (line + top_right) right_lines += [" #{vertical}"] * (diagram.height - diagram.entry - 1) right_lines << (line + bottom_right) separator << ("#{line * (left_width - 1)}#{top_right} #{top_left}#{line * (right_width - 2)}") separator << ("#{' ' * (left_width - 1)} #{cross} #{' ' * (right_width - 2)}") separator << ("#{line * (left_width - 1)}#{bottom_right} #{bottom_left}#{line * (right_width - 2)}") left_lines << (' ' * 2) right_lines << (' ' * 2) left_size, right_size = text_gaps(context, second.width, 0) second = (second, left_width - left_size, right_width - right_size, line) diagram = diagram.append_below(second, separator, move_entry: true, move_exit: true) left_lines << (top_left + line) left_lines += ["#{vertical} "] * second.entry left_lines << (bottom_left + line) right_lines << (line + top_right) right_lines += [" #{vertical}"] * second.entry right_lines << (line + bottom_right) midpoint = first.height + (separator.size / 2) diagram = diagram.alter(new_entry: midpoint, new_exit: midpoint) left_td = TextDiagram.new(midpoint, midpoint, left_lines) right_td = TextDiagram.new(midpoint, midpoint, right_lines) diagram = left_td.append_right(diagram, '').append_right(right_td, '') TextDiagram.new(1, 1, [top_left, tee_left, bottom_left]) .append_right(diagram, '') .append_right(TextDiagram.new(1, 1, [top_right, tee_right, bottom_right]), '') end |
#svg_path(data) ⇒ Object
314 315 316 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 314 def svg_path(data) Svg::Element.new('path', { 'd' => data }, self_closing: true) end |
#text_diagram ⇒ TextDiagram
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 120 def text_diagram cross_diag, corner_bot_left, corner_bot_right, corner_top_left, corner_top_right, line, line_vertical, tee_left, tee_right = TextDiagram.get_parts( %w[ cross_diag roundcorner_bot_left roundcorner_bot_right roundcorner_top_left roundcorner_top_right line line_vertical tee_left tee_right ] ) first_td = @items[0].text_diagram second_td = @items[1].text_diagram max_width = [TextDiagram.max_width(first_td, second_td), 4].max left_width, right_width = TextDiagram.gaps(max_width, 0) left_lines = [] right_lines = [] separator = [] left_size, right_size = TextDiagram.gaps(first_td.width, 0) diagram_td = first_td.(left_width - left_size, right_width - right_size, 0, 0) left_lines += [' ' * 2] * diagram_td.entry left_lines << (corner_top_left + line) left_lines += ["#{line_vertical} "] * (diagram_td.height - diagram_td.entry - 1) left_lines << (corner_bot_left + line) right_lines += [' ' * 2] * diagram_td.entry right_lines << (line + corner_top_right) right_lines += [" #{line_vertical}"] * (diagram_td.height - diagram_td.entry - 1) right_lines << (line + corner_bot_right) separator << ("#{line * (left_width - 1)}#{corner_top_right} #{corner_top_left}#{line * (right_width - 2)}") separator << ("#{' ' * (left_width - 1)} #{cross_diag} #{' ' * (right_width - 2)}") separator << ("#{line * (left_width - 1)}#{corner_bot_right} #{corner_bot_left}#{line * (right_width - 2)}") left_lines << (' ' * 2) right_lines << (' ' * 2) left_size, right_size = TextDiagram.gaps(second_td.width, 0) second_td = second_td.(left_width - left_size, right_width - right_size, 0, 0) diagram_td = diagram_td.append_below(second_td, separator, move_entry: true, move_exit: true) left_lines << (corner_top_left + line) left_lines += ["#{line_vertical} "] * second_td.entry left_lines << (corner_bot_left + line) right_lines << (line + corner_top_right) right_lines += [" #{line_vertical}"] * second_td.entry right_lines << (line + corner_bot_right) mid_point = first_td.height + (separator.size / 2) diagram_td = diagram_td.alter(new_entry: mid_point, new_exit: mid_point) left_td = TextDiagram.new(mid_point, mid_point, left_lines) right_td = TextDiagram.new(mid_point, mid_point, right_lines) diagram_td = left_td.append_right(diagram_td, '').append_right(right_td, '') TextDiagram.new(1, 1, [corner_top_left, tee_left, corner_bot_left]) .append_right(diagram_td, '') .append_right(TextDiagram.new(1, 1, [corner_top_right, tee_right, corner_bot_right]), '') end |
#text_gaps(context, outer, inner) ⇒ Object
326 327 328 329 330 331 332 333 334 335 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 326 def text_gaps(context, outer, inner) difference = outer - inner case context..internal_alignment when :left then [0, difference] when :right then [difference, 0] else left = difference / 2 [left, difference - left] end end |
#to_s ⇒ String
50 51 52 53 |
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 50 def to_s items = @items.map(&:to_s).join(', ') "AlternatingSequence(#{items})" end |