archsight 0.5.4 → 0.5.6

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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/docs/archimate.md +4 -3
  3. data/docs/diagram.md +107 -6
  4. data/docs/icons.md +1 -0
  5. data/docs/index.md.erb +1 -0
  6. data/docs/modeling.md +71 -2
  7. data/lib/archsight/diagram/graph/attributes.rb +66 -11
  8. data/lib/archsight/diagram/graph/builder.rb +101 -3
  9. data/lib/archsight/diagram/graph/edge.rb +7 -1
  10. data/lib/archsight/diagram/graph/node.rb +29 -1
  11. data/lib/archsight/diagram/graph/port.rb +28 -0
  12. data/lib/archsight/diagram/graph.rb +18 -1
  13. data/lib/archsight/diagram/includes.rb +22 -4
  14. data/lib/archsight/diagram/layout/alignment.rb +138 -0
  15. data/lib/archsight/diagram/layout/expander.rb +13 -0
  16. data/lib/archsight/diagram/layout/ports.rb +182 -0
  17. data/lib/archsight/diagram/layout/rank_arranger.rb +27 -1
  18. data/lib/archsight/diagram/layout/stacker.rb +3 -2
  19. data/lib/archsight/diagram/layout.rb +15 -5
  20. data/lib/archsight/diagram/legend/inventory.rb +52 -4
  21. data/lib/archsight/diagram/legend/row.rb +44 -0
  22. data/lib/archsight/diagram/parser/ast.rb +9 -0
  23. data/lib/archsight/diagram/parser.rb +56 -5
  24. data/lib/archsight/diagram/renderer/dataflow_renderer.rb +2 -1
  25. data/lib/archsight/diagram/renderer/edge_renderer.rb +5 -2
  26. data/lib/archsight/diagram/renderer/label_placer.rb +98 -22
  27. data/lib/archsight/diagram/renderer/node_renderer.rb +49 -1
  28. data/lib/archsight/diagram/renderer/path_renderer.rb +2 -2
  29. data/lib/archsight/diagram/renderer/stylesheet.rb +10 -1
  30. data/lib/archsight/diagram/renderer/text_renderer.rb +1 -0
  31. data/lib/archsight/diagram/renderer.rb +21 -5
  32. data/lib/archsight/diagram/resource_links.rb +18 -12
  33. data/lib/archsight/diagram/routing/edge_routing.rb +106 -5
  34. data/lib/archsight/diagram/support/markdown_text.rb +2 -2
  35. data/lib/archsight/diagram/support/text_metrics.rb +7 -3
  36. data/lib/archsight/export/confluence/analysis_output.rb +12 -5
  37. data/lib/archsight/linter/structure.rb +17 -3
  38. data/lib/archsight/linter.rb +30 -4
  39. data/lib/archsight/pdf/embeds.rb +3 -2
  40. data/lib/archsight/pdf/typst/archsight.typ +4 -0
  41. data/lib/archsight/resources/base.rb +5 -0
  42. data/lib/archsight/resources/grouping.rb +80 -0
  43. data/lib/archsight/resources/technology_node.rb +17 -2
  44. data/lib/archsight/resources/technology_service.rb +6 -0
  45. data/lib/archsight/resources/technology_system_software.rb +5 -0
  46. data/lib/archsight/resources.rb +28 -0
  47. data/lib/archsight/summary/lens.rb +10 -0
  48. data/lib/archsight/template.rb +2 -2
  49. data/lib/archsight/version.rb +1 -1
  50. metadata +5 -1
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Archsight
4
+ module Diagram
5
+ class Graph
6
+ # A labelled tab on a leaf's border (`port "id" { ... }`) that edges attach to with
7
+ # `leaf.id`. `side`/`at` are the explicit placement, `nil` when left to `PortPlacement`.
8
+ Port = Struct.new(:id, :node, :attrs, :line, keyword_init: true) do
9
+ def label = attrs["label"] || id
10
+ def side = attrs["side"]
11
+ def at = attrs["at"]
12
+ def badge = attrs["badge"]
13
+ def link = attrs["link"]
14
+ # Why its `resource` reference couldn't be resolved (see `ResourceLinks`), like `Node#broken_link`.
15
+ def broken_link = attrs["broken"]
16
+
17
+ # `node.port`, as an edge names it
18
+ def ref = "#{node.id}.#{id}"
19
+ end
20
+
21
+ # A top-level `badge "id" { tint; label }` or `line "style" { label }` (see `AST::Definition`).
22
+ Definition = Struct.new(:kind, :id, :attrs, :line, keyword_init: true) do
23
+ def label = attrs["label"] || id
24
+ def tint = attrs.fetch("tint", "gray")
25
+ end
26
+ end
27
+ end
28
+ end
@@ -8,6 +8,7 @@ require_relative "style/relations"
8
8
  require_relative "style/theme"
