1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 23:13:13 -04:00

Rename Texture to FontImage

This commit is contained in:
Emil Ernerfeldt
2021-12-28 21:02:23 +01:00
parent d775eb3733
commit 190c85a40f
21 changed files with 93 additions and 81 deletions

View File

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

View File

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

View File

@@ -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()

View File

@@ -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)
}
}