Skip to content
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

Text anchoring fixes #120

Merged
merged 6 commits into from
Sep 14, 2024
Merged

Text anchoring fixes #120

merged 6 commits into from
Sep 14, 2024

Conversation

bhperry
Copy link
Contributor

@bhperry bhperry commented Sep 11, 2024

Anchoring was not working properly for multiline text because of the way text.Orig and text.bounds are aligned. Orig is at the bottom of the first line, and bounds are extended downward after each \n character. For a single line of text, Orig is effectively at bounds.Min, so anchoring relative to bounds works as expected. However after one or more line breaks there is a growing offset between Orig and bounds.Min and the anchor vec from bounds is no longer correctly aligned.

In order to preserve the previous behavior of text, in which characters are drawn starting at the origin and expanding right and down, the anchor offset is disabled by default until AlignedTo(...) is called. Otherwise now that anchoring is correctly computed text would shift up after each newline since the default anchor value is TopRight (a.k.a pixel.Anchor{0, 0}).

Broken anchoring

image

Here the anchor is set to pixel.Center, but instead of being centered on Orig, it is anchored to un-untransformed bounds.Min, so the entire text moves down after every line break. Additionally, there is no convenient way to get the true bounds or Dot of the text in anchor-space, since anchoring is applied at draw time.

image
Here there are a number of \n and \t characters entered first before entering text. Because bounds did not account for them, the anchoring is not aligned to Orig.

Fixed anchoring

After offsetting the anchor vec by the height minus the first line, anchoring works as expected. I have also added AnchoredBounds() and AnchoredDot() convenience functions to easily retrieve their positions in anchor-space.

Centered on Orig

image

Compute bounds/anchoring corrrectly for control characters \n and \t

Previously bounds were not expanded for line break and tab characters, which could throw off the anchoring.
Anchored to the top right corner (or pixel.BottomLeft for... reasons?)
image

Compute bounds for space characters when font atlas returns empty rect

Some text fonts return empty bounds for the space character, meaning text that starts with space would not start it's bounds until after the first non-space rune. Again, throwing off the anchoring from what would be expected.
Anchored to bottom left corner (pixel.TopRight)
image

Unanchored text

image

@bhperry bhperry requested a review from a team September 11, 2024 07:14
@bhperry bhperry merged commit f1509ba into gopxl:main Sep 14, 2024
1 check passed
@bhperry bhperry deleted the text-anchoring-fixes branch September 14, 2024 16:02
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.

3 participants