ori_core/views/
text.rs

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::fmt::{self, Write};

use ori_macro::{example, Build};
use smol_str::SmolStr;

use crate::{
    canvas::Color,
    context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
    event::Event,
    layout::{Size, Space},
    rebuild::Rebuild,
    style::{Stylable, Style, StyleBuilder, Theme},
    text::{
        FontAttributes, FontFamily, FontStretch, FontStyle, FontWeight, Paragraph, TextAlign,
        TextWrap,
    },
    view::View,
};

use smol_str;

pub use crate::format_text as text;

/// Create a formatted [`Text`].
///
/// This macro is slightly more efficient than using [`format!`] and [`Text::new`].
#[macro_export]
macro_rules! format_text {
    ($($tt:tt)*) => {
        $crate::views::Text::from(::std::format_args!($($tt)*))
    };
}

/// Create a new [`Text`].
pub fn text(text: impl Into<SmolStr>) -> Text {
    Text::new(text)
}

/// The style of a [`Text`].
#[derive(Clone, Rebuild)]
pub struct TextStyle {
    /// The font size of the text.
    #[rebuild(layout)]
    pub font_size: f32,

    /// The font family of the text.
    #[rebuild(layout)]
    pub font_family: FontFamily,

    /// The font weight of the text.
    #[rebuild(layout)]
    pub font_weight: FontWeight,

    /// The font stretch of the text.
    #[rebuild(layout)]
    pub font_stretch: FontStretch,

    /// The font style of the text.
    #[rebuild(layout)]
    pub font_style: FontStyle,

    /// The color of the text.
    #[rebuild(draw)]
    pub color: Color,

    /// The horizontal alignment of the text.
    #[rebuild(layout)]
    pub align: TextAlign,

    /// The line height of the text.
    #[rebuild(layout)]
    pub line_height: f32,

    /// The text wrap of the text.
    #[rebuild(layout)]
    pub wrap: TextWrap,
}

impl Style for TextStyle {
    fn default_style() -> StyleBuilder<Self> {
        StyleBuilder::new(|theme: &Theme| Self {
            font_size: 16.0,
            font_family: FontFamily::default(),
            font_weight: FontWeight::NORMAL,
            font_stretch: FontStretch::Normal,
            font_style: FontStyle::Normal,
            color: theme.contrast,
            align: TextAlign::Left,
            line_height: 1.2,
            wrap: TextWrap::None,
        })
    }
}

/// A view that displays text.
///
/// Can be styled using the [`TextStyle`].
#[example(name = "text", width = 400, height = 300)]
#[derive(Build, Rebuild)]
pub struct Text {
    /// The text.
    #[rebuild(layout)]
    pub text: SmolStr,

    /// The font size of the text.
    pub font_size: Option<f32>,

    /// The font family of the text.
    pub font_family: Option<FontFamily>,

    /// The font weight of the text.
    pub font_weight: Option<FontWeight>,

    /// The font stretch of the text.
    pub font_stretch: Option<FontStretch>,

    /// The font.into of the text.
    pub font_style: Option<FontStyle>,

    /// The color of the text.
    pub color: Option<Color>,

    /// The horizontal alignment of the text.
    pub align: Option<TextAlign>,

    /// The line height of the text.
    pub line_height: Option<f32>,

    /// The text wrap of the text.
    pub wrap: Option<TextWrap>,
}

impl Text {
    /// Create a new text.
    pub fn new(text: impl Into<SmolStr>) -> Self {
        Self {
            text: text.into(),
            font_size: None,
            font_family: None,
            font_weight: None,
            font_stretch: None,
            font_style: None,
            color: None,
            align: None,
            line_height: None,
            wrap: None,
        }
    }

    fn font_attributes(&self, style: &TextStyle) -> FontAttributes {
        FontAttributes {
            size: style.font_size,
            family: style.font_family.clone(),
            stretch: style.font_stretch,
            weight: style.font_weight,
            style: style.font_style,
            ligatures: true,
            color: style.color,
        }
    }
}

impl Stylable for Text {
    type Style = TextStyle;

    fn style(&self, style: &Self::Style) -> Self::Style {
        Self::Style {
            font_size: self.font_size.unwrap_or(style.font_size),
            font_family: (self.font_family.clone()).unwrap_or_else(|| style.font_family.clone()),
            font_weight: self.font_weight.unwrap_or(style.font_weight),
            font_stretch: self.font_stretch.unwrap_or(style.font_stretch),
            font_style: self.font_style.unwrap_or(style.font_style),
            color: self.color.unwrap_or(style.color),
            align: self.align.unwrap_or(style.align),
            line_height: self.line_height.unwrap_or(style.line_height),
            wrap: self.wrap.unwrap_or(style.wrap),
        }
    }
}

impl<T> View<T> for Text {
    type State = (TextStyle, Paragraph);

    fn build(&mut self, cx: &mut BuildCx, _data: &mut T) -> Self::State {
        let style = self.style(cx.style());

        let mut paragraph = Paragraph::new(style.line_height, style.align, style.wrap);
        paragraph.push_text(&self.text, self.font_attributes(&style));
        (style, paragraph)
    }

    fn rebuild(
        &mut self,
        (style, paragraph): &mut Self::State,
        cx: &mut RebuildCx,
        _data: &mut T,
        old: &Self,
    ) {
        Rebuild::rebuild(self, cx, old);
        self.rebuild_style(cx, style);

        paragraph.line_height = style.line_height;
        paragraph.align = style.align;
        paragraph.wrap = style.wrap;

        paragraph.set_text(&self.text, self.font_attributes(style));
    }

    fn event(
        &mut self,
        _state: &mut Self::State,
        _cx: &mut EventCx,
        _data: &mut T,
        _event: &Event,
    ) -> bool {
        false
    }

    fn layout(
        &mut self,
        (_, paragraph): &mut Self::State,
        cx: &mut LayoutCx,
        _data: &mut T,
        space: Space,
    ) -> Size {
        cx.fonts().measure(paragraph, space.max.width)
    }

    fn draw(&mut self, (_, paragraph): &mut Self::State, cx: &mut DrawCx, _data: &mut T) {
        cx.paragraph(paragraph, cx.rect());
    }
}

impl From<fmt::Arguments<'_>> for Text {
    fn from(args: fmt::Arguments<'_>) -> Text {
        let mut w = smol_str::SmolStrBuilder::new();
        let _ = w.write_fmt(args);
        Text::new(w)
    }
}