use std::collections::HashMap;
#[cfg(feature = "parsed-font")]
use glam::Vec2;
#[cfg(feature = "parsed-font")]
use skrifa::instance::{LocationRef, Size};
#[cfg(feature = "parsed-font")]
use skrifa::outline::{
DrawSettings, HintingInstance, HintingOptions, OutlinePen, SmoothMode, Target,
};
#[cfg(feature = "parsed-font")]
use skrifa::{FontRef, MetadataProvider};
#[cfg(feature = "parsed-font")]
use super::face::{SYNTHETIC_BOLD_EM, SYNTHETIC_ITALIC_SLANT};
#[cfg(feature = "parsed-font")]
use super::raster::{Point, Rasterizer};
use super::{Font, FontId, GlyphId};
use crate::image::TexelRect;
pub const GLYPH_PAGE_SIZE: u32 = 1024;
pub const GLYPH_MAX_PAGES: usize = 2;
pub const GLYPH_RASTER_BUDGET: u32 = 256;
pub const SUBPIXEL_BINS: u8 = 4;
pub const GLYPH_GUTTER: u32 = 1;
pub const SHELF_QUANTUM: u32 = 4;
#[cfg(feature = "parsed-font")]
const HINTING_INSTANCES: usize = 16;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct GlyphKey {
pub font: FontId,
pub glyph: GlyphId,
pub size_bits: u32,
pub bin: u8,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AtlasGlyph {
pub page: u32,
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32,
pub left: i32,
pub top: i32,
}
impl AtlasGlyph {
pub const EMPTY: Self = Self {
page: 0,
x: 0,
y: 0,
width: 0,
height: 0,
left: 0,
top: 0,
};
#[must_use]
pub const fn is_empty(&self) -> bool {
self.width == 0 || self.height == 0
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct GlyphAtlasStats {
pub rasterized: u32,
pub deferred: u32,
pub evicted: u32,
pub unplaced: u32,
}
#[derive(Clone, Copy, Debug)]
enum Cached {
Placed(AtlasGlyph),
Empty,
TooLarge,
}
#[derive(Clone, Copy, Debug)]
struct Entry {
cached: Cached,
last_used: u64,
}
#[derive(Clone, Copy, Debug)]
struct Slot {
x: u32,
width: u32,
key: Option<GlyphKey>,
}
#[derive(Clone, Debug)]
struct Shelf {
y: u32,
height: u32,
slots: Vec<Slot>,
cursor: u32,
}
impl Shelf {
fn is_empty(&self) -> bool {
self.slots.iter().all(|slot| slot.key.is_none())
}
}
#[derive(Clone, Debug)]
struct Page {
pixels: Vec<u8>,
shelves: Vec<Shelf>,
dirty: Option<TexelRect>,
}
impl Page {
fn new(size: u32) -> Self {
Self {
pixels: vec![0; size as usize * size as usize],
shelves: Vec::new(),
dirty: None,
}
}
fn allocate(
&mut self,
size: u32,
key: GlyphKey,
width: u32,
height: u32,
) -> Option<(u32, u32)> {
let class = height.next_multiple_of(SHELF_QUANTUM);
for shelf in &mut self.shelves {
if shelf.height < height || !(shelf.height == class || shelf.is_empty()) {
continue;
}
if let Some(at) = shelf
.slots
.iter()
.position(|slot| slot.key.is_none() && slot.width >= width)
{
let slot = shelf.slots[at];
if slot.width > width {
shelf.slots.insert(
at + 1,
Slot {
x: slot.x + width,
width: slot.width - width,
key: None,
},
);
}
shelf.slots[at] = Slot {
x: slot.x,
width,
key: Some(key),
};
return Some((slot.x, shelf.y));
}
if size - shelf.cursor >= width {
let x = shelf.cursor;
shelf.slots.push(Slot {
x,
width,
key: Some(key),
});
shelf.cursor += width;
return Some((x, shelf.y));
}
}
let top = self
.shelves
.last()
.map_or(0, |shelf| shelf.y + shelf.height);
if size - top < height || size < width {
return None;
}
self.shelves.push(Shelf {
y: top,
height: class.min(size - top),
slots: vec![Slot {
x: 0,
width,
key: Some(key),
}],
cursor: width,
});
Some((0, top))
}
fn free(&mut self, x: u32, y: u32) {
let Some(shelf) = self.shelves.iter_mut().find(|shelf| shelf.y == y) else {
return;
};
let Some(mut at) = shelf.slots.iter().position(|slot| slot.x == x) else {
return;
};
shelf.slots[at].key = None;
if at + 1 < shelf.slots.len() && shelf.slots[at + 1].key.is_none() {
shelf.slots[at].width += shelf.slots[at + 1].width;
shelf.slots.remove(at + 1);
}
if at > 0 && shelf.slots[at - 1].key.is_none() {
shelf.slots[at - 1].width += shelf.slots[at].width;
shelf.slots.remove(at);
at -= 1;
}
if at + 1 == shelf.slots.len() {
shelf.cursor = shelf.slots[at].x;
shelf.slots.pop();
}
while self
.shelves
.last()
.is_some_and(|shelf| shelf.slots.is_empty())
{
self.shelves.pop();
}
}
}
#[cfg(feature = "parsed-font")]
#[derive(Clone, Copy, Debug)]
enum Segment {
Line(Vec2, Vec2),
Quadratic(Vec2, Vec2, Vec2),
Cubic(Vec2, Vec2, Vec2, Vec2),
}
#[cfg(feature = "parsed-font")]
const fn mask_point(point: Vec2) -> Point {
Point::new(point.x, point.y)
}
#[cfg(feature = "parsed-font")]
struct Collect<'a> {
segments: &'a mut Vec<Segment>,
offset: f32,
slant: f32,
start: Vec2,
current: Vec2,
}
#[cfg(feature = "parsed-font")]
impl Collect<'_> {
fn point(&self, x: f32, y: f32) -> Vec2 {
Vec2::new(x + y * self.slant + self.offset, -y)
}
fn close_contour(&mut self) {
if self.current != self.start {
self.segments.push(Segment::Line(self.current, self.start));
}
self.current = self.start;
}
}
#[cfg(feature = "parsed-font")]
impl OutlinePen for Collect<'_> {
fn move_to(&mut self, x: f32, y: f32) {
self.close_contour();
self.start = self.point(x, y);
self.current = self.start;
}
fn line_to(&mut self, x: f32, y: f32) {
let to = self.point(x, y);
self.segments.push(Segment::Line(self.current, to));
self.current = to;
}
fn quad_to(&mut self, cx0: f32, cy0: f32, x: f32, y: f32) {
let to = self.point(x, y);
self.segments
.push(Segment::Quadratic(self.current, self.point(cx0, cy0), to));
self.current = to;
}
fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32) {
let to = self.point(x, y);
self.segments.push(Segment::Cubic(
self.current,
self.point(cx0, cy0),
self.point(cx1, cy1),
to,
));
self.current = to;
}
fn close(&mut self) {
self.close_contour();
}
}
#[cfg(feature = "parsed-font")]
fn hinting_options() -> HintingOptions {
HintingOptions::from(Target::Smooth {
mode: SmoothMode::Light,
symmetric_rendering: true,
preserve_linear_metrics: true,
})
}
#[cfg(feature = "parsed-font")]
fn draw_outline(
font: &Font,
glyph: GlyphId,
size: f32,
offset: f32,
hinting: Option<&HintingInstance>,
segments: &mut Vec<Segment>,
) -> bool {
let synthesis = font.synthesis();
let slant = if synthesis.italic {
SYNTHETIC_ITALIC_SLANT
} else {
0.0
};
let drawn = draw_contours(font, glyph, size, (offset, slant), hinting, segments);
if drawn && synthesis.bold {
let strength = Vec2::new(size * SYNTHETIC_BOLD_EM, 0.0);
let first = segments.len();
for at in 0..first {
let shifted = match segments[at] {
Segment::Line(a, b) => Segment::Line(a + strength, b + strength),
Segment::Quadratic(a, b, c) => {
Segment::Quadratic(a + strength, b + strength, c + strength)
}
Segment::Cubic(a, b, c, d) => {
Segment::Cubic(a + strength, b + strength, c + strength, d + strength)
}
};
segments.push(shifted);
}
}
drawn
}
#[cfg(feature = "parsed-font")]
fn draw_contours(
font: &Font,
glyph: GlyphId,
size: f32,
(offset, slant): (f32, f32),
hinting: Option<&HintingInstance>,
segments: &mut Vec<Segment>,
) -> bool {
segments.clear();
let Ok(font_ref) = FontRef::new(font.data()) else {
return false;
};
let outlines = font_ref.outline_glyphs();
let Some(outline) = outlines.get(skrifa::GlyphId::new(glyph.0)) else {
return false;
};
let mut pen = Collect {
segments,
offset,
slant,
start: Vec2::ZERO,
current: Vec2::ZERO,
};
let drawn = match hinting {
Some(instance) => outline.draw(DrawSettings::hinted(instance, false), &mut pen),
None => outline.draw(
DrawSettings::unhinted(Size::new(size), LocationRef::default()),
&mut pen,
),
};
pen.close_contour();
drawn.is_ok()
}
pub struct GlyphAtlas {
page_size: u32,
max_pages: usize,
raster_budget: u32,
pages: Vec<Page>,
entries: HashMap<GlyphKey, Entry>,
frame: u64,
stats: GlyphAtlasStats,
#[cfg(feature = "parsed-font")]
hinting: HashMap<(FontId, u32), Option<HintingInstance>>,
#[cfg(feature = "parsed-font")]
rasterizer: Rasterizer,
#[cfg(feature = "parsed-font")]
segments: Vec<Segment>,
mask: Vec<u8>,
}
impl core::fmt::Debug for GlyphAtlas {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("GlyphAtlas")
.field("page_size", &self.page_size)
.field("pages", &self.pages.len())
.field("max_pages", &self.max_pages)
.field("glyphs", &self.entries.len())
.field("frame", &self.frame)
.field("stats", &self.stats)
.finish_non_exhaustive()
}
}
impl Default for GlyphAtlas {
fn default() -> Self {
Self::new(GLYPH_PAGE_SIZE, GLYPH_MAX_PAGES, GLYPH_RASTER_BUDGET)
}
}
impl GlyphAtlas {
#[must_use]
pub fn new(page_size: u32, max_pages: usize, raster_budget: u32) -> Self {
Self {
page_size: page_size.max(1),
max_pages: max_pages.max(1),
raster_budget: raster_budget.max(1),
pages: Vec::new(),
entries: HashMap::new(),
frame: 0,
stats: GlyphAtlasStats::default(),
#[cfg(feature = "parsed-font")]
hinting: HashMap::new(),
#[cfg(feature = "parsed-font")]
rasterizer: Rasterizer::default(),
#[cfg(feature = "parsed-font")]
segments: Vec::new(),
mask: Vec::new(),
}
}
#[must_use]
pub const fn page_size(&self) -> u32 {
self.page_size
}
#[must_use]
pub const fn max_pages(&self) -> usize {
self.max_pages
}
#[must_use]
pub const fn raster_budget(&self) -> u32 {
self.raster_budget
}
#[must_use]
pub fn page_count(&self) -> usize {
self.pages.len()
}
#[must_use]
pub fn len(&self) -> usize {
self.entries.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
#[must_use]
pub const fn stats(&self) -> GlyphAtlasStats {
self.stats
}
pub fn begin_frame(&mut self) {
self.frame += 1;
self.stats = GlyphAtlasStats::default();
}
#[must_use]
pub fn key(font: &Font, glyph: GlyphId, size: f32, bin: u8) -> GlyphKey {
GlyphKey {
font: font.id(),
glyph,
size_bits: size.to_bits(),
bin: bin % SUBPIXEL_BINS,
}
}
pub fn glyph(&mut self, font: &Font, glyph: GlyphId, size: f32, bin: u8) -> Option<AtlasGlyph> {
let key = Self::key(font, glyph, size, bin);
if let Some(entry) = self.entries.get_mut(&key) {
entry.last_used = self.frame;
return match entry.cached {
Cached::Placed(placed) => Some(placed),
Cached::Empty => Some(AtlasGlyph::EMPTY),
Cached::TooLarge => None,
};
}
if !(size.is_finite() && size > 0.0) {
return Some(AtlasGlyph::EMPTY);
}
if self.stats.rasterized >= self.raster_budget {
self.stats.deferred += 1;
return None;
}
self.stats.rasterized += 1;
let Some((width, height, left, top)) = self.rasterize(font, key) else {
self.insert(key, Cached::Empty);
return Some(AtlasGlyph::EMPTY);
};
let slot_width = width + 2 * GLYPH_GUTTER;
let slot_height = height + 2 * GLYPH_GUTTER;
if slot_width > self.page_size || slot_height > self.page_size {
self.stats.unplaced += 1;
self.insert(key, Cached::TooLarge);
return None;
}
let Some((page, x, y)) = self.place(key, slot_width, slot_height) else {
self.stats.unplaced += 1;
return None;
};
let placed = AtlasGlyph {
page: page as u32,
x: x + GLYPH_GUTTER,
y: y + GLYPH_GUTTER,
width,
height,
left,
top,
};
self.blit(&placed, slot_width, slot_height);
self.insert(key, Cached::Placed(placed));
Some(placed)
}
fn insert(&mut self, key: GlyphKey, cached: Cached) {
self.entries.insert(
key,
Entry {
cached,
last_used: self.frame,
},
);
}
#[cfg(not(feature = "parsed-font"))]
#[expect(
clippy::unused_self,
reason = "the feature-on twin draws into `self`'s scratch buffers"
)]
fn rasterize(&mut self, font: &Font, _key: GlyphKey) -> Option<(u32, u32, i32, i32)> {
match font.source.never {}
}
#[cfg(feature = "parsed-font")]
fn rasterize(&mut self, font: &Font, key: GlyphKey) -> Option<(u32, u32, i32, i32)> {
let size = f32::from_bits(key.size_bits);
let hinting_key = (key.font, key.size_bits);
if !self.hinting.contains_key(&hinting_key) {
if self.hinting.len() >= HINTING_INSTANCES {
self.hinting.clear();
}
let instance = FontRef::new(font.data()).ok().and_then(|font_ref| {
HintingInstance::new(
&font_ref.outline_glyphs(),
Size::new(size),
LocationRef::default(),
hinting_options(),
)
.ok()
});
self.hinting.insert(hinting_key, instance);
}
let offset = f32::from(key.bin) / f32::from(SUBPIXEL_BINS);
let hinting = self.hinting.get(&hinting_key).and_then(Option::as_ref);
let drawn = draw_outline(font, key.glyph, size, offset, hinting, &mut self.segments)
|| draw_outline(font, key.glyph, size, offset, None, &mut self.segments);
if !drawn || self.segments.is_empty() {
return None;
}
let mut min = Vec2::splat(f32::INFINITY);
let mut max = Vec2::splat(f32::NEG_INFINITY);
for segment in &self.segments {
let points: &[Vec2] = match segment {
Segment::Line(a, b) => &[*a, *b],
Segment::Quadratic(a, b, c) => &[*a, *b, *c],
Segment::Cubic(a, b, c, d) => &[*a, *b, *c, *d],
};
for point in points {
min = min.min(*point);
max = max.max(*point);
}
}
if !(min.is_finite() && max.is_finite()) {
return None;
}
let origin = min.floor();
let extent = max.ceil() - origin;
let (width, height) = (extent.x as u32, extent.y as u32);
if width == 0 || height == 0 {
return None;
}
self.rasterizer.reset(width, height);
let at = |point: Vec2| mask_point(point - origin);
for segment in &self.segments {
match *segment {
Segment::Line(a, b) => self.rasterizer.line(at(a), at(b)),
Segment::Quadratic(a, b, c) => self.rasterizer.quadratic(at(a), at(b), at(c)),
Segment::Cubic(a, b, c, d) => self.rasterizer.cubic(at(a), at(b), at(c), at(d)),
}
}
self.mask.clear();
self.rasterizer.write_mask(&mut self.mask);
Some((width, height, origin.x as i32, origin.y as i32))
}
fn place(&mut self, key: GlyphKey, width: u32, height: u32) -> Option<(usize, u32, u32)> {
let size = self.page_size;
for (index, page) in self.pages.iter_mut().enumerate() {
if let Some((x, y)) = page.allocate(size, key, width, height) {
return Some((index, x, y));
}
}
if self.pages.len() < self.max_pages {
let mut page = Page::new(size);
let placed = page.allocate(size, key, width, height);
self.pages.push(page);
return placed.map(|(x, y)| (self.pages.len() - 1, x, y));
}
self.evict_for(key, width, height)
}
fn evict_for(&mut self, key: GlyphKey, width: u32, height: u32) -> Option<(usize, u32, u32)> {
let mut by_page: Vec<Vec<(u64, GlyphKey, AtlasGlyph)>> = vec![Vec::new(); self.pages.len()];
for (candidate, entry) in &self.entries {
if let Cached::Placed(placed) = entry.cached
&& entry.last_used != self.frame
{
by_page[placed.page as usize].push((entry.last_used, *candidate, placed));
}
}
for glyphs in &mut by_page {
glyphs.sort_unstable_by_key(|(last_used, ..)| *last_used);
}
let mut order: Vec<usize> = (0..by_page.len())
.filter(|&page| !by_page[page].is_empty())
.collect();
order.sort_unstable_by_key(|&page| by_page[page][0].0);
let size = self.page_size;
for page in order {
for &(_, victim, placed) in &by_page[page] {
self.entries.remove(&victim);
self.pages[page].free(placed.x - GLYPH_GUTTER, placed.y - GLYPH_GUTTER);
self.stats.evicted += 1;
if let Some((x, y)) = self.pages[page].allocate(size, key, width, height) {
return Some((page, x, y));
}
}
}
None
}
fn blit(&mut self, placed: &AtlasGlyph, slot_width: u32, slot_height: u32) {
let size = self.page_size as usize;
let page = &mut self.pages[placed.page as usize];
let (slot_x, slot_y) = (
(placed.x - GLYPH_GUTTER) as usize,
(placed.y - GLYPH_GUTTER) as usize,
);
for row in 0..slot_height as usize {
let start = (slot_y + row) * size + slot_x;
page.pixels[start..start + slot_width as usize].fill(0);
}
let width = placed.width as usize;
for (row, source) in self.mask.chunks_exact(width).enumerate() {
let start = (placed.y as usize + row) * size + placed.x as usize;
page.pixels[start..start + width].copy_from_slice(source);
}
let rect = TexelRect {
x: slot_x as u32,
y: slot_y as u32,
width: slot_width,
height: slot_height,
};
page.dirty = Some(page.dirty.map_or(rect, |dirty| dirty.union(rect)));
}
#[must_use]
pub fn page_pixels(&self, page: usize) -> &[u8] {
&self.pages[page].pixels
}
#[must_use]
pub fn dirty(&self, page: usize) -> Option<TexelRect> {
self.pages.get(page).and_then(|page| page.dirty)
}
pub fn take_dirty(&mut self, page: usize) -> Option<TexelRect> {
self.pages.get_mut(page).and_then(|page| page.dirty.take())
}
pub fn mark_dirty(&mut self, page: usize, rect: TexelRect) {
if let Some(page) = self.pages.get_mut(page) {
page.dirty = Some(page.dirty.map_or(rect, |dirty| dirty.union(rect)));
}
}
#[must_use]
pub fn region(&self, page: usize, rect: TexelRect) -> Vec<u8> {
let size = self.page_size as usize;
let pixels = &self.pages[page].pixels;
let mut out = Vec::with_capacity(rect.width as usize * rect.height as usize);
for row in rect.y as usize..(rect.y + rect.height) as usize {
let start = row * size + rect.x as usize;
out.extend_from_slice(&pixels[start..start + rect.width as usize]);
}
out
}
#[must_use]
pub fn cached(&self, key: GlyphKey) -> Option<AtlasGlyph> {
match self.entries.get(&key)?.cached {
Cached::Placed(placed) => Some(placed),
Cached::Empty => Some(AtlasGlyph::EMPTY),
Cached::TooLarge => None,
}
}
}
#[cfg(all(test, feature = "parsed-font"))]
mod tests {
use super::*;
use crate::font::face::Synthesis;
use crate::font::raster::coverage_byte;
fn sans() -> &'static Font {
Font::sans()
}
fn glyph(c: char) -> GlyphId {
sans().glyph_id(c)
}
fn overlaps(a: &AtlasGlyph, b: &AtlasGlyph) -> bool {
let g = GLYPH_GUTTER;
a.page == b.page
&& a.x - g < b.x + b.width + g
&& b.x - g < a.x + a.width + g
&& a.y - g < b.y + b.height + g
&& b.y - g < a.y + a.height + g
}
#[test]
fn glyphs_pack_without_overlap_and_hold_their_own_coverage() {
let mut atlas = GlyphAtlas::new(256, 4, 10_000);
atlas.begin_frame();
let mut placed = Vec::new();
for size in [11.0, 23.0] {
for c in ('A'..='Z').chain('a'..='z').chain(['é', 'Ç', '@']) {
for bin in 0..SUBPIXEL_BINS {
let at = atlas.glyph(sans(), glyph(c), size, bin).expect("room");
assert!(!at.is_empty(), "{c:?} has ink");
placed.push((c, size, bin, at));
}
}
}
assert!(
atlas.page_count() > 1,
"the test is meant to open a second page"
);
assert_eq!(atlas.stats().evicted, 0);
for (i, (_, _, _, a)) in placed.iter().enumerate() {
assert!(a.x >= GLYPH_GUTTER && a.y >= GLYPH_GUTTER);
assert!(a.x + a.width + GLYPH_GUTTER <= 256 && a.y + a.height + GLYPH_GUTTER <= 256);
for (_, _, _, b) in &placed[i + 1..] {
assert!(!overlaps(a, b), "{a:?} overlaps {b:?}");
}
}
let (c, size, bin, at) = placed[37];
let mut segments = Vec::new();
let instance = HintingInstance::new(
&FontRef::new(sans().data()).expect("reads").outline_glyphs(),
Size::new(size),
LocationRef::default(),
hinting_options(),
)
.expect("hints");
assert!(draw_outline(
sans(),
glyph(c),
size,
f32::from(bin) / 4.0,
Some(&instance),
&mut segments
));
let origin = Vec2::new(at.left as f32, at.top as f32);
let mut rasterizer = Rasterizer::new(at.width, at.height);
let local = |point: Vec2| mask_point(point - origin);
for segment in segments {
match segment {
Segment::Line(a, b) => rasterizer.line(local(a), local(b)),
Segment::Quadratic(a, b, c) => rasterizer.quadratic(local(a), local(b), local(c)),
Segment::Cubic(a, b, c, d) => {
rasterizer.cubic(local(a), local(b), local(c), local(d));
}
}
}
let want: Vec<u8> = rasterizer
.coverage()
.into_iter()
.map(coverage_byte)
.collect();
let page = atlas.page_pixels(at.page as usize);
let texel = |x: u32, y: u32| page[(y * 256 + x) as usize];
for y in 0..at.height {
for x in 0..at.width {
assert_eq!(texel(at.x + x, at.y + y), want[(y * at.width + x) as usize]);
}
}
for x in at.x - 1..=at.x + at.width {
assert_eq!(texel(x, at.y - 1), 0, "top gutter");
assert_eq!(texel(x, at.y + at.height), 0, "bottom gutter");
}
assert!(want.contains(&255), "{c:?} has a solid stem");
}
#[test]
fn a_hit_is_free_and_a_space_takes_no_room() {
let mut atlas = GlyphAtlas::new(256, 1, 100);
atlas.begin_frame();
let first = atlas.glyph(sans(), glyph('Q'), 20.0, 1).expect("room");
let second = atlas.glyph(sans(), glyph('Q'), 20.0, 1).expect("cached");
assert_eq!(first, second);
assert_eq!(atlas.stats().rasterized, 1);
assert_eq!(atlas.glyph(sans(), glyph('Q'), 20.0, 5), Some(first));
assert_eq!(atlas.stats().rasterized, 1);
let space = atlas
.glyph(sans(), glyph(' '), 20.0, 0)
.expect("empty, not deferred");
assert!(space.is_empty());
assert_eq!(atlas.dirty(0).map(|rect| rect.width), Some(first.width + 2));
}
#[test]
fn the_budget_defers_misses_to_later_frames() {
let mut atlas = GlyphAtlas::new(256, 1, 3);
let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
let mut frames = 0;
loop {
atlas.begin_frame();
frames += 1;
let drawn = letters
.iter()
.filter(|&&c| atlas.glyph(sans(), glyph(c), 16.0, 0).is_some())
.count();
let stats = atlas.stats();
assert!(stats.rasterized <= 3, "{stats:?}");
assert_eq!(drawn + stats.deferred as usize, letters.len(), "{stats:?}");
if drawn == letters.len() {
break;
}
}
assert_eq!(frames, 3, "seven glyphs at three a frame take three frames");
}
#[test]
fn eviction_takes_the_least_recently_used_and_respects_the_budget() {
const PAGE: u32 = 48;
const SIZE: f32 = 14.0;
let fonts: Vec<Font> = (0..40)
.map(|_| Font::parse(crate::font::SANS_TTF).expect("parses"))
.collect();
let h = sans().glyph_id('H');
let draw = |atlas: &mut GlyphAtlas, font: &Font| atlas.glyph(font, h, SIZE, 0);
let cached = |atlas: &GlyphAtlas, font: &Font| {
atlas.cached(GlyphAtlas::key(font, h, SIZE, 0)).is_some()
};
let mut one_frame = GlyphAtlas::new(PAGE, 1, 1000);
one_frame.begin_frame();
let capacity = fonts
.iter()
.take_while(|font| draw(&mut one_frame, font).is_some())
.count();
assert!(capacity >= 6 && capacity * 2 < fonts.len(), "{capacity}");
assert_eq!(one_frame.stats().evicted, 0);
assert_eq!(one_frame.stats().unplaced, 1);
let mut atlas = GlyphAtlas::new(PAGE, 1, 1000);
for font in &fonts[..capacity] {
atlas.begin_frame();
draw(&mut atlas, font).expect("fits");
}
atlas.begin_frame();
for font in &fonts[..2] {
draw(&mut atlas, font).expect("cached");
}
assert_eq!(atlas.stats().rasterized, 0, "a hit rasterised");
draw(&mut atlas, &fonts[capacity]).expect("room after eviction");
assert_eq!(
atlas.stats().evicted,
1,
"one slot frees room for one glyph"
);
let gone: Vec<usize> = (0..capacity)
.filter(|&index| !cached(&atlas, &fonts[index]))
.collect();
assert_eq!(
gone,
[2],
"the evicted glyph is not the least recently used"
);
let mut returning = GlyphAtlas::new(PAGE, 1, 1000);
returning.begin_frame();
for font in &fonts[..capacity] {
draw(&mut returning, font).expect("fits");
}
returning.begin_frame();
for font in &fonts[capacity..2 * capacity] {
draw(&mut returning, font).expect("room after eviction");
}
assert_eq!(returning.stats().evicted as usize, capacity);
returning.raster_budget = 2;
let mut frames = 0;
let mut back = 0;
while back < capacity {
returning.begin_frame();
frames += 1;
back = fonts[..capacity]
.iter()
.filter(|font| draw(&mut returning, font).is_some())
.count();
let stats = returning.stats();
assert_eq!(
stats.rasterized as usize,
2.min(capacity - (back - stats.rasterized as usize)),
"frame {frames}: {stats:?}"
);
assert_eq!(stats.unplaced, 0, "frame {frames}: {stats:?}");
}
assert_eq!(frames, capacity.div_ceil(2));
}
#[test]
fn subpixel_bins_shift_the_ink_by_quarter_pixels() {
let mut atlas = GlyphAtlas::new(256, 1, 100);
atlas.begin_frame();
let centroid = |atlas: &mut GlyphAtlas, bin: u8| {
let at = atlas.glyph(sans(), glyph('l'), 24.0, bin).expect("room");
let page = atlas.page_pixels(0);
let (mut sum, mut weight) = (0.0f64, 0.0f64);
for y in 0..at.height {
for x in 0..at.width {
let c = f64::from(page[((at.y + y) * 256 + at.x + x) as usize]);
sum += c * (f64::from(at.left) + f64::from(x) + 0.5);
weight += c;
}
}
sum / weight
};
let base = centroid(&mut atlas, 0);
for bin in 1..SUBPIXEL_BINS {
let shift = centroid(&mut atlas, bin) - base;
let want = f64::from(bin) / 4.0;
assert!(
(shift - want).abs() < 0.02,
"bin {bin} shifted {shift}, not {want}"
);
}
}
fn points(segments: &[Segment]) -> Vec<Vec2> {
segments
.iter()
.flat_map(|segment| match *segment {
Segment::Line(a, b) => vec![a, b],
Segment::Quadratic(a, b, c) => vec![a, b, c],
Segment::Cubic(a, b, c, d) => vec![a, b, c, d],
})
.collect()
}
#[test]
fn hinting_snaps_vertical_metrics_and_moves_nothing_horizontally() {
let outlines = || FontRef::new(sans().data()).expect("reads").outline_glyphs();
for size in [11.0, 13.0, 16.0, 24.0] {
let instance = HintingInstance::new(
&outlines(),
Size::new(size),
LocationRef::default(),
hinting_options(),
)
.expect("hints");
for c in ('A'..='Z').chain('a'..='z') {
let (mut hinted, mut plain) = (Vec::new(), Vec::new());
assert!(draw_outline(
sans(),
glyph(c),
size,
0.0,
Some(&instance),
&mut hinted
));
assert!(draw_outline(sans(), glyph(c), size, 0.0, None, &mut plain));
let (hinted, plain) = (points(&hinted), points(&plain));
assert_eq!(hinted.len(), plain.len(), "{c:?} at {size}");
for (a, b) in hinted.iter().zip(&plain) {
assert_eq!(a.x, b.x, "{c:?} at {size}px moved horizontally");
}
}
}
let size = 13.0;
let instance = HintingInstance::new(
&outlines(),
Size::new(size),
LocationRef::default(),
hinting_options(),
)
.expect("hints");
let top = |hinting: Option<&HintingInstance>| {
let mut segments = Vec::new();
assert!(draw_outline(
sans(),
glyph('H'),
size,
0.0,
hinting,
&mut segments
));
points(&segments)
.iter()
.fold(f32::INFINITY, |top, point| top.min(point.y))
};
let (hinted, plain) = (top(Some(&instance)), top(None));
assert_eq!(hinted, hinted.round(), "the hinted cap height is {hinted}");
assert_ne!(
plain,
plain.round(),
"the unhinted cap height is already whole"
);
}
#[test]
fn the_dirty_rectangle_covers_what_was_placed() {
let mut atlas = GlyphAtlas::new(128, 1, 100);
atlas.begin_frame();
assert_eq!(atlas.dirty(0), None);
let a = atlas.glyph(sans(), glyph('W'), 18.0, 0).expect("room");
let b = atlas.glyph(sans(), glyph('g'), 30.0, 2).expect("room");
let dirty = atlas.take_dirty(0).expect("two glyphs changed the page");
for at in [a, b] {
assert!(dirty.x + GLYPH_GUTTER <= at.x && dirty.y + GLYPH_GUTTER <= at.y);
assert!(at.x + at.width + GLYPH_GUTTER <= dirty.x + dirty.width);
assert!(at.y + at.height + GLYPH_GUTTER <= dirty.y + dirty.height);
}
assert_eq!(atlas.dirty(0), None);
assert_eq!(
atlas.region(0, dirty).len(),
(dirty.width * dirty.height) as usize
);
atlas.mark_dirty(0, dirty);
assert_eq!(atlas.dirty(0), Some(dirty));
}
#[test]
fn a_glyph_larger_than_a_page_is_refused_once() {
let mut atlas = GlyphAtlas::new(32, 1, 100);
atlas.begin_frame();
assert_eq!(atlas.glyph(sans(), glyph('M'), 200.0, 0), None);
assert_eq!(atlas.stats().unplaced, 1);
assert_eq!(atlas.glyph(sans(), glyph('M'), 200.0, 0), None);
assert_eq!(atlas.stats().rasterized, 1);
assert_eq!(atlas.page_count(), 0);
}
fn mask(font: &Font, c: char) -> (Vec<Vec<f32>>, i32) {
let mut atlas = GlyphAtlas::new(256, 1, 100);
atlas.begin_frame();
let at = atlas.glyph(font, glyph(c), 48.0, 0).expect("room");
let page = atlas.page_pixels(0);
let rows = (0..at.height)
.map(|y| {
(0..at.width)
.map(|x| f32::from(page[((at.y + y) * 256 + at.x + x) as usize]) / 255.0)
.collect()
})
.collect();
(rows, at.left)
}
fn row_centroid(row: &[f32], left: i32) -> f32 {
let (mut sum, mut weight) = (0.0, 0.0);
for (x, c) in row.iter().enumerate() {
sum += c * (left as f32 + x as f32 + 0.5);
weight += c;
}
sum / weight
}
#[test]
fn a_synthesised_italic_shears_the_outline_by_the_slant() {
let lean = |font: &Font| {
let (rows, left) = mask(font, 'l');
let (top, bottom) = (rows.len() / 4, rows.len() * 3 / 4);
let rise = (bottom - top) as f32;
(row_centroid(&rows[top], left) - row_centroid(&rows[bottom], left)) / rise
};
let italic = sans().synthesized(Synthesis {
bold: false,
italic: true,
});
assert!(lean(sans()).abs() < 0.02, "upright leans {}", lean(sans()));
assert!(
(lean(&italic) - SYNTHETIC_ITALIC_SLANT).abs() < 0.02,
"synthesised italic leans {} rather than {SYNTHETIC_ITALIC_SLANT}",
lean(&italic)
);
}
#[test]
fn a_synthesised_bold_thickens_each_stroke_by_its_strength() {
let ink = |font: &Font| {
let (rows, _) = mask(font, 'l');
let middle = &rows[rows.len() / 2];
middle.iter().sum::<f32>()
};
let bold = sans().synthesized(Synthesis {
bold: true,
italic: false,
});
let strength = 48.0 * SYNTHETIC_BOLD_EM;
let thicker = ink(&bold) - ink(sans());
assert!(
(thicker - strength).abs() < 0.1,
"the stem is {thicker}px thicker rather than {strength}px"
);
let units = f32::from(sans().metrics().units_per_em);
assert_eq!(
bold.advance(glyph('l')),
sans().advance(glyph('l')) + units * SYNTHETIC_BOLD_EM
);
assert_ne!(bold.id(), sans().id(), "the bold glyphs share a cache key");
assert_eq!(
bold,
sans().synthesized(bold.synthesis()),
"asking again is another identity"
);
}
}