gemini_engine/ascii/
text.rs1use crate::core::{CanDraw, ColChar, Modifier, Vec2D};
2
3use super::TextAlign;
4
5#[non_exhaustive]
7#[derive(Debug, Clone)]
8pub struct Text {
9 pub pos: Vec2D,
11 pub content: String,
13 pub align: TextAlign,
15 pub modifier: Modifier,
17}
18
19impl Text {
20 #[must_use]
25 pub fn new(pos: Vec2D, content: &str, modifier: Modifier) -> Self {
26 assert!(
27 !content.contains('\n'),
28 "Text was created with a content string containing a \n character"
29 );
30
31 Self {
32 pos,
33 content: String::from(content),
34 align: TextAlign::Begin,
35 modifier,
36 }
37 }
38
39 #[must_use]
41 pub const fn with_align(mut self, align: TextAlign) -> Self {
42 self.align = align;
43 self
44 }
45}
46
47impl CanDraw for Text {
48 fn draw_to(&self, canvas: &mut impl crate::core::Canvas) {
49 let mut pos = self.pos;
50 pos.x = self.align.apply_to(pos.x, self.content.len() as i64);
51
52 for (x, text_char) in (0..).zip(self.content.chars()) {
53 if text_char != ' ' {
54 canvas.plot(
55 pos + Vec2D::new(x, 0),
56 ColChar::new(text_char, self.modifier),
57 );
58 }
59 }
60 }
61}