Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to Hyprburst are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Native and Rio icons are no longer about half the text height. An icon
followed by a blank cell (the usual `icon name` layout) is now fitted
across two cells, like a double-width glyph in a terminal, instead of being
squeezed into one text cell. Icons with no room next to them keep the
one-cell size.

## [0.9.0] - 2026-10-17

### Added
Expand Down
130 changes: 126 additions & 4 deletions src/gpu/font.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Monospace-font resolution for the GUI launcher.
//!
//! Both GPU frontends retain the requested primary text font and search fallback
//! fonts for missing glyphs. Private-use icons are fitted to the primary cell;
//! without an icon font, a procedural four-tile app symbol avoids missing-glyph
//! boxes. The `tui` fallback still uses the hosting terminal's font.
//! fonts for missing glyphs. Private-use icons are fitted across two primary
//! cells when the next cell is blank (one cell otherwise), so the icon, not the
//! text face's advance width, sets their size; without an icon font, a
//! procedural four-tile app symbol avoids missing-glyph boxes. The `tui` fallback still uses the hosting terminal's font.
//! Resolution order, most specific first:
//!
//! 1. an explicit path from `[font] path` in the config,
Expand Down Expand Up @@ -33,6 +34,8 @@ use std::time::SystemTime;

use ab_glyph::{Font, FontVec, PxScale};

use crate::gpu::grid::GlyphSpan;

/// Immutable font selection for one GUI session; text geometry always uses primary.
pub(crate) struct FontSet {
pub(crate) primary: FontVec,
Expand All @@ -47,13 +50,55 @@ impl FontSet {
.find(|font| font.glyph_id(ch).0 != 0)
}

/// One-cell coverage of `ch`: the span every non-icon cell uses.
#[cfg(test)]
pub(crate) fn coverage(
&self,
ch: char,
size: (u32, u32),
scale: PxScale,
ascent: f32,
) -> Vec<u8> {
self.coverage_span(ch, GlyphSpan::Single, size, scale, ascent)
}

/// One `size` tile of `ch`. For an icon, `Left`/`Right` are the halves of
/// the icon fitted across two cells; text ignores the span, and without an
/// icon font the square badge stays in the left cell.
pub(crate) fn coverage_span(
&self,
ch: char,
span: GlyphSpan,
size: (u32, u32),
scale: PxScale,
ascent: f32,
) -> Vec<u8> {
let (cw, height) = size;
let split = match span {
GlyphSpan::Single => None,
_ if !is_icon(ch) => None,
_ if self.glyph_font(ch).is_none() => {
return match span {
GlyphSpan::Right => vec![0; (cw * height) as usize],
_ => self.rasterize(ch, size, scale, ascent),
};
}
GlyphSpan::Left => Some(0),
GlyphSpan::Right => Some(cw as usize),
};
let Some(offset) = split else {
return self.rasterize(ch, size, scale, ascent);
};
let wide_w = cw.checked_mul(2).expect("cell size overflow");
let wide = self.rasterize(ch, (wide_w, height), scale, ascent);
wide.chunks_exact(wide_w as usize)
.flat_map(|row| &row[offset..offset + cw as usize])
.copied()
.collect()
}

/// Rasterize `ch` into one `size` tile; icons are fitted to the whole tile.
fn rasterize(&self, ch: char, size: (u32, u32), scale: PxScale, ascent: f32) -> Vec<u8> {
let (cw, height) = size;
let mut coverage = vec![0; cw.checked_mul(height).expect("cell size overflow") as usize];
let icon = is_icon(ch);
Expand Down Expand Up @@ -114,7 +159,7 @@ impl FontSet {
}

/// Nerd Font icons occupy Unicode's private-use areas (including supplementary).
fn is_icon(ch: char) -> bool {
pub(crate) fn is_icon(ch: char) -> bool {
matches!(ch as u32, 0xe000..=0xf8ff | 0xf0000..=0xffffd | 0x100000..=0x10fffd)
}

Expand Down Expand Up @@ -526,6 +571,83 @@ mod tests {
);
}

/// Ink bounding box `(left, right, top, bottom)` of a `w`-wide coverage map.
fn ink_bounds(pixels: &[u8], w: u32) -> (u32, u32, u32, u32) {
let points: Vec<_> = pixels
.iter()
.enumerate()
.filter(|(_, c)| **c > 0)
.map(|(i, _)| (i as u32 % w, i as u32 / w))
.collect();
assert!(!points.is_empty(), "glyph must be visible");
(
points.iter().map(|p| p.0).min().unwrap(),
points.iter().map(|p| p.0).max().unwrap(),
points.iter().map(|p| p.1).min().unwrap(),
points.iter().map(|p| p.1).max().unwrap(),
)
}

/// Join a left and a right `w`×`h` tile into one `2w`×`h` coverage map.
fn join_halves(left: &[u8], right: &[u8], w: u32, h: u32) -> Vec<u8> {
let w = w as usize;
let mut joined = Vec::with_capacity(2 * w * h as usize);
for row in 0..h as usize {
joined.extend_from_slice(&left[row * w..(row + 1) * w]);
joined.extend_from_slice(&right[row * w..(row + 1) * w]);
}
joined
}

#[test]
fn two_cell_icon_is_larger_than_one_cell_fit_and_never_clipped() {
let fonts = fixture_fonts();
let (w, h, scale, ascent) = (12, 25, PxScale::from(25.0), 20.0);
let icon = '\u{f269}';
let single = fonts.coverage(icon, (w, h), scale, ascent);
let left = fonts.coverage_span(icon, GlyphSpan::Left, (w, h), scale, ascent);
let right = fonts.coverage_span(icon, GlyphSpan::Right, (w, h), scale, ascent);
assert_eq!(left.len(), (w * h) as usize);
assert_eq!(right.len(), (w * h) as usize);
let wide = join_halves(&left, &right, w, h);

let (sl, sr, st, sb) = ink_bounds(&single, w);
let (wl, wr, wt, wb) = ink_bounds(&wide, 2 * w);
assert!(wr - wl > sr - sl, "two cells give the icon more width");
assert!(wb - wt >= sb - st, "and never less height");
assert!(
wl > 0 && wr < 2 * w - 1 && wt > 0 && wb < h - 1,
"wide icon must fit both cells without clipping"
);
assert!((wl as i32 - (2 * w - 1 - wr) as i32).abs() <= 1, "centred");
assert!(left.iter().any(|c| *c > 0) && right.iter().any(|c| *c > 0));
}

#[test]
fn text_and_badge_ignore_the_two_cell_span() {
let fonts = fixture_fonts();
let (size, scale) = ((12, 20), PxScale::from(20.0));
// Non-icons are never split: the left half is the one-cell glyph.
assert_eq!(
fonts.coverage_span('A', GlyphSpan::Left, size, scale, 16.0),
fonts.coverage('A', size, scale, 16.0)
);
// Without an icon font, the square badge stays in the left cell.
let mut fonts = fixture_fonts();
fonts.fallbacks.clear();
let icon = '\u{f269}';
assert_eq!(
fonts.coverage_span(icon, GlyphSpan::Left, size, scale, 16.0),
fonts.coverage(icon, size, scale, 16.0)
);
assert!(
fonts
.coverage_span(icon, GlyphSpan::Right, size, scale, 16.0)
.iter()
.all(|c| *c == 0)
);
}

#[test]
fn small_icon_outlines_are_enlarged_to_remain_legible() {
let fonts = fixture_fonts();
Expand Down
137 changes: 135 additions & 2 deletions src/gpu/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ pub struct CellInstance {
pub color: [f32; 3],
}

/// The atlas tiles that draw `glyphs`, each with the cell it lands in. An icon
/// whose right-hand neighbour is blank and inside the grid spans both cells
/// (left half, right half), like a double-width glyph in a terminal; otherwise
/// every glyph is one tile. `glyphs` must be row-major (both producers are), so
/// the neighbour, if any, is the next entry.
pub fn glyph_tiles(
glyphs: &[CellInstance],
cols: u16,
) -> impl Iterator<Item = (CellInstance, GlyphKey)> + '_ {
glyphs.iter().enumerate().flat_map(move |(i, c)| {
let next_free = c.col.checked_add(1).is_some_and(|next| next < cols)
&& glyphs
.get(i + 1)
.is_none_or(|n| n.row != c.row || n.col != c.col + 1);
let (first, second) = if next_free && crate::gpu::font::is_icon(c.ch) {
let right = CellInstance {
col: c.col + 1,
..*c
};
(
(*c, GlyphKey::half(c.ch, GlyphSpan::Left)),
Some((right, GlyphKey::half(c.ch, GlyphSpan::Right))),
)
} else {
((*c, GlyphKey::new(c.ch)), None)
};
std::iter::once(first).chain(second)
})
}

/// Map a window's pixel size to a grid: as many whole cells as fit, at least 1×1.
/// The remainder pixels are left as padding the renderer can letterbox. The
/// *total* cell count is capped at `u16::MAX`: the renderer iterates cells
Expand Down Expand Up @@ -115,15 +145,32 @@ pub fn cell_at_pixel(
/// (the shader tints a single white-on-clear glyph), and neither does weight:
/// the rasterizer draws one regular-weight bitmap per character, so keying bold
/// separately would just burn a second atlas slot for an identical tile. If
/// true bold rasterization lands later, add the flag back here.
/// true bold rasterization lands later, add the flag back here. The span does
/// key the slot: a two-cell icon is two tiles, distinct from its one-cell form.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct GlyphKey {
pub ch: char,
pub span: GlyphSpan,
}

/// Which part of a glyph a one-cell tile holds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GlyphSpan {
/// The whole glyph, fitted to one cell.
Single,
/// The left cell of an icon fitted across two cells.
Left,
/// The right cell of an icon fitted across two cells.
Right,
}

impl GlyphKey {
pub fn new(ch: char) -> Self {
Self { ch }
Self::half(ch, GlyphSpan::Single)
}

pub fn half(ch: char, span: GlyphSpan) -> Self {
Self { ch, span }
}
}

Expand Down Expand Up @@ -323,6 +370,92 @@ mod tests {
assert_eq!(atlas.len(), 1);
}

#[test]
fn atlas_keys_each_half_of_a_wide_icon_to_its_own_slot() {
// A two-cell icon is drawn as two tiles; neither half may alias the
// one-cell rendering of the same character.
let mut atlas = Atlas::new((64, 64), CellMetrics::new(16, 16));
let icon = '\u{f269}';
let single = atlas.get_or_insert(GlyphKey::new(icon)).unwrap();
let left = atlas
.get_or_insert(GlyphKey::half(icon, GlyphSpan::Left))
.unwrap();
let right = atlas
.get_or_insert(GlyphKey::half(icon, GlyphSpan::Right))
.unwrap();
assert!(left.newly_inserted && right.newly_inserted);
let mut indices = [single.index, left.index, right.index];
indices.sort_unstable();
assert_eq!(indices, [0, 1, 2]);
assert_eq!(GlyphKey::new(icon).span, GlyphSpan::Single);
}

fn glyph(col: u16, row: u16, ch: char) -> CellInstance {
CellInstance {
col,
row,
ch,
bold: false,
color: [1.0; 3],
}
}

fn tiles(glyphs: &[CellInstance], cols: u16) -> Vec<(u16, u16, GlyphKey)> {
glyph_tiles(glyphs, cols)
.map(|(c, key)| (c.col, c.row, key))
.collect()
}

const ICON: char = '\u{f269}';

#[test]
fn icon_before_a_blank_cell_spans_two_cells() {
// "> <icon> Firefox": the space after the icon is not a glyph.
let glyphs = [glyph(0, 0, '>'), glyph(2, 0, ICON), glyph(4, 0, 'F')];
assert_eq!(
tiles(&glyphs, 10),
vec![
(0, 0, GlyphKey::new('>')),
(2, 0, GlyphKey::half(ICON, GlyphSpan::Left)),
(3, 0, GlyphKey::half(ICON, GlyphSpan::Right)),
(4, 0, GlyphKey::new('F')),
]
);
}

#[test]
fn icon_with_an_occupied_neighbour_or_at_the_edge_stays_one_cell() {
let crowded = [glyph(0, 0, ICON), glyph(1, 0, 'x')];
assert_eq!(
tiles(&crowded, 10),
vec![(0, 0, GlyphKey::new(ICON)), (1, 0, GlyphKey::new('x'))]
);
let edge = [glyph(9, 0, ICON)];
assert_eq!(tiles(&edge, 10), vec![(9, 0, GlyphKey::new(ICON))]);
}

#[test]
fn icon_spans_when_the_next_glyph_is_on_the_next_row() {
let glyphs = [glyph(3, 0, ICON), glyph(4, 1, 'x')];
assert_eq!(
tiles(&glyphs, 10),
vec![
(3, 0, GlyphKey::half(ICON, GlyphSpan::Left)),
(4, 0, GlyphKey::half(ICON, GlyphSpan::Right)),
(4, 1, GlyphKey::new('x')),
]
);
}

#[test]
fn text_is_never_widened() {
let glyphs = [glyph(0, 0, 'a'), glyph(2, 0, 'b')];
assert_eq!(
tiles(&glyphs, 10),
vec![(0, 0, GlyphKey::new('a')), (2, 0, GlyphKey::new('b'))]
);
}

#[test]
fn atlas_lays_slots_out_row_major() {
// 64/16 = 4 columns. Slot 0 at (0,0), slot 4 wraps to row 1 at (0,16).
Expand Down
Loading
Loading