rsyntaxtree 2.5.0 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -51,13 +51,15 @@ module RSyntaxTree
51
51
  @shear_deg = (params[:shear] || 0).to_f
52
52
  @shear_k = (-Math.tan(@shear_deg * Math::PI / 180)).round(3)
53
53
  @shear_plane = params[:shear_plane] || "on"
54
+ @corner = params[:corner]
55
+ # The hatch pattern of each colour a coloured stretch asks for.
56
+ @span_hatches = {}
54
57
  @line_styles = "<line style='fill: none; stroke:#{@col_line}; stroke-width:#{@global[:stroke_normal]}; stroke-linejoin:round; stroke-linecap:round;' x1='X1' y1='Y1' x2='X2' y2='Y2' />\n"
55
58
  @polyline_styles = "<polyline style='stroke:#{@col_line}; stroke-width:#{@global[:stroke_normal]}; fill:none; stroke-linejoin:round; stroke-linecap:round;'
56
59
  points='CHIX CHIY MIDX1 MIDY1 MIDX2 MIDY2 PARX PARY' />\n"
57
60
  @polygon_styles = "<polygon style='fill: none; stroke: #{@col_connector}; stroke-width:#{@global[:stroke_normal]}; stroke-linejoin:round;stroke-linecap:round;' points='X1 Y1 X2 Y2 X3 Y3' />\n"
58
61
  @text_styles = "<text white-space='pre' alignment-baseline='text-top' style='fill: COLOR; stroke-width: 0; font-size: fontsize' x='X_VALUE' y='Y_VALUE'>CONTENT</text>\n"
59
62
  @tree_data = String.new
60
- @visited_x = {}
61
63
  @visited_y = {}
62
64
  @global = global
63
65
  end
@@ -146,6 +148,13 @@ module RSyntaxTree
146
148
  @height = y2
147
149
  end
148
150
 
151
+ corner = ""
152
+ if @corner
153
+ corner, x1, y1, x2, y2 = corner_label(x1, y1, x2, y2)
154
+ @width = x2
155
+ @height = y2
156
+ end
157
+
149
158
  extra_lines = @extra_lines.join("\n")
150
159
 
151
160
  # Rounded like the stroke widths are: the arithmetic leaves float dust
@@ -191,6 +200,10 @@ module RSyntaxTree
191
200
  </defs>
192
201
  HDR
193
202
 
203
+ # Only a figure with a hatched shape in a coloured stretch has these, and
204
+ # every other figure's header is left as it was.
205
+ header = header.sub(" </defs>", "#{span_hatch_patterns(hatch_cell, hatch_stroke)}\n </defs>") if @span_hatches.any?
206
+
194
207
  rect = <<~RCT
195
208
  <rect x="#{x1}" y="#{y1}" width="#{x2}" height="#{y2}" stroke="none" fill="white" />
196
209
  RCT
@@ -201,6 +214,8 @@ module RSyntaxTree
201
214
  # below tree connectors and labels.
202
215
  shades = @region_shades.join
203
216
 
217
+ footer = corner + footer
218
+
204
219
  if @shear_k.zero?
205
220
  if @transparent
206
221
  header + shades + @tree_data + extra_lines + footer
@@ -238,57 +253,65 @@ module RSyntaxTree
238
253
  end
239
254
  end
240
255
 
256
+ # A short string set in a corner of the finished image, such as the
257
+ # judgement mark of the sentence the tree is drawn for. It gets a strip of
258
+ # its own on its side of the canvas, so it never touches the tree and sits
259
+ # in the same place in every figure. Its top is level with the tree's
260
+ # highest ink, or its bottom with the lowest. It is drawn after the shear
261
+ # and the mirror, because it marks the image rather than the tree.
262
+ # Returns the text element and the grown canvas.
263
+ def corner_label(x1, y1, x2, y2)
264
+ size = @corner[:size] * FONT_SCALING
265
+ family = FontFamily.for_pango(@corner[:font_style])
266
+ m = FontMetrics.get_metrics(@corner[:text], family, size, :normal, :normal)
267
+ gap = @global[:h_gap_between_nodes]
268
+ inked = @element_list.get_elements.select { |e| e.ink_top && e.ink_bottom }
269
+ top = inked.map(&:ink_top).min || (y1 + gap)
270
+ bottom = inked.map(&:ink_bottom).max || (y1 + y2 - gap)
271
+ vertical, horizontal = @corner[:position].split("-")
272
+
273
+ baseline = if vertical == "top"
274
+ top + m.ink_above
275
+ else
276
+ bottom - (m.ink_height - m.ink_above)
277
+ end
278
+ # The strip holds both the advance and the ink, whichever reaches
279
+ # further, so a glyph that overhangs its origin is not cut off.
280
+ left = [0, m.ink_left].min
281
+ width = [m.width, m.ink_left + m.ink_width].max - left
282
+ if horizontal == "left"
283
+ x1 -= width + gap
284
+ x = x1 + gap - left
285
+ else
286
+ x = x1 + x2 - left
287
+ end
288
+ x2 += width + gap
289
+
290
+ # Grown downward or upward only when the mark is taller than the tree.
291
+ ink_t = baseline - m.ink_above
292
+ ink_b = ink_t + m.ink_height
293
+ new_y1 = [y1, ink_t - gap].min
294
+ y2 = [y1 + y2, ink_b + gap].max - new_y1
295
+ y1 = new_y1
296
+
297
+ text = "<text x='#{x.round(3)}' y='#{baseline.round(3)}' white-space='pre' " \
298
+ "style='fill: #{@corner[:color]}; stroke-width: 0; font-size: #{size.round(3)}px' " \
299
+ "font-family=\"#{FontFamily.for_svg(@corner[:font_style])}\">" \
300
+ "#{CGI.escapeHTML(@corner[:text])}</text>\n"
301
+ [text, x1.round(3), y1.round(3), x2.round(3), y2.round(3)]
302
+ end
303
+
241
304
  # Collect a background rectangle for every element flagged with '%'.
242
305
  # Outer (shallower) regions are emitted first so deeper ones layer on
243
306
  # top; with low opacity the overlap reads as a darker nesting.
244
307
  def collect_region_shades
245
- hctt = @global[:height_connector_to_text]
246
- pad = @global[:h_gap_between_nodes] # generous padding for the non-parent-facing edges
247
- pad_parent = hctt / 2.0 # tighter margin on the parent-facing edge of the root
248
- radius = hctt / 2.0
308
+ radius = @global[:height_connector_to_text] / 2.0
249
309
  stroke_width = @global[:stroke_normal]
250
310
  half = stroke_width / 2.0
251
311
  @element_list.get_elements.each do |element|
252
312
  next unless element.region
253
313
 
254
- b = region_subtree_bounds(element.id)
255
- left = b[:left] - pad
256
- right = b[:right] + pad
257
- top = b[:top] - pad
258
- bottom = b[:bottom] + pad
259
-
260
- # The edge facing the parent is handled specially so the incoming
261
- # connector neither overlaps nor touches the plane. That edge is the
262
- # TOP in top-to-bottom layout and the LEFT in left-to-right layout; the
263
- # other edges keep the generous padding. For a non-root node the edge
264
- # is placed midway between the connector anchor and the content edge
265
- # (even gap on both sides); the tree root has no incoming connector, so
266
- # it just gets a tighter-but-consistent margin there.
267
- is_root = element.parent.zero?
268
- if @direction == "ltr"
269
- anchor = element.horizontal_indent - hctt
270
- left = if is_root
271
- b[:left] - pad_parent
272
- elsif anchor < b[:left]
273
- (b[:left] + anchor) / 2.0
274
- else
275
- left
276
- end
277
- else
278
- anchor = element.vertical_indent + hctt / 2.0
279
- top = if is_root
280
- b[:top] - pad_parent
281
- elsif anchor < b[:top]
282
- (b[:top] + anchor) / 2.0
283
- else
284
- top
285
- end
286
- end
287
-
288
- x = left
289
- y = top
290
- w = right - left
291
- h = bottom - top
314
+ x, y, w, h = region_rect(element).values_at(:x, :y, :w, :h)
292
315
  # An explicit shade color is always honored (consistent with the
293
316
  # @color: node-text color); bare '%' falls back to gray.
294
317
  color = element.region_color || REGION_DEFAULT_COLOR
@@ -339,6 +362,68 @@ module RSyntaxTree
339
362
  # Union of element_visual_box over the subtree rooted at +id+, in final
