Struct cgl_rs::graphics::text::Font

source ·
pub struct Font { /* private fields */ }
Expand description

The public font object

Implementations§

source§

impl Font

source

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();
source

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 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
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();
source

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();
source

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();
source

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 Clone for Font

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Drop for Font

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Index<u8> for Font

source§

fn index(&self, index: u8) -> &Self::Output

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
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();
§

type Output = FontCharacter

The returned type after indexing.

Auto Trait Implementations§

§

impl RefUnwindSafe for Font

§

impl !Send for Font

§

impl !Sync for Font

§

impl Unpin for Font

§

impl UnwindSafe for Font

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.