diff --git a/drape/drape_tests/font_loader_tests.cpp b/drape/drape_tests/font_loader_tests.cpp index fc7e507029..af46c7ae02 100644 --- a/drape/drape_tests/font_loader_tests.cpp +++ b/drape/drape_tests/font_loader_tests.cpp @@ -11,22 +11,25 @@ using ::testing::Return; using ::testing::InSequence; using ::testing::AnyNumber; -void TestCoords(m2::RectF &texRect, float x1, float y1, float x2, float y2) +namespace { - TEST_ALMOST_EQUAL(texRect.minX(), x1, ("TESTING_TEXTURE_COORDS_FAILURE")) - TEST_ALMOST_EQUAL(texRect.minY(), y1, ("TESTING_TEXTURE_COORDS_FAILURE")) - TEST_ALMOST_EQUAL(texRect.maxX(), x2, ("TESTING_TEXTURE_COORDS_FAILURE")) - TEST_ALMOST_EQUAL(texRect.maxY(), y2, ("TESTING_TEXTURE_COORDS_FAILURE")) -} + void TestCoords(m2::RectF const & texRect, float x1, float y1, float x2, float y2) + { + TEST_ALMOST_EQUAL(texRect.minX(), x1, ("TESTING_TEXTURE_COORDS_FAILURE")) + TEST_ALMOST_EQUAL(texRect.minY(), y1, ("TESTING_TEXTURE_COORDS_FAILURE")) + TEST_ALMOST_EQUAL(texRect.maxX(), x2, ("TESTING_TEXTURE_COORDS_FAILURE")) + TEST_ALMOST_EQUAL(texRect.maxY(), y2, ("TESTING_TEXTURE_COORDS_FAILURE")) + } -void PrepareOpenGL(int size) -{ - EXPECTGL(glGetInteger(gl_const::GLMaxTextureSize)).WillOnce(Return(size)); - EXPECTGL(glBindTexture(_)).Times(AnyNumber()); - EXPECTGL(glDeleteTexture(_)).Times(AnyNumber()); - EXPECTGL(glTexParameter(_, _)).Times(AnyNumber()); - EXPECTGL(glTexImage2D(_, _, _, _, _)).Times(AnyNumber()); - EXPECTGL(glGenTexture()).Times(AnyNumber()); + void PrepareOpenGL(int size) + { + EXPECTGL(glGetInteger(gl_const::GLMaxTextureSize)).WillOnce(Return(size)); + EXPECTGL(glBindTexture(_)).Times(AnyNumber()); + EXPECTGL(glDeleteTexture(_)).Times(AnyNumber()); + EXPECTGL(glTexParameter(_, _)).Times(AnyNumber()); + EXPECTGL(glTexImage2D(_, _, _, _, _)).Times(AnyNumber()); + EXPECTGL(glGenTexture()).Times(AnyNumber()); + } } UNIT_TEST(SimpleFontLoading_1024) diff --git a/drape/font_loader.cpp b/drape/font_loader.cpp index 01aac996d6..6ce51caa54 100644 --- a/drape/font_loader.cpp +++ b/drape/font_loader.cpp @@ -24,7 +24,7 @@ using boost::gil::gray8_pixel_t; namespace { - void CreateFontChar(string & s, FontChar & symbol) + void CreateFontChar(string const & s, FontChar & symbol) { vector tokens; strings::Tokenize(s, "\t", MakeBackInsertFunctor(tokens)); @@ -40,15 +40,21 @@ namespace } } -void FontLoader::Add(FontChar & symbol) +void FontLoader::Add(FontChar const & 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(make_pair(symbol.m_unicode, symbol)); } -int FontLoader::GetBlockByUnicode(int unicode) +int FontLoader::GetSymbolCoords(FontChar & symbol) const +{ + int block = (symbol.m_y / m_supportedSize) * m_blockCnt + symbol.m_x / m_supportedSize; + symbol.m_blockNum = block; + symbol.m_x %= m_supportedSize; + symbol.m_y %= m_supportedSize; + return block; +} + +int FontLoader::GetBlockByUnicode(int unicode) const { FontChar symbol; if(GetSymbolByUnicode(unicode, symbol)) @@ -57,7 +63,7 @@ int FontLoader::GetBlockByUnicode(int unicode) return -1; } -bool FontLoader::GetSymbolByUnicode(int unicode, FontChar & symbol) +bool FontLoader::GetSymbolByUnicode(int unicode, FontChar & symbol) const { map::const_iterator itm = m_dictionary.find(unicode); if(itm == m_dictionary.end()) @@ -87,6 +93,7 @@ vector FontLoader::Load(string const & path) int w, h, bpp; unsigned char * data = stbi_png_load_from_memory(&buffer[0], buffer.size(), &w, &h, &bpp, 0); + CHECK(bpp == 1, ("WRONG_FONT_TEXTURE_FORMAT")); m_realSize = w; int32_t maxTextureSize = GLFunctions::glGetInteger(gl_const::GLMaxTextureSize); @@ -109,7 +116,7 @@ vector FontLoader::Load(string const & path) pages[i * m_blockCnt + j].Load(m_supportedSize, &buffer[0], i * m_blockCnt + j); } } - delete [] data; + stbi_image_free(data); buffer.clear(); string s; @@ -132,8 +139,8 @@ vector FontLoader::Load(string const & path) { FontChar symbol; CreateFontChar(tokens[i], symbol); + pages[GetSymbolCoords(symbol)].Add(symbol); Add(symbol); - pages[symbol.m_blockNum].Add(symbol); } return pages; diff --git a/drape/font_loader.hpp b/drape/font_loader.hpp index 2ad9844217..dd2b8c49bd 100644 --- a/drape/font_loader.hpp +++ b/drape/font_loader.hpp @@ -1,14 +1,13 @@ #pragma once -#include "../std/map.hpp" -#include "../base/string_utils.hpp" #include "texture_font.hpp" + #include "../platform/platform.hpp" +#include "../base/string_utils.hpp" #include "../base/stl_add.hpp" - -using namespace std; +#include "../std/map.hpp" struct FontChar { @@ -35,8 +34,9 @@ private: map m_dictionary; public: - void Add(FontChar & symbol); - int GetBlockByUnicode(int unicode); - bool GetSymbolByUnicode(int unicode, FontChar & symbol); + void Add(FontChar const & symbol); + int GetSymbolCoords(FontChar & symbol) const; + int GetBlockByUnicode(int unicode) const; + bool GetSymbolByUnicode(int unicode, FontChar & symbol) const; vector Load(string const & path); }; diff --git a/drape/symbols_texture.cpp b/drape/symbols_texture.cpp index d76deb1367..bc28f4cd29 100644 --- a/drape/symbols_texture.cpp +++ b/drape/symbols_texture.cpp @@ -1,6 +1,5 @@ #include "symbols_texture.hpp" -//#include "utils/lodepng.h" #include "utils/stb_image.h" #include "../platform/platform.hpp" @@ -46,7 +45,7 @@ void SymbolsTexture::Load(string const & skinPathName) ASSERT(width == w && height == h, ()); Create(width, height, Texture::RGBA8, MakeStackRefPointer(data)); - delete [] data; + stbi_image_free(data); } bool SymbolsTexture::FindResource(Texture::Key const & key, m2::RectF & texRect, m2::PointU & pixelSize) const diff --git a/drape/texture_font.cpp b/drape/texture_font.cpp index 213cfcf3cb..8dc8bac2a4 100644 --- a/drape/texture_font.cpp +++ b/drape/texture_font.cpp @@ -24,7 +24,7 @@ bool TextureFont::FindResource(Texture::Key const & key, m2::RectF & texRect, m2 return true; } -bool TextureFont::GetSymbolByUnicode(int unicode, FontChar & symbol) +bool TextureFont::GetSymbolByUnicode(int unicode, FontChar & symbol) const { map::const_iterator itm = m_chars.find(unicode); if(itm == m_chars.end()) diff --git a/drape/texture_font.hpp b/drape/texture_font.hpp index 5cc7b741ec..8f570730ea 100644 --- a/drape/texture_font.hpp +++ b/drape/texture_font.hpp @@ -13,7 +13,7 @@ public: class FontKey : public Key { public: - FontKey(int32_t unicode):m_unicode(unicode) {} + FontKey(int32_t unicode) : m_unicode(unicode) {} virtual Type GetType() const { return Texture::Key::Font; } int32_t GetUnicode() const { return m_unicode; } @@ -26,7 +26,7 @@ public: void Load(int size, void * data, int32_t blockNum); void Add(FontChar const & letter); - bool GetSymbolByUnicode(int unicode, FontChar & symbol); + bool GetSymbolByUnicode(int unicode, FontChar & symbol) const; private: map m_chars;