summaryrefslogtreecommitdiff
path: root/src/layout/text.rs
blob: 3986690769ce5e2819cfb0c3db09de788c577e61 (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
use std::fmt::{self, Debug, Formatter};

use super::*;
use crate::exec::FontProps;

/// A consecutive, styled run of text.
#[derive(Clone, PartialEq)]
pub struct TextNode {
    /// The text direction.
    pub dir: Dir,
    /// How to align this text node in its parent.
    pub aligns: LayoutAligns,
    /// The text.
    pub text: String,
    /// Properties used for font selection and layout.
    pub props: FontProps,
}

impl Layout for TextNode {
    fn layout(&self, ctx: &mut LayoutContext, _: &Areas) -> Fragment {
        let frame = shape(&self.text, &mut ctx.env.fonts, &self.props);
        Fragment::Frame(frame, self.aligns)
    }
}

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

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