use stb_sys as sys;
use std::ffi::CStr;
use std::mem::size_of;
#[derive(Debug, Default, Copy, Clone)]
#[repr(C)]
pub struct Vertex {
pub xyz: [f32; 3],
pub col: [u8; 4],
}
pub fn stb_easy_font_spacing(spacing: f32) {
unsafe { sys::stb_easy_font_spacing_(spacing) };
}
pub fn stb_easy_font_width(text: &CStr) -> i32 {
unsafe { sys::stb_easy_font_width_(text.as_ptr() as *mut i8) }
}
pub fn stb_easy_font_height(text: &CStr) -> i32 {
unsafe { sys::stb_easy_font_height_(text.as_ptr() as *mut i8) }
}
pub fn stb_easy_font_print(
x: f32,
y: f32,
text: &CStr,
color: Option<[u8; 4]>,
buffer: &mut [Vertex],
) -> usize {
let buffer_size = size_of::<Vertex>() * buffer.len();
let color = if let Some(color) = color {
color.as_ptr() as *mut u8
} else {
std::ptr::null_mut()
};
let quad_count = unsafe {
sys::stb_easy_font_print_(
x,
y,
text.as_ptr() as *mut i8,
color,
buffer.as_mut_ptr() as *mut std::ffi::c_void,
buffer_size as i32,
)
};
quad_count as _
}
#[cfg(test)]
mod tests {
use std::ffi::CString;
#[test]
fn stb_easy_font_width() {
let abc = CString::new("abc").unwrap();
assert_eq!(18, super::stb_easy_font_width(&abc));
}
#[test]
fn stb_easy_font_height() {
let abc = CString::new("ab\nc").unwrap();
assert_eq!(24, super::stb_easy_font_height(&abc));
}
#[test]
fn stb_easy_font_print() {
let mut vertices = [super::Vertex::default(); 128];
let a = CString::new("a").unwrap();
let quad_count = super::stb_easy_font_print(1.0, 1.0, &a, None, &mut vertices);
let vertex_count = quad_count * 4;
assert_eq!(vertex_count, 16);
for v in &vertices[..vertex_count] {
assert_eq!(v.col, [255, 255, 255, 255]);
}
}
#[test]
fn stb_easy_font_print_empty() {
let mut buffer = [super::Vertex::default(); 0];
let a = CString::new("a").unwrap();
let quad_count = super::stb_easy_font_print(1.0, 1.0, &a, None, &mut buffer);
assert_eq!(0, quad_count);
}
}