Struct Font

Source
#[repr(C)]
pub struct Font(pub NonNull<_FontT>);
Expand description

This class represents a text font asset! On the back-end, this asset is composed of a texture with font characters rendered to it, and a list of data about where, and how large those characters are on the texture.

This asset is used anywhere that text shows up, like in the UI or Text classes! https://stereokit.net/Pages/StereoKit/Font.html

§Examples

use stereokit_rust::{ui::Ui, maths::{Vec3, Quat, Pose, Matrix},
                     font::Font, system::{Assets, Text}, util::named_colors};

// Load font assets
let emoji_font = if cfg!(windows) {
    // TODO: Doesn't work on Windows Github Actions.
    // return;
    Font::from_file("C:\\Windows\\Fonts\\seguiemj.ttf").unwrap_or_default()
} else {
    Font::from_file("fonts/Noto_Emoji/NotoEmoji-VariableFont_wght.ttf").unwrap_or_default()
};
let text_font = if cfg!(windows) {
    Font::from_file("C:\\Windows\\Fonts\\Arial.ttf").unwrap_or_default()
} else {
    Font::from_file("fonts/Inter/Inter-VariableFont_opsz_wght.ttf").unwrap_or_default()
};
Assets::block_for_priority(i32::MAX);
let emoji_style = Some(Text::make_style(emoji_font, 0.35, named_colors::RED));
let text_style = Text::make_style(text_font, 0.025, named_colors::GREEN);
let mut window_pose = Pose::new(
    [0.0, 0.0, 0.90], Some([0.0, 160.0, 0.0].into()));

filename_scr = "screenshots/font.jpeg";
test_screenshot!( // !!!! Get a proper main loop !!!!
    Text::add_at(token, "😋 Emojis🤪\n\n  🧐", Matrix::IDENTITY, emoji_style,
                 None, None, None, None, None, None);

    Ui::window_begin("Default Font", &mut window_pose, None, None, None);
    Ui::push_text_style(text_style);
    Ui::text("text font", None, None, None, Some(0.14), None, None);
    Ui::pop_text_style();
    Ui::window_end();
);
screenshot

Tuple Fields§

§0: NonNull<_FontT>

Implementations§

Source§

impl Font

Source

pub fn from_file(file_utf8: impl AsRef<Path>) -> Result<Font, StereoKitError>

Loads a font and creates a font asset from it. If a glyph is not found you can call unwrap_or_default() to get the default font. https://stereokit.net/Pages/StereoKit/Font/FromFile.html

  • file_utf8 - The path to the font file to load.

see also font_create

§Examples
use stereokit_rust::{maths:: Matrix, font::Font, system::Text, util::named_colors};

let text_font = if cfg!(windows) {
    Font::from_file("C:\\Windows\\Fonts\\Arial.ttf").unwrap_or_default()
} else {
    Font::from_file("fonts/Inter/Inter-VariableFont_opsz_wght.ttf").unwrap_or_default()
};
let text_style = Some(Text::make_style(&text_font, 0.025, named_colors::GREEN));

test_steps!( // !!!! Get a proper main loop !!!!
    Text::add_at(token, "My Green Text", Matrix::IDENTITY, text_style,
                 None, None, None, None, None, None);
    assert_ne!(text_font.get_id(), "default/font");
    assert   !(text_font.get_id().starts_with("sk/font/"));
);
Source

pub fn from_files<P: AsRef<Path>>( files_utf8: &[P], ) -> Result<Font, StereoKitError>

Loads a font and creates a font asset from it. If a glyph is not found, StereoKit will look in the next font file in the list. If None has been found you can call unwrap_or_default() to get the default font. https://stereokit.net/Pages/StereoKit/Font/FromFile.html

  • files_utf8 - The paths to the font files.

see also font_create_files

§Examples
use stereokit_rust::{maths:: Matrix, font::Font, system::Text, util::named_colors};

let font_files = if cfg!(windows) {
    ["C:\\Windows\\Fonts\\Arial.ttf",
     "C:\\Windows\\Fonts\\Calibri.ttf"]
} else {
    ["/usr/share/fonts/truetype/freefont/FreeSans.ttf",
     "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"]
};

let text_font = Font::from_files(&font_files).unwrap_or_default();
let text_style = Some(Text::make_style(&text_font, 0.025, named_colors::GREEN));

test_steps!( // !!!! Get a proper main loop !!!!
    Text::add_at(token, "My Green Text", Matrix::IDENTITY, text_style,
                 None, None, None, None, None, None);
    assert_ne!(text_font.get_id(), "default/font");
    assert!   (text_font.get_id().starts_with("sk/font/"));
);
Source

pub fn from_family(font_family: impl AsRef<str>) -> Result<Font, StereoKitError>

Doesn’t work on Linux Loads font from a specified list of font family names. Returns a font from the given font family names, Most of the OS provide fallback fonts, hence there will always be a set of fonts. https://stereokit.net/Pages/StereoKit/Font/FromFamily.html

  • font_family - List of font family names separated by comma(,) similar to a list of names css allows.

see also font_create_family

§Examples
use stereokit_rust::{maths:: Matrix, font::Font, system::Text, util::named_colors};

let font_family = if cfg!(windows) {
    "Arial, Helvetica, Verdana, Geneva, Tahoma, sans-serif;"
} else {
    // TODO: Doesn't work on Linux
    return;
    "FreeSans, Liberation Sans, Nimbus Sans L, DejaVu Sans, Bitstream Vera Sans, sans-serif;"
};

let text_font = Font::from_family(&font_family).unwrap_or_default();
let text_style = Some(Text::make_style(&text_font, 0.025, named_colors::GREEN));

test_steps!( // !!!! Get a proper main loop !!!!
    Text::add_at(token, "My Green Text", Matrix::IDENTITY, text_style,
                 None, None, None, None, None, None);
    assert_ne!(text_font.get_id(), "default/font");
    assert!   (text_font.get_id().starts_with("sk/font/"));
);
Source

pub fn find<S: AsRef<str>>(id: S) -> Result<Font, StereoKitError>

Searches the asset list for a font with the given Id. https://stereokit.net/Pages/StereoKit/Font/Find.html

  • id - The Id of the font to find.

see also font_find

§Examples
use stereokit_rust::{maths:: Matrix, font::Font, system::Text, util::named_colors};

let font_files = if cfg!(windows) {
    ["C:\\Windows\\Fonts\\Arial.ttf",
     "C:\\Windows\\Fonts\\Calibri.ttf"]
} else {
    ["/usr/share/fonts/truetype/freefont/FreeSans.ttf",
     "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"]
};

let text_font = Font::from_files(&font_files).unwrap_or_default();
assert_ne!(text_font.get_id(), "default/font");

let id = text_font.get_id();
let same_font = Font::find(id).unwrap_or_default();

assert_eq!(text_font.get_id(), same_font.get_id())
Source

pub fn clone_ref(&self) -> Font

Creates a clone of the same reference. Basically, the new variable is the same asset. This is what you get by calling find() method. https://stereokit.net/Pages/StereoKit/Font/Find.html

see also font_find

§Examples
use stereokit_rust::{maths:: Matrix, font::Font, system::Text, util::named_colors};

let font_files = if cfg!(windows) {
    ["C:\\Windows\\Fonts\\Arial.ttf",
     "C:\\Windows\\Fonts\\Calibri.ttf"]
} else {
    ["/usr/share/fonts/truetype/freefont/FreeSans.ttf",
     "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"]
};

let text_font = Font::from_files(&font_files).unwrap_or_default();
assert_ne!(text_font.get_id(), "default/font");

let same_font = text_font.clone_ref();

assert_eq!(text_font.get_id(), same_font.get_id())
Source

pub fn id<S: AsRef<str>>(&mut self, id: S) -> &mut Self

Gets or sets the unique identifier of this asset resource! This can be helpful for debugging, managing your assets, or finding them later on! https://stereokit.net/Pages/StereoKit/Font/Id.html

  • id - Must be a unique identifier of this asset resource!

see also font_set_id

§Examples
use stereokit_rust::{maths:: Matrix, font::Font, system::Text, util::named_colors};

let font_files = if cfg!(windows) {
    ["C:\\Windows\\Fonts\\Arial.ttf",
     "C:\\Windows\\Fonts\\Calibri.ttf"]
} else {
    ["/usr/share/fonts/truetype/freefont/FreeSans.ttf",
     "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"]
};

let mut text_font = Font::from_files(&font_files).unwrap_or_default();
assert_ne!(text_font.get_id(), "default/font");
text_font.id("my_font");

let same_font = Font::find("my_font").unwrap_or_default();

assert_eq!(text_font.get_id(), same_font.get_id())
Source

pub fn get_id(&self) -> &str

Trait Implementations§

Source§

impl AsRef<Font> for Font

Source§

fn as_ref(&self) -> &Font

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for Font

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Font

Source§

fn default() -> Self

The default font used by StereoKit’s text. This varies from platform to platform, but is typically a sans-serif general purpose font, such as Segoe UI. https://stereokit.net/Pages/StereoKit/Font/Default.html

see also font_find

Source§

impl Drop for Font

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl IAsset for Font

Source§

fn get_id(&self) -> &str

gets the unique identifier of this asset resource! This can be helpful for debugging, managing your assets, or finding them later on! https://stereokit.net/Pages/StereoKit/IAsset/Id.html

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more