13 boost::filesystem::path path{ pathToFont };
14 if( boost::filesystem::is_regular_file(pathToFont) )
17 std::cout <<
"Font file found." << std::endl;
22 std::cout <<
"Font file not found." << std::endl;
28 throw std::runtime_error{
"Failed to initialize FreeType library!" };
34 throw std::runtime_error{
"Failed to create new face!" };
40 throw std::runtime_error{
"Failed to set char size!" };
43 for(
int i = 32; i < 128; i++ )
63 FT_UInt glyph_index = 0;
64 glyph_index = FT_Get_Char_Index(
mFace, (std::uint16_t)(character));
68 throw std::runtime_error{
"Failed to load glyph!" };
74 throw std::runtime_error{
"Failed to render glyph!" };
77 std::vector<unsigned int> buffer;
78 for(
int i = 0; i < (
mFace->glyph->bitmap.width *
mFace->glyph->bitmap.rows); ++i )
80 buffer.push_back((
unsigned int)(255));
81 buffer.push_back((
unsigned int)(255));
82 buffer.push_back((
unsigned int)(255));
83 buffer.push_back((
unsigned int)(
mFace->glyph->bitmap.buffer[i]));
94 std::vector<std::pair<unsigned int, ImageData>> glyphBitmaps;
95 unsigned int stringWidthPx = 0U;
96 unsigned int stringHeighPx = 0U;
97 for(
auto glypth : text )
100 auto const offsetX = stringWidthPx;
101 stringWidthPx += glBitmap.GetWidth();
102 if( glBitmap.GetHeight() > stringHeighPx )
104 stringHeighPx = glBitmap.GetHeight();
106 glyphBitmaps.push_back(std::pair<unsigned int, ImageData>(offsetX, glBitmap));
109 ImageData stringBitmap(stringWidthPx, stringHeighPx);
110 for(
auto it : glyphBitmaps )
112 unsigned int const xPos = it.first;
113 unsigned int const yPos = stringHeighPx - it.second.GetHeight();
122 boost::filesystem::path path{ pathToFont };
123 if( boost::filesystem::is_regular_file(pathToFont) )
125 std::cout <<
"Font file found." << std::endl;
129 std::cout <<
"Font file not found." << std::endl;
132 FT_Error error = FT_Err_Ok;
133 FT_Library library = 0;
136 error = FT_Init_FreeType( &library );
139 throw std::runtime_error{
"Failed to initialize FreeType library!" };
142 error = FT_New_Face( library, pathToFont.c_str(), 0, &face);
145 throw std::runtime_error{
"Failed to create new face!" };
148 error = FT_Set_Char_Size(face, 10 << 6, 10 << 6, 90, 90);
151 throw std::runtime_error{
"Failed to set char size!" };
154 FT_UInt glyph_index = 0;
155 glyph_index = FT_Get_Char_Index(face, (std::uint16_t)(character));
156 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
159 throw std::runtime_error{
"Failed to load glyph!"};
162 error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
165 throw std::runtime_error{
"Failed to render glyph!" };
168 std::vector<unsigned int> buffer;
169 for(
int i = 0; i < (face->glyph->bitmap.width * face->glyph->bitmap.rows); ++i )
171 buffer.push_back((
unsigned int)(255));
172 buffer.push_back((
unsigned int)(255));
173 buffer.push_back((
unsigned int)(255));
174 buffer.push_back((
unsigned int)(face->glyph->bitmap.buffer[i]));
177 ImageData imageData(face->glyph->bitmap.width, face->glyph->bitmap.rows, buffer);
180 FT_Done_FreeType(library);
static void DrawImageOnImageAtPos(unsigned int const x, unsigned int const y, ImageData &toImage, ImageData &fromImage)
Overlays one image on top of another at a given position.
std::map< char, ImageData > mGlyphBitmaps
std::map< char, std::pair< unsigned int, unsigned int > > mTexAtlasData
ImageData mGlyphTextureAtlas
ImageData CreateGlyphBitmap(char const character)
ImageData CreateStringBitmap(std::string text)
TextRenderer(std::string const pathToFont, unsigned int const size)
bool CreateTextureAtlas(std::map< char, std::pair< unsigned int, unsigned int > > &texAtlasData, ImageData &texAtlasImage)