class EE::Graphics::RichText

Overview

A drawable class that renders rich text with multiple styles and spans. More…

#include <richtext.hpp>

class RichText: public EE::Graphics::Drawable {
public:
    // enums

    enum BaselineAlignment;
    enum InlineClear;
    enum InlineFloat;
    enum InlineSourceType;

    // structs

    struct BaselineAlignValue;
    struct InlineFragment;
    struct InlineItem;
    struct InlineSource;
    struct RenderParagraph;
    struct RenderSpan;

    // construction

    ~RichText();

    // methods

    static RichText* New();
    void addSpan(const String& text, const FontStyleConfig& style);

    void addSpan(
        const String& text,
        const FontStyleConfig& style,
        const Rectf& margin,
        const Rectf& padding,
        Float lineHeight = 0,
        const BaselineAlignValue& baselineAlign = {},
        InlineSource source = {}
    );

    void addSpan(const String& text, Font* font = nullptr, Uint32 characterSize = 0, Color color = Color::White, Uint32 style = Text::Regular, Color backgroundColor = Color::Transparent);
    void clear();
    void setFontStyleConfig(const FontStyleConfig& styleConfig);
    FontStyleConfig& getFontStyleConfig();
    const FontStyleConfig& getFontStyleConfig() const;
    void setAlign(Uint32 align);
    Uint32 getAlign() const;
    void setMaxWidth(Float width);
    Float getMaxWidth() const;
    virtual Float getMinIntrinsicWidth();
    virtual Float getMaxIntrinsicWidth();
    void addDrawable(std::shared_ptr<Drawable> drawable);
    void addCustomSize(const Sizef& size, InlineFloat floatType = InlineFloat::None, InlineClear clearType = InlineClear::None, Float baseline = -1.f, const BaselineAlignValue& baselineAlign = {}, InlineSource source = {});
    void addLineBreak();

    virtual void draw(
        const Float& X,
        const Float& Y,
        const Vector2f& scale = Vector2f::One,
        const Float& rotation = 0,
        BlendMode effect = BlendMode::Alpha(),
        const OriginPoint& rotationCenter = OriginPoint::OriginCenter,
        const OriginPoint& scaleCenter = OriginPoint::OriginCenter
    );

    virtual void draw();
    virtual void draw(const Vector2f& position);
    virtual void draw(const Vector2f& position, const Sizef& size);
    virtual bool isStateful();
    virtual Sizef getSize();
    virtual Sizef getPixelsSize();
    void invalidate();
    const std::vector<RenderParagraph>& getLines() const;
    void setLineHeight(Float height);
    Float getLineHeight() const;
    void setTextIndent(Float indent);
    Float getTextIndent() const;
    void setSelection(TextSelectionRange range);
    TextSelectionRange getSelection() const;
    void setSelectionColor(const Color& color);
    const Color& getSelectionColor() const;
    void setSelectionBackColor(const Color& color);
    const Color& getSelectionBackColor() const;
    Int64 getCharacterCount() const;
    Int64 findCharacterFromPos(const Vector2i& pos) const;
    Vector2f findCharacterPos(Int64 index) const;
    SmallVector<Rectf> getSelectionRects() const;
    String getSelectionString() const;
    void updateLayout();
    void invalidateLayout();
    const std::vector<InlineItem>& getInlineItems() const;
    const std::vector<InlineFragment>& getInlineFragments() const;

    void pushInlineBox(
        const Rectf& margin,
        const Rectf& padding,
        Float lineHeight,
        const BaselineAlignValue& baselineAlign,
        const Color& backgroundColor = Color::Transparent,
        Float borderWidth = 0,
        const Color& borderColor = Color::Transparent,
        Uint32 textDecoration = 0,
        InlineSource source = {},
        Drawable* backgroundDrawable = nullptr,
        Drawable* borderDrawable = nullptr,
        bool backgroundDrawableUsesFragmentColor = false
    );

    void popInlineBox();

    void addInlineText(
        const String& text,
        const FontStyleConfig& style,
        const Rectf& margin,
        const Rectf& padding,
        Float lineHeight,
        const BaselineAlignValue& baselineAlign,
        InlineSource source = {}
    );

    void addInlineAtomicBox(
        const Sizef& size,
        InlineFloat floatType,
        InlineClear clearType,
        Float baseline,
        bool isLineBreak,
        const BaselineAlignValue& baselineAlign,
        InlineSource source = {}
    );
};

Inherited Members

public:
    // enums

    enum Type;

    // methods

    virtual Sizef getSize() = 0;
    virtual Sizef getPixelsSize() = 0;
    virtual Float getMinIntrinsicWidth();
    virtual Float getMaxIntrinsicWidth();
    virtual void draw() = 0;
    virtual void draw(const Vector2f& position) = 0;
    virtual void draw(const Vector2f& position, const Sizef& size) = 0;
    virtual bool isStateful() = 0;
    void setAlpha(Uint8 alpha);
    const Uint8& getAlpha();
    void setColor(const Color& color);
    const Color& getColor() const;
    void setColorFilter(const Color& color);
    RGB getColorFilter();
    void clearColor();
    void clearColorFilter();
    void resetAlpha();
    Type getDrawableType() const;
    const Vector2f& getPosition() const;
    void setPosition(const Vector2f& position);
    virtual bool isDrawableResource() const;

Detailed Documentation

A drawable class that renders rich text with multiple styles and spans.

RichText allows rendering text with different fonts, sizes, colors, and styles mixed together. It supports word wrapping and alignment.

Construction

~RichText()

Destructor.

Methods

static RichText* New()

Returns:

A new instance of RichText.

void addSpan(const String& text, const FontStyleConfig& style)

Adds a text span with a specific style configuration.

Parameters:

text

The text content.

style

The font style configuration to apply.

void addSpan(const String& text, Font* font = nullptr, Uint32 characterSize = 0, Color color = Color::White, Uint32 style = Text::Regular, Color backgroundColor = Color::Transparent)

Adds a text span with individual style parameters.

Parameters:

text

The text content.

font

The font to use (optional, uses default if null).

characterSize

The character size (optional, uses default if 0).

color

The text color (optional, uses default if White).

style

The text style (optional, uses default if Regular).

void clear()

Clears all text spans.

void setFontStyleConfig(const FontStyleConfig& styleConfig)

Sets the default font style configuration used for new spans if not specified.

FontStyleConfig& getFontStyleConfig()

Returns:

The default font style configuration.

const FontStyleConfig& getFontStyleConfig() const

Returns:

The default font style configuration.

void setAlign(Uint32 align)

Sets the text alignment (Left, Center, Right).

Uint32 getAlign() const

Returns:

The text alignment.

void setMaxWidth(Float width)

Sets the maximum width for wrapping. If 0, wrapping is disabled.

Float getMaxWidth() const

Returns:

The maximum width for wrapping.

virtual Float getMinIntrinsicWidth()

Returns:

The minimum intrinsic width of the text block.

virtual Float getMaxIntrinsicWidth()

Returns:

The maximum intrinsic width of the text block.

void addDrawable(std::shared_ptr<Drawable> drawable)

Adds a drawable (e.g., an image) into the text flow.

Parameters:

drawable

The drawable to add.

void addCustomSize(const Sizef& size, InlineFloat floatType = InlineFloat::None, InlineClear clearType = InlineClear::None, Float baseline = -1.f, const BaselineAlignValue& baselineAlign = {}, InlineSource source = {})

Adds a custom size spacer into the text flow.

Parameters:

size

The physical dimensions of the spacer.

void addLineBreak()

Adds a virtual line break that is not associated with a DOM text character.

void invalidate()

Invalidates the layout, forcing a recalculation on the next update.

const std::vector<RenderParagraph>& getLines() const

Returns:

The list of rendered lines.

void setLineHeight(Float height)

Sets line-height as a multiplier of the font’s default line spacing. Use 0 to reset to font default.

Float getLineHeight() const

Returns:

The line height multiplier.

void setTextIndent(Float indent)

Sets text-indent (pixels) for the first line.

Float getTextIndent() const

Returns:

The text indent in pixels.

void setSelection(TextSelectionRange range)

Sets the text selection range.

TextSelectionRange getSelection() const

Returns:

The current text selection range.

void setSelectionColor(const Color& color)

Sets the selection text color.

const Color& getSelectionColor() const

Returns:

The selection text color.

void setSelectionBackColor(const Color& color)

Sets the selection background color.

const Color& getSelectionBackColor() const

Returns:

The selection background color.

Int64 getCharacterCount() const

Returns:

The total number of characters in the RichText.

Int64 findCharacterFromPos(const Vector2i& pos) const

Returns:

The character index at the given position.

Vector2f findCharacterPos(Int64 index) const

Returns:

The position of the character at the given index.

SmallVector<Rectf> getSelectionRects() const

Returns:

A list of rectangles that cover the selection.

String getSelectionString() const

Returns:

The current selection as a string.

void updateLayout()

Tries to update the layout if has been invalidated. This is automatically called before draw.

void invalidateLayout()

Invalidates the current layout

const std::vector<InlineItem>& getInlineItems() const

Returns:

The inline item tree.

const std::vector<InlineFragment>& getInlineFragments() const

Returns:

The generated inline fragments.

void pushInlineBox(
    const Rectf& margin,
    const Rectf& padding,
    Float lineHeight,
    const BaselineAlignValue& baselineAlign,
    const Color& backgroundColor = Color::Transparent,
    Float borderWidth = 0,
    const Color& borderColor = Color::Transparent,
    Uint32 textDecoration = 0,
    InlineSource source = {},
    Drawable* backgroundDrawable = nullptr,
    Drawable* borderDrawable = nullptr,
    bool backgroundDrawableUsesFragmentColor = false
)

Begin an inline box scope. All subsequent inline items are added as children of this box until popInlineBox() is called. An InlineItem::Box is created in the inline tree.

void popInlineBox()

Close the current inline box scope.

void addInlineText(
    const String& text,
    const FontStyleConfig& style,
    const Rectf& margin,
    const Rectf& padding,
    Float lineHeight,
    const BaselineAlignValue& baselineAlign,
    InlineSource source = {}
)

Add a text run to the current inline context.

void addInlineAtomicBox(
    const Sizef& size,
    InlineFloat floatType,
    InlineClear clearType,
    Float baseline,
    bool isLineBreak,
    const BaselineAlignValue& baselineAlign,
    InlineSource source = {}
)

Add an atomic inline-level box to the current inline context.