diff --git a/egui/src/context.rs b/egui/src/context.rs index 0553dc1a8..493c2b06c 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -428,11 +428,17 @@ impl Context { .expect("No fonts available until first call to CtxRef::run()") } - /// The egui texture, containing font characters etc. + /// The egui font image, containing font characters etc. + /// /// Not valid until first call to [`CtxRef::run()`]. /// That's because since we don't know the proper `pixels_per_point` until then. - pub fn texture(&self) -> Arc { - self.fonts().texture() + pub fn font_image(&self) -> Arc { + self.fonts().font_image() + } + + #[deprecated = "Renamed font_image"] + pub fn texture(&self) -> Arc { + self.fonts().font_image() } /// Tell `egui` which fonts to use. @@ -657,7 +663,7 @@ impl Context { let clipped_meshes = tessellator::tessellate_shapes( shapes, tessellation_options, - self.fonts().texture().size(), + self.fonts().font_image().size(), ); *self.paint_stats.lock() = paint_stats.with_clipped_meshes(&clipped_meshes); clipped_meshes @@ -808,7 +814,7 @@ impl Context { .show(ui, |ui| { let mut font_definitions = self.fonts().definitions().clone(); font_definitions.ui(ui); - self.fonts().texture().ui(ui); + self.fonts().font_image().ui(ui); self.set_fonts(font_definitions); }); diff --git a/egui/src/introspection.rs b/egui/src/introspection.rs index d1da680b7..f6fe12737 100644 --- a/egui/src/introspection.rs +++ b/egui/src/introspection.rs @@ -1,7 +1,7 @@ //! uis for egui types. use crate::*; -impl Widget for &epaint::Texture { +impl Widget for &epaint::FontImage { fn ui(self, ui: &mut Ui) -> Response { use epaint::Mesh; diff --git a/egui/src/lib.rs b/egui/src/lib.rs index dc9cc5617..76ea75328 100644 --- a/egui/src/lib.rs +++ b/egui/src/lib.rs @@ -386,7 +386,7 @@ pub use emath::{lerp, pos2, remap, remap_clamp, vec2, Align, Align2, NumExt, Pos pub use epaint::{ color, mutex, text::{FontData, FontDefinitions, FontFamily, TextStyle}, - ClippedMesh, Color32, Rgba, Shape, Stroke, Texture, TextureId, + ClippedMesh, Color32, FontImage, Rgba, Shape, Stroke, TextureId, }; pub mod text { diff --git a/egui_demo_lib/benches/benchmark.rs b/egui_demo_lib/benches/benchmark.rs index cf2a4bc9a..97eb7f4b3 100644 --- a/egui_demo_lib/benches/benchmark.rs +++ b/egui_demo_lib/benches/benchmark.rs @@ -97,7 +97,11 @@ pub fn criterion_benchmark(c: &mut Criterion) { let text_shape = TextShape::new(egui::Pos2::ZERO, galley); c.bench_function("tessellate_text", |b| { b.iter(|| { - tessellator.tessellate_text(fonts.texture().size(), text_shape.clone(), &mut mesh); + tessellator.tessellate_text( + fonts.font_image().size(), + text_shape.clone(), + &mut mesh, + ); mesh.clear(); }) }); diff --git a/egui_demo_lib/src/apps/demo/plot_demo.rs b/egui_demo_lib/src/apps/demo/plot_demo.rs index 4bea5469c..6e060fe68 100644 --- a/egui_demo_lib/src/apps/demo/plot_demo.rs +++ b/egui_demo_lib/src/apps/demo/plot_demo.rs @@ -347,8 +347,8 @@ impl Widget for &mut ItemsDemo { TextureId::Egui, Value::new(0.0, 10.0), [ - ui.fonts().texture().width as f32 / 100.0, - ui.fonts().texture().height as f32 / 100.0, + ui.fonts().font_image().width as f32 / 100.0, + ui.fonts().font_image().height as f32 / 100.0, ], ); diff --git a/egui_glium/src/epi_backend.rs b/egui_glium/src/epi_backend.rs index 1dd52810c..001b6a660 100644 --- a/egui_glium/src/epi_backend.rs +++ b/egui_glium/src/epi_backend.rs @@ -86,7 +86,7 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { &mut target, integration.egui_ctx.pixels_per_point(), clipped_meshes, - &integration.egui_ctx.texture(), + &integration.egui_ctx.font_image(), ); target.finish().unwrap(); diff --git a/egui_glium/src/lib.rs b/egui_glium/src/lib.rs index c020b3725..69c9645f5 100644 --- a/egui_glium/src/lib.rs +++ b/egui_glium/src/lib.rs @@ -153,7 +153,7 @@ impl EguiGlium { target, self.egui_ctx.pixels_per_point(), clipped_meshes, - &self.egui_ctx.texture(), + &self.egui_ctx.font_image(), ); } } diff --git a/egui_glium/src/painter.rs b/egui_glium/src/painter.rs index e190171a5..d6735d637 100644 --- a/egui_glium/src/painter.rs +++ b/egui_glium/src/painter.rs @@ -65,15 +65,15 @@ impl Painter { pub fn upload_egui_texture( &mut self, facade: &dyn glium::backend::Facade, - texture: &egui::Texture, + font_image: &egui::FontImage, ) { - if self.egui_texture_version == Some(texture.version) { + if self.egui_texture_version == Some(font_image.version) { return; // No change } - let pixels: Vec> = texture + let pixels: Vec> = font_image .pixels - .chunks(texture.width as usize) + .chunks(font_image.width as usize) .map(|row| { row.iter() .map(|&a| Color32::from_white_alpha(a).to_tuple()) @@ -85,7 +85,7 @@ impl Painter { let mipmaps = texture::MipmapsOption::NoMipmap; self.egui_texture = Some(SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap()); - self.egui_texture_version = Some(texture.version); + self.egui_texture_version = Some(font_image.version); } /// Main entry-point for painting a frame. @@ -97,9 +97,9 @@ impl Painter { target: &mut T, pixels_per_point: f32, cipped_meshes: Vec, - egui_texture: &egui::Texture, + font_image: &egui::FontImage, ) { - self.upload_egui_texture(display, egui_texture); + self.upload_egui_texture(display, font_image); for egui::ClippedMesh(clip_rect, mesh) in cipped_meshes { self.paint_mesh(target, display, pixels_per_point, clip_rect, &mesh); diff --git a/egui_glow/src/epi_backend.rs b/egui_glow/src/epi_backend.rs index 73afcde63..8b48ee7a6 100644 --- a/egui_glow/src/epi_backend.rs +++ b/egui_glow/src/epi_backend.rs @@ -99,7 +99,7 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { gl.clear_color(color[0], color[1], color[2], color[3]); gl.clear(glow::COLOR_BUFFER_BIT); } - painter.upload_egui_texture(&gl, &integration.egui_ctx.texture()); + painter.upload_egui_texture(&gl, &integration.egui_ctx.font_image()); painter.paint_meshes( &gl, gl_window.window().inner_size().into(), diff --git a/egui_glow/src/lib.rs b/egui_glow/src/lib.rs index fcf821794..c7878585b 100644 --- a/egui_glow/src/lib.rs +++ b/egui_glow/src/lib.rs @@ -164,7 +164,7 @@ impl EguiGlow { let clipped_meshes = self.egui_ctx.tessellate(shapes); let dimensions: [u32; 2] = gl_window.window().inner_size().into(); self.painter - .upload_egui_texture(gl, &self.egui_ctx.texture()); + .upload_egui_texture(gl, &self.egui_ctx.font_image()); self.painter.paint_meshes( gl, dimensions, diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index b2bb84cf2..49f35eb2e 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -202,10 +202,10 @@ impl Painter { } } - pub fn upload_egui_texture(&mut self, gl: &glow::Context, texture: &egui::Texture) { + pub fn upload_egui_texture(&mut self, gl: &glow::Context, font_image: &egui::FontImage) { self.assert_not_destroyed(); - if self.egui_texture_version == Some(texture.version) { + if self.egui_texture_version == Some(font_image.version) { return; // No change } let gamma = if self.is_embedded && self.post_process.is_none() { @@ -213,7 +213,7 @@ impl Painter { } else { 1.0 }; - let pixels: Vec = texture + let pixels: Vec = font_image .srgba_pixels(gamma) .flat_map(|a| Vec::from(a.to_array())) .collect(); @@ -225,15 +225,15 @@ impl Painter { self.is_webgl_1, self.srgb_support, &pixels, - texture.width, - texture.height, + font_image.width, + font_image.height, )), ) { unsafe { gl.delete_texture(old_tex); } } - self.egui_texture_version = Some(texture.version); + self.egui_texture_version = Some(font_image.version); } unsafe fn prepare_painting( diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index eda65ee63..4ed7afbd8 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -214,7 +214,8 @@ impl AppRunner { } pub fn paint(&mut self, clipped_meshes: Vec) -> Result<(), JsValue> { - self.painter.upload_egui_texture(&self.egui_ctx.texture()); + self.painter + .upload_egui_texture(&self.egui_ctx.font_image()); self.painter.clear(self.app.clear_color()); self.painter .paint_meshes(clipped_meshes, self.egui_ctx.pixels_per_point())?; diff --git a/egui_web/src/glow_wrapping.rs b/egui_web/src/glow_wrapping.rs index 5709774de..c7d8426b6 100644 --- a/egui_web/src/glow_wrapping.rs +++ b/egui_web/src/glow_wrapping.rs @@ -1,5 +1,5 @@ use crate::{canvas_element_or_die, console_error}; -use egui::{ClippedMesh, Rgba, Texture}; +use egui::{ClippedMesh, FontImage, Rgba}; use egui_glow::glow; use wasm_bindgen::JsCast; use wasm_bindgen::JsValue; @@ -86,8 +86,8 @@ impl crate::Painter for WrappedGlowPainter { &self.canvas_id } - fn upload_egui_texture(&mut self, texture: &Texture) { - self.painter.upload_egui_texture(&self.gl_ctx, texture) + fn upload_egui_texture(&mut self, font_image: &FontImage) { + self.painter.upload_egui_texture(&self.gl_ctx, font_image) } fn clear(&mut self, clear_color: Rgba) { diff --git a/egui_web/src/painter.rs b/egui_web/src/painter.rs index 079b9fd57..269761c8c 100644 --- a/egui_web/src/painter.rs +++ b/egui_web/src/painter.rs @@ -10,7 +10,7 @@ pub trait Painter { /// id of the canvas html element containing the rendering fn canvas_id(&self) -> &str; - fn upload_egui_texture(&mut self, texture: &egui::Texture); + fn upload_egui_texture(&mut self, font_image: &egui::FontImage); fn clear(&mut self, clear_color: egui::Rgba); diff --git a/egui_web/src/webgl1.rs b/egui_web/src/webgl1.rs index 39cba041e..a1c4f8eb5 100644 --- a/egui_web/src/webgl1.rs +++ b/egui_web/src/webgl1.rs @@ -11,7 +11,7 @@ use { use egui::{ emath::vec2, - epaint::{Color32, Texture}, + epaint::{Color32, FontImage}, }; type Gl = WebGlRenderingContext; @@ -323,8 +323,8 @@ impl crate::Painter for WebGlPainter { &self.canvas_id } - fn upload_egui_texture(&mut self, texture: &Texture) { - if self.egui_texture_version == Some(texture.version) { + fn upload_egui_texture(&mut self, font_image: &FontImage) { + if self.egui_texture_version == Some(font_image.version) { return; // No change } @@ -333,8 +333,8 @@ impl crate::Painter for WebGlPainter { } else { 1.0 // post process enables linear blending }; - let mut pixels: Vec = Vec::with_capacity(texture.pixels.len() * 4); - for srgba in texture.srgba_pixels(gamma) { + let mut pixels: Vec = Vec::with_capacity(font_image.pixels.len() * 4); + for srgba in font_image.srgba_pixels(gamma) { pixels.push(srgba.r()); pixels.push(srgba.g()); pixels.push(srgba.b()); @@ -353,8 +353,8 @@ impl crate::Painter for WebGlPainter { Gl::TEXTURE_2D, level, internal_format as i32, - texture.width as i32, - texture.height as i32, + font_image.width as i32, + font_image.height as i32, border, src_format, src_type, @@ -362,7 +362,7 @@ impl crate::Painter for WebGlPainter { ) .unwrap(); - self.egui_texture_version = Some(texture.version); + self.egui_texture_version = Some(font_image.version); } fn clear(&mut self, clear_color: egui::Rgba) { diff --git a/egui_web/src/webgl2.rs b/egui_web/src/webgl2.rs index 207bc833e..b5f2e29b2 100644 --- a/egui_web/src/webgl2.rs +++ b/egui_web/src/webgl2.rs @@ -12,7 +12,7 @@ use { use egui::{ emath::vec2, - epaint::{Color32, Texture}, + epaint::{Color32, FontImage}, }; type Gl = WebGl2RenderingContext; @@ -307,13 +307,13 @@ impl crate::Painter for WebGl2Painter { &self.canvas_id } - fn upload_egui_texture(&mut self, texture: &Texture) { - if self.egui_texture_version == Some(texture.version) { + fn upload_egui_texture(&mut self, font_image: &FontImage) { + if self.egui_texture_version == Some(font_image.version) { return; // No change } - let mut pixels: Vec = Vec::with_capacity(texture.pixels.len() * 4); - for srgba in texture.srgba_pixels(1.0) { + let mut pixels: Vec = Vec::with_capacity(font_image.pixels.len() * 4); + for srgba in font_image.srgba_pixels(1.0) { pixels.push(srgba.r()); pixels.push(srgba.g()); pixels.push(srgba.b()); @@ -333,8 +333,8 @@ impl crate::Painter for WebGl2Painter { Gl::TEXTURE_2D, level, internal_format as i32, - texture.width as i32, - texture.height as i32, + font_image.width as i32, + font_image.height as i32, border, src_format, src_type, @@ -342,7 +342,7 @@ impl crate::Painter for WebGl2Painter { ) .unwrap(); - self.egui_texture_version = Some(texture.version); + self.egui_texture_version = Some(font_image.version); } fn clear(&mut self, clear_color: egui::Rgba) { diff --git a/epaint/CHANGELOG.md b/epaint/CHANGELOG.md index f21489251..12b8f7b87 100644 --- a/epaint/CHANGELOG.md +++ b/epaint/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to the epaint crate will be documented in this file. ## Unreleased * `Rgba` now implements `Hash` ([#886](https://github.com/emilk/egui/pull/886)). * Anti-alias path ends ([#893](https://github.com/emilk/egui/pull/893)). +* Rename `Texture` to `FontImage`. ## 0.15.0 - 2021-10-24 diff --git a/epaint/src/lib.rs b/epaint/src/lib.rs index 046a911ee..3799cd4e5 100644 --- a/epaint/src/lib.rs +++ b/epaint/src/lib.rs @@ -109,7 +109,7 @@ pub use { stroke::Stroke, tessellator::{tessellate_shapes, TessellationOptions, Tessellator}, text::{Fonts, Galley, TextStyle}, - texture_atlas::{Texture, TextureAtlas}, + texture_atlas::{FontImage, TextureAtlas}, }; pub use emath::{pos2, vec2, Pos2, Rect, Vec2}; diff --git a/epaint/src/text/font.rs b/epaint/src/text/font.rs index e97c478dd..966a4f4a1 100644 --- a/epaint/src/text/font.rs +++ b/epaint/src/text/font.rs @@ -377,7 +377,7 @@ fn allocate_glyph( } else { let glyph_pos = atlas.allocate((glyph_width, glyph_height)); - let texture = atlas.texture_mut(); + let texture = atlas.image_mut(); glyph.draw(|x, y, v| { if v > 0.0 { let px = glyph_pos.0 + x as usize; diff --git a/epaint/src/text/fonts.rs b/epaint/src/text/fonts.rs index 8fb6d9f03..806e8fe93 100644 --- a/epaint/src/text/fonts.rs +++ b/epaint/src/text/fonts.rs @@ -6,7 +6,7 @@ use crate::{ font::{Font, FontImpl}, Galley, LayoutJob, }, - Texture, TextureAtlas, + FontImage, TextureAtlas, }; // TODO: rename @@ -233,9 +233,10 @@ pub struct Fonts { definitions: FontDefinitions, fonts: BTreeMap, atlas: Arc>, - /// Copy of the texture in the texture atlas. + + /// Copy of the font image in the texture atlas. /// This is so we can return a reference to it (the texture atlas is behind a lock). - buffered_texture: Mutex>, + buffered_font_image: Mutex>, galley_cache: Mutex, } @@ -258,7 +259,7 @@ impl Fonts { // Make the top left pixel fully white: let pos = atlas.allocate((1, 1)); assert_eq!(pos, (0, 0)); - atlas.texture_mut()[pos] = 255; + atlas.image_mut()[pos] = 255; } let atlas = Arc::new(Mutex::new(atlas)); @@ -284,7 +285,7 @@ impl Fonts { { let mut atlas = atlas.lock(); - let texture = atlas.texture_mut(); + let texture = atlas.image_mut(); // Make sure we seed the texture version with something unique based on the default characters: texture.version = crate::util::hash(&texture.pixels); } @@ -294,7 +295,7 @@ impl Fonts { definitions, fonts, atlas, - buffered_texture: Default::default(), //atlas.lock().texture().clone(); + buffered_font_image: Default::default(), //atlas.lock().texture().clone(); galley_cache: Default::default(), } } @@ -319,11 +320,11 @@ impl Fonts { } /// Call each frame to get the latest available font texture data. - pub fn texture(&self) -> Arc { + pub fn font_image(&self) -> Arc { let atlas = self.atlas.lock(); - let mut buffered_texture = self.buffered_texture.lock(); - if buffered_texture.version != atlas.texture().version { - *buffered_texture = Arc::new(atlas.texture().clone()); + let mut buffered_texture = self.buffered_font_image.lock(); + if buffered_texture.version != atlas.image().version { + *buffered_texture = Arc::new(atlas.image().clone()); } buffered_texture.clone() diff --git a/epaint/src/texture_atlas.rs b/epaint/src/texture_atlas.rs index 3c45aba7c..054a2c1c6 100644 --- a/epaint/src/texture_atlas.rs +++ b/epaint/src/texture_atlas.rs @@ -1,7 +1,6 @@ -// TODO: `TextureData` or similar? /// An 8-bit texture containing font data. #[derive(Clone, Default)] -pub struct Texture { +pub struct FontImage { /// e.g. a hash of the data. Use this to detect changes! /// If the texture changes, this too will change. pub version: u64, @@ -11,7 +10,7 @@ pub struct Texture { pub pixels: Vec, } -impl Texture { +impl FontImage { pub fn size(&self) -> [usize; 2] { [self.width, self.height] } @@ -36,7 +35,7 @@ impl Texture { } } -impl std::ops::Index<(usize, usize)> for Texture { +impl std::ops::Index<(usize, usize)> for FontImage { type Output = u8; #[inline] @@ -47,7 +46,7 @@ impl std::ops::Index<(usize, usize)> for Texture { } } -impl std::ops::IndexMut<(usize, usize)> for Texture { +impl std::ops::IndexMut<(usize, usize)> for FontImage { #[inline] fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut u8 { assert!(x < self.width); @@ -61,7 +60,7 @@ impl std::ops::IndexMut<(usize, usize)> for Texture { /// More characters can be added, possibly expanding the texture. #[derive(Clone, Default)] pub struct TextureAtlas { - texture: Texture, + image: FontImage, /// Used for when allocating new rectangles. cursor: (usize, usize), @@ -71,7 +70,7 @@ pub struct TextureAtlas { impl TextureAtlas { pub fn new(width: usize, height: usize) -> Self { Self { - texture: Texture { + image: FontImage { version: 0, width, height, @@ -81,13 +80,13 @@ impl TextureAtlas { } } - pub fn texture(&self) -> &Texture { - &self.texture + pub fn image(&self) -> &FontImage { + &self.image } - pub fn texture_mut(&mut self) -> &mut Texture { - self.texture.version += 1; - &mut self.texture + pub fn image_mut(&mut self) -> &mut FontImage { + self.image.version += 1; + &mut self.image } /// Returns the coordinates of where the rect ended up. @@ -98,12 +97,12 @@ impl TextureAtlas { const PADDING: usize = 1; assert!( - w <= self.texture.width, + w <= self.image.width, "Tried to allocate a {} wide glyph in a {} wide texture atlas", w, - self.texture.width + self.image.width ); - if self.cursor.0 + w > self.texture.width { + if self.cursor.0 + w > self.image.width { // New row: self.cursor.0 = 0; self.cursor.1 += self.row_height + PADDING; @@ -111,19 +110,19 @@ impl TextureAtlas { } self.row_height = self.row_height.max(h); - while self.cursor.1 + self.row_height >= self.texture.height { - self.texture.height *= 2; + while self.cursor.1 + self.row_height >= self.image.height { + self.image.height *= 2; } - if self.texture.width * self.texture.height > self.texture.pixels.len() { - self.texture + if self.image.width * self.image.height > self.image.pixels.len() { + self.image .pixels - .resize(self.texture.width * self.texture.height, 0); + .resize(self.image.width * self.image.height, 0); } let pos = self.cursor; self.cursor.0 += w + PADDING; - self.texture.version += 1; + self.image.version += 1; (pos.0 as usize, pos.1 as usize) } }