Description
We should add some support to Font
for fitting strings withing some pre-determined pixel width.
This could have benefits for both string clipping, and word wrap.
There was some iteration in OPHD in TextField::onMouseDown
that was sub-optimal. Instead of a single loop, incrementing by individual character widths, it had a call to Font::width(std::string_view) const
, which resulted in a nested loop, and a lot of wasted effort as the width of the entire string was recalculated for each new char
added. It could have passed each new char
as it's own std::string_view
, though that still seems like extra work for a simple operation.
We could improve support by allowing for direct querying for the width
of individual char
elements.
We could also add special member functions that would count the number of characters that would fit within a given pixel width, and return the length of the longest sub-string that fits within the given pixel width.
Naming such a function may be hard, though the signature might look something like:
std::size_t maxLengthSizeFit(std::string_view string, int maxWidth) const;
Related: