Struct TextStyle

Source
#[repr(C)]
pub struct TextStyle { /* private fields */ }
Expand description

A text style is a font plus size/color/material parameters, and are used to keep text looking more consistent through the application by encouraging devs to re-use styles throughout the project. See Text.MakeStyle for making a TextStyle object. https://stereokit.net/Pages/StereoKit/TextStyle.html

§Examples

use stereokit_rust::{system::{TextStyle, Pivot, Align, Text, Lines, Hierarchy},
                     font::Font, material::Material, mesh::Mesh, maths::{Vec3, Matrix},
                     util::named_colors::{WHITE, GOLD, GREEN, BLUE, RED, BLACK}};

let font = Font::default();
let style = TextStyle::from_font(font, 0.88, WHITE);
let text = "ÂjlD;";
let size = Text::size_layout(text, Some(style), None);

let base_line_at   = -style.get_cap_height();
let ascender_at    = base_line_at + style.get_ascender();
let cap_height_at  = base_line_at + style.get_cap_height();
let descender_at   = base_line_at - style.get_descender();
let line_height_at = ascender_at - style.get_line_height_pct() * style. get_total_height();

let sizex = size.x;

let recenter = Matrix::t(Vec3::Y * 0.6);

filename_scr = "screenshots/text_style.jpeg"; fov_scr=110.0;
test_screenshot!( // !!!! Get a proper main loop !!!!
   Hierarchy::push(token, recenter, None);
   Text::add_at(token, text, Matrix::Y_180, Some(style), Some(GOLD.into()),
            Some(Pivot::TopCenter), Some(Align::TopLeft), None, None, None);
    
   Lines::add(token, [sizex, ascender_at, 0.0],    [-sizex, ascender_at, 0.0],    GREEN, None, 0.03  );
   Lines::add(token, [sizex, base_line_at, 0.0],   [-sizex, base_line_at, 0.0],   WHITE, None, 0.03  );
   Lines::add(token, [sizex, cap_height_at, 0.0],  [-sizex, cap_height_at, 0.0],  BLACK, None, 0.03  );
   Lines::add(token, [sizex, descender_at, 0.0],   [-sizex, descender_at, 0.0],   BLUE, None, 0.03  );
   Lines::add(token, [sizex, line_height_at, 0.0], [-sizex, line_height_at, 0.0], RED, None, 0.03  );
   Hierarchy::pop(token);
);
screenshot

Implementations§

Source§

impl TextStyle

Source

pub fn from_font( font: impl AsRef<Font>, layout_height_meters: f32, color_gamma: impl Into<Color128>, ) -> Self

Create a text style for use with other text functions! A text style is a font plus size/color/material parameters, and are used to keep text looking more consistent through the application by encouraging devs to re-use styles throughout the project.

This fn will create an unique Material for this style based on Default.ShaderFont. https://stereokit.net/Pages/StereoKit/TextStyle/FromFont.html

  • font - Font asset you want attached to this style.
  • layout_height_meters - Height of a text glyph in meters. StereoKit currently bases this on CapHeight.
  • color_gamma - The gamma space color of the text style. This will be embedded in the vertex color of the text mesh.

see also text_make_style

§Examples
use stereokit_rust::{system::TextStyle, font::Font, util::named_colors};

let font = Font::default();

let text_style = TextStyle::from_font(&font, 0.02, named_colors::WHITE);

assert_eq!(text_style.get_material().get_id(), "sk/text_style/2/material");
assert_eq!(text_style.get_layout_height(), 0.02);
Source

pub fn from_font_and_shader( font: impl AsRef<Font>, layout_height_meters: f32, shader: impl AsRef<Shader>, color_gamma: impl Into<Color128>, ) -> Self

Create a text style for use with other text functions! A text style is a font plus size/color/material parameters, and are used to keep text looking more consistent through the application by encouraging devs to re-use styles throughout the project.

This function will create an unique Material for this style based on the provided Shader. https://stereokit.net/Pages/StereoKit/TextStyle/FromFont.html

  • font - Font asset you want attached to this style.
  • layout_height_meters - Height of a text glyph in meters. StereoKit currently bases this on CapHeight.
  • shader - This style will create and use a unique/ Material based on the Shader that you provide here.
  • color_gamma - The gamma space color of the text style. This will be embedded in the vertex color of the text mesh.

see also text_make_style_shader

§Examples
use stereokit_rust::{system::TextStyle, font::Font, util::named_colors, shader::Shader};

