mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 21:00:03 -04:00
Partial font texture update (#1149)
This commit is contained in:
@@ -374,14 +374,12 @@ fn allocate_glyph(
|
||||
if glyph_width == 0 || glyph_height == 0 {
|
||||
UvRect::default()
|
||||
} else {
|
||||
let glyph_pos = atlas.allocate((glyph_width, glyph_height));
|
||||
|
||||
let image = atlas.image_mut();
|
||||
let (glyph_pos, image) = atlas.allocate((glyph_width, glyph_height));
|
||||
glyph.draw(|x, y, v| {
|
||||
if v > 0.0 {
|
||||
let px = glyph_pos.0 + x as usize;
|
||||
let py = glyph_pos.1 + y as usize;
|
||||
image.image[(px, py)] = (v * 255.0).round() as u8;
|
||||
image[(px, py)] = (v * 255.0).round() as u8;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::{
|
||||
font::{Font, FontImpl},
|
||||
Galley, LayoutJob,
|
||||
},
|
||||
FontImage, TextureAtlas,
|
||||
TextureAtlas,
|
||||
};
|
||||
|
||||
// TODO: rename
|
||||
@@ -233,11 +233,6 @@ pub struct Fonts {
|
||||
definitions: FontDefinitions,
|
||||
fonts: BTreeMap<TextStyle, Font>,
|
||||
atlas: Arc<Mutex<TextureAtlas>>,
|
||||
|
||||
/// 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_font_image: Mutex<Arc<FontImage>>,
|
||||
|
||||
galley_cache: Mutex<GalleyCache>,
|
||||
}
|
||||
|
||||
@@ -257,9 +252,9 @@ impl Fonts {
|
||||
|
||||
{
|
||||
// Make the top left pixel fully white:
|
||||
let pos = atlas.allocate((1, 1));
|
||||
let (pos, image) = atlas.allocate((1, 1));
|
||||
assert_eq!(pos, (0, 0));
|
||||
atlas.image_mut().image[pos] = 255;
|
||||
image[pos] = 255;
|
||||
}
|
||||
|
||||
let atlas = Arc::new(Mutex::new(atlas));
|
||||
@@ -283,19 +278,11 @@ impl Fonts {
|
||||
})
|
||||
.collect();
|
||||
|
||||
{
|
||||
let mut atlas = atlas.lock();
|
||||
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.image);
|
||||
}
|
||||
|
||||
Self {
|
||||
pixels_per_point,
|
||||
definitions,
|
||||
fonts,
|
||||
atlas,
|
||||
buffered_font_image: Default::default(),
|
||||
galley_cache: Default::default(),
|
||||
}
|
||||
}
|
||||
@@ -319,15 +306,14 @@ impl Fonts {
|
||||
(point * self.pixels_per_point).floor() / self.pixels_per_point
|
||||
}
|
||||
|
||||
/// Call each frame to get the latest available font texture data.
|
||||
pub fn font_image(&self) -> Arc<FontImage> {
|
||||
let atlas = self.atlas.lock();
|
||||
let mut buffered_texture = self.buffered_font_image.lock();
|
||||
if buffered_texture.version != atlas.image().version {
|
||||
*buffered_texture = Arc::new(atlas.image().clone());
|
||||
}
|
||||
/// Call each frame to get the change to the font texture since last call.
|
||||
pub fn font_image_delta(&self) -> Option<crate::ImageDelta> {
|
||||
self.atlas.lock().take_delta()
|
||||
}
|
||||
|
||||
buffered_texture.clone()
|
||||
/// Current size of the font image
|
||||
pub fn font_image_size(&self) -> [usize; 2] {
|
||||
self.atlas.lock().size()
|
||||
}
|
||||
|
||||
/// Width of this character in points.
|
||||
|
||||
Reference in New Issue
Block a user