News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Aligning different-size text to character baselines

Started by NoCforMe, May 06, 2025, 10:31:57 AM

Previous topic - Next topic

NoCforMe

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:

You cannot view this attachment.

.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):

You cannot view this attachment.

32-bit code and Windows 7 foreva!

NoCforMe

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.
32-bit code and Windows 7 foreva!

zedd

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.



Point being there should be a relatively easy way to do it programatically.

The xp source code on github should have wordpad... (hint, hint...)
:biggrin:  :skrewy:

NoCforMe

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.
32-bit code and Windows 7 foreva!

zedd

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:

:biggrin:  :skrewy:

NoCforMe

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).
32-bit code and Windows 7 foreva!

NoCforMe

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.
32-bit code and Windows 7 foreva!

NoCforMe

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.
32-bit code and Windows 7 foreva!

NoCforMe

Just a little preview of what I'm working on that uses this technique:

You cannot view this attachment.

I won't say anything more about it until it's finished.
(It's 90% done with 90% left to go.)
32-bit code and Windows 7 foreva!

daydreamer

my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding