Coverage for app / documents / visuals / mermaid / shapes.py: 97%

194 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1"""A node's shape: how big it is around its label, where an edge meets its 

2outline, and how it is drawn. One registered class a shape, named as 

3Mermaid's flowchart names them (``SHAPES``); state, class and ER boxes are 

4rectangles drawn by their own diagram. 

5 

6Sizes follow Mermaid's own (``rendering-elements/shapes``): a rhombus is a 

7square on its label's width plus height, a hexagon's points are a quarter of 

8its height, a cylinder's ends are as deep as Mermaid's ``rx / (2.5 + w / 50)``. 

9""" 

10 

11from __future__ import annotations 

12 

13import math 

14from abc import ABC, abstractmethod 

15from dataclasses import dataclass 

16from typing import ClassVar 

17 

18from reportlab.lib.colors import Color 

19 

20from .canvas import KAPPA, Canvas, Point 

21from .compound import Box, rect_boundary 

22 

23 

24@dataclass(frozen=True) 

25class Look: 

26 fill: Color 

27 stroke: Color 

28 width: float 

29 dash: tuple[float, ...] | None = None 

30 

31 

32class Shape(ABC): 

33 name: ClassVar[str] 

34 #: Wraps its label narrower: a rhombus grows with its label twice over. 

35 narrow: ClassVar[bool] = False 

36 

37 def size( 

38 self, text_w: float, text_h: float, pad_x: float, pad_y: float 

39 ) -> tuple[float, float]: 

40 return text_w + 2 * pad_x, text_h + 2 * pad_y 

41 

42 @abstractmethod 

43 def outline(self, box: Box) -> list[Point]: 

44 """The outline as a polygon, for where an edge meets it.""" 

45 

46 def draw(self, canvas: Canvas, box: Box, look: Look) -> None: 

47 canvas.polygon( 

48 self.outline(box), 

49 fill=look.fill, 

50 stroke=look.stroke, 

51 width=look.width, 

52 dash=look.dash, 

53 ) 

54 

55 def boundary(self, box: Box, toward: Point) -> Point: 

56 return polygon_boundary(self.outline(box), (box.cx, box.cy), toward) 

57 

58 

59SHAPES: dict[str, Shape] = {} 

60 

61 

62def register_shape(cls: type[Shape]) -> type[Shape]: 

63 SHAPES[cls.name] = cls() 

64 return cls 

65 

66 

67def polygon_boundary(polygon: list[Point], centre: Point, toward: Point) -> Point: 

68 """Where the ray from ``centre`` toward ``toward`` leaves ``polygon``.""" 

69 cx, cy = centre 

70 dx, dy = toward[0] - cx, toward[1] - cy 

71 if not dx and not dy: 

72 return toward 

73 best: float | None = None 

74 for (x0, y0), (x1, y1) in zip(polygon, polygon[1:] + polygon[:1]): 

75 ex, ey = x1 - x0, y1 - y0 

76 det = dx * (-ey) - dy * (-ex) 

77 if abs(det) < 1e-12: 

78 continue 

79 rx, ry = x0 - cx, y0 - cy 

80 t = (rx * (-ey) - ry * (-ex)) / det 

81 u = (dx * ry - dy * rx) / det 

82 if t > 1e-9 and -1e-9 <= u <= 1 + 1e-9 and (best is None or t < best): 

83 best = t 

84 if best is None: 

85 return toward 

86 return cx + dx * best, cy + dy * best 

87 

88 

89def _arc( 

90 cx: float, cy: float, rx: float, ry: float, start: float, end: float, steps: int 

91) -> list[Point]: 

92 return [ 

93 (cx + rx * math.cos(a), cy + ry * math.sin(a)) 

94 for a in (start + (end - start) * i / steps for i in range(steps + 1)) 

95 ] 

96 

97 

98@register_shape 

99class RectShape(Shape): 

100 name = "rect" 

101 

102 def outline(self, box: Box) -> list[Point]: 

103 return [ 

104 (box.x, box.y), 

105 (box.right, box.y), 

106 (box.right, box.bottom), 

107 (box.x, box.bottom), 

108 ] 

109 

110 def draw(self, canvas: Canvas, box: Box, look: Look) -> None: 

111 canvas.rect( 

112 box.x, box.y, box.width, box.height, 

113 fill=look.fill, stroke=look.stroke, width=look.width, dash=look.dash, 

114 ) # fmt: skip 

115 

116 def boundary(self, box: Box, toward: Point) -> Point: 

117 return rect_boundary(box, toward) 

118 

119 

120@register_shape 

121class RoundedShape(RectShape): 

122 name = "rounded" 

123 

124 def draw(self, canvas: Canvas, box: Box, look: Look) -> None: 

125 canvas.rect( 

126 box.x, box.y, box.width, box.height, radius=min(5.0, box.height / 2), 

127 fill=look.fill, stroke=look.stroke, width=look.width, dash=look.dash, 

128 ) # fmt: skip 

129 

130 

131@register_shape 

132class StadiumShape(Shape): 

133 name = "stadium" 

134 

