Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 0 additions & 13 deletions crates/egui-wgpu/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,19 +564,6 @@ impl Renderer {
);
Cow::Borrowed(&image.pixels)
}
epaint::ImageData::Font(image) => {
assert_eq!(
width as usize * height as usize,
image.pixels.len(),
"Mismatch between texture size and texel count"
);
profiling::scope!("font -> sRGBA");
Cow::Owned(
image
.srgba_pixels(Default::default())
.collect::<Vec<epaint::Color32>>(),
)
}
};
let data_bytes: &[u8] = bytemuck::cast_slice(data_color32.as_slice());

Expand Down
63 changes: 9 additions & 54 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Default for WrappedTextureManager {
// Will be filled in later
let font_id = tex_mngr.alloc(
"egui_font_texture".into(),
epaint::FontImage::new([0, 0]).into(),
epaint::ColorImage::filled([0, 0], Color32::TRANSPARENT).into(),
Default::default(),
);
assert_eq!(
Expand Down Expand Up @@ -610,6 +610,8 @@ impl ContextImpl {
log::trace!("Adding new fonts");
}

let text_alpha_from_coverage = self.memory.options.style().visuals.text_alpha_from_coverage;

let mut is_new = false;

let fonts = self
Expand All @@ -624,13 +626,14 @@ impl ContextImpl {
Fonts::new(
pixels_per_point,
max_texture_side,
text_alpha_from_coverage,
self.font_definitions.clone(),
)
});

{
profiling::scope!("Fonts::begin_pass");
fonts.begin_pass(pixels_per_point, max_texture_side);
fonts.begin_pass(pixels_per_point, max_texture_side, text_alpha_from_coverage);
}

if is_new && self.memory.options.preload_font_glyphs {
Expand Down Expand Up @@ -1921,16 +1924,6 @@ impl Context {
}
}

pub(crate) fn reset_font_atlas(&self) {
let pixels_per_point = self.pixels_per_point();
let fonts = self.read(|ctx| {
ctx.fonts
.get(&pixels_per_point.into())
.map(|current_fonts| current_fonts.lock().fonts.definitions().clone())
});
self.memory_mut(|mem| mem.new_font_definitions = fonts);
}

/// Tell `egui` which fonts to use.
///
/// The default `egui` fonts only support latin and cyrillic alphabets,
Expand Down Expand Up @@ -2066,19 +2059,10 @@ impl Context {
/// You can use [`Ui::style_mut`] to change the style of a single [`Ui`].
pub fn set_style_of(&self, theme: Theme, style: impl Into<Arc<Style>>) {
let style = style.into();
let mut recreate_font_atlas = false;
self.options_mut(|opt| {
let dest = match theme {
Theme::Dark => &mut opt.dark_style,
Theme::Light => &mut opt.light_style,
};
recreate_font_atlas =
dest.visuals.text_alpha_from_coverage != style.visuals.text_alpha_from_coverage;
*dest = style;
self.options_mut(|opt| match theme {
Theme::Dark => opt.dark_style = style,
Theme::Light => opt.light_style = style,
});
if recreate_font_atlas {
self.reset_font_atlas();
}
}

/// The [`crate::Visuals`] used by all subsequent windows, panels etc.
Expand Down Expand Up @@ -2475,28 +2459,7 @@ impl ContextImpl {
}

// Inform the backend of all textures that have been updated (including font atlas).
let textures_delta = {
// HACK to get much nicer looking text in light mode.
// This assumes all text is black-on-white in light mode,
// and white-on-black in dark mode, which is not necessarily true,
// but often close enough.
// Of course this fails for cases when there is black-on-white text in dark mode,
// and white-on-black text in light mode.

let text_alpha_from_coverage =
self.memory.options.style().visuals.text_alpha_from_coverage;

let mut textures_delta = self.tex_manager.0.write().take_delta();

for (_, delta) in &mut textures_delta.set {
if let ImageData::Font(font) = &mut delta.image {
delta.image =
ImageData::Color(font.to_color_image(text_alpha_from_coverage).into());
}
}

textures_delta
};
let textures_delta = self.tex_manager.0.write().take_delta();

let mut platform_output: PlatformOutput = std::mem::take(&mut viewport.output);

Expand Down Expand Up @@ -3094,17 +3057,9 @@ impl Context {

options.ui(ui);

let text_alpha_from_coverage_changed =
prev_options.style().visuals.text_alpha_from_coverage
!= options.style().visuals.text_alpha_from_coverage;

if options != prev_options {
self.options_mut(move |o| *o = options);
}

if text_alpha_from_coverage_changed {
ui.ctx().reset_font_atlas();
}
}

fn fonts_tweak_ui(&self, ui: &mut Ui) {
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ pub use emath::{
remap_clamp, vec2,
};
pub use epaint::{
ClippedPrimitive, ColorImage, CornerRadius, FontImage, ImageData, Margin, Mesh, PaintCallback,
ClippedPrimitive, ColorImage, CornerRadius, ImageData, Margin, Mesh, PaintCallback,
PaintCallbackInfo, Shadow, Shape, Stroke, StrokeKind, TextureHandle, TextureId, mutex,
text::{FontData, FontDefinitions, FontFamily, FontId, FontTweak},
textures::{TextureFilter, TextureOptions, TextureWrapMode, TexturesDelta},
Expand Down
7 changes: 6 additions & 1 deletion crates/egui_demo_lib/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let fonts = egui::epaint::text::Fonts::new(
pixels_per_point,
max_texture_side,
egui::epaint::AlphaFromCoverage::default(),
egui::FontDefinitions::default(),
);
{
Expand Down Expand Up @@ -210,7 +211,11 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let mut rng = rand::rng();
b.iter(|| {
fonts.begin_pass(pixels_per_point, max_texture_side);
fonts.begin_pass(
pixels_per_point,
max_texture_side,
egui::epaint::AlphaFromCoverage::default(),
);

// Delete a random character, simulating a user making an edit in a long file:
let mut new_string = string.clone();
Expand Down
17 changes: 0 additions & 17 deletions crates/egui_glow/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,23 +534,6 @@ impl Painter {

self.upload_texture_srgb(delta.pos, image.size, delta.options, data);
}
egui::ImageData::Font(image) => {
assert_eq!(
image.width() * image.height(),
image.pixels.len(),
"Mismatch between texture size and texel count"
);

let data: Vec<u8> = {
profiling::scope!("font -> sRGBA");
image
.srgba_pixels(Default::default())
.flat_map(|a| a.to_array())
.collect()
};

self.upload_texture_srgb(delta.pos, image.size, delta.options, &data);
}
};
}

Expand Down
6 changes: 3 additions & 3 deletions crates/epaint/benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use criterion::{Criterion, black_box, criterion_group, criterion_main};

use epaint::{
ClippedShape, Color32, Mesh, PathStroke, Pos2, Rect, Shape, Stroke, TessellationOptions,
Tessellator, TextureAtlas, Vec2, pos2, tessellator::Path,
AlphaFromCoverage, ClippedShape, Color32, Mesh, PathStroke, Pos2, Rect, Shape, Stroke,
TessellationOptions, Tessellator, TextureAtlas, Vec2, pos2, tessellator::Path,
};

#[global_allocator]
Expand Down Expand Up @@ -66,7 +66,7 @@ fn tessellate_circles(c: &mut Criterion) {
let pixels_per_point = 2.0;
let options = TessellationOptions::default();

let atlas = TextureAtlas::new([4096, 256]);
let atlas = TextureAtlas::new([4096, 256], AlphaFromCoverage::default());
let font_tex_size = atlas.size();
let prepared_discs = atlas.prepared_discs();

Expand Down
Loading
Loading