Another small step (for mankind ...).
Part of another project: I wanted to be able to align different font sizes to the font's baseline.
Not as easy as it might sound, but do-able.
Like this:
screenshot.jpg
.exe, code and even the font it uses (Century Schoolbook) attached.
As usual, writing things down on paper did the trick in figuring this out
(some stuff from Petzold's huuuge Windows book on GetTextMetrics() was very helpful too):
Baseline alignment.gif
Dunno if anyone bothered looking at this closely, but it turns out I was using a more complicated formula for finding the baseline than necessary. (I seem to have a knack for finding needlessly complex solutions to problems, then seeing the simpler way later.)
The formula I had was:
baseline = (rect.height - tmHeight) / 2 + tmHeight - tmDescent
where rect.height is the height of the text + top & bottom padding.
Looking at the diagram I drew, all that's needed is
tm.tmAscent + top padding
(proven by recoding the demo program)
So I admit it: I'm not a genius. No Nobel Prizes for me.
I don't know if this will help, but these are different sizes of 'segoe ui' font written into a *.rtf file in wordpad. This is from a scrren capture. I added the red line.
Perfectly aligns to the baseline that you speak about.
(https://i.postimg.cc/MHJv6TCn/untitled.png)
Point being there should be a relatively easy way to do it programatically.
The xp source code on github should have wordpad... (hint, hint...)
But Zedd, I already figured it out. Didn't you see my screenshot? That's the output of a program, not something I drew.
I just figured out the easy way instead of the hard way. Two lines of code.
Quote from: NoCforMe on May 08, 2025, 11:40:56 AMBut Zedd, I already figured it out.
I didn't see any code or attachments in your second post, so I thought you were still trying to figure it out. :smiley:
No, I just was reporting that I'm a bit thick in the head, and found the easy way after trying the hard way. It worked the first time. As my drawing says, Q.E.D. (quod erat demonstrandum, it is proven).
Ackshooly (and that's specially for our friend @sinsi here), it's even easier than what I posted earlier:
- Get tm.tmAscent for each font
- Find and save tm.tmAscent for the largest font
- When drawing text in WM_PAINT, simply subtract each font's tm.tmAscent from the largest saved value; the difference is the vertical offset needed to align the font to the same baseline.
Easy peasy.
You know, I got to say that having something work out right in your code, having a success (finally!), even a small one, certainly does a lot to improve one's mood.
Am I right? I know I'm not the only one who experiences this.
And if you haven't? Then you definitely have my sympathy. Keep plugging away.
Just a little preview of what I'm working on that uses this technique:
mcLB2 1.jpg
I won't say anything more about it until it's finished.
(It's 90% done with 90% left to go.)
David good optimisation :thumbsup: