1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use crate::{
    color::Color,
    ffi,
    math::{Rectangle, Vector2},
    texture::Image,
};
use std::ffi::CString;

pub use crate::ffi::FontType;

/// Font, font texture and GlyphInfo array data
#[derive(Debug)]
#[repr(transparent)]
pub struct Font {
    pub(crate) raw: ffi::Font,
}

impl Font {
    /// Base size (default chars height)
    #[inline]
    pub fn base_size(&self) -> u32 {
        self.raw.baseSize as _
    }

    /// Number of glyph characters
    #[inline]
    pub fn glyph_count(&self) -> usize {
        self.raw.glyphCount as _
    }

    /// Padding around the glyph characters
    #[inline]
    pub fn glyph_padding(&self) -> u32 {
        self.raw.glyphPadding as _
    }

    /// Load font from file into GPU memory (VRAM)
    #[inline]
    pub fn from_file(file_name: &str) -> Option<Self> {
        let file_name = CString::new(file_name).unwrap();

        let raw = unsafe { ffi::LoadFont(file_name.as_ptr()) };

        if unsafe { ffi::IsFontReady(raw.clone()) } {
            Some(Self { raw })
        } else {
            None
        }
    }

    /// Load font from file with extended parameters
    #[inline]
    pub fn from_file_ex(file_name: &str, font_size: u32, chars: &[char]) -> Option<Self> {
        let file_name = CString::new(file_name).unwrap();

        let raw = unsafe {
            ffi::LoadFontEx(
                file_name.as_ptr(),
                font_size as _,
                chars.as_ptr() as *mut _,
                chars.len() as _,
            )
        };

        if unsafe { ffi::IsFontReady(raw.clone()) } {
            Some(Self { raw })
        } else {
            None
        }
    }

    /// Load font from Image (XNA style)
    #[inline]
    pub fn from_image(image: &Image, key_color: Color, first_char: char) -> Option<Self> {
        let raw =
            unsafe { ffi::LoadFontFromImage(image.raw.clone(), key_color.into(), first_char as _) };

        if unsafe { ffi::IsFontReady(raw.clone()) } {
            Some(Self { raw })
        } else {
            None
        }
    }

    /// Load font from memory buffer, fileType refers to extension: i.e. '.ttf'
    #[inline]
    pub fn from_memory(
        file_type: &str,
        file_data: &[u8],
        font_size: u32,
        chars: &[char],
    ) -> Option<Self> {
        let file_type = CString::new(file_type).unwrap();

        let raw = unsafe {
            ffi::LoadFontFromMemory(
                file_type.as_ptr(),
                file_data.as_ptr(),
                file_data.len() as _,
                font_size as _,
                chars.as_ptr() as *mut _,
                chars.len() as _,
            )
        };

        if unsafe { ffi::IsFontReady(raw.clone()) } {
            Some(Self { raw })
        } else {
            None
        }
    }

    /// Export font as code file, returns true on success
    #[inline]
    pub fn export_as_code(&self, file_name: &str) -> bool {
        let file_name = CString::new(file_name).unwrap();

        unsafe { ffi::ExportFontAsCode(self.raw.clone(), file_name.as_ptr()) }
    }

    /// Measure string width for default font
    #[inline]
    pub fn measure_text(text: &str, font_size: u32) -> u32 {
        let text = CString::new(text).unwrap();

        unsafe { ffi::MeasureText(text.as_ptr(), font_size as _) as _ }
    }

    /// Measure string size for Font
    #[inline]
    pub fn measure_text_ex(&self, text: &str, font_size: f32, spacing: f32) -> Vector2 {
        let text = CString::new(text).unwrap();

        unsafe {
            ffi::MeasureTextEx(self.raw.clone(), text.as_ptr(), font_size, spacing).into()
        }
    }

    /// Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found
    #[inline]
    pub fn get_glyph_index(&self, codepoint: char) -> usize {
        unsafe { ffi::GetGlyphIndex(self.raw.clone(), codepoint as _) as _ }
    }

    /// Get glyph rectangle in font atlas for a codepoint (unicode character), fallback to '?' if not found
    #[inline]
    pub fn get_glyph_atlas_rect(&self, codepoint: char) -> Rectangle {
        unsafe { ffi::GetGlyphAtlasRec(self.raw.clone(), codepoint as _).into() }
    }