9
9
  require_relative "legend/modes"
10
10
  require_relative "graph/node"
11
+ require_relative "graph/port"
11
12
  require_relative "graph/edge"
12
13
  require_relative "graph/dataflow"
13
14
  require_relative "graph/attributes"
@@ -30,7 +31,7 @@ module Archsight
30
31
  SETTING_VALUES = { "theme" => -> { Theme.names }, "legend" => -> { Legend::MODES }, "ranks" => -> { RANKS_MODES } }.freeze
31
32
  SETTING_IVARS = { "theme" => :@theme_name, "legend" => :@legend_mode, "ranks" => :@ranks_mode }.freeze
32
33
 
33
- attr_reader :roots, :nodes_by_id, :edges, :dataflows, :theme_name, :legend_mode, :ranks_mode
34
+ attr_reader :roots, :nodes_by_id, :edges, :dataflows, :theme_name, :legend_mode, :ranks_mode, :definitions
34
35
 
35
36
  def self.build(statements)
36
37
  new(statements)
@@ -38,6 +39,7 @@ module Archsight
38
39
 
39
40
  def initialize(statements)
40
41
  @nodes_by_id = {}
42
+ @definitions = {}
41
43
  @roots = []
42
44
  raw_edges = []
43
45
  raw_dataflows = []
@@ -52,8 +54,11 @@ module Archsight
52
54
  raw_dataflows << stmt
53
55
  when AST::Setting
54
56
  apply_setting(stmt)
57
+ when AST::Definition
58
+ add_definition(stmt)
55
59
  end
56
60
  end
61
+ check_references!
57
62
 
58
63
  raw_edges.each { |e| Attributes.check_edge!(e) }
59
64
  raw_dataflows.each_with_object({}) do |df, seen|
@@ -70,9 +75,21 @@ module Archsight
70
75
 
71
76
  def node(id)
72
77
  @nodes_by_id.fetch(id) do
78
+ owner = port_owner(id)
79
+ raise GraphError, "#{id.inspect} is a port of #{owner.id.inspect}; only edges attach to ports" if owner&.port(id.delete_prefix("#{owner.id}."))
80
+
73
81
  raise GraphError, "unknown id #{id.inspect}"
74
82
  end
75
83
  end
84
+
85
+ # The `badge "id"` definition, `nil` if there is none.
86
+ def badge_definition(id) = @definitions[[:badge, id]]
87
+
88
+ # The legend text a top-level `line "dashed" { label }` gives that line style, `nil` without one.
89
+ def line_definition(style) = @definitions[[:line, style]]
90
+
91
+ # Every port of every leaf, in declaration order.
92
+ def ports = @nodes_by_id.values.flat_map(&:ports)
76
93
  end
77
94
  end
78
95
  end
@@ -93,11 +93,16 @@ module Archsight
93
93
  raise IncludeError, "in include #{path.inspect}: #{e.message}"
94
94
  end
95
95
 
96
- # Dataflows and settings apply to the whole diagram, so inside a container they are ignored (with a warning)
96
+ # Dataflows, settings and badge/line definitions apply to the whole diagram, so inside a container they are
97
+ # ignored (with a warning)
97
98
  def drop_whole_diagram_statements(path, parsed, line)
98
- dropped, kept = parsed.partition { |s| s.is_a?(AST::DataFlow) || s.is_a?(AST::Setting) }
99
+ dropped, kept = parsed.partition { |s| s.is_a?(AST::DataFlow) || s.is_a?(AST::Setting) || s.is_a?(AST::Definition) }
99
100
  dropped.each do |s|
100
- what = s.is_a?(AST::DataFlow) ? "dataflow #{s.id.inspect}" : "#{s.key} setting"
101
+ what = case s
102
+ when AST::DataFlow then "dataflow #{s.id.inspect}"
103
+ when AST::Definition then "#{s.kind} #{s.id.inspect}"
104
+ else "#{s.key} setting"
105
+ end
101
106
  @warnings << "include #{path.inspect} (line #{line}): the #{what} (line #{s.line}) is ignored, it only applies when included at the top level"
