SDL Window Engine  20200905
swe_fontset.h
1 /***************************************************************************
2  * Copyright (C) 2017 by SWE team <sdl.window.engine@gmail.com> *
3  * *
4  * Part of the SWE: SDL Window Engine: *
5  * https://github.com/AndreyBarmaley/sdl-window-engine *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 
23 #ifndef _SWE_FONTRENDER_
24 #define _SWE_FONTRENDER_
25 
26 #include "swe_types.h"
27 #include "swe_surface.h"
28 #include "swe_binarybuf.h"
29 #include "swe_cunicode_color.h"
30 
32 namespace SWE
33 {
34 
37  enum AlignType
38  {
44  };
45 
46  struct CharsetID;
47 
48 #if (SDL_VERSIONNUM(SDL_TTF_MAJOR_VERSION, SDL_TTF_MINOR_VERSION, SDL_TTF_PATCHLEVEL) <= SDL_VERSIONNUM(2, 0, 10))
49 #define TTF_STYLE_STRIKETHROUGH 0x08
50 #define TTF_HINTING_NORMAL 0
51 #define TTF_HINTING_LIGHT 1
52 #define TTF_HINTING_MONO 2
53 #define TTF_HINTING_NONE 3
54 #endif
55 
56 #ifdef SWE_DISABLE_TTF
57 #define TTF_STYLE_NORMAL 0
58 #define TTF_STYLE_BOLD 0x01
59 #define TTF_STYLE_ITALIC 0x02
60 #define TTF_STYLE_UNDERLINE 0x04
61 #define TTF_STYLE_STRIKETHROUGH 0x08
62 #define TTF_HINTING_NORMAL 0
63 #define TTF_HINTING_LIGHT 1
64 #define TTF_HINTING_MONO 2
65 #define TTF_HINTING_NONE 3
66 #endif
67 
71  {
76  };
77 
80  enum CharStyle
81  {
82  StyleDefault = -1,
83  StyleNormal = TTF_STYLE_NORMAL,
84  StyleBold = TTF_STYLE_BOLD,
85  StyleItalic = TTF_STYLE_ITALIC,
86  StyleUnderLine = TTF_STYLE_UNDERLINE,
87  StyleStrikeThrough = TTF_STYLE_STRIKETHROUGH
88  };
89 
93  {
95  HintingNormal = TTF_HINTING_NORMAL,
96  HintingLight = TTF_HINTING_LIGHT,
97  HintingMono = TTF_HINTING_MONO,
98  HintingNone = TTF_HINTING_NONE
99  };
100 
103  {
104  /* blended 2 bit, style 4 bit, hinting 2 bit */
105  u8 val;
106 
107  CharProperty(const CharRender & = RenderSolid, int style = StyleNormal, const CharHinting & = HintingNormal);
108 
109  int operator()(void) const;
110  CharRender render(void) const;
111  int style(void) const;
112  CharHinting hinting(void) const;
113  void reset(void);
114  void setRender(const CharRender &);
115  void setStyle(int);
116  void setHinting(const CharHinting &);
117 
118  bool operator<(const CharProperty & cp) const;
119  bool operator!=(const CharProperty &) const;
120  };
121 
123  struct FontID
124  {
125  u16 val1;
126  u8 val2;
127  CharProperty val3;
128 
129  /* font id 16 bit, font size 8 bit, charset property 8 bit */
130  FontID() : val1(0), val2(0) {}
131  FontID(int id, int sz, const CharProperty & cp = CharProperty());
132 
133  int operator()(void) const;
134  int value(void) const;
135  bool operator<(const FontID &) const;
136  bool operator>(const FontID &) const;
137  bool operator==(const FontID &) const;
138 
139  int id(void) const;
140  int size(void) const;
141 
142  const CharProperty & property(void) const;
143 
144  void reset(void);
145  void setId(int v);
146  void setSize(int v);
147  void setProperty(const CharProperty &);
148  };
149 
152  {
153  protected:
154  Size fontSize;
155 
156  public:
157  FontRender() {}
158  FontRender(const Size & fsz) : fontSize(fsz) {}
159  virtual ~FontRender() {}
160 
161  virtual const FontID & id(void) const = 0;
162  virtual bool isValid(void) const = 0;
163  virtual bool isTTF(void) const = 0;
164 
165  virtual Size stringSize(const std::string &, bool horizontal = true) const = 0;
166  virtual Size unicodeSize(const UnicodeString &, bool horizontal = true) const = 0;
167 
168  virtual int symbolAdvance(int = 0x20) const = 0;
169  virtual int lineSkipHeight(void) const = 0;
170 
171  virtual Surface renderCharset(int, const Color &, const CharRender & = RenderDefault, int style = StyleDefault, const CharHinting & = HintingDefault) const = 0;
172 
173  //
174  UCStringList splitUCStringWidth(const UCString &, int) const;
175  UnicodeList splitUnicodeWidth(const UnicodeString &, int) const;
176  StringList splitStringWidth(const std::string &, int) const;
177 
178  const Size & size(void) const
179  {
180  return fontSize;
181  }
182 
183  static void clearCache(void);
184  static void dumpCache(void);
185 
186  void renderString(const std::string &, const Color &, const Point &, Surface &) const;
187  };
188 
191  {
192  const FontRender* render;
193 
194  public:
195  FontsCache(const FontRender* font = nullptr) : render(font) {}
196 
197  static void clear(void);
198  static void dump(void);
199  void erase(void);
200 
201  Texture renderCharset(int ch, const Color & col)
202  {
203  return renderCharset(ch, col, RenderDefault, StyleDefault, HintingDefault);
204  }
205 
206  Texture renderCharset(int ch, const Color &, const CharRender &, int style, const CharHinting &);
207  };
208 
209 #ifndef SWE_DISABLE_TTF
210  class FontRenderTTF : public FontRender
212  {
213  std::shared_ptr<TTF_Font> ptr;
214  FontID fid;
215  Color shaded;
216 
217  Size char1Size(int ch) const;
218  Size char2Size(int ch) const;
219 
220  public:
221  FontRenderTTF() {}
222  FontRenderTTF(const std::string &, size_t fsz, const CharRender & = RenderSolid, int style = StyleNormal, const CharHinting & = HintingNormal);
223  FontRenderTTF(const BinaryBuf &, size_t fsz, const CharRender & = RenderSolid, int style = StyleNormal, const CharHinting & = HintingNormal);
224  ~FontRenderTTF();
225 
226  void setShadedBackground(const Color &);
227  void reset(void);
228 
229  bool open(const std::string &, size_t fsz, const CharRender & = RenderSolid, int style = StyleNormal, const CharHinting & = HintingNormal);
230  bool load(const BinaryBuf &, size_t fsz, const CharRender & = RenderSolid, int style = StyleNormal, const CharHinting & = HintingNormal);
231 
232  const FontID & id(void) const override;
233  bool isValid(void) const override;
234  bool isTTF(void) const override;
235 
236  TTF_Font* toSDLFont(void) const;
237 
238  Size stringSize(const std::string &, bool horizontal = true) const override;
239  Size unicodeSize(const UnicodeString &, bool horizontal = true) const override;
240 
241  int symbolAdvance(int sym) const override;
242  int lineSkipHeight(void) const override;
243 
244  Surface renderCharset(int, const Color &, const CharRender & = RenderDefault, int style = StyleDefault, const CharHinting & = HintingDefault) const override;
245  // render ttf string
246  Surface renderString(const std::string &, const Color &, const CharRender & = RenderDefault, int style = StyleDefault, const CharHinting & = HintingDefault) const;
247  Surface renderUnicode(const UnicodeString &, const Color &, const CharRender & = RenderDefault, int style = StyleDefault, const CharHinting & = HintingDefault) const;
248  };
249 #endif
250 
252  class FontRenderPSF : public FontRender
253  {
254  BinaryBuf buf;
255  FontID fid;
256 
257  Size fixedSize(size_t, bool) const;
258 
259  public:
260  FontRenderPSF(const u8*, size_t, const Size &);
261  FontRenderPSF(const std::string &, const Size &);
262 
263  const FontID & id(void) const override
264  {
265  return fid;
266  }
267 
268  bool isValid(void) const override
269  {
270  return buf.size();
271  }
272 
273  bool isTTF(void) const override
274  {
275  return false;
276  }
277 
278  Size stringSize(const std::string &, bool horizontal = true) const override;
279  Size unicodeSize(const UnicodeString &, bool horizontal = true) const override;
280 
281  int symbolAdvance(int sym) const override;
282  int lineSkipHeight(void) const override;
283 
284  Surface renderCharset(int, const Color &, const CharRender & = RenderDefault, int style = StyleDefault, const CharHinting & = HintingDefault) const override;
285  };
286 
289  {
290  public:
291  FontAltC8x16();
292  };
293 
294 #define FontRenderSystem FontAltC8x16
295 
298  const FontRenderSystem & systemFont(void);
299 
300 } // SWE
301 #endif
Definition: swe_surface.h:154
по нижнему краю
Definition: swe_fontset.h:42
FontAltC8x16()
встроенный шрифт altc_8x16.
Definition: swe_fontset.cpp:807
пространство SWE.
Definition: swe_binarybuf.cpp:30
тип normal.
Definition: swe_fontset.h:95
базовый класс рендера PSF шрифта
Definition: swe_fontset.h:252
по умолчанию (используется значение заданное при инициализации)
Definition: swe_fontset.h:82
встроенный системный рендер PSF шрифта
Definition: swe_fontset.h:288
класс идентификации шрифта
Definition: swe_fontset.h:123
класс точки с двумя координатами
Definition: swe_rect.h:72
класс цветной unicode строки
Definition: swe_cunicode_color.h:67
стиль normal.
Definition: swe_fontset.h:83
const FontRenderSystem & systemFont(void)
альяс на встроенный рендер PSF шрифта
Definition: swe_fontset.cpp:811
стиль bold.
Definition: swe_fontset.h:84
класс списка unicode строк
Definition: swe_cunicode.h:109
класс цвета
Definition: swe_colors.h:65
базовый класс шрифта
Definition: swe_fontset.h:151
стиль italic.
Definition: swe_fontset.h:85
тип none.
Definition: swe_fontset.h:98
по левому краю
Definition: swe_fontset.h:39
тип blended.
Definition: swe_fontset.h:74
тип light.
Definition: swe_fontset.h:96
по центру
Definition: swe_fontset.h:43
класс списка строк
Definition: swe_cstring.h:36
свойства отрисовка символа
Definition: swe_fontset.h:102
класс бинарного массива
Definition: swe_binarybuf.h:35
по верхнему краю
Definition: swe_fontset.h:41
AlignType
перечисление типа выравнивания
Definition: swe_fontset.h:37
класс кеширования спрайтов символов
Definition: swe_fontset.h:190
стиль under line.
Definition: swe_fontset.h:86
тип solid.
Definition: swe_fontset.h:73
по правому краю
Definition: swe_fontset.h:40
класс двухмерной размерности
Definition: swe_rect.h:36
по умолчанию (используется значение заданное при инициализации)
Definition: swe_fontset.h:94
CharRender
перечисление типа рендера
Definition: swe_fontset.h:70
CharStyle
перечисление типа стиля шрифта
Definition: swe_fontset.h:80
тип shaded.
Definition: swe_fontset.h:75
стиль strike through.
Definition: swe_fontset.h:87
Definition: swe_surface.h:58
по умолчанию (используется значение заданное при инициализации)
Definition: swe_fontset.h:72
базовый класс рендера TTF шрифта
Definition: swe_fontset.h:211
класс список цветных unicode строк
Definition: swe_cunicode_color.h:110
CharHinting
перечисление типа сглаживания контура
Definition: swe_fontset.h:92
тип mono.
Definition: swe_fontset.h:97
класс unicode строки
Definition: swe_cunicode.h:41