Skip to content

feat: Add text_align option to Style #9

@davefowler

Description

@davefowler

Summary

Add a text_align option to the Style class to control horizontal text alignment.

Use Case

When embedding mdsvg output in larger SVG compositions (like dashboards), callers often need to align text. Currently this requires hacky string manipulation:

# Current workaround in dataface
aligned_title = title_svg.replace("<text", f'<text text-anchor="{title_anchor}"')

Proposed API

from mdsvg import Style, render

# Option 1: Style parameter
style = Style(text_align="center")  # "left", "center", "right"
svg = render("# Title", style=style)

# Option 2: Render parameter
svg = render("# Title", text_align="center")

Implementation Notes

  • Map text_align to SVG text-anchor attribute:
    • "left"text-anchor="start"
    • "center"text-anchor="middle"
    • "right"text-anchor="end"
  • Apply to all <text> elements in the output
  • Default to "left" (current behavior)

@cursor Instructions

  1. Add text_align: str = "left" to the Style class
  2. In the SVGRenderer, apply text-anchor attribute based on text_align value
  3. Add tests for left/center/right alignment
  4. Update README with example

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions