diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
commit | 6352e8e3196f78388b6c771073f9e03eaa612673 (patch) | |
tree | e23772f79a7fbc41bc9108951e9e136857484bf4 /source/rendering/StarTextPainter.hpp | |
parent | 6741a057e5639280d85d0f88ba26f000baa58f61 (diff) |
everything everywhere
all at once
Diffstat (limited to 'source/rendering/StarTextPainter.hpp')
-rw-r--r-- | source/rendering/StarTextPainter.hpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/source/rendering/StarTextPainter.hpp b/source/rendering/StarTextPainter.hpp new file mode 100644 index 0000000..64b2748 --- /dev/null +++ b/source/rendering/StarTextPainter.hpp @@ -0,0 +1,109 @@ +#ifndef STAR_TEXT_PAINTER_HPP +#define STAR_TEXT_PAINTER_HPP + +#include "StarFontTextureGroup.hpp" +#include "StarAnchorTypes.hpp" + +namespace Star { + +STAR_CLASS(TextPainter); + +namespace Text { + unsigned char const StartEsc = '\x1b'; + unsigned char const EndEsc = ';'; + unsigned char const CmdEsc = '^'; + unsigned char const SpecialCharLimit = ' '; + + String stripEscapeCodes(String const& s); + String preprocessEscapeCodes(String const& s); + String extractCodes(String const& s); +} + +enum class FontMode { + Normal, + Shadow +}; + +float const DefaultLineSpacing = 1.3f; + +struct TextPositioning { + TextPositioning(); + + TextPositioning(Vec2F pos, + HorizontalAnchor hAnchor = HorizontalAnchor::LeftAnchor, + VerticalAnchor vAnchor = VerticalAnchor::BottomAnchor, + Maybe<unsigned> wrapWidth = {}, + Maybe<unsigned> charLimit = {}); + + TextPositioning(Json const& v); + Json toJson() const; + + TextPositioning translated(Vec2F translation) const; + + Vec2F pos; + HorizontalAnchor hAnchor; + VerticalAnchor vAnchor; + Maybe<unsigned> wrapWidth; + Maybe<unsigned> charLimit; +}; + +// Renders text while caching individual glyphs for fast rendering but with *no +// kerning*. +class TextPainter { +public: + TextPainter(FontPtr font, RendererPtr renderer, TextureGroupPtr textureGroup); + + RectF renderText(String const& s, TextPositioning const& position); + RectF renderLine(String const& s, TextPositioning const& position); + RectF renderGlyph(String::Char c, TextPositioning const& position); + + RectF determineTextSize(String const& s, TextPositioning const& position); + RectF determineLineSize(String const& s, TextPositioning const& position); + RectF determineGlyphSize(String::Char c, TextPositioning const& position); + + int glyphWidth(String::Char c); + int stringWidth(String const& s); + + StringList wrapText(String const& s, Maybe<unsigned> wrapWidth); + + unsigned fontSize() const; + void setFontSize(unsigned size); + void setLineSpacing(float lineSpacing); + void setMode(FontMode mode); + void setSplitIgnore(String const& splitIgnore); + void setFontColor(Vec4B color); + void setProcessingDirectives(String directives); + + void cleanup(int64_t textureTimeout); + +private: + struct RenderSettings { + FontMode mode; + Vec4B color; + }; + + RectF doRenderText(String const& s, TextPositioning const& position, bool reallyRender, unsigned* charLimit); + RectF doRenderLine(String const& s, TextPositioning const& position, bool reallyRender, unsigned* charLimit); + RectF doRenderGlyph(String::Char c, TextPositioning const& position, bool reallyRender); + + void renderGlyph(String::Char c, Vec2F const& screenPos, unsigned fontSize, float scale, Vec4B const& color, String const& processingDirectives); + + RendererPtr m_renderer; + FontTextureGroup m_fontTextureGroup; + + unsigned m_fontSize; + float m_lineSpacing; + + RenderSettings m_renderSettings; + RenderSettings m_savedRenderSettings; + + String m_splitIgnore; + String m_splitForce; + String m_nonRenderedCharacters; + + String m_processingDirectives; +}; + +} + +#endif |