14Font::Glyph* Font::FetchGlyph(
wchar_t in) {
16 auto character = glyphs_.find(in);
17 if (character != glyphs_.end()) {
18 return character->second.get();
22 if (line_height_ == 0.F) {
24 auto character = glyphs_.find(in);
25 if (character != glyphs_.end()) {
26 return character->second.get();
34Font& Font::operator=(Font&& other)
noexcept {
35 glyphs_.insert(std::make_move_iterator(begin(other.glyphs_)),
36 std::make_move_iterator(end(other.glyphs_)));
37 filename_ = other.filename_;
38 line_height_ = other.line_height_;
39 baseline_position_ = other.baseline_position_;
43Font::Font(std::string filename,
float line_height)
44 : filename_(std::move(filename)), line_height_(line_height) {
45 std::vector<wchar_t> preloaded_characters;
46 const int character_to_preload = 256;
47 for (
wchar_t c = 0; c < character_to_preload; ++c) {
48 preloaded_characters.push_back(c);
50 LoadGlyphs(preloaded_characters);
53void Font::LoadGlyphs(
const std::vector<wchar_t>& chars) {
55 if (FT_Init_FreeType(&ft)) {
56 std::cerr <<
"SMK > FreeType: Could not init FreeType Library" << std::endl;
60 if (FT_New_Face(ft, filename_.c_str(), 0, &face)) {
61 std::cerr <<
"SMK > FreeType: Failed to load" << filename_ << std::endl;
66 ((float(face->ascender) / float(face->ascender - face->descender)));
67 FT_Set_Pixel_Sizes(face, FT_UInt(line_height_), FT_UInt(line_height_));
69 if (FT_Load_Char(face,
'X', FT_LOAD_RENDER)) {
70 std::cerr <<
"ERROR::FREETYTPE: Failed to load Glyph for file " << filename_
74 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
76 for (
auto c : chars) {
79 if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
80 std::wcout << L
"SMK > FreeType: Failed to load Glyph: \"" << c <<
"\""
85 int width = int(face->glyph->bitmap.width);
86 int height = int(face->glyph->bitmap.rows);
87 auto character = std::make_unique<Glyph>();
89 glm::ivec2(+face->glyph->bitmap_left, -face->glyph->bitmap_top);
90 const float advance_scale = 1.F / 64.F;
91 character->advance = float(face->glyph->advance.x) * advance_scale;
93 if (width * height != 0) {
94 std::vector<uint8_t> buffer_rgba(width * height * 4);
97 for (
int i = 0; i < width * height; ++i) {
98 const uint8_t v = face->glyph->bitmap.buffer[i];
99 buffer_rgba[j++] = 255;
100 buffer_rgba[j++] = 255;
101 buffer_rgba[j++] = 255;
102 buffer_rgba[j++] = v;
109 glyphs_[c] = std::move(character);
113 FT_Done_FreeType(ft);