From 92c01da36016e94ff20163806ddcbcf7e33d4031 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 10 Oct 2020 22:19:36 +0200 Subject: =?UTF-8?q?Switch=20back=20to=20custom=20geometry=20types,=20unifi?= =?UTF-8?q?ed=20with=20layout=20primitives=20=F0=9F=8F=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layout/text.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/layout/text.rs (limited to 'src/layout/text.rs') diff --git a/src/layout/text.rs b/src/layout/text.rs new file mode 100644 index 00000000..fafc1b14 --- /dev/null +++ b/src/layout/text.rs @@ -0,0 +1,51 @@ +use std::fmt::{self, Debug, Formatter}; +use std::rc::Rc; + +use fontdock::{FallbackTree, FontVariant}; + +use super::*; +use crate::shaping; + +/// A text node. +#[derive(Clone, PartialEq)] +pub struct Text { + pub text: String, + pub size: Length, + pub dir: Dir, + pub fallback: Rc, + pub variant: FontVariant, + pub aligns: Gen, +} + +#[async_trait(?Send)] +impl Layout for Text { + async fn layout( + &self, + ctx: &mut LayoutContext, + _constraints: LayoutConstraints, + ) -> Vec { + let mut loader = ctx.loader.borrow_mut(); + let boxed = shaping::shape( + &self.text, + self.size, + self.dir, + &mut loader, + &self.fallback, + self.variant, + ) + .await; + vec![LayoutItem::Box(boxed, self.aligns)] + } +} + +impl Debug for Text { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "Text({})", self.text) + } +} + +impl From for LayoutNode { + fn from(text: Text) -> Self { + Self::Text(text) + } +} -- cgit v1.2.3