lotus_script/
font.rs

1use lotus_script_sys::FfiObject;
2use lotus_shared::content::ContentId;
3pub use lotus_shared::font::*;
4
5/// A bitmap font that can be used to render text.
6pub struct BitmapFont(ContentId);
7
8impl BitmapFont {
9    /// Create a new bitmap font from a content id.
10    pub fn new(content_id: ContentId) -> Self {
11        Self(content_id)
12    }
13
14    /// Get the properties of this font.
15    /// Returns `None` if the font is not currently loaded. It will be loaded in the background.
16    /// Just call this function again later until it returns `Some`.
17    pub fn properties(&self) -> Option<BitmapFontProperties> {
18        let font = FfiObject::new(&self.0);
19        let properties = unsafe { lotus_script_sys::font::bitmap_font_properties(font.packed()) };
20
21        if properties == 0 {
22            None
23        } else {
24            Some(FfiObject::from_packed(properties).deserialize())
25        }
26    }
27
28    /// Get the width of the text in pixels.
29    /// Returns `None` if the font is not currently loaded. It will be loaded in the background.
30    /// Just call this function again later until it returns `Some`.
31    pub fn text_len(&self, text: &str, letter_spacing: i32) -> Option<u32> {
32        let font = FfiObject::new(&self.0);
33        let text = FfiObject::new(&text);
34
35        let len = unsafe {
36            lotus_script_sys::font::text_len(font.packed(), text.packed(), letter_spacing)
37        };
38
39        if len < 0 {
40            None
41        } else {
42            Some(len as u32)
43        }
44    }
45}