Add lazy load char function

Now less fonts are used to prevent mismatch, also include font license file
This commit is contained in:
Benau 2015-10-17 02:33:37 +08:00
parent f557dce27e
commit d166aa620e
14 changed files with 249 additions and 49 deletions

9
data/ttf/LICENSE Normal file
View File

@ -0,0 +1,9 @@
GNU FreeFont (FreeSans, FreeSansBold, FreeMono) is released under the GPLv3
wqyMicroHei is released under the GPLv3 with font embedding exception and Apache-2.0 licenses
By Qianqian Fang and The WenQuanYi Project Contributors
Copyright (C) 2008-2009 The WenQuanYi Project Board of Trustees
Copyright (C) 2007 Google Corporation
Noto Naskh Arabic UI is released under Apache-2.0 license

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -57,13 +57,9 @@ void FTEnvironment::loadFont()
0, &(FTEnvironment::ft_face[F_CJK]));
FTEnvironment::ft_err += FT_New_Face(FTEnvironment::ft_lib, (file_manager->getAssetChecked
(FileManager::TTF, "amiri-bold.ttf",true)).c_str(),
(FileManager::TTF, "NotoNaskhArabicUI-Bold.ttf",true)).c_str(),
0, &(FTEnvironment::ft_face[F_AR]));
FTEnvironment::ft_err += FT_New_Face(FTEnvironment::ft_lib, (file_manager->getAssetChecked
(FileManager::TTF, "Layne_Hansom.ttf",true)).c_str(),
0, &(FTEnvironment::ft_face[F_LAYNE])); //to be removed?
FTEnvironment::ft_err += FT_New_Face(FTEnvironment::ft_lib, (file_manager->getAssetChecked
(FileManager::TTF, "FreeSansBold.ttf",true)).c_str(),
0, &(FTEnvironment::ft_face[F_BOLD]));

View File

@ -18,7 +18,7 @@
#include <ft2build.h>
#include FT_FREETYPE_H
#define FONTNUM 6
#define FONTNUM 5
/**
* \ingroup guiengine

View File

@ -70,29 +70,9 @@ void getFontProperties::findScale()
void getFontProperties::loadChar(const core::stringc langname, FontUse& fu, float scale)
{
//Determine which ttf file to load first
if (langname == "ar" || langname == "fa")
fu = F_AR;
fu = F_DEFAULT; //Default font file
else if (langname == "sq" || langname == "eu" || langname == "br" ||
langname == "da" || langname == "nl" || langname == "en" ||
langname == "gd" || langname == "gl" || langname == "de" ||
langname == "is" || langname == "id" || langname == "it" ||
langname == "nb" || langname == "nn" || langname == "pt" ||
langname == "es" || langname == "sv" || langname == "uz")
//They are sorted out by running fc-list :lang="name" |grep Layne
//But we may get rid of the above by using a font that suitable for most language
//Like FreeSans/FreeSerif
fu = F_LAYNE;
else if (langname == "zh" || langname == "ja" || langname == "ko")
fu = F_CJK;
else
fu = F_DEFAULT; //Default font file
usedchar = translations->getCurrentAllChar(); //Loading unique characters
for (int i = 32; i < 256; ++i)
for (int i = 32; i < 128; ++i)
usedchar.insert((wchar_t)i); //Include basic Latin too
usedchar.insert((wchar_t)160); //Non-breaking space
usedchar.insert((wchar_t)215); //Used on resolution selection screen (X).

View File

@ -23,7 +23,7 @@ namespace irr
namespace gui
{
enum FontUse {F_DEFAULT, F_CJK, F_AR, F_LAYNE, F_BOLD, F_DIGIT};
enum FontUse {F_DEFAULT, F_CJK, F_AR, F_BOLD, F_DIGIT};
enum TTFLoadingType {T_NORMAL, T_DIGIT, T_BOLD};

View File

@ -31,6 +31,7 @@ namespace GUIEngine
GlyphPageCreator::GlyphPageCreator()
{
page = GUIEngine::getDriver()->createImage(video::ECF_A8R8G8B8, core::dimension2du(512, 512));
newchar.clear();
}
GlyphPageCreator::~GlyphPageCreator()
@ -76,6 +77,15 @@ video::IImage* GlyphPageCreator::getPage()
return page;
}
core::stringw GlyphPageCreator::getNewChar()
{
core::stringw c;
for (std::set<wchar_t>::iterator it = newchar.begin(); it != newchar.end(); ++it)
c += *it;
return c;
}
bool GlyphPageCreator::insertGlyph(FT_Bitmap bits, core::rect<s32>& rect)
{
core::dimension2du d(bits.width + 1, bits.rows + 1);

View File

@ -19,6 +19,7 @@
#include <irrlicht.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <set>
using namespace irr;
@ -61,6 +62,11 @@ namespace GUIEngine
*/
video::IImage* getPage();
/** Used to get the string of new characters inside set newchar. (Mainly for debug)
* \return string of wild-character.
*/
core::stringw getNewChar();
/** 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.
@ -68,6 +74,10 @@ namespace GUIEngine
*/
bool insertGlyph(FT_Bitmap bits, core::rect<s32>& rect);
/** A temporary holder stored new char to be inserted.
*/
std::set<wchar_t> newchar;
private:
/** A temporary storage for a single glyph.
*/

