beamterm_data/atlas.rs
1use std::fmt::Debug;
2
3use compact_str::CompactString;
4
5use crate::{Deserializer, FontAtlasDeserializationError, Glyph, Serializable};
6
7/// Font atlas data for GPU-accelerated terminal rendering.
8///
9/// Contains a pre-rasterized font atlas stored as a 2D texture array, where each layer
10/// holds 32 glyphs in a 1×32 grid. The atlas includes multiple font styles (normal, bold,
11/// italic, bold+italic) and full Unicode support including emoji.
12#[derive(Clone, PartialEq)]
13pub struct FontAtlasData {
14 /// The name of the font
15 pub font_name: CompactString,
16 /// The font size in points
17 pub font_size: f32,
18 /// The number of single-cell (halfwidth) glyphs per layer, before fullwidth glyphs begin.
19 ///
20 /// Fullwidth glyphs (e.g., CJK characters) are assigned IDs starting from this value,
21 /// aligned to even boundaries. This allows the renderer to distinguish halfwidth from
22 /// fullwidth glyphs by comparing against this threshold.
23 pub max_halfwidth_base_glyph_id: u16,
24 /// Width, height and depth of the texture in pixels
25 pub texture_dimensions: (i32, i32, i32),
26 /// Width and height of each character cell
27 pub cell_size: (i32, i32),
28 /// Underline configuration
29 pub underline: LineDecoration,
30 /// Strikethrough configuration
31 pub strikethrough: LineDecoration,
32 /// The glyphs in the font
33 pub glyphs: Vec<Glyph>,
34 /// The 3d texture data containing the font glyphs
35 pub texture_data: Vec<u8>,
36}
37
38impl Debug for FontAtlasData {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 f.debug_struct("FontAtlasData")
41 .field("font_name", &self.font_name)
42 .field("font_size", &self.font_size)
43 .field("texture_dimensions", &self.texture_dimensions)
44 .field("cell_size", &self.cell_size)
45 .field("glyphs_count", &self.glyphs.len())
46 .field("texture_data_kb", &(self.texture_data.len() * 4 / 1024))
47 .finish()
48 }
49}
50
51impl FontAtlasData {
52 pub const PADDING: i32 = 1;
53 pub const CELLS_PER_SLICE: i32 = 32;
54
55 /// Deserializes a font atlas from binary format.
56 ///
57 /// # Arguments
58 /// * `serialized` - Binary data containing the serialized font atlas
59 ///
60 /// # Returns
61 /// The deserialized font atlas or an error if deserialization fails
62 pub fn from_binary(serialized: &[u8]) -> Result<Self, FontAtlasDeserializationError> {
63 let mut deserializer = Deserializer::new(serialized);
64 FontAtlasData::deserialize(&mut deserializer).map_err(|e| FontAtlasDeserializationError {
65 message: format!("Failed to deserialize font atlas: {}", e.message),
66 })
67 }
68
69 /// Serializes the font atlas to binary format.
70 ///
71 /// # Returns
72 /// A byte vector containing the serialized font atlas data
73 pub fn to_binary(&self) -> Vec<u8> {
74 self.serialize()
75 }
76
77 /// Calculates how many terminal columns and rows fit in the given viewport dimensions.
78 ///
79 /// # Arguments
80 /// * `viewport_width` - Width of the viewport in pixels
81 /// * `viewport_height` - Height of the viewport in pixels
82 ///
83 /// # Returns
84 /// A tuple of (columns, rows) that fit in the viewport
85 pub fn terminal_size(&self, viewport_width: i32, viewport_height: i32) -> (i32, i32) {
86 (
87 viewport_width / self.cell_size.0,
88 viewport_height / self.cell_size.1,
89 )
90 }
91
92 /// Returns the padded terminal cell size.
93 ///
94 /// The cell size includes padding (1 pixel on each side, 2 pixels total per dimension)
95 /// to prevent texture bleeding artifacts during GPU rendering.
96 ///
97 /// # Returns
98 /// A tuple of (width, height) in pixels for each terminal cell
99 pub fn cell_size(&self) -> (i32, i32) {
100 self.cell_size
101 }
102}
103
104impl Default for FontAtlasData {
105 fn default() -> Self {
106 Self::from_binary(include_bytes!("../atlas/bitmap_font.atlas")).unwrap()
107 }
108}
109
110#[derive(Copy, Clone, Debug, PartialEq)]
111pub struct LineDecoration {
112 /// 0.0 to 1.0, where 0.0 is the top of the text line and 1.0 is the bottom.
113 pub position: f32,
114 /// Thickness of the line as a fraction of the cell height (0.0 to 1.0)
115 pub thickness: f32,
116}
117
118impl LineDecoration {
119 pub fn new(position: f32, thickness: f32) -> Self {
120 Self {
121 position: position.clamp(0.0, 1.0),
122 thickness: thickness.clamp(0.0, 1.0),
123 }
124 }
125}
126
127/// Debug pattern for validating pixel-perfect rendering of cell dimensions.
128///
129/// When enabled, replaces the space glyph with a checkered pattern to help
130/// verify that cell boundaries align correctly with pixel boundaries.
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub enum DebugSpacePattern {
133 /// 1px alternating checkerboard pattern
134 OnePixel,
135 /// 2x2 pixel checkerboard pattern
136 TwoByTwo,
137}