diff options
| author | 2023-05-02 03:30:06 +0200 | |
|---|---|---|
| committer | 2023-05-02 03:30:06 +0200 | |
| commit | 2d7d9a130ece3fb6fa4cd52f9b32b4abd7887cf5 (patch) | |
| tree | 6acba55171f28af1a3c9c4e65a99ad10abc11c65 /core | |
| parent | 57a276e16539d6aeca0619e0c5e36d0b1c1b5ef9 (diff) | |
| parent | edf3432bf5176be13437b9fd5d25b890ec9dbe69 (diff) | |
| download | iced-2d7d9a130ece3fb6fa4cd52f9b32b4abd7887cf5.tar.gz iced-2d7d9a130ece3fb6fa4cd52f9b32b4abd7887cf5.tar.bz2 iced-2d7d9a130ece3fb6fa4cd52f9b32b4abd7887cf5.zip | |
Merge pull request #1822 from iced-rs/basic-shaping
`text::Shaping` strategy selection
Diffstat (limited to '')
| -rw-r--r-- | core/src/renderer/null.rs | 2 | ||||
| -rw-r--r-- | core/src/text.rs | 40 | ||||
| -rw-r--r-- | core/src/widget/text.rs | 13 | 
3 files changed, 53 insertions, 2 deletions
| diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index 88c58825..22afb058 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -62,6 +62,7 @@ impl text::Renderer for Null {          _size: f32,          _font: Font,          _bounds: Size, +        _shaping: text::Shaping,      ) -> (f32, f32) {          (0.0, 20.0)      } @@ -72,6 +73,7 @@ impl text::Renderer for Null {          _size: f32,          _font: Self::Font,          _bounds: Size, +        _shaping: text::Shaping,          _point: Point,          _nearest_only: bool,      ) -> Option<text::Hit> { diff --git a/core/src/text.rs b/core/src/text.rs index 4c72abc3..c59d8fce 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -27,6 +27,33 @@ pub struct Text<'a, Font> {      /// The vertical alignment of the [`Text`].      pub vertical_alignment: alignment::Vertical, + +    /// The [`Shaping`] strategy of the [`Text`]. +    pub shaping: Shaping, +} + +/// The shaping strategy of some text. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] +pub enum Shaping { +    /// No shaping and no font fallback. +    /// +    /// This shaping strategy is very cheap, but it will not display complex +    /// scripts properly nor try to find missing glyphs in your system fonts. +    /// +    /// You should use this strategy when you have complete control of the text +    /// and the font you are displaying in your application. +    /// +    /// This is the default. +    #[default] +    Basic, +    /// Advanced text shaping and font fallback. +    /// +    /// You will need to enable this flag if the text contains a complex +    /// script, the font used needs it, and/or multiple fonts in your system +    /// may be needed to display all of the glyphs. +    /// +    /// Advanced shaping is expensive! You should only enable it when necessary. +    Advanced,  }  /// The result of hit testing on text. @@ -77,11 +104,19 @@ pub trait Renderer: crate::Renderer {          size: f32,          font: Self::Font,          bounds: Size, +        shaping: Shaping,      ) -> (f32, f32);      /// Measures the width of the text as if it were laid out in a single line. -    fn measure_width(&self, content: &str, size: f32, font: Self::Font) -> f32 { -        let (width, _) = self.measure(content, size, font, Size::INFINITY); +    fn measure_width( +        &self, +        content: &str, +        size: f32, +        font: Self::Font, +        shaping: Shaping, +    ) -> f32 { +        let (width, _) = +            self.measure(content, size, font, Size::INFINITY, shaping);          width      } @@ -99,6 +134,7 @@ pub trait Renderer: crate::Renderer {          size: f32,          font: Self::Font,          bounds: Size, +        shaping: Shaping,          point: Point,          nearest_only: bool,      ) -> Option<Hit>; diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 3193ba84..f0392168 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -24,6 +24,7 @@ where      horizontal_alignment: alignment::Horizontal,      vertical_alignment: alignment::Vertical,      font: Option<Renderer::Font>, +    shaping: text::Shaping,      style: <Renderer::Theme as StyleSheet>::Style,  } @@ -42,6 +43,7 @@ where              height: Length::Shrink,              horizontal_alignment: alignment::Horizontal::Left,              vertical_alignment: alignment::Vertical::Top, +            shaping: text::Shaping::Basic,              style: Default::default(),          }      } @@ -98,6 +100,12 @@ where          self.vertical_alignment = alignment;          self      } + +    /// Sets the [`text::Shaping`] strategy of the [`Text`]. +    pub fn shaping(mut self, shaping: text::Shaping) -> Self { +        self.shaping = shaping; +        self +    }  }  impl<'a, Message, Renderer> Widget<Message, Renderer> for Text<'a, Renderer> @@ -129,6 +137,7 @@ where              size,              self.font.unwrap_or_else(|| renderer.default_font()),              bounds, +            self.shaping,          );          let size = limits.resolve(Size::new(width, height)); @@ -156,6 +165,7 @@ where              theme.appearance(self.style.clone()),              self.horizontal_alignment,              self.vertical_alignment, +            self.shaping,          );      }  } @@ -180,6 +190,7 @@ pub fn draw<Renderer>(      appearance: Appearance,      horizontal_alignment: alignment::Horizontal,      vertical_alignment: alignment::Vertical, +    shaping: text::Shaping,  ) where      Renderer: text::Renderer,  { @@ -205,6 +216,7 @@ pub fn draw<Renderer>(          font: font.unwrap_or_else(|| renderer.default_font()),          horizontal_alignment,          vertical_alignment, +        shaping,      });  } @@ -234,6 +246,7 @@ where              vertical_alignment: self.vertical_alignment,              font: self.font,              style: self.style.clone(), +            shaping: self.shaping,          }      }  } | 
