Margin top in <margin> tag

This commit is contained in:
Twan van Laarhoven
2020-05-17 22:52:56 +02:00
parent b3ddb295fc
commit ab4e7e59f3
5 changed files with 25 additions and 15 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ Template features:
* Added <font:...> tag to change the font inside a text field.
* Added <margin:...> tag to change the margins of a block of text.
* Added <align:...> tag to change the horizontal alignment of a block of text.
* Added <li> tag for list bullet points.
* Added <li> tag for list bullet points. (Experimental!)
* Colors can now be written using hex notation, #rrggbb / #rrggbbaa, and short hex notation (#rgb / #rgba)
Scripting:
+1 -1
View File
@@ -19,7 +19,7 @@ This is written as the character with code 1 in files.
| @<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.
| @<align:???>@ The block inside the tag is aligned with the given horizontal [[type:alignment]]
| @<margin:??:??>@ The block inside the tag has additional left and right (optional) margins of the specified size in pixels.
| @<margin:??:??>@ The block inside the tag has additional left, right (optional), and top (optional) margins of the specified size in pixels.
| @<li>@ 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 @<li>@ tag.
| @<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]].
+20 -11
View File
@@ -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<Color> colors;
vector<double> sizes;
vector<String> fonts;
vector<pair<double,double>> margins;
vector<Margins> margins;
vector<Alignment> aligns;
const TextStyle& style;
@@ -169,16 +173,20 @@ private:
size_t colon = text.find_first_of(_(">:"), 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 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, _("</margin"))) {
if (!margins.empty()) margins.pop_back();
@@ -267,8 +275,9 @@ private:
paragraphs.back().start = i + 1;
paragraphs.back().margin_end_char = i + 1;
if (!margins.empty()) {
paragraphs.back().margin_left = margins.back().first;
paragraphs.back().margin_right = margins.back().second;
paragraphs.back().margin_left = margins.back().left;
paragraphs.back().margin_right = margins.back().right;
paragraphs.back().margin_top = margins.back().top;
}
if (!aligns.empty()) {
paragraphs.back().alignment = aligns.back();
+1 -1
View File
@@ -149,7 +149,7 @@ class TextParagraph {
public:
optional<Alignment> 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)
};
+1
View File
@@ -691,6 +691,7 @@ bool TextViewer::prepareLinesAtScale(RotatedDC& dc, const vector<CharInfo>& 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;