dear_imgui_rs/fonts/
mod.rs1pub mod atlas;
7pub mod font;
8pub mod glyph;
9#[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
29impl Ui {
31 #[doc(alias = "GetFont")]
33 pub fn current_font(&self) -> &Font {
34 unsafe { Font::from_raw(crate::sys::igGetFont() as *const _) }
35 }
36
37 #[doc(alias = "GetFontSize")]
39 pub fn current_font_size(&self) -> f32 {
40 unsafe { crate::sys::igGetFontSize() }
41 }
42
43 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 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 #[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 #[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}