OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
FontTTF.cpp
Go to the documentation of this file.
1 
8 #include <fstream>
9 
10 #include "global.h"
11 #include "Log.h"
12 #include "TextureManager.h"
13 #include "utils/pixel.h"
14 #include "system/FontTTF.h"
15 
16 #define MAP_WIDTH 512
17 #define MAP_HEIGHT 512
18 #define FONT_SIZE 33
19 #define MAP_NUM_CHARS 100
20 
21 FontMapTTF::FontMapTTF() : begin(-1), texture(-1), charInfo(nullptr) { }
22 
24  begin = other.begin;
25  texture = other.texture;
26  other.begin = other.texture = -1;
27  charInfo = other.charInfo;
28  other.charInfo = nullptr;
29 }
30 
32  if (charInfo != nullptr) {
33  delete [] charInfo;
34  }
35 
36  if (texture > -1) {
38  }
39 }
40 
41 int FontMapTTF::initialize(unsigned char* fontData, int firstChar) {
42  stbtt_pack_context context;
43  unsigned char* pixels = new unsigned char[MAP_WIDTH * MAP_HEIGHT];
44  if (!stbtt_PackBegin(&context, pixels, MAP_WIDTH, MAP_HEIGHT, 0, 1, nullptr)) {
45  delete [] pixels;
46  Log::get(LOG_ERROR) << "Error initializing font map in stbtt_PackBegin!" << Log::endl;
47  return -1;
48  }
49 
50  stbtt_PackSetOversampling(&context, 2, 2);
51 
52  if (charInfo != nullptr)
53  delete [] charInfo;
54 
56  if (!stbtt_PackFontRange(&context, fontData, 0, FONT_SIZE, firstChar, MAP_NUM_CHARS, charInfo)) {
57  stbtt_PackEnd(&context);
58  delete [] pixels;
59  delete [] charInfo;
60  charInfo = nullptr;
61  Log::get(LOG_ERROR) << "Error packing font map!" << Log::endl;
62  return -2;
63  }
64 
65  stbtt_PackEnd(&context);
66  unsigned char* rgb = grayscale2rgba(pixels, MAP_WIDTH, MAP_HEIGHT);
67  delete [] pixels;
68 
71  delete [] rgb;
72  if (texture < 0) {
73  delete [] charInfo;
74  charInfo = nullptr;
75  Log::get(LOG_ERROR) << "Error loading new font map texture!" << Log::endl;
76  return -3;
77  }
78 
79  begin = firstChar;
80  return 0;
81 }
82 
83 bool FontMapTTF::contains(int c) {
84  assert(begin >= 0);
85  return (begin >= 0) && (c >= begin) && (c < (begin + MAP_NUM_CHARS));
86 }
87 
88 void FontMapTTF::getQuad(int c, float* xpos, float* ypos, stbtt_aligned_quad* quad) {
89  assert(contains(c));
90  stbtt_GetPackedQuad(charInfo, MAP_WIDTH, MAP_HEIGHT, c - begin, xpos, ypos, quad, 0);
91 }
92 
93 // ----------------------------------------------------------------------------
94 
95 unsigned char* FontTTF::fontData = nullptr;
96 std::vector<FontMapTTF> FontTTF::maps;
99 
100 int FontTTF::initialize(std::string f) {
101  assert(f.length() > 0);
102 
103  std::ifstream file(f, std::ios::binary);
104  if (!file) {
105  Log::get(LOG_ERROR) << "Couldn't open file \"" << f << "\"!" << Log::endl;
106  return -1;
107  }
108 
109  file.seekg(0, std::ios::end);
110  auto size = file.tellg();
111  assert(size > 0);
112  file.seekg(0);
113 
114  maps.clear();
115  if (fontData != nullptr) {
116  delete [] fontData;
117  fontData = nullptr;
118  }
119 
120  fontData = new unsigned char[size];
121  if (!file.read(reinterpret_cast<char*>(fontData), size)) {
122  Log::get(LOG_ERROR) << "Couldn't read data in \"" << f << "\"" << Log::endl;
123  delete [] fontData;
124  fontData = nullptr;
125  return -2;
126  }
127 
128  maps.emplace_back();
129  if (maps.at(0).initialize(fontData, ' ') < 0) {
130  delete [] fontData;
131  fontData = nullptr;
132  return -3;
133  }
134 
135  return 0;
136 }
137 
139  if (fontData != nullptr) {
140  delete [] fontData;
141  fontData = nullptr;
142  }
143 
144  maps.clear();
145 }
146 
147 unsigned int FontTTF::widthText(float scale, std::string s) {
148  float x = 0.0f, y = 0.0f;
150  for (int i = 0; i < s.length(); i++) {
151  if ((s[i] < 0x20) || (s[i] == 0x7F)) {
152  continue;
153  }
154 
155  getQuad(s[i], &x, &y, &q);
156  }
157  return x * scale;
158 }
159 
160 void FontTTF::drawText(unsigned int x, unsigned int y, float scale,
161  glm::vec4 color, std::string s) {
162  drawTextInternal(x, y, scale, color, 0, s, false);
163 }
164 
165 unsigned int FontTTF::heightText(float scale, unsigned int maxWidth, std::string s) {
166  float x = 0.0f, y = FONT_SIZE;
168  for (int i = 0; i < s.length(); i++) {
169  if ((s[i] < 0x20) || (s[i] == 0x7F)) {
170  continue;
171  }
172 
173  getQuad(s[i], &x, &y, &q);
174  if (x > maxWidth) {
175  x = 0.0f;
176  y += FONT_SIZE;
177  }
178  }
179  return y * scale;
180 }
181 
182 void FontTTF::drawTextWrapped(unsigned int x, unsigned int y, float scale,
183  glm::vec4 color, unsigned int maxWidth, std::string s) {
184  drawTextInternal(x, y, scale, color, maxWidth, s, true);
185 }
186 
187 void FontTTF::drawTextInternal(unsigned int x, unsigned int y, float scale,
188  glm::vec4 color, unsigned int maxWidth, std::string s,
189  bool drawWrapped) {
190  std::vector<glm::vec2> vertices;
191  std::vector<glm::vec2> uvs;
192  int texture = -1;
193  float xpos = x, ypos = y + (FONT_SIZE * scale);
194  for (int i = 0; i < s.length(); i++) {
195  if ((s[i] < 0x20) || (s[i] == 0x7F)) {
196  continue;
197  }
198 
199  stbtt_aligned_quad quad;
200  int tex = getQuad(s[i], &xpos, &ypos, &quad);
201 
202  if (drawWrapped && (xpos > (x + maxWidth))) {
203  xpos = x;
204  ypos += FONT_SIZE * scale;
205  if (s[i] != ' ')
206  i--;
207  continue;
208  }
209 
210  if ((texture != tex) && (texture != -1)) {
211  vertexBuffer.bufferData(vertices);
212  uvBuffer.bufferData(uvs);
213  Shader::drawGL(vertexBuffer, uvBuffer, color, texture);
214  vertices.clear();
215  uvs.clear();
216  }
217 
218  texture = tex;
219 
220  float xmin = quad.x0;
221  float xmax = quad.x0 + ((quad.x1 - quad.x0) * scale);
222  float ymin = quad.y1;
223  float ymax = quad.y1 + ((quad.y0 - quad.y1) * scale);
224 
225  vertices.emplace_back(xmin, ymin);
226  vertices.emplace_back(xmin, ymax);
227  vertices.emplace_back(xmax, ymax);
228  vertices.emplace_back(xmax, ymin);
229  vertices.emplace_back(xmin, ymin);
230  vertices.emplace_back(xmax, ymax);
231 
232  uvs.emplace_back(quad.s0, quad.t1);
233  uvs.emplace_back(quad.s0, quad.t0);
234  uvs.emplace_back(quad.s1, quad.t0);
235  uvs.emplace_back(quad.s1, quad.t1);
236  uvs.emplace_back(quad.s0, quad.t1);
237  uvs.emplace_back(quad.s1, quad.t0);
238  }
239 
240  vertexBuffer.bufferData(vertices);
241  uvBuffer.bufferData(uvs);
242  Shader::drawGL(vertexBuffer, uvBuffer, color, texture);
243 }
244 
246  for (int i = 0; i < maps.size(); i++) {
247  if (maps.at(i).contains(c)) {
248  return i;
249  }
250  }
251 
252  int begin = c;
253  if (c >= (MAP_NUM_CHARS / 2))
254  begin -= (MAP_NUM_CHARS / 2);
255 
256  Log::get(LOG_INFO) << "Unmapped character '" << char(c) << "', new map from " << begin << " to "
257  << begin + MAP_NUM_CHARS - 1 << "..." << Log::endl;
258 
259  int p = maps.size();
260  maps.emplace_back();
261  if (maps.at(p).initialize(fontData, begin) < 0) {
262  return -1;
263  }
264 
265  return p;
266 }
267 
268 int FontTTF::getQuad(int c, float* xpos, float* ypos, stbtt_aligned_quad* quad) {
269  if (c < 0) {
271  c += 128;
272  }
273 
274  int map = charIsMapped(c);
275  assert(map >= 0);
276  maps.at(map).getQuad(c, xpos, ypos, quad);
277  return maps.at(map).getTexture();
278 }
279 
void bufferData(int elem, int size, void *data)
Definition: Shader.cpp:18
FontMapTTF()
Definition: FontTTF.cpp:21
unsigned char * grayscale2rgba(unsigned char *image, unsigned int w, unsigned int h)
Definition: pixel.cpp:65
static int initialize(std::string f)
Definition: FontTTF.cpp:100
STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size, int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range)
#define MAP_NUM_CHARS
Definition: FontTTF.cpp:19
int begin
Definition: FontTTF.h:29
Included everywhere.
static unsigned int widthText(float scale, std::string s)
Definition: FontTTF.cpp:147
static LogLevel & get(int level)
Definition: Log.cpp:14
STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int width, int height, int stride_in_bytes, int padding, void *alloc_context)
TrueType Font Implementation.
static std::vector< FontMapTTF > maps
Definition: FontTTF.h:55
static unsigned char * fontData
Definition: FontTTF.h:54
Global Logging Utility.
~FontMapTTF()
Definition: FontTTF.cpp:31
Pixel buffer utilities.
static void drawTextWrapped(unsigned int x, unsigned int y, float scale, glm::vec4 color, unsigned int maxWidth, std::string s)
Definition: FontTTF.cpp:182
#define assert(x)
Definition: global.h:124
static ShaderBuffer uvBuffer
Definition: FontTTF.h:57
static unsigned int heightText(float scale, unsigned int maxWidth, std::string s)
Definition: FontTTF.cpp:165
static void drawGL(ShaderBuffer &vertices, ShaderBuffer &uvs, glm::vec4 color, unsigned int texture, TextureStorage store=TextureStorage::SYSTEM, unsigned int mode=GL_TRIANGLES, ShaderTexture *target=nullptr, Shader &shader=textShader)
Definition: Shader.cpp:278
#define LOG_INFO
Definition: Log.h:21
static const char endl
Definition: Log.h:35
int initialize(unsigned char *fontData, int firstChar)
Definition: FontTTF.cpp:41
void getQuad(int c, float *xpos, float *ypos, stbtt_aligned_quad *quad)
Definition: FontTTF.cpp:88
STBTT_DEF void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int align_to_integer)
#define LOG_ERROR
Definition: Log.h:19
static void shutdown()
Definition: FontTTF.cpp:138
STBTT_DEF void stbtt_PackEnd(stbtt_pack_context *spc)
bool contains(int c)
Definition: FontTTF.cpp:83
static ShaderBuffer vertexBuffer
Definition: FontTTF.h:56
Texture Registry.
stbtt_packedchar * charInfo
Definition: FontTTF.h:31
static void drawTextInternal(unsigned int x, unsigned int y, float scale, glm::vec4 color, unsigned int maxWidth, std::string s, bool drawWrapped)
Definition: FontTTF.cpp:187
static int charIsMapped(int c)
Definition: FontTTF.cpp:245
static const float scale
Definition: Sprite.cpp:15
STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample)
int texture
Definition: FontTTF.h:30
static void drawText(unsigned int x, unsigned int y, float scale, glm::vec4 color, std::string s)
Definition: FontTTF.cpp:160
static int loadBufferSlot(unsigned char *image=nullptr, unsigned int width=256, unsigned int height=256, ColorMode mode=ColorMode::RGBA, unsigned int bpp=32, TextureStorage s=TextureStorage::GAME, int slot=-1, bool filter=true)
Loads Buffer as texture.
static int getQuad(int c, float *xpos, float *ypos, stbtt_aligned_quad *quad)
Definition: FontTTF.cpp:268
#define MAP_WIDTH
Definition: FontTTF.cpp:16
#define FONT_SIZE
Definition: FontTTF.cpp:18
#define MAP_HEIGHT
Definition: FontTTF.cpp:17