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
14enum
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
22enum 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
45enum 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
60enum class EFontPreset
61{
62 DEFAULT_FONT,
63 ICON_FONT,
64};
65
66namespace FontIcons
67{
68 // Each font icon is named according to its official name in Font Awesome
69 [[maybe_unused]] static const char *FONT_ICON_PLUS = "+";
70 [[maybe_unused]] static const char *FONT_ICON_MINUS = "-";
71 [[maybe_unused]] static const char *FONT_ICON_LOCK = "\xEF\x80\xA3";
72 [[maybe_unused]] static const char *FONT_ICON_MAGNIFYING_GLASS = "\xEF\x80\x82";
73 [[maybe_unused]] static const char *FONT_ICON_HEART = "\xEF\x80\x84";
74 [[maybe_unused]] static const char *FONT_ICON_HEART_CRACK = "\xEF\x9E\xA9";
75 [[maybe_unused]] static const char *FONT_ICON_STAR = "\xEF\x80\x85";
76 [[maybe_unused]] static const char *FONT_ICON_XMARK = "\xEF\x80\x8D";
77 [[maybe_unused]] static const char *FONT_ICON_CIRCLE = "\xEF\x84\x91";
78 [[maybe_unused]] static const char *FONT_ICON_ARROW_ROTATE_LEFT = "\xEF\x83\xA2";
79 [[maybe_unused]] static const char *FONT_ICON_ARROW_ROTATE_RIGHT = "\xEF\x80\x9E";
80 [[maybe_unused]] static const char *FONT_ICON_FLAG_CHECKERED = "\xEF\x84\x9E";
81 [[maybe_unused]] static const char *FONT_ICON_BAN = "\xEF\x81\x9E";
82 [[maybe_unused]] static const char *FONT_ICON_CIRCLE_CHEVRON_DOWN = "\xEF\x84\xBA";
83 [[maybe_unused]] static const char *FONT_ICON_KEY = "\xEF\x82\x84";
84 [[maybe_unused]] static const char *FONT_ICON_SQUARE_MINUS = "\xEF\x85\x86";
85 [[maybe_unused]] static const char *FONT_ICON_SQUARE_PLUS = "\xEF\x83\xBE";
86 [[maybe_unused]] static const char *FONT_ICON_SORT_UP = "\xEF\x83\x9E";
87 [[maybe_unused]] static const char *FONT_ICON_SORT_DOWN = "\xEF\x83\x9D";
88 [[maybe_unused]] static const char *FONT_ICON_TRIANGLE_EXCLAMATION = "\xEF\x81\xB1";
89
90 [[maybe_unused]] static const char *FONT_ICON_HOUSE = "\xEF\x80\x95";
91 [[maybe_unused]] static const char *FONT_ICON_BOOKMARK = "\xEF\x80\xAE";
92 [[maybe_unused]] static const char *FONT_ICON_NEWSPAPER = "\xEF\x87\xAA";
93 [[maybe_unused]] static const char *FONT_ICON_POWER_OFF = "\xEF\x80\x91";
94 [[maybe_unused]] static const char *FONT_ICON_GEAR = "\xEF\x80\x93";
95 [[maybe_unused]] static const char *FONT_ICON_PEN_TO_SQUARE = "\xEF\x81\x84";
96 [[maybe_unused]] static const char *FONT_ICON_CLAPPERBOARD = "\xEE\x84\xB1";
97 [[maybe_unused]] static const char *FONT_ICON_EARTH_AMERICAS = "\xEF\x95\xBD";
98 [[maybe_unused]] static const char *FONT_ICON_NETWORK_WIRED = "\xEF\x9B\xBF";
99 [[maybe_unused]] static const char *FONT_ICON_LIST_UL = "\xEF\x83\x8A";
100 [[maybe_unused]] static const char *FONT_ICON_INFO = "\xEF\x84\xA9";
101 [[maybe_unused]] static const char *FONT_ICON_TERMINAL = "\xEF\x84\xA0";
102
103 [[maybe_unused]] static const char *FONT_ICON_SLASH = "\xEF\x9C\x95";
104 [[maybe_unused]] static const char *FONT_ICON_PLAY = "\xEF\x81\x8B";
105 [[maybe_unused]] static const char *FONT_ICON_PAUSE = "\xEF\x81\x8C";
106 [[maybe_unused]] static const char *FONT_ICON_STOP = "\xEF\x81\x8D";
107 [[maybe_unused]] static const char *FONT_ICON_CHEVRON_LEFT = "\xEF\x81\x93";
108 [[maybe_unused]] static const char *FONT_ICON_CHEVRON_RIGHT = "\xEF\x81\x94";
109 [[maybe_unused]] static const char *FONT_ICON_CHEVRON_UP = "\xEF\x81\xB7";
110 [[maybe_unused]] static const char *FONT_ICON_CHEVRON_DOWN = "\xEF\x81\xB8";
111 [[maybe_unused]] static const char *FONT_ICON_BACKWARD = "\xEF\x81\x8A";
112 [[maybe_unused]] static const char *FONT_ICON_FORWARD = "\xEF\x81\x8E";
113 [[maybe_unused]] static const char *FONT_ICON_RIGHT_FROM_BRACKET = "\xEF\x8B\xB5";
114 [[maybe_unused]] static const char *FONT_ICON_RIGHT_TO_BRACKET = "\xEF\x8B\xB6";
115 [[maybe_unused]] static const char *FONT_ICON_ARROW_UP_RIGHT_FROM_SQUARE = "\xEF\x82\x8E";
116 [[maybe_unused]] static const char *FONT_ICON_BACKWARD_STEP = "\xEF\x81\x88";
117 [[maybe_unused]] static const char *FONT_ICON_FORWARD_STEP = "\xEF\x81\x91";
118 [[maybe_unused]] static const char *FONT_ICON_BACKWARD_FAST = "\xEF\x81\x89";
119 [[maybe_unused]] static const char *FONT_ICON_FORWARD_FAST = "\xEF\x81\x90";
120 [[maybe_unused]] static const char *FONT_ICON_KEYBOARD = "\xE2\x8C\xA8";
121 [[maybe_unused]] static const char *FONT_ICON_ELLIPSIS = "\xEF\x85\x81";
122
123 [[maybe_unused]] static const char *FONT_ICON_FOLDER = "\xEF\x81\xBB";
124 [[maybe_unused]] static const char *FONT_ICON_FOLDER_OPEN = "\xEF\x81\xBC";
125 [[maybe_unused]] static const char *FONT_ICON_FOLDER_TREE = "\xEF\xA0\x82";
126 [[maybe_unused]] static const char *FONT_ICON_FILM = "\xEF\x80\x88";
127 [[maybe_unused]] static const char *FONT_ICON_VIDEO = "\xEF\x80\xBD";
128 [[maybe_unused]] static const char *FONT_ICON_MAP = "\xEF\x89\xB9";
129 [[maybe_unused]] static const char *FONT_ICON_IMAGE = "\xEF\x80\xBE";
130 [[maybe_unused]] static const char *FONT_ICON_MUSIC = "\xEF\x80\x81";
131 [[maybe_unused]] static const char *FONT_ICON_FILE = "\xEF\x85\x9B";
132
133 [[maybe_unused]] static const char *FONT_ICON_PENCIL = "\xEF\x8C\x83";
134 [[maybe_unused]] static const char *FONT_ICON_TRASH = "\xEF\x87\xB8";
135
136 [[maybe_unused]] static const char *FONT_ICON_ARROWS_LEFT_RIGHT = "\xEF\x8C\xB7";
137 [[maybe_unused]] static const char *FONT_ICON_ARROWS_UP_DOWN = "\xEF\x81\xBD";
138 [[maybe_unused]] static const char *FONT_ICON_CIRCLE_PLAY = "\xEF\x85\x84";
139 [[maybe_unused]] static const char *FONT_ICON_BORDER_ALL = "\xEF\xA1\x8C";
140 [[maybe_unused]] static const char *FONT_ICON_EYE = "\xEF\x81\xAE";
141 [[maybe_unused]] static const char *FONT_ICON_EYE_SLASH = "\xEF\x81\xB0";
142 [[maybe_unused]] static const char *FONT_ICON_EYE_DROPPER = "\xEF\x87\xBB";
143 [[maybe_unused]] static const char *FONT_ICON_COMMENT = "\xEF\x81\xB5";
144 [[maybe_unused]] static const char *FONT_ICON_COMMENT_SLASH = "\xEF\x92\xB3";
145
146 [[maybe_unused]] static const char *FONT_ICON_DICE_ONE = "\xEF\x94\xA5";
147 [[maybe_unused]] static const char *FONT_ICON_DICE_TWO = "\xEF\x94\xA8";
148 [[maybe_unused]] static const char *FONT_ICON_DICE_THREE = "\xEF\x94\xA7";
149 [[maybe_unused]] static const char *FONT_ICON_DICE_FOUR = "\xEF\x94\xA4";
150 [[maybe_unused]] static const char *FONT_ICON_DICE_FIVE = "\xEF\x94\xA3";
151 [[maybe_unused]] static const char *FONT_ICON_DICE_SIX = "\xEF\x94\xA6";
152
153 [[maybe_unused]] static const char *FONT_ICON_LAYER_GROUP = "\xEF\x97\xBD";
154 [[maybe_unused]] static const char *FONT_ICON_UNDO = "\xEF\x8B\xAA";
155 [[maybe_unused]] static const char *FONT_ICON_REDO = "\xEF\x8B\xB9";
156
157 [[maybe_unused]] static const char *FONT_ICON_ARROWS_ROTATE = "\xEF\x80\xA1";
158 [[maybe_unused]] static const char *FONT_ICON_QUESTION = "?";
159
160 [[maybe_unused]] static const char *FONT_ICON_CAMERA = "\xEF\x80\xB0";
161}
162
163enum ETextCursorSelectionMode
164{
165 // ignore any kind of selection
166 TEXT_CURSOR_SELECTION_MODE_NONE = 0,
167 // calculates the selection based on the mouse press and release cursor position
168 TEXT_CURSOR_SELECTION_MODE_CALCULATE,
169 // sets the selection based on the character start and end count(these values have to be decoded character offsets)
170 TEXT_CURSOR_SELECTION_MODE_SET,
171};
172
173enum ETextCursorCursorMode
174{
175 // ignore any kind of cursor
176 TEXT_CURSOR_CURSOR_MODE_NONE = 0,
177 // calculates the cursor based on the mouse release cursor position
178 TEXT_CURSOR_CURSOR_MODE_CALCULATE,
179 // sets the cursor based on the current character (this value has to be decoded character offset)
180 TEXT_CURSOR_CURSOR_MODE_SET,
181};
182
183struct STextBoundingBox
184{
185 float m_X;
186 float m_Y;
187 float m_W;
188 float m_H;
189
190 float Right() const { return m_X + m_W; }
191 float Bottom() const { return m_Y + m_H; }
192 vec2 Size() const { return vec2(m_W, m_H); }
193 void MoveBy(vec2 Offset)
194 {
195 m_X += Offset.x;
196 m_Y += Offset.y;
197 }
198};
199
200// Allow to render multi colored text in one go without having to call TextEx() multiple times.
201// Needed to allow multi colored multi line texts
202struct STextColorSplit
203{
204 int m_CharIndex; // Which index within the text should the split occur
205 int m_Length; // How long is the split
206 ColorRGBA m_Color; // The color the text should be starting from m_CharIndex
207
208 STextColorSplit(int CharIndex, int Length, const ColorRGBA &Color) :
209 m_CharIndex(CharIndex), m_Length(Length), m_Color(Color) {}
210};
211
212class CTextCursor
213{
214public:
215 int m_Flags = TEXTFLAG_RENDER;
216 int m_LineCount = 1;
217 int m_GlyphCount = 0;
218 int m_CharCount = 0;
219 int m_MaxLines = 0;
220
221 float m_StartX = 0.0f;
222 float m_StartY = 0.0f;
223 float m_LineWidth = -1.0f;
224 float m_X = 0.0f;
225 float m_Y = 0.0f;
226 float m_MaxCharacterHeight = 0.0f;
227 float m_LongestLineWidth = 0.0f;
228
229 float m_FontSize = 0.0f;
230 float m_AlignedFontSize = 0.0f;
231 float m_LineSpacing = 0.0f;
232 float m_AlignedLineSpacing = 0.0f;
233
234 ETextCursorSelectionMode m_CalculateSelectionMode = TEXT_CURSOR_SELECTION_MODE_NONE;
235 float m_SelectionHeightFactor = 1.0f;
236
237 // these coordinates are respected if selection mode is set to calculate @see ETextCursorSelectionMode
238 vec2 m_PressMouse = vec2(0.0f, 0.0f);
239 // these coordinates are respected if selection/cursor mode is set to calculate @see ETextCursorSelectionMode / @see ETextCursorCursorMode
240 vec2 m_ReleaseMouse = vec2(0.0f, 0.0f);
241
242 // note m_SelectionStart can be bigger than m_SelectionEnd, depending on how the mouse cursor was dragged
243 // also note, that these are the character offsets decoded
244 int m_SelectionStart = 0;
245 int m_SelectionEnd = 0;
246
247 ETextCursorCursorMode m_CursorMode = TEXT_CURSOR_CURSOR_MODE_NONE;
248 bool m_ForceCursorRendering = false;
249 // note this is the decoded character offset
250 int m_CursorCharacter = -1;
251 vec2 m_CursorRenderedPosition = vec2(-1.0f, -1.0f);
252
253 /**
254 * Whether the text was truncated with @link TEXTFLAG_STOP_AT_END @endlink or @link TEXTFLAG_ELLIPSIS_AT_END @endlink being set.
255 */
256 bool m_Truncated = false;
257
258 // Color splits of the cursor to allow multicolored text
259 std::vector<STextColorSplit> m_vColorSplits;
260
261 float Height() const;
262 STextBoundingBox BoundingBox() const;
263 void SetPosition(vec2 Position);
264};
265
266struct STextContainerUsages
267{
268 int m_Dummy = 0;
269};
270
271struct STextContainerIndex
272{
273 int m_Index;
274 std::shared_ptr<STextContainerUsages> m_UseCount =
275 std::make_shared<STextContainerUsages>(args: STextContainerUsages());
276
277 STextContainerIndex() { Reset(); }
278 bool Valid() const { return m_Index >= 0; }
279 void Reset() { m_Index = -1; }
280};
281
282struct STextSizeProperties
283{
284 float *m_pHeight = nullptr;
285 float *m_pAlignedFontSize = nullptr;
286 float *m_pMaxCharacterHeightInLine = nullptr;
287 int *m_pLineCount = nullptr;
288};
289
290class ITextRender : public IInterface
291{
292 MACRO_INTERFACE("textrender")
293public:
294 virtual bool LoadFonts() = 0;
295 virtual void SetFontPreset(EFontPreset FontPreset) = 0;
296 virtual void SetFontLanguageVariant(const char *pLanguageFile) = 0;
297
298 virtual void SetRenderFlags(unsigned Flags) = 0;
299 virtual unsigned GetRenderFlags() const = 0;
300
301 ColorRGBA DefaultTextColor() const { return ColorRGBA(1, 1, 1, 1); }
302 ColorRGBA DefaultTextOutlineColor() const { return ColorRGBA(0, 0, 0, 0.3f); }
303 ColorRGBA DefaultTextSelectionColor() const { return ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f); }
304
305 //
306 virtual void TextEx(CTextCursor *pCursor, const char *pText, int Length = -1) = 0;
307 virtual bool CreateTextContainer(STextContainerIndex &TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0;
308 virtual void AppendTextContainer(STextContainerIndex TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0;
309 // either creates a new text container or appends to a existing one
310 virtual bool CreateOrAppendTextContainer(STextContainerIndex &TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0;
311 // just deletes and creates text container
312 virtual void RecreateTextContainer(STextContainerIndex &TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0;
313 virtual void RecreateTextContainerSoft(STextContainerIndex &TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0;
314 virtual void DeleteTextContainer(STextContainerIndex &TextContainerIndex) = 0;
315
316 virtual void UploadTextContainer(STextContainerIndex TextContainerIndex) = 0;
317
318 virtual void RenderTextContainer(STextContainerIndex TextContainerIndex, const ColorRGBA &TextColor, const ColorRGBA &TextOutlineColor) = 0;
319 virtual void RenderTextContainer(STextContainerIndex TextContainerIndex, const ColorRGBA &TextColor, const ColorRGBA &TextOutlineColor, float X, float Y) = 0;
320
321 virtual STextBoundingBox GetBoundingBoxTextContainer(STextContainerIndex TextContainerIndex) = 0;
322
323 virtual void UploadEntityLayerText(const CImageInfo &TextImage, int TexSubWidth, int TexSubHeight, const char *pText, int Length, float x, float y, int FontSize) = 0;
324 virtual int AdjustFontSize(const char *pText, int TextLength, int MaxSize, int MaxWidth) const = 0;
325 virtual float GetGlyphOffsetX(int FontSize, char TextCharacter) const = 0;
326 virtual int CalculateTextWidth(const char *pText, int TextLength, int FontWidth, int FontSize) const = 0;
327
328 // old foolish interface
329 virtual void TextColor(float r, float g, float b, float a) = 0;
330 virtual void TextColor(ColorRGBA Color) = 0;
331 virtual void TextOutlineColor(float r, float g, float b, float a) = 0;
332 virtual void TextOutlineColor(ColorRGBA Color) = 0;
333 virtual void TextSelectionColor(float r, float g, float b, float a) = 0;
334 virtual void TextSelectionColor(ColorRGBA Color) = 0;
335 virtual void Text(float x, float y, float Size, const char *pText, float LineWidth = -1.0f) = 0;
336 virtual float TextWidth(float Size, const char *pText, int StrLength = -1, float LineWidth = -1.0f, int Flags = 0, const STextSizeProperties &TextSizeProps = {}) = 0;
337 virtual STextBoundingBox TextBoundingBox(float Size, const char *pText, int StrLength = -1, float LineWidth = -1.0f, float LineSpacing = 0.0f, int Flags = 0) = 0;
338
339 virtual ColorRGBA GetTextColor() const = 0;
340 virtual ColorRGBA GetTextOutlineColor() const = 0;
341 virtual ColorRGBA GetTextSelectionColor() const = 0;
342
343 virtual void OnPreWindowResize() = 0;
344 virtual void OnWindowResize() = 0;
345};
346
347class IEngineTextRender : public ITextRender
348{
349 MACRO_INTERFACE("enginetextrender")
350public:
351 virtual void Init() = 0;
352 void Shutdown() override = 0;
353};
354
355extern IEngineTextRender *CreateEngineTextRender();
356
357#endif
358