about summary refs log tree commit diff
path: root/src/x/text_line.rs
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-22 03:31:51 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-22 21:40:21 -0700
commit33a0aea1fa5b12c817cc299b10f8a359d670c645 (patch)
tree1f29b867d49a6158edc679c3cc1521a77f1175d7 /src/x/text_line.rs
parentefa17e7eb566d81ce821bde459e900d7398a9977 (diff)
there are now concepts of font lists and text lines
a "line" is a list of spans

much of the layout logic is moved into TextLine, but not yet all of it

Change-Id: I2edba34cd9be5a50ac6dc646cd6e2ab2369fca13
Force-Push: yes
Diffstat (limited to 'src/x/text_line.rs')
-rw-r--r--src/x/text_line.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/x/text_line.rs b/src/x/text_line.rs
new file mode 100644
index 0000000..579f96f
--- /dev/null
+++ b/src/x/text_line.rs
@@ -0,0 +1,57 @@
+use crate::error::Result;
+use crate::x::{ Font, FontList, TextSpan };
+
+use std::sync::{ Arc, Mutex };
+
+
+pub struct TextLine {
+  fonts: Arc<Mutex<FontList>>,
+  spans: Vec<TextSpan>,
+}
+
+
+impl TextLine {
+  pub fn new(fonts: Arc<Mutex<FontList>>) -> Result<TextLine> {
+    let spans = Vec::new();
+
+    Ok(TextLine { fonts, spans })
+  }
+
+  pub fn append_text(&mut self, text: &str) -> Result<()> {
+    let mut current_span_font: Option<Arc<Font>> = None;
+    let mut current_span_text = String::new();
+
+    for char in text.chars() {
+      let codepoint = char as u32;
+
+      let new_font = &self.fonts.lock()?.find_codepoint(codepoint)?;
+      if let Some(old_font) = &current_span_font {
+        if old_font != new_font {
+          let span = old_font.text_span(&current_span_text)?;
+          self.spans.push(span);
+
+          current_span_font = Some(Arc::clone(new_font));
+          current_span_text = String::new();
+        }
+      } else {
+        current_span_font = Some(Arc::clone(new_font));
+      }
+
+      current_span_text.push(char);
+    }
+
+    if let Some(font) = current_span_font
+       && !current_span_text.is_empty()
+    {
+      let span = font.text_span(&current_span_text)?;
+      self.spans.push(span);
+    }
+
+    Ok(())
+  }
+
+  pub fn spans(&self) -> impl Iterator<Item=&TextSpan> + DoubleEndedIterator {
+    self.spans.iter()
+  }
+}
+