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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! The text module contains the text rendering, font loading functionality of CGL

#![allow(non_camel_case_types)]
use libc::{c_void, c_int, c_char, size_t, c_uchar};

use crate::graphics::texture::{Texture,  CGL_texture};
use crate::math::Vector2;

// The internal font handle
#[repr(C)]
pub(crate) struct CGL_font {
    _private: c_void
}

/// The Font Character struct contains all the information about a character in a font
#[repr(C)] #[derive(Copy, Clone)]
pub struct FontCharacter {
    pub size: Vector2,
    pub normalized_size: Vector2,
    pub offset: Vector2,
    pub normalized_offset: Vector2,
    pub bearing: Vector2,
    pub bearing_normalized: Vector2,
    pub(crate) bitmap: *mut c_uchar,
    pub ch: c_char
}


extern {
    fn CGL_text_init() -> c_int;
    fn CGL_text_shutdown() -> c_void;
    fn CGL_font_load(path: *const c_char) -> *mut CGL_font;
    fn CGL_font_load_from_memory(data: *const c_char, size: size_t) -> *mut CGL_font;
    fn CGL_font_destory(font: *mut CGL_font) -> c_void;
    fn CGL_font_get_atlas(font: *mut CGL_font) -> *mut CGL_texture;
    fn CGL_font_build_atlas(font: *mut CGL_font, width: size_t, height: size_t, font_size: size_t) -> c_int;
    fn CGL_font_get_characters(font: *mut CGL_font) -> *mut FontCharacter;
    fn CGL_text_bake_to_texture(string: *const c_char, string_length: size_t, font: *mut CGL_font, width: *mut size_t, height: *mut size_t) -> *mut CGL_texture;
}

