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
29fn assert_non_negative_finite_f32(caller: &str, name: &str, value: f32) {
30    assert!(value.is_finite(), "{caller} {name} must be finite");
31    assert!(value >= 0.0, "{caller} {name} must be non-negative");
32}
33
34fn assert_positive_finite_f32(caller: &str, name: &str, value: f32) {
35    assert!(value.is_finite(), "{caller} {name} must be finite");
36    assert!(value > 0.0, "{caller} {name} must be positive");
37}
38
39/// # Fonts
40impl Ui {
41    /// Returns the current font
42    #[doc(alias = "GetFont")]
43    pub fn current_font(&self) -> &Font {
44        unsafe { Font::from_raw(crate::sys::igGetFont() as *const _) }
45    }
46
47    /// Returns the current font size (= height in pixels) with font scale applied
48    #[doc(alias = "GetFontSize")]
49    pub fn current_font_size(&self) -> f32 {
50        unsafe { crate::sys::igGetFontSize() }
51    }
52
53    /// Push a font with dynamic size support (v1.92+ feature)
54    ///
55    /// This allows changing font size at runtime without pre-loading different sizes.
56    /// Pass None for font to use the current font with the new size.
57    pub fn push_font_with_size(&self, font: Option<&Font>, size: f32) {
58        assert_non_negative_finite_f32("Ui::push_font_with_size()", "size", size);
59        unsafe {
60            let font_ptr = font.map_or(std::ptr::null_mut(), |f| f.raw());
61            crate::sys::igPushFont(font_ptr, size);
62        }
63    }
64
65    /// Execute a closure with a specific font and size (v1.92+ dynamic fonts)
66    pub fn with_font_and_size<F, R>(&self, font: Option<&Font>, size: f32, f: F) -> R
67    where
68        F: FnOnce() -> R,
69    {
70        self.push_font_with_size(font, size);
71        let _token = crate::FontStackToken::new(self);
72        f()
73    }
74
75    /// Returns the UV coordinate for a white pixel.
76    ///
77    /// Useful for drawing custom shapes with the draw list API.
78    #[doc(alias = "GetFontTexUvWhitePixel")]
79    pub fn font_tex_uv_white_pixel(&self) -> [f32; 2] {
80        unsafe {
81            let uv = crate::sys::igGetFontTexUvWhitePixel();
82            [uv.x, uv.y]
83        }
84    }
85
86    /// Sets the legacy per-window font scale of the current window.
87    ///
88    /// Prefer [`Ui::push_font_with_size`] or `style.FontScaleMain` for new code.
89    #[doc(alias = "SetWindowFontScale")]
90    pub fn set_window_font_scale(&self, scale: f32) {
91        assert_positive_finite_f32("Ui::set_window_font_scale()", "scale", scale);
92
93        unsafe {
94            let window = crate::sys::igGetCurrentWindow();
95            if window.is_null() {
96                return;
97            }
98            (*window).FontWindowScale = scale;
99            crate::sys::igUpdateCurrentFontSize(0.0);
100        }
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    fn setup_context() -> crate::Context {
107        let mut ctx = crate::Context::create();
108        let _ = ctx.font_atlas_mut().build();
109        ctx.io_mut().set_display_size([128.0, 128.0]);
110        ctx.io_mut().set_delta_time(1.0 / 60.0);
111        ctx
112    }
113
114    #[test]
115    fn set_window_font_scale_updates_current_window_state() {
116        let mut ctx = setup_context();
117        let ui = ctx.frame();
118
119        ui.window("font_scale_test").build(|| {
120            let window = unsafe { crate::sys::igGetCurrentWindowRead() };
121            assert!(!window.is_null());
122            assert_eq!(unsafe { (*window).FontWindowScale }, 1.0);
123
124            ui.set_window_font_scale(1.5);
125
126            assert_eq!(unsafe { (*window).FontWindowScale }, 1.5);
127        });
128    }
129
130    #[test]
131    fn font_runtime_size_setters_validate_before_ffi() {
132        let mut ctx = setup_context();
133        let raw_ctx = ctx.as_raw();
134        let ui = ctx.frame();
135
136        let initial_stack_size = unsafe { (*raw_ctx).FontStack.Size };
137        ui.with_font_and_size(None, 0.0, || {});
138        assert_eq!(unsafe { (*raw_ctx).FontStack.Size }, initial_stack_size);
139
140        assert!(
141            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
142                ui.push_font_with_size(None, -1.0);
143            }))
144            .is_err()
145        );
146        assert_eq!(unsafe { (*raw_ctx).FontStack.Size }, initial_stack_size);
147
148        ui.window("font_scale_invalid").build(|| {
149            let window = unsafe { crate::sys::igGetCurrentWindowRead() };
150            assert_eq!(unsafe { (*window).FontWindowScale }, 1.0);
151
152            assert!(
153                std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
154                    ui.set_window_font_scale(f32::INFINITY);
155                }))
156                .is_err()
157            );
158            assert_eq!(unsafe { (*window).FontWindowScale }, 1.0);
159        });
160    }
161
162    #[test]
163    fn with_font_and_size_pops_after_panic() {
164        let mut ctx = setup_context();
165        let raw_ctx = ctx.as_raw();
166        let ui = ctx.frame();
167
168        let initial_stack_size = unsafe { (*raw_ctx).FontStack.Size };
169        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
170            ui.with_font_and_size(None, 18.0, || {
171                assert_eq!(unsafe { (*raw_ctx).FontStack.Size }, initial_stack_size + 1);
172                panic!("forced panic while font is pushed");
173            });
174        }));
175
176        assert!(result.is_err());
177        assert_eq!(unsafe { (*raw_ctx).FontStack.Size }, initial_stack_size);
178    }
179}