Support stretching spaces in ui::label

This commit is contained in:
Nikita Lisitsa 2022-02-15 17:12:46 +03:00
parent 0be73e2816
commit a52ea77e69
2 changed files with 17 additions and 0 deletions

View file

@ -17,6 +17,7 @@ namespace psemek::ui
left, left,
center, center,
right, right,
stretch,
}; };
enum class valignment enum class valignment

View file

@ -209,12 +209,19 @@ namespace psemek::ui
for (std::size_t l = 0; l < lines.size(); ++l) for (std::size_t l = 0; l < lines.size(); ++l)
{ {
geom::interval<float> x_range; geom::interval<float> x_range;
int spaces = 0;
for (std::size_t i = lines[l].first; i < lines[l].second; ++i) for (std::size_t i = lines[l].first; i < lines[l].second; ++i)
{
x_range |= glyphs[i].position[0]; x_range |= glyphs[i].position[0];
if (std::isspace(glyphs[i].character))
++spaces;
}
max_line_size = std::max(max_line_size, x_range.length()); max_line_size = std::max(max_line_size, x_range.length());
geom::vector<float, 2> offset; geom::vector<float, 2> offset;
float space_extra = 0.f;
switch (halign_) switch (halign_)
{ {
@ -227,6 +234,11 @@ namespace psemek::ui
case halignment::right: case halignment::right:
offset[0] = bbox[0].max - x_range.length() - x_range.min; offset[0] = bbox[0].max - x_range.length() - x_range.min;
break; break;
case halignment::stretch:
offset[0] = bbox[0].min - x_range.min;
if (l + 1 != lines.size() && spaces > 0)
space_extra = (bbox[0].length() - x_range.length()) / spaces;
break;
} }
switch (valign_) switch (valign_)
@ -243,7 +255,11 @@ namespace psemek::ui
} }
for (std::size_t i = lines[l].first; i < lines[l].second; ++i) for (std::size_t i = lines[l].first; i < lines[l].second; ++i)
{
glyphs[i].position += offset; glyphs[i].position += offset;
if (std::isspace(glyphs[i].character))
offset[0] += space_extra;
}
} }
state.glyphs = std::move(glyphs); state.glyphs = std::move(glyphs);