diff options
author | Andrei Karas <akaras@inbox.ru> | 2013-04-25 16:28:43 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2013-04-25 16:35:59 +0300 |
commit | 684171f30e61ab2c594324a3ff78d0d2df6defb3 (patch) | |
tree | 993b60b0e0511670e5f35420351a3ada78b153f3 /src | |
parent | a2af9029410b20748b053bf0ca6f78ee27efef9e (diff) | |
download | plus-684171f30e61ab2c594324a3ff78d0d2df6defb3.tar.gz plus-684171f30e61ab2c594324a3ff78d0d2df6defb3.tar.bz2 plus-684171f30e61ab2c594324a3ff78d0d2df6defb3.tar.xz plus-684171f30e61ab2c594324a3ff78d0d2df6defb3.zip |
add in sdlfont map support also for calculating string widths.
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/sdlfont.cpp | 20 | ||||
-rw-r--r-- | src/gui/sdlfont.h | 1 | ||||
-rw-r--r-- | src/gui/sdlfont_unittest.cc | 27 |
3 files changed, 47 insertions, 1 deletions
diff --git a/src/gui/sdlfont.cpp b/src/gui/sdlfont.cpp index 37174d8e9..4e71de061 100644 --- a/src/gui/sdlfont.cpp +++ b/src/gui/sdlfont.cpp @@ -226,6 +226,7 @@ void TextChunkList::insertFirst(SDLTextChunk *const item) start = item; size ++; search[SDLTextChunkSmall(item->text, item->color, item->color2)] = item; + searchWidth[item->text] = item; } void TextChunkList::moveToFirst(SDLTextChunk *item) @@ -261,6 +262,7 @@ void TextChunkList::removeBack() start = nullptr; search.erase(SDLTextChunkSmall(oldEnd->text, oldEnd->color, oldEnd->color2)); + searchWidth.erase(oldEnd->text); delete oldEnd; size --; } @@ -276,6 +278,7 @@ void TextChunkList::removeBack(int n) item = item->prev; search.erase(SDLTextChunkSmall(oldEnd->text, oldEnd->color, oldEnd->color2)); + searchWidth.erase(oldEnd->text); delete oldEnd; size --; } @@ -294,6 +297,7 @@ void TextChunkList::removeBack(int n) void TextChunkList::clear() { search.clear(); + searchWidth.clear(); SDLTextChunk *item = start; while (item) { @@ -487,6 +491,20 @@ int SDLFont::getWidth(const std::string &text) const const unsigned char chr = text[0]; TextChunkList *const cache = &mCache[chr]; + std::map<std::string, SDLTextChunk*> &search = cache->searchWidth; + std::map<std::string, SDLTextChunk*>::iterator i = search.find(text); + if (i != search.end()) + { + SDLTextChunk *const chunk = (*i).second; + cache->moveToFirst(chunk); + const Image *const image = chunk->img; + if (image) + return image->getWidth(); + else + return 0; + } + +/* #ifdef DEBUG_FONT int cnt = 0; #endif @@ -515,7 +533,9 @@ int SDLFont::getWidth(const std::string &text) const logger->log(std::string("getWidth: ").append(text).append( ", iterations: ").append(toString(cnt))); #endif +*/ + // if string was not drawed int w, h; getSafeUtf8String(text, strBuf); TTF_SizeUTF8(mFont, strBuf, &w, &h); diff --git a/src/gui/sdlfont.h b/src/gui/sdlfont.h index 41edf4145..9e439279f 100644 --- a/src/gui/sdlfont.h +++ b/src/gui/sdlfont.h @@ -99,6 +99,7 @@ class TextChunkList final SDLTextChunk *end; uint32_t size; std::map<SDLTextChunkSmall, SDLTextChunk*> search; + std::map<std::string, SDLTextChunk*> searchWidth; }; /** diff --git a/src/gui/sdlfont_unittest.cc b/src/gui/sdlfont_unittest.cc index a372d2867..7575772f0 100644 --- a/src/gui/sdlfont_unittest.cc +++ b/src/gui/sdlfont_unittest.cc @@ -35,6 +35,7 @@ TEST(TextChunkList, empty) EXPECT_EQ(nullptr, list.start); EXPECT_EQ(nullptr, list.end); EXPECT_EQ(0, list.search.size()); + EXPECT_EQ(0, list.searchWidth.size()); } TEST(TextChunkList, add1) @@ -53,9 +54,11 @@ TEST(TextChunkList, add1) EXPECT_EQ(nullptr, chunk->next); EXPECT_EQ(1, list.search.size()); - EXPECT_EQ(chunk, (*list.search.find(SDLTextChunkSmall( chunk->text, chunk->color, chunk->color2))).second); + + EXPECT_EQ(1, list.searchWidth.size()); + EXPECT_EQ(chunk, (*list.searchWidth.find(chunk->text)).second); } TEST(TextChunkList, add2) @@ -77,11 +80,15 @@ TEST(TextChunkList, add2) EXPECT_EQ(chunk2, chunk1->next); EXPECT_EQ(chunk1, chunk2->prev); EXPECT_EQ(nullptr, chunk2->next); + EXPECT_EQ(2, list.search.size()); EXPECT_EQ(chunk1, (*list.search.find(SDLTextChunkSmall( chunk1->text, chunk1->color, chunk1->color2))).second); EXPECT_EQ(chunk2, (*list.search.find(SDLTextChunkSmall( chunk2->text, chunk2->color, chunk2->color2))).second); + + EXPECT_EQ(1, list.searchWidth.size()); + EXPECT_EQ(chunk1, (*list.searchWidth.find(chunk1->text)).second); } TEST(TextChunkList, addRemoveBack1) @@ -98,6 +105,7 @@ TEST(TextChunkList, addRemoveBack1) EXPECT_EQ(nullptr, list.start); EXPECT_EQ(nullptr, list.end); EXPECT_EQ(0, list.search.size()); + EXPECT_EQ(0, list.searchWidth.size()); } TEST(TextChunkList, addRemoveBack2) @@ -118,9 +126,13 @@ TEST(TextChunkList, addRemoveBack2) EXPECT_EQ(chunk1, list.end); EXPECT_EQ(nullptr, chunk1->prev); EXPECT_EQ(nullptr, chunk1->next); + EXPECT_EQ(1, list.search.size()); EXPECT_EQ(chunk1, (*list.search.find(SDLTextChunkSmall( chunk1->text, chunk1->color, chunk1->color2))).second); + + EXPECT_EQ(1, list.searchWidth.size()); + EXPECT_EQ(chunk1, (*list.searchWidth.find(chunk1->text)).second); } TEST(TextChunkList, addRemoveBack3) @@ -139,7 +151,9 @@ TEST(TextChunkList, addRemoveBack3) EXPECT_EQ(0, list.size); EXPECT_EQ(nullptr, list.start); EXPECT_EQ(nullptr, list.end); + EXPECT_EQ(0, list.search.size()); + EXPECT_EQ(0, list.searchWidth.size()); } TEST(TextChunkList, addRemoveBack4) @@ -164,9 +178,12 @@ TEST(TextChunkList, addRemoveBack4) EXPECT_EQ(chunk1, list.end); EXPECT_EQ(nullptr, chunk1->prev); EXPECT_EQ(nullptr, chunk1->next); + EXPECT_EQ(1, list.search.size()); EXPECT_EQ(chunk1, (*list.search.find(SDLTextChunkSmall( chunk1->text, chunk1->color, chunk1->color2))).second); + + EXPECT_EQ(0, list.searchWidth.size()); } TEST(TextChunkList, moveToFirst1) @@ -274,6 +291,7 @@ TEST(TextChunkList, clear1) EXPECT_EQ(nullptr, list.end); EXPECT_EQ(chunksLeft, sdlTextChunkCnt); EXPECT_EQ(0, list.search.size()); + EXPECT_EQ(0, list.searchWidth.size()); } TEST(TextChunkList, clear2) @@ -298,6 +316,7 @@ TEST(TextChunkList, clear2) EXPECT_EQ(nullptr, list.end); EXPECT_EQ(chunksLeft, sdlTextChunkCnt); EXPECT_EQ(0, list.search.size()); + EXPECT_EQ(0, list.searchWidth.size()); } TEST(TextChunkList, clear3) @@ -318,14 +337,17 @@ TEST(TextChunkList, clear3) list.moveToFirst(chunk1); EXPECT_EQ(chunksLeft + 3, sdlTextChunkCnt); EXPECT_EQ(3, list.search.size()); + EXPECT_EQ(1, list.searchWidth.size()); list.removeBack(); EXPECT_EQ(chunksLeft + 2, sdlTextChunkCnt); EXPECT_EQ(2, list.search.size()); + EXPECT_EQ(0, list.searchWidth.size()); list.clear(); EXPECT_EQ(chunksLeft, sdlTextChunkCnt); EXPECT_EQ(0, list.search.size()); + EXPECT_EQ(0, list.searchWidth.size()); } TEST(TextChunkList, clear4) @@ -346,14 +368,17 @@ TEST(TextChunkList, clear4) list.moveToFirst(chunk2); EXPECT_EQ(chunksLeft + 3, sdlTextChunkCnt); EXPECT_EQ(3, list.search.size()); + EXPECT_EQ(2, list.searchWidth.size()); list.removeBack(2); EXPECT_EQ(chunksLeft + 1, sdlTextChunkCnt); EXPECT_EQ(1, list.search.size()); + EXPECT_EQ(0, list.searchWidth.size()); list.clear(); EXPECT_EQ(chunksLeft, sdlTextChunkCnt); EXPECT_EQ(0, list.search.size()); + EXPECT_EQ(0, list.searchWidth.size()); } TEST(TextChunkList, sort1) |