use glam::Vec2;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GlyphMetrics {
pub width: u32,
pub height: u32,
pub bearing_x: i32,
pub bearing_y: i32,
pub advance: f32,
}
impl GlyphMetrics {
#[must_use]
pub fn rect(&self, cursor: Vec2) -> (Vec2, Vec2) {
self.rect_scaled(cursor, 1.0)
}
#[must_use]
pub fn rect_scaled(&self, cursor: Vec2, scale: f32) -> (Vec2, Vec2) {
let min = Vec2::new(
cursor.x + self.bearing_x as f32 * scale,
cursor.y - self.bearing_y as f32 * scale,
);
let max = Vec2::new(
min.x + self.width as f32 * scale,
min.y + self.height as f32 * scale,
);
(min, max)
}
}
pub struct FontAtlas {
metrics: Vec<GlyphMetrics>,
pub texture_size: (u32, u32),
}
pub const ASCII_GLYPH_COUNT: usize = 95;
pub const NOTDEF_INDEX: usize = ASCII_GLYPH_COUNT;
pub const GLYPH_COUNT: usize = ASCII_GLYPH_COUNT + 1;
pub const FIRST_CHAR: u8 = 32;
pub const LAST_CHAR: u8 = 126;
pub const GLYPH_WIDTH: u32 = 8;
pub const GLYPH_HEIGHT: u32 = 13;
pub const GLYPH_ADVANCE: f32 = 10.0;
pub const LINE_HEIGHT: f32 = 16.0;
pub const ASCENDER: i32 = 10;
#[must_use]
pub fn glyph_index(c: char) -> usize {
let cp = c as u32;
if cp >= FIRST_CHAR as u32 && cp <= LAST_CHAR as u32 {
(cp - FIRST_CHAR as u32) as usize
} else {
NOTDEF_INDEX
}
}
static EMPTY_GLYPH: GlyphMetrics = GlyphMetrics {
width: 0,
height: 0,
bearing_x: 0,
bearing_y: 0,
advance: 0.0,
};
impl FontAtlas {
#[must_use]
pub fn built_in() -> Self {
let mut metrics: Vec<GlyphMetrics> = (FIRST_CHAR..=LAST_CHAR)
.map(|c| {
let w = if c <= b' ' { 0 } else { GLYPH_WIDTH };
GlyphMetrics {
width: w,
height: GLYPH_HEIGHT,
bearing_x: 1,
bearing_y: ASCENDER,
advance: GLYPH_ADVANCE,
}
})
.collect();
metrics.push(GlyphMetrics {
width: GLYPH_WIDTH,
height: GLYPH_HEIGHT,
bearing_x: 1,
bearing_y: ASCENDER,
advance: GLYPH_ADVANCE,
});
let texture_size = (GLYPH_WIDTH * GLYPH_COUNT as u32, GLYPH_HEIGHT);
Self {
metrics,
texture_size,
}
}
#[must_use]
pub fn glyph(&self, c: char) -> &GlyphMetrics {
let idx = glyph_index(c);
self.metrics
.get(idx)
.or_else(|| self.metrics.get(NOTDEF_INDEX))
.or_else(|| self.metrics.first())
.unwrap_or(&EMPTY_GLYPH)
}
#[must_use]
pub fn text_width(&self, text: &str, scale: f32) -> f32 {
let mut widest = 0.0f32;
let mut line = 0.0f32;
for c in text.chars() {
if c == '\n' {
widest = widest.max(line);
line = 0.0;
continue;
}
line += self.glyph(c).advance * scale;
}
widest.max(line)
}
#[must_use]
pub fn line_count(&self, text: &str) -> usize {
1 + text.chars().filter(|&c| c == '\n').count()
}
pub fn layout_line(&self, text: &str, pos: Vec2, scale: f32) -> Vec<(char, Vec2, Vec2)> {
let mut out = Vec::with_capacity(text.len());
out.extend(self.glyph_positions(text, pos, scale));
out
}
pub(crate) fn glyph_positions<'a>(
&'a self,
text: &'a str,
pos: Vec2,
scale: f32,
) -> impl Iterator<Item = (char, Vec2, Vec2)> + 'a {
let mut cursor = Vec2::new(pos.x, pos.y + ASCENDER as f32 * scale);
text.chars().filter_map(move |c| {
if c == '\n' {
cursor.x = pos.x;
cursor.y += LINE_HEIGHT * scale;
return None;
}
let g = self.glyph(c);
let quad = if g.width > 0 {
let (min, max) = g.rect_scaled(cursor, scale);
Some((c, min, max))
} else {
None
};
cursor.x += g.advance * scale;
quad
})
}
#[must_use]
pub fn len(&self) -> usize {
self.metrics.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.metrics.is_empty()
}
#[must_use]
pub fn glyph_u_min(&self, c: char) -> f32 {
let atlas_w = self.texture_size.0 as f32;
if atlas_w == 0.0 {
return 0.0;
}
let col = glyph_index(c) as u32;
(col * GLYPH_WIDTH) as f32 / atlas_w
}
#[must_use]
pub fn glyph_u_max(&self, c: char) -> f32 {
let atlas_w = self.texture_size.0 as f32;
if atlas_w == 0.0 {
return 0.0;
}
let col = glyph_index(c) as u32;
((col + 1) * GLYPH_WIDTH) as f32 / atlas_w
}
#[must_use]
pub fn glyph_bitmap(&self) -> (u32, u32, Vec<u8>) {
let (w, h) = self.texture_size;
let mut data = vec![0u8; (w * h) as usize];
for glyph_idx in 0..GLYPH_COUNT {
let rows = glyph_rows_at(glyph_idx);
let x_offset = glyph_idx as u32 * GLYPH_WIDTH;
for (row, &row_bits) in rows.iter().enumerate() {
for col in 0..GLYPH_WIDTH as usize {
let bit = (row_bits >> (7 - col)) & 1;
let px = (x_offset + col as u32) as usize;
let py = row;
data[py * w as usize + px] = if bit != 0 { 255 } else { 0 };
}
}
}
(w, h, data)
}
}
const NOTDEF_ROWS: [u8; 13] = [
0xFE, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00,
];
fn glyph_rows_at(index: usize) -> [u8; 13] {
if index < ASCII_GLYPH_COUNT {
glyph_rows(char::from(FIRST_CHAR + index as u8))
} else {
NOTDEF_ROWS
}
}
fn glyph_rows(c: char) -> [u8; 13] {
match c {
' ' => [0x00; 13],
'!' => [
0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00,
],
'"' => [
0x6C, 0x6C, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'#' => [
0x00, 0x6C, 0x6C, 0xFE, 0x6C, 0x6C, 0xFE, 0x6C, 0x6C, 0x00, 0x00, 0x00, 0x00,
],
'$' => [
0x10, 0x7C, 0xD6, 0xD0, 0x7C, 0x16, 0xD6, 0x7C, 0x10, 0x00, 0x00, 0x00, 0x00,
],
'%' => [
0x00, 0xE6, 0xA6, 0xEC, 0x18, 0x30, 0x6E, 0xCA, 0xCE, 0x00, 0x00, 0x00, 0x00,
],
'&' => [
0x00, 0x38, 0x6C, 0x6C, 0x38, 0x76, 0xDC, 0xCC, 0x76, 0x00, 0x00, 0x00, 0x00,
],
'\'' => [
0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'(' => [
0x0C, 0x18, 0x30, 0x30, 0x30, 0x30, 0x30, 0x18, 0x0C, 0x00, 0x00, 0x00, 0x00,
],
')' => [
0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x18, 0x30, 0x00, 0x00, 0x00, 0x00,
],
'*' => [
0x00, 0x00, 0x10, 0xD6, 0x7C, 0x38, 0x7C, 0xD6, 0x10, 0x00, 0x00, 0x00, 0x00,
],
'+' => [
0x00, 0x00, 0x00, 0x18, 0x18, 0x7E, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
],
',' => [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30, 0x00, 0x00, 0x00,
],
'-' => [
0x00, 0x00, 0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'.' => [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00,
],
'/' => [
0x06, 0x0C, 0x0C, 0x18, 0x18, 0x30, 0x30, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00,
],
'0' => [
0x7C, 0xC6, 0xCE, 0xDE, 0xF6, 0xE6, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'1' => [
0x18, 0x38, 0x78, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'2' => [
0x7C, 0xC6, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'3' => [
0x7C, 0xC6, 0x06, 0x3C, 0x06, 0x06, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'4' => [
0x0C, 0x1C, 0x3C, 0x6C, 0xCC, 0xFE, 0x0C, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'5' => [
0xFE, 0xC0, 0xC0, 0xFC, 0x06, 0x06, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'6' => [
0x3C, 0x60, 0xC0, 0xFC, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'7' => [
0xFE, 0x06, 0x0C, 0x18, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'8' => [
0x7C, 0xC6, 0xC6, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'9' => [
0x7C, 0xC6, 0xC6, 0xC6, 0x7E, 0x06, 0x0C, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00,
],
':' => [
0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
],
';' => [
0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x30, 0x00, 0x00, 0x00, 0x00,
],
'<' => [
0x00, 0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'=' => [
0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'>' => [
0x00, 0x60, 0x30, 0x18, 0x0C, 0x18, 0x30, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'?' => [
0x7C, 0xC6, 0x06, 0x0C, 0x18, 0x18, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00,
],
'@' => [
0x7C, 0xC6, 0xDE, 0xDE, 0xDE, 0xDC, 0xC0, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'A' => [
0x10, 0x38, 0x6C, 0xC6, 0xC6, 0xFE, 0xC6, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'B' => [
0xFC, 0xC6, 0xC6, 0xFC, 0xC6, 0xC6, 0xC6, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'C' => [
0x7C, 0xC6, 0xC0, 0xC0, 0xC0, 0xC0, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'D' => [
0xF8, 0xCC, 0xC6, 0xC6, 0xC6, 0xC6, 0xCC, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'E' => [
0xFE, 0xC0, 0xC0, 0xFC, 0xC0, 0xC0, 0xC0, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'F' => [
0xFE, 0xC0, 0xC0, 0xFC, 0xC0, 0xC0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'G' => [
0x7C, 0xC6, 0xC0, 0xDE, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'H' => [
0xC6, 0xC6, 0xC6, 0xFE, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'I' => [
0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'J' => [
0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0xCC, 0xCC, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'K' => [
0xC6, 0xCC, 0xD8, 0xF0, 0xF0, 0xD8, 0xCC, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'L' => [
0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'M' => [
0xC6, 0xEE, 0xFE, 0xD6, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'N' => [
0xC6, 0xE6, 0xF6, 0xDE, 0xCE, 0xC6, 0xC6, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'O' => [
0x7C, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'P' => [
0xFC, 0xC6, 0xC6, 0xC6, 0xFC, 0xC0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'Q' => [
0x7C, 0xC6, 0xC6, 0xC6, 0xC6, 0xDE, 0xCC, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'R' => [
0xFC, 0xC6, 0xC6, 0xFC, 0xD8, 0xCC, 0xC6, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'S' => [
0x7C, 0xC6, 0xC0, 0x7C, 0x06, 0x06, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'T' => [
0xFF, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'U' => [
0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'V' => [
0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x6C, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'W' => [
0xC6, 0xC6, 0xC6, 0xD6, 0xD6, 0xFE, 0x6C, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'X' => [
0xC6, 0xC6, 0x6C, 0x38, 0x38, 0x6C, 0xC6, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'Y' => [
0xC3, 0xC3, 0x66, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'Z' => [
0xFE, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'[' => [
0x7C, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'\\' => [
0x60, 0x30, 0x30, 0x18, 0x18, 0x0C, 0x0C, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00,
],
']' => [
0x7C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'^' => [
0x10, 0x38, 0x6C, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'_' => [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00,
],
'`' => [
0x30, 0x18, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'a' => [
0x00, 0x00, 0x78, 0x0C, 0x7C, 0xCC, 0xCC, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'b' => [
0xC0, 0xC0, 0xDC, 0xE6, 0xC6, 0xC6, 0xE6, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'c' => [
0x00, 0x00, 0x7C, 0xC6, 0xC0, 0xC0, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'd' => [
0x0C, 0x0C, 0x7C, 0xCC, 0xCC, 0xCC, 0xCC, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'e' => [
0x00, 0x00, 0x7C, 0xC6, 0xFE, 0xC0, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'f' => [
0x1C, 0x36, 0x30, 0xFC, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'g' => [
0x00, 0x00, 0x76, 0xCC, 0xCC, 0x7C, 0x0C, 0xCC, 0x78, 0x00, 0x00, 0x00, 0x00,
],
'h' => [
0xC0, 0xC0, 0xDC, 0xE6, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'i' => [
0x18, 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'j' => [
0x0C, 0x0C, 0x00, 0x0C, 0x0C, 0x0C, 0x0C, 0xCC, 0x78, 0x00, 0x00, 0x00, 0x00,
],
'k' => [
0xC0, 0xC0, 0xCC, 0xD8, 0xF0, 0xD8, 0xCC, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'l' => [
0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'm' => [
0x00, 0x00, 0xEC, 0xFE, 0xD6, 0xD6, 0xD6, 0xD6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'n' => [
0x00, 0x00, 0xDC, 0xE6, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'o' => [
0x00, 0x00, 0x7C, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'p' => [
0x00, 0x00, 0xDC, 0xE6, 0xC6, 0xE6, 0xDC, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00,
],
'q' => [
0x00, 0x00, 0x76, 0xCC, 0xCC, 0xCC, 0x7C, 0x0C, 0x0C, 0x00, 0x00, 0x00, 0x00,
],
'r' => [
0x00, 0x00, 0xDC, 0xE6, 0xC0, 0xC0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00,
],
's' => [
0x00, 0x00, 0x7C, 0xC6, 0x70, 0x1C, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
't' => [
0x00, 0x30, 0xFC, 0x30, 0x30, 0x30, 0x36, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'u' => [
0x00, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'v' => [
0x00, 0x00, 0xC6, 0xC6, 0xC6, 0x6C, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'w' => [
0x00, 0x00, 0xC6, 0xC6, 0xD6, 0xD6, 0xFE, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'x' => [
0x00, 0x00, 0xC6, 0x6C, 0x38, 0x38, 0x6C, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'y' => [
0x00, 0x00, 0xC6, 0xC6, 0xC6, 0x7E, 0x06, 0xC6, 0x7C, 0x00, 0x00, 0x00, 0x00,
],
'z' => [
0x00, 0x00, 0xFE, 0x0C, 0x18, 0x30, 0x60, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'{' => [
0x0E, 0x18, 0x18, 0x70, 0x18, 0x18, 0x18, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'|' => [
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00,
],
'}' => [
0x70, 0x18, 0x18, 0x0E, 0x18, 0x18, 0x18, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00,
],
'~' => [
0x76, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
],
_ => NOTDEF_ROWS,
}
}
impl Default for FontAtlas {
fn default() -> Self {
Self::built_in()
}
}
impl std::fmt::Debug for FontAtlas {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FontAtlas")
.field("glyph_count", &self.len())
.field("texture_size", &self.texture_size)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn built_in_atlas_has_ascii_plus_notdef() {
let atlas = FontAtlas::built_in();
assert_eq!(atlas.len(), GLYPH_COUNT);
assert_eq!(GLYPH_COUNT, ASCII_GLYPH_COUNT + 1);
}
#[test]
fn space_glyph_has_zero_width() {
let atlas = FontAtlas::built_in();
let g = atlas.glyph(' ');
assert_eq!(g.width, 0);
assert!((g.advance - GLYPH_ADVANCE).abs() < 0.001);
}
#[test]
fn letter_a_has_positive_width() {
let atlas = FontAtlas::built_in();
let g = atlas.glyph('A');
assert_eq!(g.width, GLYPH_WIDTH);
assert_eq!(g.height, GLYPH_HEIGHT);
}
#[test]
fn out_of_range_char_returns_notdef() {
let atlas = FontAtlas::built_in();
for c in ['\0', '\u{7f}', 'é', '€', 'Ł', 'ġ'] {
assert_eq!(glyph_index(c), NOTDEF_INDEX, "{c:?} should be .notdef");
let g = atlas.glyph(c);
assert_eq!(g.width, GLYPH_WIDTH, "{c:?} should draw the box");
}
}
#[test]
fn non_ascii_does_not_alias_onto_an_ascii_glyph() {
let atlas = FontAtlas::built_in();
assert_ne!(atlas.glyph_u_min('Ł'), atlas.glyph_u_min('A'));
assert_ne!(atlas.glyph_u_min('ġ'), atlas.glyph_u_min('!'));
}
#[test]
fn glyph_uvs_stay_inside_the_atlas_for_any_codepoint() {
let atlas = FontAtlas::built_in();
for c in ['\0', 'é', '€', 'Ł', '\u{10ffff}', 'A', ' ', '~'] {
let (u_min, u_max) = (atlas.glyph_u_min(c), atlas.glyph_u_max(c));
assert!(
(0.0..=1.0).contains(&u_min) && (0.0..=1.0).contains(&u_max) && u_min < u_max,
"{c:?}: u range {u_min}..{u_max} escapes the atlas"
);
}
}
#[test]
fn text_width_single_char() {
let atlas = FontAtlas::built_in();
let w = atlas.text_width("A", 1.0);
assert!((w - GLYPH_ADVANCE).abs() < 0.001);
}
#[test]
fn text_width_multiple_chars() {
let atlas = FontAtlas::built_in();
let w = atlas.text_width("ABC", 1.0);
assert!((w - GLYPH_ADVANCE * 3.0).abs() < 0.001);
}
#[test]
fn text_width_scales_linearly_with_the_scale_it_is_given() {
let atlas = FontAtlas::built_in();
let w1 = atlas.text_width("Hi", 1.0);
let w2 = atlas.text_width("Hi", 2.0);
assert!((w2 - w1 * 2.0).abs() < 0.001);
}
#[test]
fn layout_line_produces_glyphs() {
let atlas = FontAtlas::built_in();
let glyphs = atlas.layout_line("AB", Vec2::ZERO, 1.0);
assert_eq!(glyphs.len(), 2);
assert!((glyphs[0].1.x - 1.0).abs() < 0.001);
assert!(
(glyphs[1].1.x - glyphs[0].1.x - GLYPH_ADVANCE).abs() < 0.001,
"expected horizontal advance {}, got {}",
GLYPH_ADVANCE,
glyphs[1].1.x - glyphs[0].1.x,
);
}
#[test]
fn layout_line_scales_extents_but_not_the_anchor() {
let atlas = FontAtlas::built_in();
let glyphs = atlas.layout_line("AB", Vec2::new(100.0, 200.0), 2.0);
assert_eq!(glyphs.len(), 2);
assert!((glyphs[0].1.x - 102.0).abs() < 0.001, "{:?}", glyphs[0].1);
assert!((glyphs[0].1.y - 200.0).abs() < 0.001, "{:?}", glyphs[0].1);
assert!((glyphs[0].2.x - glyphs[0].1.x - 16.0).abs() < 0.001);
assert!((glyphs[0].2.y - glyphs[0].1.y - 26.0).abs() < 0.001);
assert!(
(glyphs[1].1.x - glyphs[0].1.x - GLYPH_ADVANCE * 2.0).abs() < 0.001,
"advance scaled twice: {}",
glyphs[1].1.x - glyphs[0].1.x
);
}
#[test]
fn layout_line_anchor_is_the_top_left_not_the_baseline() {
let atlas = FontAtlas::built_in();
let pos = Vec2::new(10.0, 100.0);
for scale in [0.5, 1.0, 2.0] {
let glyphs = atlas.layout_line("Hi", pos, scale);
for (c, min, max) in glyphs {
assert!(min.y >= pos.y - 0.001, "{c:?} at scale {scale}: {min:?}");
assert!(
max.y <= pos.y + LINE_HEIGHT * scale + 0.001,
"{c:?} at scale {scale}: {max:?}"
);
}
}
}
#[test]
fn layout_line_handles_newline() {
let atlas = FontAtlas::built_in();
let glyphs = atlas.layout_line("A\nB", Vec2::ZERO, 1.0);
assert_eq!(glyphs.len(), 2);
assert!(
(glyphs[1].1.y - glyphs[0].1.y - LINE_HEIGHT).abs() < 0.001,
"expected vertical advance {}, got {}",
LINE_HEIGHT,
glyphs[1].1.y - glyphs[0].1.y,
);
}
#[test]
fn empty_string_layout_is_empty() {
let atlas = FontAtlas::built_in();
let glyphs = atlas.layout_line("", Vec2::ZERO, 1.0);
assert!(glyphs.is_empty());
}
#[test]
fn text_width_of_multiline_is_the_widest_line() {
let atlas = FontAtlas::built_in();
let w = atlas.text_width("AB\nCDE", 1.0);
assert!(
(w - GLYPH_ADVANCE * 3.0).abs() < 0.001,
"expected the widest line ({}), got {w}",
GLYPH_ADVANCE * 3.0
);
assert_eq!(atlas.line_count("AB\nCDE"), 2);
assert_eq!(atlas.line_count("one line"), 1);
}
#[test]
fn the_font_atlas_debug_output_reports_its_glyph_count() {
let atlas = FontAtlas::built_in();
let s = format!("{atlas:?}");
assert!(s.contains(&format!("glyph_count: {GLYPH_COUNT}")));
}
#[test]
fn glyph_u_min_for_first_char_is_zero() {
let atlas = FontAtlas::built_in();
let u = atlas.glyph_u_min(' ');
assert!((u - 0.0).abs() < 0.001);
}
#[test]
fn glyph_u_max_for_the_notdef_column_is_one() {
let atlas = FontAtlas::built_in();
let u = atlas.glyph_u_max('€');
assert!((u - 1.0).abs() < 0.001, "got {u}");
let u = atlas.glyph_u_max('~');
assert!(
(u - ASCII_GLYPH_COUNT as f32 / GLYPH_COUNT as f32).abs() < 0.001,
"got {u}"
);
}
#[test]
fn glyph_u_for_letter_a_is_contiguous() {
let atlas = FontAtlas::built_in();
let u_max = atlas.glyph_u_max('A'); let u_min_b = atlas.glyph_u_min('B'); assert!((u_max - u_min_b).abs() < 0.001);
}
}