egui/epaint/src/texture_atlas.rs

102 lines
2.7 KiB
Rust
Raw Normal View History

use crate::image::AlphaImage;
2020-07-23 12:35:12 +00:00
/// An 8-bit texture containing font data.
2019-01-16 23:09:12 +00:00
#[derive(Clone, Default)]
2021-12-28 20:02:23 +00:00
pub struct FontImage {
2019-01-16 23:09:12 +00:00
/// e.g. a hash of the data. Use this to detect changes!
/// If the texture changes, this too will change.
pub version: u64,
/// The actual image data.
pub image: AlphaImage,
2019-01-16 23:09:12 +00:00
}
2021-12-28 20:02:23 +00:00
impl FontImage {
#[inline]
pub fn size(&self) -> [usize; 2] {
self.image.size
2020-11-20 18:50:47 +00:00
}
2019-01-16 23:09:12 +00:00
2021-04-01 20:10:34 +00:00
#[inline]
pub fn width(&self) -> usize {
self.image.size[0]
2019-01-16 23:09:12 +00:00
}
2021-04-01 20:10:34 +00:00
#[inline]
pub fn height(&self) -> usize {
self.image.size[1]
2019-01-16 23:09:12 +00:00
}
}
2020-07-23 12:35:12 +00:00
/// Contains font data in an atlas, where each character occupied a small rectangle.
///
/// More characters can be added, possibly expanding the texture.
#[derive(Clone, Default)]
pub struct TextureAtlas {
2021-12-28 20:02:23 +00:00
image: FontImage,
/// Used for when allocating new rectangles.
cursor: (usize, usize),
row_height: usize,
}
impl TextureAtlas {
pub fn new(size: [usize; 2]) -> Self {
Self {
2021-12-28 20:02:23 +00:00
image: FontImage {
version: 0,
image: AlphaImage::new(size),
2019-01-16 23:09:12 +00:00
},
..Default::default()
}
}
2021-12-28 20:02:23 +00:00
pub fn image(&self) -> &FontImage {
&self.image
}
2021-12-28 20:02:23 +00:00
pub fn image_mut(&mut self) -> &mut FontImage {
self.image.version += 1;
&mut self.image
}
/// Returns the coordinates of where the rect ended up.
pub fn allocate(&mut self, (w, h): (usize, usize)) -> (usize, usize) {
/// On some low-precision GPUs (my old iPad) characters get muddled up
/// if we don't add some empty pixels between the characters.
/// On modern high-precision GPUs this is not needed.
const PADDING: usize = 1;
assert!(
w <= self.image.width(),
"Tried to allocate a {} wide glyph in a {} wide texture atlas",
w,
self.image.width()
);
if self.cursor.0 + w > self.image.width() {
// New row:
self.cursor.0 = 0;
self.cursor.1 += self.row_height + PADDING;
self.row_height = 0;
}
self.row_height = self.row_height.max(h);
resize_to_min_height(&mut self.image.image, self.cursor.1 + self.row_height);
let pos = self.cursor;
self.cursor.0 += w + PADDING;
2021-12-28 20:02:23 +00:00
self.image.version += 1;
(pos.0 as usize, pos.1 as usize)
}
}
fn resize_to_min_height(image: &mut AlphaImage, min_height: usize) {
while min_height >= image.height() {
image.size[1] *= 2; // double the height
}
if image.width() * image.height() > image.pixels.len() {
image.pixels.resize(image.width() * image.height(), 0);
}
}