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-2016, 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/intpoint.h"
31 #include "math/point.h"
32 
33 #include <map>
34 #include <memory>
35 #include <vector>
36 
37 
38 // Graphics module namespace
39 namespace Gfx
40 {
41 
42 class CEngine;
43 class CDevice;
44 
46 const float FONT_SIZE_SMALL = 12.0f;
48 const float FONT_SIZE_BIG = 18.0f;
49 
55 {
56  TEXT_ALIGN_RIGHT,
57  TEXT_ALIGN_LEFT,
58  TEXT_ALIGN_CENTER
59 };
60 
61 /* Font meta char constants */
62 
64 typedef short FontMetaChar;
65 
73 {
75  FONT_BOLD = 0x04,
77  FONT_ITALIC = 0x08,
78 
80  FONT_COLOBOT = 0x00,
85 
87  FONT_COURIER = 0x01,
90 
91  // 0x02 left for possible another font
92 
94  FONT_BUTTON = 0x03,
95 };
96 
106 {
107  FONT_TITLE_BIG = 0x01 << 4,
108  FONT_TITLE_NORM = 0x02 << 4,
109  FONT_TITLE_LITTLE = 0x03 << 4,
110 };
111 
119 {
120  FONT_HIGHLIGHT_NONE = 0x00 << 6,
121  FONT_HIGHLIGHT_LINK = 0x01 << 6,
122  FONT_HIGHLIGHT_TABLE = 0x02 << 6,
123  FONT_HIGHLIGHT_KEY = 0x03 << 6,
124  FONT_HIGHLIGHT_TOKEN = 0x04 << 6,
125  FONT_HIGHLIGHT_TYPE = 0x05 << 6,
126  FONT_HIGHLIGHT_CONST = 0x06 << 6,
127  FONT_HIGHLIGHT_THIS = 0x07 << 6,
130  FONT_HIGHLIGHT_STRING = 0x0A << 6,
131 };
132 
138 {
140  FONT_MASK_FONT = 0x00f,
147 };
148 
149 
156 struct UTF8Char
157 {
158  char c1, c2, c3;
159  // Padding for 4-byte alignment
160  // It also seems to fix some problems reported by valgrind
161  char pad;
162 
163  explicit UTF8Char(char ch1 = '\0', char ch2 = '\0', char ch3 = '\0')
164  : c1(ch1), c2(ch2), c3(ch3), pad('\0') {}
165 
166  inline bool operator<(const UTF8Char &other) const
167  {
168  if (c1 < other.c1)
169  return true;
170  else if (c1 > other.c1)
171  return false;
172 
173  if (c2 < other.c2)
174  return true;
175  else if (c2 > other.c2)
176  return false;
177 
178  return c3 < other.c3;
179  }
180 
181  inline bool operator==(const UTF8Char &other) const
182  {
183  return c1 == other.c1 && c2 == other.c2 && c3 == other.c3;
184  }
185 };
186 
192 {
193  unsigned int id = 0;
194  Math::IntPoint charPos;
195  Math::IntPoint charSize;
196  Math::IntPoint tileSize;
197 };
198 
199 // Definition is private - in text.cpp
200 struct CachedFont;
201 struct MultisizeFont;
202 struct FontTexture;
203 
209 {
210  CHAR_TAB = '\t',
211  CHAR_NEWLINE = '\n',
212  CHAR_DOT = 1,
216 };
217 
233 class CText
234 {
235 public:
236  CText(CEngine* engine);
237  virtual ~CText();
238 
240  void SetDevice(CDevice *device);
241 
243  std::string GetError();
244 
246  bool Create();
248  void Destroy();
249 
251  void FlushCache();
252 
254  void SetTabSize(int tabSize);
256  int GetTabSize();
258 
260  void DrawText(const std::string &text, std::vector<FontMetaChar>::iterator format,
261  std::vector<FontMetaChar>::iterator end,
262  float size, Math::Point pos, float width, TextAlign align,
263  int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f));
265  void DrawText(const std::string &text, FontType font,
266  float size, Math::Point pos, float width, TextAlign align,
267  int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f));
268 
270  void SizeText(const std::string &text, std::vector<FontMetaChar>::iterator format,
271  std::vector<FontMetaChar>::iterator endFormat,
272  float size, Math::Point pos, TextAlign align,
273  Math::Point &start, Math::Point &end);
275  void SizeText(const std::string &text, FontType font,
276  float size, Math::Point pos, TextAlign align,
277  Math::Point &start, Math::Point &end);
278 
280  float GetAscent(FontType font, float size);
282  float GetDescent(FontType font, float size);
284  float GetHeight(FontType font, float size);
285 
287  TEST_VIRTUAL float GetStringWidth(const std::string& text,
288  std::vector<FontMetaChar>::iterator format,
289  std::vector<FontMetaChar>::iterator end, float size);
291  TEST_VIRTUAL float GetStringWidth(std::string text, FontType font, float size);
293  TEST_VIRTUAL float GetCharWidth(UTF8Char ch, FontType font, float size, float offset);
294 
296  int Justify(const std::string &text, std::vector<FontMetaChar>::iterator format,
297  std::vector<FontMetaChar>::iterator end,
298  float size, float width);
300  int Justify(const std::string &text, FontType font, float size, float width);
301 
303  int Detect(const std::string &text, std::vector<FontMetaChar>::iterator format,
304  std::vector<FontMetaChar>::iterator end,
305  float size, float offset);
307  int Detect(const std::string &text, FontType font, float size, float offset);
308 
309  UTF8Char TranslateSpecialChar(int specialChar);
310 
311  CharTexture GetCharTexture(UTF8Char ch, FontType font, float size);
312  Math::IntPoint GetFontTextureSize();
313 
314 protected:
315  CachedFont* GetOrOpenFont(FontType font, float size);
316  CharTexture CreateCharTexture(UTF8Char ch, CachedFont* font);
317  FontTexture* GetOrCreateFontTexture(Math::IntPoint tileSize);
318  FontTexture CreateFontTexture(Math::IntPoint tileSize);
319  Math::IntPoint GetNextTilePos(const FontTexture& fontTexture);
320 
321  void DrawString(const std::string &text, std::vector<FontMetaChar>::iterator format,
322  std::vector<FontMetaChar>::iterator end,
323  float size, Math::Point pos, float width, int eol, Color color);
324  void DrawString(const std::string &text, FontType font,
325  float size, Math::Point pos, float width, int eol, Color color);
326  void DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size);
327  void DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos, Color color);
328  void StringToUTFCharList(const std::string &text, std::vector<UTF8Char> &chars);
329  void StringToUTFCharList(const std::string &text, std::vector<UTF8Char> &chars, std::vector<FontMetaChar>::iterator format, std::vector<FontMetaChar>::iterator end);
330 
331 protected:
332  CEngine* m_engine;
333  CDevice* m_device;
334 
335  std::string m_error;
336  float m_defaultSize;
337  int m_tabSize;
338 
339  std::map<FontType, std::unique_ptr<MultisizeFont>> m_fonts;
340  std::vector<FontTexture> m_fontTextures;
341 
342  FontType m_lastFontType;
343  int m_lastFontSize;
344  CachedFont* m_lastCachedFont;
345 };
346 
347 
348 } // namespace Gfx
Font with multiple possible sizes.
Definition: text.cpp:47
Base TTF font with UTF-8 char cache.
Definition: text.cpp:71
Flag for bold font subtype.
Definition: text.h:75
Mask for FontType.
Definition: text.h:140
FontMask
Masks in FontMetaChar for different attributes.
Definition: text.h:137
Texture of font character.
Definition: text.h:191
types in CBot scripts
Definition: text.h:125
Point struct and related functions.
SpecialChar
Special codes for certain characters.
Definition: text.h:208
string literals in CBot scripts
Definition: text.h:130
code background in SatCom
Definition: text.h:122
Newline character - arrow pointing down and left.
Definition: text.h:212
FontHighlight
Type of color highlight for text.
Definition: text.h:118
Alias for bold courier font.
Definition: text.h:89
Tab character - :
Definition: text.h:211
builtin keywords in CBot scripts
Definition: text.h:129
short FontMetaChar
Type used for font character metainfo.
Definition: text.h:64
Courier (monospace) font used mainly in code editor (only regular & bold)
Definition: text.h:87
Flag for italic font subtype.
Definition: text.h:77
keywords in CBot scripts
Definition: text.h:124
const float FONT_SIZE_BIG
Standard big font size.
Definition: text.h:48
Square.
Definition: text.h:214
Alias for bold colobot font.
Definition: text.h:82
Color structs and related functions.
Mask for FontHighlight.
Definition: text.h:144
UTF-8 character in font cache.
Definition: text.h:156
"this" keyword in CBot scripts
Definition: text.h:127
Pseudo-font loaded from textures for buttons, icons, etc.
Definition: text.h:94
2D point
Definition: point.h:50
Single texture filled with character textures.
Definition: text.cpp:60
comments in CBot scripts
Definition: text.h:128
constants in CBot scripts
Definition: text.h:126
Namespace for (new) graphics code.
Definition: app.h:49
Filled triangle pointing right.
Definition: text.h:215
The graphics engine.
Definition: engine.h:619
background for keys in documentation in SatCom
Definition: text.h:123
Single dot in the middle.
Definition: text.h:213
TextAlign
Type of text alignment.
Definition: text.h:54
Text rendering engine.
Definition: text.h:233
const float FONT_SIZE_SMALL
Standard small font size.
Definition: text.h:46
2D Point with integer coords
Definition: intpoint.h:39
RGBA color.
Definition: color.h:39
Mask for image bit (TODO: not used?)
Definition: text.h:146
IntPoint struct.
Mask for FontTitle.
Definition: text.h:142
FontTitle
Size of font title.
Definition: text.h:105
Alias for italic colobot font.
Definition: text.h:84
FontType
Type of font.
Definition: text.h:72
Default colobot font used for interface.
Definition: text.h:80
Abstract interface of graphics device.
Definition: device.h:323
link underline
Definition: text.h:121