From 0806f9803e2dcded46193161e2ddca88b54beb7c Mon Sep 17 00:00:00 2001 From: Sofia Date: Sat, 13 Sep 2025 15:06:02 +0300 Subject: [PATCH] Format overflowing words to go to next line --- src/font.rs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/font.rs b/src/font.rs index f4c44d5..1ceaf6b 100644 --- a/src/font.rs +++ b/src/font.rs @@ -137,26 +137,32 @@ pub fn draw_text( let kerning = 9 * scale; let original_x = position.x; - for c in text.chars() { - if c == '\n' { - curr_pos.y += kerning; - continue; - } - if c == ' ' { - curr_pos.x += kerning; - continue; - } - if curr_pos.x + kerning > 240 { + for word in text.split(' ') { + let word_length = word.len() as u16 * kerning; + if curr_pos.x + word_length > 240 { curr_pos.x = original_x; curr_pos.y += kerning; } - if curr_pos.y + kerning > 240 { - curr_pos.y = 0; + + for c in word.chars() { + if c == '\n' { + curr_pos.y += kerning; + continue; + } + if curr_pos.x + kerning > 240 { + curr_pos.x = original_x; + curr_pos.y += kerning; + } + if curr_pos.y + kerning > 240 { + curr_pos.y = 0; + } + + Letter::from(c, fg, bg) + .iter() + .draw(display, curr_pos, scale); + curr_pos.x += kerning; } - Letter::from(c, fg, bg) - .iter() - .draw(display, curr_pos, scale); curr_pos.x += kerning; } }