summaryrefslogtreecommitdiff
path: root/src/layout/text.rs
blob: fafc1b14e723ed43a218736bae6387975492d0c3 (plain) (blame)
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
use std::fmt::{self, Debug, Formatter};
use std::rc::Rc;

use fontdock::{FallbackTree, FontVariant};

use super::*;
use crate::shaping;

/// A text node.
#[derive(Clone, PartialEq)]
pub struct Text {
    pub text: String,
    pub size: Length,
    pub dir: Dir,
    pub fallback: Rc<FallbackTree>,
    pub variant: FontVariant,
    pub aligns: Gen<Align>,
}

#[async_trait(?Send)]
impl Layout for Text {
    async fn layout(
        &self,
        ctx: &mut LayoutContext,
        _constraints: LayoutConstraints,
    ) -> Vec<LayoutItem> {
        let mut loader = ctx.loader.borrow_mut();
        let boxed = shaping::shape(
            &self.text,
            self.size,
            self.dir,
            &mut loader,
            &self.fallback,
            self.variant,
        )
        .await;
        vec![LayoutItem::Box(boxed, self.aligns)]
    }
}

impl Debug for Text {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "Text({})", self.text)
    }
}

impl From<Text> for LayoutNode {
    fn from(text: Text) -> Self {
        Self::Text(text)
    }
}