| 465 | |
| 466 | template<bool LAMode, bool Antialias> |
| 467 | void ResourceTrueTypeFont::initialiseFreeType() |
| 468 | { |
| 469 | //-------------------------------------------------------------------// |
| 470 | // Initialise FreeType and load the font. |
| 471 | //-------------------------------------------------------------------// |
| 472 | |
| 473 | FT_Library ftLibrary; |
| 474 | |
| 475 | if (FT_Init_FreeType(&ftLibrary) != 0) |
| 476 | MYGUI_EXCEPT("ResourceTrueTypeFont: Could not init the FreeType library!"); |
| 477 | |
| 478 | uint8* fontBuffer = nullptr; |
| 479 | |
| 480 | FT_Face ftFace = loadFace(ftLibrary, fontBuffer); |
| 481 | |
| 482 | if (ftFace == nullptr) |
| 483 | { |
| 484 | MYGUI_LOG(Error, "ResourceTrueTypeFont: Could not load the font '" << getResourceName() << "'!"); |
| 485 | FT_Done_FreeType(ftLibrary); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | #ifdef MYGUI_MSDF_FONTS |
| 490 | msdfgen::FontHandle* msdfFont = nullptr; |
| 491 | |
| 492 | if (mMsdfMode) |
| 493 | { |
| 494 | msdfFont = msdfgen::adoptFreetypeFont(ftFace); |
| 495 | } |
| 496 | #endif |
| 497 | |
| 498 | //-------------------------------------------------------------------// |
| 499 | // Calculate the font metrics. |
| 500 | //-------------------------------------------------------------------// |
| 501 | |
| 502 | // The font's overall ascent and descent are defined in three different places in a TrueType font, and with different |
| 503 | // values in each place. The most reliable source for these metrics is usually the "usWinAscent" and "usWinDescent" pair of |
| 504 | // values in the OS/2 header; however, some fonts contain inaccurate data there. To be safe, we use the highest of the set |
| 505 | // of values contained in the face metrics and the two sets of values contained in the OS/2 header. |
| 506 | int fontAscent = ftFace->size->metrics.ascender >> 6; |
| 507 | int fontDescent = -ftFace->size->metrics.descender >> 6; |
| 508 | |
| 509 | TT_OS2* os2 = (TT_OS2*)FT_Get_Sfnt_Table(ftFace, ft_sfnt_os2); |
| 510 | |
| 511 | if (os2 != nullptr) |
| 512 | { |
| 513 | setMax(fontAscent, os2->usWinAscent * ftFace->size->metrics.y_ppem / ftFace->units_per_EM); |
| 514 | setMax(fontDescent, os2->usWinDescent * ftFace->size->metrics.y_ppem / ftFace->units_per_EM); |
| 515 | |
| 516 | setMax(fontAscent, os2->sTypoAscender * ftFace->size->metrics.y_ppem / ftFace->units_per_EM); |
| 517 | setMax(fontDescent, -os2->sTypoDescender * ftFace->size->metrics.y_ppem / ftFace->units_per_EM); |
| 518 | } |
| 519 | |
| 520 | // The nominal font height is calculated as the sum of its ascent and descent as specified by the font designer. Previously |
| 521 | // it was defined by MyGUI in terms of the maximum ascent and descent of the glyphs currently in use, but this caused the |
| 522 | // font's line spacing to change whenever glyphs were added to or removed from the font definition. Doing it this way |
| 523 | // instead prevents a lot of layout problems, and it is also more typographically correct and more aesthetically pleasing. |
| 524 | mDefaultHeight = fontAscent + fontDescent; |
nothing calls this directly
no test coverage detected