Font

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

type Output = FontCharacter

The returned type after indexing.

Auto Trait Implementations§

§

impl Freeze for Font

§

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.