More code clean up with doxygen for freetype related file

This commit is contained in:
Benau
2015-10-10 12:51:01 +08:00
parent a7b2c8838f
commit 9fe84d3dd4
10 changed files with 180 additions and 132 deletions

View File

@@ -67,7 +67,11 @@ scene::IMesh* STKTextBillboard::getTextMesh(core::stringw text, gui::ScalableFon
font->doDraw(text, core::rect<s32>(0, 0, size.Width, size.Height), video::SColor(255,255,255,255),
false, false, NULL, this);
const float scale = (font->m_isTTF ? 0.03f : 0.018f); //Larger for ttf font as they are less bold
#ifdef ENABLE_FREETYPE
const float scale = 0.03f; //Larger for ttf font as they are less bold
#else
const float scale = 0.018f;
#endif // ENABLE_FREETYPE
//scene::SMesh* mesh = new scene::SMesh();
std::map<video::ITexture*, scene::SMeshBuffer*> buffers;

View File

@@ -689,7 +689,7 @@ namespace GUIEngine
IGUIEnvironment* g_env;
Skin* g_skin = NULL;
#ifdef ENABLE_FREETYPE
Ft_Env* g_ft_env = NULL;
FTEnvironment* g_ft_env = NULL;
GlyphPageCreator* g_gp_creator = NULL;
#endif // ENABLE_FREETYPE
ScalableFont *g_font;
@@ -957,7 +957,7 @@ namespace GUIEngine
g_skin = NULL;
#ifdef ENABLE_FREETYPE
g_ft_env->~Ft_Env();
g_ft_env->~FTEnvironment();
g_ft_env = NULL;
g_gp_creator->~GlyphPageCreator();
g_gp_creator = NULL;
@@ -1032,7 +1032,7 @@ namespace GUIEngine
}
#ifdef ENABLE_FREETYPE
g_ft_env = new Ft_Env();
g_ft_env = new FTEnvironment();
g_gp_creator = new GlyphPageCreator();
#endif // ENABLE_FREETYPE

View File

@@ -40,7 +40,7 @@ namespace irr
#include "utils/ptr_vector.hpp"
#ifdef ENABLE_FREETYPE
#include "guiengine/freetype_environment.hpp"
#include "guiengine/ft_environment.hpp"
#include "guiengine/glyph_page_creator.hpp"
#endif // ENABLE_FREETYPE
@@ -87,8 +87,8 @@ namespace GUIEngine
extern irr::gui::IGUIEnvironment* g_env;
extern Skin* g_skin;
#ifdef ENABLE_FREETYPE
extern irr::gui::Ft_Env* g_ft_env;
extern irr::gui::GlyphPageCreator* g_gp_creator;
extern FTEnvironment* g_ft_env;
extern GlyphPageCreator* g_gp_creator;
#endif // ENABLE_FREETYPE
extern irr::gui::ScalableFont* g_small_font;
extern irr::gui::ScalableFont* g_font;
@@ -186,13 +186,13 @@ namespace GUIEngine
* \pre GUIEngine::init must have been called first
* \return the freetype and library with face
*/
inline irr::gui::Ft_Env* get_Freetype() { return Private::g_ft_env; }
inline FTEnvironment* getFreetype() { return Private::g_ft_env; }
/**
* \pre GUIEngine::init must have been called first
* \return the glyph page creator, useful to create a glyph page from individual char
*/
inline irr::gui::GlyphPageCreator* get_GP_Creator() { return Private::g_gp_creator; }
inline GlyphPageCreator* getGlyphPageCreator() { return Private::g_gp_creator; }
#endif // ENABLE_FREETYPE
Screen* getScreenNamed(const char* name);

View File

