Skip to main content

dear_imgui_rs/fonts/
mod.rs

1//! Font system for Dear ImGui
2//!
3//! This module provides font management functionality including font atlases,
4//! individual fonts, glyph ranges, and font configuration.
5
6pub mod atlas;
7pub mod font;
8pub mod glyph;
9/// Deprecated glyph ranges helpers.
10///
11/// With Dear ImGui 1.92+, fonts are dynamically sized and glyphs are loaded on demand.
12/// In most cases you no longer need to specify glyph ranges. Keep using this module
13/// only for legacy code or very constrained environments where you explicitly want to
14/// limit the character set.
15#[deprecated(
16    since = "0.2.0",
17    note = "ImGui 1.92+ recommends dynamic fonts with on-demand glyph loading; glyph ranges are kept for legacy compatibility"
18)]
19pub mod glyph_ranges;
20
21pub use atlas::*;
22pub use font::*;
23pub use glyph::*;
24#[allow(deprecated)]
25pub use glyph_ranges::*;
26
27use crate::Ui;
28
29/// # Fonts
30impl Ui {
31    /// Returns the current font
32    #[doc(alias = "GetFont")]
33    pub fn current_font(&self) -> &Font {
34        unsafe { Font::from_raw(crate::sys::igGetFont() as *const _) }
35    }
36
37    /// Returns the current font size (= height in pixels) with font scale applied
38    #[doc(alias = "GetFontSize")]
39    pub fn current_font_size(&self) -> f32 {
40        unsafe { crate::sys::igGetFontSize() }
41    }
42
43    /// Push a font with dynamic size support (v1.92+ feature)
44    ///
45    /// This allows changing font size at runtime without pre-loading different sizes.
46    /// Pass None for font to use the current font with the new size.
47    pub fn push_font_with_size(&self, font: Option<&Font>, size: f32) {
48        unsafe {
49            let font_ptr = font.map_or(std::ptr::null_mut(), |f| f.raw());
50            crate::sys::igPushFont(font_ptr, size);
51        }
52    }
53
54    /// Execute a closure with a specific font and size (v1.92+ dynamic fonts)
55    pub fn with_font_and_size<F, R>(&self, font: Option<&Font>, size: f32, f: F) -> R
56    where
57        F: FnOnce() -> R,
58    {
59        self.push_font_with_size(font, size);
60        let result = f();
61        unsafe {
62            crate::sys::igPopFont();
63        }
64        result
65    }
66
67    /// Returns the UV coordinate for a white pixel.
68    ///
69    /// Useful for drawing custom shapes with the draw list API.
70    #[doc(alias = "GetFontTexUvWhitePixel")]
71    pub fn font_tex_uv_white_pixel(&self) -> [f32; 2] {
72        unsafe {
73            let uv = crate::sys::igGetFontTexUvWhitePixel();
74            [uv.x, uv.y]
75        }
76    }
77
78    /// Sets the legacy per-window font scale of the current window.
79    ///
80    /// Prefer [`Ui::push_font_with_size`] or `style.FontScaleMain` for new code.
81    #[doc(alias = "SetWindowFontScale")]
82    pub fn set_window_font_scale(&self, scale: f32) {
83        assert!(scale > 0.0, "window font scale must be positive");
84
85        unsafe {
86            let window = crate::sys::igGetCurrentWindow();
87            if window.is_null() {
88                return;
89            }
90            (*window).FontWindowScale = scale;
91            crate::sys::igUpdateCurrentFontSize(0.0);
92        }
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    #[test]
99    fn set_window_font_scale_updates_current_window_state() {
100        let mut ctx = crate::Context::create();
101        let _ = ctx.font_atlas_mut().build();
102        ctx.io_mut().set_display_size([128.0, 128.0]);
103        ctx.io_mut().set_delta_time(1.0 / 60.0);
104        let ui = ctx.frame();
105
106        ui.window("font_scale_test").build(|| {
107            let window = unsafe { crate::sys::igGetCurrentWindowRead() };
108            assert!(!window.is_null());
109            assert_eq!(unsafe { (*window).FontWindowScale }, 1.0);
110
111            ui.set_window_font_scale(1.5);
112
113            assert_eq!(unsafe { (*window).FontWindowScale }, 1.5);
114        });
115    }
116}