    /// Get glyph font info data for a codepoint (unicode character), fallback to '?' if not found
    #[inline]
    pub fn get_glyph_info(&self, codepoint: char) -> GlyphInfo {
        let info = unsafe { ffi::GetGlyphInfo(self.raw.clone(), codepoint as _) };

        GlyphInfo {
            value: char::from_u32(info.value as _).unwrap(),
            offset_x: info.offsetX,
            offset_y: info.offsetY,
            advance_x: info.advanceX,
            image: Image {
                raw: unsafe { ffi::ImageCopy(info.image) },
            },
        }
    }

    /// Get the 'raw' ffi type
    /// Take caution when cloning so it doesn't outlive the original
    #[inline]
    pub fn as_raw(&self) -> &ffi::Font {
        &self.raw
    }

    /// Get the 'raw' ffi type
    /// Take caution when cloning so it doesn't outlive the original
    #[inline]
    pub fn as_raw_mut(&mut self) -> &mut ffi::Font {
        &mut self.raw
    }

    /// Convert a 'raw' ffi object to a safe wrapper
    ///
    /// # Safety
    /// * The raw object must be correctly initialized
    /// * The raw object should be unique. Otherwise, make sure its clones don't outlive the newly created object.
    #[inline]
    pub unsafe fn from_raw(raw: ffi::Font) -> Self {
        Self { raw }
    }
}

impl Default for Font {
    /// Get the default Font
    #[inline]
    fn default() -> Self {
        Self {
            raw: unsafe { ffi::GetFontDefault() },
        }
    }
}

impl Drop for Font {
    #[inline]
    fn drop(&mut self) {
        unsafe { ffi::UnloadFont(self.raw.clone()) }
    }
}

/// Generate image font atlas using chars info
#[inline]
pub fn gen_image_font_atlas(
    chars: Vec<GlyphInfo>,
    font_size: u32,
    padding: i32,
    skyline_pack: bool,
) -> Option<(Image, Vec<Rectangle>)> {
    assert!(!chars.is_empty());

    let mut recs: *mut ffi::Rectangle = std::ptr::null_mut();
    let chars_ffi: Vec<_> = chars
        .iter()
        .map(|gi| ffi::GlyphInfo {
            value: gi.value as _,
            offsetX: gi.offset_x as _,
            offsetY: gi.offset_y as _,
            advanceX: gi.advance_x as _,
            image: gi.image.raw.clone(),
        })
        .collect();

    let image = unsafe {
        ffi::GenImageFontAtlas(
            chars_ffi.as_ptr(),
            (&mut recs) as *mut _,
            chars.len() as _,
            font_size as _,
            padding,
            if skyline_pack { 1 } else { 0 },
        )
    };

    if !unsafe { ffi::IsImageReady(image.clone()) } {
        return None;
    }

    let mut vec = Vec::new();

    for i in 0..chars.len() {
        vec.push(unsafe { recs.add(i).read().into() });
    }

    unsafe {
        ffi::MemFree(recs as *mut _);
    }

    Some((Image { raw: image }, vec))
}

/// GlyphInfo, font characters glyphs info
#[repr(C)]
#[derive(Clone, Debug)]
pub struct GlyphInfo {
    /// Character value (Unicode)
    pub value: char,
    /// Character offset X when drawing
    pub offset_x: i32,
    /// Character offset Y when drawing
    pub offset_y: i32,
    /// Character advance position X
    pub advance_x: i32,
    /// Character image data
    pub image: Image,
}

impl GlyphInfo {
    /// Load font data for further use
    #[inline]
    pub fn from_file_data(
        file_data: &[u8],
        font_size: u32,
        font_chars: &[char],
        font_type: FontType,
    ) -> Vec<GlyphInfo> {
        assert!(!font_chars.is_empty());
        let len = font_chars.len();

        let infos = unsafe {
            ffi::LoadFontData(
                file_data.as_ptr(),
                file_data.len() as _,
                font_size as _,
                font_chars.as_ptr() as *mut _,
                len as _,
                font_type as _,
            )
        };

        let mut vec = Vec::new();

        for i in 0..len {
            let gi = unsafe { infos.add(i).read() };

            vec.push(GlyphInfo {
                value: char::from_u32(gi.value as _).unwrap(),
                offset_x: gi.offsetX,
                offset_y: gi.offsetY,
                advance_x: gi.advanceX,
                image: Image {
                    raw: unsafe { ffi::ImageCopy(gi.image) },
                },
            });
        }

        unsafe {
            ffi::UnloadFontData(infos, len as _);
        }

        vec
    }
}