From 07839a561fa876539484fdc44e68d3f17e20a89b Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 12 May 2013 18:38:01 +0200 Subject: Unicode symbols for special characters * added support for special characters from original text bitmaps --- src/graphics/engine/text.cpp | 122 ++++++++++++++++++++++++++++++++++++------- src/graphics/engine/text.h | 31 +++++++++-- 2 files changed, 131 insertions(+), 22 deletions(-) diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 308c813..0f0122d 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -56,6 +56,7 @@ CText::CText(CEngine* engine) m_engine = engine; m_defaultSize = 12.0f; + m_tabSize = 4; m_lastFontType = FONT_COLOBOT; m_lastFontSize = 0; @@ -148,6 +149,16 @@ void CText::FlushCache() m_lastCachedFont = nullptr; } +int CText::GetTabSize() +{ + return m_tabSize; +} + +void CText::SetTabSize(int tabSize) +{ + m_tabSize = tabSize; +} + void CText::DrawText(const std::string &text, std::vector::iterator format, std::vector::iterator end, float size, Math::Point pos, float width, TextAlign align, @@ -309,11 +320,16 @@ float CText::GetStringWidth(const std::string &text, return width; } -float CText::GetStringWidth(const std::string &text, FontType font, float size) +float CText::GetStringWidth(std::string text, FontType font, float size) { assert(font != FONT_BUTTON); - // TODO: special chars? + // Skip special chars + for (char& c : text) + { + if (c < 32) + c = ':'; + } CachedFont* cf = GetOrOpenFont(font, size); assert(cf != nullptr); @@ -328,8 +344,16 @@ float CText::GetCharWidth(UTF8Char ch, FontType font, float size, float offset) // TODO: if (font == FONT_BUTTON) if (font == FONT_BUTTON) return 0.0f; - // TODO: special chars? - // TODO: tab sizing + int width = 1; + if (ch.c1 < 32) + { + if (ch.c1 == '\t') + width = m_tabSize; + + // TODO: tab sizing at intervals? + + ch.c1 = ':'; + } CachedFont* cf = GetOrOpenFont(font, size); assert(cf != nullptr); @@ -341,7 +365,7 @@ float CText::GetCharWidth(UTF8Char ch, FontType font, float size, float offset) else tex = CreateCharTexture(ch, cf); - return tex.charSize.x; + return tex.charSize.x * width; } @@ -505,6 +529,63 @@ int CText::Detect(const std::string &text, FontType font, float size, float offs return index; } +UTF8Char CText::TranslateSpecialChar(int specialChar) +{ + UTF8Char ch; + + switch (specialChar) + { + case CHAR_TAB: + ch.c1 = ':'; + ch.c2 = 0; + ch.c3 = 0; + break; + + case CHAR_NEWLINE: + // Unicode: U+21B2 + ch.c1 = 0xE2; + ch.c2 = 0x86; + ch.c3 = 0xB2; + break; + + case CHAR_DOT: + // Unicode: U+23C5 + ch.c1 = 0xE2; + ch.c2 = 0x8F; + ch.c3 = 0x85; + break; + + case CHAR_SQUARE: + // Unicode: U+25FD + ch.c1 = 0xE2; + ch.c2 = 0x97; + ch.c3 = 0xBD; + break; + + case CHAR_SKIP_RIGHT: + // Unicode: U+25B6 + ch.c1 = 0xE2; + ch.c2 = 0x96; + ch.c3 = 0xB6; + break; + + case CHAR_SKIP_LEFT: + // Unicode: U+25C0 + ch.c1 = 0xE2; + ch.c2 = 0x97; + ch.c3 = 0x80; + break; + + default: + ch.c1 = '?'; + ch.c2 = 0; + ch.c3 = 0; + break; + } + + return ch; +} + void CText::DrawString(const std::string &text, std::vector::iterator format, std::vector::iterator end, float size, Math::Point pos, float width, int eol, Color color) @@ -532,7 +613,11 @@ void CText::DrawString(const std::string &text, std::vector::itera float cw = GetCharWidth(ch, font, size, offset); if (offset + cw > width) // exceeds the maximum width? { - // TODO: special end-of-line char + UTF8Char ch = TranslateSpecialChar(CHAR_SKIP_RIGHT); + cw = GetCharWidth(ch, font, size, offset); + pos.x = start + width - cw; + color = Color(1.0f, 0.0f, 0.0f); + DrawCharAndAdjustPos(ch, font, size, pos, color); break; } @@ -550,7 +635,13 @@ void CText::DrawString(const std::string &text, std::vector::itera fmtIndex++; } - // TODO: eol + if (eol != 0) + { + FontType font = FONT_COLOBOT; + UTF8Char ch = TranslateSpecialChar(eol); + color = Color(1.0f, 0.0f, 0.0f); + DrawCharAndAdjustPos(ch, font, size, pos, color); + } } void CText::StringToUTFCharList(const std::string &text, std::vector &chars) @@ -675,23 +766,18 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::P // TODO: if (font == FONT_BUTTON) if (font == FONT_BUTTON) return; - // TODO: special chars? - CachedFont* cf = GetOrOpenFont(font, size); if (cf == nullptr) return; int width = 1; - if (ch.c1 > 0 && ch.c1 < 32) { // FIXME add support for chars with code 9 10 23 - if (ch.c1 == '\t') { - ch.c1 = ':'; - width = 4; - } else { - ch.c1 = ' '; - } - ch.c2 = 0; - ch.c3 = 0; + if (ch.c1 > 0 && ch.c1 < 32) + { + if (ch.c1 == '\t') + width = m_tabSize; + + ch = TranslateSpecialChar(ch.c1); } auto it = cf->cache.find(ch); diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index 6bcc59b..b19a224 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -205,6 +205,20 @@ struct MultisizeFont : fileName(fn) {} }; +/** + * \enum SpecialChar + * \brief Special codes for certain characters + */ +enum SpecialChar +{ + CHAR_TAB = '\t', //! Tab character - : + CHAR_NEWLINE = '\n', //! Newline character - arrow pointing down and left + CHAR_DOT = 1, //! Single dot in the middle + CHAR_SQUARE = 2, //! Square + CHAR_SKIP_RIGHT = 5, //! Filled triangle pointing right + CHAR_SKIP_LEFT = 6 //! Filled triangle pointing left +}; + /** * \class CText * \brief Text rendering engine @@ -240,6 +254,12 @@ public: //! Flushes cached textures void FlushCache(); + //@{ + //! Tab size management + void SetTabSize(int tabSize); + int GetTabSize(); + //@} + //! Draws text (multi-format) void DrawText(const std::string &text, std::vector::iterator format, std::vector::iterator end, @@ -268,11 +288,11 @@ public: float GetHeight(FontType font, float size); //! Returns width of string (multi-format) - TEST_VIRTUAL float GetStringWidth(const std::string &text, - std::vector::iterator format, - std::vector::iterator end, float size); + TEST_VIRTUAL float GetStringWidth(const std::string& text, + std::vector::iterator format, + std::vector::iterator end, float size); //! Returns width of string (single font) - TEST_VIRTUAL float GetStringWidth(const std::string &text, FontType font, float size); + TEST_VIRTUAL float GetStringWidth(std::string text, FontType font, float size); //! Returns width of single character TEST_VIRTUAL float GetCharWidth(UTF8Char ch, FontType font, float size, float offset); @@ -290,6 +310,8 @@ public: //! Returns the most suitable position to a given offset (one font) int Detect(const std::string &text, FontType font, float size, float offset); + UTF8Char TranslateSpecialChar(int specialChar); + protected: CachedFont* GetOrOpenFont(FontType type, float size); CharTexture CreateCharTexture(UTF8Char ch, CachedFont* font); @@ -309,6 +331,7 @@ protected: std::string m_error; float m_defaultSize; + int m_tabSize; std::map m_fonts; -- cgit v1.2.3-1-g7c22