egui/
widget_text.rs

1use emath::GuiRounding as _;
2use epaint::text::TextFormat;
3use std::fmt::Formatter;
4use std::{borrow::Cow, sync::Arc};
5
6use crate::{
7    Align, Color32, FontFamily, FontSelection, Galley, Style, TextStyle, TextWrapMode, Ui, Visuals,
8    text::{LayoutJob, TextWrapping},
9};
10
11/// Text and optional style choices for it.
12///
13/// The style choices (font, color) are applied to the entire text.
14/// For more detailed control, use [`crate::text::LayoutJob`] instead.
15///
16/// A [`RichText`] can be used in most widgets and helper functions, e.g. [`Ui::label`] and [`Ui::button`].
17///
18/// ### Example
19/// ```
20/// use egui::{RichText, Color32};
21///
22/// RichText::new("Plain");
23/// RichText::new("colored").color(Color32::RED);
24/// RichText::new("Large and underlined").size(20.0).underline();
25/// ```
26#[derive(Clone, Debug, PartialEq)]
27pub struct RichText {
28    text: String,
29    size: Option<f32>,
30    extra_letter_spacing: f32,
31    line_height: Option<f32>,
32    family: Option<FontFamily>,
33    text_style: Option<TextStyle>,
34    background_color: Color32,
35    expand_bg: f32,
36    text_color: Option<Color32>,
37    code: bool,
38    strong: bool,
39    weak: bool,
40    strikethrough: bool,
41    underline: bool,
42    italics: bool,
43    raised: bool,
44}
45
46impl Default for RichText {
47    fn default() -> Self {
48        Self {
49            text: Default::default(),
50            size: Default::default(),
51            extra_letter_spacing: Default::default(),
52            line_height: Default::default(),
53            family: Default::default(),
54            text_style: Default::default(),
55            background_color: Default::default(),
56            expand_bg: 1.0,
57            text_color: Default::default(),
58            code: Default::default(),
59            strong: Default::default(),
60            weak: Default::default(),
61            strikethrough: Default::default(),
62            underline: Default::default(),
63            italics: Default::default(),
64            raised: Default::default(),
65        }
66    }
67}
68
69impl From<&str> for RichText {
70    #[inline]
71    fn from(text: &str) -> Self {
72        Self::new(text)
73    }
74}
75
76impl From<&String> for RichText {
77    #[inline]
78    fn from(text: &String) -> Self {
79        Self::new(text)
80    }
81}
82
83impl From<&mut String> for RichText {
84    #[inline]
85    fn from(text: &mut String) -> Self {
86        Self::new(text.clone())
87    }
88}
89
90impl From<String> for RichText {
91    #[inline]
92    fn from(text: String) -> Self {
93        Self::new(text)
94    }
95}
96
97impl From<&Box<str>> for RichText {
98    #[inline]
99    fn from(text: &Box<str>) -> Self {
100        Self::new(text.clone())
101    }
102}
103
104impl From<&mut Box<str>> for RichText {
105    #[inline]
106    fn from(text: &mut Box<str>) -> Self {
107        Self::new(text.clone())
108    }
109}
110
111impl From<Box<str>> for RichText {
112    #[inline]
113    fn from(text: Box<str>) -> Self {
114        Self::new(text)
115    }
116}
117
118impl From<Cow<'_, str>> for RichText {
119    #[inline]
120    fn from(text: Cow<'_, str>) -> Self {
121        Self::new(text)
122    }
123}
124
125impl RichText {
126    #[inline]
127    pub fn new(text: impl Into<String>) -> Self {
128        Self {
129            text: text.into(),
130            ..Default::default()
131        }
132    }
133
134    #[inline]
135    pub fn is_empty(&self) -> bool {
136        self.text.is_empty()
137    }
138
139    #[inline]
140    pub fn text(&self) -> &str {
141        &self.text
142    }
143
144    /// Select the font size (in points).
145    /// This overrides the value from [`Self::text_style`].
146    #[inline]
147    pub fn size(mut self, size: f32) -> Self {
148        self.size = Some(size);
149        self
150    }
151
152    /// Extra spacing between letters, in points.
153    ///
154    /// Default: 0.0.
155    ///
156    /// For even text it is recommended you round this to an even number of _pixels_,
157    /// e.g. using [`crate::Painter::round_to_pixel`].
158    #[inline]
159    pub fn extra_letter_spacing(mut self, extra_letter_spacing: f32) -> Self {
160        self.extra_letter_spacing = extra_letter_spacing;
161        self
162    }
163
164    /// Explicit line height of the text in points.
165    ///
166    /// This is the distance between the bottom row of two subsequent lines of text.
167    ///
168    /// If `None` (the default), the line height is determined by the font.
169    ///
170    /// For even text it is recommended you round this to an even number of _pixels_,
171    /// e.g. using [`crate::Painter::round_to_pixel`].
172    #[inline]
173    pub fn line_height(mut self, line_height: Option<f32>) -> Self {
174        self.line_height = line_height;
175        self
176    }
177
178    /// Select the font family.
179    ///
180    /// This overrides the value from [`Self::text_style`].
181    ///
182    /// Only the families available in [`crate::FontDefinitions::families`] may be used.
183    #[inline]
184    pub fn family(mut self, family: FontFamily) -> Self {
185        self.family = Some(family);
186        self
187    }
188
189    /// Select the font and size.
190    /// This overrides the value from [`Self::text_style`].
191    #[inline]
192    pub fn font(mut self, font_id: crate::FontId) -> Self {
193        let crate::FontId { size, family } = font_id;
194        self.size = Some(size);
195        self.family = Some(family);
196        self
197    }
198
199    /// Override the [`TextStyle`].
200    #[inline]
201    pub fn text_style(mut self, text_style: TextStyle) -> Self {
202        self.text_style = Some(text_style);
203        self
204    }
205
206    /// Set the [`TextStyle`] unless it has already been set
207    #[inline]
208    pub fn fallback_text_style(mut self, text_style: TextStyle) -> Self {
209        self.text_style.get_or_insert(text_style);
210        self
211    }
212
213    /// Use [`TextStyle::Heading`].
214    #[inline]
215    pub fn heading(self) -> Self {
216        self.text_style(TextStyle::Heading)
217    }
218
219    /// Use [`TextStyle::Monospace`].
220    #[inline]
221    pub fn monospace(self) -> Self {
222        self.text_style(TextStyle::Monospace)
223    }
224
225    /// Monospace label with different background color.
226    #[inline]
227    pub fn code(mut self) -> Self {
228        self.code = true;
229        self.text_style(TextStyle::Monospace)
230    }
231
232    /// Extra strong text (stronger color).
233    #[inline]
234    pub fn strong(mut self) -> Self {
235        self.strong = true;
236        self
237    }
238
239    /// Extra weak text (fainter color).
240    #[inline]
241    pub fn weak(mut self) -> Self {
242        self.weak = true;
243        self
244    }
245
246    /// Draw a line under the text.
247    ///
248    /// If you want to control the line color, use [`LayoutJob`] instead.
249    #[inline]
250    pub fn underline(mut self) -> Self {
251        self.underline = true;
252        self
253    }
254
255    /// Draw a line through the text, crossing it out.
256    ///
257    /// If you want to control the strikethrough line color, use [`LayoutJob`] instead.
258    #[inline]
259    pub fn strikethrough(mut self) -> Self {
260        self.strikethrough = true;
261        self
262    }
263
264    /// Tilt the characters to the right.
265    #[inline]
266    pub fn italics(mut self) -> Self {
267        self.italics = true;
268        self
269    }
270
271    /// Smaller text.
272    #[inline]
273    pub fn small(self) -> Self {
274        self.text_style(TextStyle::Small)
275    }
276
277    /// For e.g. exponents.
278    #[inline]
279    pub fn small_raised(self) -> Self {
280        self.text_style(TextStyle::Small).raised()
281    }
282
283    /// Align text to top. Only applicable together with [`Self::small()`].
284    #[inline]
285    pub fn raised(mut self) -> Self {
286        self.raised = true;
287        self
288    }
289
290    /// Fill-color behind the text.
291    #[inline]
292    pub fn background_color(mut self, background_color: impl Into<Color32>) -> Self {
293        self.background_color = background_color.into();
294        self
295    }
296
297    /// Override text color.
298    ///
299    /// If not set, [`Color32::PLACEHOLDER`] will be used,
300    /// which will be replaced with a color chosen by the widget that paints the text.
301    #[inline]
302    pub fn color(mut self, color: impl Into<Color32>) -> Self {
303        self.text_color = Some(color.into());
304        self
305    }
306
307    /// Read the font height of the selected text style.
308    ///
309    /// Returns a value rounded to [`emath::GUI_ROUNDING`].
310    pub fn font_height(&self, fonts: &epaint::Fonts, style: &Style) -> f32 {
311        let mut font_id = self.text_style.as_ref().map_or_else(
312            || FontSelection::Default.resolve(style),
313            |text_style| text_style.resolve(style),
314        );
315
316        if let Some(size) = self.size {
317            font_id.size = size;
318        }
319        if let Some(family) = &self.family {
320            font_id.family = family.clone();
321        }
322        fonts.row_height(&font_id)
323    }
324
325    /// Append to an existing [`LayoutJob`]
326    ///
327    /// Note that the color of the [`RichText`] must be set, or may default to an undesirable color.
328    ///
329    /// ### Example
330    /// ```
331    /// use egui::{Style, RichText, text::LayoutJob, Color32, FontSelection, Align};
332    ///
333    /// let style = Style::default();
334    /// let mut layout_job = LayoutJob::default();
335    /// RichText::new("Normal")
336    ///     .color(style.visuals.text_color())
337    ///     .append_to(
338    ///         &mut layout_job,
339    ///         &style,
340    ///         FontSelection::Default,
341    ///         Align::Center,
342    ///     );
343    /// RichText::new("Large and underlined")
344    ///     .color(style.visuals.text_color())
345    ///     .size(20.0)
346    ///     .underline()
347    ///     .append_to(
348    ///         &mut layout_job,
349    ///         &style,
350    ///         FontSelection::Default,
351    ///         Align::Center,
352    ///     );
353    /// ```
354    pub fn append_to(
355        self,
356        layout_job: &mut LayoutJob,
357        style: &Style,
358        fallback_font: FontSelection,
359        default_valign: Align,
360    ) {
361        let (text, format) = self.into_text_and_format(style, fallback_font, default_valign);
362
363        layout_job.append(&text, 0.0, format);
364    }
365
366    fn into_layout_job(
367        self,
368        style: &Style,
369        fallback_font: FontSelection,
370        default_valign: Align,
371    ) -> LayoutJob {
372        let (text, text_format) = self.into_text_and_format(style, fallback_font, default_valign);
373        LayoutJob::single_section(text, text_format)
374    }
375
376    fn into_text_and_format(
377        self,
378        style: &Style,
379        fallback_font: FontSelection,
380        default_valign: Align,
381    ) -> (String, crate::text::TextFormat) {
382        let text_color = self.get_text_color(&style.visuals);
383
384        let Self {
385            text,
386            size,
387            extra_letter_spacing,
388            line_height,
389            family,
390            text_style,
391            background_color,
392            expand_bg,
393            text_color: _, // already used by `get_text_color`
394            code,
395            strong: _, // already used by `get_text_color`
396            weak: _,   // already used by `get_text_color`
397            strikethrough,
398            underline,
399            italics,
400            raised,
401        } = self;
402
403        let line_color = text_color.unwrap_or_else(|| style.visuals.text_color());
404        let text_color = text_color.unwrap_or(crate::Color32::PLACEHOLDER);
405
406        let font_id = {
407            let mut font_id = text_style
408                .or_else(|| style.override_text_style.clone())
409                .map_or_else(
410                    || fallback_font.resolve(style),
411                    |text_style| text_style.resolve(style),
412                );
413            if let Some(fid) = style.override_font_id.clone() {
414                font_id = fid;
415            }
416            if let Some(size) = size {
417                font_id.size = size;
418            }
419            if let Some(family) = family {
420                font_id.family = family;
421            }
422            font_id
423        };
424
425        let mut background_color = background_color;
426        if code {
427            background_color = style.visuals.code_bg_color;
428        }
429        let underline = if underline {
430            crate::Stroke::new(1.0, line_color)
431        } else {
432            crate::Stroke::NONE
433        };
434        let strikethrough = if strikethrough {
435            crate::Stroke::new(1.0, line_color)
436        } else {
437            crate::Stroke::NONE
438        };
439
440        let valign = if raised {
441            crate::Align::TOP
442        } else {
443            default_valign
444        };
445
446        (
447            text,
448            crate::text::TextFormat {
449                font_id,
450                extra_letter_spacing,
451                line_height,
452                color: text_color,
453                background: background_color,
454                italics,
455                underline,
456                strikethrough,
457                valign,
458                expand_bg,
459            },
460        )
461    }
462
463    fn get_text_color(&self, visuals: &Visuals) -> Option<Color32> {
464        if let Some(text_color) = self.text_color {
465            Some(text_color)
466        } else if self.strong {
467            Some(visuals.strong_text_color())
468        } else if self.weak {
469            Some(visuals.weak_text_color())
470        } else {
471            visuals.override_text_color
472        }
473    }
474}
475
476// ----------------------------------------------------------------------------
477
478/// This is how you specify text for a widget.
479///
480/// A lot of widgets use `impl Into<WidgetText>` as an argument,
481/// allowing you to pass in [`String`], [`RichText`], [`LayoutJob`], and more.
482///
483/// Often a [`WidgetText`] is just a simple [`String`],
484/// but it can be a [`RichText`] (text with color, style, etc),
485/// a [`LayoutJob`] (for when you want full control of how the text looks)
486/// or text that has already been laid out in a [`Galley`].
487///
488/// You can color the text however you want, or use [`Color32::PLACEHOLDER`]
489/// which will be replaced with a color chosen by the widget that paints the text.
490#[derive(Clone)]
491pub enum WidgetText {
492    /// Plain unstyled text.
493    ///
494    /// We have this as a special case, as it is the common-case,
495    /// and it uses less memory than [`Self::RichText`].
496    Text(String),
497
498    /// Text and optional style choices for it.
499    ///
500    /// Prefer [`Self::Text`] if there is no styling, as it will be faster.
501    RichText(Arc<RichText>),
502
503    /// Use this [`LayoutJob`] when laying out the text.
504    ///
505    /// Only [`LayoutJob::text`] and [`LayoutJob::sections`] are guaranteed to be respected.
506    ///
507    /// [`TextWrapping::max_width`](epaint::text::TextWrapping::max_width), [`LayoutJob::halign`], [`LayoutJob::justify`]
508    /// and [`LayoutJob::first_row_min_height`] will likely be determined by the [`crate::Layout`]
509    /// of the [`Ui`] the widget is placed in.
510    /// If you want all parts of the [`LayoutJob`] respected, then convert it to a
511    /// [`Galley`] and use [`Self::Galley`] instead.
512    ///
513    /// You can color the text however you want, or use [`Color32::PLACEHOLDER`]
514    /// which will be replaced with a color chosen by the widget that paints the text.
515    LayoutJob(Arc<LayoutJob>),
516
517    /// Use exactly this galley when painting the text.
518    ///
519    /// You can color the text however you want, or use [`Color32::PLACEHOLDER`]
520    /// which will be replaced with a color chosen by the widget that paints the text.
521    Galley(Arc<Galley>),
522}
523
524impl std::fmt::Debug for WidgetText {
525    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
526        let text = self.text();
527        match self {
528            Self::Text(_) => write!(f, "Text({text:?})"),
529            Self::RichText(_) => write!(f, "RichText({text:?})"),
530            Self::LayoutJob(_) => write!(f, "LayoutJob({text:?})"),
531            Self::Galley(_) => write!(f, "Galley({text:?})"),
532        }
533    }
534}
535
536impl Default for WidgetText {
537    fn default() -> Self {
538        Self::Text(String::new())
539    }
540}
541
542impl WidgetText {
543    #[inline]
544    pub fn is_empty(&self) -> bool {
545        match self {
546            Self::Text(text) => text.is_empty(),
547            Self::RichText(text) => text.is_empty(),
548            Self::LayoutJob(job) => job.is_empty(),
549            Self::Galley(galley) => galley.is_empty(),
550        }
551    }
552
553    #[inline]
554    pub fn text(&self) -> &str {
555        match self {
556            Self::Text(text) => text,
557            Self::RichText(text) => text.text(),
558            Self::LayoutJob(job) => &job.text,
559            Self::Galley(galley) => galley.text(),
560        }
561    }
562
563    /// Map the contents based on the provided closure.
564    ///
565    /// - [`Self::Text`] => convert to [`RichText`] and call f
566    /// - [`Self::RichText`] => call f
567    /// - else do nothing
568    #[must_use]
569    fn map_rich_text<F>(self, f: F) -> Self
570    where
571        F: FnOnce(RichText) -> RichText,
572    {
573        match self {
574            Self::Text(text) => Self::RichText(Arc::new(f(RichText::new(text)))),
575            Self::RichText(text) => Self::RichText(Arc::new(f(Arc::unwrap_or_clone(text)))),
576            other => other,
577        }
578    }
579
580    /// Override the [`TextStyle`] if, and only if, this is a [`RichText`].
581    ///
582    /// Prefer using [`RichText`] directly!
583    #[inline]
584    pub fn text_style(self, text_style: TextStyle) -> Self {
585        self.map_rich_text(|text| text.text_style(text_style))
586    }
587
588    /// Set the [`TextStyle`] unless it has already been set
589    ///
590    /// Prefer using [`RichText`] directly!
591    #[inline]
592    pub fn fallback_text_style(self, text_style: TextStyle) -> Self {
593        self.map_rich_text(|text| text.fallback_text_style(text_style))
594    }
595
596    /// Override text color if, and only if, this is a [`RichText`].
597    ///
598    /// Prefer using [`RichText`] directly!
599    #[inline]
600    pub fn color(self, color: impl Into<Color32>) -> Self {
601        self.map_rich_text(|text| text.color(color))
602    }
603
604    /// Prefer using [`RichText`] directly!
605    #[inline]
606    pub fn heading(self) -> Self {
607        self.map_rich_text(|text| text.heading())
608    }
609
610    /// Prefer using [`RichText`] directly!
611    #[inline]
612    pub fn monospace(self) -> Self {
613        self.map_rich_text(|text| text.monospace())
614    }
615
616    /// Prefer using [`RichText`] directly!
617    #[inline]
618    pub fn code(self) -> Self {
619        self.map_rich_text(|text| text.code())
620    }
621
622    /// Prefer using [`RichText`] directly!
623    #[inline]
624    pub fn strong(self) -> Self {
625        self.map_rich_text(|text| text.strong())
626    }
627
628    /// Prefer using [`RichText`] directly!
629    #[inline]
630    pub fn weak(self) -> Self {
631        self.map_rich_text(|text| text.weak())
632    }
633
634    /// Prefer using [`RichText`] directly!
635    #[inline]
636    pub fn underline(self) -> Self {
637        self.map_rich_text(|text| text.underline())
638    }
639
640    /// Prefer using [`RichText`] directly!
641    #[inline]
642    pub fn strikethrough(self) -> Self {
643        self.map_rich_text(|text| text.strikethrough())
644    }
645
646    /// Prefer using [`RichText`] directly!
647    #[inline]
648    pub fn italics(self) -> Self {
649        self.map_rich_text(|text| text.italics())
650    }
651
652    /// Prefer using [`RichText`] directly!
653    #[inline]
654    pub fn small(self) -> Self {
655        self.map_rich_text(|text| text.small())
656    }
657
658    /// Prefer using [`RichText`] directly!
659    #[inline]
660    pub fn small_raised(self) -> Self {
661        self.map_rich_text(|text| text.small_raised())
662    }
663
664    /// Prefer using [`RichText`] directly!
665    #[inline]
666    pub fn raised(self) -> Self {
667        self.map_rich_text(|text| text.raised())
668    }
669
670    /// Prefer using [`RichText`] directly!
671    #[inline]
672    pub fn background_color(self, background_color: impl Into<Color32>) -> Self {
673        self.map_rich_text(|text| text.background_color(background_color))
674    }
675
676    /// Returns a value rounded to [`emath::GUI_ROUNDING`].
677    pub(crate) fn font_height(&self, fonts: &epaint::Fonts, style: &Style) -> f32 {
678        match self {
679            Self::Text(_) => fonts.row_height(&FontSelection::Default.resolve(style)),
680            Self::RichText(text) => text.font_height(fonts, style),
681            Self::LayoutJob(job) => job.font_height(fonts),
682            Self::Galley(galley) => {
683                if let Some(placed_row) = galley.rows.first() {
684                    placed_row.height().round_ui()
685                } else {
686                    galley.size().y.round_ui()
687                }
688            }
689        }
690    }
691
692    pub fn into_layout_job(
693        self,
694        style: &Style,
695        fallback_font: FontSelection,
696        default_valign: Align,
697    ) -> Arc<LayoutJob> {
698        match self {
699            Self::Text(text) => Arc::new(LayoutJob::simple_format(
700                text,
701                TextFormat {
702                    font_id: FontSelection::Default.resolve(style),
703                    color: crate::Color32::PLACEHOLDER,
704                    valign: default_valign,
705                    ..Default::default()
706                },
707            )),
708            Self::RichText(text) => Arc::new(Arc::unwrap_or_clone(text).into_layout_job(
709                style,
710                fallback_font,
711                default_valign,
712            )),
713            Self::LayoutJob(job) => job,
714            Self::Galley(galley) => galley.job.clone(),
715        }
716    }
717
718    /// Layout with wrap mode based on the containing [`Ui`].
719    ///
720    /// `wrap_mode`: override for [`Ui::wrap_mode`]
721    pub fn into_galley(
722        self,
723        ui: &Ui,
724        wrap_mode: Option<TextWrapMode>,
725        available_width: f32,
726        fallback_font: impl Into<FontSelection>,
727    ) -> Arc<Galley> {
728        let valign = ui.text_valign();
729        let style = ui.style();
730
731        let wrap_mode = wrap_mode.unwrap_or_else(|| ui.wrap_mode());
732        let text_wrapping = TextWrapping::from_wrap_mode_and_width(wrap_mode, available_width);
733
734        self.into_galley_impl(ui.ctx(), style, text_wrapping, fallback_font.into(), valign)
735    }
736
737    pub fn into_galley_impl(
738        self,
739        ctx: &crate::Context,
740        style: &Style,
741        text_wrapping: TextWrapping,
742        fallback_font: FontSelection,
743        default_valign: Align,
744    ) -> Arc<Galley> {
745        match self {
746            Self::Text(text) => {
747                let mut layout_job = LayoutJob::simple_format(
748                    text,
749                    TextFormat {
750                        font_id: FontSelection::Default.resolve(style),
751                        color: crate::Color32::PLACEHOLDER,
752                        valign: default_valign,
753                        ..Default::default()
754                    },
755                );
756                layout_job.wrap = text_wrapping;
757                ctx.fonts(|f| f.layout_job(layout_job))
758            }
759            Self::RichText(text) => {
760                let mut layout_job = Arc::unwrap_or_clone(text).into_layout_job(
761                    style,
762                    fallback_font,
763                    default_valign,
764                );
765                layout_job.wrap = text_wrapping;
766                ctx.fonts(|f| f.layout_job(layout_job))
767            }
768            Self::LayoutJob(job) => {
769                let mut job = Arc::unwrap_or_clone(job);
770                job.wrap = text_wrapping;
771                ctx.fonts(|f| f.layout_job(job))
772            }
773            Self::Galley(galley) => galley,
774        }
775    }
776}
777
778impl From<&str> for WidgetText {
779    #[inline]
780    fn from(text: &str) -> Self {
781        Self::Text(text.to_owned())
782    }
783}
784
785impl From<&String> for WidgetText {
786    #[inline]
787    fn from(text: &String) -> Self {
788        Self::Text(text.clone())
789    }
790}
791
792impl From<String> for WidgetText {
793    #[inline]
794    fn from(text: String) -> Self {
795        Self::Text(text)
796    }
797}
798
799impl From<&Box<str>> for WidgetText {
800    #[inline]
801    fn from(text: &Box<str>) -> Self {
802        Self::Text(text.to_string())
803    }
804}
805
806impl From<Box<str>> for WidgetText {
807    #[inline]
808    fn from(text: Box<str>) -> Self {
809        Self::Text(text.into())
810    }
811}
812
813impl From<Cow<'_, str>> for WidgetText {
814    #[inline]
815    fn from(text: Cow<'_, str>) -> Self {
816        Self::Text(text.into_owned())
817    }
818}
819
820impl From<RichText> for WidgetText {
821    #[inline]
822    fn from(rich_text: RichText) -> Self {
823        Self::RichText(Arc::new(rich_text))
824    }
825}
826
827impl From<Arc<RichText>> for WidgetText {
828    #[inline]
829    fn from(rich_text: Arc<RichText>) -> Self {
830        Self::RichText(rich_text)
831    }
832}
833
834impl From<LayoutJob> for WidgetText {
835    #[inline]
836    fn from(layout_job: LayoutJob) -> Self {
837        Self::LayoutJob(Arc::new(layout_job))
838    }
839}
840
841impl From<Arc<LayoutJob>> for WidgetText {
842    #[inline]
843    fn from(layout_job: Arc<LayoutJob>) -> Self {
844        Self::LayoutJob(layout_job)
845    }
846}
847
848impl From<Arc<Galley>> for WidgetText {
849    #[inline]
850    fn from(galley: Arc<Galley>) -> Self {
851        Self::Galley(galley)
852    }
853}
854
855#[cfg(test)]
856mod tests {
857    use crate::WidgetText;
858
859    #[test]
860    fn ensure_small_widget_text() {
861        assert_eq!(size_of::<WidgetText>(), size_of::<String>());
862    }
863}