use crate::canvas::Color;
use fontconfig::Fontconfig;
use sfml::graphics::Image as Img;
use std::path::Path;
#[derive(Debug, Clone)]
pub struct FontHandler {
font_path: String,
}
impl FontHandler {
pub fn custom(font_path: String) -> Option<Self> {
let path = Path::new(&font_path);
if path.is_dir() || path.exists() {
path.to_str()?;
return Some(Self {
font_path: path.to_str().unwrap().to_string(),
});
} else {
return None;
}
}
#[cfg(target_os = "linux")]
pub fn find_fonts(f: String) -> Option<Self> {
let fc = Fontconfig::new()?;
let found = fc.find(&f, None)?;
Some(Self {
font_path: String::from(found.path.to_str()?),
})
}
#[cfg(target_os = "windows")]
pub fn find_fonts(font: String) -> Option<Self> {
let path = Path::new(
"C:\\Windows\\Fonts"
.to_owned()
.push_str(font.as_str())
.as_str(),
);
if path.exits() && path.is_dir() == false {
return Some(Self { font_path: path });
} else {
return None;
}
}
pub fn get(&self) -> String {
return self.font_path.clone();
}
}
#[derive(Clone)]
pub struct Image {
pub(crate) image: Img,
position: (f32, f32),
rotation: f32,
}
impl Image {
pub fn from_file(path: &str, position: (f32, f32)) -> Result<Self, Error> {
let img = Img::from_file(path).ok_or(Error::GeneralError(String::from(
"Unable to open file".to_string(),
)))?;
Ok(Self {
image: img,
position,
rotation: 0f32,
})
}
pub fn from_image(img: &Image) -> Self {
Self {
image: img.image.clone(),
position: img.position.clone(),
rotation: img.rotation.clone(),
}
}
}
impl Image {
pub fn save(&self, path: &str) -> bool {
self.image.save_to_file(path)
}
pub fn get_size(&self) -> (u32, u32) {
let res = &self.image.size();
(res.x, res.y)
}
pub fn get_pixel(&self, x: u32, y: u32) -> Color {
let res = &self.image.pixel_at(x, y);
Color::from_sfml_color(res.clone())
}
pub fn put_pixel(&mut self, x: u32, y: u32, color: Color) {
self.image.set_pixel(x, y, color.c);
}
pub fn pixels_memory(&self) -> &[u8] {
self.image.pixel_data()
}
pub fn set_position(&mut self, pos: (f32, f32)) {
self.position = pos;
}
pub fn get_position(&self) -> (f32, f32) {
let pos = self.position;
pos
}
pub fn set_rotation(&mut self, angle: f32) {
self.rotation = angle;
}
}
#[derive(Debug)]
pub enum Error {
IoError(std::io::Error),
FfiNullError(std::ffi::NulError),
IoErrorKind(std::io::ErrorKind),
GeneralError(String),
NoneError(std::option::NoneError),
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error::IoError(error)
}
}
impl From<std::ffi::NulError> for Error {
fn from(error: std::ffi::NulError) -> Self {
Error::FfiNullError(error)
}
}
impl From<std::io::ErrorKind> for Error {
fn from(error: std::io::ErrorKind) -> Self {
Self::IoErrorKind(error)
}
}
impl From<std::option::NoneError> for Error {
fn from(error: std::option::NoneError) -> Self {
Self::NoneError(error)
}
}
pub enum Wrapper<T> {
I32(i32),
U32(u32),
WrapString(String),
F32(f32),
Custom(T),
}