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:
@@ -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"]}"')
|
||||||
|
|||||||
Reference in New Issue
Block a user