/// Initialized the text module
/// 
/// Note: This function must be called before any other text functions
/// 
/// # Example
/// 
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("CGL Window", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// cgl_rs::graphics::text::init();
/// // Do stuff
/// cgl_rs::graphics::text::shutdown();
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn init() {
    unsafe {
        CGL_text_init();
    }
}

/// Shuts down the text module
/// 
/// Note: This function must be called after all other text functions have been called
/// 
/// # Example
/// 
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("CGL Window", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// cgl_rs::graphics::text::init();
/// // Do stuff
/// cgl_rs::graphics::text::shutdown();
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn shutdown() {
    unsafe {
        CGL_text_shutdown();
    }
}

/// The public font object
pub struct Font {
    pub(crate) handle: *mut CGL_font,
    pub(crate) has_been_destroyed: bool
}

impl std::ops::Index<u8> for Font {
    type Output = FontCharacter;

    /// Indexes the font's characters by their ASCII value.
    ///
    /// # Arguments
    ///
    /// * `index` - An `i8` representing the ASCII value of the character to retrieve.
    ///
    /// # Returns
    ///
    /// Returns a reference to the `FontCharacter` object corresponding to the given ASCII value.
    ///
    /// # Safety
    ///
    /// This function is marked as unsafe because it dereferences a raw pointer returned by the `CGL_font_get_characters` function.
    ///
    /// # Example
    /// 
    /// ```no_run
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("CGL Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// cgl_rs::graphics::text::init();
    /// let font = cgl_rs::graphics::text::Font::load("path/to/font.ttf").unwrap();
    /// let character = font[b'c'];
    /// cgl_rs::graphics::text::shutdown();
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    fn index(&self, index: u8) -> &Self::Output {
        unsafe {
            &*CGL_font_get_characters(self.handle).offset(index as isize)
        }
    }
}

impl Font {

    /// Loads a font from a file path
    ///
    /// # Arguments
    ///
    /// * `path` - A string slice that holds the path to the font file
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing a `Font` object if the font was loaded successfully, or an error message if it failed to load.
    ///
    /// # Example
    ///
    /// ```no_run
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("CGL Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// cgl_rs::graphics::text::init();
    /// {
    ///     let font = cgl_rs::graphics::text::Font::load("path/to/font.ttf").unwrap();
    /// }
    /// cgl_rs::graphics::text::shutdown();
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn load(path: &str) -> Result<Font, &'static str> {
        let c_path = std::ffi::CString::new(path).unwrap();
        let handle = unsafe { CGL_font_load(c_path.as_ptr()) };
        if handle.is_null() {
            Err("Failed to load font")
        } else {
            Ok(Font {
                handle,
                has_been_destroyed: false
            })
        }
    }

    /// Loads a font from memory
    ///
    /// # Arguments
    ///
    /// * `data` - A pointer to the font data in memory
    /// * `size` - The size of the font data in bytes
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing a `Font` object if the font was loaded successfully, or an error message if it failed to load.
    ///
    /// # Example
    ///
    /// ```no_run
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("CGL Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// cgl_rs::graphics::text::init();
    /// {
    ///     // let font_data = include_bytes!("path/to/font.ttf");
    ///     // let font = cgl_rs::graphics::text::Font::load_from_memory(font_data.as_ptr(), font_data.len()).unwrap();
    /// }
    /// cgl_rs::graphics::text::shutdown();
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn load_from_memory(data: *const u8, size: usize) -> Result<Font, &'static str> {
        let handle = unsafe { CGL_font_load_from_memory(data as *const c_char, size) };
        if handle.is_null() {
            Err("Failed to load font")
        } else {
            Ok(Font {
                handle,
                has_been_destroyed: false
            })
        }
    }

    /// Destroys the font object and frees any resources associated with it.
    ///
    /// # Example
    ///
    /// ```no_run
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("CGL Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// cgl_rs::graphics::text::init();
    /// {
    ///     let mut font = cgl_rs::graphics::text::Font::load("path/to/font.ttf").unwrap();
    ///     // Use the font...
    ///     font.destroy(); // Or, just let the font go out of scope and it will be destroyed automatically.
    /// }
    /// cgl_rs::graphics::text::shutdown();
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn destroy(&mut self) {
        if !self.has_been_destroyed {
            unsafe {
                CGL_font_destory(self.handle);
            }
            self.has_been_destroyed = true;
        }
    }

    /// Returns the texture atlas for the font. This function must be called after `build_atlas` has been called.
    /// 
    /// Note: This atlas texture is managed by the font object and will be destroyed when the font object is destroyed.
    ///
    /// # Returns
    ///
    /// Returns a `Texture` object representing the texture atlas for the font.
    ///
    /// # Example
    ///
    /// ```no_run
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("CGL Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// cgl_rs::graphics::text::init();
    /// {
    ///     let font = cgl_rs::graphics::text::Font::load("path/to/font.ttf").unwrap();
    ///     font.build_atlas(512, 512, 32).unwrap();
    ///     let texture = font.get_atlas();
    ///     // Use the texture...
    /// }
    /// cgl_rs::graphics::text::shutdown();
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_atlas(&self) -> Texture {
        let handle = unsafe { CGL_font_get_atlas(self.handle) };
        Texture {
            handle,
            has_been_destroyed: true
        }
    }

    /// Builds the texture atlas for the font. This function must be called before any text can be rendered with the font.
    ///
    /// # Arguments
    ///
    /// * `width` - The width of the texture atlas in pixels.
    /// * `height` - The height of the texture atlas in pixels.
    /// * `font_size` - The size of the font in pixels.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the texture atlas was built successfully, otherwise returns an error message.
    ///
    /// # Example
    ///
    /// ```no_run
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("CGL Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// cgl_rs::graphics::text::init();
    /// {
    ///     let font = cgl_rs::graphics::text::Font::load("path/to/font.ttf").unwrap();
    ///     font.build_atlas(512, 512, 32).unwrap();
    ///     // Use the font...
    /// }
    /// cgl_rs::graphics::text::shutdown();
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn build_atlas(&self, width: usize, height: usize, font_size: usize) -> Result<(), &'static str> {
        let result = unsafe { CGL_font_build_atlas(self.handle, width, height, font_size) };
        if result != 0 {
            Ok(())
        } else {
            Err("Failed to build font atlas")
        }
    }
}

impl Drop for Font {
    fn drop(&mut self) {
        self.destroy();
    }
}

impl Clone for Font {
    fn clone(&self) -> Self {
        Font {
            handle: self.handle.clone(),
            has_been_destroyed: true
        }
    }
}

/// Bakes the given text into a texture using the specified font and font size.
/// 
/// Note: This texture is not managed by the font object and the ownership of the texture is passed to the caller.
///
/// # Arguments
///
/// * `font` - The font to use for rendering the text.
/// * `text` - The text to render.
///
/// # Returns
///
/// Returns a tuple containing the resulting `Texture` object, as well as the width and height of the texture in pixels.
///
/// # Example
///
/// ```no_run
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("CGL Window", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// cgl_rs::graphics::text::init();
/// {
///     let font = cgl_rs::graphics::text::Font::load("path/to/font.ttf").unwrap();
///     let (texture, width, height) = cgl_rs::graphics::text::bake_to_texture(&font, "Hello, world!").unwrap();
///     // Use the texture...
/// }
/// cgl_rs::graphics::text::shutdown();
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn bake_to_texture(font: &Font, text: &str) -> Result<(Texture, u32, u32), &'static str> {
    let mut width_sz: size_t = 0;
    let mut height_sz: size_t = 0;
    let width_ptr = &mut width_sz as *mut size_t;
    let height_ptr = &mut height_sz as *mut size_t;
    let string_in_c = std::ffi::CString::new(text).unwrap();
    let handle = unsafe { CGL_text_bake_to_texture(string_in_c.as_ptr(), text.len(), font.handle, width_ptr, height_ptr) };
    if handle.is_null() {
        Err("Failed to bake text to texture")
    } else {
        Ok((Texture {
            handle,
            has_been_destroyed: false
        }, width_sz as u32, height_sz as u32))
    }
}