Coverage for app / documents / visuals / mermaid / markers.py: 98%
141 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
1"""What sits at an edge's end: arrowheads, circles, crosses, UML's triangles
2and diamonds, and ER's crow's feet. One registered class each, drawn with
3its tip on the end point and pointing along the edge's last step; ``back``
4is how far the line stops short, so it never shows through a hollow marker
5or past a tip.
6"""
8from __future__ import annotations
10import math
11from abc import ABC, abstractmethod
12from typing import ClassVar
14from reportlab.lib.colors import Color
16from .canvas import Canvas, Point
17from .look import WHITE
20class Marker(ABC):
21 name: ClassVar[str]
22 back: ClassVar[float] = 0.0
24 @abstractmethod
25 def draw(
26 self, canvas: Canvas, tip: Point, unit: Point, color: Color, width: float
27 ) -> None:
28 """Drawn at ``tip``; ``unit`` points along the edge into the tip."""
31MARKERS: dict[str, Marker] = {}
34def register_marker(cls: type[Marker]) -> type[Marker]:
35 MARKERS[cls.name] = cls()
36 return cls
39def _frame(tip: Point, unit: Point, along: float, across: float) -> Point:
40 """A point ``along`` back from the tip and ``across`` to its left."""
41 ux, uy = unit
42 return tip[0] - ux * along - uy * across, tip[1] - uy * along + ux * across
45@register_marker
46class Arrow(Marker):
47 name = "arrow"
48 back = 6.0
50 def draw(
51 self, canvas: Canvas, tip: Point, unit: Point, color: Color, width: float
52 ) -> None:
53 canvas.polygon(
54 [tip, _frame(tip, unit, 7.0, 3.2), _frame(tip, unit, 7.0, -3.2)],
55 fill=color, stroke=color, width=0.5,
56 ) # fmt: skip
59@register_marker
60class SmallArrow(Marker):
61 """A class diagram's association and dependency."""
63 name = "small_arrow"
64 back = 5.0
66 def draw(
67 self, canvas: Canvas, tip: Point, unit: Point, color: Color, width: float
68 ) -> None:
69 canvas.polygon(
70 [tip, _frame(tip, unit, 6.0, 2.8), _frame(tip, unit, 6.0, -2.8)],
71 fill=color, stroke=color, width=0.5,
72 ) # fmt: skip
75@register_marker
76class OpenArrow(Marker):
77 """A sequence diagram's asynchronous message: ``-)``."""
79 name = "open_arrow"
81 def draw(
82 self, canvas: Canvas, tip: Point, unit: Point, color: Color, width: float
83 ) -> None:
84 for across in (3.5, -3.5):
85 end = _frame(tip, unit, 7.0, across)
86 canvas.line(tip[0], tip[1], end[0], end[1], color=color, width=width)
89@register_marker
90class Dot(Marker):
91 """A flowchart's ``--o``."""
93 name = "circle"
94 back = 6.0
96 def draw(
97 self, canvas: Canvas, tip: Point, unit: Point, color: Color, width: float
98 ) -> None:
99 cx, cy = _frame(tip, unit, 3.2, 0)
100 canvas.circle(cx, cy, 3.2, fill=color, stroke=color, width=0.5)
103@register_marker
104class Cross(Marker):
105 """A flowchart's ``--x``."""
107 name = "cross"
108 back = 2.0
110 def draw(
111 self, canvas: Canvas, tip: Point, unit: Point, color: Color, width: float
112 ) -> None:
113 for sign in (1, -1):
114 a = _frame(tip, unit, 0.5, 3.0 * sign)
115 b = _frame(tip, unit, 6.5, -3.0 * sign)
116 canvas.line(a[0], a[1], b[0], b[1], color=color, width=max(width, 1.2))
119@register_marker
120class HollowTriangle(Marker):
121 """UML generalisation and realisation: ``<|``."""
123 name = "triangle"
124 back = 9.0
126 def draw(
127 self, canvas: Canvas, tip: Point, unit: Point, color: Color, width: float
128 ) -> None:
129 canvas.polygon(
130 [tip, _frame(tip, unit, 9.0, 5.0), _frame(tip, unit, 9.0, -5.0)],
131 fill=WHITE, stroke=color, width=width,
132 ) # fmt: skip
135class _Diamond(Marker):
136 back = 11.0
137 filled: ClassVar[bool]
139 def draw(
140 self, canvas: Canvas, tip: Point, unit: Point, color: Color, width: float
141 ) -> None:
142 canvas.polygon(
143 [tip, _frame(tip, unit, 5.5, 3.5), _frame(tip, unit, 11.0, 0), _frame(tip, unit, 5.5, -3.5)],
144 fill=color if self.filled else WHITE, stroke=color, width=width,
145 ) # fmt: skip
148@register_marker
149class FilledDiamond(_Diamond):
150 """UML composition: ``*``."""
152 name = "diamond_filled"
153 filled = True
156@register_marker
157class HollowDiamond(_Diamond):
158 """UML aggregation: ``o``."""
160 name = "diamond"
161 filled = False
164@register_marker
165class Lollipop(Marker):
166 """A provided interface: ``()``."""
168 name = "lollipop"
169 back = 8.0
171 def draw(
172 self, canvas: Canvas, tip: Point, unit: Point, color: Color, width: float
173 ) -> None:
174 cx, cy = _frame(tip, unit, 4.0, 0)
175 canvas.circle(cx, cy, 4.0, fill=WHITE, stroke=color, width=width)
178class _CrowsFoot(Marker):
179 """ER cardinalities, read from the entity outward: the mark nearest the
180 entity is the maximum, the farther one the minimum."""
182 #: "one" (a bar) or "many" (a crow's foot), at the entity.
183 most: ClassVar[str]
184 #: "one" (a bar) or "zero" (a circle), farther out.
185 least: ClassVar[str]
187 def draw(
188 self, canvas: Canvas, tip: Point, unit: Point, color: Color, width: float
189 ) -> None:
190 if self.most == "many":
191 spread = 5.0
192 joint = _frame(tip, unit, 9.0, 0)
193 for across in (spread, 0.0, -spread):
194 end = _frame(tip, unit, 0.0, across)
195 canvas.line(
196 joint[0], joint[1], end[0], end[1], color=color, width=width
197 )
198 else:
199 a, b = _frame(tip, unit, 4.0, 5.0), _frame(tip, unit, 4.0, -5.0)
200 canvas.line(a[0], a[1], b[0], b[1], color=color, width=width)
201 if self.least == "zero":
202 cx, cy = _frame(tip, unit, 15.0, 0)
203 canvas.circle(cx, cy, 3.5, fill=WHITE, stroke=color, width=width)
204 else:
205 a, b = _frame(tip, unit, 12.0, 5.0), _frame(tip, unit, 12.0, -5.0)
206 canvas.line(a[0], a[1], b[0], b[1], color=color, width=width)
209@register_marker
210class ExactlyOne(_CrowsFoot):
211 name = "only_one"
212 most, least = "one", "one"
215@register_marker
216class ZeroOrOne(_CrowsFoot):
217 name = "zero_or_one"
218 most, least = "one", "zero"
221@register_marker
222class OneOrMore(_CrowsFoot):
223 name = "one_or_more"
224 most, least = "many", "one"
227@register_marker
228class ZeroOrMore(_CrowsFoot):
229 name = "zero_or_more"
230 most, least = "many", "zero"
233def end_direction(points: list[Point], at_start: bool) -> Point:
234 """The unit vector along the edge into its end."""
235 if at_start:
236 a, b = points[1], points[0]
237 else:
238 a, b = points[-2], points[-1]
239 dx, dy = b[0] - a[0], b[1] - a[1]
240 d = math.hypot(dx, dy) or 1.0
241 return dx / d, dy / d
244def draw_edge(
245 canvas: Canvas,
246 points: list[Point],
247 *,
248 color: Color,
249 width: float,
250 dash: tuple[float, ...] | None = None,
251 start: str | None = None,
252 end: str | None = None,
253) -> None:
254 """The edge's curve through ``points``, with its markers."""
255 if len(points) < 2:
256 return
257 points = list(points)
258 ends: list[tuple[Marker, Point, Point]] = []
259 for name, at_start in ((start, True), (end, False)):
260 marker = MARKERS.get(name) if name else None
261 if marker is None:
262 continue
263 unit = end_direction(points, at_start)
264 tip = points[0] if at_start else points[-1]
265 ends.append((marker, tip, unit))
266 if marker.back:
267 index = 0 if at_start else -1
268 neighbour = points[1] if at_start else points[-2]
269 room = math.hypot(tip[0] - neighbour[0], tip[1] - neighbour[1])
270 step = min(marker.back, room * 0.8)
271 points[index] = (tip[0] - unit[0] * step, tip[1] - unit[1] * step)
272 canvas.curve(points, color=color, width=width, dash=dash)
273 for marker, tip, unit in ends:
274 marker.draw(canvas, tip, unit, color, width)