From 0f140ebc681b7411dd76e110b2fe304f3e583c6c Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Mon, 29 Sep 2025 12:03:12 +0800 Subject: image(text): line wrap --- src/image.rs | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'src/image.rs') diff --git a/src/image.rs b/src/image.rs index 2aea600..76e86ce 100644 --- a/src/image.rs +++ b/src/image.rs @@ -15,6 +15,7 @@ type Offset = (u32, u32); // x, y or width, height const COMPONENT_TEXT_PADDING: u32 = 1 * RESOLUTION_MULTIPLIER; const TEXT_SCALE: f32 = 12.0 * RESOLUTION_MULTIPLIER as f32; +const TEXT_LEN_WRAP: usize = 10; // TODO: LENGTH based on where to where const DIRECTION_LINE_THICKNESS: u32 = 2 * RESOLUTION_MULTIPLIER; @@ -56,6 +57,17 @@ mod colors { ]); } +fn wrap_str(s: &str) -> Vec { + s.chars() + .collect::>() + .chunks(TEXT_LEN_WRAP) + .map(|c| { + c.iter() + .collect() + }) + .collect() +} + // TODO: store as singleton fn get_font() -> Result { let mut db = Database::new(); @@ -106,27 +118,32 @@ fn ext(img: &mut DynamicImage, (curw, curh): &mut Offset, (extw, exth): &mut Off // TODO: make draw_rect func fn draw_process(img: &mut DynamicImage, txt: &str, (curw, curh): &mut Offset) -> Offset { *curw = curw - .checked_sub(TEXT_SCALE as u32 / 2) + .checked_sub(TEXT_SCALE as u32 * COMPONENT_TEXT_PADDING / 2) .unwrap_or(0); - let w = TEXT_SCALE as u32 + COMPONENT_TEXT_PADDING * 2; - let h = w; + let txt = wrap_str(txt); + let w = (TEXT_SCALE as u32 / 5 * TEXT_LEN_WRAP as u32) + (2 * COMPONENT_TEXT_PADDING); + let h = TEXT_SCALE as u32 / 2 * txt.len() as u32 + (2 * COMPONENT_TEXT_PADDING); ext(img, &mut (*curw, *curh), &mut (w, h)); draw_filled_rect_mut(img, Rect::at(*curw as i32, *curh as i32).of_size(w, h), colors::PROCESS); - draw_text_mut( - img, - colors::FG, - (*curw + COMPONENT_TEXT_PADDING) - .try_into() - .unwrap(), - (*curh + COMPONENT_TEXT_PADDING) - .try_into() - .unwrap(), - PxScale::from(TEXT_SCALE / 2.0), - &get_font().unwrap(), - txt, - ); + // TODO: move to draw_text() + for s in txt { + draw_text_mut( + img, + colors::FG, + (*curw + COMPONENT_TEXT_PADDING) + .try_into() + .unwrap(), + (*curh + COMPONENT_TEXT_PADDING) + .try_into() + .unwrap(), + PxScale::from(TEXT_SCALE / 2_f32), + &get_font().unwrap(), + s.as_str(), + ); + *curh += TEXT_SCALE as u32 / 2; + } *curw += w / 2; - *curh += h; + *curh += 2 * COMPONENT_TEXT_PADDING; (*curw, *curh) } -- cgit v1.2.3