Struct Glyph

Source
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 NameHex MaskBinary MaskDescription
0-8GLYPH_ID0x01FF0000_0001_1111_1111Base glyph identifier
9BOLD0x02000000_0010_0000_0000Bold font style
10ITALIC0x04000000_0100_0000_0000Italic font style
11EMOJI0x08000000_1000_0000_0000Emoji character flag
12UNDERLINE0x10000001_0000_0000_0000Underline effect
13STRIKETHROUGH0x20000010_0000_0000_0000Strikethrough effect
14-15RESERVED0xC0001100_0000_0000_0000Reserved 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

CharacterStyleBinary RepresentationHex ValueDescription
‘A’ (0x41)Normal0000_0000_0100_00010x0041Plain ‘A’
‘A’ (0x41)Bold0000_0010_0100_00010x0241Bold ‘A’
‘A’ (0x41)Bold + Italic0000_0110_0100_00010x0641Bold italic ‘A’
‘A’ (0x41)Bold + Underline0000_1010_0100_00010x0A41Bold underlined ‘A’
‘🚀’ (0x81)Emoji1000_0000_1000_00010x0881“rocket” emoji

Fields§

§id: u16

The glyph ID; encodes the 3d texture coordinates

§style: FontStyle

The style of the glyph, e.g., bold, italic

§symbol: CompactString

The character

§pixel_coords: (i32, i32)

The pixel coordinates of the glyph in the texture

§is_emoji: bool

Indicates if the glyph is an emoji

Implementations§

Source§

impl Glyph

Source

pub const UNASSIGNED_ID: u16 = 65_535u16

The ID is used as a short-lived placeholder until the actual ID is assigned.

Source

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.

Source

pub const BOLD_FLAG: u16 = 512u16

Bold flag - selects the bold variant of the glyph from the texture atlas.

Source

pub const ITALIC_FLAG: u16 = 1_024u16

Italic flag - selects the italic variant of the glyph from the texture atlas.

Source

pub const EMOJI_FLAG: u16 = 2_048u16

Emoji flag - indicates this glyph represents an emoji character requiring special handling.

Source

pub const UNDERLINE_FLAG: u16 = 4_096u16

Underline flag - renders a horizontal line below the character baseline.

Source

pub const STRIKETHROUGH_FLAG: u16 = 8_192u16

Strikethrough flag - renders a horizontal line through the middle of the character.

Source§

impl Glyph

Source

pub fn new(symbol: &str, style: FontStyle, pixel_coords: (i32, i32)) -> Self

Creates a new glyph with the specified symbol and pixel coordinates.

Source

pub fn new_with_id( base_id: u16, symbol: &str, style: FontStyle, pixel_coords: (i32, i32), ) -> Self

Source

pub fn is_ascii(&self) -> bool

Returns true if this glyph represents a single ASCII character.

Trait Implementations§

Source§

impl Debug for Glyph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Glyph

Source§

fn eq(&self, other: &Glyph) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Glyph

Source§

impl StructuralPartialEq for Glyph

Auto Trait Implementations§

§

impl Freeze for Glyph

§

impl RefUnwindSafe for Glyph

§

impl Send for Glyph

§

impl Sync for Glyph

§

impl Unpin for Glyph

§

impl UnwindSafe for Glyph

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.