pub struct FontMesh {
pub data: Vec<u8>,
}Expand description
Asset containing font data for 3D text mesh generation.
This asset type holds the raw bytes of a TrueType font file.
Fonts are automatically loaded from the asset server and used by the TextMesh
component to generate 3D mesh geometry.
§Loading Fonts
Fonts are loaded like any other Bevy asset:
let font: Handle<FontMesh> = asset_server.load("fonts/MyFont.ttf");Place your font files in the assets/fonts/ directory (or any subdirectory of assets/).
§Supported Formats
- TrueType (
.ttf)
Note: Some OpenType fonts (.otf) with TrueType outlines are supported,
but OpenType fonts with CFF/PostScript outlines are not (limitation of ttf-parser).
Fields§
§data: Vec<u8>Raw font file data in TTF or OTF format.
Implementations§
Source§impl FontMesh
impl FontMesh
Sourcepub fn glyph_metrics(&self, character: char) -> Option<GlyphMetrics>
pub fn glyph_metrics(&self, character: char) -> Option<GlyphMetrics>
Get metrics for a specific character.
Returns None if the character is not in the font.
§Example
if let Some(font) = font_assets.get(&font_handle) {
if let Some(metrics) = font.glyph_metrics('A') {
println!("Advance width of 'A': {}", metrics.advance);
}
}Sourcepub fn font_metrics(&self) -> Option<FontMetrics>
pub fn font_metrics(&self) -> Option<FontMetrics>
Get font-level metrics (ascender, descender, line height, etc.)
Returns None if the font data is invalid.
§Example
if let Some(font) = font_assets.get(&font_handle) {
if let Some(metrics) = font.font_metrics() {
println!("Line height: {}", metrics.line_height);
}
}Sourcepub fn text_width(&self, text: &str) -> f32
pub fn text_width(&self, text: &str) -> f32
Calculate the width of a text string.
This sums the advance widths of all characters. Does not account for kerning.
§Example
if let Some(font) = font_assets.get(&font_handle) {
let width = font.text_width("Hello");
println!("Text width: {}", width);
}Sourcepub fn char_positions(&self, text: &str) -> Vec<(usize, f32)>
pub fn char_positions(&self, text: &str) -> Vec<(usize, f32)>
Get character positions for a line of text.
Returns a vector of (char_index, x_position) pairs for each character. Useful for cursor positioning in text editors.
§Example
if let Some(font) = font_assets.get(&font_handle) {
let positions = font.char_positions("Hello");
for (idx, x) in positions {
println!("Char {} at x={}", idx, x);
}
}Trait Implementations§
Source§impl TypePath for FontMesh
impl TypePath for FontMesh
Source§fn type_path() -> &'static str
fn type_path() -> &'static str
Source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
Source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
Source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
Source§impl VisitAssetDependencies for FontMesh
impl VisitAssetDependencies for FontMesh
fn visit_dependencies(&self, visit: &mut impl FnMut(UntypedAssetId))
impl Asset for FontMesh
Auto Trait Implementations§
impl Freeze for FontMesh
impl RefUnwindSafe for FontMesh
impl Send for FontMesh
impl Sync for FontMesh
impl Unpin for FontMesh
impl UnsafeUnpin for FontMesh
impl UnwindSafe for FontMesh
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
T ShaderType for self. When used in AsBindGroup
derives, it is safe to assume that all images in self exist.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>, which can then be
downcast into Box<dyn 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>, which 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.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
Source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path.Source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
Source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident.Source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name.Source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more