From f55f36387bbb6e751ba2a714ef91e9c786a89f2c Mon Sep 17 00:00:00 2001 From: Isaac Mills Date: Mon, 29 Jan 2024 10:36:40 -0500 Subject: [PATCH 1/4] Add opacity factor to TextShape --- crates/epaint/src/shape.rs | 25 +++++++++++++++++++++++++ crates/epaint/src/shape_transform.rs | 1 + crates/epaint/src/tessellator.rs | 9 +++++++++ 3 files changed, 35 insertions(+) diff --git a/crates/epaint/src/shape.rs b/crates/epaint/src/shape.rs index 61b9d75e92b..9df928d0e03 100644 --- a/crates/epaint/src/shape.rs +++ b/crates/epaint/src/shape.rs @@ -288,6 +288,19 @@ impl Shape { .into() } + /// The entire [`Galley`] will be rendered with the given opacity. + #[inline] + pub fn galley_with_opacity_factor( + pos: Pos2, + galley: Arc, + opacity_factor: f32, + fallback_color: Color32, + ) -> Self { + TextShape::new(pos, galley, fallback_color) + .with_opacity_factor(opacity_factor) + .into() + } + #[inline] #[deprecated = "Use `Shape::galley` or `Shape::galley_with_override_text_color` instead"] pub fn galley_with_color(pos: Pos2, galley: Arc, text_color: Color32) -> Self { @@ -745,6 +758,10 @@ pub struct TextShape { /// This only affects the glyphs and will NOT replace background color nor strikethrough/underline color. pub override_text_color: Option, + /// If set, the text will be rendered with the given opacity in gamma space + /// Affects everything: backgrounds, glyphs, strikethough, underline, etc. + pub opacity_factor: f32, + /// Rotate text by this many radians clockwise. /// The pivot is `pos` (the upper left corner of the text). pub angle: f32, @@ -762,6 +779,7 @@ impl TextShape { underline: Stroke::NONE, fallback_color, override_text_color: None, + opacity_factor: 1.0, angle: 0.0, } } @@ -792,6 +810,13 @@ impl TextShape { self.angle = angle; self } + + /// Render text with this opacity in gamma space + #[inline] + pub fn with_opacity_factor(mut self, opacity_factor: f32) -> Self { + self.opacity_factor = opacity_factor; + self + } } impl From for Shape { diff --git a/crates/epaint/src/shape_transform.rs b/crates/epaint/src/shape_transform.rs index b36accb5a3f..c8edff1fcaf 100644 --- a/crates/epaint/src/shape_transform.rs +++ b/crates/epaint/src/shape_transform.rs @@ -56,6 +56,7 @@ pub fn adjust_colors(shape: &mut Shape, adjust_color: &impl Fn(&mut Color32)) { underline, fallback_color, override_text_color, + opacity_factor: _, angle: _, }) => { adjust_color(&mut underline.color); diff --git a/crates/epaint/src/tessellator.rs b/crates/epaint/src/tessellator.rs index 72899f2844c..6abd466ab40 100644 --- a/crates/epaint/src/tessellator.rs +++ b/crates/epaint/src/tessellator.rs @@ -1474,6 +1474,7 @@ impl Tessellator { underline, override_text_color, fallback_color, + opacity_factor, angle, } = text_shape; @@ -1481,6 +1482,10 @@ impl Tessellator { return; } + if *opacity_factor <= 0.0 { + return; + } + if galley.pixels_per_point != self.pixels_per_point { eprintln!("epaint: WARNING: pixels_per_point (dpi scale) have changed between text layout and tessellation. \ You must recreate your text shapes if pixels_per_point changes."); @@ -1548,6 +1553,10 @@ impl Tessellator { color = *fallback_color; } + if *opacity_factor <= 1.0 { + color = color.gamma_multiply(*opacity_factor); + } + crate::epaint_assert!(color != Color32::PLACEHOLDER, "A placeholder color made it to the tessellator. You forgot to set a fallback color."); let offset = if *angle == 0.0 { From 1f319aca12fc9d3944e8b82093f26d684a04fc26 Mon Sep 17 00:00:00 2001 From: Isaac Mills Date: Mon, 29 Jan 2024 11:27:12 -0500 Subject: [PATCH 2/4] Add opacity factor to painter --- crates/egui/src/painter.rs | 23 +++++++++++++++++++++++ crates/epaint/src/shape.rs | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/painter.rs b/crates/egui/src/painter.rs index c0eb8e12fca..a8a170b34ba 100644 --- a/crates/egui/src/painter.rs +++ b/crates/egui/src/painter.rs @@ -469,6 +469,29 @@ impl Painter { } } + /// Paint text that has already been laid out in a [`Galley`]. + /// + /// You can create the [`Galley`] with [`Self::layout`]. + /// + /// The entire [`Galley`] will be rendered with the given opacity in gamma space. + #[inline] + pub fn galley_with_opacity_factor( + &self, + pos: Pos2, + galley: Arc, + opacity_factor: f32, + fallback_color: Color32, + ) { + if !galley.is_empty() && opacity_factor > 0.0 { + self.add(Shape::galley_with_opacity_factor( + pos, + galley, + opacity_factor, + fallback_color, + )); + } + } + #[deprecated = "Use `Painter::galley` or `Painter::galley_with_override_text_color` instead"] #[inline] pub fn galley_with_color(&self, pos: Pos2, galley: Arc, text_color: Color32) { diff --git a/crates/epaint/src/shape.rs b/crates/epaint/src/shape.rs index 9df928d0e03..8e96ad3412f 100644 --- a/crates/epaint/src/shape.rs +++ b/crates/epaint/src/shape.rs @@ -288,7 +288,7 @@ impl Shape { .into() } - /// The entire [`Galley`] will be rendered with the given opacity. + /// The entire [`Galley`] will be rendered with the given opacity in gamma space. #[inline] pub fn galley_with_opacity_factor( pos: Pos2, From 05c639a260eb08c9f0386ba11c0987821dd38d4d Mon Sep 17 00:00:00 2001 From: Isaac Mills Date: Tue, 30 Jan 2024 06:55:49 -0500 Subject: [PATCH 3/4] Remvoe galley_with_opacity_factor functions and fix opacity factor in tesselator --- crates/egui/src/painter.rs | 23 ----------------------- crates/epaint/src/shape.rs | 13 ------------- crates/epaint/src/tessellator.rs | 2 +- scripts/build_demo_web.sh | 2 +- 4 files changed, 2 insertions(+), 38 deletions(-) diff --git a/crates/egui/src/painter.rs b/crates/egui/src/painter.rs index a8a170b34ba..c0eb8e12fca 100644 --- a/crates/egui/src/painter.rs +++ b/crates/egui/src/painter.rs @@ -469,29 +469,6 @@ impl Painter { } } - /// Paint text that has already been laid out in a [`Galley`]. - /// - /// You can create the [`Galley`] with [`Self::layout`]. - /// - /// The entire [`Galley`] will be rendered with the given opacity in gamma space. - #[inline] - pub fn galley_with_opacity_factor( - &self, - pos: Pos2, - galley: Arc, - opacity_factor: f32, - fallback_color: Color32, - ) { - if !galley.is_empty() && opacity_factor > 0.0 { - self.add(Shape::galley_with_opacity_factor( - pos, - galley, - opacity_factor, - fallback_color, - )); - } - } - #[deprecated = "Use `Painter::galley` or `Painter::galley_with_override_text_color` instead"] #[inline] pub fn galley_with_color(&self, pos: Pos2, galley: Arc, text_color: Color32) { diff --git a/crates/epaint/src/shape.rs b/crates/epaint/src/shape.rs index 8e96ad3412f..ceb5dd072e5 100644 --- a/crates/epaint/src/shape.rs +++ b/crates/epaint/src/shape.rs @@ -288,19 +288,6 @@ impl Shape { .into() } - /// The entire [`Galley`] will be rendered with the given opacity in gamma space. - #[inline] - pub fn galley_with_opacity_factor( - pos: Pos2, - galley: Arc, - opacity_factor: f32, - fallback_color: Color32, - ) -> Self { - TextShape::new(pos, galley, fallback_color) - .with_opacity_factor(opacity_factor) - .into() - } - #[inline] #[deprecated = "Use `Shape::galley` or `Shape::galley_with_override_text_color` instead"] pub fn galley_with_color(pos: Pos2, galley: Arc, text_color: Color32) -> Self { diff --git a/crates/epaint/src/tessellator.rs b/crates/epaint/src/tessellator.rs index 6abd466ab40..bc729045dd7 100644 --- a/crates/epaint/src/tessellator.rs +++ b/crates/epaint/src/tessellator.rs @@ -1553,7 +1553,7 @@ impl Tessellator { color = *fallback_color; } - if *opacity_factor <= 1.0 { + if *opacity_factor < 1.0 { color = color.gamma_multiply(*opacity_factor); } diff --git a/scripts/build_demo_web.sh b/scripts/build_demo_web.sh index 6bf31452137..f3c72689361 100755 --- a/scripts/build_demo_web.sh +++ b/scripts/build_demo_web.sh @@ -8,7 +8,7 @@ cd "$script_path/.." # This is required to enable the web_sys clipboard API which eframe web uses # https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Clipboard.html # https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html -export RUSTFLAGS=--cfg=web_sys_unstable_apis +export RUSTFLAGS="--cfg=web_sys_unstable_apis" CRATE_NAME="egui_demo_app" From c5dcc9be61eb0e1805e9c19bd53d7e1cf711757a Mon Sep 17 00:00:00 2001 From: Isaac Mills Date: Tue, 30 Jan 2024 07:00:07 -0500 Subject: [PATCH 4/4] Restore build_demo_web.sh --- scripts/build_demo_web.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build_demo_web.sh b/scripts/build_demo_web.sh index f3c72689361..6bf31452137 100755 --- a/scripts/build_demo_web.sh +++ b/scripts/build_demo_web.sh @@ -8,7 +8,7 @@ cd "$script_path/.." # This is required to enable the web_sys clipboard API which eframe web uses # https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Clipboard.html # https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html -export RUSTFLAGS="--cfg=web_sys_unstable_apis" +export RUSTFLAGS=--cfg=web_sys_unstable_apis CRATE_NAME="egui_demo_app"