let font = Font::default();
let shader = Shader::from_file("shaders/brick_pbr.hlsl.sks")
                         .expect("Brick_pbr should be a valid shader");

let text_style = TextStyle::from_font_and_shader(&font, 0.02, shader, named_colors::WHITE);

assert_eq!(text_style.get_material().get_id(), "sk/text_style/2/material");
assert_eq!(text_style.get_layout_height(), 0.02);
Source

pub fn from_font_and_material( font: impl AsRef<Font>, layout_height_meters: f32, material: impl AsRef<Material>, color_gamma: impl Into<Color128>, ) -> Self

Create a text style for use with other text functions! A text style is a font plus size/color/material parameters, and are used to keep text looking more consistent through the application by encouraging devs to re-use styles throughout the project.

This overload allows you to set the specific Material that is used. This can be helpful if you’re keeping styles similar enough to re-use the material and save on draw calls. If you don’t know what that means, then prefer using the overload that takes a Shader, or takes neither a Shader nor a Material! https://stereokit.net/Pages/StereoKit/TextStyle/FromFont.html

  • font - Font asset you want attached to this style.
  • layout_height_meters - Height of a text glyph in meters. StereoKit currently bases this on CapHeight.
  • material - Which material should be used to render the text with? Note that this does NOT duplicate the material, so the text with? Note that this does NOT duplicate the material, so some parameters of this Material instance will get overwritten, like the texture used for the glyph atlas. You should either use a new Material, or a Material that was already used with this same font.
  • color_gamma - The gamma space color of the text style. This will be embedded in the vertex color of the text mesh.

see also text_make_style_mat

§Examples
use stereokit_rust::{system::TextStyle, font::Font, util::named_colors, material::Material};

let font = Font::default();
let material = Material::pbr().copy();

let text_style = TextStyle::from_font_and_material(&font, 0.02, &material, named_colors::WHITE);

assert_eq!(text_style.get_material(), material);
assert_eq!(text_style.get_layout_height(), 0.02);
Source

pub fn char_height(&mut self, char_height: f32)

👎Deprecated since 0.40.0: please use TextStyle::layout_height

Height of a text glyph in meters. StereoKit currently bases this on the letter ‘T’. https://stereokit.net/Pages/StereoKit/TextStyle/CharHeight.html

see also text_style_set_layout_height

Source

pub fn layout_height(&mut self, height_meters: f32)

(meters) Layout height is the height of the font’s CapHeight, which is used for calculating the vertical height of the text when doing text layouts. This does not include the height of the descender , nor does it represent the maximum possible height a glyph may extend upwards (use Text::size_render). https://stereokit.net/Pages/StereoKit/TextStyle/LayoutHeight.html

see also text_style_set_layout_height

§Examples
use stereokit_rust::{font::Font, util::named_colors, system::TextStyle};

let font = Font::default();
let mut text_style = TextStyle::from_font(font, 0.02, named_colors::WHITE);
assert_eq!(text_style.get_layout_height(), 0.02);

text_style.layout_height(0.03);

assert!((text_style.get_layout_height() - 0.03) < 0.0001);
Source

pub fn total_height(&mut self, height_meters: f32)

(meters) Height from the layout descender to the layout ascender. This is most equivalent to the ‘font-size’ in CSS or other text layout tools. Since ascender and descenders can vary a lot, using layout_height in many cases can lead to more consistency in the long run. https://stereokit.net/Pages/StereoKit/TextStyle/TotalHeight.html

see also text_style_set_total_height

§Examples
use stereokit_rust::{font::Font, util::named_colors, system::TextStyle};

let font = Font::default();
let mut text_style = TextStyle::from_font(font, 0.02, named_colors::WHITE);

text_style.total_height(0.03);

assert_eq!(text_style.get_total_height(), 0.03);
Source

pub fn line_height_pct(&mut self, height_percent: f32)

This is the space a full line of text takes, from baseline to baseline, as a 0-1 percentage of the font’s character height. This is similar to CSS line-height, a value of 1.0 means the line takes only https://stereokit.net/Pages/StereoKit/TextStyle/LineHeightPct.html

see also text_style_set_line_height_pct

§Examples
use stereokit_rust::{font::Font, util::named_colors, system::TextStyle};

let font = Font::default();
let mut text_style = TextStyle::from_font(font, 0.02, named_colors::WHITE);
assert_eq!(text_style.get_layout_height(), 0.02);

text_style.line_height_pct(30.0);

assert_eq!(text_style.get_line_height_pct(), 30.0);
Source

