2020-12-27 22:09:51 +00:00
|
|
|
// TODO: `TextureData` or similar?
|
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)]
|
|
|
|
pub struct Texture {
|
|
|
|
/// e.g. a hash of the data. Use this to detect changes!
|
2020-09-09 11:32:40 +00:00
|
|
|
/// 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,
|
2021-01-03 17:03:11 +00:00
|
|
|
/// White color with the given alpha (linear space 0-255).
|
2019-01-16 23:09:12 +00:00
|
|
|
pub pixels: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:50:47 +00:00
|
|
|
impl Texture {
|
|
|
|
/// Returns the textures as `sRGBA` premultiplied pixels, row by row, top to bottom.
|
2021-01-02 16:02:18 +00:00
|
|
|
pub fn srgba_pixels(&'_ self) -> impl Iterator<Item = super::Color32> + '_ {
|
|
|
|
use super::Color32;
|
2021-01-03 17:03:11 +00:00
|
|
|
let srgba_from_luminance_lut: Vec<Color32> =
|
|
|
|
(0..=255).map(Color32::from_white_alpha).collect();
|
2020-11-20 18:50:47 +00:00
|
|
|
self.pixels
|
|
|
|
.iter()
|
|
|
|
.map(move |&l| srgba_from_luminance_lut[l as usize])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 23:09:12 +00:00
|
|
|
impl std::ops::Index<(usize, usize)> for Texture {
|
|
|
|
type Output = u8;
|
|
|
|
|
|
|
|
fn index(&self, (x, y): (usize, usize)) -> &u8 {
|
|
|
|
assert!(x < self.width);
|
|
|
|
assert!(y < self.height);
|
|
|
|
&self.pixels[y * self.width + x]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::IndexMut<(usize, usize)> for Texture {
|
|
|
|
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.
|
2019-01-12 23:10:53 +00:00
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct TextureAtlas {
|
2019-01-16 23:09:12 +00:00
|
|
|
texture: Texture,
|
2019-01-12 23:10:53 +00:00
|
|
|
|
2021-01-03 17:03:11 +00:00
|
|
|
/// Used for when allocating new rectangles.
|
2019-01-12 23:10:53 +00:00
|
|
|
cursor: (usize, usize),
|
|
|
|
row_height: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TextureAtlas {
|
|
|
|
pub fn new(width: usize, height: usize) -> Self {
|
2020-05-07 08:47:03 +00:00
|
|
|
Self {
|
2019-01-16 23:09:12 +00:00
|
|
|
texture: Texture {
|
2020-09-09 11:32:40 +00:00
|
|
|
version: 0,
|
2019-01-16 23:09:12 +00:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
pixels: vec![0; width * height],
|
|
|
|
},
|
2019-01-12 23:10:53 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 23:09:12 +00:00
|
|
|
pub fn texture(&self) -> &Texture {
|
|
|
|
&self.texture
|
2019-01-12 23:10:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 23:09:12 +00:00
|
|
|
pub fn texture_mut(&mut self) -> &mut Texture {
|
2020-09-09 11:32:40 +00:00
|
|
|
self.texture.version += 1;
|
2019-01-16 23:09:12 +00:00
|
|
|
&mut self.texture
|
2019-01-12 23:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the coordinates of where the rect ended up.
|
|
|
|
pub fn allocate(&mut self, (w, h): (usize, usize)) -> (usize, usize) {
|
2020-09-09 13:24:44 +00:00
|
|
|
/// On some low-precision GPUs (my old iPad) characters get muddled up
|
|
|
|
/// if we don't add some empty pixels between the characters.
|
2021-01-03 17:03:11 +00:00
|
|
|
/// On modern high-precision GPUs this is not needed.
|
2020-09-09 13:24:44 +00:00
|
|
|
const PADDING: usize = 1;
|
|
|
|
|
2019-01-16 23:09:12 +00:00
|
|
|
assert!(w <= self.texture.width);
|
|
|
|
if self.cursor.0 + w > self.texture.width {
|
2019-01-12 23:10:53 +00:00
|
|
|
// New row:
|
|
|
|
self.cursor.0 = 0;
|
2020-09-09 13:24:44 +00:00
|
|
|
self.cursor.1 += self.row_height + PADDING;
|
2019-01-12 23:10:53 +00:00
|
|
|
self.row_height = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.row_height = self.row_height.max(h);
|
2019-01-16 23:09:12 +00:00
|
|
|
while self.cursor.1 + self.row_height >= self.texture.height {
|
|
|
|
self.texture.height *= 2;
|
2019-01-12 23:10:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 23:09:12 +00:00
|
|
|
if self.texture.width * self.texture.height > self.texture.pixels.len() {
|
|
|
|
self.texture
|
|
|
|
.pixels
|
|
|
|
.resize(self.texture.width * self.texture.height, 0);
|
2019-01-12 23:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let pos = self.cursor;
|
2020-09-09 13:24:44 +00:00
|
|
|
self.cursor.0 += w + PADDING;
|
2020-09-09 11:32:40 +00:00
|
|
|
self.texture.version += 1;
|
2019-01-12 23:10:53 +00:00
|
|
|
(pos.0 as usize, pos.1 as usize)
|
|
|
|
}
|
|
|
|
}
|