Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binding for LoadFontFromMemory #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions raylib/src/core/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,46 @@ impl RaylibHandle {
Ok(Font(f))
}

/// Load font data from a given memory buffer.
/// `file_type` refers to the extension, e.g. ".ttf".
#[inline]
pub fn load_font_from_memory(
&mut self,
_: &RaylibThread,
file_type: &str,
file_data: &[u8],
font_size: i32,
chars: FontLoadEx,
) -> Result<Font, String> {
let c_file_type = CString::new(file_type).unwrap();
let f = unsafe {
match chars {
FontLoadEx::Chars(c) => ffi::LoadFontFromMemory(
c_file_type.as_ptr(),
file_data.as_ptr(),
file_data.len() as i32,
font_size,
c.as_ptr() as *mut i32,
c.len() as i32,
),
FontLoadEx::Default(count) => ffi::LoadFontFromMemory(
c_file_type.as_ptr(),
file_data.as_ptr(),
file_data.len() as i32,
font_size,
std::ptr::null_mut(),
count,
),
}
};
if f.chars.is_null() || f.texture.id == 0 {
return Err(format!(
"Error loading font from memory. Is it the right type?"
));
}
Ok(Font(f))
}

/// Loads font data for further use (see also `Font::from_data`).
/// Now supports .tiff
#[inline]
Expand Down