Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.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 <vector>
33 #include <map>
34 
35 
36 // Graphics module namespace
37 namespace Gfx {
38 
39 class CEngine;
40 class CDevice;
41 
43 const float FONT_SIZE_SMALL = 12.0f;
45 const float FONT_SIZE_BIG = 18.0f;
46 
52 {
53  TEXT_ALIGN_RIGHT,
54  TEXT_ALIGN_LEFT,
55  TEXT_ALIGN_CENTER
56 };
57 
58 /* Font meta char constants */
59 
61 typedef short FontMetaChar;
62 
70 {
72  FONT_BOLD = 0x04,
74  FONT_ITALIC = 0x08,
75 
77  FONT_COLOBOT = 0x00,
82 
84  FONT_COURIER = 0x01,
87 
88  // 0x02 left for possible another font
89 
91  FONT_BUTTON = 0x03,
92 };
93 
103 {
104  FONT_TITLE_BIG = 0x01 << 4,
105  FONT_TITLE_NORM = 0x02 << 4,
106  FONT_TITLE_LITTLE = 0x03 << 4,
107 };
108 
116 {
117  FONT_HIGHLIGHT_NONE = 0x00 << 6,
118  FONT_HIGHLIGHT_LINK = 0x01 << 6,
119  FONT_HIGHLIGHT_TOKEN = 0x02 << 6,
120  FONT_HIGHLIGHT_TYPE = 0x03 << 6,
121  FONT_HIGHLIGHT_CONST = 0x04 << 6,
122  FONT_HIGHLIGHT_REM = 0x05 << 6,
123  FONT_HIGHLIGHT_KEY = 0x06 << 6,
124  FONT_HIGHLIGHT_TABLE = 0x07 << 6,
125 };
126 
132 {
134  FONT_MASK_FONT = 0x00f,
141 };
142 
143 
150 struct UTF8Char
151 {
152  char c1, c2, c3;
153  // Padding for 4-byte alignment
154  // It also seems to fix some problems reported by valgrind
155  char pad;
156 
157  explicit UTF8Char(char ch1 = '\0', char ch2 = '\0', char ch3 = '\0')
158  : c1(ch1), c2(ch2), c3(ch3), pad('\0') {}
159 
160  inline bool operator<(const UTF8Char &other) const
161  {
162  if (c1 < other.c1)
163  return true;
164  else if (c1 > other.c1)
165  return false;
166 
167  if (c2 < other.c2)
168  return true;
169  else if (c2 > other.c2)
170  return false;
171 
172  return c3 < other.c3;
173  }
174 
175  inline bool operator==(const UTF8Char &other) const
176  {
177  return c1 == other.c1 && c2 == other.c2 && c3 == other.c3;
178  }
179 };
180 
186 {
187  unsigned int id;
188  Math::Point texSize;
189  Math::Point charSize;
190 
191  CharTexture() : id(0) {}
192 };
193 
194 // Definition is private - in text.cpp
195 struct CachedFont;
196 
202 {
203  std::string fileName;
204  std::map<int, CachedFont*> fonts;
205 
206  MultisizeFont(const std::string &fn)
207  : fileName(fn) {}
208 };
209 
215 {
216  CHAR_TAB = '\t',
217  CHAR_NEWLINE = '\n',
218  CHAR_DOT = 1,
222 };
223 
239 class CText
240 {
241 public:
242  CText(CEngine* engine);
243  virtual ~CText();
244 
246  void SetDevice(CDevice *device);
247 
249  std::string GetError();
250 
252  bool Create();
254  void Destroy();
255 
257  void FlushCache();
258 
260  void SetTabSize(int tabSize);
262  int GetTabSize();
264 
266  void DrawText(const std::string &text, std::vector<FontMetaChar>::iterator format,
267  std::vector<FontMetaChar>::iterator end,
268  float size, Math::Point pos, float width, TextAlign align,
269  int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f));
271  void DrawText(const std::string &text, FontType font,
272  float size, Math::Point pos, float width, TextAlign align,
273  int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f));
274 
276  void SizeText(const std::string &text, std::vector<FontMetaChar>::iterator format,
277  std::vector<FontMetaChar>::iterator endFormat,
278  float size, Math::Point pos, TextAlign align,
279  Math::Point &start, Math::Point &end);
281  void SizeText(const std::string &text, FontType font,
282  float size, Math::Point pos, TextAlign align,
283  Math::Point &start, Math::Point &end);
284 
286  float GetAscent(FontType font, float size);
288  float GetDescent(FontType font, float size);
290  float GetHeight(FontType font, float size);
291 
293  TEST_VIRTUAL float GetStringWidth(const std::string& text,
294  std::vector<FontMetaChar>::iterator format,
295  std::vector<FontMetaChar>::iterator end, float size);
297  TEST_VIRTUAL float GetStringWidth(std::string text, FontType font, float size);
299  TEST_VIRTUAL float GetCharWidth(UTF8Char ch, FontType font, float size, float offset);
300 
302  int Justify(const std::string &text, std::vector<FontMetaChar>::iterator format,
303  std::vector<FontMetaChar>::iterator end,
304  float size, float width);
306  int Justify(const std::string &text, FontType font, float size, float width);
307 
309  int Detect(const std::string &text, std::vector<FontMetaChar>::iterator format,
310  std::vector<FontMetaChar>::iterator end,
311  float size, float offset);
313  int Detect(const std::string &text, FontType font, float size, float offset);
314 
315  UTF8Char TranslateSpecialChar(int specialChar);
316 
317 protected:
318  CachedFont* GetOrOpenFont(FontType type, float size);
319  CharTexture CreateCharTexture(UTF8Char ch, CachedFont* font);
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, MultisizeFont*> m_fonts;
340 
341  FontType m_lastFontType;
342  int m_lastFontSize;
343  CachedFont* m_lastCachedFont;
344 };
345 
346 
347 } // namespace Gfx
348 
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:300
Font with multiple possible sizes.
Definition: text.h:201
void SetDevice(CDevice *device)
Sets the device to be used.
Definition: text.cpp:128
Base TTF font with UTF-8 char cache.
Definition: text.cpp:44
Flag for bold font subtype.
Definition: text.h:72
Mask for FontType.
Definition: text.h:134
FontMask
Masks in FontMetaChar for different attributes.
Definition: text.h:131
TEST_VIRTUAL float GetCharWidth(UTF8Char ch, FontType font, float size, float offset)
Returns width of single character.
Definition: text.cpp:351
Texture of font character.
Definition: text.h:185
Point struct and related functions.
SpecialChar
Special codes for certain characters.
Definition: text.h:214
void Destroy()
Frees resources before exit.
Definition: text.cpp:101
Newline character - arrow pointing down and left.
Definition: text.h:218
FontHighlight
Type of color highlight for text.
Definition: text.h:115
Alias for bold courier font.
Definition: text.h:86
Tab character - :
Definition: text.h:217
short FontMetaChar
Type used for font character metainfo.
Definition: text.h:61
float GetDescent(FontType font, float size)
Returns the descent font metric.
Definition: text.cpp:275
Courier (monospace) font used mainly in code editor (only regular & bold)
Definition: text.h:84
Flag for italic font subtype.
Definition: text.h:74
const float FONT_SIZE_BIG
Standard big font size.
Definition: text.h:45
Square.
Definition: text.h:220
float GetAscent(FontType font, float size)
Returns the ascent font metric.
Definition: text.cpp:263
bool Create()
Initializes the font engine; must be called after SetDevice()
Definition: text.cpp:75
Alias for bold colobot font.
Definition: text.h:79
Color structs and related functions.
Mask for FontHighlight.
Definition: text.h:138
UTF-8 character in font cache.
Definition: text.h:150
Pseudo-font loaded from textures for buttons, icons, etc.
Definition: text.h:91
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:478
2D point
Definition: point.h:49
Namespace for (new) graphics code.
Definition: app.h:49
Filled triangle pointing right.
Definition: text.h:221
The graphics engine.
Definition: engine.h:684
Single dot in the middle.
Definition: text.h:219
float GetHeight(FontType font, float size)
Returns the height font metric.
Definition: text.cpp:287
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:171
TextAlign
Type of text alignment.
Definition: text.h:51
Text rendering engine.
Definition: text.h:239
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:393
const float FONT_SIZE_SMALL
Standard small font size.
Definition: text.h:43
RGBA color.
Definition: color.h:38
void SetTabSize(int tabSize)
Tab size management.
Definition: text.cpp:166
Mask for image bit (TODO: not used?)
Definition: text.h:140
std::string GetError()
Returns the last encountered error.
Definition: text.cpp:133
Mask for FontTitle.
Definition: text.h:136
FontTitle
Size of font title.
Definition: text.h:102
Alias for italic colobot font.
Definition: text.h:81
FontType
Type of font.
Definition: text.h:69
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:216
Default colobot font used for interface.
Definition: text.h:77
Abstract interface of graphics device.
Definition: device.h:251
void FlushCache()
Flushes cached textures.
Definition: text.cpp:138