Colobot
text.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 
28 #include "graphics/core/color.h"
29 
30 #include "math/point.h"
31 
32 #include <map>
33 #include <memory>
34 #include <vector>
35 
36 
37 // Graphics module namespace
38 namespace Gfx
39 {
40 
41 class CEngine;
42 class CDevice;
43 
45 const float FONT_SIZE_SMALL = 12.0f;
47 const float FONT_SIZE_BIG = 18.0f;
48 
54 {
55  TEXT_ALIGN_RIGHT,
56  TEXT_ALIGN_LEFT,
57  TEXT_ALIGN_CENTER
58 };
59 
60 /* Font meta char constants */
61 
63 typedef short FontMetaChar;
64 
72 {
74  FONT_BOLD = 0x04,
76  FONT_ITALIC = 0x08,
77 
79  FONT_COLOBOT = 0x00,
84 
86  FONT_COURIER = 0x01,
89 
90  // 0x02 left for possible another font
91 
93  FONT_BUTTON = 0x03,
94 };
95 
105 {
106  FONT_TITLE_BIG = 0x01 << 4,
107  FONT_TITLE_NORM = 0x02 << 4,
108  FONT_TITLE_LITTLE = 0x03 << 4,
109 };
110 
118 {
119  FONT_HIGHLIGHT_NONE = 0x00 << 6,
120  FONT_HIGHLIGHT_LINK = 0x01 << 6,
121  FONT_HIGHLIGHT_TABLE = 0x02 << 6,
122  FONT_HIGHLIGHT_KEY = 0x03 << 6,
123  FONT_HIGHLIGHT_TOKEN = 0x04 << 6,
124  FONT_HIGHLIGHT_TYPE = 0x05 << 6,
125  FONT_HIGHLIGHT_CONST = 0x06 << 6,
126  FONT_HIGHLIGHT_THIS = 0x07 << 6,
129  FONT_HIGHLIGHT_STRING = 0x0A << 6,
130 };
131 
137 {
139  FONT_MASK_FONT = 0x00f,
146 };
147 
148 
155 struct UTF8Char
156 {
157  char c1, c2, c3;
158  // Padding for 4-byte alignment
159  // It also seems to fix some problems reported by valgrind
160  char pad;
161 
162  explicit UTF8Char(char ch1 = '\0', char ch2 = '\0', char ch3 = '\0')
163  : c1(ch1), c2(ch2), c3(ch3), pad('\0') {}
164 
165  inline bool operator<(const UTF8Char &other) const
166  {
167  if (c1 < other.c1)
168  return true;
169  else if (c1 > other.c1)
170  return false;
171 
172  if (c2 < other.c2)
173  return true;
174  else if (c2 > other.c2)
175  return false;
176 
177  return c3 < other.c3;
178  }
179 
180  inline bool operator==(const UTF8Char &other) const
181  {
182  return c1 == other.c1 && c2 == other.c2 && c3 == other.c3;
183  }
184 };
185 
191 {
192  unsigned int id = 0;
193  Math::Point texSize;
194  Math::Point charSize;
195 };
196 
197 // Definition is private - in text.cpp
198 struct CachedFont;
199 struct MultisizeFont;
200 
206 {
207  CHAR_TAB = '\t',
208  CHAR_NEWLINE = '\n',
209  CHAR_DOT = 1,
213 };
214 
230 class CText
231 {
232 public:
233  CText(CEngine* engine);
234  virtual ~CText();
235 
237  void SetDevice(CDevice *device);
238 
240  std::string GetError();
241 
243  bool Create();
245  void Destroy();
246 
248  void FlushCache();
249 
251  void SetTabSize(int tabSize);
253  int GetTabSize();
255 
257  void DrawText(const std::string &text, std::vector<FontMetaChar>::iterator format,
258  std::vector<FontMetaChar>::iterator end,
259  float size, Math::Point pos, float width, TextAlign align,
260  int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f));
262  void DrawText(const std::string &text, FontType font,
263  float size, Math::Point pos, float width, TextAlign align,
264  int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f));
265 
267  void SizeText(const std::string &text, std::vector<FontMetaChar>::iterator format,
268  std::vector<FontMetaChar>::iterator endFormat,
269  float size, Math::Point pos, TextAlign align,
270  Math::Point &start, Math::Point &end);
272  void SizeText(const std::string &text, FontType font,
273  float size, Math::Point pos, TextAlign align,
274  Math::Point &start, Math::Point &end);
275 
277  float GetAscent(FontType font, float size);
279  float GetDescent(FontType font, float size);
281  float GetHeight(FontType font, float size);
282 
284  TEST_VIRTUAL float GetStringWidth(const std::string& text,
285  std::vector<FontMetaChar>::iterator format,
286  std::vector<FontMetaChar>::iterator end, float size);
288  TEST_VIRTUAL float GetStringWidth(std::string text, FontType font, float size);
290  TEST_VIRTUAL float GetCharWidth(UTF8Char ch, FontType font, float size, float offset);
291 
293  int Justify(const std::string &text, std::vector<FontMetaChar>::iterator format,
294  std::vector<FontMetaChar>::iterator end,
295  float size, float width);
297  int Justify(const std::string &text, FontType font, float size, float width);
298 
300  int Detect(const std::string &text, std::vector<FontMetaChar>::iterator format,
301  std::vector<FontMetaChar>::iterator end,
302  float size, float offset);
304  int Detect(const std::string &text, FontType font, float size, float offset);
305 
306  UTF8Char TranslateSpecialChar(int specialChar);
307 
308  CharTexture GetCharTexture(UTF8Char ch, FontType font, float size);
309 
310 protected:
311  CachedFont* GetOrOpenFont(FontType type, float size);
312  CharTexture CreateCharTexture(UTF8Char ch, CachedFont* font);
313 
314  void DrawString(const std::string &text, std::vector<FontMetaChar>::iterator format,
315  std::vector<FontMetaChar>::iterator end,
316  float size, Math::Point pos, float width, int eol, Color color);
317  void DrawString(const std::string &text, FontType font,
318  float size, Math::Point pos, float width, int eol, Color color);
319  void DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size);
320  void DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos, Color color);
321  void StringToUTFCharList(const std::string &text, std::vector<UTF8Char> &chars);
322  void StringToUTFCharList(const std::string &text, std::vector<UTF8Char> &chars, std::vector<FontMetaChar>::iterator format, std::vector<FontMetaChar>::iterator end);
323 
324 protected:
325  CEngine* m_engine;
326  CDevice* m_device;
327 
328  std::string m_error;
329  float m_defaultSize;
330  int m_tabSize;
331 
332  std::map<FontType, std::unique_ptr<MultisizeFont>> m_fonts;
333 
334  FontType m_lastFontType;
335  int m_lastFontSize;
336  CachedFont* m_lastCachedFont;
337 };
338 
339 
340 } // namespace Gfx
TEST_VIRTUAL float GetStringWidth(const std::string &text, std::vector< FontMetaChar >::iterator format, std::vector< FontMetaChar >::iterator end, float size)
Returns width of string (multi-format)
Definition: text.cpp:313
Font with multiple possible sizes.
Definition: text.cpp:47
void SetDevice(CDevice *device)
Sets the device to be used.
Definition: text.cpp:143
Base TTF font with UTF-8 char cache.
Definition: text.cpp:61
Flag for bold font subtype.
Definition: text.h:74
Mask for FontType.
Definition: text.h:139
FontMask
Masks in FontMetaChar for different attributes.
Definition: text.h:136
TEST_VIRTUAL float GetCharWidth(UTF8Char ch, FontType font, float size, float offset)
Returns width of single character.
Definition: text.cpp:364
Texture of font character.
Definition: text.h:190
types in CBot scripts
Definition: text.h:124
Point struct and related functions.
SpecialChar
Special codes for certain characters.
Definition: text.h:205
string literals in CBot scripts
Definition: text.h:129
void Destroy()
Frees resources before exit.
Definition: text.cpp:132
code background in SatCom
Definition: text.h:121
Newline character - arrow pointing down and left.
Definition: text.h:209
FontHighlight
Type of color highlight for text.
Definition: text.h:117
Alias for bold courier font.
Definition: text.h:88
Tab character - :
Definition: text.h:208
builtin keywords in CBot scripts
Definition: text.h:128
short FontMetaChar
Type used for font character metainfo.
Definition: text.h:63
float GetDescent(FontType font, float size)
Returns the descent font metric.
Definition: text.cpp:288
Courier (monospace) font used mainly in code editor (only regular & bold)
Definition: text.h:86
Flag for italic font subtype.
Definition: text.h:76
keywords in CBot scripts
Definition: text.h:123
const float FONT_SIZE_BIG
Standard big font size.
Definition: text.h:47
Square.
Definition: text.h:211
float GetAscent(FontType font, float size)
Returns the ascent font metric.
Definition: text.cpp:276
bool Create()
Initializes the font engine; must be called after SetDevice()
Definition: text.cpp:106
Alias for bold colobot font.
Definition: text.h:81
Color structs and related functions.
Mask for FontHighlight.
Definition: text.h:143
UTF-8 character in font cache.
Definition: text.h:155
"this" keyword in CBot scripts
Definition: text.h:126
Pseudo-font loaded from textures for buttons, icons, etc.
Definition: text.h:93
int Detect(const std::string &text, std::vector< FontMetaChar >::iterator format, std::vector< FontMetaChar >::iterator end, float size, float offset)
Returns the most suitable position to a given offset (multi-format)
Definition: text.cpp:492
2D point
Definition: point.h:50
comments in CBot scripts
Definition: text.h:127
constants in CBot scripts
Definition: text.h:125
Namespace for (new) graphics code.
Definition: app.h:49
Filled triangle pointing right.
Definition: text.h:212
The graphics engine.
Definition: engine.h:620
background for keys in documentation in SatCom
Definition: text.h:122
Single dot in the middle.
Definition: text.h:210
float GetHeight(FontType font, float size)
Returns the height font metric.
Definition: text.cpp:300
void DrawText(const std::string &text, std::vector< FontMetaChar >::iterator format, std::vector< FontMetaChar >::iterator end, float size, Math::Point pos, float width, TextAlign align, int eol, Color color=Color(0.0f, 0.0f, 0.0f, 1.0f))
Draws text (multi-format)
Definition: text.cpp:184
TextAlign
Type of text alignment.
Definition: text.h:53
Text rendering engine.
Definition: text.h:230
int Justify(const std::string &text, std::vector< FontMetaChar >::iterator format, std::vector< FontMetaChar >::iterator end, float size, float width)
Justifies a line of text (multi-format)
Definition: text.cpp:407
const float FONT_SIZE_SMALL
Standard small font size.
Definition: text.h:45
RGBA color.
Definition: color.h:39
void SetTabSize(int tabSize)
Tab size management.
Definition: text.cpp:179
Mask for image bit (TODO: not used?)
Definition: text.h:145
std::string GetError()
Returns the last encountered error.
Definition: text.cpp:148
Mask for FontTitle.
Definition: text.h:141
FontTitle
Size of font title.
Definition: text.h:104
Alias for italic colobot font.
Definition: text.h:83
FontType
Type of font.
Definition: text.h:71
void SizeText(const std::string &text, std::vector< FontMetaChar >::iterator format, std::vector< FontMetaChar >::iterator endFormat, float size, Math::Point pos, TextAlign align, Math::Point &start, Math::Point &end)
Calculates dimensions for text (multi-format)
Definition: text.cpp:229
Default colobot font used for interface.
Definition: text.h:79
Abstract interface of graphics device.
Definition: device.h:268
void FlushCache()
Flushes cached textures.
Definition: text.cpp:153
link underline
Definition: text.h:120