102
107
  end
103
108
  kept
@@ -145,9 +150,18 @@ module Archsight
145
150
  block.attrs["label"] ||= block.id # the label defaults to the id
146
151
  block.id = ref(block.id)
147
152
  end
153
+ block.attrs["align"] = rename_align(block.attrs["align"]) if block.attrs.key?("align")
148
154
  block.children.each { |child| rename(child) }
149
155
  end
150
156
 
157
+ # `top a, b` -> `top ns.a, ns.b`: the id of each entry, after its edge word if it has one
158
+ def rename_align(value)
159
+ value.split(",").map(&:strip).map do |entry|
160
+ word, rest = entry.split(/\s+/, 2)
161
+ rest && Graph::Node::ALIGN_EDGES.key?(word) ? "#{word} #{ref(rest.strip)}" : ref(entry)
162
+ end.join(", ")
163
+ end
164
+
151
165
  def rename_dataflow(flow)
152
166
  flow.attrs["label"] ||= flow.id
153
167
  flow.id = ref(flow.id)
@@ -159,8 +173,12 @@ module Archsight
159
173
  group
160
174
  end
161
175
 
176
+ # A defined id, or `node.port` of a defined node
162
177
  def ref(id)
163
- @defined[id] ? "#{@prefix}#{id}" : id
178
+ return "#{@prefix}#{id}" if @defined[id]
179
+
180
+ owner, dot, = id.rpartition(".")
181
+ !dot.empty? && @defined[owner] ? "#{@prefix}#{id}" : id
164
182
  end
165
183
  end
166
184
  end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Archsight
