Implement left/right corner labels; add end-anchor support to text-to-paths
This commit is contained in:
+30
-2
@@ -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):
|
def _text_to_paths(text, x, y, font_size, fd: _FontData, anchor='start', fill='#1a1a1a', wrap_transform=None):
|
||||||
s = font_size / fd.upm
|
s = font_size / fd.upm
|
||||||
|
|
||||||
if anchor == 'middle':
|
if anchor in ('middle', 'end'):
|
||||||
total_w = sum(
|
total_w = sum(
|
||||||
fd.hmtx.get(fd.cmap.get(ord(ch)), (0, 0))[0] * s
|
fd.hmtx.get(fd.cmap.get(ord(ch)), (0, 0))[0] * s
|
||||||
for ch in text if fd.cmap.get(ord(ch))
|
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 = []
|
parts = []
|
||||||
cx = x
|
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))
|
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>']
|
parts += [' </g>', '</svg>']
|
||||||
return '\n'.join(parts)
|
return '\n'.join(parts)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user