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

nvgTextBounds doesn't calculate for spaces at the end of a string #636

Closed
senxd opened this issue Jun 16, 2022 · 5 comments
Closed

nvgTextBounds doesn't calculate for spaces at the end of a string #636

senxd opened this issue Jun 16, 2022 · 5 comments

Comments

@senxd
Copy link

senxd commented Jun 16, 2022

Given text such as "abc ", nvgTextBounds doesn't include the space at the end. I assume this is an intentional feature. Is there any other way to get the bounds of the text including space besides appending another space if there is one at the end?

@mulle-nat
Copy link
Contributor

Looking at the source, the calculation for the spacing is done by fons__tt_getGlyphKernAdvance:

int fons__tt_getGlyphKernAdvance(FONSttFontImpl *font, int glyph1, int glyph2)
{
	FT_Vector ftKerning;
	FT_Get_Kerning(font->font, glyph1, glyph2, FT_KERNING_DEFAULT, &ftKerning);
	return (int)((ftKerning.x + 32) >> 6);  // Round up and convert to integer
}

So the answer should lie with FreeType (or stb_truetype) I believe.

@senxd
Copy link
Author

senxd commented Jun 17, 2022

Ah. I see. I just made a simple workaround by appending a space if needed. Thanks!

@senxd senxd closed this as completed Jun 17, 2022
@mulle-nat
Copy link
Contributor

I revise my answer here. Actually I think nvgTextBounds is buggy. There is a difference how the text bounds are calculated and how the actual drawing is then performed.This shows up most noticeably with the surrounding spaces. The way to get the proper width is to use nvgTextGlyphPositions and accumulate the glyphs width.

In this picture you can see how in the third line " Space around ", you can see the actually emitted spaces and notice how the blue enclosing rectangle (uses nvgTextGlyphPositions) is perfect:

image

You can also see that there is still a problem with the right alignment in my code 😄

@zeroxer
Copy link

zeroxer commented Jul 20, 2023

Any method to fix the space render error?

@mulle-nat
Copy link
Contributor

I use nvgTextGlyphPositions instead of nvgTextBounds, or what do you mean ?

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

No branches or pull requests

3 participants