From ab4e7e59f3edf7e1900f0fc22b2f8c34db6efa45 Mon Sep 17 00:00:00 2001 From: Twan van Laarhoven Date: Sun, 17 May 2020 22:52:56 +0200 Subject: [PATCH] Margin top in tag --- CHANGES.txt | 2 +- doc/type/tagged_string.txt | 2 +- src/render/text/element.cpp | 33 +++++++++++++++++++++------------ src/render/text/element.hpp | 2 +- src/render/text/viewer.cpp | 1 + 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index e0f522fd..3036db0a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -23,7 +23,7 @@ Template features: * Added tag to change the font inside a text field. * Added tag to change the margins of a block of text. * Added tag to change the horizontal alignment of a block of text. - * Added
  • tag for list bullet points. + * Added
  • tag for list bullet points. (Experimental!) * Colors can now be written using hex notation, #rrggbb / #rrggbbaa, and short hex notation (#rgb / #rgba) Scripting: diff --git a/doc/type/tagged_string.txt b/doc/type/tagged_string.txt index 4d1158dd..30d88752 100644 --- a/doc/type/tagged_string.txt +++ b/doc/type/tagged_string.txt @@ -19,7 +19,7 @@ This is written as the character with code 1 in files. | @@ 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. | @@ The block inside the tag is aligned with the given horizontal [[type:alignment]] -| @@ The block inside the tag has additional left and right (optional) margins of the specified size in pixels. +| @@ The block inside the tag has additional left, right (optional), and top (optional) margins of the specified size in pixels. | @
  • @ The text inside the tag is treated as a list marker, meaning that if the line wraps it will be indented to match the content of the @
  • @ tag. | @@ 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]]. diff --git a/src/render/text/element.cpp b/src/render/text/element.cpp index c1bde208..3a90313e 100644 --- a/src/render/text/element.cpp +++ b/src/render/text/element.cpp @@ -27,6 +27,10 @@ Color param_colors[] = }; const size_t param_colors_count = sizeof(param_colors) / sizeof(param_colors[0]); +struct Margins { + double left, right, top; +}; + // Helper class for TextElements::fromString, to allow persistent formating state accross recusive calls struct TextElementsFromString { // What formatting is enabled? @@ -37,7 +41,7 @@ struct TextElementsFromString { vector colors; vector sizes; vector fonts; - vector> margins; + vector margins; vector aligns; const TextStyle& style; @@ -168,17 +172,21 @@ private: } else if (is_substr(text, tag_start, _(":"), tag_start); if (colon < pos - 1 && text.GetChar(colon) == _(':')) { - size_t colon2 = text.find_first_of(_(">:"), colon+1); - double margin_left = 0., margin_right = 0.; - text.substr(colon + 1, colon2 - colon - 2).ToDouble(&margin_left); - text.substr(colon2 + 1, pos - colon2 - 2).ToDouble(&margin_right); + size_t colon2 = text.find_first_of(_(">:"), colon + 1); + size_t colon3 = colon2 < pos-1 ? text.find_first_of(_(">:"), colon2 + 1) : colon2; + Margins m = {0.,0.,0.}; + text.substr(colon + 1, colon2 - colon - 2).ToDouble(&m.left); + text.substr(colon2 + 1, colon3 - colon2 - 2).ToDouble(&m.right); + text.substr(colon3 + 1, pos - colon3 - 2).ToDouble(&m.top); if (!margins.empty()) { - margin_left += margins.back().first; - margin_right += margins.back().second; + m.left += margins.back().left; + m.right += margins.back().right; + m.top += margins.back().top; } - margins.emplace_back(margin_left, margin_right); - paragraphs.back().margin_left = margin_left; - paragraphs.back().margin_right = margin_right; + margins.emplace_back(m); + paragraphs.back().margin_left = m.left; + paragraphs.back().margin_right = m.right; + paragraphs.back().margin_top = m.top; } } else if (is_substr(text, tag_start, _(" alignment; double margin_left = 0., margin_right = 0.; - //double margin_top = 0., margin_bottom = 0.; // TODO: more margin options? + double margin_top = 0.; //, margin_bottom = 0.; // TODO: more margin options? size_t start = String::npos, end = String::npos; size_t margin_end_char = 0; // end position of characters that are added to the margin (i.e. bullet points) }; diff --git a/src/render/text/viewer.cpp b/src/render/text/viewer.cpp index ecd01360..bffc25c0 100644 --- a/src/render/text/viewer.cpp +++ b/src/render/text/viewer.cpp @@ -691,6 +691,7 @@ bool TextViewer::prepareLinesAtScale(RotatedDC& dc, const vector& char assert(elements.paragraphs[i_para].start == i + 1); line.margin_left = elements.paragraphs[i_para].margin_left; line.margin_right = elements.paragraphs[i_para].margin_right; + line.top += elements.paragraphs[i_para].margin_top; line.alignment = elements.paragraphs[i_para].alignment; } line.break_after = LineBreak::NO;