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

Fundamental text layout problem in nanovg #644

Open
mulle-nat opened this issue Aug 5, 2022 · 0 comments
Open

Fundamental text layout problem in nanovg #644

mulle-nat opened this issue Aug 5, 2022 · 0 comments

Comments

@mulle-nat
Copy link
Contributor

mulle-nat commented Aug 5, 2022

The layout and drawing code of nanovg uses fonsTextIterInit and fonsTextIterNext to step through the glyphs. fonsTextIterInit needs to adjust the coordinates for the given origin and alignment (like NVG_ALIGN_CENTER). For that fonsTextIterInit needs to know the width of the text line, to adjust the origin, as the string is always drawn left to right.

fonsTextIterInit calls fonsTextBounds to calculate the width. fonsTextBounds is basically another simpler rover over the glyphs, that calculates the width. But it's calculation is unfortunately not compatible with that of nvgText or nvgTextGlyphPositions:

fontstash

fonsTextBounds:

if (q.x0 < minx) minx = q.x0;
if (q.x1 > maxx) maxx = q.x1;

nanovg

nvgTextGlyphPositions uses iter.x and iter.nextx:

positions[npos].minx = nvg__minf(iter.x, q.x0) * invscale;
positions[npos].maxx = nvg__maxf(iter.nextx, q.x1) * invscale;

So that's where I think the differences in nvgTextBounds happen, as nvgTextBounds relies solely on fonsTextBounds. From a software engineering perspective though, the situation is unfortunate. The decision to use iter.x and iter.nextx is made in nanovg, so it's not transparent to fontstash. But the alignment values are defined and calculated in fontstash, which would need that information for accuracy.

I am currently of the opinion, that using the alignment implementation of fontstash is a mistake and alignment should be done in nanovg. But hacking fonsTextBounds to also use the whatever the values of iter.x and iter.nextx are, could work too...

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

1 participant