use rten_imageproc::{BoundingRect, Rect, RotatedRect};
#[derive(Clone, Debug, PartialEq)]
pub struct TextChar {
pub char: char,
pub rect: Rect,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct TextWord {
pub chars: Vec<TextChar>,
}
impl TextWord {
pub fn text(&self) -> String {
self.chars.iter().map(|c| c.char).collect()
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct TextLine {
pub words: Vec<TextWord>,
}
impl TextLine {
pub fn new(chars: Vec<TextChar>) -> Self {
Self {
words: vec![TextWord { chars }],
}
}
pub fn text(&self) -> String {
self.words
.iter()
.map(TextWord::text)
.collect::<Vec<_>>()
.join(" ")
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum TextItem {
Line(TextLine),
Word(TextWord),
Char(TextChar),
}
impl TextItem {
pub fn as_line(&self) -> Option<&TextLine> {
match self {
Self::Line(l) => Some(l),
_ => None,
}
}
}
impl std::fmt::Display for TextLine {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.text())
}
}
pub fn line_bounding_rect(words: &[RotatedRect]) -> Option<Rect> {
words.iter().fold(None, |br: Option<Rect>, r| match br {
Some(br) => Some(br.union(r.bounding_rect().integral_bounding_rect())),
None => Some(r.bounding_rect().integral_bounding_rect()),
})
}