Skip to content

Add alignment options for text drawing - #359

Closed
1kauratus wants to merge 2 commits into
17cupsofcoffee:mainfrom
1kauratus:feat/issue-343-text-alignment
Closed

1kauratus wants to merge 2 commits into
17cupsofcoffee:mainfrom
1kauratus:feat/issue-343-text-alignment

Conversation

@1kauratus

Copy link
Copy Markdown

Summary

Adds horizontal and vertical alignment for graphics::text::Text, which is the feature requested in #343.

  • Text::set_alignment / Text::alignment store an alignment that Text::draw applies on every call.
  • Text::draw_aligned takes the alignment as a parameter for a single call, which is the extra control the issue asks for. It does not change the stored alignment.
  • The default is HorizontalAlignment::Left and VerticalAlignment::Top. That offset is zero, so existing draw calls stay where they are.
  • Center, Right, Middle and Bottom place the corresponding edge or centre of the glyph bounds on the draw position. The shift happens in local space, before scaling and rotation. The bounds are the same rectangle returned by Text::get_bounds.
  • Empty text has no bounds and is not moved.

Closes #343.

Tests

New tests in src/graphics/text.rs:

  • default_alignment_is_left_top
  • left_top_preserves_existing_layout_origin
  • center_places_horizontal_centre_on_the_origin
  • right_places_right_edge_on_the_origin
  • middle_places_vertical_centre_on_the_origin
  • bottom_places_bottom_edge_on_the_origin
  • center_and_middle_are_applied_together
  • empty_text_is_not_moved
  • quad_translation_preserves_glyph_size

TextAlignment also has a doc test.

Commands run locally on macOS (SDL 3.4.18 from Homebrew, Rust 1.99.1):

  • cargo test — 44 unit tests passed, 7 doc tests passed
  • cargo build --example hello_world --example text — finished successfully
  • cargo clippy --lib --tests --example text --example hello_world -- -D warnings — clean

The text example draws one centered line with draw_aligned so the new parameter is visible.

Made with Cursor

tushantkaura and others added 2 commits October 22, 2026 14:34
The default stays left and top, which keeps existing draw calls in place.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@17cupsofcoffee

Copy link
Copy Markdown
Owner

Sorry, I will not accept AI-generated PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add alignment option for text

3 participants