121{
122 boost::filesystem::path path{ pathToFont };
123 if( boost::filesystem::is_regular_file(pathToFont) )
124 {
125 std::cout << "Font file found." << std::endl;
126 }
127 else
128 {
129 std::cout << "Font file not found." << std::endl;
130 }
131
132 FT_Error error = FT_Err_Ok;
133 FT_Library library = 0;
134 FT_Face face = 0;
135
136 error = FT_Init_FreeType( &library );
137 if( error )
138 {
139 throw std::runtime_error{ "Failed to initialize FreeType library!" };
140 }
141
142 error = FT_New_Face( library, pathToFont.c_str(), 0, &face);
143 if( error )
144 {
145 throw std::runtime_error{ "Failed to create new face!" };
146 }
147
148 error = FT_Set_Char_Size(face, 10 << 6, 10 << 6, 90, 90);
149 if( error )
150 {
151 throw std::runtime_error{ "Failed to set char size!" };
152 }
153
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);
157 if( error )
158 {
159 throw std::runtime_error{"Failed to load glyph!"};
160 }
161
162 error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
163 if( error )
164 {
165 throw std::runtime_error{ "Failed to render glyph!" };
166 }
167
168 std::vector<unsigned int> buffer;
169 for( int i = 0; i < (face->glyph->bitmap.width * face->glyph->bitmap.rows); ++i )
170 {
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]));
175 }
176
177 ImageData imageData(face->glyph->bitmap.width, face->glyph->bitmap.rows, buffer);
178
179 FT_Done_Face(face);
180 FT_Done_FreeType(library);
181 return imageData;
182}