340
363
  # drawing coordinates. Used to size region shades so the whole subtree
341
364
  # (labels, enclosures, brackets) sits inside the plane.
365
+ # How far a phrase reaches: the boxes of everything it holds, and the
366
+ # shade of any region in it, as the shade is painted.
367
+ def phrase_reach(element)
368
+ reach = region_subtree_bounds(element.id)
369
+ stack = [element]
370
+ until stack.empty?
371
+ node = stack.pop
372
+ if node.region
373
+ r = region_rect(node)
374
+ reach[:left] = [reach[:left], r[:x]].min
375
+ reach[:right] = [reach[:right], r[:x] + r[:w]].max
376
+ reach[:top] = [reach[:top], r[:y]].min
377
+ reach[:bottom] = [reach[:bottom], r[:y] + r[:h]].max
378
+ end
379
+ node.children.each { |c| stack << @element_list.get_id(c) }
380
+ end
381
+ reach
382
+ end
383
+
384
+ # The rectangle a region shade is painted in: the subtree's boxes with a
385
+ # gap around them, the edge facing the parent closer in.
386
+ def region_rect(element)
387
+ hctt = @global[:height_connector_to_text]
388
+ pad = @global[:h_gap_between_nodes] # generous padding for the non-parent-facing edges
389
+ pad_parent = hctt / 2.0 # tighter margin on the parent-facing edge of the root
390
+ b = region_subtree_bounds(element.id)
391
+ left = b[:left] - pad
392
+ right = b[:right] + pad
393
+ top = b[:top] - pad
394
+ bottom = b[:bottom] + pad
395
+
396
+ # The edge facing the parent is handled specially so the incoming
397
+ # connector neither overlaps nor touches the plane. That edge is the
398
+ # TOP in top-to-bottom layout and the LEFT in left-to-right layout; the
399
+ # other edges keep the generous padding. For a non-root node the edge
400
+ # is placed midway between the connector anchor and the content edge
401
+ # (even gap on both sides); the tree root has no incoming connector, so
402
+ # it just gets a tighter-but-consistent margin there.
403
+ is_root = element.parent.zero?
404
+ if @direction == "ltr"
405
+ anchor = element.horizontal_indent - hctt
406
+ left = if is_root
407
+ b[:left] - pad_parent
408
+ elsif anchor < b[:left]
409
+ (b[:left] + anchor) / 2.0
410
+ else
411
+ left
412
+ end
413
+ else
414
+ anchor = element.vertical_indent + hctt / 2.0
415
+ top = if is_root
416
+ b[:top] - pad_parent
417
+ elsif anchor < b[:top]
418
+ (b[:top] + anchor) / 2.0
419
+ else
420
+ top
421
+ end
422
+ end
423
+
424
+ { x: left, y: top, w: right - left, h: bottom - top }
425
+ end
426
+
342
427
  def region_subtree_bounds(id)
343
428
  b = element_visual_box(@element_list.get_id(id))
344
429
  @element_list.get_id(id).children.each do |c|
@@ -387,6 +472,12 @@ module RSyntaxTree
387
472
  d << " L#{at.call(points.last)}"
388
473
  end
389
474
 
475
+ # The dash every dashed line in the figure is drawn with.
476
+ def dash_array
477
+ dash = @fontsize / 4.0
478
+ "stroke-dasharray='#{dash} #{dash}'"
479
+ end
480
+
390
481
  # One stroke rather than three lines meeting at right angles. Drawn this way
391
482
  # the dash pattern runs round each turn instead of restarting at it, and an
392
483
  # arrowhead is the end of the stroke rather than a marker on whichever line
@@ -402,8 +493,7 @@ module RSyntaxTree
402
493
  radius = [radius, leg.call(points[-2], points[-1]) - arrow_length].min if end_arrow
403
494
  radius = [radius, 0].max
404
495
 
405
- dash = @fontsize / 4.0
406
- dasharray = dashed ? "stroke-dasharray='#{dash} #{dash}' " : ""
496
+ dasharray = dashed ? "#{dash_array} " : ""
407
497
  markers = +""
408
498
  markers << "marker-start='url(#arrowStart)' " if start_arrow
409
499
  markers << "marker-end='url(#arrow)' " if end_arrow
@@ -444,13 +534,20 @@ module RSyntaxTree
444
534
 
445
535
  RAIL_SLACK = 1e-6
446
536
 
537
+ # How far apart two rails stand, and two path ends on one label.
538
+ def rail_spacing
539
+ @global[:h_gap_between_nodes] * 1.25
540
+ end
541
+
447
542
  # A rail that would run along one already drawn, over part of the same
448
- # span, is moved outward past it: two paths whose rails coincide read as
449
- # one line, and nothing tells which ends it joins.
543
+ # span or less than one spacing short of it, is moved outward past it: two
544
+ # paths whose rails coincide read as one line, and nothing tells which
545
+ # ends it joins. Rails a spacing or more apart end to end stay on one
546
+ # level, as two paths meeting at a label do once their ends are spread.
450
547
  #
451
548
  # Each move goes outward and past the rail in the way, so no rail can be in
452
- # the way twice and there are at most as many moves as rails. The distance
453
- # is compared with a little slack: a rail moved exactly one spacing past
549
+ # the way twice and there are at most as many moves as rails. Distances
550
+ # are compared with a little slack: a rail moved exactly one spacing past
454
551
  # another can measure a hair under it, and would be moved again forever.
455
552
  def clear_of_rails(at, span, spacing, sign)
456
553
  @rails ||= []
@@ -458,7 +555,7 @@ module RSyntaxTree
458
555
  close = spacing - RAIL_SLACK
459
556
  @rails.size.times do
460
557
  blocking = @rails.find do |r|
461
- r[:lo] <= hi + spacing && lo <= r[:hi] + spacing && (r[:at] - at).abs < close
558
+ r[:lo] < hi + close && lo < r[:hi] + close && (r[:at] - at).abs < close
462
559
  end
463
560
  break unless blocking
464
561
 
@@ -482,8 +579,38 @@ module RSyntaxTree
482
579
  b[:max_y] = [b[:max_y], ys.max].max
483
580
  end
484
581
 
485
- def draw_a_path(s_x, s_y, t_x, t_y, target_arrow = :none)
486
- spacing = @global[:h_gap_between_nodes] * 1.25
582
+ # Path ends that stand in one line across the paths' run (on one label,
583
+ # or on labels straight above one another) are spread apart, each to the
584
+ # side its path heads for: a path going left leaves from the left and one
585
+ # going right from the right, so neither crosses the other's rail. Among
586
+ # ends heading the same way, the one that goes farther takes the outer
587
+ # place, since its rail runs outside. Left to right, the same holds with
588
+ # up and down in place of left and right. Neighbouring ends stand one
589
+ # rail spacing apart, the same distance that keeps two rails apart, or
590
+ # closer where that would take them past the narrowest label they share:
591
+ # an end stays on its label, and so off its neighbours and on the canvas.
592
+ def spread_shared_ends(paths)
593
+ along = @direction == "ltr" ? 1 : 0
594
+ ends = Hash.new { |h, k| h[k] = [] }
595
+ paths.each do |t|
596
+ s = [t[:x1], t[:y1]]
597
+ e = [t[:x2], t[:y2]]
598
+ ends[s[along].round] << [t, :s_offset, s[along], e[along], t[:across1]]
599
+ ends[e[along].round] << [t, :t_offset, e[along], s[along], t[:across2]]
600
+ end
601
+ ends.each_value do |shared|
602
+ gaps = shared.size - 1
603
+ step = gaps.zero? ? 0 : [rail_spacing, shared.map(&:last).min / gaps.to_f].min
604
+ shared.each_with_index
605
+ .sort_by { |(_, _, at, other, _), i| [other <=> at, -other, i] }
606
+ .each_with_index do |((t, key, _, _, _), _), place|
607
+ t[key] = (place - (gaps / 2.0)) * step
608
+ end
609
+ end
610
+ end
611
+
612
+ def draw_a_path(s_x, s_y, t_x, t_y, target_arrow = :none, s_offset: 0, t_offset: 0)
613
+ spacing = rail_spacing
487
614
  min_bulge = @global[:height_connector_to_text]
488
615
  # An end that carries an arrowhead needs a run long enough for the head
489
616
  # and a stem before the turn. The rail clears the farther end by the
@@ -496,14 +623,6 @@ module RSyntaxTree
496
623
  # arc, and taken from a layout measure so it holds at every font size.
