Btk
font.hpp
1 #if !defined(_BOX_FONT_HPP_)
2 #define _BOX_FONT_HPP_
3 #include <cstddef>
4 #include <string>
5 #include <string_view>
6 #include "pixels.hpp"
7 #include "defs.hpp"
8 namespace Btk{
9  struct Size;
10  struct FontImpl;
15  enum class FontStyle:int{
16  Normal = 0x00,
17  Bold = 0x01,
18  Italic = 0x02,
19  Underline = 0x04,
20  Strikethrough = 0x08
21  };
26  class BTKAPI Font{
27  public:
28  //empty font
29  Font():pimpl(nullptr){};
30  Font(const Font &font);
31  Font(Font &&f){
32  pimpl = f.pimpl;
33  f.pimpl = nullptr;
34  };
41  Font(std::string_view fontname,int ptsize);
42  ~Font();
51  PixBuf render_solid(std::string_view text,Color color);
52  PixBuf render_solid(std::u16string_view text,Color color);
62  PixBuf render_shaded(std::string_view text,Color fg,Color bg);
63  PixBuf render_shaded(std::u16string_view text,Color fg,Color bg);
72  PixBuf render_blended(std::string_view text,Color color);
73  PixBuf render_blended(std::u16string_view text,Color color);
81  int kerning_size(char16_t prev_ch,char16_t ch) const;
89  bool has_glyph(char16_t ch) const;
95  int height() const;
101  int ptsize() const noexcept;
107  int refcount() const noexcept;
113  void set_ptsize(int new_ptsize);
120  Size size(std::string_view text);
121  Size size(std::u16string_view text);
126  void close();
133  void open(std::string_view fontname,int ptsize);
140  void openfile(std::string_view filename,int ptsize);
146  Font clone() const;
152  FontStyle style() const;
158  void set_style(FontStyle style);
159  FontImpl *impl() const noexcept{
160  return pimpl;
161  }
167  std::string family() const;
168  std::string style_name() const;
174  Font &operator =(const Font &);
175  Font &operator =(Font &&);
176  bool empty() const noexcept{
177  return pimpl == nullptr;
178  };
186  static Font FromFile(std::string_view filename,int ptsize);
187  private:
188  Font(FontImpl *i):pimpl(i){};
189  FontImpl *pimpl;
190  };
191  class FontSet{
192  #ifdef __gnu_linux__
193  public:
201  FontSet(void *pat,void *objset,void *fontset):
202  fc_pat(pat),fc_objset(objset),fc_fontset(fontset){}
203  FontSet(FontSet &&set){
204  fc_pat = set.fc_pat;
205  fc_objset = set.fc_objset;
206  fc_fontset = set.fc_fontset;
207 
208  set.fc_pat = nullptr;
209  set.fc_objset = nullptr;
210  set.fc_fontset = nullptr;
211  }
212  public:
217  struct Font{
223  std::string_view family() const;
224  std::string_view style() const;
225  std::string_view file() const;
226  void *font;
227  };
232  struct Iterator{
233  Iterator() = default;
234  Iterator(const Iterator &) = default;
235 
236  bool operator ==(const Iterator &i) const{
237  return i.font.font == font.font;
238  }
239  bool operator !=(const Iterator &i) const{
240  return i.font.font != font.font;
241  }
242  Font *operator ->(){
243  return &font;
244  }
245  Font &operator * (){
246  return font;
247  }
248  Iterator &operator =(const Iterator &) = default;
249 
250  Iterator &operator ++();
251  Iterator &operator --();
252 
253  Font font;
254  FontSet *set;
255  size_t index;
256  };
257  static constexpr bool Supported = true;
258  private:
259  void *fc_pat;//< FcPattern
260  void *fc_objset;//FcObjectSet
261  void *fc_fontset;//FcFontSet
262  #else
263  //Unsupported
264  struct Font{};
265  struct Iterator;
266  static constexpr bool Supported = false;
267  #endif
268  public:
269  typedef Iterator iterator;
270 
271  //General methods
272  ~FontSet();
279  Font operator [](size_t index) const;
285  size_t size() const;
286  FontSet &operator =(FontSet &&);
292  Iterator begin();
298  Iterator end();
299  friend struct Iterator;
300  };
305  namespace FontUtils{
312  BTKAPI std::string GetFileByName(std::string_view name);
317  BTKAPI void Init();
322  BTKAPI void Quit();
323 
324  BTKAPI FontSet GetFontList();
325  };
326  //operators for FontStyle
327  inline FontStyle operator |(FontStyle s1,FontStyle s2) noexcept{
328  return static_cast<FontStyle>(int(s1) | int(s2));
329  }
330  inline FontStyle operator +(FontStyle s1,FontStyle s2) noexcept{
331  return static_cast<FontStyle>(int(s1) | int(s2));
332  }
333  inline FontStyle operator &(FontStyle s1,FontStyle s2) noexcept{
334  return static_cast<FontStyle>(int(s1) & int(s2));
335  }
336  inline FontStyle operator +=(FontStyle s1,FontStyle s2) noexcept{
337  return static_cast<FontStyle>(int(s1) | int(s2));
338  }
339  inline FontStyle operator |=(FontStyle s1,FontStyle s2) noexcept{
340  return static_cast<FontStyle>(int(s1) | int(s2));
341  }
342 };
343 
344 
345 #endif // _BOX_FONT_HPP_
BTKAPI std::string GetFileByName(std::string_view name)
Get font file by its name.
Definition: fontutils.cpp:60
Definition: font.hpp:191
This header include many useful containers.
Definition: async.hpp:7
FontStyle
FontStyle from SDL_ttf.
Definition: font.hpp:15
Font Class.
Definition: font.hpp:26
Definition: pixels.hpp:32
Color structure.
Definition: pixels.hpp:19
Font Impl.
Definition: font.hpp:13
Size of a Widget or Window.
Definition: rect.hpp:104
BTKAPI void Quit()
Quit font utils.
Definition: fontutils.cpp:46