135 def size( 

136 self, text_w: float, text_h: float, pad_x: float, pad_y: float 

137 ) -> tuple[float, float]: 

138 h = text_h + 2 * pad_y 

139 return text_w + h + pad_x, h 

140 

141 def outline(self, box: Box) -> list[Point]: 

142 r = box.height / 2 

143 right = _arc(box.right - r, box.cy, r, r, -math.pi / 2, math.pi / 2, 8) 

144 left = _arc(box.x + r, box.cy, r, r, math.pi / 2, 3 * math.pi / 2, 8) 

145 return right + left 

146 

147 def draw(self, canvas: Canvas, box: Box, look: Look) -> None: 

148 canvas.rect( 

149 box.x, box.y, box.width, box.height, radius=box.height / 2, 

150 fill=look.fill, stroke=look.stroke, width=look.width, dash=look.dash, 

151 ) # fmt: skip 

152 

153 

154@register_shape 

155class SubroutineShape(RectShape): 

156 name = "subroutine" 

157 

158 def size( 

159 self, text_w: float, text_h: float, pad_x: float, pad_y: float 

160 ) -> tuple[float, float]: 

161 return text_w + 2 * pad_x + 12, text_h + 2 * pad_y 

162 

163 def draw(self, canvas: Canvas, box: Box, look: Look) -> None: 

164 super().draw(canvas, box, look) 

165 for x in (box.x + 6, box.right - 6): 

166 canvas.line(x, box.y, x, box.bottom, color=look.stroke, width=look.width) 

167 

168 

169@register_shape 

170class CylinderShape(Shape): 

171 name = "cylinder" 

172 

173 @staticmethod 

174 def depth(width: float) -> float: 

175 rx = width / 2 

176 return rx / (2.5 + width / 50) 

177 

178 def size( 

179 self, text_w: float, text_h: float, pad_x: float, pad_y: float 

180 ) -> tuple[float, float]: 

181 w = text_w + 2 * pad_x 

182 return w, text_h + 2 * pad_y + 2 * self.depth(w) 

183 

184 def outline(self, box: Box) -> list[Point]: 

185 ry = self.depth(box.width) 

186 rx = box.width / 2 

187 top = _arc(box.cx, box.y + ry, rx, ry, math.pi, 2 * math.pi, 8) 

188 bottom = _arc(box.cx, box.bottom - ry, rx, ry, 0, math.pi, 8) 

189 return top + bottom 

190 

191 def draw(self, canvas: Canvas, box: Box, look: Look) -> None: 

192 ry = self.depth(box.width) 

193 rx = box.width / 2 

194 x0, x1, cx = box.x, box.right, box.cx 

195 top, bottom = box.y + ry, box.bottom - ry 

196 k = KAPPA 

197 body = [ 

198 ("move", (x0, top)), 

199 ("line", (x0, bottom)), 

200 ("curve", (x0, bottom + ry * k, cx - rx * k, bottom + ry, cx, bottom + ry)), 

201 ("curve", (cx + rx * k, bottom + ry, x1, bottom + ry * k, x1, bottom)), 

202 ("line", (x1, top)), 

203 ("curve", (x1, top - ry * k, cx + rx * k, top - ry, cx, top - ry)), 

204 ("curve", (cx - rx * k, top - ry, x0, top - ry * k, x0, top)), 

205 ] 

206 canvas.path( 

207 body, 

208 fill=look.fill, 

209 stroke=look.stroke, 

210 width=look.width, 

211 dash=look.dash, 

212 close=True, 

213 ) 

214 rim = [ 

215 ("move", (x0, top)), 

216 ("curve", (x0, top + ry * k, cx - rx * k, top + ry, cx, top + ry)), 

217 ("curve", (cx + rx * k, top + ry, x1, top + ry * k, x1, top)), 

218 ] 

219 canvas.path(rim, stroke=look.stroke, width=look.width, dash=look.dash) 

220 

221 

222@register_shape 

223class CircleShape(Shape): 

224 name = "circle" 

225 

226 def size( 

227 self, text_w: float, text_h: float, pad_x: float, pad_y: float 

228 ) -> tuple[float, float]: 

229 d = math.hypot(text_w, text_h) + pad_y 

230 return d, d 

231 

232 def outline(self, box: Box) -> list[Point]: 

233 r = min(box.width, box.height) / 2 

234 return _arc(box.cx, box.cy, r, r, 0, 2 * math.pi, 32)[:-1] 

235 

236 def draw(self, canvas: Canvas, box: Box, look: Look) -> None: 

237 canvas.circle( 

238 box.cx, box.cy, min(box.width, box.height) / 2, 

239 fill=look.fill, stroke=look.stroke, width=look.width, dash=look.dash, 

240 ) # fmt: skip 

241 

242 def boundary(self, box: Box, toward: Point) -> Point: 

243 r = min(box.width, box.height) / 2 

244 dx, dy = toward[0] - box.cx, toward[1] - box.cy 

245 d = math.hypot(dx, dy) 

246 if not d: 

247 return toward 

248 return box.cx + dx / d * r, box.cy + dy / d * r 

