summaryrefslogtreecommitdiff
path: root/src/layout/text.rs
blob: 7faefa0d55124440729c4d09b992de70ce02691a (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
52
53
54
55
56
57
use std::fmt::{self, Debug, Formatter};
use std::rc::Rc;

use fontdock::{FallbackTree, FontVariant};

use super::*;

/// A consecutive, styled run of text.
#[derive(Clone, PartialEq)]
pub struct TextNode {
    /// The text.
    pub text: String,
    /// The text direction.
    pub dir: Dir,
    /// How to align this text node in its parent.
    pub aligns: LayoutAligns,
    /// The families used for font fallback.
    pub families: Rc<FallbackTree>,
    /// The font variant,
    pub variant: FontVariant,
    /// The font size.
    pub font_size: Length,
    /// The top end of the text bounding box.
    pub top_edge: VerticalFontMetric,
    /// The bottom end of the text bounding box.
    pub bottom_edge: VerticalFontMetric,
}

impl Layout for TextNode {
    fn layout(&self, ctx: &mut LayoutContext, _: &Areas) -> Fragment {
        Fragment::Frame(
            shape(
                &self.text,
                self.dir,
                &self.families,
                self.variant,
                self.font_size,
                self.top_edge,
                self.bottom_edge,
                &mut ctx.env.fonts,
            ),
            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)
    }
}