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

Fix text baseline position for some fonts #669

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

PreferLinux
Copy link
Contributor

Text drawn with some fonts (for example Calibri and the free metric-equivalent Carlito) is not aligned to the same baseline as on Windows.

This is because fonts have three sets of roughly equivalent metrics, and different platforms use different ones. Windows uses one set, and Pango primarily uses a combination of the other two. The set Windows uses has the line spacing included in the ascent and descent. The other sets allow the gap to be specified separately, but for many fonts (even most) the one Pango uses has got only a minimal gap with the ascent roughly matching Window's one (hence why this problem is not common). For the first line of text the baseline is positioned according to the ascent.

However, these two fonts have a significant difference between the Windows ascent and the other two. As the text baseline is positioned according to the ascent, the text is positioned equivalently higher in the bounding box:
Screenshot_20200902_204938

This works around the problem by calculating the correct baseline and adding the difference between it and the baseline used by Pango to the offset for text drawn with StringFormat->lineAlignment set to Near. As long as the metrics returned by GdipGetCellAscent and co are correct this should not make drawing wrong.

With fix:
Screenshot_20200902_204817

Windows 8.1:
screenshot-Windows-font-alignment

Some fonts (particularly Calibri or the metric-equivalent Carlito) were
not aligned to the same baseline as on Windows. This is because fonts
have three sets of roughly equivalent metrics, and different platforms
use different ones. Pango uses one set, and Windows uses a different
set. The one Windows uses has the line spacing included in the ascent
and descent. The other two allow the gap to be specified separately,
but the one Pango seems to use has normally got only a minimal gap.

The problem is that Pango is aligning the top of the font
according to just the ascent, and on fonts like the above where this is
considerably different to the Windows ascent the text is positioned
equivalently higher in the bounding box.
Don't adjust baseline on vertical text. It needs to be handled
differently to horizontal, and has enough other problems that I don't
want to start. Fixes test failure.

Fixes the compile error that happened for Windows.

Report the baseline offset separately to the box offset, because it
needs to be handled differently for measurement etc. Basically when
drawing it needs to make it draw lower (affecting position), but when
measuring it needs to make the rectangle taller (affecting size but
leaving the position unchanged).
@PreferLinux
Copy link
Contributor Author

This has some significant issues, both before and after the changes. In particular, both versions have issues with measuring. I'll continue to work on the problem though.

Base automatically changed from master to main March 12, 2021 13:26
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