2014-06-20 01:30:09 +00:00
|
|
|
#include "resources/Font.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
#include "renderers/Renderer.h"
|
2018-01-26 18:53:19 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
2017-11-15 15:59:39 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Log.h"
|
2019-08-08 20:16:11 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-05-12 22:05:28 +00:00
|
|
|
FT_Library Font::sLibrary = NULL;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
|
|
|
int Font::getSize() const { return mSize; }
|
|
|
|
|
|
|
|
std::map< std::pair<std::string, int>, std::weak_ptr<Font> > Font::sFontMap;
|
|
|
|
|
2014-08-30 20:36:56 +00:00
|
|
|
Font::FontFace::FontFace(ResourceData&& d, int size) : data(d)
|
|
|
|
{
|
2017-11-17 14:58:52 +00:00
|
|
|
int err = FT_New_Memory_Face(sLibrary, data.ptr.get(), (FT_Long)data.length, 0, &face);
|
2014-08-30 20:36:56 +00:00
|
|
|
assert(!err);
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2017-10-28 20:07:31 +00:00
|
|
|
if(!err)
|
|
|
|
FT_Set_Pixel_Sizes(face, 0, size);
|
2014-08-30 20:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Font::FontFace::~FontFace()
|
|
|
|
{
|
|
|
|
if(face)
|
|
|
|
FT_Done_Face(face);
|
|
|
|
}
|
|
|
|
|
2013-10-04 23:24:41 +00:00
|
|
|
void Font::initLibrary()
|
|
|
|
{
|
2014-05-12 22:05:28 +00:00
|
|
|
assert(sLibrary == NULL);
|
2013-10-04 23:24:41 +00:00
|
|
|
if(FT_Init_FreeType(&sLibrary))
|
|
|
|
{
|
2014-05-12 22:05:28 +00:00
|
|
|
sLibrary = NULL;
|
2013-10-04 23:24:41 +00:00
|
|
|
LOG(LogError) << "Error initializing FreeType!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 21:47:25 +00:00
|
|
|
size_t Font::getMemUsage() const
|
|
|
|
{
|
2014-07-27 21:44:02 +00:00
|
|
|
size_t memUsage = 0;
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mTextures.cbegin(); it != mTextures.cend(); it++)
|
2014-07-27 21:44:02 +00:00
|
|
|
memUsage += it->textureSize.x() * it->textureSize.y() * 4;
|
2014-03-27 21:47:25 +00:00
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mFaceCache.cbegin(); it != mFaceCache.cend(); it++)
|
2014-08-30 20:36:56 +00:00
|
|
|
memUsage += it->second->data.length;
|
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
return memUsage;
|
2014-03-27 21:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Font::getTotalMemUsage()
|
|
|
|
{
|
|
|
|
size_t total = 0;
|
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
auto it = sFontMap.cbegin();
|
|
|
|
while(it != sFontMap.cend())
|
2014-03-27 21:47:25 +00:00
|
|
|
{
|
|
|
|
if(it->second.expired())
|
|
|
|
{
|
|
|
|
it = sFontMap.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
total += it->second.lock()->getMemUsage();
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
Font::Font(int size, const std::string& path) : mSize(size), mPath(path)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2014-07-27 21:44:02 +00:00
|
|
|
assert(mSize > 0);
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-09-27 21:09:49 +00:00
|
|
|
mMaxGlyphHeight = 0;
|
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
if(!sLibrary)
|
|
|
|
initLibrary();
|
|
|
|
|
2014-08-11 23:05:18 +00:00
|
|
|
// always initialize ASCII characters
|
2017-11-10 18:48:23 +00:00
|
|
|
for(unsigned int i = 32; i < 128; i++)
|
2014-08-11 23:05:18 +00:00
|
|
|
getGlyph(i);
|
2014-08-30 20:36:56 +00:00
|
|
|
|
|
|
|
clearFaceCache();
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Font::~Font()
|
|
|
|
{
|
2014-07-27 21:44:02 +00:00
|
|
|
unload(ResourceManager::getInstance());
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:58:52 +00:00
|
|
|
void Font::reload(std::shared_ptr<ResourceManager>& /*rm*/)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2014-08-11 23:05:18 +00:00
|
|
|
rebuildTextures();
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:58:52 +00:00
|
|
|
void Font::unload(std::shared_ptr<ResourceManager>& /*rm*/)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2014-08-11 23:05:18 +00:00
|
|
|
unloadTextures();
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Font> Font::get(int size, const std::string& path)
|
|
|
|
{
|
2018-01-26 18:53:19 +00:00
|
|
|
const std::string canonicalPath = Utils::FileSystem::getCanonicalPath(path);
|
2014-05-23 22:21:01 +00:00
|
|
|
|
|
|
|
std::pair<std::string, int> def(canonicalPath.empty() ? getDefaultPath() : canonicalPath, size);
|
2013-10-04 23:24:41 +00:00
|
|
|
auto foundFont = sFontMap.find(def);
|
2017-11-11 14:56:22 +00:00
|
|
|
if(foundFont != sFontMap.cend())
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
|
|
|
if(!foundFont->second.expired())
|
|
|
|
return foundFont->second.lock();
|
|
|
|
}
|
|
|
|
|
2013-12-08 20:00:53 +00:00
|
|
|
std::shared_ptr<Font> font = std::shared_ptr<Font>(new Font(def.second, def.first));
|
2013-10-04 23:24:41 +00:00
|
|
|
sFontMap[def] = std::weak_ptr<Font>(font);
|
|
|
|
ResourceManager::getInstance()->addReloadable(font);
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2014-08-11 23:05:18 +00:00
|
|
|
void Font::unloadTextures()
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2014-07-27 21:44:02 +00:00
|
|
|
for(auto it = mTextures.begin(); it != mTextures.end(); it++)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2014-08-11 23:05:18 +00:00
|
|
|
it->deinitTexture();
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
2014-08-11 23:05:18 +00:00
|
|
|
}
|
2014-07-27 21:44:02 +00:00
|
|
|
|
2014-08-11 23:05:18 +00:00
|
|
|
Font::FontTexture::FontTexture()
|
|
|
|
{
|
|
|
|
textureId = 0;
|
2017-10-28 20:24:35 +00:00
|
|
|
textureSize = Vector2i(2048, 512);
|
|
|
|
writePos = Vector2i::Zero();
|
2014-08-11 23:05:18 +00:00
|
|
|
rowHeight = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Font::FontTexture::~FontTexture()
|
|
|
|
{
|
|
|
|
deinitTexture();
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
bool Font::FontTexture::findEmpty(const Vector2i& size, Vector2i& cursor_out)
|
2014-07-27 21:44:02 +00:00
|
|
|
{
|
|
|
|
if(size.x() >= textureSize.x() || size.y() >= textureSize.y())
|
|
|
|
return false;
|
2014-05-12 22:05:28 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
if(writePos.x() + size.x() >= textureSize.x() &&
|
|
|
|
writePos.y() + rowHeight + size.y() + 1 < textureSize.y())
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2014-07-27 21:44:02 +00:00
|
|
|
// row full, but it should fit on the next row
|
|
|
|
// move cursor to next row
|
2017-10-28 20:24:35 +00:00
|
|
|
writePos = Vector2i(0, writePos.y() + rowHeight + 1); // leave 1px of space between glyphs
|
2014-07-27 21:44:02 +00:00
|
|
|
rowHeight = 0;
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
if(writePos.x() + size.x() >= textureSize.x() ||
|
|
|
|
writePos.y() + size.y() >= textureSize.y())
|
|
|
|
{
|
|
|
|
// nope, still won't fit
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor_out = writePos;
|
|
|
|
writePos[0] += size.x() + 1; // leave 1px of space between glyphs
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
if(size.y() > rowHeight)
|
|
|
|
rowHeight = size.y();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-08-11 23:05:18 +00:00
|
|
|
void Font::FontTexture::initTexture()
|
|
|
|
{
|
|
|
|
assert(textureId == 0);
|
2019-08-08 20:16:11 +00:00
|
|
|
textureId = Renderer::createTexture(Renderer::Texture::ALPHA, false, false, textureSize.x(), textureSize.y(), nullptr);
|
2014-08-11 23:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Font::FontTexture::deinitTexture()
|
|
|
|
{
|
|
|
|
if(textureId != 0)
|
|
|
|
{
|
2019-08-08 20:16:11 +00:00
|
|
|
Renderer::destroyTexture(textureId);
|
2014-08-11 23:05:18 +00:00
|
|
|
textureId = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void Font::getTextureForNewGlyph(const Vector2i& glyphSize, FontTexture*& tex_out, Vector2i& cursor_out)
|
2014-07-27 21:44:02 +00:00
|
|
|
{
|
|
|
|
if(mTextures.size())
|
|
|
|
{
|
|
|
|
// check if the most recent texture has space
|
|
|
|
tex_out = &mTextures.back();
|
|
|
|
|
|
|
|
// will this one work?
|
|
|
|
if(tex_out->findEmpty(glyphSize, cursor_out))
|
|
|
|
return; // yes
|
|
|
|
}
|
|
|
|
|
|
|
|
// current textures are full,
|
|
|
|
// make a new one
|
|
|
|
mTextures.push_back(FontTexture());
|
|
|
|
tex_out = &mTextures.back();
|
2014-08-11 23:05:18 +00:00
|
|
|
tex_out->initTexture();
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
bool ok = tex_out->findEmpty(glyphSize, cursor_out);
|
|
|
|
if(!ok)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2014-07-27 21:44:02 +00:00
|
|
|
LOG(LogError) << "Glyph too big to fit on a new texture (glyph size > " << tex_out->textureSize.x() << ", " << tex_out->textureSize.y() << ")!";
|
|
|
|
tex_out = NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-08-30 20:36:56 +00:00
|
|
|
std::vector<std::string> getFallbackFontPaths()
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
// Windows
|
|
|
|
|
|
|
|
// get this system's equivalent of "C:\Windows" (might be on a different drive or in a different folder)
|
|
|
|
// so we can check the Fonts subdirectory for fallback fonts
|
|
|
|
TCHAR winDir[MAX_PATH];
|
|
|
|
GetWindowsDirectory(winDir, MAX_PATH);
|
|
|
|
std::string fontDir = winDir;
|
|
|
|
fontDir += "\\Fonts\\";
|
|
|
|
|
|
|
|
const char* fontNames[] = {
|
|
|
|
"meiryo.ttc", // japanese
|
|
|
|
"simhei.ttf", // chinese
|
|
|
|
"arial.ttf" // latin
|
|
|
|
};
|
|
|
|
|
|
|
|
//prepend to font file names
|
|
|
|
std::vector<std::string> fontPaths;
|
|
|
|
fontPaths.reserve(sizeof(fontNames) / sizeof(fontNames[0]));
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < sizeof(fontNames) / sizeof(fontNames[0]); i++)
|
|
|
|
{
|
|
|
|
std::string path = fontDir + fontNames[i];
|
|
|
|
if(ResourceManager::getInstance()->fileExists(path))
|
|
|
|
fontPaths.push_back(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
fontPaths.shrink_to_fit();
|
|
|
|
return fontPaths;
|
|
|
|
|
|
|
|
#else
|
|
|
|
// Linux
|
|
|
|
|
|
|
|
std::vector<std::string> fontPaths;
|
2020-05-15 16:03:42 +00:00
|
|
|
|
|
|
|
// Vera sans Unicode:
|
|
|
|
fontPaths.push_back(ResourceManager::getInstance()->getResourcePath(":/DejaVuSans.ttf"));
|
|
|
|
// Freefont monospaced:
|
|
|
|
fontPaths.push_back(ResourceManager::getInstance()->getResourcePath(":/FreeMono.ttf"));
|
|
|
|
// Various languages, such as Japanese and Chinese:
|
|
|
|
fontPaths.push_back(ResourceManager::getInstance()->getResourcePath(":/DroidSansFallbackFull.ttf"));
|
|
|
|
// Korean:
|
|
|
|
fontPaths.push_back(ResourceManager::getInstance()->getResourcePath(":/NanumMyeongjo.ttf"));
|
|
|
|
// Font Awesome icon glyphs, used for star-flagging favorites in the gamelists:
|
|
|
|
fontPaths.push_back(ResourceManager::getInstance()->getResourcePath(":/fontawesome-webfont.ttf"));
|
2014-08-30 20:36:56 +00:00
|
|
|
|
|
|
|
fontPaths.shrink_to_fit();
|
2014-08-31 14:57:38 +00:00
|
|
|
return fontPaths;
|
2014-08-30 20:36:56 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-11-10 18:48:23 +00:00
|
|
|
FT_Face Font::getFaceForChar(unsigned int id)
|
2014-08-30 20:36:56 +00:00
|
|
|
{
|
|
|
|
static const std::vector<std::string> fallbackFonts = getFallbackFontPaths();
|
|
|
|
|
|
|
|
// look through our current font + fallback fonts to see if any have the glyph we're looking for
|
|
|
|
for(unsigned int i = 0; i < fallbackFonts.size() + 1; i++)
|
|
|
|
{
|
|
|
|
auto fit = mFaceCache.find(i);
|
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
if(fit == mFaceCache.cend()) // doesn't exist yet
|
2014-08-30 20:36:56 +00:00
|
|
|
{
|
|
|
|
// i == 0 -> mPath
|
|
|
|
// otherwise, take from fallbackFonts
|
|
|
|
const std::string& path = (i == 0 ? mPath : fallbackFonts.at(i - 1));
|
|
|
|
ResourceData data = ResourceManager::getInstance()->getFileData(path);
|
|
|
|
mFaceCache[i] = std::unique_ptr<FontFace>(new FontFace(std::move(data), mSize));
|
|
|
|
fit = mFaceCache.find(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FT_Get_Char_Index(fit->second->face, id) != 0)
|
2014-09-27 21:09:49 +00:00
|
|
|
return fit->second->face;
|
2014-08-30 20:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nothing has a valid glyph - return the "real" face so we get a "missing" character
|
2017-11-11 14:56:22 +00:00
|
|
|
return mFaceCache.cbegin()->second->face;
|
2014-08-30 20:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Font::clearFaceCache()
|
|
|
|
{
|
|
|
|
mFaceCache.clear();
|
|
|
|
}
|
|
|
|
|
2017-11-10 18:48:23 +00:00
|
|
|
Font::Glyph* Font::getGlyph(unsigned int id)
|
2014-07-27 21:44:02 +00:00
|
|
|
{
|
|
|
|
// is it already loaded?
|
|
|
|
auto it = mGlyphMap.find(id);
|
2017-11-11 14:56:22 +00:00
|
|
|
if(it != mGlyphMap.cend())
|
2014-07-27 21:44:02 +00:00
|
|
|
return &it->second;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
// nope, need to make a glyph
|
2014-08-30 20:36:56 +00:00
|
|
|
FT_Face face = getFaceForChar(id);
|
|
|
|
if(!face)
|
2014-07-27 21:44:02 +00:00
|
|
|
{
|
2014-08-30 20:36:56 +00:00
|
|
|
LOG(LogError) << "Could not find appropriate font face for character " << id << " for font " << mPath;
|
2014-07-27 21:44:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
FT_GlyphSlot g = face->glyph;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
if(FT_Load_Char(face, id, FT_LOAD_RENDER))
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Could not find glyph for character " << id << " for font " << mPath << ", size " << mSize << "!";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i glyphSize(g->bitmap.width, g->bitmap.rows);
|
2014-07-27 21:44:02 +00:00
|
|
|
|
|
|
|
FontTexture* tex = NULL;
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i cursor;
|
2014-07-27 21:44:02 +00:00
|
|
|
getTextureForNewGlyph(glyphSize, tex, cursor);
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
// getTextureForNewGlyph can fail if the glyph is bigger than the max texture size (absurdly large font size)
|
|
|
|
if(tex == NULL)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Could not create glyph for character " << id << " for font " << mPath << ", size " << mSize << " (no suitable texture found)!";
|
|
|
|
return NULL;
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
// create glyph
|
|
|
|
Glyph& glyph = mGlyphMap[id];
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
glyph.texture = tex;
|
2017-10-28 20:24:35 +00:00
|
|
|
glyph.texPos = Vector2f(cursor.x() / (float)tex->textureSize.x(), cursor.y() / (float)tex->textureSize.y());
|
|
|
|
glyph.texSize = Vector2f(glyphSize.x() / (float)tex->textureSize.x(), glyphSize.y() / (float)tex->textureSize.y());
|
2014-07-27 21:44:02 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
glyph.advance = Vector2f((float)g->metrics.horiAdvance / 64.0f, (float)g->metrics.vertAdvance / 64.0f);
|
|
|
|
glyph.bearing = Vector2f((float)g->metrics.horiBearingX / 64.0f, (float)g->metrics.horiBearingY / 64.0f);
|
2014-07-27 21:44:02 +00:00
|
|
|
|
|
|
|
// upload glyph bitmap to texture
|
2019-08-08 20:16:11 +00:00
|
|
|
Renderer::updateTexture(tex->textureId, Renderer::Texture::ALPHA, cursor.x(), cursor.y(), glyphSize.x(), glyphSize.y(), g->bitmap.buffer);
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
// update max glyph height
|
|
|
|
if(glyphSize.y() > mMaxGlyphHeight)
|
|
|
|
mMaxGlyphHeight = glyphSize.y();
|
|
|
|
|
|
|
|
// done
|
|
|
|
return &glyph;
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 23:05:18 +00:00
|
|
|
// completely recreate the texture data for all textures based on mGlyphs information
|
|
|
|
void Font::rebuildTextures()
|
|
|
|
{
|
|
|
|
// recreate OpenGL textures
|
|
|
|
for(auto it = mTextures.begin(); it != mTextures.end(); it++)
|
|
|
|
{
|
|
|
|
it->initTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
// reupload the texture data
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mGlyphMap.cbegin(); it != mGlyphMap.cend(); it++)
|
2014-08-11 23:05:18 +00:00
|
|
|
{
|
2014-08-30 20:36:56 +00:00
|
|
|
FT_Face face = getFaceForChar(it->first);
|
|
|
|
FT_GlyphSlot glyphSlot = face->glyph;
|
|
|
|
|
2014-08-11 23:05:18 +00:00
|
|
|
// load the glyph bitmap through FT
|
|
|
|
FT_Load_Char(face, it->first, FT_LOAD_RENDER);
|
|
|
|
|
|
|
|
FontTexture* tex = it->second.texture;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-08-11 23:05:18 +00:00
|
|
|
// find the position/size
|
2017-11-17 14:58:52 +00:00
|
|
|
Vector2i cursor((int)(it->second.texPos.x() * tex->textureSize.x()), (int)(it->second.texPos.y() * tex->textureSize.y()));
|
|
|
|
Vector2i glyphSize((int)(it->second.texSize.x() * tex->textureSize.x()), (int)(it->second.texSize.y() * tex->textureSize.y()));
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-08-11 23:05:18 +00:00
|
|
|
// upload to texture
|
2019-08-08 20:16:11 +00:00
|
|
|
Renderer::updateTexture(tex->textureId, Renderer::Texture::ALPHA, cursor.x(), cursor.y(), glyphSize.x(), glyphSize.y(), glyphSlot->bitmap.buffer);
|
2014-08-11 23:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 23:24:41 +00:00
|
|
|
void Font::renderTextCache(TextCache* cache)
|
|
|
|
{
|
|
|
|
if(cache == NULL)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Attempted to draw NULL TextCache!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = cache->vertexLists.cbegin(); it != cache->vertexLists.cend(); it++)
|
2014-07-27 21:44:02 +00:00
|
|
|
{
|
2014-08-11 23:05:18 +00:00
|
|
|
assert(*it->textureIdPtr != 0);
|
2014-07-27 21:44:02 +00:00
|
|
|
|
|
|
|
auto vertexList = *it;
|
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
Renderer::bindTexture(*it->textureIdPtr);
|
|
|
|
Renderer::drawTriangleStrips(&it->verts[0], it->verts.size());
|
2014-07-27 21:44:02 +00:00
|
|
|
}
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f Font::sizeText(std::string text, float lineSpacing)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
|
|
|
float lineWidth = 0.0f;
|
|
|
|
float highestWidth = 0.0f;
|
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
const float lineHeight = getHeight(lineSpacing);
|
|
|
|
|
|
|
|
float y = lineHeight;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
size_t i = 0;
|
|
|
|
while(i < text.length())
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2017-11-15 15:59:39 +00:00
|
|
|
unsigned int character = Utils::String::chars2Unicode(text, i); // advances i
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2017-11-10 18:48:23 +00:00
|
|
|
if(character == '\n')
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
|
|
|
if(lineWidth > highestWidth)
|
|
|
|
highestWidth = lineWidth;
|
|
|
|
|
|
|
|
lineWidth = 0.0f;
|
2014-07-27 21:44:02 +00:00
|
|
|
y += lineHeight;
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 21:09:49 +00:00
|
|
|
Glyph* glyph = getGlyph(character);
|
|
|
|
if(glyph)
|
|
|
|
lineWidth += glyph->advance.x();
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(lineWidth > highestWidth)
|
|
|
|
highestWidth = lineWidth;
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
return Vector2f(highestWidth, y);
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2014-05-12 22:05:28 +00:00
|
|
|
float Font::getHeight(float lineSpacing) const
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2014-07-27 21:44:02 +00:00
|
|
|
return mMaxGlyphHeight * lineSpacing;
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
float Font::getLetterHeight()
|
2014-03-22 21:02:25 +00:00
|
|
|
{
|
2017-11-10 18:48:23 +00:00
|
|
|
Glyph* glyph = getGlyph('S');
|
2014-09-27 21:09:49 +00:00
|
|
|
assert(glyph);
|
2014-07-27 21:44:02 +00:00
|
|
|
return glyph->texSize.y() * glyph->texture->textureSize.y();
|
2014-03-22 21:02:25 +00:00
|
|
|
}
|
2013-10-04 23:24:41 +00:00
|
|
|
|
|
|
|
//the worst algorithm ever written
|
|
|
|
//breaks up a normal string with newlines to make it fit xLen
|
2014-07-27 21:44:02 +00:00
|
|
|
std::string Font::wrapText(std::string text, float xLen)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
|
|
|
std::string out;
|
|
|
|
|
|
|
|
std::string line, word, temp;
|
2014-05-19 02:13:36 +00:00
|
|
|
size_t space;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f textSize;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-05-19 02:13:36 +00:00
|
|
|
while(text.length() > 0) //while there's text or we still have text to render
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2014-05-19 02:13:36 +00:00
|
|
|
space = text.find_first_of(" \t\n");
|
2013-10-04 23:24:41 +00:00
|
|
|
if(space == std::string::npos)
|
|
|
|
space = text.length() - 1;
|
|
|
|
|
|
|
|
word = text.substr(0, space + 1);
|
2014-05-19 02:13:36 +00:00
|
|
|
text.erase(0, space + 1);
|
2013-10-04 23:24:41 +00:00
|
|
|
|
|
|
|
temp = line + word;
|
|
|
|
|
|
|
|
textSize = sizeText(temp);
|
|
|
|
|
2014-05-19 02:13:36 +00:00
|
|
|
// if the word will fit on the line, add it to our line, and continue
|
|
|
|
if(textSize.x() <= xLen)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
|
|
|
line = temp;
|
2014-05-19 02:13:36 +00:00
|
|
|
continue;
|
|
|
|
}else{
|
|
|
|
// the next word won't fit, so break here
|
2013-10-04 23:24:41 +00:00
|
|
|
out += line + '\n';
|
|
|
|
line = word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 02:13:36 +00:00
|
|
|
// whatever's left should fit
|
|
|
|
out += line;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f Font::sizeWrappedText(std::string text, float xLen, float lineSpacing)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
|
|
|
text = wrapText(text, xLen);
|
2014-05-12 22:05:28 +00:00
|
|
|
return sizeText(text, lineSpacing);
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f Font::getWrappedTextCursorOffset(std::string text, float xLen, size_t stop, float lineSpacing)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
|
|
|
std::string wrappedText = wrapText(text, xLen);
|
|
|
|
|
|
|
|
float lineWidth = 0.0f;
|
|
|
|
float y = 0.0f;
|
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
size_t wrapCursor = 0;
|
|
|
|
size_t cursor = 0;
|
|
|
|
while(cursor < stop)
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2017-11-15 15:59:39 +00:00
|
|
|
unsigned int wrappedCharacter = Utils::String::chars2Unicode(wrappedText, wrapCursor);
|
|
|
|
unsigned int character = Utils::String::chars2Unicode(text, cursor);
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2017-11-10 18:48:23 +00:00
|
|
|
if(wrappedCharacter == '\n' && character != '\n')
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
|
|
|
//this is where the wordwrap inserted a newline
|
|
|
|
//reset lineWidth and increment y, but don't consume a cursor character
|
|
|
|
lineWidth = 0.0f;
|
2014-05-12 22:05:28 +00:00
|
|
|
y += getHeight(lineSpacing);
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2017-11-15 15:59:39 +00:00
|
|
|
cursor = Utils::String::prevCursor(text, cursor); // unconsume
|
2013-10-04 23:24:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-10 18:48:23 +00:00
|
|
|
if(character == '\n')
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
|
|
|
lineWidth = 0.0f;
|
2014-05-12 22:05:28 +00:00
|
|
|
y += getHeight(lineSpacing);
|
2013-10-04 23:24:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-10-18 20:32:18 +00:00
|
|
|
Glyph* glyph = getGlyph(character);
|
2014-09-27 21:09:49 +00:00
|
|
|
if(glyph)
|
|
|
|
lineWidth += glyph->advance.x();
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
return Vector2f(lineWidth, y);
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================================================
|
|
|
|
//TextCache
|
|
|
|
//=============================================================================================================
|
|
|
|
|
2014-05-13 01:03:02 +00:00
|
|
|
float Font::getNewlineStartOffset(const std::string& text, const unsigned int& charStart, const float& xLen, const Alignment& alignment)
|
2014-05-01 19:47:33 +00:00
|
|
|
{
|
2014-05-13 01:03:02 +00:00
|
|
|
switch(alignment)
|
2014-05-01 19:47:33 +00:00
|
|
|
{
|
2014-05-13 01:03:02 +00:00
|
|
|
case ALIGN_LEFT:
|
|
|
|
return 0;
|
|
|
|
case ALIGN_CENTER:
|
|
|
|
{
|
2017-11-17 14:58:52 +00:00
|
|
|
unsigned int endChar = (unsigned int)text.find('\n', charStart);
|
2014-05-13 01:03:02 +00:00
|
|
|
return (xLen - sizeText(text.substr(charStart, endChar != std::string::npos ? endChar - charStart : endChar)).x()) / 2.0f;
|
|
|
|
}
|
|
|
|
case ALIGN_RIGHT:
|
|
|
|
{
|
2017-11-17 14:58:52 +00:00
|
|
|
unsigned int endChar = (unsigned int)text.find('\n', charStart);
|
2014-05-13 01:03:02 +00:00
|
|
|
return xLen - (sizeText(text.substr(charStart, endChar != std::string::npos ? endChar - charStart : endChar)).x());
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return 0;
|
2014-05-01 19:47:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
TextCache* Font::buildTextCache(const std::string& text, Vector2f offset, unsigned int color, float xLen, Alignment alignment, float lineSpacing)
|
2014-07-27 21:44:02 +00:00
|
|
|
{
|
2014-05-13 01:03:02 +00:00
|
|
|
float x = offset[0] + (xLen != 0 ? getNewlineStartOffset(text, 0, xLen, alignment) : 0);
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2017-11-10 18:48:23 +00:00
|
|
|
float yTop = getGlyph('S')->bearing.y();
|
2014-05-13 01:03:02 +00:00
|
|
|
float yBot = getHeight(lineSpacing);
|
|
|
|
float y = offset[1] + (yBot + yTop)/2.0f;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
// vertices by texture
|
2019-08-08 20:16:11 +00:00
|
|
|
std::map< FontTexture*, std::vector<Renderer::Vertex> > vertMap;
|
2014-07-27 21:44:02 +00:00
|
|
|
|
|
|
|
size_t cursor = 0;
|
|
|
|
while(cursor < text.length())
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2017-11-15 15:59:39 +00:00
|
|
|
unsigned int character = Utils::String::chars2Unicode(text, cursor); // also advances cursor
|
2017-11-10 18:48:23 +00:00
|
|
|
Glyph* glyph;
|
2014-07-27 21:44:02 +00:00
|
|
|
|
|
|
|
// invalid character
|
|
|
|
if(character == 0)
|
|
|
|
continue;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2017-11-10 18:48:23 +00:00
|
|
|
if(character == '\n')
|
2013-10-04 23:24:41 +00:00
|
|
|
{
|
2014-05-13 01:03:02 +00:00
|
|
|
y += getHeight(lineSpacing);
|
2017-11-17 14:58:52 +00:00
|
|
|
x = offset[0] + (xLen != 0 ? getNewlineStartOffset(text, (const unsigned int)cursor /* cursor is already advanced */, xLen, alignment) : 0);
|
2013-10-04 23:24:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
glyph = getGlyph(character);
|
|
|
|
if(glyph == NULL)
|
|
|
|
continue;
|
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
std::vector<Renderer::Vertex>& verts = vertMap[glyph->texture];
|
2014-07-27 21:44:02 +00:00
|
|
|
size_t oldVertSize = verts.size();
|
|
|
|
verts.resize(oldVertSize + 6);
|
2019-08-08 20:16:11 +00:00
|
|
|
Renderer::Vertex* vertices = verts.data() + oldVertSize;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
const float glyphStartX = x + glyph->bearing.x();
|
|
|
|
const Vector2i& textureSize = glyph->texture->textureSize;
|
|
|
|
const unsigned int convertedColor = Renderer::convertColor(color);
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2019-08-31 16:19:43 +00:00
|
|
|
vertices[1] = { { glyphStartX , y - glyph->bearing.y() }, { glyph->texPos.x(), glyph->texPos.y() }, convertedColor };
|
|
|
|
vertices[2] = { { glyphStartX , y - glyph->bearing.y() + (glyph->texSize.y() * textureSize.y()) }, { glyph->texPos.x(), glyph->texPos.y() + glyph->texSize.y() }, convertedColor };
|
|
|
|
vertices[3] = { { glyphStartX + glyph->texSize.x() * textureSize.x(), y - glyph->bearing.y() }, { glyph->texPos.x() + glyph->texSize.x(), glyph->texPos.y() }, convertedColor };
|
|
|
|
vertices[4] = { { glyphStartX + glyph->texSize.x() * textureSize.x(), y - glyph->bearing.y() + (glyph->texSize.y() * textureSize.y()) }, { glyph->texPos.x() + glyph->texSize.x(), glyph->texPos.y() + glyph->texSize.y() }, convertedColor };
|
|
|
|
|
|
|
|
// round vertices
|
|
|
|
for(int i = 1; i < 5; ++i)
|
|
|
|
vertices[i].pos.round();
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
// make duplicates of first and last vertex so this can be rendered as a triangle strip
|
|
|
|
vertices[0] = vertices[1];
|
|
|
|
vertices[5] = vertices[4];
|
2014-07-27 21:44:02 +00:00
|
|
|
|
|
|
|
// advance
|
|
|
|
x += glyph->advance.x();
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 21:44:02 +00:00
|
|
|
//TextCache::CacheMetrics metrics = { sizeText(text, lineSpacing) };
|
|
|
|
|
|
|
|
TextCache* cache = new TextCache();
|
|
|
|
cache->vertexLists.resize(vertMap.size());
|
|
|
|
cache->metrics = { sizeText(text, lineSpacing) };
|
|
|
|
|
|
|
|
unsigned int i = 0;
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = vertMap.cbegin(); it != vertMap.cend(); it++)
|
2014-07-27 21:44:02 +00:00
|
|
|
{
|
|
|
|
TextCache::VertexList& vertList = cache->vertexLists.at(i);
|
|
|
|
|
2014-08-11 23:05:18 +00:00
|
|
|
vertList.textureIdPtr = &it->first->textureId;
|
2014-07-27 21:44:02 +00:00
|
|
|
vertList.verts = it->second;
|
|
|
|
}
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2014-08-30 20:36:56 +00:00
|
|
|
clearFaceCache();
|
|
|
|
|
2013-10-04 23:24:41 +00:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2014-05-13 01:03:02 +00:00
|
|
|
TextCache* Font::buildTextCache(const std::string& text, float offsetX, float offsetY, unsigned int color)
|
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
return buildTextCache(text, Vector2f(offsetX, offsetY), color, 0.0f);
|
2014-05-13 01:03:02 +00:00
|
|
|
}
|
|
|
|
|
2013-10-04 23:24:41 +00:00
|
|
|
void TextCache::setColor(unsigned int color)
|
|
|
|
{
|
2019-08-08 20:16:11 +00:00
|
|
|
const unsigned int convertedColor = Renderer::convertColor(color);
|
|
|
|
|
|
|
|
for(auto it = vertexLists.begin(); it != vertexLists.end(); it++)
|
|
|
|
for(auto it2 = it->verts.begin(); it2 != it->verts.end(); it2++)
|
|
|
|
it2->col = convertedColor;
|
2013-10-04 23:24:41 +00:00
|
|
|
}
|
2014-01-01 05:39:22 +00:00
|
|
|
|
|
|
|
std::shared_ptr<Font> Font::getFromTheme(const ThemeData::ThemeElement* elem, unsigned int properties, const std::shared_ptr<Font>& orig)
|
|
|
|
{
|
|
|
|
using namespace ThemeFlags;
|
|
|
|
if(!(properties & FONT_PATH) && !(properties & FONT_SIZE))
|
|
|
|
return orig;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-01-01 05:39:22 +00:00
|
|
|
std::shared_ptr<Font> font;
|
|
|
|
int size = (orig ? orig->mSize : FONT_SIZE_MEDIUM);
|
|
|
|
std::string path = (orig ? orig->mPath : getDefaultPath());
|
|
|
|
|
|
|
|
float sh = (float)Renderer::getScreenHeight();
|
2019-08-25 15:23:02 +00:00
|
|
|
if(properties & FONT_SIZE && elem->has("fontSize"))
|
2014-01-01 05:39:22 +00:00
|
|
|
size = (int)(sh * elem->get<float>("fontSize"));
|
|
|
|
if(properties & FONT_PATH && elem->has("fontPath"))
|
|
|
|
path = elem->get<std::string>("fontPath");
|
|
|
|
|
|
|
|
return get(size, path);
|
|
|
|
}
|