Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add opacity factor to TextShape #3916

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/egui/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
emilk marked this conversation as resolved.
Show resolved Hide resolved
&self,
pos: Pos2,
galley: Arc<Galley>,
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<Galley>, text_color: Color32) {
Expand Down
25 changes: 25 additions & 0 deletions crates/epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,19 @@ impl Shape {
.into()
}

/// The entire [`Galley`] will be rendered with the given opacity in gamma space.
#[inline]
pub fn galley_with_opacity_factor(
emilk marked this conversation as resolved.
Show resolved Hide resolved
pos: Pos2,
galley: Arc<Galley>,
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<Galley>, text_color: Color32) -> Self {
Expand Down Expand Up @@ -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<Color32>,

/// 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,
Expand All @@ -762,6 +779,7 @@ impl TextShape {
underline: Stroke::NONE,
fallback_color,
override_text_color: None,
opacity_factor: 1.0,
angle: 0.0,
}
}
Expand Down Expand Up @@ -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<TextShape> for Shape {
Expand Down
1 change: 1 addition & 0 deletions crates/epaint/src/shape_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions crates/epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,13 +1474,18 @@ impl Tessellator {
underline,
override_text_color,
fallback_color,
opacity_factor,
angle,
} = text_shape;

if galley.is_empty() {
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.");
Expand Down Expand Up @@ -1548,6 +1553,10 @@ impl Tessellator {
color = *fallback_color;
}

if *opacity_factor <= 1.0 {
emilk marked this conversation as resolved.
Show resolved Hide resolved
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 {
Expand Down