Class for storing unicode symbols

This commit is contained in:
Roman Sorokin 2014-06-26 12:51:20 +03:00 committed by Alex Zolotarev
parent a3b89846dd
commit ca848cf3b0
2 changed files with 82 additions and 0 deletions

47
drape/texture_font.cpp Normal file
View file

@ -0,0 +1,47 @@
#include "texture_font.h"
TextureFont::TextureFont()
{
}
bool TextureFont::FindResource(Texture::Key const & key, m2::RectF & texRect, m2::PointU & pixelSize) const
{
if (key.GetType() != Texture::Key::Font)
return false;
FontKey K = static_cast<FontKey const &>(key);
map<int, FontChar>::iterator itm = m_chars.find(K.GetUnicode());
if(itm == m_chars.end())
return false;
FontChar symbol = itm->second;
pixelSize.x = symbol.m_width;
pixelSize.y = symbol.m_height;
float x_start = (float)symbol.m_x / (float)GetWidth();
float y_start = (float)symbol.m_y / (float)GetHeight();
float dx = (float)symbol.m_width / (float)GetWidth();
float dy = (float)symbol.m_height / (float)GetHeight();
texRect = m2::RectF(x_start, y_start, x_start + dx, y_start + dy);
return true;
}
FontChar TextureFont::GetSymbolByUnicode(int unicode)
{
return m_chars[unicode];
}
void TextureFont::Load(int size, void *data, int32_t blockNum)
{
m_blockNum = blockNum;
Create(size, size, Texture::ALPHA, MakeStackRefPointer<void>(data));
}
void TextureFont::Add(FontChar symbol)
{
m_chars.insert(pair<int, FontChar>(symbol.m_unicode, symbol));
}

35
drape/texture_font.h Normal file
View file

@ -0,0 +1,35 @@
#pragma once
#include <map>
#include "texture.hpp"
#include "font_loader.hpp"
using namespace std;
class FontChar;
class TextureFont : public Texture
{
public:
class FontKey : public Key
{
public:
FontKey(int32_t unicode):m_unicode(unicode) {}
virtual Type GetType() const { return Texture::Key::Font; }
int32_t GetUnicode() const { return m_unicode; }
private:
int32_t m_unicode;
};
public:
TextureFont();
bool FindResource(Key const & key, m2::RectF & texRect, m2::PointU & pixelSize) const;
void Load(int size, void * data, int32_t blockNum);
void Add(FontChar letter);
FontChar GetSymbolByUnicode(int unicode);
private:
mutable map<int, FontChar> m_chars;
int32_t m_blockNum;
};