Vidar Hasfjord - 2022-07-29

For a TFont f, I intuitively expected f.GetHeight() to be equivalent to f.GetObject().lfHeight. However, TFont::GetHeight () is an overload of TFont::GetHeight (TDC&), calling the latter with a TScreenDC. This is equivalent to f.GetTextMetrics(TScreenDC{}).tmHeight, or in total:

auto dc = TScreenDC{};
dc.SelectObject(f);
auto tm = TEXTMETRIC{};
dc.GetTextMetrics(tm);
dc.RestoreFont();
return tm.tmHeight;

Which seems a bit unexpected and convoluted. I would prefer to have getters on TFont work on the object itself, rather than derive values from an arbitrary device context.

As you can see in my link above, the documentation is poor and does not tell you the details: "Returns the height of the font."

Can we change the implementation without (big) issues for client code? I would love to not have to write f.GetObject().lfHeight for a simple attribute access like this. Or should we be content with just improving the documentation?

One option is to deprecate the original GetHeight, since it is confusing, and replace it with GetLogFontHeight and GetTextMetricsHeight to be absolutely clear and explicit about what the functions do. The latter should have parameter and not have an overload without. This will let us see in text editors with signature and parameter popup that this call is related to a device context. If calling it with TScreenDC is so common as to be a chore, we should perhaps also have GetScreenDcTextMetricsHeight.

What say you?

 

Last edit: Vidar Hasfjord 2022-07-29