497
624
  corner_radius = min_bulge / 2.0
498
625
 
499
- # Centered offset for multiple lines at the same endpoint
500
- s_key = "#{s_x.round}"
501
- t_key = "#{t_x.round}"
502
- @visited_x[s_key] = (@visited_x[s_key] || 0) + 1
503
- @visited_x[t_key] = (@visited_x[t_key] || 0) + 1
504
- s_offset = ((@visited_x[s_key] - 1) - (@visited_x[s_key] - 1) / 2.0) * spacing
505
- t_offset = ((@visited_x[t_key] - 1) - (@visited_x[t_key] - 1) / 2.0) * spacing
506
-
507
626
  dashed = true if target_arrow == :none
508
627
 
509
628
  if @direction == "ltr"
@@ -545,8 +664,8 @@ module RSyntaxTree
545
664
  new_y = [new_y, s_y + (sign * arrow_run)].send(outward) if source_arrow
546
665
  new_y = [new_y, t_y + (sign * arrow_run)].send(outward) if target_arrow_drawn
547
666
 
548
- new_s_x = s_x - s_offset
549
- new_t_x = t_x - t_offset
667
+ new_s_x = s_x + s_offset
668
+ new_t_x = t_x + t_offset
550
669
  new_y = clear_of_rails(new_y, [new_s_x, new_t_x].minmax, spacing, sign)
551
670
 
