osb/source/rendering/StarTextPainter.hpp

123 lines
3.6 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#ifndef STAR_TEXT_PAINTER_HPP
#define STAR_TEXT_PAINTER_HPP
#include "StarFontTextureGroup.hpp"
#include "StarAnchorTypes.hpp"
2023-06-21 09:46:23 +00:00
#include "StarRoot.hpp"
2023-06-28 10:08:11 +00:00
#include "StarStringView.hpp"
2023-06-20 04:33:09 +00:00
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:
2023-06-21 09:46:23 +00:00
TextPainter(RendererPtr renderer, TextureGroupPtr textureGroup);
2023-06-20 04:33:09 +00:00
2023-06-28 10:08:11 +00:00
RectF renderText(StringView s, TextPositioning const& position);
RectF renderLine(StringView s, TextPositioning const& position);
2023-06-20 04:33:09 +00:00
RectF renderGlyph(String::Char c, TextPositioning const& position);
2023-06-28 10:08:11 +00:00
RectF determineTextSize(StringView s, TextPositioning const& position);
RectF determineLineSize(StringView s, TextPositioning const& position);
2023-06-20 04:33:09 +00:00
RectF determineGlyphSize(String::Char c, TextPositioning const& position);
int glyphWidth(String::Char c);
2023-06-28 10:08:11 +00:00
int stringWidth(StringView s);
2023-06-20 04:33:09 +00:00
2023-06-28 10:08:11 +00:00
typedef function<bool(StringView text, int line)> WrapTextCallback;
typedef function<bool(StringView command)> WrapCommandsCallback;
void processWrapText(StringView s, Maybe<unsigned> wrapWidth, WrapTextCallback textFunc, WrapCommandsCallback commandFunc = WrapCommandsCallback(), bool includeCommandSides = false);
List<StringView> wrapTextViews(StringView s, Maybe<unsigned> wrapWidth);
StringList wrapText(StringView s, Maybe<unsigned> wrapWidth);
2023-06-20 04:33:09 +00:00
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);
2023-06-21 09:46:23 +00:00
void setFont(String const& font);
void addFont(FontPtr const& font, String const& name);
2023-06-21 13:13:37 +00:00
void reloadFonts();
2023-06-20 04:33:09 +00:00
void cleanup(int64_t textureTimeout);
2023-06-28 10:08:11 +00:00
void applyCommands(StringView unsplitCommands);
2023-06-20 04:33:09 +00:00
private:
struct RenderSettings {
FontMode mode;
Vec4B color;
2023-06-21 09:46:23 +00:00
String font;
2023-06-21 10:29:23 +00:00
String directives;
2023-06-20 04:33:09 +00:00
};
2023-06-28 10:08:11 +00:00
RectF doRenderText(StringView s, TextPositioning const& position, bool reallyRender, unsigned* charLimit);
RectF doRenderLine(StringView s, TextPositioning const& position, bool reallyRender, unsigned* charLimit);
2023-06-20 04:33:09 +00:00
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;
2023-06-21 13:13:37 +00:00
TrackerListenerPtr m_reloadTracker;
2023-06-20 04:33:09 +00:00
};
}
#endif