Skip to content

Commit

Permalink
Add font panic messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Aug 11, 2024
1 parent 3d22643 commit 9120d44
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 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::{CStr, CString},
ffi::{c_char, CStr, CString},
ptr::NonNull,
slice,
};
Expand Down Expand Up @@ -38,12 +38,16 @@ 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 unsafe fn name_raw<'a>(&self) -> &'a CStr {
unsafe {
let config = (*self.as_ptr()).ConfigData;
let name = (*config).Name.as_ptr();
CStr::from_ptr(name)
}
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")
}

pub fn name_owned(&self) -> String {
Expand Down Expand Up @@ -74,13 +78,9 @@ pub fn font_select(ui: &Ui, label: impl AsRef<str>, current: &mut Option<Font>)
const INHERIT: &str = "Inherit";

let mut changed = false;
let preview = match *current {
Some(font) => {
let name = unsafe { font.name_raw() };
name.to_string_lossy()
}
None => Cow::Borrowed(INHERIT),
};
let preview = current
.map(|current| unsafe { current.name_raw() }.to_string_lossy())
.unwrap_or(Cow::Borrowed(INHERIT));

if let Some(_token) = ui.begin_combo_with_flags(label, preview, ComboBoxFlags::HEIGHT_LARGE) {
if Selectable::new(INHERIT).build(ui) {
Expand All @@ -93,7 +93,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_raw().as_ptr(),
font.name_ptr().cast(),
is_selected,
SelectableFlags::empty().bits() as i32,
[0.0, 0.0].into(),
Expand Down

0 comments on commit 9120d44

Please sign in to comment.