Skip to content

Add Font::width(char) overload #1310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 4, 2025
Merged

Add Font::width(char) overload #1310

merged 2 commits into from
Jul 4, 2025

Conversation

DanRStevens
Copy link
Collaborator

@DanRStevens DanRStevens commented Jul 3, 2025

Sometimes it may be more convenient, or possibly even more efficient if we could get the width of individual char elements. Perhaps to build up a string of some unknown length, that must fit within a given pixel limit.

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.

Related:

For when we want to iterate through a string, successively building up the length, and checking some condition, it may be easier to have convenient access to the widths of individual characters.
@DanRStevens DanRStevens merged commit d2147cb into main Jul 4, 2025
8 checks passed
@DanRStevens DanRStevens deleted the addFontCharWidth branch July 4, 2025 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant