--------------------------------- TextureAsset::LoadTtf Rasterizes a ttf font into a SDF texture and generates SpriteFont data from it
| 157 | // Rasterizes a ttf font into a SDF texture and generates SpriteFont data from it |
| 158 | // |
| 159 | SpriteFont* FontAsset::LoadTtf(const std::vector<uint8>& binaryContent) |
| 160 | { |
| 161 | FT_Library ft; |
| 162 | if (FT_Init_FreeType(&ft)) |
| 163 | LOG("FREETYPE: Could not init FreeType Library", core::LogLevel::Warning); |
| 164 | |
| 165 | FT_Face face; |
| 166 | if (FT_New_Memory_Face(ft, binaryContent.data(), (FT_Long)binaryContent.size(), 0, &face)) |
| 167 | LOG("FREETYPE: Failed to load font", core::LogLevel::Warning); |
| 168 | |
| 169 | FT_Set_Pixel_Sizes(face, 0, m_FontSize); |
| 170 | |
| 171 | SpriteFont* pFont = new SpriteFont(); |
| 172 | pFont->m_FontSize = (int16)m_FontSize; |
| 173 | pFont->m_FontName = std::string(face->family_name) + " - " + face->style_name; |
| 174 | pFont->m_CharacterCount = face->num_glyphs; |
| 175 | |
| 176 | pFont->m_UseKerning = FT_HAS_KERNING(face) != 0; |
| 177 | |
| 178 | //Atlasing helper variables |
| 179 | ivec2 startPos[4] = { ivec2(0), ivec2(0), ivec2(0), ivec2(0) }; |
| 180 | ivec2 maxPos[4] = { ivec2(0), ivec2(0), ivec2(0), ivec2(0) }; |
| 181 | bool horizontal = false;//Direction this pass expands the map in (internal moves are !horizontal) |
| 182 | uint32 posCount = 1;//internal move count in this pass |
| 183 | uint32 curPos = 0;//internal move count |
| 184 | uint32 channel = 0;//channel to add to |
| 185 | |
| 186 | uint32 totPadding = m_Padding + m_Spread; |
| 187 | |
| 188 | //Load individual character metrics |
| 189 | std::map<int32, FontMetric*> characters; |
| 190 | for (int32 c = 0; c < SpriteFont::s_CharCount - 1; c++) |
| 191 | { |
| 192 | FontMetric* metric = &(pFont->GetMetric(static_cast<wchar_t>(c))); |
| 193 | metric->Character = static_cast<wchar_t>(c); |
| 194 | |
| 195 | uint32 glyphIdx = FT_Get_Char_Index(face, c); |
| 196 | |
| 197 | if (pFont->m_UseKerning && glyphIdx) |
| 198 | { |
| 199 | for (int32 previous = 0; previous < SpriteFont::s_CharCount - 1; previous++) |
| 200 | { |
| 201 | FT_Vector delta; |
| 202 | |
| 203 | uint32 prevIdx = FT_Get_Char_Index(face, previous); |
| 204 | FT_Get_Kerning(face, prevIdx, glyphIdx, FT_KERNING_DEFAULT, &delta); |
| 205 | |
| 206 | if (delta.x || delta.y) |
| 207 | { |
| 208 | metric->Kerning[static_cast<wchar_t>(previous)] = vec2((float)delta.x / 64.f, (float)delta.y / 64.f); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (FT_Load_Glyph(face, glyphIdx, FT_LOAD_DEFAULT)) |
| 214 | { |
| 215 | LOG("FREETYPE: Failed to load glyph", core::LogLevel::Warning); |
| 216 | continue; |
nothing calls this directly
no test coverage detected