pub struct Glyph {
pub id: u16,
pub style: FontStyle,
pub symbol: CompactString,
pub pixel_coords: (i32, i32),
pub is_emoji: bool,
}Expand description
Represents a single character glyph in a font atlas texture.
A Glyph contains the metadata needed to locate and identify a character
within a font atlas texture. Each glyph has a unique ID that maps
to its coordinates in a WebGL TEXTURE_2D_ARRAY.
§ASCII Optimization
For ASCII characters, the glyph ID directly corresponds to the character’s ASCII value, enabling fast lookups without hash table lookups. Non-ASCII characters are assigned sequential IDs starting from a base value.
§Glyph ID Bit Layout (16-bit)
| Bit(s) | Flag Name | Hex Mask | Binary Mask | Description |
|---|---|---|---|---|
| 0-8 | GLYPH_ID | 0x01FF | 0000_0001_1111_1111 | Base glyph identifier |
| 9 | BOLD | 0x0200 | 0000_0010_0000_0000 | Bold font style |
| 10 | ITALIC | 0x0400 | 0000_0100_0000_0000 | Italic font style |
| 11 | EMOJI | 0x0800 | 0000_1000_0000_0000 | Emoji character flag |
| 12 | UNDERLINE | 0x1000 | 0001_0000_0000_0000 | Underline effect |
| 13 | STRIKETHROUGH | 0x2000 | 0010_0000_0000_0000 | Strikethrough effect |
| 14-15 | RESERVED | 0xC000 | 1100_0000_0000_0000 | Reserved for future use |
- The first 9 bits (0-8) represent the base glyph ID, allowing for 512 unique glyphs.
- Emoji glyphs implicitly clear any other font style bits.
- The fragment shader uses the glyph ID to decode the texture coordinates and effects.
§Glyph ID Encoding Examples
| Character | Style | Binary Representation | Hex Value | Description |
|---|---|---|---|---|
| ‘A’ (0x41) | Normal | 0000_0000_0100_0001 | 0x0041 | Plain ‘A’ |
| ‘A’ (0x41) | Bold | 0000_0010_0100_0001 | 0x0241 | Bold ‘A’ |
| ‘A’ (0x41) | Bold + Italic | 0000_0110_0100_0001 | 0x0641 | Bold italic ‘A’ |
| ‘A’ (0x41) | Bold + Underline | 0000_1010_0100_0001 | 0x0A41 | Bold underlined ‘A’ |
| ‘🚀’ (0x81) | Emoji | 1000_0000_1000_0001 | 0x0881 | “rocket” emoji |
Fields§
§id: u16The glyph ID; encodes the 3d texture coordinates
style: FontStyleThe style of the glyph, e.g., bold, italic
symbol: CompactStringThe character
pixel_coords: (i32, i32)The pixel coordinates of the glyph in the texture
is_emoji: boolIndicates if the glyph is an emoji
Implementations§
Source§impl Glyph
impl Glyph
Sourcepub const UNASSIGNED_ID: u16 = 65_535u16
pub const UNASSIGNED_ID: u16 = 65_535u16
The ID is used as a short-lived placeholder until the actual ID is assigned.
Sourcepub const GLYPH_ID_MASK: u16 = 511u16
pub const GLYPH_ID_MASK: u16 = 511u16
Glyph ID mask - extracts the base glyph identifier (bits 0-8). Supports 512 unique base glyphs (0x000 to 0x1FF) in the texture atlas.
Sourcepub const BOLD_FLAG: u16 = 512u16
pub const BOLD_FLAG: u16 = 512u16
Bold flag - selects the bold variant of the glyph from the texture atlas.
Sourcepub const ITALIC_FLAG: u16 = 1_024u16
pub const ITALIC_FLAG: u16 = 1_024u16
Italic flag - selects the italic variant of the glyph from the texture atlas.
Sourcepub const EMOJI_FLAG: u16 = 2_048u16
pub const EMOJI_FLAG: u16 = 2_048u16
Emoji flag - indicates this glyph represents an emoji character requiring special handling.
Sourcepub const UNDERLINE_FLAG: u16 = 4_096u16
pub const UNDERLINE_FLAG: u16 = 4_096u16
Underline flag - renders a horizontal line below the character baseline.
Sourcepub const STRIKETHROUGH_FLAG: u16 = 8_192u16
pub const STRIKETHROUGH_FLAG: u16 = 8_192u16
Strikethrough flag - renders a horizontal line through the middle of the character.