diff options
author | 2023-04-19 02:00:45 +0200 | |
---|---|---|
committer | 2023-05-02 01:02:32 +0200 | |
commit | 4bd290afe7d81d9aaf7467b3ce91491f6600261a (patch) | |
tree | 906bfe10f6118c86429c3bb83a8ce742dccb170a /core | |
parent | 33b5a900197e2798a393d6d9a0834039666eddbb (diff) | |
download | iced-4bd290afe7d81d9aaf7467b3ce91491f6600261a.tar.gz iced-4bd290afe7d81d9aaf7467b3ce91491f6600261a.tar.bz2 iced-4bd290afe7d81d9aaf7467b3ce91491f6600261a.zip |
Introduce `text::Shaping` enum and replace magic boolean
Diffstat (limited to 'core')
-rw-r--r-- | core/src/renderer/null.rs | 4 | ||||
-rw-r--r-- | core/src/text.rs | 30 | ||||
-rw-r--r-- | core/src/widget/text.rs | 28 |
3 files changed, 36 insertions, 26 deletions
diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index f62a4338..22afb058 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -62,7 +62,7 @@ impl text::Renderer for Null { _size: f32, _font: Font, _bounds: Size, - _needs_shaping: bool, + _shaping: text::Shaping, ) -> (f32, f32) { (0.0, 20.0) } @@ -73,9 +73,9 @@ impl text::Renderer for Null { _size: f32, _font: Self::Font, _bounds: Size, + _shaping: text::Shaping, _point: Point, _nearest_only: bool, - _advanced_shape: bool, ) -> Option<text::Hit> { None } diff --git a/core/src/text.rs b/core/src/text.rs index 0111cf31..c59d8fce 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -28,14 +28,32 @@ pub struct Text<'a, Font> { /// The vertical alignment of the [`Text`]. pub vertical_alignment: alignment::Vertical, - /// Whether the [`Text`] needs advanced shaping and font fallback. + /// 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. - pub advanced_shape: bool, + Advanced, } /// The result of hit testing on text. @@ -86,7 +104,7 @@ pub trait Renderer: crate::Renderer { size: f32, font: Self::Font, bounds: Size, - advanced_shape: bool, + shaping: Shaping, ) -> (f32, f32); /// Measures the width of the text as if it were laid out in a single line. @@ -95,10 +113,10 @@ pub trait Renderer: crate::Renderer { content: &str, size: f32, font: Self::Font, - advanced_shape: bool, + shaping: Shaping, ) -> f32 { let (width, _) = - self.measure(content, size, font, Size::INFINITY, advanced_shape); + self.measure(content, size, font, Size::INFINITY, shaping); width } @@ -116,9 +134,9 @@ pub trait Renderer: crate::Renderer { size: f32, font: Self::Font, bounds: Size, + shaping: Shaping, point: Point, nearest_only: bool, - advanced_shape: bool, ) -> Option<Hit>; /// Loads a [`Self::Font`] from its bytes. diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 2df4556d..f0392168 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -24,8 +24,8 @@ where horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, font: Option<Renderer::Font>, + shaping: text::Shaping, style: <Renderer::Theme as StyleSheet>::Style, - advanced_shape: bool, } impl<'a, Renderer> Text<'a, Renderer> @@ -43,8 +43,8 @@ where height: Length::Shrink, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, + shaping: text::Shaping::Basic, style: Default::default(), - advanced_shape: false, } } @@ -101,17 +101,9 @@ where self } - /// Enables advanced text shaping and font fallback for the [`Text`]. - /// - /// You will need to enable this 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. - /// - /// If your text isn't displaying properly, try enabling this! - /// - /// Advanced shaping is expensive! You should only enable it when necessary. - pub fn advanced_shape(mut self) -> Self { - self.advanced_shape = true; + /// Sets the [`text::Shaping`] strategy of the [`Text`]. + pub fn shaping(mut self, shaping: text::Shaping) -> Self { + self.shaping = shaping; self } } @@ -145,7 +137,7 @@ where size, self.font.unwrap_or_else(|| renderer.default_font()), bounds, - self.advanced_shape, + self.shaping, ); let size = limits.resolve(Size::new(width, height)); @@ -173,7 +165,7 @@ where theme.appearance(self.style.clone()), self.horizontal_alignment, self.vertical_alignment, - self.advanced_shape, + self.shaping, ); } } @@ -198,7 +190,7 @@ pub fn draw<Renderer>( appearance: Appearance, horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, - advanced_shape: bool, + shaping: text::Shaping, ) where Renderer: text::Renderer, { @@ -224,7 +216,7 @@ pub fn draw<Renderer>( font: font.unwrap_or_else(|| renderer.default_font()), horizontal_alignment, vertical_alignment, - advanced_shape, + shaping, }); } @@ -254,7 +246,7 @@ where vertical_alignment: self.vertical_alignment, font: self.font, style: self.style.clone(), - advanced_shape: self.advanced_shape, + shaping: self.shaping, } } } |