From 33b5a900197e2798a393d6d9a0834039666eddbb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 19 Apr 2023 01:19:56 +0200 Subject: Make basic text shaping the default shaping strategy --- wgpu/src/backend.rs | 6 +++++- wgpu/src/geometry.rs | 1 + wgpu/src/layer.rs | 3 +++ wgpu/src/layer/text.rs | 10 ++++++++++ wgpu/src/text.rs | 7 +++++++ 5 files changed, 26 insertions(+), 1 deletion(-) (limited to 'wgpu/src') diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 9772781a..d09b2dba 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -354,8 +354,10 @@ impl backend::Text for Backend { size: f32, font: Font, bounds: Size, + advanced_shape: bool, ) -> (f32, f32) { - self.text_pipeline.measure(contents, size, font, bounds) + self.text_pipeline + .measure(contents, size, font, bounds, advanced_shape) } fn hit_test( @@ -366,6 +368,7 @@ impl backend::Text for Backend { bounds: Size, point: Point, nearest_only: bool, + advanced_shape: bool, ) -> Option { self.text_pipeline.hit_test( contents, @@ -374,6 +377,7 @@ impl backend::Text for Backend { bounds, point, nearest_only, + advanced_shape, ) } diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs index 59ec31fe..a85875a4 100644 --- a/wgpu/src/geometry.rs +++ b/wgpu/src/geometry.rs @@ -334,6 +334,7 @@ impl Frame { font: text.font, horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, + advanced_shape: text.advanced_shape, }); } diff --git a/wgpu/src/layer.rs b/wgpu/src/layer.rs index c4723397..7c5b43a3 100644 --- a/wgpu/src/layer.rs +++ b/wgpu/src/layer.rs @@ -64,6 +64,7 @@ impl<'a> Layer<'a> { font: Font::MONOSPACE, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, + advanced_shape: false, }; overlay.text.push(text); @@ -116,6 +117,7 @@ impl<'a> Layer<'a> { font, horizontal_alignment, vertical_alignment, + advanced_shape, } => { let layer = &mut layers[current_layer]; @@ -127,6 +129,7 @@ impl<'a> Layer<'a> { font: *font, horizontal_alignment: *horizontal_alignment, vertical_alignment: *vertical_alignment, + advanced_shape: *advanced_shape, }); } Primitive::Quad { diff --git a/wgpu/src/layer/text.rs b/wgpu/src/layer/text.rs index fdbdaafb..d36ff273 100644 --- a/wgpu/src/layer/text.rs +++ b/wgpu/src/layer/text.rs @@ -24,4 +24,14 @@ pub struct Text<'a> { /// The vertical alignment of the [`Text`]. pub vertical_alignment: alignment::Vertical, + + /// Whether the text needs advanced 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, } diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index f01e0b42..f433a5b6 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -83,6 +83,7 @@ impl Pipeline { height: (section.bounds.height * scale_factor) .ceil(), }, + advanced_shape: section.advanced_shape, }, ); @@ -213,6 +214,7 @@ impl Pipeline { size: f32, font: Font, bounds: Size, + advanced_shape: bool, ) -> (f32, f32) { let mut measurement_cache = self.measurement_cache.borrow_mut(); @@ -223,6 +225,7 @@ impl Pipeline { size, font, bounds, + advanced_shape, }, ); @@ -244,6 +247,7 @@ impl Pipeline { bounds: Size, point: Point, _nearest_only: bool, + advanced_shape: bool, ) -> Option { let mut measurement_cache = self.measurement_cache.borrow_mut(); @@ -254,6 +258,7 @@ impl Pipeline { size, font, bounds, + advanced_shape, }, ); @@ -364,6 +369,7 @@ impl Cache { .family(to_family(key.font.family)) .weight(to_weight(key.font.weight)) .stretch(to_stretch(key.font.stretch)), + !key.advanced_shape, ); let _ = entry.insert(buffer); @@ -388,6 +394,7 @@ struct Key<'a> { size: f32, font: Font, bounds: Size, + advanced_shape: bool, } type KeyHash = u64; -- cgit From 4bd290afe7d81d9aaf7467b3ce91491f6600261a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 19 Apr 2023 02:00:45 +0200 Subject: Introduce `text::Shaping` enum and replace magic boolean --- wgpu/src/backend.rs | 8 ++++---- wgpu/src/geometry.rs | 2 +- wgpu/src/layer.rs | 7 ++++--- wgpu/src/layer/text.rs | 12 +++--------- wgpu/src/text.rs | 16 ++++++++-------- 5 files changed, 20 insertions(+), 25 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index d09b2dba..6b847aff 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -354,10 +354,10 @@ impl backend::Text for Backend { size: f32, font: Font, bounds: Size, - advanced_shape: bool, + shaping: core::text::Shaping, ) -> (f32, f32) { self.text_pipeline - .measure(contents, size, font, bounds, advanced_shape) + .measure(contents, size, font, bounds, shaping) } fn hit_test( @@ -366,18 +366,18 @@ impl backend::Text for Backend { size: f32, font: Font, bounds: Size, + shaping: core::text::Shaping, point: Point, nearest_only: bool, - advanced_shape: bool, ) -> Option { self.text_pipeline.hit_test( contents, size, font, bounds, + shaping, point, nearest_only, - advanced_shape, ) } diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs index a85875a4..f6397ab7 100644 --- a/wgpu/src/geometry.rs +++ b/wgpu/src/geometry.rs @@ -334,7 +334,7 @@ impl Frame { font: text.font, horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, - advanced_shape: text.advanced_shape, + shaping: text.shaping, }); } diff --git a/wgpu/src/layer.rs b/wgpu/src/layer.rs index 7c5b43a3..b9fd044e 100644 --- a/wgpu/src/layer.rs +++ b/wgpu/src/layer.rs @@ -10,6 +10,7 @@ pub use mesh::Mesh; pub use quad::Quad; pub use text::Text; +use crate::core; use crate::core::alignment; use crate::core::{Background, Color, Font, Point, Rectangle, Size, Vector}; use crate::graphics::{Primitive, Viewport}; @@ -64,7 +65,7 @@ impl<'a> Layer<'a> { font: Font::MONOSPACE, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, - advanced_shape: false, + shaping: core::text::Shaping::Basic, }; overlay.text.push(text); @@ -117,7 +118,7 @@ impl<'a> Layer<'a> { font, horizontal_alignment, vertical_alignment, - advanced_shape, + shaping, } => { let layer = &mut layers[current_layer]; @@ -129,7 +130,7 @@ impl<'a> Layer<'a> { font: *font, horizontal_alignment: *horizontal_alignment, vertical_alignment: *vertical_alignment, - advanced_shape: *advanced_shape, + shaping: *shaping, }); } Primitive::Quad { diff --git a/wgpu/src/layer/text.rs b/wgpu/src/layer/text.rs index d36ff273..665f7188 100644 --- a/wgpu/src/layer/text.rs +++ b/wgpu/src/layer/text.rs @@ -1,4 +1,5 @@ use crate::core::alignment; +use crate::core::text; use crate::core::{Color, Font, Rectangle}; /// A paragraph of text. @@ -25,13 +26,6 @@ pub struct Text<'a> { /// The vertical alignment of the [`Text`]. pub vertical_alignment: alignment::Vertical, - /// Whether the text needs advanced 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, + /// The shaping strategy of the text. + pub shaping: text::Shaping, } diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index f433a5b6..fc126125 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -1,6 +1,6 @@ use crate::core::alignment; use crate::core::font::{self, Font}; -use crate::core::text::Hit; +use crate::core::text::{Hit, Shaping}; use crate::core::{Point, Rectangle, Size}; use crate::layer::Text; @@ -83,7 +83,7 @@ impl Pipeline { height: (section.bounds.height * scale_factor) .ceil(), }, - advanced_shape: section.advanced_shape, + shaping: section.shaping, }, ); @@ -214,7 +214,7 @@ impl Pipeline { size: f32, font: Font, bounds: Size, - advanced_shape: bool, + shaping: Shaping, ) -> (f32, f32) { let mut measurement_cache = self.measurement_cache.borrow_mut(); @@ -225,7 +225,7 @@ impl Pipeline { size, font, bounds, - advanced_shape, + shaping, }, ); @@ -245,9 +245,9 @@ impl Pipeline { size: f32, font: Font, bounds: Size, + shaping: Shaping, point: Point, _nearest_only: bool, - advanced_shape: bool, ) -> Option { let mut measurement_cache = self.measurement_cache.borrow_mut(); @@ -258,7 +258,7 @@ impl Pipeline { size, font, bounds, - advanced_shape, + shaping, }, ); @@ -369,7 +369,7 @@ impl Cache { .family(to_family(key.font.family)) .weight(to_weight(key.font.weight)) .stretch(to_stretch(key.font.stretch)), - !key.advanced_shape, + matches!(key.shaping, Shaping::Basic), ); let _ = entry.insert(buffer); @@ -394,7 +394,7 @@ struct Key<'a> { size: f32, font: Font, bounds: Size, - advanced_shape: bool, + shaping: Shaping, } type KeyHash = u64; -- cgit From edf3432bf5176be13437b9fd5d25b890ec9dbe69 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 2 May 2023 00:58:33 +0200 Subject: Update `glyphon` and `cosmic-text` --- wgpu/src/text.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'wgpu/src') diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index fc126125..ad7bdc8d 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -311,6 +311,13 @@ fn to_stretch(stretch: font::Stretch) -> glyphon::Stretch { } } +fn to_shaping(shaping: Shaping) -> glyphon::Shaping { + match shaping { + Shaping::Basic => glyphon::Shaping::Basic, + Shaping::Advanced => glyphon::Shaping::Advanced, + } +} + struct Cache { entries: FxHashMap, recently_used: FxHashSet, @@ -369,7 +376,7 @@ impl Cache { .family(to_family(key.font.family)) .weight(to_weight(key.font.weight)) .stretch(to_stretch(key.font.stretch)), - matches!(key.shaping, Shaping::Basic), + to_shaping(key.shaping), ); let _ = entry.insert(buffer); -- cgit