View File

@ -20,7 +20,7 @@
#include <cmath>
#include <cwctype>
#define cur_face GUIEngine::getFreetype()->ft_face[fu]
#define cur_face GUIEngine::getFreetype()->ft_face[m_font_use]
#define gp_creator GUIEngine::getGlyphPageCreator()
namespace irr
@ -45,6 +45,8 @@ ScalableFont::ScalableFont(IGUIEnvironment *env, TTFLoadingType type)
m_is_hollow_copy = false;
m_black_border = false;
m_type = type;
m_font_use = (FontUse)0;
m_dpi = 0;
m_shadow = false;
m_mono_space_digits = false;
m_rtl = translations->isRTLLanguage();
@ -371,15 +373,15 @@ bool ScalableFont::loadTTF()
//Determine which font(face) and size to load,
//also get all used char base on current language settings
FontUse fu;
getFontProperties cur_prop(translations->getCurrentLanguageNameCode().c_str(), m_type, fu);
getFontProperties cur_prop(translations->getCurrentLanguageNameCode().c_str(), m_type, m_font_use);
m_dpi = cur_prop.size;
std::vector <s32> offset;
std::vector <s32> bx;
std::vector <s32> advance;
std::vector <s32> height;
err = FT_Set_Pixel_Sizes(cur_face, 0, cur_prop.size);
err = FT_Set_Pixel_Sizes(cur_face, 0, m_dpi);
if (err)
Log::error("ScalableFont::loadTTF", "Can't set font size.");
@ -390,6 +392,7 @@ bool ScalableFont::loadTTF()
s32 t;
u32 texno = 0;
SpriteBank->addTexture(NULL);
gp_creator->clearGlyphPage();
gp_creator->createNewGlyphPage();
it = cur_prop.usedchar.begin();
@ -468,8 +471,13 @@ bool ScalableFont::loadTTF()
#ifdef FONT_DEBUG
gp_creator->dumpGlyphPage(((std::to_string(m_type)) + "_" + (std::to_string(texno))).c_str());
#endif
SpriteBank->setTexture(texno, Driver->addTexture("Glyph_page", gp_creator->getPage()));
gp_creator->clearGlyphPage();
if (m_type == T_NORMAL)
{
LastNormalPage = Driver->addTexture("Glyph_page", gp_creator->getPage());
SpriteBank->setTexture(texno, LastNormalPage);
}
else
SpriteBank->setTexture(texno, Driver->addTexture("Glyph_page", gp_creator->getPage()));
}
if (*it == (wchar_t)32 && SpriteBank->getPositions().size() == 1)
@ -529,11 +537,12 @@ bool ScalableFont::loadTTF()
WrongCharacter = getAreaIDFromCharacter(L' ', NULL);
//Add 5 for ttf bitmap to display Chinese better, 40 for digit font to display separately
//Reserve 10 for normal font new characters added, 40 for digit font to display separately
//Consider fallback font (bold) too
MaxHeight = (int)((current_maxheight + (m_type == T_DIGIT ? 40 : 5) +
MaxHeight = (int)((current_maxheight + (m_type == T_DIGIT ? 40 : 10) +
(m_type == T_BOLD ? 20 : 0))*m_scale);
GlyphMaxHeight = current_maxheight;
for(wchar_t c='0'; c<='9'; c++)
{
@ -548,22 +557,142 @@ bool ScalableFont::loadTTF()
case T_NORMAL:
Log::info("ScalableFont::loadTTF", "Created %d glyphs "
"supporting %d characters for normal font %s at %d dpi using %d glyph page(s)."
, Areas.size(), CharacterMap.size(), cur_face->family_name, cur_prop.size, texno + 1);
, Areas.size(), CharacterMap.size(), cur_face->family_name, m_dpi, SpriteBank->getTextureCount());
break;
case T_DIGIT:
Log::info("ScalableFont::loadTTF", "Created %d glyphs "
"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, cur_prop.size, texno + 1);
, Areas.size(), CharacterMap.size(), cur_face->family_name, m_dpi, SpriteBank->getTextureCount());
break;
case T_BOLD:
Log::info("ScalableFont::loadTTF", "Created %d glyphs "
"supporting %d characters for bold title font %s at %d dpi using %d glyph page(s)."
, Areas.size(), CharacterMap.size(), cur_face->family_name, cur_prop.size, texno + 1);
, Areas.size(), CharacterMap.size(), cur_face->family_name, m_dpi, SpriteBank->getTextureCount());
break;
}
return true;
}
//! lazy load new characters discovered in normal font
bool ScalableFont::lazyLoadChar()
{
//Mainly copy from loadTTF(), so removing unnecessary comments
if (m_type != T_NORMAL) return false; //Make sure only insert char inside normal font
FT_GlyphSlot slot;
FT_Error err;
s32 height;
s32 bX;
s32 offset_on_line;
s32 width;
u32 texno = SpriteBank->getTextureCount() - 1;
std::set<wchar_t>::iterator it;
it = gp_creator->newchar.begin();
while (it != gp_creator->newchar.end())
{
SGUISpriteFrame f;
SGUISprite s;
core::rect<s32> rectangle;
//Lite-Fontconfig for stk
int idx;
int count = 0;
while (count < FONTNUM - 2) //Exclude bold and digit font
{
m_font_use = (FontUse)count;
err = FT_Set_Pixel_Sizes(cur_face, 0, m_dpi);
if (err)
Log::error("ScalableFont::loadTTF", "Can't set font size.");
idx = FT_Get_Char_Index(cur_face, *it);
if (idx > 0) break;
count++;
}
slot = cur_face->glyph;
if (idx)
{
err = FT_Load_Glyph(cur_face, idx,
FT_LOAD_DEFAULT | FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL);
if (err)
Log::error("ScalableFont::loadTTF", "Can't load a single glyph.");
offset_on_line = (cur_face->glyph->metrics.height >> 6) - cur_face->glyph->bitmap_top;
height = cur_face->glyph->metrics.height >> 6;
bX = cur_face->glyph->bitmap_left;
width = cur_face->glyph->advance.x >> 6;
FT_Bitmap bits = slot->bitmap;
CharacterMap[*it] = SpriteBank->getSprites().size();
if (!gp_creator->checkEnoughSpace(bits))
{
#ifdef FONT_DEBUG
gp_creator->dumpGlyphPage(((std::to_string(m_type)) + "_" + (std::to_string(texno))).c_str());
#endif
SpriteBank->setTexture(texno, Driver->addTexture("Glyph_page", gp_creator->getPage()));
gp_creator->clearGlyphPage();
SpriteBank->addTexture(NULL);
gp_creator->createNewGlyphPage();
texno++;
}
if (gp_creator->insertGlyph(bits, rectangle))
{
f.rectNumber = SpriteBank->getPositions().size();
f.textureNumber = texno;
s.Frames.push_back(f);
s.frameTime = 0;
SpriteBank->getPositions().push_back(rectangle);
SpriteBank->getSprites().push_back(s);
}
else
return false;
SFontArea a;
a.spriteno = SpriteBank->getSprites().size() - 1;
a.offsety = GlyphMaxHeight - height + offset_on_line;
a.offsety_bt = -offset_on_line;
a.bearingx = bX;
a.width = width;
Areas.push_back(a);
}
else
CharacterMap[*it] = 2; //Set wrong character to !, preventing it from loading again
++it;
}
#ifdef FONT_DEBUG
gp_creator->dumpGlyphPage(((std::to_string(m_type)) + "_" + (std::to_string(texno))).c_str());
#endif
gp_creator->newchar.clear(); //Clear the Newly characters in creator after they are loaded
Driver->removeTexture(LastNormalPage); //Remove old texture
LastNormalPage = Driver->addTexture("Glyph_page", gp_creator->getPage());
SpriteBank->setTexture(texno, LastNormalPage);
if (!m_is_hollow_copy)
{
GUIEngine::cleanHollowCopyFont();
GUIEngine::reloadHollowCopyFont(GUIEngine::getFont());
}
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."
, cur_face->family_name, Areas.size(), CharacterMap.size(), m_dpi, SpriteBank->getTextureCount());
return true;
}
//! force create a new texture (glyph) page in a font
void ScalableFont::forceNewPage()
{
SpriteBank->setTexture(SpriteBank->getTextureCount() - 1, Driver->addTexture("Glyph_page", gp_creator->getPage()));
gp_creator->clearGlyphPage();
SpriteBank->addTexture(NULL);
gp_creator->createNewGlyphPage();
}
#endif // ENABLE_FREETYPE
void ScalableFont::setScale(const float scale)
@ -715,6 +844,27 @@ void ScalableFont::setInvisibleCharacters( const wchar_t *s )
//! returns the dimension of text
core::dimension2d<u32> ScalableFont::getDimension(const wchar_t* text) const
{
#ifdef ENABLE_FREETYPE
if (m_type == T_NORMAL || T_BOLD) //lazy load char
{
for (const wchar_t* p = text; *p; ++p)
{
if (*p == L'\r' || *p == L'\n' || *p == L' ' || *p < 32) continue;
if (GUIEngine::getFont()->getSpriteNoFromChar(p) == WrongCharacter)
gp_creator->newchar.insert(*p);
}
if (gp_creator->newchar.size() > 0 && !m_is_hollow_copy && m_scale == 1)
{
Log::debug("ScalableFont::getDimension", "New character(s) %s discoverd, perform lazy loading",
StringUtils::wide_to_utf8(gp_creator->getNewChar().c_str()).c_str());
if (!GUIEngine::getFont()->lazyLoadChar())
Log::error("ScalableFont::lazyLoadChar", "Can't insert new char into glyph pages.");
}
}
#endif // ENABLE_FREETYPE
assert(Areas.size() > 0);
core::dimension2d<u32> dim(0, 0);
@ -825,6 +975,37 @@ void ScalableFont::doDraw(const core::stringw& text,
core::array<core::position2di> offsets(text_size);
std::vector<bool> fallback(text_size);
#ifdef ENABLE_FREETYPE
if (m_type == T_NORMAL || T_BOLD) //lazy load char, have to do this again
{ //because some text isn't drawn with getDimension
for (u32 i = 0; i<text_size; i++)
{
wchar_t c = text[i];
if (c == L'\r' || c == L'\n' || c == L' ' || c < 32) continue;
if (GUIEngine::getFont()->getSpriteNoFromChar(&c) == WrongCharacter)
gp_creator->newchar.insert(c);
if (charCollector != NULL && m_type == T_NORMAL && SpriteBank->getSprites()
[GUIEngine::getFont()->getSpriteNoFromChar(&c)].Frames[0].textureNumber
== SpriteBank->getTextureCount() - 1) //Prevent overwriting texture used by billboard text
{
Log::debug("ScalableFont::doDraw", "Character used by billboard text is in the last glyph page of normal font."
" Create a new glyph page for new characters inserted later to prevent it from being removed.");
GUIEngine::getFont()->forceNewPage();
}
}
if (gp_creator->newchar.size() > 0 && !m_is_hollow_copy && m_scale == 1)
{
Log::debug("ScalableFont::doDraw", "New character(s) %s discoverd, perform lazy loading",
StringUtils::wide_to_utf8(gp_creator->getNewChar().c_str()).c_str());
if (!GUIEngine::getFont()->lazyLoadChar())
Log::error("ScalableFont::lazyLoadChar", "Can't insert new char into glyph pages.");
}
}
#endif // ENABLE_FREETYPE
for (u32 i = 0; i<text_size; i++)
{
wchar_t c = text[i];

View File

@ -86,6 +86,8 @@ public:
#ifdef ENABLE_FREETYPE
TTFLoadingType m_type;
FontUse m_font_use;
u32 m_dpi;
#endif // ENABLE_FREETYPE
ScalableFont* m_fallback_font;
@ -181,6 +183,12 @@ public:
#ifdef ENABLE_FREETYPE
//! re-create fonts when language is changed
void recreateFromLanguage();
//! lazy load new characters discovered in normal font
bool lazyLoadChar();
//! force create a new texture (glyph) page in a font
void forceNewPage();
#endif // ENABLE_FREETYPE
private:
@ -221,6 +229,10 @@ private:
u32 WrongCharacter;
s32 MaxHeight;
s32 GlobalKerningWidth, GlobalKerningHeight;
#ifdef ENABLE_FREETYPE
s32 GlyphMaxHeight;
video::ITexture* LastNormalPage;
#endif // ENABLE_FREETYPE
core::stringw Invisible;
};

View File

@ -13,6 +13,7 @@
#include "Keycodes.h"
#include "graphics/2dutils.hpp"
#include "utils/string_utils.hpp"
#include "utils/translation.hpp"
#include "utils/time.hpp"
@ -279,9 +280,9 @@ bool CGUIEditBox::processKey(const SEvent& event)
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
core::stringc s;
core::stringw s;
s = Text.subString(realmbgn, realmend - realmbgn).c_str();
Operator->copyToClipboard(s.c_str());
Operator->copyToClipboard(StringUtils::wide_to_utf8(s.c_str()).c_str());
}
break;
case KEY_KEY_X:
@ -292,9 +293,9 @@ bool CGUIEditBox::processKey(const SEvent& event)
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
// copy
core::stringc sc;
core::stringw sc;
sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
Operator->copyToClipboard(sc.c_str());
Operator->copyToClipboard(StringUtils::wide_to_utf8(sc.c_str()).c_str());
if (isEnabled())
{
@ -330,13 +331,13 @@ bool CGUIEditBox::processKey(const SEvent& event)
{
// insert text
core::stringw s = Text.subString(0, CursorPos);
s.append(p);
s.append(StringUtils::utf8_to_wide(p));
s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
if (!Max || s.size()<=Max) // thx to Fish FH for fix
{
Text = s;
s = p;
s = StringUtils::utf8_to_wide(p);
CursorPos += s.size();
}
}
@ -345,13 +346,13 @@ bool CGUIEditBox::processKey(const SEvent& event)
// replace text
core::stringw s = Text.subString(0, realmbgn);
s.append(p);
s.append(StringUtils::utf8_to_wide(p));
s.append( Text.subString(realmend, Text.size()-realmend) );
if (!Max || s.size()<=Max) // thx to Fish FH for fix
{
Text = s;
s = p;
s = StringUtils::utf8_to_wide(p);
CursorPos = realmbgn + s.size();
}
}

View File

@ -56,7 +56,6 @@ DynamicRibbonWidget::DynamicRibbonWidget(const bool combo, const bool multi_row)
m_item_count_hint = 0;
m_font = GUIEngine::getFont()->getHollowCopy();
m_max_label_width = 0;
}
// -----------------------------------------------------------------------------
@ -385,6 +384,8 @@ void DynamicRibbonWidget::buildInternalStructure()
ribbon->m_properties[PROP_ID] = name.str();
ribbon->m_event_handler = this;
m_font = GUIEngine::getFont()->getHollowCopy();
// calculate font size
if (m_col_amount > 0)
{