text.rs

  1use crate::{
  2    color::Color,
  3    font_cache::FamilyId,
  4    fonts::{FontId, TextStyle},
  5    geometry::{
  6        rect::RectF,
  7        vector::{vec2f, Vector2F},
  8    },
  9    json::{ToJson, Value},
 10    text_layout::Line,
 11    DebugContext, Element, Event, EventContext, FontCache, LayoutContext, PaintContext,
 12    SizeConstraint,
 13};
 14use serde::Deserialize;
 15use serde_json::json;
 16use smallvec::{smallvec, SmallVec};
 17
 18pub struct Text {
 19    text: String,
 20    family_id: FamilyId,
 21    font_size: f32,
 22    style: TextStyle,
 23}
 24
 25impl Text {
 26    pub fn new(text: String, family_id: FamilyId, font_size: f32) -> Self {
 27        Self {
 28            text,
 29            family_id,
 30            font_size,
 31            style: Default::default(),
 32        }
 33    }
 34
 35    pub fn with_style(mut self, style: &TextStyle) -> Self {
 36        self.style = style.clone();
 37        self
 38    }
 39
 40    pub fn with_default_color(mut self, color: Color) -> Self {
 41        self.style.color = color;
 42        self
 43    }
 44}
 45
 46impl Element for Text {
 47    type LayoutState = Line;
 48    type PaintState = ();
 49
 50    fn layout(
 51        &mut self,
 52        constraint: SizeConstraint,
 53        cx: &mut LayoutContext,
 54    ) -> (Vector2F, Self::LayoutState) {
 55        let font_id = cx
 56            .font_cache
 57            .select_font(self.family_id, &self.style.font_properties)
 58            .unwrap();
 59
 60        todo!()
 61        // let line =
 62        //     cx.text_layout_cache
 63        //         .layout_str(self.text.as_str(), self.font_size, runs.as_slice());
 64
 65        // let size = vec2f(
 66        //     line.width().max(constraint.min.x()).min(constraint.max.x()),
 67        //     cx.font_cache.line_height(font_id, self.font_size).ceil(),
 68        // );
 69
 70        // (size, line)
 71    }
 72
 73    fn paint(
 74        &mut self,
 75        bounds: RectF,
 76        line: &mut Self::LayoutState,
 77        cx: &mut PaintContext,
 78    ) -> Self::PaintState {
 79        line.paint(
 80            bounds.origin(),
 81            RectF::new(vec2f(0., 0.), bounds.size()),
 82            cx,
 83        )
 84    }
 85
 86    fn dispatch_event(
 87        &mut self,
 88        _: &Event,
 89        _: RectF,
 90        _: &mut Self::LayoutState,
 91        _: &mut Self::PaintState,
 92        _: &mut EventContext,
 93    ) -> bool {
 94        false
 95    }
 96
 97    fn debug(
 98        &self,
 99        bounds: RectF,
100        _: &Self::LayoutState,
101        _: &Self::PaintState,
102        cx: &DebugContext,
103    ) -> Value {
104        json!({
105            "type": "Label",
106            "bounds": bounds.to_json(),
107            "text": &self.text,
108            "font_family": cx.font_cache.family_name(self.family_id).unwrap(),
109            "font_size": self.font_size,
110            "style": self.style.to_json(),
111        })
112    }
113}