Path graph: keep edge-pinned relay labels from vanishing
A relay a balanced rank pins near a canvas edge centred its long label off-canvas, placed nothing, and — unlike an endpoint — had no inward-slide or side-placement fallback, so its marker went silently unlabelled. Give every node the endpoint's inward-anchor clamp and marker_label fallback. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
b561ff2d27
commit
8b64b39513
2 files changed
+37
-12
No files matched your search
+17
-12
@@ -568,10 +568,13 @@ def _place_labels(
|
||||
|
||||
Endpoints first, then relays down the lanes, so the named ends win any contest for a
|
||||
cell. Each label is tried on the side away from the graph's middle first (spreading the
|
||||
text outward off the busy centre), preferring a row the drawn lines don't already occupy;
|
||||
an endpoint, hard against a canvas edge, slides its anchor inward far enough for the whole
|
||||
name to land, and falls back to sitting beside its marker. A name is only ever shortened
|
||||
when it is wider than the entire canvas.
|
||||
text outward off the busy centre), preferring a row the drawn lines don't already occupy.
|
||||
Any node hard against a canvas edge — an endpoint by construction, or a relay a balanced
|
||||
rank pins near the origin or us — slides its centre anchor inward far enough for the whole
|
||||
name to land, since a centred label overhanging the edge places nothing and would leave the
|
||||
marker silently unlabelled. When both stacked rows are blocked the label falls back to
|
||||
sitting beside its marker. A name is only ever shortened when it is wider than the entire
|
||||
canvas.
|
||||
"""
|
||||
endpoints = [n for n in (DST_NODE, SRC_NODE) if n in pos]
|
||||
relays = sorted(
|
||||
@@ -586,11 +589,12 @@ def _place_labels(
|
||||
label = label[: max(1, width - 1)] + "…"
|
||||
rgb = label_rgb_of(node)
|
||||
x, y = pos[node]
|
||||
anchor_x = x
|
||||
if node in (SRC_NODE, DST_NODE):
|
||||
half = len(label) // 2
|
||||
cx = min(max(x >> 1, half), max(half, width - (len(label) - half)))
|
||||
anchor_x = cx * 2
|
||||
# Clamp the centre cell so the whole label fits between the canvas edges: a marker near
|
||||
# an edge would otherwise centre its name off-canvas, place nothing, and be dropped. A
|
||||
# marker with room to spare keeps its true centre (the clamp is a no-op there).
|
||||
half = len(label) // 2
|
||||
cx = min(max(x >> 1, half), max(half, width - (len(label) - half)))
|
||||
anchor_x = cx * 2
|
||||
rows_out = (y - 4, y + 4) if y <= mid_y else (y + 4, y - 4)
|
||||
if any(
|
||||
canvas.place_label(anchor_x, sy, label, rgb, bold=True, avoid_dots=True)
|
||||
@@ -601,6 +605,7 @@ def _place_labels(
|
||||
canvas.place_label(anchor_x, sy, label, rgb, bold=True) for sy in rows_out
|
||||
):
|
||||
continue
|
||||
if node in (SRC_NODE, DST_NODE):
|
||||
if not canvas.marker_label(x, y, label, rgb, avoid_dots=True):
|
||||
canvas.marker_label(x, y, label, rgb)
|
||||
# Both rows blocked: sit the label beside the marker (clear of the drawn lines if it
|
||||
# can, else over them) rather than drop it.
|
||||
if not canvas.marker_label(x, y, label, rgb, avoid_dots=True):
|
||||
canvas.marker_label(x, y, label, rgb)
|
||||
@@ -147,6 +147,26 @@ def test_every_label_shows_on_a_busy_graph() -> None:
|
||||
assert name in plain, f"{name} was dropped from the graph"
|
||||
|
||||
|
||||
def test_edge_pinned_relay_labels_are_not_dropped() -> None:
|
||||
"""A relay a balanced rank pins near a canvas edge still shows its whole name.
|
||||
|
||||
A long chain hung off ``cc`` drives its balanced rank hard against the origin and pushes
|
||||
the shared ``bb`` hard against us, so both land near an edge where a *centred* long label
|
||||
overhangs the canvas. Those anchors must slide inward — like an endpoint's — rather than
|
||||
place nothing and leave the marker silently unlabelled.
|
||||
"""
|
||||
names = {"aa": "UpperStation", "bb": "RightEdgeStation", "cc": "LeftEdgeStation"}
|
||||
tail = tuple(f"g{i}" for i in range(1, 13))
|
||||
layers = [
|
||||
PathLayer(("bb",), WHITE, 3),
|
||||
PathLayer(("aa", "bb"), GREY, 2),
|
||||
PathLayer(("cc", *tail, "bb"), GREY, 1),
|
||||
]
|
||||
plain = _ANSI.sub("", "\n".join(_render(layers, label_of=lambda n: names.get(n))))
|
||||
for name in names.values():
|
||||
assert name in plain, f"{name} was dropped near a canvas edge"
|
||||
|
||||
|
||||
def test_a_subsumed_route_adds_no_duplicate_markers() -> None:
|
||||
"""A route whose hops are all carried by a stronger one draws through them, not doubled."""
|
||||
layers = [
|
||||
|
||||
Reference in new issue
Block a user