552
671
  note_path_extent([[new_s_x, s_y], [new_s_x, new_y],
@@ -706,6 +825,8 @@ module RSyntaxTree
706
825
  # here, so this could no longer live inside the drawing loop.
707
826
  def render_run(e, this_x, text_y, element, col)
708
827
  out = +""
828
+ # A coloured stretch paints its marks and the shapes drawn around them.
829
+ col = e[:color] if e[:color]
709
830
  this_y = text_y
710
831
  escaped_text = e[:text].gsub('>', '&gt;').gsub('<', '&lt;');
711
832
  decorations = []
@@ -730,6 +851,7 @@ module RSyntaxTree
730
851
 
731
852
  style += "font-weight: bold; fill: #{@col_emph}; " if e[:decoration].include?(:bold) || e[:decoration].include?(:bolditalic)
732
853
  style += "font-style: italic; " if e[:decoration].include?(:italic) || e[:decoration].include?(:bolditalic)
854
+ style += "fill: #{col}; " if e[:color]
733
855
  style += "\""
734
856
 
735
857
  fontstyle = FontFamily.for_svg(@fontstyle)
@@ -742,7 +864,9 @@ module RSyntaxTree
742
864
  enc_width = e[:width]
743
865
  enc_x = this_x
744
866
 
745
- if e[:decoration].include?(:hatched)
867
+ if e[:decoration].include?(:hatched) && e[:color]
868
+ fill = "url(##{@span_hatches[col] ||= "hatchSpan#{@span_hatches.size}"})"
869
+ elsif e[:decoration].include?(:hatched)
746
870
  case element.type
747
871
  when ETYPE_LEAF
748
872
  fill = if @color
@@ -814,6 +938,14 @@ module RSyntaxTree
814
938
  elsif e[:decoration].include?(:whitespace)
815
939
  return [out, this_x + e[:width]]
816
940
  else
941
+ # Text that changes colour part way is still one stretch, set as one
942
+ # and painted part by part.
943
+ if e[:parts]
944
+ escaped_text = e[:parts].map do |part, color|
945
+ t = part.gsub('>', '&gt;').gsub('<', '&lt;')
946
+ color ? "<tspan style='fill: #{color};'>#{t}</tspan>" : t
947
+ end.join
948
+ end
817
949
  out << set_tspan(this_x, this_y, style, decoration, fontstyle, escaped_text)
818
950
  this_x += e[:width]
819
951
  end
@@ -887,6 +1019,15 @@ module RSyntaxTree
887
1019
  [out, this_x + advance - lead]
888
1020
  end
889
1021
 
1022
+ def span_hatch_patterns(cell, stroke)
1023
+ @span_hatches.map do |color, id|
1024
+ "<pattern id=\"#{id}\" x=\"10\" y=\"10\" width=\"#{cell}\" height=\"#{cell}\" " \
1025
+ "patternUnits=\"userSpaceOnUse\" patternTransform=\"rotate(45)\">" \
1026
+ "<line x1=\"0\" y=\"0\" x2=\"0\" y2=\"#{cell}\" stroke=\"#{color}\" stroke-width=\"#{stroke}\"></line>" \
1027
+ "</pattern>"
1028
+ end.join("\n")
1029
+ end
1030
+
890
1031
  def draw_rectangle(x1, y1, width, height, col, bline = false)
891
1032
  swidth = bline ? @global[:stroke_bold] : @global[:stroke_normal]
892
1033
  @extra_lines << "<polygon style='stroke:#{col}; stroke-width:#{swidth}; fill:none; stroke-linejoin:round; stroke-linecap:round;'
@@ -958,8 +1099,29 @@ module RSyntaxTree
958
1099
  lgeom = nil
959
1100
  end
960
1101
  # A movement path leaves a label on the side the words are on: below
961
- # it top to bottom, above it bottom to top.
1102
+ # it top to bottom, above it bottom to top. A path to a phrase stops
1103
+ # at the edge of everything the phrase holds instead, its shade
1104
+ # included, so that it reads as reaching the phrase as a whole and
1105
+ # does not run in through the daughters to the label.
1106
+ path_x = x1
962
1107
  path_y = @direction == "btt" ? element.vertical_indent - @global[:height_connector_to_text] : y1
1108
+ if !element.children.empty? && element.path.any? { |marker| !marker.start_with?("-") }
1109
+ reach = phrase_reach(element)
1110
+ hctt = @global[:height_connector_to_text]
1111
+ case @direction
1112
+ when "ltr" then path_x = reach[:right] + hctt
1113
+ when "btt" then path_y = reach[:top] - hctt
1114
+ else path_y = reach[:bottom] + hctt
1115
+ end
1116
+ end
1117
+ # How much of the label there is across the paths' run, for the ends
1118
+ # that share it to be spread over: an even amount either side of the
1119
+ # anchor, which left to right sits below the middle of the label.
1120
+ across = if @direction == "ltr"
1121
+ [element.content_height - (@global[:height_connector_to_text] * 1.5), 0].max
1122
+ else
1123
+ element.content_width
1124
+ end
963
1125
  et = element.path
964
1126
  et.each do |tr|
965
1127
  if /\A-(>|<)?(\d+)\z/ =~ tr
@@ -974,20 +1136,20 @@ module RSyntaxTree
974
1136
  elsif /\A(?:>|<)(\d+)\z/ =~ tr
975
1137
  tr = $1
976
1138
  if path_pool_target[tr]
977
- path_pool_target[tr] << [x1, path_y]
1139
+ path_pool_target[tr] << [path_x, path_y, across]
978
1140
  else
979
- path_pool_target[tr] = [[x1, path_y]]
1141
+ path_pool_target[tr] = [[path_x, path_y, across]]
980
1142
  end
981
1143
  path_flags << tr
982
1144
  elsif path_pool_source[tr]
983
1145
  if path_pool_other[tr]
984
- path_pool_other[tr] << [x1, path_y]
1146
+ path_pool_other[tr] << [path_x, path_y, across]
985
1147
  else
986
- path_pool_other[tr] = [[x1, path_y]]
1148
+ path_pool_other[tr] = [[path_x, path_y, across]]
987
1149
  end
988
1150
  path_flags << tr
989
1151
  else
990
- path_pool_source[tr] = [x1, path_y]
1152
+ path_pool_source[tr] = [path_x, path_y, across]
991
1153
  path_flags << tr
992
1154
  end
993
1155
  raise RSTError.new(+"Error: input text contains a path having more than two ends:\n > #{tr}", code: :path_multiple_ends, hint: "A path or line takes exactly two ends; check the numbers.", retryable: true) if path_flags.tally.any? { |_k, v| v > 2 } || line_flags.tally.any? { |_k, v| v > 2 }
@@ -1008,11 +1170,11 @@ module RSyntaxTree
1008
1170
  path_flags.delete(k)
1009
1171
  if (targets = path_pool_target[k])
1010
1172
  targets.each do |t|
1011
- paths << { x1: v[0], y1: v[1], x2: t[0], y2: t[1], arrow: :single }
1173
+ paths << { x1: v[0], y1: v[1], x2: t[0], y2: t[1], across1: v[2], across2: t[2], arrow: :single }
1012
1174
  end
1013
1175
  elsif (others = path_pool_other[k])
1014
1176
  others.each do |t|
1015
- paths << { x1: v[0], y1: v[1], x2: t[0], y2: t[1], arrow: :none }
1177
+ paths << { x1: v[0], y1: v[1], x2: t[0], y2: t[1], across1: v[2], across2: t[2], arrow: :none }
1016
1178
  end
1017
1179
  end
1018
1180
  end
@@ -1025,7 +1187,7 @@ module RSyntaxTree
1025
1187
  next if fst.nil?
1026
1188
 
1027
1189
  targets.each do |t|
1028
- paths << { x1: fst[0], y1: fst[1], x2: t[0], y2: t[1], arrow: :double }
1190
+ paths << { x1: fst[0], y1: fst[1], x2: t[0], y2: t[1], across1: fst[2], across2: t[2], arrow: :double }
1029
1191
  end
1030
1192
  end
1031
1193
 
@@ -1033,8 +1195,9 @@ module RSyntaxTree
1033
1195
  # than across its ends. Left to right, every rail already clears the
1034
1196
  # ones drawn before it, in the order written.
1035
1197
  paths = paths.each_with_index.sort_by { |t, i| [(t[:x1] - t[:x2]).abs, i] }.map(&:first) unless @direction == "ltr"
1198
+ spread_shared_ends(paths)
1036
1199
  paths.each do |t|
1037
- draw_a_path(t[:x1], t[:y1], t[:x2], t[:y2], t[:arrow])
1200
+ draw_a_path(t[:x1], t[:y1], t[:x2], t[:y2], t[:arrow], s_offset: t[:s_offset], t_offset: t[:t_offset])
1038
1201
  end
1039
1202
 
1040
1203
  line_pool.each do |_k, v|
@@ -1102,8 +1265,7 @@ module RSyntaxTree
1102
1265
  else
1103
1266
  ""
1104
1267
  end
1105
- dash = @fontsize / 4.0
1106
- dasharray = dashed ? "stroke-dasharray='#{dash} #{dash}'" : ""
1268
+ dasharray = dashed ? dash_array : ""
1107
1269
  swidth = bline ? @global[:stroke_bold] : @global[:stroke_normal]
1108
1270
 
1109
1271
  if s_arrow && t_arrow
@@ -1168,8 +1330,7 @@ module RSyntaxTree
1168
1330
  else
1169
1331
  ""
1170
1332
  end
1171
- dash = @fontsize / 4.0
1172
- dasharray = dashed ? "stroke-dasharray='#{dash} #{dash}'" : ""
1333
+ dasharray = dashed ? dash_array : ""
1173
1334
  swidth = bline ? @global[:stroke_bold] : @global[:stroke_normal]
1174
1335
 
1175
1336
  "<line x1='#{x1}' y1='#{y1}' x2='#{x2}' y2='#{y2}' style='fill: none; stroke: #{col}; stroke-width:#{swidth}; stroke-linecap:round;' #{dasharray} #{string}/>"
@@ -1372,8 +1533,12 @@ module RSyntaxTree
1372
1533
  [child_y, parent_y]
1373
1534
  end
1374
1535
 
1375
- def line_to_parent(parent, child)
1376
- return if child.horizontal_indent.zero?
1536
+ # Dashed from +dash_from+ on, when given: a line through the joints leaf
1537
+ # alignment inserted is drawn one piece at a time, and each piece picks up
1538
+ # the dash where the one above it left off, so the run reads as one line.
1539
+ # Returns how long the line drawn is.
1540
+ def line_to_parent(parent, child, dash_from: nil)
1541
+ return 0 if child.horizontal_indent.zero?
1377
1542
 
1378
1543
  if @direction == "ltr"
1379
1544
  # LTR: parent's right side → child's left side
@@ -1391,19 +1556,9 @@ module RSyntaxTree
1391
1556
  mid_y1 = y1
1392
1557
  mid_x2 = mid_x1
1393
1558
  mid_y2 = y2
1394
- @tree_data += @polyline_styles.sub(/CHIX/, x1.to_s)
1395
- .sub(/CHIY/, y1.to_s)
1396
- .sub(/MIDX1/, mid_x1.to_s)
1397
- .sub(/MIDY1/, mid_y1.to_s)
1398
- .sub(/MIDX2/, mid_x2.to_s)
1399
- .sub(/MIDY2/, mid_y2.to_s)
1400
- .sub(/PARX/, x2.to_s)
1401
- .sub(/PARY/, y2.to_s)
1559
+ points = [[x1, y1], [mid_x1, mid_y1], [mid_x2, mid_y2], [x2, y2]]
1402
1560
  else
1403
- line_data = @line_styles.sub(/X1/, x1.to_s)
1404
- line_data = line_data.sub(/Y1/, y1.to_s)
1405
- line_data = line_data.sub(/X2/, x2.to_s)
1406
- @tree_data += line_data.sub(/Y2/, y2.to_s)
1561
+ points = [[x1, y1], [x2, y2]]
1407
1562
  end
1408
1563
  else
1409
1564
  # TTB: parent's bottom → child's top
@@ -1418,25 +1573,38 @@ module RSyntaxTree
1418
1573
  mid_x2 = par_x
1419
1574
  mid_y2 = mid_y1
1420
1575
 
1421
- @tree_data += @polyline_styles.sub(/CHIX/, chi_x.to_s)
1422
- .sub(/CHIY/, chi_y.to_s)
1423
- .sub(/MIDX1/, mid_x1.to_s)
1424
- .sub(/MIDY1/, mid_y1.to_s)
1425
- .sub(/MIDX2/, mid_x2.to_s)
1426
- .sub(/MIDY2/, mid_y2.to_s)
1427
- .sub(/PARX/, par_x.to_s)
1428
- .sub(/PARY/, par_y.to_s)
1576
+ points = [[chi_x, chi_y], [mid_x1, mid_y1], [mid_x2, mid_y2], [par_x, par_y]]
1429
1577
  else
1430
1578
  x1 = child.horizontal_indent + child.content_width / 2.0
1431
1579
  x2 = parent.horizontal_indent + parent.content_width / 2.0
1432
1580
  y1, y2 = connector_edges(child, parent)
1433
-
1434
- line_data = @line_styles.sub(/X1/, x1.to_s)
1435
- line_data = line_data.sub(/Y1/, y1.to_s)
1436
- line_data = line_data.sub(/X2/, x2.to_s)
1437
- @tree_data += line_data.sub(/Y2/, y2.to_s)
1581
+ points = [[x1, y1], [x2, y2]]
1438
1582
  end
1439
1583
  end
1584
+ emit_connector(points, dash_from)
1585
+ end
1586
+
1587
+ # +points+ run from the child to the parent. A dashed line is laid the
1588
+ # other way, from the parent down, so that its dash carries on into the
1589
+ # piece below it.
1590
+ def emit_connector(points, dash_from)
1591
+ dash = ""
1592
+ if dash_from
1593
+ points = points.reverse
1594
+ dash = " #{dash_array} stroke-dashoffset='#{dash_from.round(3)}'"
1595
+ end
1596
+ if points.size == 2
1597
+ (x1, y1), (x2, y2) = points
1598
+ @tree_data += @line_styles.sub(/X1/, x1.to_s).sub(/Y1/, y1.to_s)
1599
+ .sub(/X2/, x2.to_s).sub(/Y2/, y2.to_s).sub(" />", "#{dash} />")
1600
+ else
1601
+ (chix, chiy), (mx1, my1), (mx2, my2), (parx, pary) = points
1602
+ @tree_data += @polyline_styles.sub(/CHIX/, chix.to_s).sub(/CHIY/, chiy.to_s)
1603
+ .sub(/MIDX1/, mx1.to_s).sub(/MIDY1/, my1.to_s)
1604
+ .sub(/MIDX2/, mx2.to_s).sub(/MIDY2/, my2.to_s)
1605
+ .sub(/PARX/, parx.to_s).sub(/PARY/, pary.to_s).sub(" />", "#{dash} />")
1606
+ end
1607
+ points.each_cons(2).sum { |(ax, ay), (bx, by)| Math.hypot(bx - ax, by - ay) }
1440
1608
  end
1441
1609
 
1442
1610
  # Draw a triangle between child/parent elements