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 font scale of the current window
79 ///
80 /// Note: This function is not available in our Dear ImGui version.
81 /// Font scaling should be handled through font size instead.
82 #[doc(alias = "SetWindowFontScale")]
83 pub fn set_window_font_scale(&self, _scale: f32) {
84 // TODO: Implement when SetWindowFontScale is available in our Dear ImGui version
85 // unsafe { crate::sys::igSetWindowFontScale(scale) }
86 }
87}