Skip to content

Commit

Permalink
Add <font:$family> tag, closes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
twanvl committed May 12, 2020
1 parent 41ed84e commit dbb6d34
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
5 changes: 4 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <font:...> 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
Expand All @@ -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

------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions doc/type/tagged_string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This is written as the character with code 1 in files.
| @<sym>@ The text inside the tag is rendered as symbols, if a [[prop:style:symbol font]] is set for the text box.
| @<color:???>@ The text inside the tag is rendered with the given [[type:color]].
| @<size:???>@ The text inside the tag is rendered with the given font size in points, for example @"<size:12>text</size>"@ 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.
| @<font:???>@ The text inside the tag is rendered with the given font family.
| @<line>@ Line breaks inside this tag use the [[prop:style:line height line]], and they show a horizontal line.
| @<soft-line>@ Line breaks inside this tag use the [[prop:style:soft line height]].
| @<atom>@ An atomic piece of text. The cursor can never be inside it; it is selected as a whole.
Expand Down
5 changes: 4 additions & 1 deletion src/data/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/data/font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class Font : public IntrusivePtrBase<Font> {
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;
Expand Down
10 changes: 10 additions & 0 deletions src/render/text/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct TextElementsFromString {
int param_id;
vector<Color> colors;
vector<double> sizes;
vector<String> fonts;
/// put angle brackets around the text?
bool bracket;

Expand Down Expand Up @@ -138,6 +139,14 @@ struct TextElementsFromString {
} else if (is_substr(text, tag_start, _("</color"))) {
if (!colors.empty()) colors.pop_back();
}
else if (is_substr(text, tag_start, _( "<font"))) {
size_t colon = text.find_first_of(_(">:"), 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, _("</font"))) {
if (!fonts.empty()) fonts.pop_back();
}
else if (is_substr(text, tag_start, _( "<size"))) {
size_t colon = text.find_first_of(_(">:"), tag_start);
if (colon < pos - 1 && text.GetChar(colon) == _(':')) {
Expand Down Expand Up @@ -251,6 +260,7 @@ struct TextElementsFromString {
(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
? &param_colors[(param_id++) % param_colors_count]
: !colors.empty()
Expand Down
2 changes: 1 addition & 1 deletion src/util/tagged_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Expand Down

0 comments on commit dbb6d34

Please sign in to comment.