Fix #56: Add default values to TextStyle::layout

This commit is contained in:
Twan van Laarhoven
2020-05-25 22:34:25 +02:00
parent 221a8e99e5
commit 22a7bce749
2 changed files with 25 additions and 18 deletions
+25 -16
View File
@@ -37,6 +37,20 @@ IMPLEMENT_REFLECTION(TextField) {
// ----------------------------------------------------------------------------- : TextStyle // ----------------------------------------------------------------------------- : TextStyle
TextLayoutP dummy_layout() {
auto layout = make_intrusive<TextLayout>();
auto line = make_intrusive<LineLayout>(0, 0, 0, LineLayout::Type::LINE);
auto paragraph = make_intrusive<LineLayout>(0, 0, 0, LineLayout::Type::PARAGRAPH);
auto block = make_intrusive<LineLayout>(0, 0, 0, LineLayout::Type::BLOCK);
paragraph->lines.push_back(line);
block->lines.push_back(line);
layout->lines.push_back(line);
block->paragraphs.push_back(paragraph);
layout->paragraphs.push_back(paragraph);
layout->blocks.push_back(block);
return layout;
}
TextStyle::TextStyle(const TextFieldP& field) TextStyle::TextStyle(const TextFieldP& field)
: Style(field) : Style(field)
, always_symbol(false), allow_formating(true) , always_symbol(false), allow_formating(true)
@@ -53,12 +67,12 @@ TextStyle::TextStyle(const TextFieldP& field)
, line_height_line_max(0.0) , line_height_line_max(0.0)
, paragraph_height(-1) , paragraph_height(-1)
, direction(LEFT_TO_RIGHT) , direction(LEFT_TO_RIGHT)
, content_width(0), content_height(0), content_lines(0) , layout(dummy_layout())
{} {}
double TextStyle::getStretch() const { double TextStyle::getStretch() const {
if (content_width > 0 && (alignment() & ALIGN_STRETCH)) { if (layout->width > 0 && (alignment() & ALIGN_STRETCH)) {
double factor = (width - padding_left - padding_right) / content_width; double factor = (width - padding_left - padding_right) / layout->width;
if (!(alignment() & ALIGN_IF_OVERFLOW) || factor < 1.0) { if (!(alignment() & ALIGN_IF_OVERFLOW) || factor < 1.0) {
return factor; return factor;
} }
@@ -97,18 +111,13 @@ void TextStyle::checkContentDependencies(Context& ctx, const Dependency& dep) co
alignment.initDependencies(ctx, dep); alignment.initDependencies(ctx, dep);
} }
template <typename T> void reflect_layout(T& handler, const TextStyle& ts) {} template <typename T> void reflect_layout(T& handler, const TextLayoutP& layout) {}
template <> void reflect_layout(GetMember& handler, const TextStyle& ts) { template <> void reflect_layout(GetMember& handler, const TextLayoutP& layout) {
REFLECT_N("layout", ts.layout); assert(layout);
if (ts.layout) { REFLECT_N("layout", layout);
REFLECT_N("content_width", ts.layout->width); REFLECT_N("content_width", layout->width);
REFLECT_N("content_height", ts.layout->height); REFLECT_N("content_height", layout->height);
REFLECT_N("content_lines", ts.layout->lines.size()); REFLECT_N("content_lines", layout->lines.size());
} else {
REFLECT_N("content_width", 0.);
REFLECT_N("content_height", 0.);
REFLECT_N("content_lines", 0);
}
} }
template <> void GetMember::handle(LineLayout const& obj) { obj.reflect(*this); } template <> void GetMember::handle(LineLayout const& obj) { obj.reflect(*this); }
@@ -154,7 +163,7 @@ IMPLEMENT_REFLECTION(TextStyle) {
REFLECT(line_height_line_max); REFLECT(line_height_line_max);
REFLECT(paragraph_height); REFLECT(paragraph_height);
REFLECT(direction); REFLECT(direction);
reflect_layout(handler, *this); reflect_layout(handler, layout);
} }
// ----------------------------------------------------------------------------- : TextValue // ----------------------------------------------------------------------------- : TextValue
-2
View File
@@ -92,8 +92,6 @@ public:
Direction direction; ///< In what direction is text layed out? Direction direction; ///< In what direction is text layed out?
// information from text rendering // information from text rendering
TextLayoutP layout; TextLayoutP layout;
double content_width, content_height; ///< Size of the rendered text
int content_lines; ///< Number of rendered lines
int update(Context&) override; int update(Context&) override;
void initDependencies(Context&, const Dependency&) const override; void initDependencies(Context&, const Dependency&) const override;