Fix character alignment with textLength attribute

Use SVG textLength and lengthAdjust='spacingAndGlyphs' to enforce
exact character spacing, preventing gaps between box-drawing and
other characters that may render at slightly different widths.

Also include whitespace spans in output for proper alignment.
This commit is contained in:
GitHub Copilot
2026-01-24 18:51:10 +00:00
parent 62c12db537
commit d82f40c4fa
+9 -4
View File
@@ -191,8 +191,8 @@ def render_terminal_svg(
f'fill="{span["bg"]}"/>' f'fill="{span["bg"]}"/>'
) )
if not text or (text.isspace() and not span["has_bg"]): if not text:
# Skip empty spans without background, but advance position # Skip truly empty spans (wide char placeholders already counted)
x += columns * char_width x += columns * char_width
continue continue
@@ -208,14 +208,19 @@ def render_terminal_svg(
for span in spans: for span in spans:
text = span["text"] text = span["text"]
columns = span["columns"] columns = span["columns"]
if not text or (text.isspace() and not span["has_bg"]): if not text:
# Skip empty spans without background, but advance position # Skip truly empty spans
x += columns * char_width x += columns * char_width
continue continue
# Build tspan attributes # Build tspan attributes
attrs = [f'x="{x:.1f}"'] attrs = [f'x="{x:.1f}"']
# Use textLength to enforce exact character spacing for alignment
span_width = columns * char_width
attrs.append(f'textLength="{span_width:.1f}"')
attrs.append('lengthAdjust="spacingAndGlyphs"')
# Foreground color # Foreground color
if span["fg"] != foreground: if span["fg"] != foreground:
attrs.append(f'fill="{span["fg"]}"') attrs.append(f'fill="{span["fg"]}"')