2022-01-15 12:59:52 +00:00
|
|
|
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!
|
2020-09-09 11:32:40 +00:00
|
|
|
/// If the texture changes, this too will change.
|
|
|
|
pub version: u64,
|
2022-01-15 12:59:52 +00:00
|
|
|
|
|
|
|
/// 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 {
|
2022-01-15 12:59:52 +00:00
|
|
|
#[inline]
|
2021-03-30 19:41:39 +00:00
|
|
|
pub fn size(&self) -> [usize; 2] {
|
2022-01-15 12:59:52 +00:00
|
|
|
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]
|
2022-01-15 12:59:52 +00:00
|
|
|
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]
|
2022-01-15 12:59:52 +00:00
|
|
|
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.
|
2019-01-12 23:10:53 +00:00
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct TextureAtlas {
|
2021-12-28 20:02:23 +00:00
|
|
|
image: FontImage,
|
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 {
|
2022-01-15 12:59:52 +00:00
|
|
|
pub fn new(size: [usize; 2]) -> Self {
|
2020-05-07 08:47:03 +00:00
|
|
|
Self {
|
2021-12-28 20:02:23 +00:00
|
|
|
image: FontImage {
|
2020-09-09 11:32:40 +00:00
|
|
|
version: 0,
|
2022-01-15 12:59:52 +00:00
|
|
|
image: AlphaImage::new(size),
|
2019-01-16 23:09:12 +00:00
|
|
|
},
|
2019-01-12 23:10:53 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 20:02:23 +00:00
|
|
|
pub fn image(&self) -> &FontImage {
|
|
|
|
&self.image
|
2019-01-12 23:10:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 20:02:23 +00:00
|
|
|
pub fn image_mut(&mut self) -> &mut FontImage {
|
|
|
|
self.image.version += 1;
|
|
|
|
&mut self.image
|
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;
|
|
|
|
|
2021-02-23 11:03:20 +00:00
|
|
|
assert!(
|
2022-01-15 12:59:52 +00:00
|
|
|
w <= self.image.width(),
|
2021-02-23 11:03:20 +00:00
|
|
|
"Tried to allocate a {} wide glyph in a {} wide texture atlas",
|
|
|
|
w,
|
2022-01-15 12:59:52 +00:00
|
|
|
self.image.width()
|
2021-02-23 11:03:20 +00:00
|
|
|
);
|
2022-01-15 12:59:52 +00:00
|
|
|
if self.cursor.0 + w > self.image.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);
|
2022-01-15 12:59:52 +00:00
|
|
|
resize_to_min_height(&mut self.image.image, self.cursor.1 + self.row_height);
|
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;
|
2021-12-28 20:02:23 +00:00
|
|
|
self.image.version += 1;
|
2019-01-12 23:10:53 +00:00
|
|
|
(pos.0 as usize, pos.1 as usize)
|
|
|
|
}
|
|
|
|
}
|
2022-01-15 12:59:52 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|