From 8025d1075db0779bde635148f6e38303eb29d6c8 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 6 Nov 2022 13:54:02 +0100 Subject: Formatted text with color in matrix, monospace for codeblocks --- include/Text.hpp | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'include/Text.hpp') diff --git a/include/Text.hpp b/include/Text.hpp index 1533380..73dd565 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -21,17 +21,26 @@ namespace mgl { namespace QuickMedia { + static constexpr size_t FONT_ARRAY_SIZE = 6; + + enum FormattedTextFlag : uint8_t { + FORMATTED_TEXT_FLAG_NONE = 0, + //FORMATTED_TEXT_FLAG_BOLD = 1 << 0, + FORMATTED_TEXT_FLAG_MONOSPACE = 1 << 1, + FORMATTED_TEXT_FLAG_COLOR = 1 << 2 + }; + struct TextElement { enum class Type { TEXT, + FORMAT_START, + FORMAT_END, IMAGE }; enum class TextType { - LATIN, - CJK, - SYMBOL, + TEXT, EMOJI }; @@ -39,7 +48,7 @@ namespace QuickMedia void create_text(std::string_view text) { this->text = text; - text_type = TextType::LATIN; + text_type = TextType::TEXT; type = Type::TEXT; } @@ -50,10 +59,14 @@ namespace QuickMedia this->size = size; type = Type::IMAGE; } + + // TODO: Remove some fields // TODO: union grouped std::string_view text; - TextType text_type = TextType::LATIN; + TextType text_type = TextType::TEXT; + mgl::Color color = mgl::Color(255, 255, 255, 255); + uint8_t text_flags = FORMATTED_TEXT_FLAG_NONE; // FormattedTextFlag // TODO: Remove these std::string url; @@ -78,17 +91,17 @@ namespace QuickMedia { public: Text(std::string str, bool bold_font, unsigned int characterSize, float maxWidth, bool highlight_urls = false); - Text(const Text &other); - Text& operator=(const Text&); void setString(std::string str); const std::string& getString() const; void appendText(const std::string &str); // size = {0, 0} = keep original image size - void append_image(const std::string &url, bool local, mgl::vec2i size); static std::string formatted_image(const std::string &url, bool local, mgl::vec2i size); - static std::string formatted_text(const std::string &text, mgl::Color color, bool bold); + // text_flags is bit-or of FormattedTextFlag + static std::string formatted_text(const std::string &text, mgl::Color color, uint8_t text_flags); void insert_text_at_caret_position(const std::string &str); + + static std::string to_printable_string(const std::string &str); void set_position(float x, float y); void set_position(const mgl::vec2f &position); @@ -104,7 +117,7 @@ namespace QuickMedia int getCaretIndex() const; int getNumLines() const; - void set_color(mgl::Color color); + void set_color(mgl::Color color, bool force_color = false); void setLineSpacing(float lineSpacing); void setCharacterSpacing(float characterSpacing); void setEditable(bool editable); @@ -151,7 +164,8 @@ namespace QuickMedia int getPreviousLineClosestPosition(int startIndex) const; int getNextLineClosestPosition(int startIndex) const; - void split_text_by_type(std::vector &text_elements, const std::string &str); + static void split_text_by_type(std::vector &text_elements, std::string_view str, float vspace); + void split_text_by_type(); float font_get_real_height(mgl::Font *font); float get_text_quad_left_side(const VertexRef &vertex_ref) const; @@ -164,14 +178,15 @@ namespace QuickMedia uint32_t get_vertex_codepoint(int index) const; size_t get_string_index_from_caret_index(size_t caret_index) const; private: - std::string str; // TODO: Remove this for non-editable text??? also replace with std::string? then we get more efficient editing of text + std::string str; // TODO: Remove this for non-editable text??? bool bold_font; unsigned int characterSize; - std::array, 5> vertices; - std::array vertex_buffers; + std::array, FONT_ARRAY_SIZE> vertices; + std::array vertex_buffers; float maxWidth; mgl::vec2f position; mgl::Color color; + bool force_color; bool dirty; bool dirtyText; bool dirtyCaret; -- cgit v1.2.3