use get_error;
use rwops::RWops;
use std::error;
use std::fmt;
use std::io;
use std::os::raw::{c_int, c_long};
use std::path::Path;
use sys::ttf;
use version::Version;
use super::font::{
internal_load_font, internal_load_font_at_index, internal_load_font_from_ll, Font,
};
#[must_use]
pub struct Sdl2TtfContext;
impl Drop for Sdl2TtfContext {
fn drop(&mut self) {
unsafe {
ttf::TTF_Quit();
}
}
}
impl Sdl2TtfContext {
pub fn load_font<'ttf, P: AsRef<Path>>(
&'ttf self,
path: P,
point_size: u16,
) -> Result<Font<'ttf, 'static>, String> {
internal_load_font(path, point_size)
}
pub fn load_font_at_index<'ttf, P: AsRef<Path>>(
&'ttf self,
path: P,
index: u32,
point_size: u16,
) -> Result<Font<'ttf, 'static>, String> {
internal_load_font_at_index(path, index, point_size)
}
pub fn load_font_from_rwops<'ttf, 'r>(
&'ttf self,
rwops: RWops<'r>,
point_size: u16,
) -> Result<Font<'ttf, 'r>, String> {
let raw = unsafe { ttf::TTF_OpenFontRW(rwops.raw(), 0, point_size as c_int) };
if (raw as *mut ()).is_null() {
Err(get_error())
} else {
Ok(internal_load_font_from_ll(raw, Some(rwops)))
}
}
pub fn load_font_at_index_from_rwops<'ttf, 'r>(
&'ttf self,
rwops: RWops<'r>,
index: u32,
point_size: u16,
) -> Result<Font<'ttf, 'r>, String> {
let raw = unsafe {
ttf::TTF_OpenFontIndexRW(rwops.raw(), 0, point_size as c_int, index as c_long)
};
if (raw as *mut ()).is_null() {
Err(get_error())
} else {
Ok(internal_load_font_from_ll(raw, Some(rwops)))
}
}
}
pub fn get_linked_version() -> Version {
unsafe { Version::from_ll(*ttf::TTF_Linked_Version()) }
}
#[derive(Debug)]
pub enum InitError {
InitializationError(io::Error),
AlreadyInitializedError,
}
impl error::Error for InitError {
fn description(&self) -> &str {
match *self {
InitError::AlreadyInitializedError => "SDL2_TTF has already been initialized",
InitError::InitializationError(ref error) => error.description(),
}
}
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
InitError::AlreadyInitializedError => None,
InitError::InitializationError(ref error) => Some(error),
}
}
}
impl fmt::Display for InitError {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
formatter.write_str("SDL2_TTF has already been initialized")
}
}
pub fn init() -> Result<Sdl2TtfContext, InitError> {
unsafe {
if ttf::TTF_WasInit() == 1 {
Err(InitError::AlreadyInitializedError)
} else if ttf::TTF_Init() == 0 {
Ok(Sdl2TtfContext)
} else {
Err(InitError::InitializationError(io::Error::last_os_error()))
}
}
}
pub fn has_been_initialized() -> bool {
unsafe { ttf::TTF_WasInit() == 1 }
}