From e06ce690cb84b3247a1838bf74f5bb6d51ffd5fc Mon Sep 17 00:00:00 2001 From: Sofia Date: Sat, 16 May 2026 19:45:12 +0300 Subject: [PATCH] Add text alignment --- src/font.rs | 14 +++++++++++++- src/main.rs | 6 +++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/font.rs b/src/font.rs index 60ef25b..3958a8a 100644 --- a/src/font.rs +++ b/src/font.rs @@ -64,7 +64,19 @@ impl<'a> FontRenderer<'a> { v_align: VerticalAlignment, ) { let data = self.prepare(text.into()); - self.raw_render(display, data, position); + + let x = match h_align { + HorizontalAlignment::RightToLeft => position.x - data.pixel_width as i16, + HorizontalAlignment::Center => position.x - (data.pixel_width as i16 / 2), + HorizontalAlignment::LeftToRight => position.x, + }; + let y = match v_align { + VerticalAlignment::BottomToTop => position.y - self.height as i16, + VerticalAlignment::Center => position.y - (self.height as i16 / 2), + VerticalAlignment::TopToBottom => position.y, + }; + + self.raw_render(display, data, Position { x, y }); } pub fn prepare(&self, text: String) -> RawRenderData<'a> { diff --git a/src/main.rs b/src/main.rs index 6f47b23..2dda463 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,9 +114,9 @@ fn main() -> ! { font_renderer.render( &mut display, "Hello World!", - Position::new(0, 0), - HorizontalAlignment::LeftToRight, - VerticalAlignment::TopToBottom, + Position::new(120, 120), + HorizontalAlignment::Center, + VerticalAlignment::Center, ); let sim_rst = Output::new(peripherals.GPIO15, Level::High, OutputConfig::default());