Coverage for app / documents / visuals / primitives.py: 94%

88 statements  

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

1"""Shapes every visual draws with: labels, rules, rounded bars, a legend. 

2 

3Every ``String`` is given its font: reportlab's default is Times, which the 

4PDF never uses. Text is drawn as ``String``, which interprets no markup. 

5""" 

6 

7from __future__ import annotations 

8 

9from collections.abc import Sequence 

10from dataclasses import dataclass 

11 

12from reportlab.graphics.shapes import Group, Line, Path, Rect, String 

13from reportlab.lib.colors import Color 

14 

15from .text import ellipsize, measure 

16from .theme import LEGEND_SIZE, MUTED, PX, Theme 

17 

18 

19def label( 

20 x: float, 

21 y: float, 

22 text: str, 

23 font: str, 

24 size: float, 

25 color: Color, 

26 anchor: str = "start", 

27 angle: float = 0.0, 

28) -> String | Group: 

29 """``text`` at ``(x, y)``, its baseline there, turned ``angle`` degrees 

30 anticlockwise about that point.""" 

31 string = String( 

32 0 if angle else x, 

33 0 if angle else y, 

34 text, 

35 fontName=font, 

36 fontSize=size, 

37 fillColor=color, 

38 textAnchor=anchor, 

39 ) 

40 if not angle: 

41 return string 

42 group = Group(string) 

43 group.translate(x, y) 

44 group.rotate(angle) 

45 return group 

46 

47 

48def rule( 

49 x0: float, 

50 y0: float, 

51 x1: float, 

52 y1: float, 

53 color: Color, 

54 width: float = 0.5, 

55 dash: Sequence[float] | None = None, 

56 opacity: float = 1.0, 

57) -> Line: 

58 line = Line(x0, y0, x1, y1, strokeColor=color, strokeWidth=width) 

59 if dash: 

60 line.strokeDashArray = list(dash) 

61 if opacity < 1: 

62 line.strokeOpacity = opacity 

63 return line 

64 

65 

66def rounded_bar( 

67 x: float, width: float, base: float, end: float, radius: float, color: Color 

68) -> Path | Rect: 

69 """A bar from ``base`` to ``end``, its corners at ``end`` rounded — the 

70 end away from zero, as recharts rounds ``radius=[3,3,0,0]``.""" 

71 lo, hi = sorted((base, end)) 

72 height = hi - lo 

73 r = min(radius, width / 2, height) 

74 if r <= 0.05: 

75 return Rect(x, lo, width, height, fillColor=color, strokeColor=None) 

76 path = Path(fillColor=color, strokeColor=None) 

77 k = 0.5523 * r # a quarter circle's control length 

78 if end >= base: 

79 path.moveTo(x, lo) 

80 path.lineTo(x, hi - r) 

81 path.curveTo(x, hi - r + k, x + r - k, hi, x + r, hi) 

82 path.lineTo(x + width - r, hi) 

83 path.curveTo(x + width - r + k, hi, x + width, hi - r + k, x + width, hi - r) 

84 path.lineTo(x + width, lo) 

85 else: 

86 path.moveTo(x, hi) 

87 path.lineTo(x, lo + r) 

88 path.curveTo(x, lo + r - k, x + r - k, lo, x + r, lo) 

89 path.lineTo(x + width - r, lo) 

90 path.curveTo(x + width - r + k, lo, x + width, lo + r - k, x + width, lo + r) 

91 path.lineTo(x + width, hi) 

92 path.closePath() 

93 return path 

94 

95 

96@dataclass(frozen=True) 

97class LegendEntry: 

98 text: str 

99 color: Color 

100 #: ``square`` for a filled series, ``line`` for a drawn one. 

101 swatch: str = "square" 

102 

103 

104#: The most entries a legend lists; the rest are counted. 

105MAX_LEGEND = 30 

106_SWATCH = 7.0 

107_GAP = 10.0 

108_ENTRY_MAX = 140.0 

109 

110 

111def legend( 

112 entries: Sequence[LegendEntry], width: float, theme: Theme 

113) -> tuple[list[object], float]: 

114 """A legend laid out in centred rows across ``width``: its shapes, drawn 

115 downward from y = 0, and its height.""" 

116 size = LEGEND_SIZE 

117 shown = list(entries[:MAX_LEGEND]) 

118 items: list[tuple[str, LegendEntry | None, float]] = [] 

119 for entry in shown: 

120 text = ellipsize(entry.text or "—", theme.regular, size, _ENTRY_MAX) 

121 items.append((text, entry, _SWATCH + 4 + measure(text, theme.regular, size))) 

122 if len(entries) > MAX_LEGEND: 

123 more = f"+{len(entries) - MAX_LEGEND} more" 

124 items.append((more, None, measure(more, theme.regular, size))) 

125 rows: list[list[tuple[str, LegendEntry | None, float]]] = [[]] 

126 used = 0.0 

127 for item in items: 

128 need = item[2] + (_GAP if rows[-1] else 0) 

129 if rows[-1] and used + need > width: 

130 rows.append([]) 

131 used, need = 0.0, item[2] 

132 rows[-1].append(item) 

133 used += need 

134 line_height = size + 5 

135 shapes: list[object] = [] 

136 for r, row in enumerate(rows): 

137 total = sum(w for _, _, w in row) + _GAP * (len(row) - 1) 

138 x = max(0.0, (width - total) / 2) 

139 y = -(r + 1) * line_height + 3 

140 for text, entry, w in row: 

141 if entry is not None: 

142 if entry.swatch == "line": 

143 shapes.append( 

144 rule(x, y + 2.5, x + _SWATCH, y + 2.5, entry.color, 2 * PX) 

145 ) 

146 else: 

147 shapes.append( 

148 Rect( 

149 x, 

150 y, 

151 _SWATCH, 

152 _SWATCH - 1, 

153 fillColor=entry.color, 

154 strokeColor=None, 

155 ) 

156 ) 

157 shapes.append( 

158 label(x + _SWATCH + 4, y, text, theme.regular, size, MUTED) 

159 ) 

160 else: 

161 shapes.append(label(x, y, text, theme.regular, size, MUTED)) 

162 x += w + _GAP 

163 return shapes, len(rows) * line_height + 2