4
+ module Diagram
5
+ class Layout
6
+ # `align "top other"` (or `left`, `center`, `right`, `middle`, `bottom`; a bare id is `center`): once
7
+ # everything is placed, a node moves until that edge of it lines up with the same edge of the other node,
8
+ # which may sit in another container -- like the align buttons of a drawing editor. It only uses the
9
+ # free room its container already has, never growing one: where its container packs along that axis (a
10
+ # `layer` for left/right, a `stack` for up/down) it pushes its siblings along, keeping their order and
11
+ # gaps; elsewhere it moves within the container's inner box. It stops as close as it gets.
12
+ class Alignment
13
+ # Shifts below this are left alone (no visible change, no churn).
14
+ EPSILON = 0.5
15
+
16
+ AXES = { x: Axis::WIDTH, y: Axis::HEIGHT }.freeze
17
+
18
+ def initialize(graph, boxes, theme:)
19
+ @graph = graph
20
+ @boxes = boxes
21
+ @theme = theme
22
+ end
23
+
24
+ def apply
25
+ aligned = @graph.nodes_by_id.values.select(&:align)
26
+ return if aligned.empty?
27
+
28
+ # A second round settles a node aligned to a node that is aligned itself.
29
+ 2.times do
30
+ aligned.each { |node| node.alignments.each { |axis, edge, id| align(node, AXES.fetch(axis), edge, id) } }
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def align(node, axis, edge, target_id)
37
+ delta = wanted(axis, edge, @boxes[target_id]) - wanted(axis, edge, @boxes[node.id])
38
+ return if delta.abs < EPSILON
39
+
40
+ lo, hi = room(node, axis)
41
+ parent = node.parent
42
+ if parent && parent.main_axis == axis
43
+ sweep(parent.children.sort_by { |c| axis.position(@boxes[c.id]) }, node, axis, delta, lo, hi)
44
+ else
45
+ box = @boxes[node.id]
46
+ shift(node, axis, delta.clamp([lo - axis.leading_edge(box), 0.0].min, [hi - axis.trailing_edge(box), 0.0].max))
47
+ end
48
+ refit(parent, axis)
49
+ end
50
+
51
+ # The coordinate of `box` that `edge` lines up.
52
+ def wanted(axis, edge, box)
53
+ case edge
54
+ when "left", "top" then axis.leading_edge(box)
55
+ when "right", "bottom" then axis.trailing_edge(box)
56
+ else axis.position(box)
57
+ end
58
+ end
59
+
60
+ # The children of a container packed along `axis`, in order, with `node` wanting to move by `delta`: the
61
+ # others stay where they are unless they come closer to a neighbour than `min_gap`, then they are pushed
62
+ # along; everything stays inside `lo..hi`.
63
+ def sweep(children, node, axis, delta, lo, hi)
64
+ boxes = children.map { |c| @boxes[c.id] }
65
+ sizes = boxes.map { |b| axis.size(b) }
66
+ gaps = children.each_cons(2).map { |a, b| min_gap(a, b, axis) }
67
+ leads = boxes.each_with_index.map { |b, i| axis.leading_edge(b) + (children[i].equal?(node) ? delta : 0.0) }
68
+
69
+ push_on(leads, sizes, gaps)
70
+ leads[-1] = [leads[-1], hi - sizes[-1]].min
71
+ (leads.length - 2).downto(0) { |i| leads[i] = [leads[i], leads[i + 1] - gaps[i] - sizes[i]].min }
72
+ leads[0] = [leads[0], lo].max
73
+ push_on(leads, sizes, gaps)
74
+
75
+ children.each_with_index { |c, i| shift(c, axis, leads[i] - axis.leading_edge(boxes[i])) }
76
+ end
77
+
78
+ # How close two neighbours may come: the sibling gap plus half a port tab on each side that faces the
79
+ # other (see `RankArranger#port_overhangs`), never more than they are apart now.
80
+ def min_gap(a, b, axis)
81
+ current = [axis.leading_edge(@boxes[b.id]) - axis.trailing_edge(@boxes[a.id]), 0.0].max
82
+ trailing, leading = axis == Axis::HEIGHT ? %w[bottom top] : %w[right left]
83
+ [current, @theme.sibling_gap + tab_overhang(a, trailing) + tab_overhang(b, leading)].min
84
+ end
85
+
86
+ def tab_overhang(node, side)
87
+ return 0.0 unless node.leaf? && node.ports.any? { |p| p.side.nil? || p.side == side }
88
+
89
+ PortMetrics.new(@theme).height / 2.0
90
+ end
91
+
92
+ # Each leading edge at least its gap past the box before it.
93
+ def push_on(leads, sizes, gaps)
94
+ (1...leads.length).each { |i| leads[i] = [leads[i], leads[i - 1] + sizes[i - 1] + gaps[i - 1]].max }
95
+ end
96
+
97
+ # The room (lo, hi) a node may move in along `axis`: its container's inner box. An anonymous container
98
+ # packing along that axis is only as long as its children, so its room is that of its own container
99
+ # (one packing across it is as long as the line it sits in). Top-level nodes may use the whole drawing.
100
+ def room(node, axis)
101
+ container = node.parent
102
+ container = container.parent while container&.anonymous? && container.main_axis == axis
103
+ unless container
104
+ all = @graph.roots.map { |r| @boxes[r.id] }
105
+ return [all.map { |b| axis.leading_edge(b) }.min, all.map { |b| axis.trailing_edge(b) }.max]
106
+ end
107
+
108
+ box = @boxes[container.id]
109
+ padding = container.layout_padding(@theme)
110
+ title = axis == Axis::HEIGHT ? container.layout_title_height(@theme) : 0.0
111
+ [axis.leading_edge(box) + padding + title, axis.trailing_edge(box) - padding]
112
+ end
113
+
114
+ def shift(node, axis, delta)
115
+ return if delta.abs < EPSILON
116
+
117
+ box = @boxes[node.id]
118
+ axis.set_position(box, axis.position(box) + delta)
119
+ node.children.each { |c| shift(c, axis, delta) }
120
+ end
121
+
122
+ # An anonymous layer/stack's box follows its children (it draws nothing, but routing and the
123
+ # alignment of its own parent read it).
124
+ def refit(container, axis)
125
+ while container&.anonymous?
126
+ boxes = container.children.map { |c| @boxes[c.id] }
127
+ box = @boxes[container.id]
128
+ lead = boxes.map { |b| axis.leading_edge(b) }.min
129
+ trail = boxes.map { |b| axis.trailing_edge(b) }.max
130
+ axis.set_position(box, (lead + trail) / 2.0)
131
+ axis.set_size(box, trail - lead)
132
+ container = container.parent
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -46,6 +46,7 @@ module Archsight
46
46
  # tied to its box size in ways that break if stretched to match
47
47
  # an unrelated, much larger sibling).
