This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.hpp
345 lines (296 loc) · 11.9 KB
/
font.hpp
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
/**
* @file font.hpp
* @brief Font class definition and implementation
* @author Jari Ronkainen
* @version 0.5
* @date 2017-08-22
*
* Font class acts as a way to read font data from a file and retrieve glyph data as bitmap
* and metrics. Supports Freetype Fonts if MUSH_DISABLE_FREETYPE_FONTS is not defined and
* bitmap fonts if MUSH_DISABLE_BITMAP_FONTS is defined before including the file.
*
* Using freetype fonts requires FreeType2
* Using bitmap fonts requires stb_image
*/
#ifndef MUSH_FONT_HEADER
#define MUSH_FONT_HEADER
#ifndef MUSH_DISABLE_FREETYPE_FONTS
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_LCD_FILTER_H
#endif
#ifdef MUSH_BITMAP_FONTS
#endif
#include <cstdint>
#include <unordered_map>
#include <iostream>
#include "core.hpp"
#include "string.hpp"
#include "buffer.hpp"
namespace mush
{
// I don't want this piece of code to depend on any header,
// so I just duplicate it.
#ifndef MUSH_DEPENDENT_FALSE
#define MUSH_DEPENDENT_FALSE
template<typename T> struct dependent_false
{
constexpr static bool value = false;
bool operator()() { return false; }
};
#endif
}
namespace mush
{
// I wish there was opaque type for this
using FontType = uint32_t;
constexpr FontType UNKNOWN_FONT = 0x00;
constexpr FontType FREETYPE_FONT = 0x01;
constexpr FontType BITMAP_FONT = 0x02;
struct GlyphMetrics
{
int32_t left;
int32_t width;
int32_t top;
int32_t height;
int32_t advance;
int32_t vertical_advance;
};
struct Glyph
{
GlyphMetrics metrics;
Buffer bitmap;
ColourFormat format;
};
struct Freetype_Basis
{
#ifndef MUSH_DISABLE_FREETYPE_FONTS
static FT_Library library;
static uint32_t l_count;
#endif
};
template <ColourFormat Format, FontType T>
class Font : Freetype_Basis
{
protected:
std::unordered_map<char32_t, Glyph> cache;
mush::Buffer font_data;
public:
mush::String prefix;
int32_t line_spacing;
int32_t space_length;
const uint32_t pixel_size;
#ifndef MUSH_DISABLE_FREETYPE_FONTS
FT_Face face;
#endif
Font(const mush::String& in_prefix,
uint32_t in_size,
Buffer data,
mush::String load_chars
) : pixel_size(in_size)
{
// Common for all font types, incoming font data
font_data = data;
// Freetype Font specific
if constexpr(T == FREETYPE_FONT)
{
#ifndef MUSH_DISABLE_FREETYPE_FONTS
prefix = "freetype/" + in_prefix + "/" + in_size + "/";
if (l_count == 0) if (FT_Init_FreeType(&library))
assert(0 && "FreeType init failed");
l_count++;
if (FT_New_Memory_Face(library, &font_data[0], data.size(), 0, &face))
assert(0 && "Freetype error: couldn't create new face from memory buffer");
FT_Select_Charmap(face, ft_encoding_unicode);
FT_Set_Pixel_Sizes(face, 0, in_size);
line_spacing = (face->height >> 6);
FT_Load_Char(face, ' ', FT_LOAD_RENDER | FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT);
space_length = face->glyph->advance.x >> 6;
for (char32_t c : load_chars)
add_glyph(c);
#else
static_assert(dependent_false<decltype(T)>(), "Freetype fonts not available, define MUSH_FREETYPE_FONTS?");
#endif
}
// Bitmap font specific
else if constexpr (T == BITMAP_FONT)
#ifndef MUSH_DISABLE_BITMAP_FONTS
prefix = "bitmap/" + in_prefix + "/" + in_size;
#else
static_assert(dependent_false<decltype(T)>(), "Bitmap fonts not available, define MUSH_BITMAP_FONTS?");
#endif
else
prefix = in_prefix;
}
~Font()
{
if constexpr (T == FREETYPE_FONT)
{
#ifndef MUSH_DISABLE_FREETYPE_FONTS
FT_Done_Face(face);
l_count--;
if (l_count == 0)
FT_Done_FreeType(library);
#endif
}
}
Glyph& glyph(char32_t c)
{
return cache[c];
}
bool has_glyph(char32_t c)
{
return (cache.count(c) != 0);
}
bool add_glyph(char32_t c)
{
if constexpr(T == FREETYPE_FONT)
{
#ifndef MUSH_DISABLE_FREETYPE_FONTS
if (!face)
{
std::cout << "Truetype font has no face\n";
return false;
}
if (FT_Load_Char(face, c, FT_LOAD_RENDER | FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT))
{
std::cout << "FT_Load_Char failed: Can't load glyph '" << mush::String(c) << "'(" << c << ")!\n";
return false;
}
if (cache.count(c) != 0)
{
std::cout << "glyph '" << c << "' already in font cache\n";
return false;
}
cache[c].metrics.advance = face->glyph->advance.x >> 6;
cache[c].metrics.vertical_advance = 0;
cache[c].metrics.left = face->glyph->bitmap_left;
cache[c].metrics.top = face->glyph->bitmap_top;
cache[c].metrics.width = face->glyph->bitmap.width;
cache[c].metrics.height = face->glyph->bitmap.rows;
return update_glyph_data(c, face->glyph->bitmap.width, face->glyph->bitmap.rows, 1, face->glyph->bitmap.buffer);
#else
static_assert(dependent_false<decltype(T)>(), "Freetype fonts not available, define MUSH_FREETYPE_FONTS when building?");
#endif
}
else if constexpr(T == BITMAP_FONT)
{
static_assert(dependent_false<decltype(T)>(), "Loading bitmap fonts not implemented");
}
else
{
static_assert(dependent_false<decltype(T)>(), "Unknown font format");
}
return true;
}
bool update_glyph_data(char32_t c, int w, int h, int ch, void* data)
{
if (ch == 0)
{
std::cout << " FAILED: 0-size image data\n";
return false;
}
if (w * h == 0)
{
std::cout << " Can't read data for glyph " << prefix + c << ":";
std::cout << " FAILED: undimensioned image data (no glyph available)\n";
cache.erase(c);
return false;
}
Buffer bmdata;
if constexpr(Format == RGBA)
{
// we can take in either monochrome, RGB or RGBA
for (int row = 0; row < h; ++row)
for (int col = 0; col < w; ++col)
{
if (ch == 1)
{
bmdata.push_back(0xff);
bmdata.push_back(0xff);
bmdata.push_back(0xff);
bmdata.push_back(*((uint8_t*)(data) + col + row * w));
}
else if (ch == 3)
{
bmdata.push_back(*((uint8_t*)(data) + 0 + col * 3 + row * w));
bmdata.push_back(*((uint8_t*)(data) + 1 + col * 3 + row * w));
bmdata.push_back(*((uint8_t*)(data) + 2 + col * 3 + row * w));
bmdata.push_back(0xff);
}
else if (ch == 4)
{
bmdata.push_back(*((uint8_t*)(data) + 0 + col * 4 + row * w));
bmdata.push_back(*((uint8_t*)(data) + 1 + col * 4 + row * w));
bmdata.push_back(*((uint8_t*)(data) + 2 + col * 4 + row * w));
bmdata.push_back(*((uint8_t*)(data) + 3 + col * 4 + row * w));
}
else
return false;
}
}
else if constexpr(Format == ALPHA)
{
// accept only 1-channel source data
if (ch != 1) return false;
for (int row = 0; row < h; ++row)
for (int col = 0; col < w; ++col)
{
bmdata.push_back(*((uint8_t*)(data) + col + row * w));
}
}
else if constexpr(Format == PALETTE_ALPHA)
{
// we want either 1 or 2 channels with palette_alpha
for (int row = 0; row < h; ++row)
for (int col = 0; col < w; ++col)
{
if (ch == 1)
{
bmdata.push_back(0xff);
bmdata.push_back(*((uint8_t*)(data) + 1 + col * 2 + row * w));
}
else if (ch == 2)
{
bmdata.push_back(*((uint8_t*)(data) + 0 + col * 2 + row * w));
bmdata.push_back(*((uint8_t*)(data) + 1 + col * 2 + row * w));
}
else
return false;
}
}
else
return false;
cache[c].bitmap = bmdata;
cache[c].format = Format;
return true;
}
};
#ifndef MUSH_DISABLE_FREETYPE_FONTS
FT_Library Freetype_Basis::library;
uint32_t Freetype_Basis::l_count;
//FT_Face Freetype_Basis::face;
template <ColourFormat Fmt = RGBA>
static mush::Font<Fmt, FREETYPE_FONT> load_freetype(const char* file, uint32_t size)
{
return mush::Font<Fmt, FREETYPE_FONT>(file, size, file_to_buffer(file), "1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZzÅåÄäÖö.,:;-+=?!_*\"$£€<>()'\\");
}
#endif
}
#endif
/*
Copyright (c) 2018 Jari Ronkainen
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/