pub fn get_material(&self) -> Material

This provides a reference to the Material used by this style, so you can override certain features! Note that if you’re creating TextStyles with manually provided Materials, this Material may not be unique to this style. https://stereokit.net/Pages/StereoKit/TextStyle/Material.html

see also text_style_get_material see example in [TextStyle::from_font_and material]

Source

pub fn get_char_height(&self) -> f32

👎Deprecated since 0.40.0: please use get_layout_height

Returns the maximum height of a text character using this style, in meters. https://stereokit.net/Pages/StereoKit/TextStyle/CharHeight.html

see also text_style_get_layout_height

Source

pub fn get_layout_height(&self) -> f32

(meters) Layout height is the height of the font’s ascender, which is used for calculating the vertical height of the text when doing text layouts. This does not include the height of the descender (use total_height), nor does it represent the maximum possible height a glyph may extend upwards (use Text::size_render). https://stereokit.net/Pages/StereoKit/TextStyle/LayoutHeight.html

see also text_style_get_layout_height see example in TextStyle::layout_height

Source

pub fn get_total_height(&self) -> f32

(meters) Height from the layout descender to the layout ascender. This is most equivalent to the ‘font-size’ in CSS or other text layout tools. Since ascender and descenders can vary a lot, using layout_height in many cases can lead to more consistency in the long run. https://stereokit.net/Pages/StereoKit/TextStyle/TotalHeight.html

see also text_style_get_total_height see example in TextStyle::total_height

Source

pub fn get_line_height_pct(&self) -> f32

This is the space a full line of text takes, from baseline to baseline, as a 0-1 percentage of the font’s character height. This is similar to CSS line-height, a value of 1.0 means the line takes only https://stereokit.net/Pages/StereoKit/TextStyle/LineHeightPct.html

see also text_style_get_line_height_pct see example in TextStyle::line_height_pct

Source

pub fn get_cap_height(&self) -> f32

(meters) The height of a standard captial letter, such as ‘H’ or ‘T’ https://stereokit.net/Pages/StereoKit/TextStyle/CapHeight.html

see also text_style_get_cap_height

§Examples
use stereokit_rust::{font::Font, util::named_colors, system::TextStyle};

let font = Font::default();
let mut text_style = TextStyle::from_font(font, 0.02, named_colors::WHITE);
assert_eq!(text_style.get_layout_height(), 0.02);

assert_eq!(text_style.get_cap_height(), 0.02);
Source

pub fn get_ascender(&self) -> f32

(meters) The layout ascender of the font, this is the height of the “tallest” glyphs as far as layout is concerned. Characters such as ‘l’ typically rise above the CapHeight, and this value usually matches this height. Some glyphs such as those with hats or umlauts will almost always be taller than this height (see Text::size_render), but this is not used when laying out characters. https://stereokit.net/Pages/StereoKit/TextStyle/Ascender.html

see also text_style_get_ascender

§Examples
use stereokit_rust::{font::Font, util::named_colors, system::TextStyle};

let font = Font::default();
let mut text_style = TextStyle::from_font(font, 0.02, named_colors::WHITE);
assert_eq!(text_style.get_layout_height(), 0.02);

//TODO: linux   assert_eq!(text_style.get_ascender(), 0.03);
//TODO: windows assert_eq!(text_style.get_ascender(),  0.021176472);
Source

pub fn get_descender(&self) -> f32

(meters) The layout descender of the font, this is the positive height below the baseline https://stereokit.net/Pages/StereoKit/TextStyle/Descender.html

see also text_style_get_descender

§Examples
use stereokit_rust::{font::Font, util::named_colors, system::TextStyle};

let font = Font::default();
let mut text_style = TextStyle::from_font(font, 0.02, named_colors::WHITE);
assert_eq!(text_style.get_layout_height(), 0.02);

assert_ne!(text_style.get_descender(), 0.0);

Trait Implementations§

Source§

impl Clone for TextStyle

Source§

fn clone(&self) -> TextStyle

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 Debug for TextStyle

Source§

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

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

impl Default for TextStyle

Source§

fn default() -> Self

This is the default text style used by StereoKit. https://stereokit.net/Pages/StereoKit/TextStyle/Default.html

Source§

impl PartialEq for TextStyle

Source§

fn eq(&self, other: &TextStyle) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for TextStyle

Source§

impl Eq for TextStyle

Source§

impl StructuralPartialEq for TextStyle

Auto Trait Implementations§

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> 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> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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> 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.
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