Implement left/right corner labels; add end-anchor support to text-to-paths

This commit is contained in:
2026-07-28 23:58:36 +02:00
parent b6e1c8e76a
commit 4ca61016a7
+30 -2
View File
@@ -300,12 +300,12 @@ def _markup_unit_svg(unit: str, fd: _FontData, fill='#1a1a1a') -> str:
def _text_to_paths(text, x, y, font_size, fd: _FontData, anchor='start', fill='#1a1a1a', wrap_transform=None):
s = font_size / fd.upm
if anchor == 'middle':
if anchor in ('middle', 'end'):
total_w = sum(
fd.hmtx.get(fd.cmap.get(ord(ch)), (0, 0))[0] * s
for ch in text if fd.cmap.get(ord(ch))
)
x -= total_w / 2
x -= total_w / 2 if anchor == 'middle' else total_w
parts = []
cx = x
@@ -506,6 +506,34 @@ def generate_svg(unit, min_val, max_val, left_label, right_label, big_ticks, sma
)
parts.append(' ' + _markup_unit_svg(unit, fd))
left_lines = left_label.split('\n')[:2]
right_lines = right_label.split('\n')[:2]
for i, line in enumerate(left_lines):
if not line:
continue
y = LEFT_Y0 + i * CORNER_LINE_SPACING
if use_paths:
parts.append(' ' + _text_to_paths(line, LEFT_X, y, CORNER_FONT_SIZE, fd, anchor='start'))
else:
parts.append(
f' <text style="font-size:{CORNER_FONT_SIZE:.5f}px;font-family:\'{fd.label}\',monospace;fill:#1a1a1a"'
f' x="{LEFT_X:.5f}" y="{y:.5f}">{escape(line)}</text>'
)
for i, line in enumerate(right_lines):
if not line:
continue
y = RIGHT_Y0 + i * CORNER_LINE_SPACING
if use_paths:
parts.append(' ' + _text_to_paths(line, RIGHT_X, y, CORNER_FONT_SIZE, fd, anchor='end'))
else:
parts.append(
f' <text style="font-size:{CORNER_FONT_SIZE:.5f}px;font-family:\'{fd.label}\',monospace;fill:#1a1a1a"'
f' text-anchor="end" x="{RIGHT_X:.5f}" y="{y:.5f}">{escape(line)}</text>'
)
parts += [' </g>', '</svg>']
return '\n'.join(parts)