Browse Source

Rename `Texture` to `FontImage`

pull/1014/head
Emil Ernerfeldt 3 years ago
parent
commit
190c85a40f
  1. 16
      egui/src/context.rs
  2. 2
      egui/src/introspection.rs
  3. 2
      egui/src/lib.rs
  4. 6
      egui_demo_lib/benches/benchmark.rs
  5. 4
      egui_demo_lib/src/apps/demo/plot_demo.rs
  6. 2
      egui_glium/src/epi_backend.rs
  7. 2
      egui_glium/src/lib.rs
  8. 14
      egui_glium/src/painter.rs
  9. 2
      egui_glow/src/epi_backend.rs
  10. 2
      egui_glow/src/lib.rs
  11. 12
      egui_glow/src/painter.rs
  12. 3
      egui_web/src/backend.rs
  13. 6
      egui_web/src/glow_wrapping.rs
  14. 2
      egui_web/src/painter.rs
  15. 16
      egui_web/src/webgl1.rs
  16. 16
      egui_web/src/webgl2.rs
  17. 1
      epaint/CHANGELOG.md
  18. 2
      epaint/src/lib.rs
  19. 2
      epaint/src/text/font.rs
  20. 21
      epaint/src/text/fonts.rs
  21. 41
      epaint/src/texture_atlas.rs

16
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<epaint::Texture> {
self.fonts().texture()
pub fn font_image(&self) -> Arc<epaint::FontImage> {
self.fonts().font_image()
}
#[deprecated = "Renamed font_image"]
pub fn texture(&self) -> Arc<epaint::FontImage> {
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);
});

2
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;

2
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 {

6
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();
})
});

4
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,
],
);

2
egui_glium/src/epi_backend.rs

@ -86,7 +86,7 @@ pub fn run(app: Box<dyn epi::App>, 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();

2
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(),
);
}
}

14
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<Vec<(u8, u8, u8, u8)>> = texture
let pixels: Vec<Vec<(u8, u8, u8, u8)>> = 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::ClippedMesh>,
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);

2
egui_glow/src/epi_backend.rs

@ -99,7 +99,7 @@ pub fn run(app: Box<dyn epi::App>, 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(),

2
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,

12
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<u8> = texture
let pixels: Vec<u8> = 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(

3
egui_web/src/backend.rs

@ -214,7 +214,8 @@ impl AppRunner {
}
pub fn paint(&mut self, clipped_meshes: Vec<egui::ClippedMesh>) -> 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())?;

6
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) {

2
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);

16
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<u8> = Vec::with_capacity(texture.pixels.len() * 4);
for srgba in texture.srgba_pixels(gamma) {
let mut pixels: Vec<u8> = 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) {

16
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<u8> = Vec::with_capacity(texture.pixels.len() * 4);
for srgba in texture.srgba_pixels(1.0) {
let mut pixels: Vec<u8> = 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) {

1
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

2
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};

2
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;

21
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<TextStyle, Font>,
atlas: Arc<Mutex<TextureAtlas>>,
/// 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<Arc<Texture>>,
buffered_font_image: Mutex<Arc<FontImage>>,
galley_cache: Mutex<GalleyCache>,
}
@ -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<Texture> {
pub fn font_image(&self) -> Arc<FontImage> {
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()

41
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<u8>,
}
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)
}
}

Loading…
Cancel
Save