lotus_script/font.rs
1//! Bitmap-Schriftarten zum Rendern von Text auf Script-Texturen.
2//!
3//! Bitmap fonts for rendering text on script textures.
4
5use lotus_script_sys::FfiObject;
6use lotus_shared::content::ContentId;
7pub use lotus_shared::font::*;
8
9/// Bitmap-Schrift, die zum Rendern von Text verwendet werden kann.
10///
11/// A bitmap font that can be used to render text.
12pub struct BitmapFont {
13 content_id: ContentId,
14 properties: BitmapFontProperties,
15}
16
17impl BitmapFont {
18 /// Versucht, eine Bitmap-Schrift anhand einer Content-ID zu laden.
19 /// Gibt `None` zurück, solange das Asset noch im Hintergrund lädt — erneut aufrufen, bis `Some` zurückkommt.
20 ///
21 /// Tries to load a bitmap font from a content id.
22 /// Returns `None` while the asset is still loading in the background; call again until `Some` is returned.
23 pub fn try_load(content_id: ContentId) -> Option<Self> {
24 let font = FfiObject::new(&content_id);
25 let properties = unsafe { lotus_script_sys::font::bitmap_font_properties(font.packed()) };
26
27 if properties == 0 {
28 None
29 } else {
30 let properties = FfiObject::from_packed(properties).deserialize();
31 Some(Self {
32 content_id,
33 properties,
34 })
35 }
36 }
37
38 /// Gibt die Eigenschaften dieser Schrift zurück.
39 ///
40 /// Returns the properties of this font.
41 pub fn properties(&self) -> &BitmapFontProperties {
42 &self.properties
43 }
44
45 /// Gibt die Textbreite in Pixeln zurück.
46 ///
47 /// Returns the width of the text in pixels.
48 pub fn text_len(&self, text: &str, letter_spacing: i32) -> u32 {
49 let font = FfiObject::new(&self.content_id);
50 let text = FfiObject::new(&text);
51
52 let len = unsafe {
53 lotus_script_sys::font::text_len(font.packed(), text.packed(), letter_spacing)
54 };
55
56 assert!(len >= 0);
57
58 len as u32
59 }
60}