Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
text.h
Go to the documentation of this file.
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
3 // * Copyright (C) 2012, Polish Portal of Colobot (PPC)
4 // *
5 // * This program is free software: you can redistribute it and/or modify
6 // * it under the terms of the GNU General Public License as published by
7 // * the Free Software Foundation, either version 3 of the License, or
8 // * (at your option) any later version.
9 // *
10 // * This program is distributed in the hope that it will be useful,
11 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // * GNU General Public License for more details.
14 // *
15 // * You should have received a copy of the GNU General Public License
16 // * along with this program. If not, see http://www.gnu.org/licenses/.
17 
23 #pragma once
24 
25 
26 #include "graphics/core/color.h"
27 
28 #include "math/point.h"
29 
30 #include <vector>
31 #include <map>
32 
33 
34 // Graphics module namespace
35 namespace Gfx {
36 
37 class CEngine;
38 class CDevice;
39 
41 const float FONT_SIZE_SMALL = 12.0f;
43 const float FONT_SIZE_BIG = 18.0f;
44 
50 {
51  TEXT_ALIGN_RIGHT,
52  TEXT_ALIGN_LEFT,
53  TEXT_ALIGN_CENTER
54 };
55 
56 /* Font meta char constants */
57 
59 typedef short FontMetaChar;
60 
68 {
70  FONT_BOLD = 0x04,
72  FONT_ITALIC = 0x08,
73 
75  FONT_COLOBOT = 0x00,
80 
82  FONT_COURIER = 0x01,
85 
86  // 0x02 left for possible another font
87 
89  FONT_BUTTON = 0x03,
90 };
91 
101 {
102  FONT_TITLE_BIG = 0x01 << 4,
103  FONT_TITLE_NORM = 0x02 << 4,
104  FONT_TITLE_LITTLE = 0x03 << 4,
105 };
106 
114 {
115  FONT_HIGHLIGHT_NONE = 0x00 << 6,
116  FONT_HIGHLIGHT_LINK = 0x01 << 6,
117  FONT_HIGHLIGHT_TOKEN = 0x02 << 6,
118  FONT_HIGHLIGHT_TYPE = 0x03 << 6,
119  FONT_HIGHLIGHT_CONST = 0x04 << 6,
120  FONT_HIGHLIGHT_REM = 0x05 << 6,
121  FONT_HIGHLIGHT_KEY = 0x06 << 6,
122  FONT_HIGHLIGHT_TABLE = 0x07 << 6,
123 };
124 
130 {
132  FONT_MASK_FONT = 0x00f,
139 };
140 
141 
148 struct UTF8Char
149 {
150  char c1, c2, c3;
151  // Padding for 4-byte alignment
152  // It also seems to fix some problems reported by valgrind
153  char pad;
154 
155  explicit UTF8Char(char ch1 = '\0', char ch2 = '\0', char ch3 = '\0')
156  : c1(ch1), c2(ch2), c3(ch3), pad('\0') {}
157 
158  inline bool operator<(const UTF8Char &other) const
159  {
160  if (c1 < other.c1)
161  return true;
162  else if (c1 > other.c1)
163  return false;
164 
165  if (c2 < other.c2)
166  return true;
167  else if (c2 > other.c2)
168  return false;
169 
170  return c3 < other.c3;
171  }
172 
173  inline bool operator==(const UTF8Char &other) const
174  {
175  return c1 == other.c1 && c2 == other.c2 && c3 == other.c3;
176  }
177 };
178 
184 {
185  unsigned int id;
186  Math::Point texSize;
187  Math::Point charSize;
188 
189  CharTexture() : id(0) {}
190 };
191 
192 // Definition is private - in text.cpp
193 struct CachedFont;
194 
200 {
201  std::string fileName;
202  std::map<int, CachedFont*> fonts;
203 
204  MultisizeFont(const std::string &fn)
205  : fileName(fn) {}
206 };
207 
213 {
214  CHAR_TAB = '\t',
215  CHAR_NEWLINE = '\n',
216  CHAR_DOT = 1,
220 };
221 
237 class CText
238 {
239 public:
240  CText(CEngine* engine);
241  virtual ~CText();
242 
244  void SetDevice(CDevice *device);
245 
247  std::string GetError();
248 
250  bool Create();
252  void Destroy();
253 
255  void FlushCache();
256 
258 
259  void SetTabSize(int tabSize);
260  int GetTabSize();
262 
264  void DrawText(const std::string &text, std::vector<FontMetaChar>::iterator format,
265  std::vector<FontMetaChar>::iterator end,
266  float size, Math::Point pos, float width, TextAlign align,
267  int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f));
269  void DrawText(const std::string &text, FontType font,
270  float size, Math::Point pos, float width, TextAlign align,
271  int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f));
272 
274  void SizeText(const std::string &text, std::vector<FontMetaChar>::iterator format,
275  std::vector<FontMetaChar>::iterator endFormat,
276  float size, Math::Point pos, TextAlign align,
277  Math::Point &start, Math::Point &end);
279  void SizeText(const std::string &text, FontType font,
280  float size, Math::Point pos, TextAlign align,
281  Math::Point &start, Math::Point &end);
282 
284  float GetAscent(FontType font, float size);
286  float GetDescent(FontType font, float size);
288  float GetHeight(FontType font, float size);
289 
291  TEST_VIRTUAL float GetStringWidth(const std::string& text,
292  std::vector<FontMetaChar>::iterator format,
293  std::vector<FontMetaChar>::iterator end, float size);
295  TEST_VIRTUAL float GetStringWidth(std::string text, FontType font, float size);
297  TEST_VIRTUAL float GetCharWidth(UTF8Char ch, FontType font, float size, float offset);
298 
300  int Justify(const std::string &text, std::vector<FontMetaChar>::iterator format,
301  std::vector<FontMetaChar>::iterator end,
302  float size, float width);
304  int Justify(const std::string &text, FontType font, float size, float width);
305 
307  int Detect(const std::string &text, std::vector<FontMetaChar>::iterator format,
308  std::vector<FontMetaChar>::iterator end,
309  float size, float offset);
311  int Detect(const std::string &text, FontType font, float size, float offset);
312 
313  UTF8Char TranslateSpecialChar(int specialChar);
314 
315 protected:
316  CachedFont* GetOrOpenFont(FontType type, float size);
317  CharTexture CreateCharTexture(UTF8Char ch, CachedFont* font);
318 
319  void DrawString(const std::string &text, std::vector<FontMetaChar>::iterator format,
320  std::vector<FontMetaChar>::iterator end,
321  float size, Math::Point pos, float width, int eol, Color color);
322  void DrawString(const std::string &text, FontType font,
323  float size, Math::Point pos, float width, int eol, Color color);
324  void DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size);
325  void DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos, Color color);
326  void StringToUTFCharList(const std::string &text, std::vector<UTF8Char> &chars);
327 
328 protected:
329  CEngine* m_engine;
330  CDevice* m_device;
331 
332  std::string m_error;
333  float m_defaultSize;
334  int m_tabSize;
335 
336  std::map<FontType, MultisizeFont*> m_fonts;
337 
338  FontType m_lastFontType;
339  int m_lastFontSize;
340  CachedFont* m_lastCachedFont;
341 };
342 
343 
344 } // namespace Gfx
345