forked from edukaj/fontdef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fontdefgenerator.cpp
306 lines (251 loc) · 8.51 KB
/
fontdefgenerator.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
#include <cmath>
#include <cstdint>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <functional>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include <FreeImage.h>
#include "fontdefgenerator.h"
using namespace std;
class ScopeExit {
public:
ScopeExit(std::function<void()> f): f_(f) {}
~ScopeExit() { f_(); }
private:
std::function<void()> f_;
};
static inline uint32_t firstPO2From(uint32_t n)
{
--n;
n |= n >> 16;
n |= n >> 8;
n |= n >> 4;
n |= n >> 2;
n |= n >> 1;
++n;
return n;
}
FontdefGenerator::FontdefGenerator(const ProgramOptions& po)
: mProgramOptions(po)
{
if ((int)po.verboseLevel() >= (int)ProgramOptions::LogLevel::MEDIUM)
po.printParameterOn(cout);
}
void FontdefGenerator::generate()
{
FT_Library ftLib = nullptr;
if (FT_Init_FreeType(&ftLib))
throw runtime_error{"unable to init FreeType"};
FT_Face face = nullptr;
if (FT_New_Face(ftLib, mProgramOptions.inputFont().c_str(), 0, &face ))
throw runtime_error{"unable to init font face"};
// Convert our point size to freetype 26.6 fixed point format
const auto ttfSize = (FT_F26Dot6)(mProgramOptions.size() * (1 << 6));
const auto ttfResolution = mProgramOptions.resolution();
if( FT_Set_Char_Size( face, ttfSize, 0, ttfResolution, ttfResolution ) )
throw std::runtime_error{"unable to set char size"};
int max_width = 0;
int max_height = 0;
int max_bearing_y = 0;
size_t glyphCount = 0;
for( const auto& codePoints : mProgramOptions.codepoints() )
{
for (int i = codePoints.first(); i <= codePoints.last(); ++i, ++glyphCount)
{
FT_Load_Char( face, i, FT_LOAD_RENDER );
if( ( 2 * ( ((int)face->glyph->bitmap.rows) << 6 ) - face->glyph->metrics.horiBearingY ) > max_height )
max_height = ( 2 * ( ((int)face->glyph->bitmap.rows) << 6 ) - face->glyph->metrics.horiBearingY );
if( face->glyph->metrics.horiBearingY > max_bearing_y )
max_bearing_y = static_cast<int>(face->glyph->metrics.horiBearingY);
if( (face->glyph->advance.x >> 6 ) + ( face->glyph->metrics.horiBearingX >> 6 ) > max_width)
max_width = (face->glyph->advance.x >> 6 ) + ( face->glyph->metrics.horiBearingX >> 6 );
}
}
const auto character_spacer = mProgramOptions.charachterSpace();
// Now work out how big our texture needs to be
size_t rawSize = (max_width + character_spacer) * ((max_height >> 6) + character_spacer) * glyphCount;
uint32_t tex_side = static_cast<uint32_t>(std::sqrt((Real)rawSize));
// just in case the size might chop a glyph in half, add another glyph width/height
tex_side += std::max(max_width, (max_height>>6));
uint32_t roundUpSize = firstPO2From(tex_side);
// Would we benefit from using a non-square texture (2X width)
uint32_t finalWidth, finalHeight;
if (roundUpSize * roundUpSize * 0.5 >= rawSize)
finalHeight = static_cast<uint32_t>(roundUpSize * 0.5);
else
finalHeight = roundUpSize;
finalWidth = roundUpSize;
if ((int)mProgramOptions.verboseLevel() >= (int)ProgramOptions::LogLevel::HIGH)
std::cout
<< "\nmax width: " << max_width
<< "\nmax height: " << (max_height >> 6)
<< "\nmax Y bearing: " << max_bearing_y
<< "\nraw size: " << rawSize
<< "\ntexture side: " << tex_side
<< "\nrounded to ^2: " << roundUpSize
<< "\n"
<< "\nfinal image: " << finalWidth << 'x' << finalHeight
<< '\n'
<< std::endl;
auto textureAspect = (Real)finalWidth / (Real)finalHeight;
const auto pixel_bytes = mProgramOptions.pixelSize();
size_t data_width = finalWidth * pixel_bytes;
size_t data_size = finalWidth * finalHeight * pixel_bytes;
std::vector<uint8_t> imageData(data_size, 0);
if (pixel_bytes == 2)
{
for (size_t i = 0; i < data_size; i += pixel_bytes)
imageData[i] = 0xFF; // luminance
}
size_t x = 0, y = 0;
glyphCount = 0;
for( const auto& codepoint : mProgramOptions.codepoints() )
{
for (int i = codepoint.first(); i <= codepoint.last(); ++i)
{
if(FT_Load_Char( face, i, FT_LOAD_RENDER ))
{
std::cerr << "codepoint " << i << " NOT loaded" << std::endl;
continue;
}
FT_Pos advance = face->glyph->advance.x >> 6;
const uint8_t* buffer = face->glyph->bitmap.buffer;
if (!buffer)
{
std::cerr << "WARNING: Freetype returned null for character "
<< i << " in font "
<< mProgramOptions.inputFont() << std::endl;
continue;
}
++glyphCount;
FT_Pos y_bearing = ( max_bearing_y >> 6 ) - ( face->glyph->metrics.horiBearingY >> 6 );
FT_Pos x_bearing = face->glyph->metrics.horiBearingX >> 6;
if (pixel_bytes == 2)
{
for(unsigned int j = 0; j < face->glyph->bitmap.rows; j++ )
{
size_t row = j + y + y_bearing;
uint8_t* pDest = &imageData[(row * data_width) + (x + x_bearing) * pixel_bytes];
for(unsigned int k = 0; k < face->glyph->bitmap.width; k++ )
{
if (mProgramOptions.useAntialiasColor())
*pDest++= *buffer++;
else
*pDest++= 0xFF;
// Always use the greyscale value for alpha
*pDest++= *buffer++;
}
}
}
else
{
const size_t bitmapRows = face->glyph->bitmap.rows;
const size_t bitmapWidth = face->glyph->bitmap.width;
uint8_t* pDest = &imageData[((y + y_bearing) * data_width) + (x + x_bearing) * pixel_bytes];
for(unsigned int j = 0; j < bitmapRows; j++, buffer += bitmapWidth, pDest += bitmapWidth)
{
const size_t row = j + y + y_bearing;
uint8_t* pDest = &imageData[(row * data_width) + (x + x_bearing) * pixel_bytes];
copy(buffer, buffer + bitmapWidth, pDest);
}
}
setGlyphTexCoords(i,
(Real)x / (Real)finalWidth, // u1
(Real)y / (Real)finalHeight, // v1
(Real)( x + ( face->glyph->advance.x >> 6 ) ) / (Real)finalWidth, // u2
( y + ( max_height >> 6 ) ) / (Real)finalHeight, // v2
textureAspect
);
// Advance a column
x += (advance + character_spacer);
// If at end of row
if( finalWidth - 1 < x + ( advance ) )
{
y += ( max_height >> 6 ) + character_spacer;
x = 0;
}
}
}
FT_Done_FreeType(ftLib);
saveFontImage(imageData, finalWidth, finalHeight, pixel_bytes * 8,
boost::algorithm::to_lower_copy(mProgramOptions.imageExtension()));
if ((int)mProgramOptions.verboseLevel() >= (int)ProgramOptions::LogLevel::MEDIUM)
std::cout << "\nglyph count: " << glyphCount << std::endl;
createFontDef();
}
void FontdefGenerator::setGlyphTexCoords(int id, Real u1, Real v1, Real u2, Real v2, Real textureAspect)
{
GlyphInfoMap::iterator i = mGlyphMap.find(id);
if (i != mGlyphMap.end())
{
i->second.uvRect.left = u1;
i->second.uvRect.top = v1;
i->second.uvRect.right = u2;
i->second.uvRect.bottom = v2;
i->second.aspectRatio = textureAspect * (u2 - u1) / (v2 - v1);
}
else
{
mGlyphMap.insert(GlyphInfoMap::value_type(
id, GlyphInfo(
UVRect(u1, v1, u2, v2),
textureAspect * (u2 - u1) / (v2 - v1))
)
);
}
}
void FontdefGenerator::createFontDef()
{
std::ios_base::openmode openMode = mProgramOptions.isAppend() ?
(std::ios_base::out | std::ios_base::app) :
(std::ios_base::out | std::ios_base::trunc);
std::fstream os(mProgramOptions.output(), openMode );
os << mProgramOptions.fontName()
<< "\n{\n\t"
"type image\n\t"
"source "
<< mProgramOptions.imageFilename() << "\n\n";
for(const auto& glyph : mGlyphMap)
{
const auto& glyphInfo = glyph.second;
os << "\tglyph u" << glyph.first << ' '
<< glyphInfo.uvRect.left << ' '
<< glyphInfo.uvRect.top << ' '
<< glyphInfo.uvRect.right << ' '
<< glyphInfo.uvRect.bottom << '\n';
}
os << "}\n\n" << std::flush;
}
int FontdefGenerator::extractFreeImageExtensionFrom(const std::string& ext)
{
auto freeImageExt = FIF_BMP;
if (ext == "bmp")
freeImageExt = FIF_BMP;
else if (ext == "png")
freeImageExt = FIF_PNG;
else if (ext == "jpg")
freeImageExt = FIF_JPEG;
else if (ext == "jpeg")
freeImageExt = FIF_JPEG;
else if (ext == "tga")
freeImageExt = FIF_TARGA;
else if (ext == "tiff")
freeImageExt = FIF_TIFF;
else
throw std::invalid_argument{ ext + " is an unsuppordet image extension"};
return freeImageExt;
}
void FontdefGenerator::saveFontImage(const std::vector<uint8_t>& imageData, size_t width, size_t height, int bpp, const std::string& ext)
{
FIBITMAP* dib = FreeImage_ConvertFromRawBits(const_cast<uint8_t*>(imageData.data()),
width, height, width, bpp, 0, 0, 0, TRUE);
auto freeImageExt = static_cast<FREE_IMAGE_FORMAT>(
extractFreeImageExtensionFrom(ext));
FreeImage_Save(freeImageExt, dib, mProgramOptions.imageFilename().c_str());
}