MCPcopy Create free account
hub / github.com/defold/defold / LoadTTFInternal

Function LoadTTFInternal

engine/font/src/font_ttf.cpp:215–266  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

213#endif
214
215static HFont LoadTTFInternal(const char* path, const void* buffer, uint32_t buffer_size, bool allocate)
216{
217 TTFFont* font = new TTFFont;
218 memset(font, 0, sizeof(*font));
219
220 font->m_Base.m_LoadFontFromMemory = FontLoadFromMemoryTTF;
221 font->m_Base.m_DestroyFont = FontDestroyTTF;
222 font->m_Base.m_GetResourceSize = GetResourceSizeTTF;
223 font->m_Base.m_GetScaleFromSize = GetScaleFromSizeTTF;
224 font->m_Base.m_GetAscent = GetAscentTTF;
225 font->m_Base.m_GetDescent = GetDescentTTF;
226 font->m_Base.m_GetLineGap = GetLineGapTTF;
227 font->m_Base.m_GetGlyphIndex = GetGlyphIndexTTF;
228 font->m_Base.m_GetGlyph = GetGlyphTTF;
229 font->m_Base.m_FreeGlyph = FreeGlyphTTF;
230
231 if (allocate)
232 {
233 font->m_DataSize = buffer_size;
234 font->m_Data = (const void*)malloc(buffer_size);
235 memcpy((void*)font->m_Data, buffer, buffer_size);
236 font->m_Allocated = 1;
237 }
238 else
239 {
240 font->m_Data = buffer;
241 font->m_DataSize= buffer_size;
242 }
243
244#if defined(FONT_USE_HARFBUZZ)
245 if (!LoadHBFont(font, (void*)font->m_Data, font->m_DataSize))
246 {
247 dmLogError("Failed to create Harfbuzz font from '%s'", path);
248 FontDestroyTTF((HFont)font);
249 delete font;
250 return 0;
251 }
252#endif
253
254 int index = stbtt_GetFontOffsetForIndex((const unsigned char*)font->m_Data,0);
255 int result = stbtt_InitFont(&font->m_Font, (const unsigned char*)font->m_Data, index);
256 if (!result)
257 {
258 dmLogError("Failed to load font from '%s'", path);
259 FontDestroyTTF((HFont)font);
260 delete font;
261 return 0;
262 }
263
264 stbtt_GetFontVMetrics(&font->m_Font, &font->m_Ascent, &font->m_Descent, &font->m_LineGap);
265 return (HFont)font;
266}
267
268#if defined(FONT_USE_HARFBUZZ)
269hb_font_t* FontGetHarfbuzzFontFromTTF(HFont hfont)

Callers 1

FontLoadFromMemoryTTFFunction · 0.85

Calls 6

mallocFunction · 0.85
LoadHBFontFunction · 0.85
FontDestroyTTFFunction · 0.85
stbtt_InitFontFunction · 0.85
stbtt_GetFontVMetricsFunction · 0.85

Tested by

no test coverage detected