48
48
  return if target_size <= current
49
+ return if natural_size?(node)
49
50
  return unless node.representer.stretchable?
50
51
 
51
52
  axis.set_size(box, target_size)
@@ -109,6 +110,7 @@ module Archsight
109
110
  node.children.each do |c|
110
111
  expand(c, axis, inner, inherited: extend)
111
112
  axis.set_position(@boxes[c.id], center)
113
+ place_natural(c, axis, inner, center) if natural_size?(c)
112
114
  end
113
115
  end
114
116
  return
@@ -121,6 +123,17 @@ module Archsight
121
123
  expand(node.children.first, axis, own_target - inset(node, axis), inherited: extend)
122
124
  end
123
125
 
126
+ # A leaf with `extend "false"` keeps its own size inside a wider stack or layer.
127
+ def natural_size?(node) = node.leaf? && node.attrs["extend"] == "false"
128
+
129
+ # Puts such a leaf at its `anchor` (centered by default) inside the `room` long stretch of `axis`
130
+ # around `center` that its stack or layer gave it.
131
+ def place_natural(node, axis, room, center)
132
+ box = @boxes[node.id]
133
+ extra = [room - axis.size(box), 0.0].max
134
+ axis.set_position(box, center - (room / 2.0) + (axis.size(box) / 2.0) + node.anchor_shift(axis, extra, default: :center))
135
+ end
136
+
124
137
  # `extend "height"`: grows `node` to `target` height keeping its
125
138
  # content top-aligned (or at its `anchor`), then hands the extra room to whatever inside
