SuperTuxKart
face_ttf.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2016 SuperTuxKart-Team
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (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, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_FACE_TTF_HPP
20 #define HEADER_FACE_TTF_HPP
21 
22 #include "utils/no_copy.hpp"
23 
24 #include <cassert>
25 #include <map>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 #ifndef SERVER_ONLY
31 #include <ft2build.h>
32 #include FT_FREETYPE_H
33 #endif
34 
36 struct FontArea
37 {
38  FontArea() : advance_x(0), bearing_x(0), offset_y(0), offset_y_bt(0),
39  spriteno(0) {}
41  int advance_x;
43  int bearing_x;
45  int offset_y;
49  int spriteno;
50 };
51 
56 class FaceTTF : public NoCopy
57 {
58 #ifndef SERVER_ONLY
59 private:
62  std::vector<std::pair<FT_Face, std::map<unsigned, FontArea> > > m_ft_faces;
63 #endif
64 public:
65  LEAK_CHECK()
66  // ------------------------------------------------------------------------
67  FaceTTF() {}
68  // ------------------------------------------------------------------------
69  ~FaceTTF() {}
70  // ------------------------------------------------------------------------
71  void reset()
72  {
73 #ifndef SERVER_ONLY
74  for (unsigned int i = 0; i < m_ft_faces.size(); i++)
75  m_ft_faces[i].second.clear();
76 #endif
77  }
78 #ifndef SERVER_ONLY
79  // ------------------------------------------------------------------------
80  void loadTTF(std::vector<FT_Face> faces)
81  {
82  for (unsigned int i = 0; i < faces.size(); i++)
83  m_ft_faces.emplace_back(faces[i], std::map<unsigned, FontArea>());
84  }
85  // ------------------------------------------------------------------------
89  FT_Face getFace(unsigned int i) const
90  {
91  assert(i < m_ft_faces.size());
92  return m_ft_faces[i].first;
93  }
94  // ------------------------------------------------------------------------
96  unsigned int getTotalFaces() const
97  { return (unsigned int)m_ft_faces.size(); }
98  // ------------------------------------------------------------------------
99  void insertFontArea(const FontArea& a, unsigned font_index,
100  unsigned glyph_index)
101  {
102  auto& ttf = m_ft_faces.at(font_index).second;
103  ttf[glyph_index] = a;
104  }
105  // ------------------------------------------------------------------------
106  bool enabledForFont(unsigned idx) const
107  { return m_ft_faces.size() > idx && m_ft_faces[idx].first != NULL; }
108  // ------------------------------------------------------------------------
109  const FontArea* getFontArea(unsigned font_index, unsigned glyph_index)
110  {
111  auto& ttf = m_ft_faces.at(font_index).second;
112  auto it = ttf.find(glyph_index);
113  if (it != ttf.end())
114  return &it->second;
115  return NULL;
116  }
117  // ------------------------------------------------------------------------
118  bool getFontAndGlyphFromChar(uint32_t c, unsigned* font, unsigned* glyph)
119  {
120  for (unsigned i = 0; i < m_ft_faces.size(); i++)
121  {
122  unsigned glyph_index = FT_Get_Char_Index(m_ft_faces[i].first, c);
123  if (glyph_index > 0)
124  {
125  *font = i;
126  *glyph = glyph_index;
127  return true;
128  }
129  }
130  return false;
131  }
132 #endif
133 
134 }; // FaceTTF
135 
136 #endif
137 /* EOF */
int offset_y_bt
Top side bearing for horizontal layout used in billboard text.
Definition: face_ttf.hpp:47
This class will load a list of TTF files from FontManager, and save them inside m_ft_faces for FontWi...
Definition: face_ttf.hpp:56
FT_Face getFace(unsigned int i) const
Return a TTF in m_ft_faces.
Definition: face_ttf.hpp:89
int advance_x
Advance width for horizontal layout.
Definition: face_ttf.hpp:41
int offset_y
Top side bearing for horizontal layout.
Definition: face_ttf.hpp:45
std::vector< std::pair< FT_Face, std::map< unsigned, FontArea > > > m_ft_faces
Contains all FT_Face with a list of loaded glyph index with the FontArea.
Definition: face_ttf.hpp:62
Glyph metrics for each glyph loaded.
Definition: face_ttf.hpp:36
int bearing_x
Left side bearing for horizontal layout.
Definition: face_ttf.hpp:43
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:25
int spriteno
Index number in sprite bank of FontWithFace.
Definition: face_ttf.hpp:49
unsigned int getTotalFaces() const
Return the total TTF files loaded.
Definition: face_ttf.hpp:96