From 9f4c1454dc75d697b43a634927ebcfd6186a4e93 Mon Sep 17 00:00:00 2001 From: erihel Date: Thu, 27 Sep 2012 23:42:52 +0200 Subject: * Ingame help should now display correctly --- src/graphics/engine/text.cpp | 57 +++++++++++++++++++++++++++++++------------- src/graphics/engine/text.h | 12 +++++----- 2 files changed, 47 insertions(+), 22 deletions(-) (limited to 'src/graphics/engine') diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 00b6617..a77a6fd 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -149,7 +149,7 @@ void CText::FlushCache() } } -void CText::DrawText(const std::string &text, const std::vector &format, +void CText::DrawText(const std::string &text, std::map &format, float size, Math::Point pos, float width, TextAlign align, int eol) { @@ -193,7 +193,7 @@ void CText::DrawText(const std::string &text, FontType font, DrawString(text, font, size, pos, width, eol); } -void CText::SizeText(const std::string &text, const std::vector &format, +void CText::SizeText(const std::string &text, std::map &format, float size, Math::Point pos, TextAlign align, Math::Point &start, Math::Point &end) { @@ -277,16 +277,20 @@ float CText::GetHeight(FontType font, float size) float CText::GetStringWidth(const std::string &text, - const std::vector &format, float size) + std::map &format, float size) { - assert(StrUtils::Utf8StringLength(text) == format.size()); + // TODO assert was commented as new code uses map not vector and if's size doesn't have to match text length + // this has to be tested if it's correct + //assert(StrUtils::Utf8StringLength(text) == format.size()); float width = 0.0f; unsigned int index = 0; unsigned int fmtIndex = 0; while (index < text.length()) { - FontType font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + FontType font = FONT_COLOBOT; + if (format.count(fmtIndex)) + font = static_cast(format[fmtIndex] & FONT_MASK_FONT); UTF8Char ch; @@ -343,10 +347,12 @@ float CText::GetCharWidth(UTF8Char ch, FontType font, float size, float offset) } -int CText::Justify(const std::string &text, const std::vector &format, +int CText::Justify(const std::string &text, std::map &format, float size, float width) { - assert(StrUtils::Utf8StringLength(text) == format.size()); + // TODO assert was commented as new code uses map not vector and if's size doesn't have to match text length + // this has to be tested if it's correct + //assert(StrUtils::Utf8StringLength(text) == format.size()); float pos = 0.0f; int cut = 0; @@ -354,7 +360,9 @@ int CText::Justify(const std::string &text, const std::vector &for unsigned int fmtIndex = 0; while (index < text.length()) { - FontType font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + FontType font = FONT_COLOBOT; + if (format.count(fmtIndex)) + font = static_cast(format[fmtIndex] & FONT_MASK_FONT); UTF8Char ch; @@ -426,17 +434,21 @@ int CText::Justify(const std::string &text, FontType font, float size, float wid return index; } -int CText::Detect(const std::string &text, const std::vector &format, +int CText::Detect(const std::string &text, std::map &format, float size, float offset) { - assert(StrUtils::Utf8StringLength(text) == format.size()); + // TODO assert was commented as new code uses map not vector and if's size doesn't have to match text length + // this has to be tested if it's correct + //assert(StrUtils::Utf8StringLength(text) == format.size()); float pos = 0.0f; unsigned int index = 0; unsigned int fmtIndex = 0; while (index < text.length()) { - FontType font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + FontType font = FONT_COLOBOT; + if (format.count(fmtIndex)) + font = static_cast(format[fmtIndex] & FONT_MASK_FONT); // TODO: if (font == FONT_BUTTON) if (font == FONT_BUTTON) continue; @@ -499,10 +511,12 @@ int CText::Detect(const std::string &text, FontType font, float size, float offs return index; } -void CText::DrawString(const std::string &text, const std::vector &format, +void CText::DrawString(const std::string &text, std::map &format, float size, Math::Point pos, float width, int eol) { - assert(StrUtils::Utf8StringLength(text) == format.size()); + // TODO assert was commented as new code uses map not vector and if's size doesn't have to match text length + // this has to be tested if it's correct + //assert(StrUtils::Utf8StringLength(text) == format.size()); m_engine->SetState(ENG_RSTATE_TEXT); @@ -515,7 +529,9 @@ void CText::DrawString(const std::string &text, const std::vector StringToUTFCharList(text, chars); for (auto it = chars.begin(); it != chars.end(); ++it) { - font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + FontType font = FONT_COLOBOT; + if (format.count(fmtIndex)) + font = static_cast(format[fmtIndex] & FONT_MASK_FONT); // TODO: if (font == FONT_BUTTON) if (font == FONT_BUTTON) continue; @@ -674,6 +690,15 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::P if (cf == nullptr) return; + + int width = 1; + if (ch.c1 < 32) { // FIXME add support for chars with code 9 10 23 + ch.c1 = ' '; + ch.c2 = 0; + ch.c3 = 0; + if (ch.c1 == '\t') + width = 4; + } auto it = cf->cache.find(ch); CharTexture tex; @@ -692,7 +717,7 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::P } Math::Point p1(pos.x, pos.y + tex.charSize.y - tex.texSize.y); - Math::Point p2(pos.x + tex.texSize.x, pos.y + tex.charSize.y); + Math::Point p2(pos.x + tex.texSize.x * width, pos.y + tex.charSize.y); Math::Vector n(0.0f, 0.0f, -1.0f); // normal @@ -708,7 +733,7 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::P m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, quad, 4); m_engine->AddStatisticTriangle(2); - pos.x += tex.charSize.x; + pos.x += tex.charSize.x * width; } CachedFont* CText::GetOrOpenFont(FontType font, float size) diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index bb9a32d..cc18f55 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -239,7 +239,7 @@ public: void FlushCache(); //! Draws text (multi-format) - void DrawText(const std::string &text, const std::vector &format, + void DrawText(const std::string &text, std::map &format, float size, Math::Point pos, float width, TextAlign align, int eol); //! Draws text (one font) @@ -248,7 +248,7 @@ public: int eol); //! Calculates dimensions for text (multi-format) - void SizeText(const std::string &text, const std::vector &format, + void SizeText(const std::string &text, std::map &format, float size, Math::Point pos, TextAlign align, Math::Point &start, Math::Point &end); //! Calculates dimensions for text (one font) @@ -265,20 +265,20 @@ public: //! Returns width of string (multi-format) float GetStringWidth(const std::string &text, - const std::vector &format, float size); + std::map &format, float size); //! Returns width of string (single font) float GetStringWidth(const std::string &text, FontType font, float size); //! Returns width of single character float GetCharWidth(UTF8Char ch, FontType font, float size, float offset); //! Justifies a line of text (multi-format) - int Justify(const std::string &text, const std::vector &format, + int Justify(const std::string &text, std::map &format, float size, float width); //! Justifies a line of text (one font) int Justify(const std::string &text, FontType font, float size, float width); //! Returns the most suitable position to a given offset (multi-format) - int Detect(const std::string &text, const std::vector &format, + int Detect(const std::string &text, std::map &format, float size, float offset); //! Returns the most suitable position to a given offset (one font) int Detect(const std::string &text, FontType font, float size, float offset); @@ -287,7 +287,7 @@ protected: CachedFont* GetOrOpenFont(FontType type, float size); CharTexture CreateCharTexture(UTF8Char ch, CachedFont* font); - void DrawString(const std::string &text, const std::vector &format, + void DrawString(const std::string &text, std::map &format, float size, Math::Point pos, float width, int eol); void DrawString(const std::string &text, FontType font, float size, Math::Point pos, float width, int eol); -- cgit v1.2.3-1-g7c22