126
139
  # can absorb it -- a `stack` child carrying `extend "height"`
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../support/text_metrics"
4
+
5
+ module Archsight
6
+ module Diagram
7
+ class Layout
8
+ # The size of a port's tab (see `Graph::Port`): its label in monospace at the edge-label font size, a
9
+ # little padding around it, and room for its badge dot.
10
+ class PortMetrics
11
+ PADDING_X = 6.0
12
+ PADDING_Y = 4.5
13
+ BADGE_ROOM = 10.0
14
+ # Never closer to a corner of the leaf than this, never closer to the next tab than `GAP`.
15
+ MARGIN = 10.0
16
+ GAP = 8.0
17
+
18
+ def initialize(theme)
19
+ @theme = theme
20
+ end
21
+
22
+ def font_size = @theme.edge_label_font_size
23
+ def height = font_size + (2 * PADDING_Y)
24
+
25
+ def width(port)
26
+ chars = TextMetrics.lines(port.label).map { |line| TextMetrics.markdown_runs(line).sum { |content, _| content.length } }.max
27
+ (chars * TextMetrics::MONO_WIDTH * font_size / 1000.0) + (2 * PADDING_X) + (port.badge ? BADGE_ROOM : 0.0)
28
+ end
29
+
30
+ # The length a side needs to hold `ports` side by side (`along` is the tab's size along that side).
31
+ def side_length(ports, &)
32
+ return 0.0 if ports.empty?
33
+
34
+ ports.sum(&) + (GAP * (ports.length - 1)) + (2 * MARGIN)
35
+ end
36
+
37
+ # `[min width, min height]` of a leaf with `ports`. A port without a `side` may end up on either
38
+ # side of the leaf, so it counts for the top and bottom alike (where most ports go).
39
+ def leaf_minimum(ports)
40
+ on = ->(side) { ports.select { |p| p.side == side || (p.side.nil? && %w[top bottom].include?(side)) } }
41
+ tab_height = height
42
+ [%w[top bottom].map { |side| side_length(on.call(side)) { |p| width(p) } }.max,
43
+ %w[left right].map { |side| side_length(on.call(side)) { tab_height } }.max]
44
+ end
45
+ end
46
+
47
+ # Puts every port's tab on its leaf's border, once the leaves have their final position: on its `side`
48
+ # (or the side facing what its edges connect it to), at its `at` (or lined up with what its edges
49
+ # connect it to, when that does not span the whole side; else spread in declaration order), the tabs of
50
+ # one side never overlapping. A tab straddles the border, half inside the leaf. Two rounds, so ports
51
+ # whose edges connect them to other ports settle on each other's place.
52
+ class PortPlacement
53
+ HORIZONTAL_SIDES = %i[top bottom].freeze
54
+
55
+ def initialize(graph, boxes, theme:)
56
+ @graph = graph
57
+ @boxes = boxes
58
+ @metrics = PortMetrics.new(theme)
59
+ end
60
+
61
+ # `{ "node.port" => Box }`
62
+ def place
63
+ ports = @graph.ports
64
+ return {} if ports.empty?
65
+
66
+ tabs = {}
67
+ 2.times { tabs = place_all(ports, tabs) }
68
+ tabs.each_value(&:freeze_bounds!)
69
+ tabs
70
+ end
71
+
72
+ private
73
+
74
+ def place_all(ports, previous)
75
+ sides = Hash.new { |h, k| h[k] = [] }
76
+ ports.each_with_index do |port, index|
77
+ box = @boxes[port.node.id]
78
+ peers = peer_boxes(port, previous)
79
+ side = (port.side || facing_side(box, peers)).to_sym
80
+ width = @metrics.width(port)
81
+ along = HORIZONTAL_SIDES.include?(side) ? width : @metrics.height
82
+ wanted = port.at ? at_position(box, side, port.at, along) : preference(box, side, peers)
83
+ sides[[port.node.id, side]] << { port: port, along: along, width: width, wanted: wanted, index: index }
84
+ end
85
+
86
+ sides.each_with_object({}) do |((node_id, side), members), tabs|
87
+ box = @boxes[node_id]
88
+ spread(box, side, members).each { |m| tabs[m[:port].ref] = tab_box(box, side, m[:position], m[:width]) }
89
+ end
90
+ end
91
+
92
+ # The boxes at the other ends of `port`'s edges: the peer's tab once it is placed, else its leaf.
93
+ def peer_boxes(port, previous)
94
+ @graph.edges.filter_map do |edge|
95
+ if edge.from_port.equal?(port)
96
+ (edge.to_port && previous[edge.to_port.ref]) || @boxes[edge.to.id]
97
+ elsif edge.to_port.equal?(port)
98
+ (edge.from_port && previous[edge.from_port.ref]) || @boxes[edge.from.id]
99
+ end
100
+ end
101
+ end
102
+
103
+ # The side whose outside the peers' mean center lies furthest beyond (top without peers).
104
+ def facing_side(box, peers)
105
+ return "top" if peers.empty?
106
+
107
+ dx = (peers.sum(&:x) / peers.length) - box.x
108
+ dy = (peers.sum(&:y) / peers.length) - box.y
109
+ if dy.abs - (box.height / 2.0) >= dx.abs - (box.width / 2.0)
110
+ dy.negative? ? "top" : "bottom"
111
+ else
112
+ dx.negative? ? "left" : "right"
113
+ end
114
+ end
115
+
116
+ # Where along its side a port lines up with its peers: the mean of their centers, leaving out a peer
117
+ # that spans the whole side (it lines up anywhere). `nil` when no peer has a preference.
118
+ def preference(box, side, peers)
119
+ lo, hi = side_range(box, side)
120
+ centers = peers.filter_map do |peer|
121
+ peer_lo, peer_hi, center = span(peer, side)
122
+ center unless peer_lo <= lo && peer_hi >= hi
123
+ end
124
+ centers.empty? ? nil : centers.sum / centers.length
125
+ end
126
+
127
+ def at_position(box, side, at, along)
128
+ lo, hi = side_range(box, side)
129
+ case at
130
+ when "left", "top" then lo + (along / 2.0)
131
+ when "right", "bottom" then hi - (along / 2.0)
132
+ when "center" then (lo + hi) / 2.0
133
+ else lo + (along / 2.0) + ((hi - lo - along) * at.to_f / 100.0)
134
+ end
135
+ end
136
+
137
+ # Ports without a preference share the side evenly in declaration order, then every port is pushed
138
+ # just far enough for the tabs not to overlap and to stay inside the side.
139
+ def spread(box, side, members)
140
+ lo, hi = side_range(box, side)
141
+ free = members.select { |m| m[:wanted].nil? }
142
+ free.each_with_index { |m, i| m[:wanted] = lo + ((hi - lo) * (i + 0.5) / free.length) }
143
+
144
+ ordered = members.sort_by { |m| [m[:wanted], m[:index]] }
145
+ ordered.each { |m| m[:position] = m[:wanted].clamp(lo + half(m), [lo + half(m), hi - half(m)].max) }
146
+ ordered.each_cons(2) { |a, b| b[:position] = [b[:position], a[:position] + apart(a, b)].max }
147
+ last = ordered.last
148
+ last[:position] = [last[:position], hi - half(last)].min
149
+ ordered.reverse.each_cons(2) { |b, a| a[:position] = [a[:position], b[:position] - apart(a, b)].min }
150
+ ordered
151
+ end
152
+
153
+ def half(member) = member[:along] / 2.0
154
+
155
+ # How far apart the centers of two neighbouring tabs must be.
156
+ def apart(one, other) = half(one) + half(other) + PortMetrics::GAP
157
+
158
+ # The part of a side a tab may sit on: inside the leaf's corners.
159
+ def side_range(box, side)
160
+ if HORIZONTAL_SIDES.include?(side)
161
+ [box.left + PortMetrics::MARGIN, box.right - PortMetrics::MARGIN]
162
+ else
163
+ [box.top + PortMetrics::MARGIN, box.bottom - PortMetrics::MARGIN]
164
+ end
165
+ end
166
+
167
+ def span(peer, side)
168
+ HORIZONTAL_SIDES.include?(side) ? [peer.left, peer.right, peer.x] : [peer.top, peer.bottom, peer.y]
169
+ end
170
+
171
+ def tab_box(box, side, position, width)
172
+ case side
173
+ when :top then Box.new(position, box.top, width, @metrics.height)
174
+ when :bottom then Box.new(position, box.bottom, width, @metrics.height)
175
+ when :left then Box.new(box.left, position, width, @metrics.height)
176
+ else Box.new(box.right, position, width, @metrics.height)
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
@@ -101,6 +101,30 @@ module Archsight
101
101
  # up, not necessarily through the narrow strip between the ranks, so
102
102
  # widening the strip for it doesn't reliably help and just leaves an
103
103
  # oversized gap.
104
+ # How much further apart two ranks go because port tabs stick out of the sides they face (half a tab
105
+ # each), keyed like `Stacker#pack`'s gaps.
106
+ def port_overhangs(children)
107
+ metrics = PortMetrics.new(@theme)
108
+ children.each_cons(2).with_object({}) do |(a, b), gaps|
109
+ extra = overhang(a, "bottom", metrics) + overhang(b, "top", metrics)
110
+ gaps[[a.id, b.id].sort] = extra if extra.positive?
111
+ end
112
+ end
113
+
114
+ # Half a tab if a port sits on `side` of `node`, or of a leaf an anonymous layer/stack puts on that side
115
+ # (a named container's padding already leaves room). A port without a `side` may end up on any side.
116
+ def overhang(node, side, metrics)
117
+ if node.leaf?
118
+ node.ports.any? { |p| p.side.nil? || p.side == side } ? metrics.height / 2.0 : 0.0
119
+ elsif !node.anonymous? || node.children.empty?
120
+ 0.0
121
+ elsif node.stack?
122
+ overhang(side == "top" ? node.children.first : node.children.last, side, metrics)
123
+ else
124
+ node.children.map { |c| overhang(c, side, metrics) }.max
125
+ end
126
+ end
127
+
104
128
  def arrange_stack(group, children)
105
129
  return if children.empty?
106
130
 
@@ -114,8 +138,10 @@ module Archsight
114
138
  children, axis: Axis::HEIGHT, default_gap: group.gap(@theme.stack_gap),
115
139
  related_gap: group.gap(@theme.sibling_gap),
116
140
  label_gaps: @rank_topology.rank_label_gaps(rank_edges),
117
- related_pairs: rank_edges.map { |(ra, rb, _e)| [ra.id, rb.id].sort }
141
+ related_pairs: rank_edges.map { |(ra, rb, _e)| [ra.id, rb.id].sort },
142
+ extra_gaps: port_overhangs(children)
118
143
  )
144
+ children.each { |c| expander.place_natural(c, Axis::WIDTH, max_width, max_width / 2.0) if expander.natural_size?(c) }
119
145
  end
120
146
  end
121
147
  end
@@ -28,7 +28,8 @@ module Archsight
28
28
  # `related_pairs`, otherwise `default_gap`; a label gap is only a
29
29
  # minimum on top of that (room for the label), so it can widen the
30
30
  # gap but never shrink a larger one -- e.g. an explicit `gap "150"`.
31
- def pack(children, axis:, default_gap:, related_gap:, label_gaps: {}, related_pairs: [])
31
+ # `extra_gaps` (by sorted id pair) is added on top of the pair's gap, e.g. for port tabs sticking out.
32
+ def pack(children, axis:, default_gap:, related_gap:, label_gaps: {}, related_pairs: [], extra_gaps: {})
32
33
  return if children.empty?
33
34
 
34
35
  cross = axis.cross
@@ -40,7 +41,7 @@ module Archsight
40
41
  unless i.zero?
41
42
  pair = [children[i - 1].id, c.id].sort
42
43
  base = related_pairs.include?(pair) ? related_gap : default_gap
43
- pos += [label_gaps[pair], base].compact.max
44
+ pos += [label_gaps[pair], base].compact.max + extra_gaps.fetch(pair, 0.0)
44
45
  end
45
46
 
46
47
  size = axis.size(box)
@@ -16,6 +16,8 @@ require_relative "layout/ranked_arranger"
16
16
  require_relative "layout/rank_arranger"
17
17
  require_relative "layout/root_alignment"
18
18
  require_relative "layout/globalizer"
19
+ require_relative "layout/alignment"
20
+ require_relative "layout/ports"
19
21
  require_relative "legend"
20
22
 
21
23
  module Archsight
@@ -74,8 +76,9 @@ module Archsight
74
76
  # font sizes/title offsets the boxes were sized for; `legend` (a
75
77
  # `Legend::Placement`, or nil for none) is where the legend went --
76
78
  # its boxes are in `boxes` like any node's.
77
- Result = Struct.new(:boxes, :width, :height, :theme, :legend, keyword_init: true) do
78
- def initialize(theme: Theme::DEFAULT, **) = super
79
+ # `ports` holds each port's tab (`{ "node.port" => Box }`, see `PortPlacement`).
80
+ Result = Struct.new(:boxes, :width, :height, :theme, :legend, :ports, keyword_init: true) do
81
+ def initialize(theme: Theme::DEFAULT, ports: {}, **) = super
79
82
  end
80
83
 
81
84
  # Pairs a Graph::Node with its computed Box, for the routing stages
@@ -129,10 +132,11 @@ module Archsight
129
132
  @root_alignment.align_connected_roots
130
133
  end
131
134
  @globalizer.globalize_all(@graph.roots)
135
+ Alignment.new(@graph, @boxes, theme: @theme).apply
132
136
  legend = place_legend
133
137
  normalize_to_canvas
134
138
  @boxes.each_value(&:freeze_bounds!)
135
- Result.new(boxes: @boxes, width: @canvas_width, height: @canvas_height, theme: @theme, legend: legend)
139
+ Result.new(boxes: @boxes, width: @canvas_width, height: @canvas_height, theme: @theme, legend: legend, ports: place_ports)
136
140
  end
137
141
 
138
142
  # See `Layout.attach_legend`: the canvas only ever grows to take the
@@ -145,11 +149,13 @@ module Archsight
145
149
  frame = @boxes[legend.frame.id]
146
150
  width = [result.width, frame.right + @theme.canvas_margin].max
147
151
  height = [result.height, frame.bottom + @theme.canvas_margin].max
148
- Result.new(boxes: @boxes, width: width, height: height, theme: @theme, legend: legend)
152
+ Result.new(boxes: @boxes, width: width, height: height, theme: @theme, legend: legend, ports: result.ports)
149
153
  end
150
154
 
151
155
  private
152
156
 
157
+ def place_ports = PortPlacement.new(@graph, @boxes, theme: @theme).place
158
+
153
159
  # Bottom-up: determine width/height for every node, and (for groups)
154
160
  # position children relative to the group's own local origin (0, 0).
155
161
  def size_and_place(node)
@@ -177,7 +183,11 @@ module Archsight
177
183
  return text_size(node, extra_height) if node.kind == :text
178
184
 
179
185
  width = [label_width(node) + @theme.node_label_padding, @theme.node_min_width].max
180
- [width, @theme.node_height + extra_height]
186
+ height = @theme.node_height + extra_height
187
+ return [width, height] if node.ports.empty?
188
+
189
+ min_width, min_height = PortMetrics.new(@theme).leaf_minimum(node.ports)
190
+ [[width, min_width].max, [height, min_height].max]
181
191
  end
182
192
 
183
193
  # A text node has no box to pad out: one line height per line and a little air, and no minimum width, so a