| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | #ifndef ENGINE_TEXTRENDER_H |
| 4 | #define ENGINE_TEXTRENDER_H |
| 5 | #include "kernel.h" |
| 6 | |
| 7 | #include <base/color.h> |
| 8 | |
| 9 | #include <engine/graphics.h> |
| 10 | |
| 11 | #include <cstdint> |
| 12 | #include <memory> |
| 13 | |
| 14 | enum |
| 15 | { |
| 16 | TEXTFLAG_RENDER = 1 << 0, |
| 17 | TEXTFLAG_DISALLOW_NEWLINE = 1 << 1, |
| 18 | TEXTFLAG_STOP_AT_END = 1 << 2, |
| 19 | TEXTFLAG_ELLIPSIS_AT_END = 1 << 3, |
| 20 | }; |
| 21 | |
| 22 | enum ETextAlignment |
| 23 | { |
| 24 | TEXTALIGN_LEFT = 0, |
| 25 | TEXTALIGN_CENTER = 1 << 1, |
| 26 | TEXTALIGN_RIGHT = 1 << 2, |
| 27 | TEXTALIGN_TOP = 0, // this is also 0, so the default alignment is top-left |
| 28 | TEXTALIGN_MIDDLE = 1 << 3, |
| 29 | TEXTALIGN_BOTTOM = 1 << 4, |
| 30 | |
| 31 | TEXTALIGN_TL = TEXTALIGN_TOP | TEXTALIGN_LEFT, |
| 32 | TEXTALIGN_TC = TEXTALIGN_TOP | TEXTALIGN_CENTER, |
| 33 | TEXTALIGN_TR = TEXTALIGN_TOP | TEXTALIGN_RIGHT, |
| 34 | TEXTALIGN_ML = TEXTALIGN_MIDDLE | TEXTALIGN_LEFT, |
| 35 | TEXTALIGN_MC = TEXTALIGN_MIDDLE | TEXTALIGN_CENTER, |
| 36 | TEXTALIGN_MR = TEXTALIGN_MIDDLE | TEXTALIGN_RIGHT, |
| 37 | TEXTALIGN_BL = TEXTALIGN_BOTTOM | TEXTALIGN_LEFT, |
| 38 | TEXTALIGN_BC = TEXTALIGN_BOTTOM | TEXTALIGN_CENTER, |
| 39 | TEXTALIGN_BR = TEXTALIGN_BOTTOM | TEXTALIGN_RIGHT, |
| 40 | |
| 41 | TEXTALIGN_MASK_HORIZONTAL = TEXTALIGN_LEFT | TEXTALIGN_CENTER | TEXTALIGN_RIGHT, |
| 42 | TEXTALIGN_MASK_VERTICAL = TEXTALIGN_TOP | TEXTALIGN_MIDDLE | TEXTALIGN_BOTTOM, |
| 43 | }; |
| 44 | |
| 45 | enum ETextRenderFlags |
| 46 | { |
| 47 | TEXT_RENDER_FLAG_NO_X_BEARING = 1 << 0, |
| 48 | TEXT_RENDER_FLAG_NO_Y_BEARING = 1 << 1, |
| 49 | TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH = 1 << 2, |
| 50 | TEXT_RENDER_FLAG_NO_PIXEL_ALIGNMENT = 1 << 3, |
| 51 | TEXT_RENDER_FLAG_KERNING = 1 << 4, |
| 52 | TEXT_RENDER_FLAG_NO_OVERSIZE = 1 << 5, |
| 53 | TEXT_RENDER_FLAG_NO_FIRST_CHARACTER_X_BEARING = 1 << 6, |
| 54 | TEXT_RENDER_FLAG_NO_LAST_CHARACTER_ADVANCE = 1 << 7, |
| 55 | TEXT_RENDER_FLAG_NO_AUTOMATIC_QUAD_UPLOAD = 1 << 8, |
| 56 | // text is only rendered once and then discarded (a hint for buffer creation) |
| 57 | TEXT_RENDER_FLAG_ONE_TIME_USE = 1 << 9, |
| 58 | }; |
| 59 | |
| 60 | enum class EFontPreset |
| 61 | { |
| 62 | DEFAULT_FONT, |
| 63 | ICON_FONT, |
| 64 | }; |
| 65 | |
| 66 | enum ETextCursorSelectionMode |
| 67 | { |
| 68 | // ignore any kind of selection |
| 69 | TEXT_CURSOR_SELECTION_MODE_NONE = 0, |
| 70 | // calculates the selection based on the mouse press and release cursor position |
| 71 | TEXT_CURSOR_SELECTION_MODE_CALCULATE, |
| 72 | // sets the selection based on the character start and end count(these values have to be decoded character offsets) |
| 73 | TEXT_CURSOR_SELECTION_MODE_SET, |
| 74 | }; |
| 75 | |
| 76 | enum ETextCursorCursorMode |
| 77 | { |
| 78 | // ignore any kind of cursor |
| 79 | TEXT_CURSOR_CURSOR_MODE_NONE = 0, |
| 80 | // calculates the cursor based on the mouse release cursor position |
| 81 | TEXT_CURSOR_CURSOR_MODE_CALCULATE, |
| 82 | // sets the cursor based on the current character (this value has to be decoded character offset) |
| 83 | TEXT_CURSOR_CURSOR_MODE_SET, |
| 84 | }; |
| 85 | |
| 86 | struct STextBoundingBox |
| 87 | { |
| 88 | float m_X; |
| 89 | float m_Y; |
| 90 | float m_W; |
| 91 | float m_H; |
| 92 | |
| 93 | float Right() const { return m_X + m_W; } |
| 94 | float Bottom() const { return m_Y + m_H; } |
| 95 | vec2 Size() const { return vec2(m_W, m_H); } |
| 96 | void MoveBy(vec2 Offset) |
| 97 | { |
| 98 | m_X += Offset.x; |
| 99 | m_Y += Offset.y; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | // Allow to render multi colored text in one go without having to call TextEx() multiple times. |
| 104 | // Needed to allow multi colored multi line texts |
| 105 | struct STextColorSplit |
| 106 | { |
| 107 | int m_CharIndex; // Which index within the text should the split occur |
| 108 | int m_Length; // How long is the split |
| 109 | ColorRGBA m_Color; // The color the text should be starting from m_CharIndex |
| 110 | |
| 111 | STextColorSplit(int CharIndex, int Length, const ColorRGBA &Color) : |
| 112 | m_CharIndex(CharIndex), m_Length(Length), m_Color(Color) {} |
| 113 | }; |
| 114 | |
| 115 | class CTextCursor |
| 116 | { |
| 117 | public: |
| 118 | int m_Flags = TEXTFLAG_RENDER; |
| 119 | int m_LineCount = 1; |
| 120 | int m_GlyphCount = 0; |
| 121 | int m_CharCount = 0; |
| 122 | int m_MaxLines = 0; |
| 123 | |
| 124 | float m_StartX = 0.0f; |
| 125 | float m_StartY = 0.0f; |
| 126 | float m_LineWidth = -1.0f; |
| 127 | float m_X = 0.0f; |
| 128 | float m_Y = 0.0f; |
| 129 | float m_MaxCharacterHeight = 0.0f; |
| 130 | float m_LongestLineWidth = 0.0f; |
| 131 | |
| 132 | float m_FontSize = 0.0f; |
| 133 | float m_AlignedFontSize = 0.0f; |
| 134 | float m_LineSpacing = 0.0f; |
| 135 | float m_AlignedLineSpacing = 0.0f; |
| 136 | |
| 137 | ETextCursorSelectionMode m_CalculateSelectionMode = TEXT_CURSOR_SELECTION_MODE_NONE; |
| 138 | float m_SelectionHeightFactor = 1.0f; |
| 139 | |
| 140 | // these coordinates are respected if selection mode is set to calculate @see ETextCursorSelectionMode |
| 141 | vec2 m_PressMouse = vec2(0.0f, 0.0f); |
| 142 | // these coordinates are respected if selection/cursor mode is set to calculate @see ETextCursorSelectionMode / @see ETextCursorCursorMode |
| 143 | vec2 m_ReleaseMouse = vec2(0.0f, 0.0f); |
| 144 | |
| 145 | // note m_SelectionStart can be bigger than m_SelectionEnd, depending on how the mouse cursor was dragged |
| 146 | // also note, that these are the character offsets decoded |
| 147 | int m_SelectionStart = 0; |
| 148 | int m_SelectionEnd = 0; |
| 149 | |
| 150 | ETextCursorCursorMode m_CursorMode = TEXT_CURSOR_CURSOR_MODE_NONE; |
| 151 | bool m_ForceCursorRendering = false; |
| 152 | // note this is the decoded character offset |
| 153 | int m_CursorCharacter = -1; |
| 154 | vec2 m_CursorRenderedPosition = vec2(-1.0f, -1.0f); |
| 155 | |
| 156 | /** |
| 157 | * Whether the text was truncated with @link TEXTFLAG_STOP_AT_END @endlink or @link TEXTFLAG_ELLIPSIS_AT_END @endlink being set. |
| 158 | */ |
| 159 | bool m_Truncated = false; |
| 160 | |
| 161 | // Color splits of the cursor to allow multicolored text |
| 162 | std::vector<STextColorSplit> m_vColorSplits; |
| 163 | |
| 164 | float Height() const; |
| 165 | STextBoundingBox BoundingBox() const; |
| 166 | void SetPosition(vec2 Position); |
| 167 | }; |
| 168 | |
| 169 | struct STextContainerUsages |
| 170 | { |
| 171 | int m_Dummy = 0; |
| 172 | }; |
| 173 | |
| 174 | struct STextContainerIndex |
| 175 | { |
| 176 | int m_Index; |
| 177 | std::shared_ptr<STextContainerUsages> m_UseCount = |
| 178 | std::make_shared<STextContainerUsages>(args: STextContainerUsages()); |
| 179 | |
| 180 | STextContainerIndex() { Reset(); } |
| 181 | bool Valid() const { return m_Index >= 0; } |
| 182 | void Reset() { m_Index = -1; } |
| 183 | }; |
| 184 | |
| 185 | struct STextSizeProperties |
| 186 | { |
| 187 | float *m_pHeight = nullptr; |
| 188 | float *m_pAlignedFontSize = nullptr; |
| 189 | float *m_pMaxCharacterHeightInLine = nullptr; |
| 190 | int *m_pLineCount = nullptr; |
| 191 | }; |
| 192 | |
| 193 | class ITextRender : public IInterface |
| 194 | { |
| 195 | MACRO_INTERFACE("textrender" ) |
| 196 | public: |
| 197 | virtual bool LoadFonts() = 0; |
| 198 | virtual void SetFontPreset(EFontPreset FontPreset) = 0; |
| 199 | virtual void SetFontLanguageVariant(const char *pLanguageFile) = 0; |
| 200 | |
| 201 | virtual void SetRenderFlags(unsigned Flags) = 0; |
| 202 | virtual unsigned GetRenderFlags() const = 0; |
| 203 | |
| 204 | ColorRGBA DefaultTextColor() const { return ColorRGBA(1, 1, 1, 1); } |
| 205 | ColorRGBA DefaultTextOutlineColor() const { return ColorRGBA(0, 0, 0, 0.3f); } |
| 206 | ColorRGBA DefaultTextSelectionColor() const { return ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f); } |
| 207 | |
| 208 | // |
| 209 | virtual void TextEx(CTextCursor *pCursor, const char *pText, int Length = -1) = 0; |
| 210 | virtual bool CreateTextContainer(STextContainerIndex &TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0; |
| 211 | virtual void AppendTextContainer(STextContainerIndex TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0; |
| 212 | // either creates a new text container or appends to a existing one |
| 213 | virtual bool CreateOrAppendTextContainer(STextContainerIndex &TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0; |
| 214 | // just deletes and creates text container |
| 215 | virtual void RecreateTextContainer(STextContainerIndex &TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0; |
| 216 | virtual void RecreateTextContainerSoft(STextContainerIndex &TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0; |
| 217 | virtual void DeleteTextContainer(STextContainerIndex &TextContainerIndex) = 0; |
| 218 | |
| 219 | virtual void UploadTextContainer(STextContainerIndex TextContainerIndex) = 0; |
| 220 | |
| 221 | virtual void RenderTextContainer(STextContainerIndex TextContainerIndex, const ColorRGBA &TextColor, const ColorRGBA &TextOutlineColor) = 0; |
| 222 | virtual void RenderTextContainer(STextContainerIndex TextContainerIndex, const ColorRGBA &TextColor, const ColorRGBA &TextOutlineColor, float X, float Y) = 0; |
| 223 | |
| 224 | virtual STextBoundingBox GetBoundingBoxTextContainer(STextContainerIndex TextContainerIndex) = 0; |
| 225 | |
| 226 | virtual void UploadEntityLayerText(const CImageInfo &TextImage, int TexSubWidth, int TexSubHeight, const char *pText, int Length, float x, float y, int FontSize) = 0; |
| 227 | virtual int AdjustFontSize(const char *pText, int TextLength, int MaxSize, int MaxWidth) const = 0; |
| 228 | virtual float GetGlyphOffsetX(int FontSize, char TextCharacter) const = 0; |
| 229 | virtual int CalculateTextWidth(const char *pText, int TextLength, int FontWidth, int FontSize) const = 0; |
| 230 | |
| 231 | // old foolish interface |
| 232 | virtual void TextColor(float r, float g, float b, float a) = 0; |
| 233 | virtual void TextColor(ColorRGBA Color) = 0; |
| 234 | virtual void TextOutlineColor(float r, float g, float b, float a) = 0; |
| 235 | virtual void TextOutlineColor(ColorRGBA Color) = 0; |
| 236 | virtual void TextSelectionColor(float r, float g, float b, float a) = 0; |
| 237 | virtual void TextSelectionColor(ColorRGBA Color) = 0; |
| 238 | virtual void Text(float x, float y, float Size, const char *pText, float LineWidth = -1.0f) = 0; |
| 239 | virtual float TextWidth(float Size, const char *pText, int StrLength = -1, float LineWidth = -1.0f, int Flags = 0, const STextSizeProperties &TextSizeProps = {}) = 0; |
| 240 | virtual STextBoundingBox TextBoundingBox(float Size, const char *pText, int StrLength = -1, float LineWidth = -1.0f, float LineSpacing = 0.0f, int Flags = 0) = 0; |
| 241 | |
| 242 | virtual ColorRGBA GetTextColor() const = 0; |
| 243 | virtual ColorRGBA GetTextOutlineColor() const = 0; |
| 244 | virtual ColorRGBA GetTextSelectionColor() const = 0; |
| 245 | |
| 246 | virtual void OnPreWindowResize() = 0; |
| 247 | virtual void OnWindowResize() = 0; |
| 248 | }; |
| 249 | |
| 250 | class IEngineTextRender : public ITextRender |
| 251 | { |
| 252 | MACRO_INTERFACE("enginetextrender" ) |
| 253 | public: |
| 254 | virtual void Init() = 0; |
| 255 | void Shutdown() override = 0; |
| 256 | }; |
| 257 | |
| 258 | extern IEngineTextRender *CreateEngineTextRender(); |
| 259 | |
| 260 | #endif |
| 261 | |