blob: 579f96f43e3b24a1166195fbf64cade591404b63 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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) = ¤t_span_font {
if old_font != new_font {
let span = old_font.text_span(¤t_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(¤t_span_text)?;
self.spans.push(span);
}
Ok(())
}
pub fn spans(&self) -> impl Iterator<Item=&TextSpan> + DoubleEndedIterator {
self.spans.iter()
}
}
|