From dbb6d34bb338b8d0a1c4c21da4b59eda8235412c Mon Sep 17 00:00:00 2001 From: Twan van Laarhoven Date: Tue, 12 May 2020 22:27:57 +0200 Subject: [PATCH] Add tag, closes #24 --- CHANGES.txt | 5 ++++- doc/type/tagged_string.txt | 1 + src/data/font.cpp | 5 ++++- src/data/font.hpp | 4 ++-- src/render/text/element.cpp | 10 ++++++++++ src/util/tagged_string.cpp | 2 +- 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 478039e2..5144bb93 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -17,6 +17,9 @@ Bug fixes: * length function now gives correct results for maps * substr("foo",begin:3) now returns "" instead of true +Template features: + * Added tag to change the font inside a text field. + Scripting: * Added type_name function * nil != "", so missing values are no longer equal to the empty string @@ -26,7 +29,7 @@ Scripting: Internal: * Switch build system to to CMake - * Update code to work with wxWidgets 3.0/3.1 and C++ 17 + * Update code to work with wxWidgets 3.1 and C++ 17 * Lots of code cleanup ------------------------------------------------------------------------------ diff --git a/doc/type/tagged_string.txt b/doc/type/tagged_string.txt index 844f65af..4bbd7f31 100644 --- a/doc/type/tagged_string.txt +++ b/doc/type/tagged_string.txt @@ -17,6 +17,7 @@ This is written as the character with code 1 in files. | @@ The text inside the tag is rendered as symbols, if a [[prop:style:symbol font]] is set for the text box. | @@ The text inside the tag is rendered with the given [[type:color]]. | @@ The text inside the tag is rendered with the given font size in points, for example @"text"@ makes the text 12 points. The text is scaled down proportionally when it does not fit in a text box and the @scale down to@ attribute allows it. +| @@ The text inside the tag is rendered with the given font family. | @@ Line breaks inside this tag use the [[prop:style:line height line]], and they show a horizontal line. | @@ Line breaks inside this tag use the [[prop:style:soft line height]]. | @@ An atomic piece of text. The cursor can never be inside it; it is selected as a whole. diff --git a/src/data/font.cpp b/src/data/font.cpp index 548192af..d5c34b7c 100644 --- a/src/data/font.cpp +++ b/src/data/font.cpp @@ -50,7 +50,7 @@ void Font::initDependencies(Context& ctx, const Dependency& dep) const { shadow_color.initDependencies(ctx, dep); } -FontP Font::make(int add_flags, Color* other_color, double* other_size) const { +FontP Font::make(int add_flags, String const* other_family, Color const* other_color, double const* other_size) const { FontP f(new Font(*this)); f->flags |= add_flags; if (add_flags & FONT_CODE_STRING) { @@ -73,6 +73,9 @@ FontP Font::make(int add_flags, Color* other_color, double* other_size) const { if (other_size) { f->size = *other_size; } + if (other_family && !other_family->empty()) { + f->name = *other_family; + } return f; } diff --git a/src/data/font.hpp b/src/data/font.hpp index b5c50a36..13a055a4 100644 --- a/src/data/font.hpp +++ b/src/data/font.hpp @@ -59,8 +59,8 @@ public: return shadow_displacement.width != 0 || shadow_displacement.height != 0; } - /// Add style to a font, and optionally change the color and size - FontP make(int add_flags, Color* other_color, double* other_size) const; + /// Add style to a font, and optionally change the font family, color and size + FontP make(int add_flags, String const* other_family, Color const* other_color, double const* other_size) const; /// Convert this font to a wxFont wxFont toWxFont(double scale) const; diff --git a/src/render/text/element.cpp b/src/render/text/element.cpp index a6cc1ee6..9b9ae386 100644 --- a/src/render/text/element.cpp +++ b/src/render/text/element.cpp @@ -78,6 +78,7 @@ struct TextElementsFromString { int param_id; vector colors; vector sizes; + vector fonts; /// put angle brackets around the text? bool bracket; @@ -138,6 +139,14 @@ struct TextElementsFromString { } else if (is_substr(text, tag_start, _(":"), tag_start); + if (colon < pos - 1 && text.GetChar(colon) == _(':')) { + fonts.push_back(text.substr(colon+1, pos-colon-2)); + } + } else if (is_substr(text, tag_start, _(":"), tag_start); if (colon < pos - 1 && text.GetChar(colon) == _(':')) { @@ -251,6 +260,7 @@ private: (code > 0 ? FONT_CODE : FONT_NORMAL) | (code_kw > 0 ? FONT_CODE_KW : FONT_NORMAL) | (code_string > 0 ? FONT_CODE_STRING : FONT_NORMAL), + fonts.empty() ? nullptr : &fonts.back(), param > 0 || param_ref > 0 ? ¶m_colors[(param_id++) % param_colors_count] : !colors.empty() diff --git a/src/util/tagged_string.cpp b/src/util/tagged_string.cpp index 4bb65d12..29589949 100644 --- a/src/util/tagged_string.cpp +++ b/src/util/tagged_string.cpp @@ -613,7 +613,7 @@ void check_tagged(const String& str, bool check_balance) { } for (size_t j = i + 1 ; j + 1 < end ; ++j) { Char c = str.GetChar(j); - if (c == _(' ') || c == _('<')) { + if (c == ESCAPED_LANGLE || c == _('<')) { queue_message(MESSAGE_WARNING, _("Invalid character in tag")); } }