Add text alignment

This commit is contained in:
Sofia 2026-05-16 19:45:12 +03:00
parent 5438f09820
commit e06ce690cb
2 changed files with 16 additions and 4 deletions

View File

@ -64,7 +64,19 @@ impl<'a> FontRenderer<'a> {
v_align: VerticalAlignment, v_align: VerticalAlignment,
) { ) {
let data = self.prepare(text.into()); 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> { pub fn prepare(&self, text: String) -> RawRenderData<'a> {

View File

@ -114,9 +114,9 @@ fn main() -> ! {
font_renderer.render( font_renderer.render(
&mut display, &mut display,
"Hello World!", "Hello World!",
Position::new(0, 0), Position::new(120, 120),
HorizontalAlignment::LeftToRight, HorizontalAlignment::Center,
VerticalAlignment::TopToBottom, VerticalAlignment::Center,
); );
let sim_rst = Output::new(peripherals.GPIO15, Level::High, OutputConfig::default()); let sim_rst = Output::new(peripherals.GPIO15, Level::High, OutputConfig::default());