From ca848cf3b0911d22390c5dabf0edb68f48afd71e Mon Sep 17 00:00:00 2001 From: Roman Sorokin Date: Thu, 26 Jun 2014 12:51:20 +0300 Subject: [PATCH] Class for storing unicode symbols --- drape/texture_font.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++ drape/texture_font.h | 35 +++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 drape/texture_font.cpp create mode 100644 drape/texture_font.h diff --git a/drape/texture_font.cpp b/drape/texture_font.cpp new file mode 100644 index 0000000000..5344b1c0c0 --- /dev/null +++ b/drape/texture_font.cpp @@ -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(key); + + map::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(data)); +} + +void TextureFont::Add(FontChar symbol) +{ + m_chars.insert(pair(symbol.m_unicode, symbol)); +} diff --git a/drape/texture_font.h b/drape/texture_font.h new file mode 100644 index 0000000000..7eeab6021e --- /dev/null +++ b/drape/texture_font.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#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 m_chars; + int32_t m_blockNum; +};