Minor FreeType cleanup

This commit is contained in:
Marianne Gagnon 2015-10-19 19:25:51 -04:00
parent 0d58774b54
commit 2dfc493adf
2 changed files with 52 additions and 29 deletions

View File

@ -32,6 +32,8 @@ enum FontUse
F_DEFAULT_FALLBACK = 1, F_DEFAULT_FALLBACK = 1,
F_CJK = 2, F_CJK = 2,
F_AR = 3, F_AR = 3,
F_LAST_REGULAR_FONT = F_AR,
F_BOLD = 4, F_BOLD = 4,
F_BOLD_FALLBACK = 5, F_BOLD_FALLBACK = 5,
F_DIGIT = 6, F_DIGIT = 6,

View File

@ -20,9 +20,6 @@
#include <cmath> #include <cmath>
#include <cwctype> #include <cwctype>
#define cur_face GUIEngine::getFreetype()->ft_face[m_font_use]
#define gp_creator GUIEngine::getGlyphPageCreator()
namespace irr namespace irr
{ {
namespace gui namespace gui
@ -367,6 +364,8 @@ bool ScalableFont::loadTTF()
return false; return false;
} }
GUIEngine::GlyphPageCreator* gp_creator = GUIEngine::getGlyphPageCreator();
//Initialize glyph slot //Initialize glyph slot
FT_GlyphSlot slot; FT_GlyphSlot slot;
FT_Error err; FT_Error err;
@ -389,6 +388,8 @@ bool ScalableFont::loadTTF()
gp_creator->clearGlyphPage(); gp_creator->clearGlyphPage();
gp_creator->createNewGlyphPage(); gp_creator->createNewGlyphPage();
GUIEngine::FTEnvironment* ft_env = GUIEngine::getFreetype();
it = cur_prop.usedchar.begin(); it = cur_prop.usedchar.begin();
while (it != cur_prop.usedchar.end()) while (it != cur_prop.usedchar.end())
{ {
@ -403,50 +404,54 @@ bool ScalableFont::loadTTF()
while (count < irr::gui::F_COUNT) while (count < irr::gui::F_COUNT)
{ {
m_font_use = (FontUse)count; m_font_use = (FontUse)count;
err = FT_Set_Pixel_Sizes(cur_face, 0, m_dpi); FT_Face curr_face = ft_env->ft_face[m_font_use];
err = FT_Set_Pixel_Sizes(curr_face, 0, m_dpi);
if (err) if (err)
Log::error("ScalableFont::loadTTF", "Can't set font size."); Log::error("ScalableFont::loadTTF", "Can't set font size.");
idx = FT_Get_Char_Index(cur_face, *it); idx = FT_Get_Char_Index(curr_face, *it);
if (idx > 0) break; if (idx > 0) break;
count++; count++;
} }
} }
else else
{ {
err = FT_Set_Pixel_Sizes(cur_face, 0, m_dpi); FT_Face curr_face = ft_env->ft_face[m_font_use];
err = FT_Set_Pixel_Sizes(curr_face, 0, m_dpi);
if (err) if (err)
Log::error("ScalableFont::loadTTF", "Can't set font size."); Log::error("ScalableFont::loadTTF", "Can't set font size.");
idx = FT_Get_Char_Index(cur_face, *it); idx = FT_Get_Char_Index(curr_face, *it);
} }
slot = cur_face->glyph; FT_Face curr_face = ft_env->ft_face[m_font_use];
slot = curr_face->glyph;
if (idx) if (idx)
{ {
// Load glyph image into the slot (erase previous one) // Load glyph image into the slot (erase previous one)
err = FT_Load_Glyph(cur_face, idx, err = FT_Load_Glyph(curr_face, idx,
FT_LOAD_DEFAULT | FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL); FT_LOAD_DEFAULT | FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL);
if (err) if (err)
Log::error("ScalableFont::loadTTF", "Can't load a single glyph."); Log::error("ScalableFont::loadTTF", "Can't load a single glyph.");
// Store vertical offset on line. // Store vertical offset on line.
s32 offset_on_line = (cur_face->glyph->metrics.height >> 6) - cur_face->glyph->bitmap_top; s32 offset_on_line = (curr_face->glyph->metrics.height >> 6) - curr_face->glyph->bitmap_top;
offset.push_back(offset_on_line); offset.push_back(offset_on_line);
// This is to be used later. // This is to be used later.
t = cur_face->glyph->metrics.height >> 6; t = curr_face->glyph->metrics.height >> 6;
height.push_back(t); height.push_back(t);
if (t > current_maxheight) if (t > current_maxheight)
current_maxheight = t; current_maxheight = t;
// Store horizontal padding (bearingX). // Store horizontal padding (bearingX).
s32 bX = cur_face->glyph->bitmap_left; s32 bX = curr_face->glyph->bitmap_left;
bx.push_back(bX); bx.push_back(bX);
// Store total width on horizontal line. // Store total width on horizontal line.
s32 width = cur_face->glyph->advance.x >> 6; s32 width = curr_face->glyph->advance.x >> 6;
advance.push_back(width); advance.push_back(width);
// Convert to an anti-aliased bitmap // Convert to an anti-aliased bitmap
@ -576,17 +581,17 @@ bool ScalableFont::loadTTF()
case T_NORMAL: case T_NORMAL:
Log::info("ScalableFont::loadTTF", "Created %d glyphs " Log::info("ScalableFont::loadTTF", "Created %d glyphs "
"supporting %d characters for normal font %s at %d dpi using %d glyph page(s)." "supporting %d characters for normal font %s at %d dpi using %d glyph page(s)."
, Areas.size(), CharacterMap.size(), cur_face->family_name, m_dpi, SpriteBank->getTextureCount()); , Areas.size(), CharacterMap.size(), GUIEngine::getFreetype()->ft_face[m_font_use]->family_name, m_dpi, SpriteBank->getTextureCount());
break; break;
case T_DIGIT: case T_DIGIT:
Log::info("ScalableFont::loadTTF", "Created %d glyphs " Log::info("ScalableFont::loadTTF", "Created %d glyphs "
"supporting %d characters for high-res digits font %s at %d dpi using %d glyph page(s)." "supporting %d characters for high-res digits font %s at %d dpi using %d glyph page(s)."
, Areas.size(), CharacterMap.size(), cur_face->family_name, m_dpi, SpriteBank->getTextureCount()); , Areas.size(), CharacterMap.size(), GUIEngine::getFreetype()->ft_face[m_font_use]->family_name, m_dpi, SpriteBank->getTextureCount());
break; break;
case T_BOLD: case T_BOLD:
Log::info("ScalableFont::loadTTF", "Created %d glyphs " Log::info("ScalableFont::loadTTF", "Created %d glyphs "
"supporting %d characters for bold title font %s at %d dpi using %d glyph page(s)." "supporting %d characters for bold title font %s at %d dpi using %d glyph page(s)."
, Areas.size(), CharacterMap.size(), cur_face->family_name, m_dpi, SpriteBank->getTextureCount()); , Areas.size(), CharacterMap.size(), GUIEngine::getFreetype()->ft_face[m_font_use]->family_name, m_dpi, SpriteBank->getTextureCount());
break; break;
} }
@ -602,6 +607,9 @@ bool ScalableFont::lazyLoadChar()
FT_GlyphSlot slot; FT_GlyphSlot slot;
FT_Error err; FT_Error err;
GUIEngine::FTEnvironment* ft_env = GUIEngine::getFreetype();
GUIEngine::GlyphPageCreator* gp_creator = GUIEngine::getGlyphPageCreator();
s32 height; s32 height;
s32 bX; s32 bX;
s32 offset_on_line; s32 offset_on_line;
@ -617,31 +625,38 @@ bool ScalableFont::lazyLoadChar()
//Lite-Fontconfig for stk //Lite-Fontconfig for stk
int idx; int idx;
int count = 0; int font = irr::gui::F_DEFAULT;
while (count < irr::gui::F_COUNT - 3) //Exclude bold with fallback and digit font while (font <= irr::gui::F_LAST_REGULAR_FONT)
{ {
m_font_use = (FontUse)count; m_font_use = (FontUse)font;
err = FT_Set_Pixel_Sizes(cur_face, 0, m_dpi);
FT_Face face = ft_env->ft_face[font];
err = FT_Set_Pixel_Sizes(face, 0, m_dpi);
if (err) if (err)
Log::error("ScalableFont::loadTTF", "Can't set font size."); Log::error("ScalableFont::loadTTF", "Can't set font size.");
idx = FT_Get_Char_Index(cur_face, *it); idx = FT_Get_Char_Index(face, *it);
if (idx > 0) break; if (idx > 0) break;
count++;
font++;
} }
slot = cur_face->glyph;
FT_Face curr_face = ft_env->ft_face[m_font_use];
slot = curr_face->glyph;
if (idx) if (idx)
{ {
err = FT_Load_Glyph(cur_face, idx, err = FT_Load_Glyph(curr_face, idx,
FT_LOAD_DEFAULT | FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL); FT_LOAD_DEFAULT | FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL);
if (err) if (err)
Log::error("ScalableFont::loadTTF", "Can't load a single glyph."); Log::error("ScalableFont::loadTTF", "Can't load a single glyph.");
offset_on_line = (cur_face->glyph->metrics.height >> 6) - cur_face->glyph->bitmap_top; offset_on_line = (curr_face->glyph->metrics.height >> 6) - curr_face->glyph->bitmap_top;
height = cur_face->glyph->metrics.height >> 6; height = curr_face->glyph->metrics.height >> 6;
bX = cur_face->glyph->bitmap_left; bX = curr_face->glyph->bitmap_left;
width = cur_face->glyph->advance.x >> 6; width = curr_face->glyph->advance.x >> 6;
FT_Bitmap bits = slot->bitmap; FT_Bitmap bits = slot->bitmap;
CharacterMap[*it] = SpriteBank->getSprites().size(); CharacterMap[*it] = SpriteBank->getSprites().size();
@ -698,7 +713,7 @@ bool ScalableFont::lazyLoadChar()
Log::debug("ScalableFont::lazyLoadChar", "New characters drawn by %s inserted, there are %d glyphs " Log::debug("ScalableFont::lazyLoadChar", "New characters drawn by %s inserted, there are %d glyphs "
"supporting %d characters for normal font at %d dpi using %d glyph page(s) now." "supporting %d characters for normal font at %d dpi using %d glyph page(s) now."
, cur_face->family_name, Areas.size(), CharacterMap.size(), m_dpi, SpriteBank->getTextureCount()); , GUIEngine::getFreetype()->ft_face[m_font_use]->family_name, Areas.size(), CharacterMap.size(), m_dpi, SpriteBank->getTextureCount());
return true; return true;
} }
@ -706,6 +721,8 @@ bool ScalableFont::lazyLoadChar()
//! force create a new texture (glyph) page in a font //! force create a new texture (glyph) page in a font
void ScalableFont::forceNewPage() void ScalableFont::forceNewPage()
{ {
GUIEngine::GlyphPageCreator* gp_creator = GUIEngine::getGlyphPageCreator();
SpriteBank->setTexture(SpriteBank->getTextureCount() - 1, Driver->addTexture("Glyph_page", gp_creator->getPage())); SpriteBank->setTexture(SpriteBank->getTextureCount() - 1, Driver->addTexture("Glyph_page", gp_creator->getPage()));
gp_creator->clearGlyphPage(); gp_creator->clearGlyphPage();
SpriteBank->addTexture(NULL); SpriteBank->addTexture(NULL);
@ -864,6 +881,8 @@ void ScalableFont::setInvisibleCharacters( const wchar_t *s )
core::dimension2d<u32> ScalableFont::getDimension(const wchar_t* text) const core::dimension2d<u32> ScalableFont::getDimension(const wchar_t* text) const
{ {
#ifdef ENABLE_FREETYPE #ifdef ENABLE_FREETYPE
GUIEngine::GlyphPageCreator* gp_creator = GUIEngine::getGlyphPageCreator();
if (m_type == T_NORMAL || T_BOLD) //lazy load char if (m_type == T_NORMAL || T_BOLD) //lazy load char
{ {
for (const wchar_t* p = text; *p; ++p) for (const wchar_t* p = text; *p; ++p)
@ -956,6 +975,8 @@ void ScalableFont::doDraw(const core::stringw& text,
{ {
if (!Driver) return; if (!Driver) return;
GUIEngine::GlyphPageCreator* gp_creator = GUIEngine::getGlyphPageCreator();
if (m_shadow) if (m_shadow)
{ {
m_shadow = false; // avoid infinite recursion m_shadow = false; // avoid infinite recursion