use {
crate::{
SfResult,
cpp::FBox,
graphics::{Font, Glyph, Texture, font::Info},
},
std::{
cell::{Ref, RefCell},
io::{Read, Seek},
rc::Rc,
},
};
#[derive(Debug)]
pub struct RcFont {
font: Rc<RefCell<FBox<Font>>>,
}
impl RcFont {
#[must_use]
pub fn kerning(&self, first: u32, second: u32, character_size: u32) -> f32 {
self.font.borrow().kerning(first, second, character_size)
}
#[must_use]
pub fn bold_kerning(&self, first: u32, second: u32, character_size: u32) -> f32 {
self.font
.borrow()
.bold_kerning(first, second, character_size)
}
#[must_use]
pub fn line_spacing(&self, character_size: u32) -> f32 {
self.font.borrow().line_spacing(character_size)
}
#[must_use]
pub fn glyph(
&self,
codepoint: u32,
character_size: u32,
bold: bool,
outline_thickness: f32,
) -> Glyph {
self.font
.borrow()
.glyph(codepoint, character_size, bold, outline_thickness)
}
#[must_use]
pub fn info(&self) -> Info {
self.font.borrow().info()
}
#[must_use]
pub fn underline_position(&self, character_size: u32) -> f32 {
self.font.borrow().underline_position(character_size)
}
#[must_use]
pub fn underline_thickness(&self, character_size: u32) -> f32 {
self.font.borrow().underline_thickness(character_size)
}
pub fn from_file(filename: &str) -> SfResult<Self> {
Ok(RcFont {
font: Rc::new(RefCell::new(Font::from_file(filename)?)),
})
}
pub unsafe fn from_stream<T: Read + Seek>(stream: &mut T) -> SfResult<Self> {
Ok(RcFont {
font: Rc::new(RefCell::new(unsafe { Font::from_stream(stream) }?)),
})
}
pub unsafe fn from_memory(memory: &[u8]) -> SfResult<Self> {
Ok(RcFont {
font: Rc::new(RefCell::new(unsafe { Font::from_memory(memory) }?)),
})
}
pub fn from_memory_static(memory: &'static [u8]) -> SfResult<Self> {
unsafe { Self::from_memory(memory) }
}
#[must_use]
pub fn texture(&self, character_size: u32) -> Ref<'_, Texture> {
Ref::map(self.font.borrow(), move |r| r.texture(character_size))
}
#[must_use]
pub fn is_smooth(&self) -> bool {
self.font.borrow().is_smooth()
}
pub fn set_smooth(&mut self, smooth: bool) {
self.font.borrow_mut().set_smooth(smooth)
}
pub(super) fn downgrade(&self) -> std::rc::Weak<RefCell<FBox<Font>>> {
Rc::downgrade(&self.font)
}
}
impl ToOwned for RcFont {
type Owned = RcFont;
fn to_owned(&self) -> Self {
RcFont {
font: Rc::new(RefCell::new(self.font.borrow().to_owned())),
}
}
}