Skip to content

Commit

Permalink
Fix font usage before loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Aug 11, 2024
1 parent eea8388 commit dc042ed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/addon/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Addon {

pub fn render_displays(&mut self, ui: &Ui) {
if self.context.ui.should_show() || self.context.edit.is_editing() {
let _font = self.context.font.map(|font| font.push());
let _font = self.context.font.and_then(|font| font.push());
for pack in &mut self.packs {
pack.render(ui, &self.context);
}
Expand Down
2 changes: 1 addition & 1 deletion src/elements/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Render for Text {
self.update(ctx, state);

if let Some(text) = &self.text_memo {
let _font = self.loaded_font.map(|font| font.push());
let _font = self.loaded_font.and_then(|font| font.push());
let font_scale = self.props.scale;
let pos = self.calc_pos(ui, state.pos, text);
let [r, g, b, a] = self.props.color;
Expand Down
30 changes: 14 additions & 16 deletions src/render_util/font.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nexus::imgui::{sys, ComboBoxFlags, Selectable, SelectableFlags, Ui};
use std::{
borrow::Cow,
ffi::{c_char, CStr, CString},
ffi::{CStr, CString},
ptr::NonNull,
slice,
};
Expand Down Expand Up @@ -38,29 +38,27 @@ impl Font {
result
}

pub unsafe fn name_ptr(&self) -> *const [c_char] {
let font = self.0.as_ref();
let config = font.ConfigData.as_ref().expect("font config is null");
config.Name.as_slice() as *const _
pub fn as_ptr(&self) -> *mut sys::ImFont {
self.0.as_ptr()
}

pub fn is_loaded(&self) -> bool {
unsafe { sys::ImFont_IsLoaded(self.as_ptr()) }
}

pub unsafe fn name_raw<'a>(&self) -> &'a CStr {
let name = self.name_ptr();
let bytes = (name as *const [u8]).as_ref().expect("font name is null");
CStr::from_bytes_until_nul(bytes).expect("font name without nul terminator")
CStr::from_ptr(sys::ImFont_GetDebugName(self.as_ptr()))
}

pub fn name_owned(&self) -> String {
unsafe { self.name_raw() }.to_string_lossy().into_owned()
}

pub fn as_ptr(&self) -> *mut sys::ImFont {
self.0.as_ptr()
}

pub fn push(&self) -> FontToken {
unsafe { sys::igPushFont(self.as_ptr()) }
FontToken
pub fn push(&self) -> Option<FontToken> {
self.is_loaded().then(|| {
unsafe { sys::igPushFont(self.as_ptr()) };
FontToken
})
}
}

Expand Down Expand Up @@ -93,7 +91,7 @@ pub fn font_select(ui: &Ui, label: impl AsRef<str>, current: &mut Option<Font>)
let is_selected = Some(font) == *current;
if unsafe {
sys::igSelectable_Bool(
font.name_ptr().cast(),
font.name_raw().as_ptr(),
is_selected,
SelectableFlags::empty().bits() as i32,
[0.0, 0.0].into(),
Expand Down

0 comments on commit dc042ed

Please sign in to comment.