#[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();
);

Tuple Fields§
§0: NonNull<_FontT>
Implementations§
Source§impl Font
impl Font
Sourcepub fn from_file(file_utf8: impl AsRef<Path>) -> Result<Font, StereoKitError>
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/"));
);
Sourcepub fn from_files<P: AsRef<Path>>(
files_utf8: &[P],
) -> Result<Font, StereoKitError>
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/"));
);
Sourcepub fn from_family(font_family: impl AsRef<str>) -> Result<Font, StereoKitError>
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/"));
);
Sourcepub fn find<S: AsRef<str>>(id: S) -> Result<Font, StereoKitError>
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())
Sourcepub fn clone_ref(&self) -> Font
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())
Sourcepub fn id<S: AsRef<str>>(&mut self, id: S) -> &mut Self
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())
Sourcepub fn get_id(&self) -> &str
pub fn get_id(&self) -> &str
The id of this font https://stereokit.net/Pages/StereoKit/Font/Id.html
see also font_get_id
see example Font::id
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.