Class: RSyntaxTree::SVGGraph
- Defined in:
- lib/rsyntaxtree/svg_graph.rb
Constant Summary collapse
- REGION_DEFAULT_COLOR =
Default color for a region shade drawn with bare ‘%’ (no color spec). An explicit ‘%@color:’ is always honored, even in color-off mode, to stay consistent with how the @color: node-text color behaves.
"#888888"- REGION_FILL_OPACITY =
Fill opacity for region shades; low enough that nested/overlapping shades stack naturally without obscuring the tree underneath.
0.2- REGION_STROKE_OPACITY =
The border reuses the fill color but at a higher opacity, so it reads as a darker shade of the same color — keeping the region clearly bounded on a white page without a per-color “darker shade” lookup.
0.55
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#collect_region_shades ⇒ Object
Collect a background rectangle for every element flagged with ‘%’.
- #draw_a_path(s_x, s_y, t_x, t_y, target_arrow = :none) ⇒ Object
- #draw_bracket(x1, y1, width, height, col, bline = false) ⇒ Object
- #draw_element(element) ⇒ Object
- #draw_paths ⇒ Object
- #draw_rectangle(x1, y1, width, height, col, bline = false) ⇒ Object
- #generate_connectors(x1, y1, x2, y2, col, dashed = false, s_arrow = false, t_arrow = false, bline = false) ⇒ Object
- #generate_line(x1, y1, x2, y2, col, dashed = false, arrow = false, bline = false) ⇒ Object
-
#initialize(element_list, params, global) ⇒ SVGGraph
constructor
A new instance of SVGGraph.
-
#line_to_parent(parent, child) ⇒ Object
Draw a line between child/parent elements.
- #set_tspan(this_x, this_y, style, decoration, fontstyle, text) ⇒ Object
- #svg_data ⇒ Object
-
#triangle_to_parent(parent, child) ⇒ Object
Draw a triangle between child/parent elements.
Methods inherited from BaseGraph
#calculate_height, #calculate_indent, #calculate_level, #calculate_width, #draw_connector, #draw_elements, #finalize_ltr, #get_leftmost, #get_rightmost, #make_balance, #node_centering, #parse_list, #prepare_ltr, #subtree_bounds
Constructor Details
#initialize(element_list, params, global) ⇒ SVGGraph
Returns a new instance of SVGGraph.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 30 def initialize(element_list, params, global) super(element_list, params, global) @height = 0 @width = 0 @extra_lines = [] @region_shades = [] @fontset = params[:fontset] @fontsize = params[:fontsize] @linewidth = params[:linewidth] @transparent = params[:transparent] @color = params[:color] @fontstyle = params[:fontstyle] @polyline = params[:polyline] @direction = params[:direction] || "ttb" @line_styles = "<line style='fill: none; stroke:#{@col_line}; stroke-width:#{@linewidth + LINE_SCALING}; stroke-linejoin:round; stroke-linecap:round;' x1='X1' y1='Y1' x2='X2' y2='Y2' />\n" @polyline_styles = "<polyline style='stroke:#{@col_line}; stroke-width:#{@linewidth + LINE_SCALING}; fill:none; stroke-linejoin:round; stroke-linecap:round;' points='CHIX CHIY MIDX1 MIDY1 MIDX2 MIDY2 PARX PARY' />\n" @polygon_styles = "<polygon style='fill: none; stroke: black; stroke-width:#{@linewidth + LINE_SCALING}; stroke-linejoin:round;stroke-linecap:round;' points='X1 Y1 X2 Y2 X3 Y3' />\n" @text_styles = "<text white-space='pre' alignment-baseline='text-top' style='fill: COLOR; storoke-width: 0; font-size: fontsize' x='X_VALUE' y='Y_VALUE'>CONTENT</text>\n" @tree_data = String.new @visited_x = {} @visited_y = {} @global = global end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
28 29 30 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 28 def height @height end |
#width ⇒ Object
Returns the value of attribute width.
28 29 30 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 28 def width @width end |
Instance Method Details
#collect_region_shades ⇒ Object
Collect a background rectangle for every element flagged with ‘%’. Outer (shallower) regions are emitted first so deeper ones layer on top; with low opacity the overlap reads as a darker nesting.
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 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 144 def collect_region_shades pad = @global[:h_gap_between_nodes] / 2 stroke_width = @linewidth + LINE_SCALING half = stroke_width / 2.0 @element_list.get_elements.each do |element| next unless element.region b = subtree_bounds(element.id) x = b[:left] - pad y = b[:top] - pad w = (b[:right] - b[:left]) + pad * 2 h = (b[:bottom] - b[:top]) + pad * 2 # An explicit shade color is always honored (consistent with the # @color: node-text color); bare '%' falls back to gray. color = element.region_color || REGION_DEFAULT_COLOR @region_shades << "<rect x='#{x}' y='#{y}' width='#{w}' height='#{h}' rx='#{pad}' ry='#{pad}' " \ "fill='#{color}' fill-opacity='#{REGION_FILL_OPACITY}' " \ "stroke='#{color}' stroke-opacity='#{REGION_STROKE_OPACITY}' stroke-width='#{stroke_width}' />\n" # Track the union of region extents (the stroke straddles the edge, so # it reaches half a stroke-width outside the rect) for canvas growth. rb = (@region_bounds ||= { min_x: x - half, min_y: y - half, max_x: x + w + half, max_y: y + h + half }) rb[:min_x] = x - half if x - half < rb[:min_x] rb[:min_y] = y - half if y - half < rb[:min_y] rb[:max_x] = x + w + half if x + w + half > rb[:max_x] rb[:max_y] = y + h + half if y + h + half > rb[:max_y] end end |
#draw_a_path(s_x, s_y, t_x, t_y, target_arrow = :none) ⇒ Object
173 174 175 176 177 178 179 180 181 182 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 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 237 238 239 240 241 242 243 244 245 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 173 def draw_a_path(s_x, s_y, t_x, t_y, target_arrow = :none) spacing = @global[:h_gap_between_nodes] * 1.25 min_bulge = @global[:height_connector_to_text] # Centered offset for multiple lines at the same endpoint s_key = "#{s_x.round}" t_key = "#{t_x.round}" @visited_x[s_key] = (@visited_x[s_key] || 0) + 1 @visited_x[t_key] = (@visited_x[t_key] || 0) + 1 s_offset = ((@visited_x[s_key] - 1) - (@visited_x[s_key] - 1) / 2.0) * spacing t_offset = ((@visited_x[t_key] - 1) - (@visited_x[t_key] - 1) / 2.0) * spacing dashed = true if target_arrow == :none if @direction == "ltr" # LTR: route to the RIGHT of the tree (⊃ shape / reversed C) # Source → right → vertical → left → Target xmax = [s_x, t_x].max bulge = [min_bulge, (s_y - t_y).abs * 0.3 + min_bulge].min # Ensure paths don't overlap: extend beyond previous paths new_x = if xmax < @width @width + bulge else xmax + bulge end new_s_y = s_y + s_offset new_t_y = t_y + t_offset case target_arrow when :single @extra_lines << generate_line(s_x, new_s_y, new_x, new_s_y, @col_path, dashed) @extra_lines << generate_line(new_x, new_s_y, new_x, new_t_y, @col_path, dashed) @extra_lines << generate_line(new_x, new_t_y, t_x, new_t_y, @col_path, dashed, true) when :double @extra_lines << generate_line(new_x, new_s_y, s_x, new_s_y, @col_path, dashed, true) @extra_lines << generate_line(new_x, new_s_y, new_x, new_t_y, @col_path, dashed) @extra_lines << generate_line(new_x, new_t_y, t_x, new_t_y, @col_path, dashed, true) else @extra_lines << generate_line(s_x, new_s_y, new_x, new_s_y, @col_path, dashed) @extra_lines << generate_line(new_x, new_s_y, new_x, new_t_y, @col_path, dashed) @extra_lines << generate_line(new_x, new_t_y, t_x, new_t_y, @col_path, dashed) end @width = new_x if new_x > @width else # TTB: route BELOW the tree (U shape) # Source → down → horizontal → up → Target ymax = [s_y, t_y].max # Proportional bulge: based on distance between endpoints, # not the full tree height bulge = [min_bulge, (s_x - t_x).abs * 0.3 + min_bulge].min new_y = ymax + bulge new_s_x = s_x - s_offset new_t_x = t_x - t_offset case target_arrow when :single @extra_lines << generate_line(new_s_x, s_y, new_s_x, new_y, @col_path, dashed) @extra_lines << generate_line(new_s_x, new_y, new_t_x, new_y, @col_path, dashed) @extra_lines << generate_line(new_t_x, new_y, new_t_x, t_y, @col_path, dashed, true) when :double @extra_lines << generate_line(new_s_x, new_y, new_s_x, s_y, @col_path, dashed, true) @extra_lines << generate_line(new_s_x, new_y, new_t_x, new_y, @col_path, dashed) @extra_lines << generate_line(new_t_x, new_y, new_t_x, t_y, @col_path, dashed, true) else @extra_lines << generate_line(new_s_x, s_y, new_s_x, new_y, @col_path, dashed) @extra_lines << generate_line(new_s_x, new_y, new_t_x, new_y, @col_path, dashed) @extra_lines << generate_line(new_t_x, new_y, new_t_x, t_y, @col_path, dashed) end @height = new_y if new_y > @height end end |
#draw_bracket(x1, y1, width, height, col, bline = false) ⇒ Object
466 467 468 469 470 471 472 473 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 466 def draw_bracket(x1, y1, width, height, col, bline = false) swidth = bline ? @linewidth + BLINE_SCALING : @linewidth + LINE_SCALING slwidth = @global[:h_gap_between_nodes] / 2 @extra_lines << "<polyline style='stroke:#{col}; stroke-width:#{swidth}; fill:none; stroke-linejoin:round; stroke-linecap:round;' points='#{x1 + slwidth},#{y1} #{x1},#{y1} #{x1},#{y1 + height} #{x1 + slwidth},#{y1 + height}' />\n" @extra_lines << "<polyline style='stroke:#{col}; stroke-width:#{swidth}; fill:none; stroke-linejoin:round; stroke-linecap:round;' points='#{x1 + width - slwidth},#{y1} #{x1 + width},#{y1} #{x1 + width},#{y1 + height} #{x1 + width - slwidth},#{y1 + height}' />\n" end |
#draw_element(element) ⇒ Object
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 247 def draw_element(element) top = element.vertical_indent left = element.horizontal_indent right = left + element.content_width txt_pos = left + (right - left) / 2 # Use element's custom color if specified, otherwise use default based on type col = if element.color element.color elsif element.type == ETYPE_LEAF @col_leaf else @col_node end text_data = @text_styles.sub(/COLOR/, col) text_data = text_data.sub(/fontsize/, @fontsize.to_s + "px;") text_x = txt_pos - element.content_width / 2 text_y = top + @global[:single_line_height] - @global[:height_connector_to_text] text_data = text_data.sub(/X_VALUE/, text_x.to_s) text_data = text_data.sub(/Y_VALUE/, text_y.to_s) new_text = +"" this_x = 0 this_y = 0 bc = { x: text_x - @global[:h_gap_between_nodes] / 2, y: top, width: element.content_width + @global[:h_gap_between_nodes], height: nil } element.content.each_with_index do |l, idx| case l[:type] when :border, :bborder x1 = text_x if idx.zero? text_y -= l[:height] else text_y += l[:height] end y1 = text_y - @global[:single_line_height] / 8 x2 = text_x + element.content_width y2 = y1 case l[:type] when :border stroke_width = @linewidth + LINE_SCALING when :bborder stroke_width = @linewidth + BLINE_SCALING end @extra_lines << "<line style=\"stroke:#{col}; fill:none; stroke-linecap:round; stroke-width:#{stroke_width}; \" x1=\"#{x1}\" y1=\"#{y1}\" x2=\"#{x2}\" y2=\"#{y2}\"></line>" else if @direction == "ltr" && element.type == ETYPE_LEAF # LTR leaves: left-align text at the node's left edge this_x = left elsif element.enclosure == :brackets this_x = txt_pos - element.content_width / 2 else ewidth = 0 l[:elements].each do |e| ewidth += e[:width] end this_x = txt_pos - (ewidth / 2) end text_y += l[:elements].map { |e| e[:height] }.max if idx != 0 l[:elements].each do |e| escaped_text = e[:text].gsub('>', '>').gsub('<', '<'); decorations = [] decorations << "overline" if e[:decoration].include?(:overline) decorations << "underline" if e[:decoration].include?(:underline) decorations << "line-through" if e[:decoration].include?(:linethrough) decoration = "text-decoration=\"" + decorations.join(" ") + "\"" style = "style=\"" if e[:decoration].include?(:small) style += "font-size: #{(SUBSCRIPT_CONST.to_f * 100).to_i}%; " this_y = text_y - ((@global[:single_x_metrics].height - @global[:single_x_metrics].height * SUBSCRIPT_CONST) / 4) + 2 elsif e[:decoration].include?(:superscript) style += "font-size: #{(SUBSCRIPT_CONST.to_f * 100).to_i}%; " this_y = text_y - (@global[:single_x_metrics].height / 4) + 1 elsif e[:decoration].include?(:subscript) style += "font-size: #{(SUBSCRIPT_CONST.to_f * 100).to_i}%; " this_y = text_y + 4 else this_y = text_y end style += "font-weight: bold; fill: #{@col_emph}; " if e[:decoration].include?(:bold) || e[:decoration].include?(:bolditalic) style += "font-style: italic; " if e[:decoration].include?(:italic) || e[:decoration].include?(:bolditalic) style += "\"" case @fontstyle when /(?:cjk)/ fontstyle = "'WenQuanYi Zen Hei', 'Noto Sans', OpenMoji, 'OpenMoji Color', 'OpenMoji Black', sans-serif" when /(?:mono)/ fontstyle = if e[:cjk] "'Noto Sans JP', 'Noto Sans Mono SemiCondensed', OpenMoji, 'OpenMoji Color', 'OpenMoji Black', sans-serif" else "'Noto Sans Mono SemiCondensed', 'Noto Sans JP', OpenMoji, 'OpenMoji Color', 'OpenMoji Black', sans-serif" end when /(?:sans)/ fontstyle = if e[:cjk] "'Noto Sans JP', 'Noto Sans', OpenMoji, 'OpenMoji Color', 'OpenMoji Black', sans-serif" else "'Noto Sans', 'Noto Sans JP', OpenMoji, 'OpenMoji Color', 'OpenMoji Black', sans-serif" end when /(?:serif)/ fontstyle = if e[:cjk] "'Noto Serif JP', 'Noto Serif', OpenMoji, 'OpenMoji Color', 'OpenMoji Black', serif" else "'Noto Serif', 'Noto Serif JP', OpenMoji, 'OpenMoji Color', 'OpenMoji Black', serif" end end if e[:decoration].include?(:box) || e[:decoration].include?(:circle) || e[:decoration].include?(:bar) enc_height = e[:height] enc_y = this_y - e[:height] * 0.8 enc_width = e[:width] enc_x = this_x if e[:decoration].include?(:hatched) case element.type when ETYPE_LEAF fill = if @color "url(#hatchForLeaf)" else "url(#hatchBlack)" end when ETYPE_NODE fill = if @color "url(#hatchForNode)" else "url(#hatchBlack)" end end else fill = "none" end enc = nil stroke_width = if e[:decoration].include?(:bstroke) @linewidth + BLINE_SCALING else @linewidth + LINE_SCALING end if e[:decoration].include?(:box) enc = "<rect style='stroke: #{col}; stroke-linejoin:round; stroke-width:#{stroke_width};' x='#{enc_x}' y='#{enc_y}' width='#{enc_width}' height='#{enc_height}' fill='#{fill}' />\n" elsif e[:decoration].include?(:circle) enc = "<rect style='stroke: #{col}; stroke-width:#{stroke_width};' x='#{enc_x}' y='#{enc_y}' rx='#{enc_height / 2}' ry='#{enc_height / 2}' width='#{enc_width}' height='#{enc_height}' fill='#{fill}' />\n" elsif e[:decoration].include?(:bar) x1 = enc_x y1 = enc_y + enc_height / 2 x2 = enc_x + enc_width y2 = y1 ar_hwidth = e[:width] / 4.0 = "<line style='fill:none; stroke:#{col}; stroke-linejoin:round; stroke-linecap:round; stroke-width:#{stroke_width};' x1='#{x1 + stroke_width / 2}' y1='#{y1}' x2='#{x2 - stroke_width / 2}' y2='#{y2}'></line>\n" @extra_lines << if e[:decoration].include?(:arrow_to_l) l_arrowhead = "<polyline stroke-linejoin='round' stroke-linecap='round' fill='none' stroke='#{col}' stroke-width='#{stroke_width}' points='#{x1 + ar_hwidth},#{y1 + ar_hwidth / 2} #{x1 + stroke_width / 2},#{y1} #{x1 + ar_hwidth},#{y1 - ar_hwidth / 2}' />\n" @extra_lines << l_arrowhead end if e[:decoration].include?(:arrow_to_r) r_arrowhead = "<polyline stroke-linejoin='round' stroke-linecap='round' fill='none' stroke='#{col}' stroke-width='#{stroke_width}' points='#{x2 - ar_hwidth},#{y2 - ar_hwidth / 2} #{x2 - stroke_width / 2},#{y2} #{x2 - ar_hwidth},#{y2 + ar_hwidth / 2}' />\n" @extra_lines << r_arrowhead end end @extra_lines << enc if enc this_x += if e[:text].size == 1 (e[:height] - e[:content_width]) / 2 else @global[:width_half_x] / 2 end new_text << set_tspan(this_x, this_y, style, decoration, fontstyle, escaped_text) this_x += if e[:text].size == 1 e[:content_width] + (e[:height] - e[:content_width]) / 2 else e[:content_width] + @global[:width_half_x] / 2 end elsif e[:decoration].include?(:whitespace) this_x += e[:width] next else new_text << set_tspan(this_x, this_y, style, decoration, fontstyle, escaped_text) this_x += e[:width] end end end @height = text_y if text_y > @height end bc[:y] = bc[:y] + @global[:height_connector_to_text] * 3 / 4 bc[:height] = text_y - bc[:y] + @global[:height_connector_to_text] case element.enclosure when :brackets draw_bracket(bc[:x], bc[:y], bc[:width], bc[:height], col) when :rectangle draw_rectangle(bc[:x], bc[:y], bc[:width], bc[:height], col) when :brectangle draw_rectangle(bc[:x], bc[:y], bc[:width], bc[:height], col, true) end element.content_height = bc[:height] @tree_data += text_data.sub(/CONTENT/, new_text) end |
#draw_paths ⇒ Object
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 483 def draw_paths paths = [] path_pool_target = {} path_pool_other = {} path_pool_source = {} path_flags = [] line_pool = {} line_flags = [] elist = @element_list.get_elements elist.each do |element| if @direction == "ltr" # LTR anchors: paths attach at left/right edges, vertically centered hctt = @global[:height_connector_to_text] y_center = element.vertical_indent + (element.content_height + hctt * 1.5) / 2 x0 = element.horizontal_indent - hctt x1 = element.horizontal_indent + element.content_width + hctt x2 = element.horizontal_indent + element.content_width + hctt * 2 y0 = y_center y1 = y_center else x0 = element.horizontal_indent - @global[:h_gap_between_nodes] x1 = element.horizontal_indent + element.content_width / 2 x2 = element.horizontal_indent + element.content_width + @global[:h_gap_between_nodes] y0 = element.vertical_indent + @global[:height_connector_to_text] / 2 y1 = element.vertical_indent + element.content_height + @global[:height_connector_to_text] end et = element.path et.each do |tr| if /\A-(>|<)?(\d+)\z/ =~ tr arrow = $1 tr = $2 if line_pool[tr] line_pool[tr] << { x: { left: x0, center: x1, right: x2 }, y: { top: y0, center: y0 + (y1 - y0) / 2, bottom: y1 }, arrow: arrow } else line_pool[tr] = [{ x: { left: x0, center: x1, right: x2 }, y: { top: y0, center: y0 + (y1 - y0) / 2, bottom: y1 }, arrow: arrow }] end line_flags << tr elsif /\A(?:>|<)(\d+)\z/ =~ tr tr = $1 if path_pool_target[tr] path_pool_target[tr] << [x1, y1] else path_pool_target[tr] = [[x1, y1]] end path_flags << tr elsif path_pool_source[tr] if path_pool_other[tr] path_pool_other[tr] << [x1, y1] else path_pool_other[tr] = [[x1, y1]] end path_flags << tr else path_pool_source[tr] = [x1, y1] path_flags << tr end raise RSTError, +"Error: input text contains a path having more than two ends:\n > #{tr}" if path_flags.tally.any? { |_k, v| v > 2 } || line_flags.tally.any? { |_k, v| v > 2 } end end path_flags.tally.each do |k, v| raise RSTError, +"Error: input text contains a path having only one end:\n > #{k}" if v == 1 end path_pool_source.each do |k, v| path_flags.delete(k) if (targets = path_pool_target[k]) targets.each do |t| paths << { x1: v[0], y1: v[1], x2: t[0], y2: t[1], arrow: :single } end elsif (others = path_pool_other[k]) others.each do |t| paths << { x1: v[0], y1: v[1], x2: t[0], y2: t[1], arrow: :none } end end end path_flags.uniq.each do |k| targets = path_pool_target[k] next if targets.nil? || targets.empty? fst = targets.shift next if fst.nil? targets.each do |t| paths << { x1: fst[0], y1: fst[1], x2: t[0], y2: t[1], arrow: :double } end end paths.each do |t| draw_a_path(t[:x1], t[:y1], t[:x2], t[:y2], t[:arrow]) end line_pool.each do |_k, v| a = v[0] b = v[1] if @direction == "ltr" # LTR: use x-position to determine depth relationship, # y-position for sibling relationship if a[:x][:left] > b[:x][:right] generate_connectors(a[:x][:left], a[:y][:center], b[:x][:right], b[:y][:center], @col_extra, false, a[:arrow], b[:arrow]) elsif a[:x][:right] < b[:x][:left] generate_connectors(b[:x][:left], b[:y][:center], a[:x][:right], a[:y][:center], @col_extra, false, b[:arrow], a[:arrow]) elsif a[:y][:center] < b[:y][:center] generate_connectors(a[:x][:center], a[:y][:bottom], b[:x][:center], b[:y][:top], @col_extra, false, a[:arrow], b[:arrow]) else generate_connectors(b[:x][:center], b[:y][:bottom], a[:x][:center], a[:y][:top], @col_extra, false, b[:arrow], a[:arrow]) end else # TTB: use y-position to determine depth relationship if a[:y][:top] > b[:y][:bottom] generate_connectors(a[:x][:center], a[:y][:top], b[:x][:center], b[:y][:bottom], @col_extra, false, a[:arrow], b[:arrow]) elsif a[:y][:bottom] < b[:y][:top] generate_connectors(b[:x][:center], b[:y][:top], a[:x][:center], a[:y][:bottom], @col_extra, false, b[:arrow], a[:arrow]) elsif a[:x][:center] < b[:x][:center] if a[:y][:top] == b[:y][:top] upper_y = a[:y][:center] < b[:y][:center] ? a[:y][:center] : b[:y][:center] generate_connectors(a[:x][:right], upper_y, b[:x][:left], upper_y, @col_extra, false, a[:arrow], b[:arrow]) else generate_connectors(a[:x][:right], a[:y][:center], b[:x][:left], b[:y][:center], @col_extra, false, a[:arrow], b[:arrow]) end elsif a[:y][:top] == b[:y][:top] upper_y = a[:y][:center] < b[:y][:center] ? a[:y][:center] : b[:y][:center] generate_connectors(b[:x][:right], upper_y, a[:x][:left], upper_y, @col_extra, false, b[:arrow], a[:arrow]) else generate_connectors(b[:x][:right], b[:y][:center], a[:x][:left], a[:y][:center], @col_extra, false, b[:arrow], a[:arrow]) end end end paths.size + line_pool.keys.size end |
#draw_rectangle(x1, y1, width, height, col, bline = false) ⇒ Object
460 461 462 463 464 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 460 def draw_rectangle(x1, y1, width, height, col, bline = false) swidth = bline ? @linewidth + BLINE_SCALING : @linewidth + LINE_SCALING @extra_lines << "<polygon style='stroke:#{col}; stroke-width:#{swidth}; fill:none; stroke-linejoin:round; stroke-linecap:round;' points='#{x1},#{y1} #{x1 + width},#{y1} #{x1 + width},#{y1 + height} #{x1},#{y1 + height}' />\n" end |
#generate_connectors(x1, y1, x2, y2, col, dashed = false, s_arrow = false, t_arrow = false, bline = false) ⇒ Object
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 619 def generate_connectors(x1, y1, x2, y2, col, dashed = false, s_arrow = false, t_arrow = false, bline = false) string = if s_arrow && t_arrow "marker-mid='url(#arrowBothways)' " elsif s_arrow "marker-mid='url(#arrowForward)' " elsif t_arrow "marker-mid='url(#arrowBackward)' " else "" end dasharray = dashed ? "stroke-dasharray='8 8'" : "" swidth = bline ? @linewidth + BLINE_SCALING : @linewidth + LINE_SCALING if s_arrow || t_arrow x_mid = if x2 > x1 x1 + (x2 - x1) / 2 else x1 - (x1 - x2) / 2 end y_mid = if y2 > y1 y1 + (y2 - y1) / 2 else y1 - (y1 - y2) / 2 end @extra_lines << "<path d='M#{x1},#{y1} L#{x_mid},#{y_mid} L#{x2}, #{y2}' style='fill: none; stroke: #{col}; stroke-width:#{swidth}; stroke-linecap:round;' #{dasharray} #{string}/>" else @extra_lines << "<line x1='#{x1}' y1='#{y1}' x2='#{x2}' y2='#{y2}' style='fill: none; stroke: #{col}; stroke-width:#{swidth}; stroke-linecap:round;' #{dasharray} #{string}/>" end end |
#generate_line(x1, y1, x2, y2, col, dashed = false, arrow = false, bline = false) ⇒ Object
649 650 651 652 653 654 655 656 657 658 659 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 649 def generate_line(x1, y1, x2, y2, col, dashed = false, arrow = false, bline = false) string = if arrow "marker-end='url(#arrow)' " else "" end dasharray = dashed ? "stroke-dasharray='8 8'" : "" swidth = bline ? @linewidth + BLINE_SCALING : @linewidth + LINE_SCALING "<line x1='#{x1}' y1='#{y1}' x2='#{x2}' y2='#{y2}' style='fill: none; stroke: #{col}; stroke-width:#{swidth}; stroke-linecap:round;' #{dasharray} #{string}/>" end |
#line_to_parent(parent, child) ⇒ Object
Draw a line between child/parent elements
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 662 def line_to_parent(parent, child) return if child.horizontal_indent.zero? if @direction == "ltr" # LTR: parent's right side → child's left side # Y center = midpoint between TTB top-connection and bottom-connection # top = vi + hctt/2, bottom = vi + content_height + hctt # midpoint = vi + (content_height + hctt * 1.5) / 2 hctt = @global[:height_connector_to_text] y1 = child.vertical_indent + (child.content_height + hctt * 1.5) / 2 y2 = parent.vertical_indent + (parent.content_height + hctt * 1.5) / 2 x1 = child.horizontal_indent - hctt x2 = parent.horizontal_indent + parent.content_width + hctt if @polyline mid_x1 = x2 + (x1 - x2) / 2 mid_y1 = y1 mid_x2 = mid_x1 mid_y2 = y2 @tree_data += @polyline_styles.sub(/CHIX/, x1.to_s) .sub(/CHIY/, y1.to_s) .sub(/MIDX1/, mid_x1.to_s) .sub(/MIDY1/, mid_y1.to_s) .sub(/MIDX2/, mid_x2.to_s) .sub(/MIDY2/, mid_y2.to_s) .sub(/PARX/, x2.to_s) .sub(/PARY/, y2.to_s) else line_data = @line_styles.sub(/X1/, x1.to_s) line_data = line_data.sub(/Y1/, y1.to_s) line_data = line_data.sub(/X2/, x2.to_s) @tree_data += line_data.sub(/Y2/, y2.to_s) end else # TTB: parent's bottom → child's top if @polyline chi_x = child.horizontal_indent + child.content_width / 2 chi_y = child.vertical_indent + @global[:height_connector_to_text] / 2 par_x = parent.horizontal_indent + parent.content_width / 2 par_y = parent.vertical_indent + parent.content_height + @global[:height_connector_to_text] mid_x1 = chi_x mid_y1 = par_y + (chi_y - par_y) / 2 mid_x2 = par_x mid_y2 = mid_y1 @tree_data += @polyline_styles.sub(/CHIX/, chi_x.to_s) .sub(/CHIY/, chi_y.to_s) .sub(/MIDX1/, mid_x1.to_s) .sub(/MIDY1/, mid_y1.to_s) .sub(/MIDX2/, mid_x2.to_s) .sub(/MIDY2/, mid_y2.to_s) .sub(/PARX/, par_x.to_s) .sub(/PARY/, par_y.to_s) else x1 = child.horizontal_indent + child.content_width / 2 y1 = child.vertical_indent + @global[:height_connector_to_text] / 2 x2 = parent.horizontal_indent + parent.content_width / 2 y2 = parent.vertical_indent + parent.content_height + @global[:height_connector_to_text] line_data = @line_styles.sub(/X1/, x1.to_s) line_data = line_data.sub(/Y1/, y1.to_s) line_data = line_data.sub(/X2/, x2.to_s) @tree_data += line_data.sub(/Y2/, y2.to_s) end end end |
#set_tspan(this_x, this_y, style, decoration, fontstyle, text) ⇒ Object
475 476 477 478 479 480 481 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 475 def set_tspan(this_x, this_y, style, decoration, fontstyle, text) text.gsub!(/■+/) do |x| num_spaces = x.size "<tspan style='fill:none;'>" + "■" * num_spaces + "</tspan>" end "<tspan x='#{this_x}' y='#{this_y}' #{style} #{decoration} font-family=\"#{fontstyle}\">#{text}</tspan>\n" end |
#svg_data ⇒ Object
55 56 57 58 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 55 def svg_data metrics = parse_list # Region shades depend on the final element positions and on the node # content_height that draw_element recomputes, so collect them only # after parse_list (which runs draw_elements) has completed. collect_region_shades @width = metrics[:width] + @global[:h_gap_between_nodes] * 2 @height = metrics[:height] + @global[:height_connector_to_text] / 2 x1 = 0 - @global[:h_gap_between_nodes] y1 = 0 x2 = @width + @global[:h_gap_between_nodes] y2 = @height + @global[:height_connector_to_text] / 2 # Grow the canvas so region shades that reach past the tree's own bounds # (e.g. a region on the root node, whose top sits above y=0) are not # clipped. x2/y2 are the viewBox width/height, so right = x1 + x2. if @region_bounds rb = @region_bounds left = [x1, rb[:min_x]].min top = [y1, rb[:min_y]].min right = [x1 + x2, rb[:max_x]].max bottom = [y1 + y2, rb[:max_y]].max new_x2 = right - left new_y2 = bottom - top @width += new_x2 - x2 @height += new_y2 - y2 x1 = left y1 = top x2 = new_x2 y2 = new_y2 end extra_lines = @extra_lines.join("\n") as2 = @global[:h_gap_between_nodes] * 1.0 as4 = as2 * 3 header = <<~HDR <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="#{@width}" height="#{@height}" viewBox="#{x1}, #{y1}, #{x2}, #{y2}" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <marker id="arrow" markerUnits="userSpaceOnUse" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="#{as2}" markerHeight="#{as2}" orient="auto"> <path d="M 0 0 L 10 5 L 0 10" fill="#{@col_extra}"/> </marker> <marker id="arrowBackward" markerUnits="userSpaceOnUse" viewBox="0 0 10 10" refX="5" refY="5" markerWidth="#{as2}" markerHeight="#{as2}" orient="auto"> <path d="M 0 0 L 10 5 L 0 10 z" fill="#{@col_extra}"/> </marker> <marker id="arrowForward" markerUnits="userSpaceOnUse" viewBox="0 0 10 10" refX="5" refY="5" markerWidth="#{as2}" markerHeight="#{as2}" orient="auto"> <path d="M 10 0 L 0 5 L 10 10 z" fill="#{@col_extra}"/> </marker> <marker id="arrowBothways" markerUnits="userSpaceOnUse" viewBox="0 0 30 10" refX="15" refY="5" markerWidth="#{as4}" markerHeight="#{as2}" orient="auto"> <path d="M 0 5 L 10 0 L 10 5 L 20 5 L 20 0 L 30 5 L 20 10 L 20 5 L 10 5 L 10 10 z" fill="#{@col_extra}"/> </marker> <pattern id="hatchBlack" x="10" y="10" width="10" height="10" patternUnits="userSpaceOnUse" patternTransform="rotate(45)"> <line x1="0" y="0" x2="0" y2="10" stroke="black" stroke-width="4"></line> </pattern> <pattern id="hatchForNode" x="10" y="10" width="10" height="10" patternUnits="userSpaceOnUse" patternTransform="rotate(45)"> <line x1="0" y="0" x2="0" y2="10" stroke="#{@col_node}" stroke-width="4"></line> </pattern> <pattern id="hatchForLeaf" x="10" y="10" width="10" height="10" patternUnits="userSpaceOnUse" patternTransform="rotate(45)"> <line x1="0" y="0" x2="0" y2="10" stroke="#{@col_leaf}" stroke-width="4"></line> </pattern> </defs> HDR rect = <<~RCT <rect x="#{x1}" y="#{y1}" width="#{x2}" height="#{y2}" stroke="none" fill="white" />" RCT = "</svg>" # Region shades go at the very back: above the white background but # below tree connectors and labels. shades = @region_shades.join if @transparent header + shades + @tree_data + extra_lines + else header + rect + shades + @tree_data + extra_lines + end end |
#triangle_to_parent(parent, child) ⇒ Object
Draw a triangle between child/parent elements
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 733 def triangle_to_parent(parent, child) return if child.horizontal_indent.zero? if @direction == "ltr" # LTR: triangle opens vertically (top-bottom of child), # apex at parent's right side. # Use same vertical reference and dimensions as TTB uses horizontally. hctt = @global[:height_connector_to_text] child_y_center = child.vertical_indent + (child.content_height + hctt * 1.5) / 2 parent_y_center = parent.vertical_indent + (parent.content_height + hctt * 1.5) / 2 tri_half = @global[:single_x_metrics].height / 2 x1 = child.horizontal_indent - @global[:height_connector_to_text] / 2 y1 = child_y_center - tri_half x2 = child.horizontal_indent - @global[:height_connector_to_text] / 2 y2 = child_y_center + tri_half x3 = parent.horizontal_indent + parent.content_width + @global[:height_connector_to_text] y3 = parent_y_center else # TTB: triangle opens horizontally (left-right of child text), # apex at parent's bottom x1 = child.horizontal_indent y1 = child.vertical_indent + @global[:height_connector_to_text] / 2 x2 = child.horizontal_indent + child.content_width y2 = child.vertical_indent + @global[:height_connector_to_text] / 2 x3 = parent.horizontal_indent + parent.content_width / 2 y3 = parent.vertical_indent + parent.content_height + @global[:height_connector_to_text] end polygon_data = @polygon_styles.sub(/X1/, x1.to_s) polygon_data = polygon_data.sub(/Y1/, y1.to_s) polygon_data = polygon_data.sub(/X2/, x2.to_s) polygon_data = polygon_data.sub(/Y2/, y2.to_s) polygon_data = polygon_data.sub(/X3/, x3.to_s) @tree_data += polygon_data.sub(/Y3/, y3.to_s) end |