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,
center,
right,
stretch,
};
enum class valignment

View file

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