egui/epaint/src/texture_atlas.rs

131 lines
3.9 KiB
Rust
Raw Normal View History

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,
2019-01-16 23:09:12 +00:00
pub width: usize,
pub height: usize,
2022-01-08 10:15:05 +00:00
/// The alpha (linear space 0-255) of something white.
///
/// One byte per pixel. Often you want to use [`Self::srgba_pixels`] instead.
2019-01-16 23:09:12 +00:00
pub pixels: Vec<u8>,
}
2021-12-28 20:02:23 +00:00
impl FontImage {
pub fn size(&self) -> [usize; 2] {
[self.width, self.height]
}
2020-11-20 18:50:47 +00:00
/// Returns the textures as `sRGBA` premultiplied pixels, row by row, top to bottom.
///
/// `gamma` should normally be set to 1.0.
/// If you are having problems with egui text looking skinny and pixelated, try
/// setting a lower gamma, e.g. `0.5`.
pub fn srgba_pixels(&'_ self, gamma: f32) -> impl Iterator<Item = super::Color32> + '_ {
2021-01-02 16:02:18 +00:00
use super::Color32;
let srgba_from_luminance_lut: Vec<Color32> = (0..=255)
.map(|a| {
let a = super::color::linear_f32_from_linear_u8(a).powf(gamma);
super::Rgba::from_white_alpha(a).into()
})
.collect();
2020-11-20 18:50:47 +00:00
self.pixels
.iter()
.map(move |&l| srgba_from_luminance_lut[l as usize])
}
}
2021-12-28 20:02:23 +00:00
impl std::ops::Index<(usize, usize)> for FontImage {
2019-01-16 23:09:12 +00:00
type Output = u8;
2021-04-01 20:10:34 +00:00
#[inline]
2019-01-16 23:09:12 +00:00
fn index(&self, (x, y): (usize, usize)) -> &u8 {
assert!(x < self.width);
assert!(y < self.height);
&self.pixels[y * self.width + x]
}
}
2021-12-28 20:02:23 +00:00
impl std::ops::IndexMut<(usize, usize)> for FontImage {
2021-04-01 20:10:34 +00:00
#[inline]
2019-01-16 23:09:12 +00:00
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut u8 {
assert!(x < self.width);
assert!(y < self.height);
&mut self.pixels[y * self.width + x]
}
}
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(width: usize, height: usize) -> Self {
Self {
2021-12-28 20:02:23 +00:00
image: FontImage {
version: 0,
2019-01-16 23:09:12 +00:00
width,
height,
pixels: vec![0; width * height],
},
..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!(
2021-12-28 20:02:23 +00:00
w <= self.image.width,
"Tried to allocate a {} wide glyph in a {} wide texture atlas",
w,
2021-12-28 20:02:23 +00:00
self.image.width
);
2021-12-28 20:02:23 +00:00
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);
2021-12-28 20:02:23 +00:00
while self.cursor.1 + self.row_height >= self.image.height {
self.image.height *= 2;
}
2021-12-28 20:02:23 +00:00
if self.image.width * self.image.height > self.image.pixels.len() {
self.image
2019-01-16 23:09:12 +00:00
.pixels
2021-12-28 20:02:23 +00:00
.resize(self.image.width * self.image.height, 0);
}
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)
}
}