The MASM Forum

General => The Workshop => Windows Graphics Programming => Topic started by: NoCforMe on May 06, 2025, 10:31:57 AM

Title: Aligning different-size text to character baselines
Post by: NoCforMe on May 06, 2025, 10:31:57 AM
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

Title: Re: Aligning different-size text to character baselines
Post by: NoCforMe on May 08, 2025, 09:48:10 AM
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.
Title: Re: Aligning different-size text to character baselines
Post by: zedd on May 08, 2025, 11:37:09 AM
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...)
Title: Re: Aligning different-size text to character baselines
Post by: NoCforMe on May 08, 2025, 11:40:56 AM
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.
Title: Re: Aligning different-size text to character baselines
Post by: zedd on May 08, 2025, 11:44:12 AM
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:

Title: Re: Aligning different-size text to character baselines
Post by: NoCforMe on May 08, 2025, 11:55:41 AM
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).
Title: Re: Aligning different-size text to character baselines
Post by: NoCforMe on May 09, 2025, 06:26:17 AM
Ackshooly (and that's specially for our friend @sinsi here), it's even easier than what I posted earlier:


Easy peasy.
Title: Re: Aligning different-size text to character baselines
Post by: NoCforMe on May 09, 2025, 01:14:46 PM
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.
Title: Re: Aligning different-size text to character baselines
Post by: NoCforMe on May 10, 2025, 01:03:46 PM
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.)
Title: Re: Aligning different-size text to character baselines
Post by: daydreamer on May 10, 2025, 08:47:57 PM
David good optimisation  :thumbsup: