2024-02-25 14:46:47 +00:00
|
|
|
#pragma once
|
2023-06-20 04:33:09 +00:00
|
|
|
|
|
|
|
#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"
|
2024-04-21 20:07:59 +00:00
|
|
|
#include "StarText.hpp"
|
2023-06-20 04:33:09 +00:00
|
|
|
|
|
|
|
namespace Star {
|
|
|
|
|
2024-04-21 20:07:59 +00:00
|
|
|
// deprecated in favor of explicit shadow color
|
|
|
|
enum class FontMode : uint8_t {
|
2023-06-20 04:33:09 +00:00
|
|
|
Normal,
|
|
|
|
Shadow
|
|
|
|
};
|
|
|
|
|
2024-04-21 20:07:59 +00:00
|
|
|
inline Color const& fontModeToColor(FontMode mode) {
|
|
|
|
return mode == FontMode::Shadow ? Color::Black : Color::Clear;
|
|
|
|
}
|
|
|
|
|
|
|
|
STAR_CLASS(TextPainter);
|
2023-06-20 04:33:09 +00:00
|
|
|
|
|
|
|
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);
|
2024-04-21 20:07:59 +00:00
|
|
|
int stringWidth(StringView s, unsigned charLimit = 0);
|
2023-06-20 04:33:09 +00:00
|
|
|
|
2023-06-28 10:08:11 +00:00
|
|
|
|
2024-04-21 20:07:59 +00:00
|
|
|
typedef function<bool(StringView, unsigned)> WrapTextCallback;
|
2023-07-01 14:52:36 +00:00
|
|
|
bool processWrapText(StringView s, unsigned* wrapWidth, WrapTextCallback textFunc);
|
2023-06-28 10:08:11 +00:00
|
|
|
|
|
|
|
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 setFontColor(Vec4B color);
|
2024-04-21 20:07:59 +00:00
|
|
|
void setProcessingDirectives(StringView directives, bool back = false);
|
2023-06-21 09:46:23 +00:00
|
|
|
void setFont(String const& font);
|
2024-04-21 20:07:59 +00:00
|
|
|
TextStyle& setTextStyle(TextStyle const& textStyle);
|
|
|
|
void clearTextStyle();
|
2023-06-21 09:46:23 +00:00
|
|
|
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:
|
2024-04-21 20:07:59 +00:00
|
|
|
void modifyDirectives(Directives& directives);
|
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);
|
|
|
|
|
2024-04-21 20:07:59 +00:00
|
|
|
void renderPrimitives();
|
2024-04-23 21:44:53 +00:00
|
|
|
void renderGlyph(String::Char c, Vec2F const& screenPos, List<RenderPrimitive>& out, unsigned fontSize, float scale, Vec4B color, Directives const* processingDirectives = nullptr);
|
2023-08-04 13:47:52 +00:00
|
|
|
static FontPtr loadFont(String const& fontPath, Maybe<String> fontName = {});
|
2023-06-20 04:33:09 +00:00
|
|
|
|
|
|
|
RendererPtr m_renderer;
|
2024-04-21 20:07:59 +00:00
|
|
|
List<RenderPrimitive> m_shadowPrimitives;
|
|
|
|
List<RenderPrimitive> m_backPrimitives;
|
|
|
|
List<RenderPrimitive> m_frontPrimitives;
|
2023-06-20 04:33:09 +00:00
|
|
|
FontTextureGroup m_fontTextureGroup;
|
|
|
|
|
2024-04-21 20:07:59 +00:00
|
|
|
TextStyle m_defaultRenderSettings;
|
|
|
|
TextStyle m_renderSettings;
|
|
|
|
TextStyle m_savedRenderSettings;
|
2023-06-20 04:33:09 +00:00
|
|
|
|
|
|
|
String m_nonRenderedCharacters;
|
2023-06-21 13:13:37 +00:00
|
|
|
TrackerListenerPtr m_reloadTracker;
|
2023-06-20 04:33:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|