[Win32] Fix font metrics being scaled by GC transform for fallback fonts#3355
Conversation
When a font that does not exist on the platform is set on a GC and a Transform is active, GDI+ internally calls Font::GetLogFontW with the transformed Graphics to derive a fallback GDI font handle. This caused the lfHeight in the resulting LOGFONT to be scaled by the transform, so GetTextMetrics on the DC returned inflated metrics. Fix by temporarily saving and restoring the GDI+ transform around the Font::GetLogFontW call so the LOGFONT always reflects device-pixel metrics, independent of any user-applied transform. Fixes eclipse-platform#2978 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@ptziegler I am not sure how severe #2978 still is for you, but this may be a solution to that issue |
|
@HeikoKlare I think we agreed that this problem primarily happens when using a non-existent font, right? The reason I originally reported this was due to the GEF Logic editor, but otherwise I've never seen it in practice. Especially when the "fix" is to use a valid font instead. 😅 |
|
Yes, this problem should only happen when using a non-existent font (because the height calculation is wrong in exactly that case). |
Summary
Fixes #2978
When a font that does not exist on the platform is set on a
GCand aTransformis active, GDI+ callsFont::GetLogFontWwith the transformedGraphicsto derive a fallback GDI font handle. This causes thelfHeightin the resultingLOGFONTto be scaled by the transform, soGetTextMetricson the DC returns inflated (transform-scaled) metrics.The fix temporarily saves and restores the GDI+ transform around the
Font::GetLogFontWcall so theLOGFONTalways reflects device-pixel metrics, independent of any user-applied transform.A regression test is added that verifies font metrics are consistent across all four combinations of transform-set/cleared and font-set/kept that the original issue demonstrated.
How to Test
The snippet from #2978 may be used for testing. That snippet reflects what is also done by the test added with this PR.