pub struct FontRenderer { /* private fields */ }Expand description
Font renderer for rendering text with WGPU.
This is the hybrid renderer that combines both bitmap and SDF backends, automatically selecting the best mode based on text size and effects.
- Small text (< 24px) without effects: uses bitmap for sharpness
- Large text (>= 24px) or text with effects: uses SDF for quality
This is the backwards-compatible default renderer.
Implementations§
Source§impl FontRenderer
impl FontRenderer
Sourcepub fn new(context: Arc<GraphicsContext>, font_system: FontSystem) -> Self
pub fn new(context: Arc<GraphicsContext>, font_system: FontSystem) -> Self
Create a new font renderer.
Sourcepub fn new_with_atlas_size(
context: Arc<GraphicsContext>,
font_system: FontSystem,
atlas_size: u32,
) -> Self
pub fn new_with_atlas_size( context: Arc<GraphicsContext>, font_system: FontSystem, atlas_size: u32, ) -> Self
Create a new font renderer with a custom atlas size.
Sourcepub fn with_config(
context: Arc<GraphicsContext>,
font_system: FontSystem,
config: TextRendererConfig,
) -> Self
pub fn with_config( context: Arc<GraphicsContext>, font_system: FontSystem, config: TextRendererConfig, ) -> Self
Create a new font renderer with custom configuration.
Sourcepub fn measure_text(&self, text: &Text) -> (f32, f32)
pub fn measure_text(&self, text: &Text) -> (f32, f32)
Measure text dimensions without rendering.
Sourcepub fn buffer_bounds(&self, buffer: &TextBuffer) -> (f32, f32)
pub fn buffer_bounds(&self, buffer: &TextBuffer) -> (f32, f32)
Get the logical (unscaled) bounds of a prepared text buffer.
Sourcepub fn get_text_metrics(&self, text: &Text) -> TextMetrics
pub fn get_text_metrics(&self, text: &Text) -> TextMetrics
Get font metrics for the given text style.
Sourcepub fn get_baseline_offset(&self, text: &Text) -> f32
pub fn get_baseline_offset(&self, text: &Text) -> f32
Get the baseline offset from the top of the text bounding box.
Sourcepub fn set_render_mode(&mut self, mode: TextRenderMode)
pub fn set_render_mode(&mut self, mode: TextRenderMode)
Set the text render mode (Bitmap or SDF).
Sourcepub fn render_mode(&self) -> TextRenderMode
pub fn render_mode(&self) -> TextRenderMode
Get the current render mode.
Sourcepub fn set_sdf_config(&mut self, config: SdfConfig)
pub fn set_sdf_config(&mut self, config: SdfConfig)
Set SDF configuration.
Sourcepub fn sdf_config(&self) -> &SdfConfig
pub fn sdf_config(&self) -> &SdfConfig
Get the current SDF configuration.
Sourcepub fn select_render_mode(font_size: f32, has_effects: bool) -> TextRenderMode
pub fn select_render_mode(font_size: f32, has_effects: bool) -> TextRenderMode
Determine the appropriate render mode based on font size and effects.
- Small text (< 24px) without effects: use Bitmap for sharpness
- Large text (>= 24px) or text with effects: use SDF for quality
Sourcepub fn set_viewport(&mut self, viewport: Viewport)
pub fn set_viewport(&mut self, viewport: Viewport)
Set the viewport for rendering.
Sourcepub fn prepare(&mut self, text: &Text) -> TextBuffer
pub fn prepare(&mut self, text: &Text) -> TextBuffer
Prepare text for rendering.
Sourcepub fn draw_text(&mut self, buffer: &mut TextBuffer, position: Vec2)
pub fn draw_text(&mut self, buffer: &mut TextBuffer, position: Vec2)
Draw text at a position.
The position represents the top-left corner of the text’s bounding box.
Sourcepub fn draw_text_with_effects(
&mut self,
buffer: &mut TextBuffer,
position: Vec2,
effects: &TextEffects,
)
pub fn draw_text_with_effects( &mut self, buffer: &mut TextBuffer, position: Vec2, effects: &TextEffects, )
Draw text with effects at a position using SDF rendering.
Sourcepub fn draw_text_with_decoration(
&mut self,
buffer: &mut TextBuffer,
position: Vec2,
text: &Text,
)
pub fn draw_text_with_decoration( &mut self, buffer: &mut TextBuffer, position: Vec2, text: &Text, )
Draw text with decoration (underline, strikethrough, background).
This method handles both the text rendering and any decorations. Decorations are rendered in the correct order:
- Background: behind text
- Text glyphs
- Underline/strikethrough: on top of text
§Arguments
buffer- The prepared text bufferposition- Top-left position of the texttext- The Text object containing decoration configuration
Sourcepub fn render(&mut self, render_pass: &mut RenderPass<'_>)
pub fn render(&mut self, render_pass: &mut RenderPass<'_>)
Render all queued text to the given render pass.
Automatically selects bitmap or SDF pipeline based on the current render mode. Renders in the correct order:
- Background decorations (behind text)
- Text glyphs
- Line decorations (underline, strikethrough - on top of text)
Sourcepub fn font_system(&self) -> Arc<RwLock<FontSystem>>
pub fn font_system(&self) -> Arc<RwLock<FontSystem>>
Get the font system.
Sourcepub fn swash_cache(&self) -> Arc<RwLock<SwashCache>>
pub fn swash_cache(&self) -> Arc<RwLock<SwashCache>>
Get the swash cache.
Sourcepub fn atlas_size(&self) -> u32
pub fn atlas_size(&self) -> u32
Get the atlas size in pixels.
Sourcepub fn atlas_texture_view(&self) -> &TextureView
pub fn atlas_texture_view(&self) -> &TextureView
Get the atlas texture view for binding.
Sourcepub fn atlas_sampler(&self) -> &Sampler
pub fn atlas_sampler(&self) -> &Sampler
Get the atlas sampler for binding.
Sourcepub fn is_atlas_dirty(&self) -> bool
pub fn is_atlas_dirty(&self) -> bool
Check if the atlas has pending changes.
Sourcepub fn upload_atlas_if_dirty(&mut self)
pub fn upload_atlas_if_dirty(&mut self)
Upload atlas data to GPU if dirty.
Sourcepub fn ensure_glyph_in_atlas(
&mut self,
cache_key: CacheKey,
) -> Option<&AtlasEntry>
pub fn ensure_glyph_in_atlas( &mut self, cache_key: CacheKey, ) -> Option<&AtlasEntry>
Ensure a glyph is in the atlas using a cache key.
Sourcepub fn get_glyph_placement(
&mut self,
cache_key: CacheKey,
) -> Option<GlyphPlacement>
pub fn get_glyph_placement( &mut self, cache_key: CacheKey, ) -> Option<GlyphPlacement>
Get glyph placement information.
Sourcepub fn ensure_glyph_with_placement(
&mut self,
cache_key: CacheKey,
) -> Option<(AtlasEntry, GlyphPlacement)>
pub fn ensure_glyph_with_placement( &mut self, cache_key: CacheKey, ) -> Option<(AtlasEntry, GlyphPlacement)>
Ensure a glyph is in the atlas and get its placement info.
Sourcepub fn get_atlas_entry(&self, cache_key: CacheKey) -> Option<&AtlasEntry>
pub fn get_atlas_entry(&self, cache_key: CacheKey) -> Option<&AtlasEntry>
Get an atlas entry by cache key (if it exists).
Trait Implementations§
Source§impl TextRender for FontRenderer
impl TextRender for FontRenderer
Source§fn draw_text(&mut self, buffer: &mut TextBuffer, position: Vec2)
fn draw_text(&mut self, buffer: &mut TextBuffer, position: Vec2)
Source§fn render(&mut self, render_pass: &mut RenderPass<'_>)
fn render(&mut self, render_pass: &mut RenderPass<'_>)
Source§fn set_viewport(&mut self, viewport: Viewport)
fn set_viewport(&mut self, viewport: Viewport)
Source§fn buffer_bounds(&self, buffer: &TextBuffer) -> (f32, f32)
fn buffer_bounds(&self, buffer: &TextBuffer) -> (f32, f32)
Auto Trait Implementations§
impl Freeze for FontRenderer
impl !RefUnwindSafe for FontRenderer
impl Send for FontRenderer
impl Sync for FontRenderer
impl Unpin for FontRenderer
impl UnsafeUnpin for FontRenderer
impl !UnwindSafe for FontRenderer
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.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
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