Skip to content

Commit

Permalink
Added <li>, <margin> and <align> tags
Browse files Browse the repository at this point in the history
  • Loading branch information
twanvl committed May 14, 2020
1 parent 6d4d973 commit 2b7bd25
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 195 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Bug fixes:

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.
* Colors can now be written using hex notation, #rrggbb / #rrggbbaa, and short hex notation (#rgb / #rgba)

Scripting:
Expand Down
4 changes: 3 additions & 1 deletion doc/type/tagged_string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ This is written as the character with code 1 in files.
| @<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.
| @<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.
| @<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]].
| @<atom>@ An atomic piece of text. The cursor can never be inside it; it is selected as a whole.
Expand All @@ -26,7 +29,6 @@ This is written as the character with code 1 in files.
| @<code-kw>@ The text inside the tag is highlighted as a keyword in source code.
| @<code-str>@ The text inside the tag is highlighted as a string in source code.


--Other tags--
! Tag Description
| @<kw-?>@ Indicates that the text inside it is a keyword. This tag is automatically inserted by
Expand Down
38 changes: 33 additions & 5 deletions src/render/text/compound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,52 @@
// ----------------------------------------------------------------------------- : CompoundTextElement

void CompoundTextElement::draw(RotatedDC& dc, double scale, const RealRect& rect, const double* xs, DrawWhat what, size_t start, size_t end) const {
elements.draw(dc, scale, rect, xs, what, start, end);
for (auto const& e : children) {
size_t start_ = max(start, e->start);
size_t end_ = min(end, e->end);
if (start_ < end_) {
e->draw(dc, scale,
RealRect(rect.x + xs[start_ - start] - xs[0], rect.y,
xs[end_ - start] - xs[start_ - start], rect.height),
xs + start_ - start, what, start_, end_);
}
if (end <= e->end) return; // nothing can be after this
}
}

void CompoundTextElement::getCharInfo(RotatedDC& dc, double scale, vector<CharInfo>& out) const {
elements.getCharInfo(dc, scale, start, end, out);
for (auto const& e : children) {
// characters before this element, after the previous
assert(e->start >= out.size());
out.resize(e->start);
e->getCharInfo(dc, scale, out);
}
assert(end >= out.size());
out.resize(end);
}

double CompoundTextElement::minScale() const {
return elements.minScale();
double m = 0.0001;
for (auto const& e : children) {
m = max(m, e->minScale());
}
return m;
}

double CompoundTextElement::scaleStep() const {
return elements.scaleStep();
double m = 1;
for (auto const& e : children) {
m = min(m, e->scaleStep());
}
return m;
}

// ----------------------------------------------------------------------------- : AtomTextElement

