-
Notifications
You must be signed in to change notification settings - Fork 5
/
font.cpp
366 lines (292 loc) · 10.7 KB
/
font.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
Copyright (C) 2008 Rian Shelley
Copyright (C) 2011 Kai Sterker <kai.sterker@gmail.com>
Part of the Adonthell Project http://adonthell.linuxgames.com
Adonthell is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Adonthell is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Adonthell; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* @file gui/font.cpp
* @author Rian Shelley
* @author Kai Sterker
* @brief Implements the font class.
*/
#include <string>
#include <sstream>
#include <ctype.h>
#include "font.h"
#include "fontcache.h"
#include <adonthell/base/base.h>
#include <adonthell/base/endians.h>
#include <adonthell/base/logging.h>
#include <adonthell/base/utf8.h>
#include <adonthell/gfx/gfx.h>
using std::string;
namespace gui
{
FT_Library library;
font_cache font::FontCache;
// ctor
font::font(const char* path, int size)
{
Face = NULL;
//check for the path
const char* defont = "gfx/gui/Vera.ttf";
if (!path)
{
path = defont;
}
std::string fullpath (path);
if (!base::Paths().find_in_path (fullpath))
{
Error = true;
LOG(ERROR) << logging::indent() << "Unable to find font '" << path << "'";
return;
}
// make sure the library is initialized
ref (true);
// load font
if ((Error = FT_New_Face(library, fullpath.c_str(), 0, &Face)))
{
LOG(ERROR) << logging::indent() << "Unable to load font '" << path << "'";
}
else
{
set_size (size);
Name = path;
}
Color = 0xffffffff; //set to white
}
font::~font()
{
if (Face) FT_Done_Face (Face);
ref(false);
}
void font::ref(bool addref)
{
static int refcount = 0;
if (addref)
{
if (!refcount)
{
if ((Error = FT_Init_FreeType(&library)))
{
LOG(ERROR) << logging::indent() << "Unable to initialize the freetype library";
return;
}
}
refcount++;
}
else
{
refcount--;
if (!refcount)
{
FT_Done_FreeType(library);
}
}
}
void font::set_size(int size)
{
FontSize = size;
if ((Error = FT_Set_Pixel_Sizes (Face, 0, FontSize)))
{
LOG(ERROR) << logging::indent() << "Unable to set the size of the font";
}
}
void font::draw_text(const string& s, const s_int16 & x, const s_int16 & y, const gfx::drawing_area *da, gfx::surface* target)
{
s_int16 ox = x;
u_int32 current = 0, prev = 0;
FT_Vector kerning;
string::const_iterator i;
for (i = s.begin(); i != s.end(); /* nothing */)
{
u_int32 chr = base::utf8::to_utf32 (s, i);
const glyph_info *gi = FontCache.get (chr, this);
if (FT_HAS_KERNING (Face))
{
current = FT_Get_Char_Index (Face, chr);
FT_Get_Kerning(Face, prev, current, FT_KERNING_DEFAULT, &kerning);
ox += kerning.x >> 6;
}
gi->Foreground->s->draw(ox + gi->x, y + gi->y, da, target);
ox += gi->length;
prev = current;
}
}
void font::draw_shadow(const string& s, const s_int16 & x, const s_int16 & y, const gfx::drawing_area *da, gfx::surface* target)
{
s_int16 ox = x;
u_int32 current = 0, prev = 0;
FT_Vector kerning;
string::const_iterator i;
for (i = s.begin(); i != s.end(); /* nothing */)
{
u_int32 chr = base::utf8::to_utf32 (s, i);
const glyph_info *gi = FontCache.get (chr, this);
if (FT_HAS_KERNING (Face))
{
current = FT_Get_Char_Index (Face, chr);
FT_Get_Kerning(Face, prev, current, FT_KERNING_DEFAULT, &kerning);
ox += kerning.x >> 6;
}
gi->Background->s->draw(ox + gi->x - 3, y + gi->y - 3, da, target);
ox += gi->length;
prev = current;
}
}
gui::glyph_info *font::create_glyph (const u_int32 & chr)
{
// new glyph structure
glyph_info *gi = new glyph_info();
if ((Error = FT_Load_Char(Face, chr, FT_LOAD_RENDER)))
{
LOG(ERROR) << logging::indent() << "Unable to load the glyph for character '" << chr << "'";
gi->length = 0;
gi->height = 0;
gi->x = 0;
gi->y = 0;
}
else
{
std::stringstream name (std::ios::out);
gui::color fg_color, bg_color;
bg_color.i = 0;
// the foreground glyph is always unique, so just create it
gfx::surface *foreground = gfx::create_surface();
foreground->set_alpha (255, true);
foreground->resize (Face->glyph->bitmap.width, Face->glyph->bitmap.rows);
if (foreground->length() > 0 && foreground->height() > 0)
{
foreground->unmap_color (Color, fg_color.b[2], fg_color.b[1], fg_color.b[0], fg_color.b[3]);
// TODO: big endian? foreground->unmap_color (Color, fg_color.b[0], fg_color.b[1], fg_color.b[2], fg_color.b[3]);
foreground->fillrect (0, 0, foreground->length(), foreground->height(), foreground->map_color (fg_color.b[0], fg_color.b[1], fg_color.b[2], 0));
// render foreground image
render_glyph(0, 0, &Face->glyph->bitmap, foreground, fg_color);
}
// add it to surface cache
name << std::hex << chr << "_" << fg_color.i << "_" << std::dec << Name << "_" << FontSize;
gi->Foreground = gfx::surfaces->add_surface_mem (name.str(), foreground);
// determine color to use for the background
float intensity = fg_color.b[0]*.3 + fg_color.b[1]*0.59 + fg_color.b[2]*0.11;
bg_color.b[0] = bg_color.b[1] = bg_color.b[2] = intensity < 128.0 ? 0xff : 0x00;
// a suitable background glyph may already exist, so try to get it
name.str("b_");
name << std::hex << chr << "_" << bg_color.i << "_" << std::dec << Name << "_" << FontSize;
gi->Background = gfx::surfaces->get_surface_mem (name.str());
if (gi->Background == NULL)
{
gfx::surface *background = gfx::create_surface();
background->set_alpha (255, true);
background->resize (Face->glyph->bitmap.width + 6, Face->glyph->bitmap.rows + 6);
background->fillrect(0, 0, background->length(), background->height(), background->map_color (bg_color.b[0], bg_color.b[1], bg_color.b[2], 0));
// render background image
render_glyph(3, 3, &Face->glyph->bitmap, background, bg_color);
// apply gaussian blur
background->blur();
// store in cache
gi->Background = gfx::surfaces->add_surface_mem (name.str(), background);
}
// make sure font is rendered on its proper base line
int drop = (Face->size->metrics.height >> 6) - FontSize;
gi->length = Face->glyph->advance.x >> 6;
gi->height = Face->glyph->advance.y >> 6;
gi->x = Face->glyph->bitmap_left;
gi->y = -Face->glyph->bitmap_top - drop;
}
return gi;
}
// render a single glyph to a surface
void font::render_glyph(int x, int y, FT_Bitmap* bmp, gfx::surface *surf, gui::color & col)
{
gui::color p1;
surf->lock();
unsigned char* data = bmp->buffer;
for (int j = 0; j < bmp->rows; j++)
{
for (int i = 0; i < bmp->width; i++)
{
if (*data)
{
float a2 = *data / 255.0;
p1.b[0] = (u_int8) (col.b[0] * a2);
p1.b[1] = (u_int8) (col.b[1] * a2);
p1.b[2] = (u_int8) (col.b[2] * a2);
p1.b[3] = *data;
surf->put_pix (x + i, y + j, surf->map_color (p1.b[0], p1.b[1], p1.b[2], p1.b[3]));
}
data++;
}
data += bmp->pitch;
data -= bmp->width;
}
surf->unlock();
}
// get text size with line wrapping enabled
void font::get_text_size(const string& s, const u_int16 & max_line_width, vector<textsize>& ts, u_int16 & w, u_int16 & h)
{
w = 0;
h = FontSize;
int line_width = 0;
int space_width = 0;
int max_width = 0;
// how much the font extends below the base line
int drop = (Face->size->metrics.height >> 6) - FontSize;
string::const_iterator i, last_space = s.end();
for (i = s.begin(); i != s.end(); /* nothing */)
{
u_int32 chr = base::utf8::to_utf32 (s, i);
const glyph_info *gi = FontCache.get (chr, this);
if (isspace(*i))
{
line_width = w;
last_space = i;
space_width = gi->length;
}
w += gi->length;
// exceeding maximum line width or manual line break?
if (w > max_line_width || *i == '\n')
{
// wrap at last whitespace character
ts.push_back(textsize(line_width, FontSize, last_space - s.begin()));
// calculate line widths
if (max_width < line_width) max_width = line_width;
// subtract the section, as well as the space
w = w - line_width - space_width;
// update height
h += FontSize;
}
}
// add last line
ts.push_back(textsize(w, FontSize, s.end() - s.begin()));
// final line height
h += drop;
// final text width
w = max_width > w ? max_width : w;
}
//get text size without line wrapping
void font::get_text_size(const string& s, u_int32 & w, u_int32 & h)
{
w = 0;
h = FontSize;
// how much the font extends below the base line
int drop = (Face->size->metrics.height >> 6) - FontSize;
for (string::const_iterator i = s.begin(); i != s.end(); /* nothing */)
{
u_int32 chr = base::utf8::to_utf32 (s, i);
const glyph_info *gi = FontCache.get (chr, this);
w += gi->length;
}
h += drop;
}
}