@@ -16,83 +16,82 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifdef ENABLE_FREETYPE
#include "guiengine/freetype_environment.hpp"
#include "guiengine/ft_environment.hpp"
#include "guiengine/get_font_properties.hpp"
#include "io/file_manager.hpp"
#include "utils/log.hpp"
namespace irr
{
namespace gui
using namespace gui;
namespace GUIEngine
{
Ft_Env::Ft_Env()
FTEnvironment::FTEnvironment()
{
Ft_Env::ft_err += FT_Init_FreeType(&(Ft_Env::ft_lib));
FTEnvironment::ft_err += FT_Init_FreeType(&(FTEnvironment::ft_lib));
loadFont();
}
Ft_Env::~Ft_Env()
FTEnvironment::~FTEnvironment()
{
for (int i = 0; i < FONTNUM; ++i)
Ft_Env::ft_err += FT_Done_Face((Ft_Env::ft_face[i]));
FTEnvironment::ft_err += FT_Done_Face((FTEnvironment::ft_face[i]));
Ft_Env::ft_err += FT_Done_FreeType(Ft_Env::ft_lib);
FTEnvironment::ft_err += FT_Done_FreeType(FTEnvironment::ft_lib);
if (Ft_Env::ft_err > 0)
if (FTEnvironment::ft_err > 0)
Log::error("Freetype Environment", "Can't destroy all fonts.");
else
Log::info("Freetype Environment", "Successfully destroy all fonts.");
}
void Ft_Env::loadFont()
void FTEnvironment::loadFont()
{
Ft_Env::ft_err += FT_New_Face(Ft_Env::ft_lib, (file_manager->getAssetChecked
FTEnvironment::ft_err += FT_New_Face(FTEnvironment::ft_lib, (file_manager->getAssetChecked
(FileManager::TTF, "FreeSans.ttf",true)).c_str(),
0, &(Ft_Env::ft_face[F_DEFAULT]));
0, &(FTEnvironment::ft_face[F_DEFAULT]));
Ft_Env::ft_err += FT_New_Face(Ft_Env::ft_lib, (file_manager->getAssetChecked
FTEnvironment::ft_err += FT_New_Face(FTEnvironment::ft_lib, (file_manager->getAssetChecked
(FileManager::TTF, "wqy-microhei.ttf",true)).c_str(),
0, &(Ft_Env::ft_face[F_CJK]));
0, &(FTEnvironment::ft_face[F_CJK]));
Ft_Env::ft_err += FT_New_Face(Ft_Env::ft_lib, (file_manager->getAssetChecked
FTEnvironment::ft_err += FT_New_Face(FTEnvironment::ft_lib, (file_manager->getAssetChecked
(FileManager::TTF, "amiri-bold.ttf",true)).c_str(),
0, &(Ft_Env::ft_face[F_AR]));
0, &(FTEnvironment::ft_face[F_AR]));
Ft_Env::ft_err += FT_New_Face(Ft_Env::ft_lib, (file_manager->getAssetChecked
FTEnvironment::ft_err += FT_New_Face(FTEnvironment::ft_lib, (file_manager->getAssetChecked
(FileManager::TTF, "Layne_Hansom.ttf",true)).c_str(),
0, &(Ft_Env::ft_face[F_LAYNE])); //to be removed?
0, &(FTEnvironment::ft_face[F_LAYNE])); //to be removed?
Ft_Env::ft_err += FT_New_Face(Ft_Env::ft_lib, (file_manager->getAssetChecked
FTEnvironment::ft_err += FT_New_Face(FTEnvironment::ft_lib, (file_manager->getAssetChecked
(FileManager::TTF, "FreeSansBold.ttf",true)).c_str(),
0, &(Ft_Env::ft_face[F_BOLD]));
0, &(FTEnvironment::ft_face[F_BOLD]));
Ft_Env::ft_err += FT_New_Face(Ft_Env::ft_lib, (file_manager->getAssetChecked
FTEnvironment::ft_err += FT_New_Face(FTEnvironment::ft_lib, (file_manager->getAssetChecked
(FileManager::TTF, "FreeMonoBold.ttf",true)).c_str(),
0, &(Ft_Env::ft_face[F_DIGIT]));
0, &(FTEnvironment::ft_face[F_DIGIT]));
//Set charmap
for (int h = 0; h < FONTNUM; ++h)
{
for (int i = 0; i < Ft_Env::ft_face[h]->num_charmaps; ++i)
for (int i = 0; i < FTEnvironment::ft_face[h]->num_charmaps; ++i)
{
FT_UShort pid = Ft_Env::ft_face[h]->charmaps[i]->platform_id;
FT_UShort eid = Ft_Env::ft_face[h]->charmaps[i]->encoding_id;
FT_UShort pid = FTEnvironment::ft_face[h]->charmaps[i]->platform_id;
FT_UShort eid = FTEnvironment::ft_face[h]->charmaps[i]->encoding_id;
if (((pid == 0) && (eid == 3)) || ((pid == 3) && (eid == 1)))
Ft_Env::ft_err += FT_Set_Charmap(Ft_Env::ft_face[h], Ft_Env::ft_face[h]->charmaps[i]);
FTEnvironment::ft_err += FT_Set_Charmap(FTEnvironment::ft_face[h], FTEnvironment::ft_face[h]->charmaps[i]);
}
}
if (Ft_Env::ft_err > 0)
if (FTEnvironment::ft_err > 0)
Log::error("Freetype Environment", "Can't load all fonts.");
else
Log::info("Freetype Environment", "Successfully loaded all fonts.");
}
FT_Library Ft_Env::ft_lib = NULL;
FT_Error Ft_Env::ft_err = 0;
FT_Library FTEnvironment::ft_lib = NULL;
FT_Error FTEnvironment::ft_err = 0;
} // end namespace gui
} // end namespace irr
} // guiengine
#endif // ENABLE_FREETYPE

View File

@@ -20,23 +20,28 @@
#define FONTNUM 6
namespace irr
{
namespace gui
/**
* \ingroup guiengine
*/
namespace GUIEngine
{
/**
* \brief Initialize a freetype environment with a single freetype library.
*/
class FTEnvironment
{
public:
FTEnvironment();
~FTEnvironment();
FT_Face ft_face[FONTNUM];
class Ft_Env
{
public:
Ft_Env();
~Ft_Env();
FT_Face ft_face[FONTNUM];
private:
/** Load font face into memory, but don't create glyph yet.
*/
void loadFont();
private:
void loadFont();
static FT_Library ft_lib;
static FT_Error ft_err;
};
static FT_Library ft_lib;
static FT_Error ft_err;
};
} // end namespace gui
} // end namespace irr
} // guiengine

View File

@@ -30,6 +30,13 @@ enum TTFLoadingType {T_NORMAL, T_DIGIT, T_BOLD};
class getFontProperties
{
public:
/** Get properties used for load characters with ttf.
* \param langname The current gui language.
* \param type Current loaded font type (normal, bold or digit font).
* \param fu Give a suitable font file.
* \return Font dpi and characters required to preload for current gui language.
*/
getFontProperties (const core::stringc &langname, TTFLoadingType type, FontUse &fu);
unsigned short size;
@@ -40,6 +47,8 @@ private:
void loadNumber(float);
void loadBoldChar(float);
/** Find a suitable font scale base on current resolution
*/
void findScale();
float normal_text_scale;

View File

@@ -25,9 +25,7 @@
#include <irrlicht.h>
#include "guiengine/engine.hpp"
namespace irr
{
namespace gui
namespace GUIEngine
{
GlyphPageCreator::GlyphPageCreator()
@@ -69,7 +67,7 @@ void GlyphPageCreator::clearGlyphPage()
void GlyphPageCreator::createNewGlyphPage()
{
//Clean the current glyph page by filling it with transparent content
page->fill(video::SColor(0, 0,0,0));
page->fill(video::SColor(0, 255, 255, 255));
}
video::IImage* GlyphPageCreator::getPage()
@@ -131,6 +129,7 @@ bool GlyphPageCreator::insertGlyph(FT_Bitmap bits, core::rect<s32>& rect)
//Store the rectangle of current glyph
rect = core::rect<s32> (used_width, used_height, used_width + bits.width, used_height + bits.rows);
image->drop();
image = 0;
//Store used area
@@ -145,6 +144,5 @@ u32 GlyphPageCreator::used_height = 0;
u32 GlyphPageCreator::temp_height = 0;
video::IImage* GlyphPageCreator::page = 0;
} // end namespace gui
} // end namespace irr
} // guiengine
#endif // ENABLE_FREETYPE

View File

@@ -20,31 +20,66 @@
#include <ft2build.h>
#include FT_FREETYPE_H
namespace irr
{
namespace gui
using namespace irr;
/**
* \ingroup guiengine
*/
namespace GUIEngine
{
/**
* \brief Create glyph pages for different fonts.
*/
class GlyphPageCreator
{
public:
GlyphPageCreator();
~GlyphPageCreator();
class GlyphPageCreator
{
public:
GlyphPageCreator();
~GlyphPageCreator();
/** Write the current glyph page into png on current running directory.
* Mainly for debug use.
* \param fn The file name.
*/
static void dumpGlyphPage(const core::stringc fn);
static void dumpGlyphPage(const core::stringc);
static bool checkEnoughSpace(FT_Bitmap);
static void clearGlyphPage();
static void createNewGlyphPage();
static video::IImage* getPage();
bool insertGlyph(FT_Bitmap, core::rect<s32>&);
/** Check whether it is ok the fit the inputted glyph into the current glyph page.
* \param bits The Glyph bitmap inputted.
* \return True if there is enough space.
*/
static bool checkEnoughSpace(FT_Bitmap bits);
private:
video::IImage* image = 0;
static video::IImage* page;
static u32 temp_height;
static u32 used_width;
static u32 used_height;
};
/** Reset position of glyph on the current glyph page.
*/
static void clearGlyphPage();
} // end namespace gui
} // end namespace irr
/** Clear (fill it with transparent content) the current glyph page.
*/
static void createNewGlyphPage();
/** Used to get a glyph page which is loaded later for texture
* \return Glyph page image.
*/
static video::IImage* getPage();
/** Used to insert a single glyph bitmap into the glyph page
* \param bits The Glyph bitmap inputted.
* \param rect Give the rectangle of the glyph on the page.
* \return True if a glyph is loaded.
*/
bool insertGlyph(FT_Bitmap bits, core::rect<s32>& rect);
private:
/** A temporary storage for a single glyph.
*/
video::IImage* image = 0;
/** A full glyph page.
*/
static video::IImage* page;
static u32 temp_height;
static u32 used_width;
static u32 used_height;
};
} // guiengine

View File

@@ -20,8 +20,8 @@
#include <cmath>
#include <cwctype>
#define cur_face GUIEngine::get_Freetype()->ft_face[fu]
#define gp_creator GUIEngine::get_GP_Creator()
#define cur_face GUIEngine::getFreetype()->ft_face[fu]
#define gp_creator GUIEngine::getGlyphPageCreator()
namespace irr
{
@@ -44,7 +44,6 @@ ScalableFont::ScalableFont(IGUIEnvironment *env, TTFLoadingType type)
m_scale = 1.0f;
m_is_hollow_copy = false;
m_black_border = false;
m_isTTF = true;
m_type = type;
m_shadow = false;
m_mono_space_digits = false;
@@ -55,10 +54,7 @@ ScalableFont::ScalableFont(IGUIEnvironment *env, TTFLoadingType type)
// don't grab environment, to avoid circular references
Driver = Environment->getVideoDriver();
if (!m_isTTF)
Log::fatal("ScalableFont", "Loading TTF font failed");
else
SpriteBank = Environment->addEmptySpriteBank((std::to_string(type)).c_str());
SpriteBank = Environment->addEmptySpriteBank((std::to_string(type)).c_str());
if (SpriteBank)
SpriteBank->grab();
}
@@ -110,7 +106,6 @@ ScalableFont::ScalableFont(IGUIEnvironment *env, const std::string &filename)
m_scale = 1.0f;
m_is_hollow_copy = false;
m_black_border = false;
m_isTTF = false;
m_shadow = false;
m_mono_space_digits = false;
m_rtl = translations->isRTLLanguage();
@@ -361,7 +356,7 @@ bool ScalableFont::load(io::IXMLReader* xml)
#endif // ENABLE_FREETYPE
#ifdef ENABLE_FREETYPE
//! loads a font file from a TTF file
//! loads a font from a TTF file
bool ScalableFont::loadTTF()
{
if (!SpriteBank)
@@ -433,7 +428,7 @@ bool ScalableFont::loadTTF()
// Convert to an anti-aliased bitmap
FT_Bitmap bits = slot->bitmap;
CharacterMap[*it] = SpriteBank->getSprites().size();
CharacterMap[*it] = SpriteBank->getSprites().size();
if (!gp_creator->checkEnoughSpace(bits))
// Glyph page is full, save current one and reset the current page
@@ -479,14 +474,14 @@ bool ScalableFont::loadTTF()
//Fix unused glyphs....
if (m_type == T_NORMAL || T_BOLD)
{
CharacterMap[(wchar_t)32] = getAreaIDFromCharacter((wchar_t)160, NULL); //Use non-breaking space glyph to all space/tab characters.
CharacterMap[(wchar_t)9] = getAreaIDFromCharacter((wchar_t)160, NULL);
CharacterMap[(wchar_t)32] = getAreaIDFromCharacter((wchar_t)160, NULL); //Use non-breaking space glyph to all space/tab characters.
CharacterMap[(wchar_t)9] = getAreaIDFromCharacter((wchar_t)160, NULL);
CharacterMap[(wchar_t)173] = 0; //Don't need a glyph for the soft hypen, as it only print when not having enough space.
//And then it will convert to a "-".
if (m_type == T_NORMAL)
{
CharacterMap[(wchar_t)8204] = 0; //They are zero width chars found in Arabic.
CharacterMap[(wchar_t)8204] = 0; //They are zero width chars found in Arabic.
CharacterMap[(wchar_t)65279] = 0;
}
}
@@ -516,13 +511,13 @@ bool ScalableFont::loadTTF()
//characters close to the bottom line too, than only one offsety is needed.
if (!n) //Skip soft hypen and space
a.bearingx = 0;
a.bearingx = 0;
else
a.bearingx = bx.at(n);
a.bearingx = bx.at(n);
if (!n) //Skip soft hypen and space
a.width = 0;
a.width = 0;
else
a.width = advance.at(n);
a.width = advance.at(n);
// add character to font
Areas.push_back(a);
}
@@ -573,6 +568,7 @@ void ScalableFont::setScale(const float scale)
m_scale = scale;
}
#ifndef ENABLE_FREETYPE
void ScalableFont::setMaxHeight()
{
MaxHeight = 0;
@@ -591,7 +587,7 @@ void ScalableFont::setMaxHeight()
const TextureInfo& info = (*(m_texture_files.find(texID))).second;
if (info.m_exclude_from_max_height_calculation) continue;
float char_scale = (m_isTTF ? 1.0f:info.m_scale);
float char_scale = info.m_scale;
t = (int)(t*char_scale);
if (t>MaxHeight)
@@ -600,6 +596,7 @@ void ScalableFont::setMaxHeight()
MaxHeight = (int)(MaxHeight*m_scale);
}
#endif // ENABLE_FREETYPE
@@ -706,7 +703,6 @@ const ScalableFont::SFontArea &ScalableFont::getAreaFromCharacter(const wchar_t
} // getAreaFromCharacter
void ScalableFont::setInvisibleCharacters( const wchar_t *s )
{
Invisible = s;
@@ -915,17 +911,15 @@ void ScalableFont::doDraw(const core::stringw& text,
(*fallback_positions)[(*fallback_sprites)[spriteID].Frames[0].rectNumber] :
positions[sprites[spriteID].Frames[0].rectNumber]);
float char_scale;
if (!m_isTTF)
{
const TextureInfo& info = (fallback[n] ?
(*(m_fallback_font->m_texture_files.find(texID))).second :
(*(m_texture_files.find(texID))).second
);
char_scale = info.m_scale;
}
else
char_scale = 1.0f;
#ifdef ENABLE_FREETYPE
float char_scale = 1.0f;
#else
const TextureInfo& info = (fallback[n] ?
(*(m_fallback_font->m_texture_files.find(texID))).second :
(*(m_texture_files.find(texID))).second
);
float char_scale = info.m_scale;
#endif // ENABLE_FREETYPE
core::dimension2d<s32> size = source.getSize();
@@ -958,7 +952,8 @@ void ScalableFont::doDraw(const core::stringw& text,
GL32_draw2DRectangle(video::SColor(255, 255,0,0), dest,clip);
#endif
if (texture == NULL && !m_isTTF)
#ifndef ENABLE_FREETYPE
if (texture == NULL)
{
// perform lazy loading
@@ -979,6 +974,7 @@ void ScalableFont::doDraw(const core::stringw& text,
continue; // no such character
}
}
#endif // ENABLE_FREETYPE
if (m_black_border && charCollector == NULL)
{
@@ -1049,6 +1045,7 @@ void ScalableFont::doDraw(const core::stringw& text,
}
#ifndef ENABLE_FREETYPE
void ScalableFont::lazyLoadTexture(int texID)
{
Driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
@@ -1087,9 +1084,13 @@ void ScalableFont::lazyLoadTexture(int texID)
}
}
}
#endif // ENABLE_FREETYPE
int ScalableFont::getCharWidth(const SFontArea& area, const bool fallback) const
{
#ifdef ENABLE_FREETYPE
const float char_scale = 1.0f;
#else
core::array< SGUISprite >& sprites = SpriteBank->getSprites();
core::array< SGUISprite >* fallback_sprites = (m_fallback_font != NULL ?
&m_fallback_font->SpriteBank->getSprites() :
@@ -1099,18 +1100,13 @@ int ScalableFont::getCharWidth(const SFontArea& area, const bool fallback) const
(*fallback_sprites)[area.spriteno].Frames[0].textureNumber :
sprites[area.spriteno].Frames[0].textureNumber);
float char_scale;
if (!m_isTTF)
{
const TextureInfo& info = (fallback ?
(*(m_fallback_font->m_texture_files.find(texID))).second :
(*(m_texture_files.find(texID))).second
);
assert(info.m_file_name.size() > 0);
char_scale = info.m_scale;
}
else
char_scale = 1.0f;
const TextureInfo& info = (fallback ?
(*(m_fallback_font->m_texture_files.find(texID))).second :
(*(m_texture_files.find(texID))).second
);
assert(info.m_file_name.size() > 0);
const float char_scale = info.m_scale;
#endif // ENABLE_FREETYPE
//Log::info("ScalableFont", "area.spriteno = %d, char_scale = %f", area.spriteno, char_scale);

View File

@@ -55,6 +55,7 @@ class ScalableFont : public IGUIFontBitmap
bool m_mono_space_digits;
irr::video::SColor m_shadow_color;
#ifndef ENABLE_FREETYPE
struct TextureInfo
{
irr::core::stringc m_file_name;
@@ -72,6 +73,7 @@ class ScalableFont : public IGUIFontBitmap
std::map<int /* texture file ID */, TextureInfo> m_texture_files;
void doReadXmlFile(io::IXMLReader* xml);
#endif // ENABLE_FREETYPE
bool m_is_hollow_copy;
bool m_rtl;
@@ -81,7 +83,7 @@ public:
LEAK_CHECK()
bool m_black_border;
bool m_isTTF;
#ifdef ENABLE_FREETYPE
TTFLoadingType m_type;
#endif // ENABLE_FREETYPE
@@ -122,9 +124,9 @@ public:
#else
//! loads a font from an XML file
bool load(io::IXMLReader* xml);
#endif // ENABLE_FREETYPE
void lazyLoadTexture(int texID);
#endif // ENABLE_FREETYPE
//! draws an text and clips it to the specified rectangle if wanted
virtual void draw(const core::stringw& text, const core::rect<s32>& position,
@@ -186,7 +188,7 @@ private:
#ifdef ENABLE_FREETYPE
struct SFontArea
{
SFontArea() : width(0), spriteno(0), offsety(0), bearingx(0) {}
SFontArea() : width(0), spriteno(0), offsety(0), offsety_bt(0), bearingx(0) {}
s32 width;
u32 spriteno;
s32 offsety;