Skip to main content

TextProvider

Trait TextProvider 

Source
pub trait TextProvider {
    type Char: Clone + Debug;

    // Required methods
    fn decode_byte(&self, byte: u8) -> Option<Self::Char>;
    fn render_char(&self, c: &Self::Char, buffer: &mut TileBuffer);
    fn string_width(&self, text: &[Self::Char]) -> u16;
    fn is_control_code(&self, c: &Self::Char) -> bool;
    fn process_control(
        &self,
        c: &Self::Char,
        state: &mut DialogState,
    ) -> ControlAction;

    // Provided method
    fn decode_stream(&self, bytes: &[u8]) -> TextStream<Self::Char> { ... }
}
Expand description

The core abstraction for text encoding in a JRPG engine.

Implementations provide:

  • A character type (Char) representing decoded text units.
  • Single-byte decoding (decode_byte) from a custom charmap.
  • Stream decoding (decode_stream) from raw byte sequences.
  • Tile rendering (render_char) into a TileBuffer.
  • Width measurement (string_width) for layout.
  • Control-code detection (is_control_code) and processing (process_control).

§Associated Type

  • Char — The decoded character type. May be an enum with variants for printable characters, control codes, and substitutions.

§Example

struct MyProvider;

impl TextProvider for MyProvider {
    type Char = MyChar;
    // ...
}

Required Associated Types§

Source

type Char: Clone + Debug

The decoded character type produced by this provider.

Required Methods§

Source

fn decode_byte(&self, byte: u8) -> Option<Self::Char>

Decodes a single byte into an optional character.

Returns None if the byte is not recognised by this charmap.

Source

fn render_char(&self, c: &Self::Char, buffer: &mut TileBuffer)

Draws a single character into the tile buffer.

Implementations should write the character’s tile at the buffer’s current cursor position and advance the cursor by the appropriate amount (typically one tile for fixed-width fonts).

Source

fn string_width(&self, text: &[Self::Char]) -> u16

Returns the pixel width of the given text when rendered.

Used for text layout and centering calculations.

Source

fn is_control_code(&self, c: &Self::Char) -> bool

Returns true if the character is a control code (not printable).

Source

fn process_control( &self, c: &Self::Char, state: &mut DialogState, ) -> ControlAction

Processes a control code and returns the resulting action.

The state parameter allows the provider to inspect or modify the dialog state (e.g. to track page breaks).

Provided Methods§

Source

fn decode_stream(&self, bytes: &[u8]) -> TextStream<Self::Char>

Decodes a byte slice into a TextStream.

The default implementation calls [decode_byte] for each byte, collecting all Some results. Override for multi-byte encodings.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§