249 

250 

251@register_shape 

252class DoubleCircleShape(CircleShape): 

253 name = "doublecircle" 

254 GAP = 3.5 

255 

256 def size( 

257 self, text_w: float, text_h: float, pad_x: float, pad_y: float 

258 ) -> tuple[float, float]: 

259 d, _ = super().size(text_w, text_h, pad_x, pad_y) 

260 return d + 2 * self.GAP, d + 2 * self.GAP 

261 

262 def draw(self, canvas: Canvas, box: Box, look: Look) -> None: 

263 super().draw(canvas, box, look) 

264 canvas.circle( 

265 box.cx, box.cy, min(box.width, box.height) / 2 - self.GAP, 

266 fill=None, stroke=look.stroke, width=look.width, 

267 ) # fmt: skip 

268 

269 

270class _PolygonShape(Shape): 

271 @abstractmethod 

272 def outline(self, box: Box) -> list[Point]: ... 

273 

274 

275@register_shape 

276class DiamondShape(_PolygonShape): 

277 name = "diamond" 

278 narrow = True 

279 

280 def size( 

281 self, text_w: float, text_h: float, pad_x: float, pad_y: float 

282 ) -> tuple[float, float]: 

283 s = text_w + text_h + 2 * pad_y 

284 return s, s 

285 

286 def outline(self, box: Box) -> list[Point]: 

287 return [ 

288 (box.cx, box.y), 

289 (box.right, box.cy), 

290 (box.cx, box.bottom), 

291 (box.x, box.cy), 

292 ] 

293 

294 

295@register_shape 

296class HexagonShape(_PolygonShape): 

297 name = "hexagon" 

298 

299 def size( 

300 self, text_w: float, text_h: float, pad_x: float, pad_y: float 

301 ) -> tuple[float, float]: 

302 h = text_h + 2 * pad_y 

303 return text_w + 2 * pad_x + h / 2, h 

304 

305 def outline(self, box: Box) -> list[Point]: 

306 m = box.height / 4 

307 return [ 

308 (box.x + m, box.y), (box.right - m, box.y), (box.right, box.cy), 

309 (box.right - m, box.bottom), (box.x + m, box.bottom), (box.x, box.cy), 

310 ] # fmt: skip 

311 

312 

313@register_shape 

314class AsymmetricShape(_PolygonShape): 

315 """``>text]``: a flag, notched on the left.""" 

316 

317 name = "asymmetric" 

318 

319 def size( 

320 self, text_w: float, text_h: float, pad_x: float, pad_y: float 

321 ) -> tuple[float, float]: 

322 h = text_h + 2 * pad_y 

323 return text_w + 2 * pad_x + h / 2, h 

324 

325 def outline(self, box: Box) -> list[Point]: 

326 return [ 

327 (box.x, box.y), (box.right, box.y), (box.right, box.bottom), 

328 (box.x, box.bottom), (box.x + box.height / 2, box.cy), 

329 ] # fmt: skip 

330 

331 

332class _Slanted(_PolygonShape): 

333 #: Which way the top and bottom edges lean: (top, bottom) offsets as 

334 #: fractions of the slant, left side. 

335 top_left: ClassVar[float] 

336 top_right: ClassVar[float] 

337 bottom_left: ClassVar[float] 

338 bottom_right: ClassVar[float] 

339 

340 def size( 

341 self, text_w: float, text_h: float, pad_x: float, pad_y: float 

342 ) -> tuple[float, float]: 

343 h = text_h + 2 * pad_y 

344 return text_w + 2 * pad_x + 2 * h / 3, h 

345 

346 def outline(self, box: Box) -> list[Point]: 

347 s = box.height / 3 

348 return [ 

349 (box.x + self.top_left * s, box.y), 

350 (box.right - self.top_right * s, box.y), 

351 (box.right - self.bottom_right * s, box.bottom), 

352 (box.x + self.bottom_left * s, box.bottom), 

353 ] 

354 

355 

356@register_shape 

357class LeanRightShape(_Slanted): 

358 """``[/text/]``""" 

359 

360 name = "lean_right" 

361 top_left, top_right, bottom_left, bottom_right = 2.0, 0.0, 0.0, 2.0 

362 

363 

364@register_shape 

365class LeanLeftShape(_Slanted): 

366 """``[\\text\\]``""" 

367 

368 name = "lean_left" 

369 top_left, top_right, bottom_left, bottom_right = 0.0, 2.0, 2.0, 0.0 

370 

371 

372@register_shape 

373class TrapezoidShape(_Slanted): 

374 """``[/text\\]``: wider at the bottom.""" 

375 

376 name = "trapezoid" 

377 top_left, top_right, bottom_left, bottom_right = 1.0, 1.0, 0.0, 0.0 

378 

379 

380@register_shape 

381class InvTrapezoidShape(_Slanted): 

382 """``[\\text/]``: wider at the top.""" 

383 

384 name = "inv_trapezoid" 

385 top_left, top_right, bottom_left, bottom_right = 0.0, 0.0, 1.0, 1.0