Skip to main content

Text

Struct Text 

Source
pub struct Text { /* private fields */ }
Expand description

Text builder for creating styled text.

Text positioning uses a top-left coordinate system where (0, 0) is the top-left corner and Y increases downward, consistent with UI layout systems like CSS and Flutter.

§SDF Effects

Text supports effects like shadows, outlines, and glows via SDF (Signed Distance Field) rendering. When effects are present, the text automatically uses SDF mode:

let text = Text::new("Hello")
    .size(24.0)
    .with_shadow(Vec2::new(2.0, 2.0), 2.0, Color::rgba(0.0, 0.0, 0.0, 0.5))
    .with_outline(1.5, Color::BLACK);

Implementations§

Source§

impl Text

Source

pub fn new(content: impl Into<String>) -> Self

Create a new text instance.

Source

pub fn size(self, size: f32) -> Self

Set the font size in pixels.

Source

pub fn line_height(self, height: f32) -> Self

Set the line height multiplier.

Source

pub fn font(self, family: impl Into<String>) -> Self

Set the font family.

Source

pub fn weight(self, weight: FontWeight) -> Self

Set the font weight.

Source

pub fn style(self, style: FontStyle) -> Self

Set the font style.

Source

pub fn stretch(self, stretch: FontStretch) -> Self

Set the font stretch.

Source

pub fn font_attrs(self, attrs: FontAttributes) -> Self

Set font attributes.

Source

pub fn color(self, color: Color) -> Self

Set the text color.

Source

pub fn align(self, align: TextAlign) -> Self

Set the text alignment (horizontal).

Source

pub fn vertical_align(self, vertical_align: VerticalAlign) -> Self

Set the vertical alignment.

Source

pub fn wrap(self, wrap: TextWrap) -> Self

Set the text wrapping mode.

Source

pub fn line_break(self, config: LineBreakConfig) -> Self

Set line breaking configuration for advanced control.

This provides more control than .wrap() alone, allowing configuration of hyphen breaks and future UAX#14 options.

§Arguments
  • config - The line break configuration
§Example
use astrelis_text::{Text, LineBreakConfig, TextWrap};

let text = Text::new("Long text with a-very-long-hyphenated-word")
    .line_break(
        LineBreakConfig::new(TextWrap::WordOrGlyph)
            .with_hyphen_breaks(true)
    )
    .max_width(200.0);
Source

pub fn max_width(self, width: f32) -> Self

Set the maximum width for text wrapping.

Source

pub fn max_height(self, height: f32) -> Self

Set the maximum height for text.

Source

pub fn letter_spacing(self, spacing: f32) -> Self

Set letter spacing in pixels.

Source

pub fn word_spacing(self, spacing: f32) -> Self

Set word spacing in pixels.

Source

pub fn bold(self) -> Self

Make the text bold.

Source

pub fn italic(self) -> Self

Make the text italic.

Source

pub fn get_content(&self) -> &str

Source

pub fn get_font_size(&self) -> f32

Source

pub fn get_line_height(&self) -> f32

Source

pub fn get_font_attrs(&self) -> &FontAttributes

Source

pub fn get_color(&self) -> Color

Source

pub fn get_align(&self) -> TextAlign

Source

pub fn get_vertical_align(&self) -> VerticalAlign

Source

pub fn get_wrap(&self) -> TextWrap

Source

pub fn get_max_width(&self) -> Option<f32>

Source

pub fn get_max_height(&self) -> Option<f32>

Source

pub fn get_letter_spacing(&self) -> f32

Source

pub fn get_word_spacing(&self) -> f32

Source

pub fn get_break_at_hyphens(&self) -> bool

Check if hyphen breaks are enabled.

Source

pub fn with_effect(self, effect: TextEffect) -> Self

Add a single text effect.

Effects are rendered using SDF (Signed Distance Field) rendering, which enables high-quality shadows, outlines, and glows at any scale.

Multiple effects can be combined by chaining calls. Effects are rendered in priority order: shadows first (background), then outlines (foreground).

§Arguments
  • effect - The text effect to add
§Example
use astrelis_text::{Text, TextEffect, Color};
use astrelis_core::math::Vec2;

// Single shadow effect
let text = Text::new("Hello")
    .size(32.0)
    .with_effect(TextEffect::shadow(
        Vec2::new(2.0, 2.0),
        Color::rgba(0.0, 0.0, 0.0, 0.5)
    ));

// Combine shadow and outline
let text = Text::new("Bold")
    .size(48.0)
    .with_effect(TextEffect::shadow(
        Vec2::new(2.0, 2.0),
        Color::BLACK
    ))
    .with_effect(TextEffect::outline(
        2.0,
        Color::WHITE
    ));
Source

pub fn with_effects(self, effects: TextEffects) -> Self

Add multiple text effects at once.

Source

pub fn with_shadow(self, offset: Vec2, color: Color) -> Self

Add a shadow effect.

Creates a drop shadow behind the text. This is the most commonly used effect for improving text readability on varied backgrounds.

§Arguments
  • offset - Shadow offset in pixels (x, y). Positive values offset down and right.
  • color - Shadow color (typically semi-transparent black)
§Example
use astrelis_text::{Text, Color};
use astrelis_core::math::Vec2;

// Standard drop shadow (2px right and down)
let text = Text::new("Readable")
    .size(24.0)
    .with_shadow(Vec2::new(2.0, 2.0), Color::rgba(0.0, 0.0, 0.0, 0.5));
Source

pub fn with_shadow_blurred( self, offset: Vec2, blur_radius: f32, color: Color, ) -> Self

Add a blurred shadow effect for softer appearance.

Creates a drop shadow with a blur radius, producing a softer, more natural shadow that’s useful for headings and titles.

§Arguments
  • offset - Shadow offset in pixels (x, y)
  • blur_radius - Blur radius in pixels (0 = hard edge, 2-5 = soft shadow)
  • color - Shadow color (typically semi-transparent)
§Example
use astrelis_text::{Text, Color};
use astrelis_core::math::Vec2;

// Soft shadow for a heading
let text = Text::new("Title")
    .size(48.0)
    .with_shadow_blurred(
        Vec2::new(3.0, 3.0),
        4.0,  // 4px blur radius
        Color::rgba(0.0, 0.0, 0.0, 0.6)
    );
Source

pub fn with_outline(self, width: f32, color: Color) -> Self

Add an outline effect around the text.

Creates a stroke around text characters, useful for making text stand out against complex backgrounds or creating stylized text.

§Arguments
  • width - Outline width in pixels (typically 1-3px)
  • color - Outline color (often contrasting with text color)
§Example
use astrelis_text::{Text, Color};

// White text with black outline (classic game text style)
let text = Text::new("Game Text")
    .size(32.0)
    .color(Color::WHITE)
    .with_outline(2.0, Color::BLACK);

// Bold outline for emphasis
let text = Text::new("Important!")
    .size(40.0)
    .color(Color::YELLOW)
    .with_outline(3.0, Color::RED);
Source

pub fn with_glow(self, radius: f32, color: Color, intensity: f32) -> Self

Add a glow effect around the text.

Creates a soft luminous halo around text, useful for magical, sci-fi, or neon-style text effects.

§Arguments
  • radius - Glow radius in pixels (typically 3-10px)
  • color - Glow color (often bright, saturated colors)
  • intensity - Glow intensity multiplier (0.5 to 1.0 for subtle, > 1.0 for intense)
§Example
use astrelis_text::{Text, Color};

// Neon blue glow
let text = Text::new("Cyber")
    .size(36.0)
    .color(Color::CYAN)
    .with_glow(6.0, Color::BLUE, 0.8);

// Intense magical glow
let text = Text::new("Magic")
    .size(40.0)
    .color(Color::WHITE)
    .with_glow(8.0, Color::rgba(1.0, 0.0, 1.0, 1.0), 1.2);
Source

pub fn render_mode(self, mode: TextRenderMode) -> Self

Set the render mode (Bitmap or SDF).

By default, render mode is auto-selected based on font size and effects:

  • Bitmap for small text (< 24px) without effects - sharper at small sizes
  • SDF for large text (>= 24px) or text with effects - scalable and smooth

Use this method to override the automatic selection.

§Arguments
  • mode - The render mode to use:
    • TextRenderMode::Bitmap - Traditional rasterized glyphs
    • TextRenderMode::SDF { spread } - Distance field rendering
§Example
use astrelis_text::{Text, TextRenderMode};

// Force bitmap even for large text
let text = Text::new("Large but Sharp")
    .size(48.0)
    .render_mode(TextRenderMode::Bitmap);

// Force SDF with custom spread
let text = Text::new("Custom SDF")
    .size(20.0)
    .render_mode(TextRenderMode::SDF { spread: 6.0 });
Source

pub fn sdf(self) -> Self

Force SDF rendering mode with default spread.

Useful for text that needs to scale smoothly or maintain quality at various sizes. Equivalent to .render_mode(TextRenderMode::SDF { spread: 4.0 }).

§When to Use
  • Text that will be animated or scaled
  • Text in UI elements that change size
  • High-DPI displays where extra sharpness helps
  • When preparing text for future effects
§Example
use astrelis_text::Text;

// Small text that will be scaled up smoothly
let text = Text::new("UI Label")
    .size(14.0)
    .sdf();  // Force SDF for smooth scaling
Source

pub fn get_effects(&self) -> Option<&TextEffects>

Get the text effects, if any.

Source

pub fn get_render_mode(&self) -> Option<TextRenderMode>

Get the render mode, if explicitly set.

Source

pub fn has_effects(&self) -> bool

Check if this text has any effects configured.

Source

pub fn with_decoration(self, decoration: TextDecoration) -> Self

Set text decoration (underline, strikethrough, background).

Text decorations are rendered separately from the text glyphs:

  • Background: colored quad behind text (rendered first)
  • Underline: line below the text baseline
  • Strikethrough: line through the middle of text
§Arguments
  • decoration - The decoration configuration
§Example
use astrelis_text::{Text, TextDecoration, UnderlineStyle, Color};

let decoration = TextDecoration::new()
    .underline(UnderlineStyle::solid(Color::BLUE, 1.0))
    .background(Color::YELLOW);

let text = Text::new("Important text")
    .with_decoration(decoration);
Source

pub fn underline(self, color: Color) -> Self

Add a solid underline with the specified color.

Convenience method for adding a simple solid underline. For more control (thickness, offset, style), use with_decoration().

§Arguments
  • color - The underline color
§Example
use astrelis_text::{Text, Color};

let text = Text::new("Underlined")
    .underline(Color::BLUE);
Source

pub fn strikethrough(self, color: Color) -> Self

Add a solid strikethrough with the specified color.

Convenience method for adding a simple solid strikethrough. For more control (thickness, offset, style), use with_decoration().

§Arguments
  • color - The strikethrough color
§Example
use astrelis_text::{Text, Color};

let text = Text::new("Deleted")
    .strikethrough(Color::RED);
Source

pub fn background_color(self, color: Color) -> Self

Add a background highlight color.

Convenience method for adding a simple background highlight. For more control (padding), use with_decoration().

§Arguments
  • color - The background highlight color
§Example
use astrelis_text::{Text, Color};

let text = Text::new("Highlighted")
    .background_color(Color::YELLOW);
Source

pub fn get_decoration(&self) -> Option<&TextDecoration>

Get the text decoration, if any.

Source

pub fn has_decoration(&self) -> bool

Check if this text has any decoration configured.

Source

pub fn effective_render_mode(&self) -> TextRenderMode

Determine the appropriate render mode for this text.

Returns the explicitly set mode via .render_mode() or .sdf(), or auto-selects based on font size and effects using the hybrid rendering strategy.

§Auto-Selection Logic

If no explicit mode is set:

  • Font size >= 24px → SDF (better scaling for large text)
  • Has effects → SDF (required for shadows, outlines, glows)
  • Otherwise → Bitmap (sharper for small UI text)
§Returns

The render mode that will be used when this text is rendered

§Example
use astrelis_text::{Text, TextRenderMode};

let text = Text::new("Hello").size(32.0);
assert!(text.effective_render_mode().is_sdf());  // Auto-selected SDF for 32px

let text = Text::new("Small").size(14.0);
assert!(!text.effective_render_mode().is_sdf());  // Auto-selected Bitmap for 14px

let text = Text::new("Effects").size(16.0).with_shadow(...);
assert!(text.effective_render_mode().is_sdf());  // Auto-selected SDF for effects

Trait Implementations§

Source§

impl Default for Text

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Text

§

impl RefUnwindSafe for Text

§

impl Send for Text

§

impl Sync for Text

§

impl Unpin for Text

§

impl UnsafeUnpin for Text

§

impl UnwindSafe for Text

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<T> for T

Source§

fn downcast(&self) -> &T

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

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
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,