dear_imgui/fonts/
glyph.rs

1//! Font glyph data structures
2//!
3//! This module provides types for working with individual font glyphs
4//! and their rendering data.
5
6use crate::sys;
7
8/// A single font glyph with its rendering data
9///
10/// This represents a single character glyph with its texture coordinates,
11/// advance information, and other rendering data.
12#[derive(Debug, Clone, Copy)]
13pub struct Glyph {
14    /// The raw ImFontGlyph data
15    pub(crate) raw: sys::ImFontGlyph,
16}
17
18impl Glyph {
19    /// Creates a Glyph from raw ImFontGlyph data
20    pub fn from_raw(raw: sys::ImFontGlyph) -> Self {
21        Self { raw }
22    }
23
24    /// Get the Unicode codepoint for this glyph
25    pub fn codepoint(&self) -> u32 {
26        self.raw.Codepoint()
27    }
28
29    /// Get the visibility flag for this glyph
30    pub fn visible(&self) -> bool {
31        self.raw.Visible() != 0
32    }
33
34    /// Get the advance X value for this glyph
35    pub fn advance_x(&self) -> f32 {
36        self.raw.AdvanceX
37    }
38
39    /// Get the texture coordinates for this glyph
40    pub fn tex_coords(&self) -> ([f32; 2], [f32; 2]) {
41        ([self.raw.U0, self.raw.V0], [self.raw.U1, self.raw.V1])
42    }
43
44    /// Get the glyph position and size
45    pub fn position_and_size(&self) -> ([f32; 2], [f32; 2]) {
46        ([self.raw.X0, self.raw.Y0], [self.raw.X1, self.raw.Y1])
47    }
48}
49
50impl From<sys::ImFontGlyph> for Glyph {
51    fn from(raw: sys::ImFontGlyph) -> Self {
52        Self::from_raw(raw)
53    }
54}