void AtomTextElement::draw(RotatedDC& dc, double scale, const RealRect& rect, const double* xs, DrawWhat what, size_t start, size_t end) const {
if (what & DRAW_ACTIVE) {
dc.SetPen(*wxTRANSPARENT_PEN);
dc.SetBrush(Color(210,210,210));
dc.SetBrush(background_color);
dc.DrawRectangle(rect);
}
CompoundTextElement::draw(dc, scale, rect, xs, what, start, end);
Expand Down
186 changes: 102 additions & 84 deletions src/render/text/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,6 @@ DECLARE_POINTER_TYPE(FontTextElement);

// ----------------------------------------------------------------------------- : TextElements

void TextElements::draw(RotatedDC& dc, double scale, const RealRect& rect, const double* xs, DrawWhat what, size_t start, size_t end) const {
FOR_EACH_CONST(e, elements) {
size_t start_ = max(start, e->start);
size_t end_ = min(end, e->end);
if (start_ < end_) {
e->draw(dc, scale,
RealRect(rect.x + xs[start_-start] - xs[0], rect.y,
xs[end_-start] - xs[start_-start], rect.height),
xs + start_ - start, what, start_, end_);
}
if (end <= e->end) return; // nothing can be after this
}
}

void TextElements::getCharInfo(RotatedDC& dc, double scale, size_t start, size_t end, vector<CharInfo>& out) const {
FOR_EACH_CONST(e, elements) {
// characters before this element, after the previous
while (out.size() < e->start) {
out.push_back(CharInfo());
}
e->getCharInfo(dc, scale, out);
}
while (out.size() < end) {
out.push_back(CharInfo());
}
}

double TextElements::minScale() const {
double m = 0.0001;
FOR_EACH_CONST(e, elements) {
m = max(m, e->minScale());
}
return m;
}
double TextElements::scaleStep() const {
double m = 1;
FOR_EACH_CONST(e, elements) {
m = min(m, e->scaleStep());
}
return m;
}

// Colors for <atom-param> tags
Color param_colors[] =
{ Color(0,170,0)
Expand All @@ -72,33 +30,43 @@ const size_t param_colors_count = sizeof(param_colors) / sizeof(param_colors[0])
// Helper class for TextElements::fromString, to allow persistent formating state accross recusive calls
struct TextElementsFromString {
// What formatting is enabled?
int bold, italic, symbol;
int soft, kwpph, param, line, soft_line;
int code, code_kw, code_string, param_ref, error;
int param_id;
int bold = 0, italic = 0, symbol = 0;
int soft = 0, kwpph = 0, param = 0, line = 0, soft_line = 0;
int code = 0, code_kw = 0, code_string = 0, param_ref = 0;
int param_id = 0;
vector<Color> colors;
vector<double> sizes;
vector<String> fonts;
/// put angle brackets around the text?
bool bracket;

TextElementsFromString()
: bold(0), italic(0), symbol(0), soft(0), kwpph(0), param(0), line(0), soft_line(0)
, code(0), code_kw(0), code_string(0), param_ref(0), error(0)
, param_id(0), bracket(false) {}
vector<pair<double,double>> margins;
vector<Alignment> aligns;

const TextStyle& style;
Context& ctx;
vector<TextParagraph>& paragraphs;

TextElementsFromString(TextElements& out, const String& text, const TextStyle& style, Context& ctx)
: style(style), ctx(ctx), paragraphs(out.paragraphs)
{
out.start = 0;
out.end = text.size();
paragraphs.emplace_back();
paragraphs.back().start = 0;
fromString(out.children, text, 0, text.size());
paragraphs.back().end = text.size();
}

private:
// read TextElements from a string
void fromString(TextElements& te, const String& text, size_t start, size_t end, const TextStyle& style, Context& ctx) {
te.elements.clear();
end = min(end, text.size());
void fromString(vector<TextElementP>& elements, const String& text, size_t start, size_t end) {
size_t text_start = start;
// for each character...
for (size_t pos = start ; pos < end ; ) {
Char c = text.GetChar(pos);
if (c == _('<')) {
if (text_start < pos) {
// text element before this tag?
addText(te, text, text_start, pos, style, ctx);
addText(elements, text, text_start, pos);
addParagraphs(text, text_start, pos);
}
// a (formatting) tag
size_t tag_start = pos;
Expand Down Expand Up @@ -175,18 +143,54 @@ struct TextElementsFromString {
else if (is_substr(text, tag_start, _("</atom-param"))) param -= 1;
else if (is_substr(text, tag_start, _("<atom"))) {
// 'atomic' indicator
#if 0
// it would be nice if we could have semi-transparent brushes
Color color = style.font.color; color.a /= 5;
#else
Color fg = style.font.color;
Color color = fg.r+fg.g+fg.b < 255*2 ? Color(210,210,210) : Color(60,60,60);
#endif
size_t end_tag = min(end, match_close_tag(text, tag_start));
intrusive_ptr<AtomTextElement> e(new AtomTextElement(pos, end_tag));
fromString(e->elements, text, pos, end_tag, style, ctx);
te.elements.push_back(e);
intrusive_ptr<AtomTextElement> e = make_intrusive<AtomTextElement>(pos, end_tag, color);
fromString(e->children, text, pos, end_tag);
elements.push_back(e);
pos = skip_tag(text, end_tag);
} else if (is_substr(text, tag_start, _( "<error"))) {
// error indicator
size_t end_tag = min(end, match_close_tag(text, tag_start));
intrusive_ptr<ErrorTextElement> e(new ErrorTextElement(pos, end_tag));
fromString(e->elements, text, pos, end_tag, style, ctx);
te.elements.push_back(e);
intrusive_ptr<ErrorTextElement> e = make_intrusive<ErrorTextElement>(pos, end_tag);
fromString(e->children, text, pos, end_tag);
elements.push_back(e);
pos = skip_tag(text, end_tag);
} else if (is_substr(text, tag_start, _("</li"))) {
// end of bullet point, set margin here
paragraphs.back().margin_end_char = pos;
} else if (is_substr(text, tag_start, _("<margin"))) {
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);
if (!margins.empty()) {
margin_left += margins.back().first;
margin_right += margins.back().second;
}
margins.emplace_back(margin_left, margin_right);
paragraphs.back().margin_left = margin_left;
paragraphs.back().margin_right = margin_right;
}
} else if (is_substr(text, tag_start, _("</margin"))) {
if (!margins.empty()) margins.pop_back();
} else if (is_substr(text, tag_start, _("<align"))) {
size_t colon = text.find_first_of(_(">:"), tag_start);
if (colon < pos - 1 && text.GetChar(colon) == _(':')) {
Alignment align = alignment_from_string(text.substr(colon+1, pos-colon-2));
aligns.push_back(align);
paragraphs.back().alignment = align;
}
} else if (is_substr(text, tag_start, _("</align"))) {
if (!aligns.empty()) aligns.pop_back();
} else {
// ignore other tags
}
Expand All @@ -199,18 +203,19 @@ struct TextElementsFromString {
}
if (text_start < end) {
// remaining text at the end
addText(te, text, text_start, end, style, ctx);
addText(elements, text, text_start, end);
addParagraphs(text, text_start, end);
}
}

private:
/// Create a text element for a piece of text, text[start..end)
void addText(TextElements& te, const String& text, size_t start, size_t end, const TextStyle& style, Context& ctx) {
void addText(vector<TextElementP>& elements, const String& text, size_t start, size_t end) {
String content = untag(text.substr(start, end - start));
assert(content.size() == end-start);
// use symbol font?
if (symbol > 0 && style.symbol_font.valid()) {
te.elements.push_back(make_intrusive<SymbolTextElement>(content, start, end, style.symbol_font, &ctx));
elements.push_back(make_intrusive<SymbolTextElement>(content, start, end, style.symbol_font, &ctx));
} else {
// text, possibly mixed with symbols
DrawWhat what = soft > 0 ? DRAW_ACTIVE : DRAW_NORMAL;
Expand All @@ -221,8 +226,7 @@ struct TextElementsFromString {
content = String(LEFT_ANGLE_BRACKET) + content + RIGHT_ANGLE_BRACKET;
start -= 1;
end += 1;
}
if (style.always_symbol && style.symbol_font.valid()) {
} else if (style.always_symbol && style.symbol_font.valid()) {
// mixed symbols/text, autodetected by symbol font
size_t text_pos = 0;
size_t pos = 0;
Expand All @@ -233,20 +237,40 @@ struct TextElementsFromString {
if (text_pos < pos) {
// text before it?
if (!font) font = makeFont(style);
te.elements.push_back(make_intrusive<FontTextElement>(content.substr(text_pos, pos-text_pos), start+text_pos, start+pos, font, what, line_break));
elements.push_back(make_intrusive<FontTextElement>(content.substr(text_pos, pos-text_pos), start+text_pos, start+pos, font, what, line_break));
}
te.elements.push_back(make_intrusive<SymbolTextElement>(content.substr(pos,n), start+pos, start+pos+n, style.symbol_font, &ctx));
elements.push_back(make_intrusive<SymbolTextElement>(content.substr(pos,n), start+pos, start+pos+n, style.symbol_font, &ctx));
text_pos = pos += n;
} else {
++pos;
}
}
if (text_pos < pos) {
if (!font) font = makeFont(style);
te.elements.push_back(make_intrusive<FontTextElement>(content.substr(text_pos), start+text_pos, end, font, what, line_break));
elements.push_back(make_intrusive<FontTextElement>(content.substr(text_pos), start+text_pos, end, font, what, line_break));
}
} else {
te.elements.push_back(make_intrusive<FontTextElement>(content, start, end, makeFont(style), what, line_break));
elements.push_back(make_intrusive<FontTextElement>(content, start, end, makeFont(style), what, line_break));
}
}
}
// Find paragraph breaks in text
void addParagraphs(const String& text, size_t start, size_t end) {
if (line == 0 && soft_line > 0) return;
for (size_t i = start; i < end; ++i) {
wxUniChar c = text.GetChar(i);
if (c == '\n') {
paragraphs.back().end = i + 1;
paragraphs.emplace_back();
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;
}
if (!aligns.empty()) {
paragraphs.back().alignment = aligns.back();
}
}
}
}
Expand All @@ -271,18 +295,12 @@ struct TextElementsFromString {
}
};

void TextElements::fromString(const String& text, size_t start, size_t end, const TextStyle& style, Context& ctx) {
TextElementsFromString f;
f.fromString(*this, text, start, end, style, ctx);
void TextElements::clear() {
children.clear();
paragraphs.clear();
}
/*
// ----------------------------------------------------------------------------- : CompoundTextElement

void CompoundTextElement::draw(RotatedDC& dc, double scale, const RealRect& rect, DrawWhat what, size_t start, size_t end) const {
elements.draw(dc, scale, rect, what, start, end);
void TextElements::fromString(const String& text, const TextStyle& style, Context& ctx) {
clear();
TextElementsFromString f(*this, text, style, ctx);
}
RealSize CompoundTextElement::charSize(RotatedDC& dc, double scale, size_t index) const {
return elements.charSize(rot, scale, index);
}
*/
Loading

0 comments on commit 2b7bd25

Please sign in to comment.