pub struct Font { /* private fields */ }
Expand description
The public font object
Implementations§
source§impl Font
impl Font
sourcepub fn load(path: &str) -> Result<Font, &'static str>
pub fn load(path: &str) -> Result<Font, &'static str>
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
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();
sourcepub fn load_from_memory(
data: *const u8,
size: usize
) -> Result<Font, &'static str>
pub fn load_from_memory( data: *const u8, size: usize ) -> Result<Font, &'static str>
Loads a font from memory
Arguments
data
- A pointer to the font data in memorysize
- 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
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();
sourcepub fn destroy(&mut self)
pub fn destroy(&mut self)
Destroys the font object and frees any resources associated with it.
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();
{
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();
sourcepub fn get_atlas(&self) -> Texture
pub fn get_atlas(&self) -> Texture
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
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();
sourcepub fn build_atlas(
&self,
width: usize,
height: usize,
font_size: usize
) -> Result<(), &'static str>
pub fn build_atlas( &self, width: usize, height: usize, font_size: usize ) -> Result<(), &'static str>
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
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();
Trait Implementations§
source§impl Index<u8> for Font
impl Index<u8> for Font
source§fn index(&self, index: u8) -> &Self::Output
fn index(&self, index: u8) -> &Self::Output
Indexes the font’s characters by their ASCII value.
Arguments
index
- Ani8
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
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();