class for loading font files and creating from it new TextureFont objects

This commit is contained in:
Roman Sorokin 2014-06-26 12:50:31 +03:00 committed by Alex Zolotarev
parent 403e81e420
commit a3b89846dd
2 changed files with 186 additions and 0 deletions

114
drape/font_loader.cpp Normal file
View file

@ -0,0 +1,114 @@
#include "font_loader.hpp"
#include "../platform/platform.hpp"
#include "glfunctions.hpp"
#include "glconstants.hpp"
#include "utils/lodepng.h"
#include "../std/string.hpp"
#include "../std/map.hpp"
#include "../std/vector.hpp"
FontLoader::FontLoader()
{
}
void FontLoader::Add(FontChar &symbol)
{
symbol.m_blockNum = (symbol.m_y / m_supportedSize) * m_blockCnt + symbol.m_x / m_supportedSize;
symbol.m_x %= m_supportedSize;
symbol.m_y %= m_supportedSize;
m_dictionary.insert(pair<int, FontChar>(symbol.m_unicode, symbol));
}
int FontLoader::GetBlockByUnicode(int unicode)
{
return m_dictionary[unicode].m_blockNum;
}
FontChar FontLoader::GetSymbolByUnicode(int unicode)
{
return m_dictionary[unicode];
}
vector<TextureFont> FontLoader::Load(string const & path)
{
m_dictionary.clear();
vector<unsigned char> image, buffer;
unsigned w, h;
try
{
ReaderPtr<ModelReader> reader = GetPlatform().GetReader(path + ".png");
uint64_t size = reader.Size();
buffer.resize(size);
reader.Read(0, &buffer[0], size);
}
catch (RootException & e)
{
LOG(LERROR, (e.what()));
return vector<TextureFont>();
}
lodepng::decode(image, w, h, &buffer[0], buffer.size(), LCT_GREY);
m_realSize = w;
int32_t maxTextureSize = GLFunctions::glGetInteger(gl_const::GLMaxTextureSize);
m_supportedSize = min(m_realSize, maxTextureSize);
m_blockCnt = m_realSize / m_supportedSize;
vector<TextureFont> pages;
pages.resize(m_blockCnt * m_blockCnt);
buffer.resize(m_supportedSize * m_supportedSize);
for(int i = 0 ; i < m_blockCnt ; ++i)
{
for(int j = 0; j < m_blockCnt ; ++j)
{
vector<unsigned char>::iterator itr_x = image.begin() + i * m_realSize * m_supportedSize + j * m_supportedSize;
for(int k = 0; k < m_supportedSize ; k++)
{
for(int l = 0; l < m_supportedSize ; l++)
{
buffer[k * m_supportedSize + l] = (*itr_x);
itr_x ++;
}
itr_x += (m_realSize - m_supportedSize);
}
pages[i * m_blockCnt + j].Load(m_supportedSize, buffer.data(), i * m_blockCnt + j);
}
}
string s;
try
{
ReaderPtr<ModelReader> reader = GetPlatform().GetReader(path + ".txt");
reader.ReadAsString(s);
}
catch (RootException & e)
{
LOG(LERROR, (e.what()));
return pages;
}
int pos = s.find("\n");
FontChar symbol;
while(pos != string::npos)
{
string line = s.substr(0, pos);
symbol.ReadFromString(line);
Add(symbol);
pages[symbol.m_blockNum].Add(symbol);
s = s.substr(pos+1, s.length());
pos = s.find("\n");
}
if(s.compare("") != 0)
{
symbol.ReadFromString(s);
Add(symbol);
pages[symbol.m_blockNum].Add(symbol);
}
return pages;
}

72
drape/font_loader.hpp Normal file
View file

@ -0,0 +1,72 @@
#pragma once
#include <fstream>
#include <map>
#include "texture_font.h"
#include "../platform/platform.hpp"
using namespace std;
class FontChar
{
public:
int m_unicode;
int m_x;
int m_y;
int m_height;
int m_width;
int m_xOffset;
int m_yOffset;
int m_spacing;
int m_blockNum;
public:
FontChar(){}
~FontChar(){}
void read(ifstream &in)
{
in>>m_unicode>>m_x>>m_y>>m_width>>m_height>>m_xOffset>>m_yOffset>>m_spacing;
}
void ReadFromString(string & s)
{
vector<string> tokens;
int pos;
pos = s.find("\t");
while(pos != string::npos)
{
tokens.push_back(s.substr(0, pos));
s = s.substr(pos+1, s.length());
pos = s.find("\t");
}
tokens.push_back(s);
m_unicode = atoi(tokens[0].c_str());
m_x = atoi(tokens[1].c_str());
m_y = atoi(tokens[2].c_str());
m_width = atoi(tokens[3].c_str());
m_height = atoi(tokens[4].c_str());
m_xOffset = atoi(tokens[5].c_str());
m_yOffset = atoi(tokens[6].c_str());
m_spacing = atoi(tokens[7].c_str());
}
};
class TextureFont;
class FontLoader
{
private:
int m_realSize;
int m_supportedSize;
int m_blockCnt;
map<int, FontChar> m_dictionary;
public:
FontLoader();
void Add(FontChar &symbol);
int GetBlockByUnicode(int unicode);
FontChar GetSymbolByUnicode(int unicode);
vector<TextureFont> Load(const string &path);
};