pub mod atlas;
pub mod font;
pub mod glyph;
#[deprecated(
since = "0.2.0",
note = "ImGui 1.92+ recommends dynamic fonts with on-demand glyph loading; glyph ranges are kept for legacy compatibility"
)]
pub mod glyph_ranges;
pub use atlas::*;
pub use font::*;
pub use glyph::*;
#[allow(deprecated)]
pub use glyph_ranges::*;
use crate::Ui;
impl Ui {
#[doc(alias = "GetFont")]
pub fn current_font(&self) -> &Font {
unsafe { Font::from_raw(crate::sys::igGetFont() as *const _) }
}
#[doc(alias = "GetFontSize")]
pub fn current_font_size(&self) -> f32 {
unsafe { crate::sys::igGetFontSize() }
}
pub fn push_font_with_size(&self, font: Option<&Font>, size: f32) {
unsafe {
let font_ptr = font.map_or(std::ptr::null_mut(), |f| f.raw());
crate::sys::igPushFont(font_ptr, size);
}
}
pub fn with_font_and_size<F, R>(&self, font: Option<&Font>, size: f32, f: F) -> R
where
F: FnOnce() -> R,
{
self.push_font_with_size(font, size);
let result = f();
unsafe {
crate::sys::igPopFont();
}
result
}
#[doc(alias = "GetFontTexUvWhitePixel")]
pub fn font_tex_uv_white_pixel(&self) -> [f32; 2] {
unsafe {
let uv = crate::sys::igGetFontTexUvWhitePixel();
[uv.x, uv.y]
}
}
#[doc(alias = "SetWindowFontScale")]
pub fn set_window_font_scale(&self, scale: f32) {
assert!(scale > 0.0, "window font scale must be positive");
unsafe {
let window = crate::sys::igGetCurrentWindow();
if window.is_null() {
return;
}
(*window).FontWindowScale = scale;
crate::sys::igUpdateCurrentFontSize(0.0);
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn set_window_font_scale_updates_current_window_state() {
let mut ctx = crate::Context::create();
let _ = ctx.font_atlas_mut().build();
ctx.io_mut().set_display_size([128.0, 128.0]);
ctx.io_mut().set_delta_time(1.0 / 60.0);
let ui = ctx.frame();
ui.window("font_scale_test").build(|| {
let window = unsafe { crate::sys::igGetCurrentWindowRead() };
assert!(!window.is_null());
assert_eq!(unsafe { (*window).FontWindowScale }, 1.0);
ui.set_window_font_scale(1.5);
assert_eq!(unsafe { (*window).FontWindowScale }, 1.5);
});
}
}