From 61bfb22f27f5216f989c023a5e39fad7e356d2d6 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Fri, 3 Aug 2012 23:23:13 +0200 Subject: Basic font rendering - added basic font rendering - minor refactoring & fixes --- src/graphics/engine/text.h | 267 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 220 insertions(+), 47 deletions(-) (limited to 'src/graphics/engine/text.h') diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index c2de220..19d9882 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -19,95 +19,268 @@ #pragma once -#include "graphics/engine/engine.h" -#include "graphics/core/device.h" #include "math/point.h" +#include "math/size.h" +#include +#include class CInstanceManager; - namespace Gfx { -const float SMALLFONT = 10.0f; -const float BIGFONT = 15.0f; +class CEngine; +class CDevice; + +//! Standard small font size +const float FONT_SIZE_SMALL = 10.0f; +//! Standard big font size +const float FONT_SIZE_BIG = 15.0f; + +/** + \enum TextAlignType + \brief Type of text alignment */ +enum JustifyType +{ + TEXT_ALIGN_RIGHT, + TEXT_ALIGN_LEFT, + TEXT_ALIGN_CENTER +}; -const float NORMSTRETCH = 0.8f; +/* Font meta char constants */ +//! Type used for font character metainfo +typedef short FontMetaChar; +/** + \enum FontType + \brief Type of font + Bitmask in lower 4 bits (mask 0x00f) */ enum FontType { - FONT_COLOBOT = 0, - FONT_COURIER = 1, - FONT_BUTTON = 2, + //! Flag for bold font subtype + FONT_BOLD = 0x04, + //! Flag for italic font subtype + FONT_ITALIC = 0x08, + + //! Default colobot font used for interface + FONT_COLOBOT = 0x00, + //! Alias for bold colobot font + FONT_COLOBOT_BOLD = FONT_COLOBOT | FONT_BOLD, + //! Alias for italic colobot font + FONT_COLOBOT_ITALIC = FONT_COLOBOT | FONT_ITALIC, + + //! Courier (monospace) font used mainly in code editor (only regular & bold) + FONT_COURIER = 0x01, + //! Alias for bold courier font + FONT_COURIER_BOLD = FONT_COURIER | FONT_BOLD, + + // 0x02 left for possible another font + + //! Pseudo-font loaded from textures for buttons, icons, etc. + FONT_BUTTON = 0x03, }; +/** + \enum FontTitle + \brief Size of font title + + Bitmask in 2 bits left shifted 4 (mask 0x030) */ enum FontTitle { - TITLE_BIG = 0x04, - TITLE_NORM = 0x08, - TITLE_LITTLE = 0x0c, + FONT_TITLE_BIG = 0x01 << 4, + FONT_TITLE_NORM = 0x02 << 4, + FONT_TITLE_LITTLE = 0x03 << 4, }; +/** + \enum FontColor + \brief Font color type (?) + + Bitmask in 3 bits left shifted 6 (mask 0x1c0) */ enum FontColor { - COLOR_LINK = 0x10, - COLOR_TOKEN = 0x20, - COLOR_TYPE = 0x30, - COLOR_CONST = 0x40, - COLOR_REM = 0x50, - COLOR_KEY = 0x60, - COLOR_TABLE = 0x70, + FONT_COLOR_LINK = 0x01 << 6, + FONT_COLOR_TOKEN = 0x02 << 6, + FONT_COLOR_TYPE = 0x03 << 6, + FONT_COLOR_CONST = 0x04 << 6, + FONT_COLOR_REM = 0x05 << 6, + FONT_COLOR_KEY = 0x06 << 6, + FONT_COLOR_TABLE = 0x07 << 6, }; -const short FONT_MASK = 0x03; -const short TITLE_MASK = 0x0c; -const short COLOR_MASK = 0x70; -const short IMAGE_MASK = 0x80; +/** + \enum FontMask + \brief Masks in FontMetaChar for different attributes */ +enum FontMask +{ + //! Mask for FontType + FONT_MASK_FONT = 0x00f, + //! Mask for FontTitle + FONT_MASK_TITLE = 0x030, + //! Mask for FontColor + FONT_MASK_COLOR = 0x1c0, + //! Mask for image bit + FONT_MASK_IMAGE = 0x200 +}; + + +/** + \struct UTF8Char + \brief UTF-8 character in font cache + + Only 3-byte chars are supported */ +struct UTF8Char +{ + char c1, c2, c3; + + explicit UTF8Char(char ch1 = '\0', char ch2 = '\0', char ch3 = '\0') + : c1(ch1), c2(ch2), c3(ch3) {} + + inline bool operator<(const UTF8Char &other) const + { + if (c1 < other.c1) + return true; + else if (c1 > other.c1) + return false; + if (c2 < other.c2) + return true; + else if (c2 > other.c2) + return false; + return c3 < other.c3; + } + + inline bool operator==(const UTF8Char &other) const + { + return c1 == other.c1 && c2 == other.c2 && c3 == other.c3; + } +}; -class CText { +/** + \struct CharTexture + \brief Texture of font character */ +struct CharTexture +{ + unsigned int id; + Math::Size texSize; + Math::Size charSize; + + CharTexture() : id(0) {} +}; + +// Definition is private - in text.cpp +struct CachedFont; + +/** + \struct MultisizeFont + \brief Font with multiple possible sizes */ +struct MultisizeFont +{ + std::string fileName; + std::map fonts; + + MultisizeFont(const std::string &fn) + : fileName(fn) {} +}; + +/** + \class CText + \brief Text rendering engine + + ... */ +class CText +{ public: CText(CInstanceManager *iMan, Gfx::CEngine* engine); ~CText(); + //! Sets the device to be used void SetDevice(Gfx::CDevice *device); - void DrawText(char *string, char *format, int len, Math::Point pos, float width, int justif, float size, float stretch, int eol); - void DrawText(char *string, char *format, Math::Point pos, float width, int justif, float size, float stretch, int eol); - void DrawText(char *string, int len, Math::Point pos, float width, int justif, float size, float stretch, FontType font, int eol); - void DrawText(char *string, Math::Point pos, float width, int justif, float size, float stretch, FontType font, int eol); - void DimText(char *string, char *format, int len, Math::Point pos, int justif, float size, float stretch, Math::Point &start, Math::Point &end); - void DimText(char *string, char *format, Math::Point pos, int justif, float size, float stretch, Math::Point &start, Math::Point &end); - void DimText(char *string, int len, Math::Point pos, int justif, float size, float stretch, FontType font, Math::Point &start, Math::Point &end); - void DimText(char *string, Math::Point pos, int justif, float size, float stretch, FontType font, Math::Point &start, Math::Point &end); + //! Returns the last encountered error + std::string GetError(); - float RetAscent(float size, FontType font); - float RetDescent(float size, FontType font); - float RetHeight(float size, FontType font); + //! Initializes the font engine; must be called after SetDevice() + bool Create(); + //! Frees resources before exit + void Destroy(); - float RetStringWidth(char *string, char *format, int len, float size, float stretch); - float RetStringWidth(char *string, int len, float size, float stretch, FontType font); - float RetCharWidth(int character, float offset, float size, float stretch, FontType font); + //! Flushes cached textures + void FlushCache(); - int Justif(char *string, char *format, int len, float width, float size, float stretch); - int Justif(char *string, int len, float width, float size, float stretch, FontType font); - int Detect(char *string, char *format, int len, float offset, float size, float stretch); - int Detect(char *string, int len, float offset, float size, float stretch, FontType font); + //! Draws text (multi-format) + void DrawText(const std::string &text, const std::vector &format, + Math::Point pos, float width, Gfx::JustifyType justify, float size, + float stretch, int eol); + //! Draws text (one font) + void DrawText(const std::string &text, Gfx::FontType font, + Math::Point pos, float width, Gfx::JustifyType justify, float size, + float stretch, int eol); -protected: - void DrawString(char *string, char *format, int len, Math::Point pos, float width, float size, float stretch, int eol); - void DrawString(char *string, int len, Math::Point pos, float width, float size, float stretch, FontType font, int eol); - void DrawColor(Math::Point pos, float size, float width, int color); - void DrawChar(int character, Math::Point pos, float size, float stretch, FontType font); + //! Calculates dimensions for text (multi-format) + void SizeText(const std::string &text, const std::vector &format, + Math::Point pos, Gfx::JustifyType justify, float size, + Math::Point &start, Math::Point &end); + //! Calculates dimensions for text (one font) + void SizeText(const std::string &text, Gfx::FontType font, + Math::Point pos, Gfx::JustifyType justify, float size, + Math::Point &start, Math::Point &end); + + //! Returns the ascent font metric + float GetAscent(Gfx::FontType font, float size); + //! Returns the descent font metric + float GetDescent(Gfx::FontType font, float size); + //! Returns the height font metric + float GetHeight(Gfx::FontType font, float size); + + //! Returns width of string (multi-format) + float GetStringWidth(const std::string &text, + const std::vector &format, float size); + //! Returns width of string (single font) + float GetStringWidth(const std::string &text, Gfx::FontType font, float size); + //! Returns width of single character + float GetCharWidth(int character, Gfx::FontType font, float size, float offset); + + //! Justifies a line of text (multi-format) + int Justify(const std::string &text, const std::vector &format, + float size, float width); + //! Justifies a line of text (one font) + int Justify(const std::string &text, Gfx::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, + float size, float offset); + //! Returns the most suitable position to a given offset (one font) + int Detect(const std::string &text, Gfx::FontType font, float size, float offset); + +public: // for testing! + Gfx::CachedFont* GetOrOpenFont(Gfx::FontType type, float size); + Gfx::CharTexture CreateCharTexture(const char* utf8Char, Gfx::CachedFont* font); + + void DrawString(const std::string &text, const std::vector &format, + float size, Math::Point pos, float width, int eol); + void DrawString(const std::string &text, Gfx::FontType font, + float size, Math::Point pos, float width, int eol); + void DrawColor(int color, float size, Math::Point pos, float width); + void DrawChar(UTF8Char character, Gfx::FontType font, float size, Math::Point &pos); protected: CInstanceManager* m_iMan; Gfx::CEngine* m_engine; Gfx::CDevice* m_device; + std::string m_error; + float m_defaultSize; + std::string m_fontPath; + + std::map m_fonts; + + Gfx::FontType m_lastFontType; + int m_lastFontSize; + Gfx::CachedFont* m_lastCachedFont; }; }; // namespace Gfx -- cgit v1.2.3-1-g7c22 From f7e78b21e9655604ba6fba1d068a9bf7f00b85a5 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Mon, 6 Aug 2012 20:20:50 +0200 Subject: Font rendering - implemented rest of CText interface excluding some minor features --- src/graphics/engine/text.h | 67 +++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 27 deletions(-) (limited to 'src/graphics/engine/text.h') diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index 19d9882..6209c39 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -38,9 +38,9 @@ const float FONT_SIZE_SMALL = 10.0f; const float FONT_SIZE_BIG = 15.0f; /** - \enum TextAlignType + \enum TextAlign \brief Type of text alignment */ -enum JustifyType +enum TextAlign { TEXT_ALIGN_RIGHT, TEXT_ALIGN_LEFT, @@ -86,6 +86,8 @@ enum FontType \enum FontTitle \brief Size of font title + Used internally by CEdit + Bitmask in 2 bits left shifted 4 (mask 0x030) */ enum FontTitle { @@ -95,19 +97,20 @@ enum FontTitle }; /** - \enum FontColor - \brief Font color type (?) + \enum FontHighlight + \brief Type of color highlight for text Bitmask in 3 bits left shifted 6 (mask 0x1c0) */ -enum FontColor +enum FontHighlight { - FONT_COLOR_LINK = 0x01 << 6, - FONT_COLOR_TOKEN = 0x02 << 6, - FONT_COLOR_TYPE = 0x03 << 6, - FONT_COLOR_CONST = 0x04 << 6, - FONT_COLOR_REM = 0x05 << 6, - FONT_COLOR_KEY = 0x06 << 6, - FONT_COLOR_TABLE = 0x07 << 6, + FONT_HIGHLIGHT_NONE = 0x00 << 6, + FONT_HIGHLIGHT_LINK = 0x01 << 6, + FONT_HIGHLIGHT_TOKEN = 0x02 << 6, + FONT_HIGHLIGHT_TYPE = 0x03 << 6, + FONT_HIGHLIGHT_CONST = 0x04 << 6, + FONT_HIGHLIGHT_REM = 0x05 << 6, + FONT_HIGHLIGHT_KEY = 0x06 << 6, + FONT_HIGHLIGHT_TABLE = 0x07 << 6, }; /** @@ -119,9 +122,9 @@ enum FontMask FONT_MASK_FONT = 0x00f, //! Mask for FontTitle FONT_MASK_TITLE = 0x030, - //! Mask for FontColor - FONT_MASK_COLOR = 0x1c0, - //! Mask for image bit + //! Mask for FontHighlight + FONT_MASK_HIGHLIGHT = 0x1c0, + //! Mask for image bit (TODO: not used?) FONT_MASK_IMAGE = 0x200 }; @@ -190,7 +193,17 @@ struct MultisizeFont \class CText \brief Text rendering engine - ... */ + CText is responsible for drawing text in 2D interface. Font rendering is done using + textures generated by SDL_ttf from TTF font files. + + All functions rendering text are divided into two types: + - single font - function takes a single Gfx::FontType argument that (along with size) + determines the font to be used for all characters, + - multi-font - function takes the text as one argument and a std::vector of FontMetaChar + with per-character formatting information (font, highlights and some other info used by CEdit) + + All font rendering is done in UTF-8. +*/ class CText { public: @@ -213,20 +226,20 @@ public: //! Draws text (multi-format) void DrawText(const std::string &text, const std::vector &format, - Math::Point pos, float width, Gfx::JustifyType justify, float size, - float stretch, int eol); + float size, Math::Point pos, float width, Gfx::TextAlign align, + int eol); //! Draws text (one font) void DrawText(const std::string &text, Gfx::FontType font, - Math::Point pos, float width, Gfx::JustifyType justify, float size, - float stretch, int eol); + float size, Math::Point pos, float width, Gfx::TextAlign align, + int eol); //! Calculates dimensions for text (multi-format) void SizeText(const std::string &text, const std::vector &format, - Math::Point pos, Gfx::JustifyType justify, float size, + float size, Math::Point pos, Gfx::TextAlign align, Math::Point &start, Math::Point &end); //! Calculates dimensions for text (one font) void SizeText(const std::string &text, Gfx::FontType font, - Math::Point pos, Gfx::JustifyType justify, float size, + float size, Math::Point pos, Gfx::TextAlign align, Math::Point &start, Math::Point &end); //! Returns the ascent font metric @@ -242,7 +255,7 @@ public: //! Returns width of string (single font) float GetStringWidth(const std::string &text, Gfx::FontType font, float size); //! Returns width of single character - float GetCharWidth(int character, Gfx::FontType font, float size, float offset); + float GetCharWidth(Gfx::UTF8Char ch, Gfx::FontType font, float size, float offset); //! Justifies a line of text (multi-format) int Justify(const std::string &text, const std::vector &format, @@ -256,16 +269,16 @@ public: //! Returns the most suitable position to a given offset (one font) int Detect(const std::string &text, Gfx::FontType font, float size, float offset); -public: // for testing! +protected: Gfx::CachedFont* GetOrOpenFont(Gfx::FontType type, float size); - Gfx::CharTexture CreateCharTexture(const char* utf8Char, Gfx::CachedFont* font); + Gfx::CharTexture CreateCharTexture(Gfx::UTF8Char ch, Gfx::CachedFont* font); void DrawString(const std::string &text, const std::vector &format, float size, Math::Point pos, float width, int eol); void DrawString(const std::string &text, Gfx::FontType font, float size, Math::Point pos, float width, int eol); - void DrawColor(int color, float size, Math::Point pos, float width); - void DrawChar(UTF8Char character, Gfx::FontType font, float size, Math::Point &pos); + void DrawHighlight(Gfx::FontHighlight hl, Math::Point pos, Math::Size size); + void DrawChar(Gfx::UTF8Char ch, Gfx::FontType font, float size, Math::Point &pos); protected: CInstanceManager* m_iMan; -- cgit v1.2.3-1-g7c22 From 7f80ca297154809523cd533edf1842ab1ae391aa Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sat, 11 Aug 2012 17:17:04 +0200 Subject: Render mode setting, refactoring - finished SetState in CEngine - refactored Size and IntSize back to Point and IntPoint - other minor changes in CEngine --- src/graphics/engine/text.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/graphics/engine/text.h') diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index 6209c39..7e2f84b 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -20,7 +20,6 @@ #pragma once #include "math/point.h" -#include "math/size.h" #include #include @@ -168,8 +167,8 @@ struct UTF8Char struct CharTexture { unsigned int id; - Math::Size texSize; - Math::Size charSize; + Math::Point texSize; + Math::Point charSize; CharTexture() : id(0) {} }; @@ -277,7 +276,7 @@ protected: float size, Math::Point pos, float width, int eol); void DrawString(const std::string &text, Gfx::FontType font, float size, Math::Point pos, float width, int eol); - void DrawHighlight(Gfx::FontHighlight hl, Math::Point pos, Math::Size size); + void DrawHighlight(Gfx::FontHighlight hl, Math::Point pos, Math::Point size); void DrawChar(Gfx::UTF8Char ch, Gfx::FontType font, float size, Math::Point &pos); protected: -- cgit v1.2.3-1-g7c22 From 1996507fd3d4d9de90de99845b71a6bf3fbe62da Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sat, 11 Aug 2012 18:39:16 +0200 Subject: Documentation update - updated Doxyfile - added/changed file, dir and namespace descriptions - fixed some errors in doxygen tags --- src/graphics/engine/text.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/graphics/engine/text.h') diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index 7e2f84b..24251ab 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -15,7 +15,10 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -// text.h +/** + * \file graphics/engine/text.h + * \brief Text rendering - Gfx::CText class + */ #pragma once -- cgit v1.2.3-1-g7c22