Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Making text sit on baseline

Author
Message
MonoCoder
20
Years of Service
User Offline
Joined: 4th Dec 2005
Location: england
Posted: 23rd Dec 2013 23:40 Edited at: 23rd Dec 2013 23:43
Hello. I'm trying to draw text so that the given y-coord does not specify the top of the text (from where it hangs; the default), but the baseline of the text (upon where it sits). Here's a picture describing the situation:



And here's an example of my present workaround-ish solution. Hold the left-mouse button to move the baseline. Try switching in different fonts.



Here's a second example (requires Advanced2D) which shows a larger set of fonts. Hold up/down arrow keys to change text size; press n, b, i, or x keys to change style to normal, bold, italics or bold-italics respectively.



So my question is this: how can I find the true baseline of a font automatically? You can see in the second example that I've had to eyeball a unique baseline offset factor for each individual font, which is a real pain (and makes implementation of, say, custom user fonts awkward).

I've looked at some msdn articles and tried using some dll functions - and will continue to and post back any success - but no luck yet. I've also searched the forum but can't find anything (though it seems the sort of thing someone would need eventually ).

thanks for reading
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 24th Dec 2013 04:22
Maybe I'm missing what you're after, but I think you're making this way over complicated.



Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 24th Dec 2013 06:37 Edited at: 24th Dec 2013 06:39
I wonder if you could check on setup the position of the bottom edge of pixels for the font and find the base line that way. Won't be super accurate on all fonts but close enough. Example:



I guess you could improve this by testing a few characters that have a horizontal bottom and getting the average baseline out of them.

"Get in the Van!" - Van B
MonoCoder
20
Years of Service
User Offline
Joined: 4th Dec 2005
Location: england
Posted: 24th Dec 2013 15:01 Edited at: 24th Dec 2013 15:10
@Phaelax, the problem with that is the characters are descender height-aligned rather than baseline-aligned; that is, they don't so much sit on the line as stand on it:






@Sasuke: that's a good idea. I'd mostly left it out - considering it a too-hefty workaround - but in lieu of direct acquisition of the true value, it's probably the best approach.

Basing it on the 'Z' is good, though of course that won't work if, say, the font is in a handwritten style (that is, the base isn't flat). I've briefly had a think about improving it, in similar vein to your suggestion about averaging:


[prob need to zoom to see better :V]
[for reference, first font is calibri, second is freestyle script]

- guessed a few more letters that could conceivably be flat-based even if the 'Z' isn't
- sample each letter 1+ times, measuring distance from bottom to first non-empty pixel
- for each distance, keep count of number of times measured (eg. 5px:twice, 6px:once...)
- distance most counted is taken as baseline (if a tie, pick lowest or perhaps middle/average).

Can probably be improved further by working with a higher-resolution font size, and by biasing the sample positions (eg. more samples roughly where you expect the 'M's legs to be, and less between). Could even process the whole character set, but probably would muddy results or slow it down.

Will take a shot at implementing this. Also going to research font file structures, see if I can make sense of them. Thanks
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 24th Dec 2013 15:21
Why not just create an AppGameKit style bitmap font system? I created one which works exactly the same as AppGameKit text in a few minutes, then you can valign the text however you want?

MonoCoder
20
Years of Service
User Offline
Joined: 4th Dec 2005
Location: england
Posted: 24th Dec 2013 17:43
I'm using it for UI, not for in-game huds or menus. Bitmap fonts aren't appropriate or preferred (and incidentally don't, as far as I can tell, address the baseline question). Thanks for the suggestion though.
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 24th Dec 2013 19:40
I use bitmap fonts in my UI's. Check out my bang bang 2 videos, I think they work quite well, plus you can scale and rotate text in realtime too. Even affect them with a2d shaders.

As for the baseline issue, with bitmap fonts, you can specify where you want the character to be grabbed from, therefore setting its baseline wherever you want, even changing basekine on a letter by letter basis if you want.

I use a combination of actual and bitmap text in my engine, but true bitmap text is just as simple to do.

MonoCoder
20
Years of Service
User Offline
Joined: 4th Dec 2005
Location: england
Posted: 29th Dec 2013 00:23 Edited at: 29th Dec 2013 00:35
@Mobiius, I've taken a look at the videos, and they look nice, but they lack the flexibility of normal vector fonts. Don't get me wrong, in a game (especially for release, monetised), a bitmap font is almost certainly the way to go.

As it happens though, I've been working on Sasuke's method and the result could fairly easily be adjusted to generate baseline (ascent, descent, etc. - collectively "metrics") data for bitmap fonts as well (since in both cases its looking at a bitmap of a font character).

So here's what I've got.

commands and notes


full code with example


Requires
Diggsey's Advanced2D (linked in an above post)
IanM's Matrix1Utils

Output


Try:
Run the example once, it will generate metrics for each font. Current progress is shown on screen. This process takes about a minute.
Run the example again, it will finish in about a second.

On the first run the metrics for each font have been saved to a file (FontMet.dat, in the same folder as the exe).
On the second run these were loaded, and nothing new needed generating. Hence the time difference.

The contents of FontMet.dat after running the example:


Give it a try
It's still work-in-progress (I'll be tweaking test character sets and how hit-test results are interpreted), and the approach has obvious limitations: fonts with big swishes will be poorly described, and non-horizontal text just won't work. So I'm still interested if anyone (ianM, diggsey... :V ) can sneak direct access to this data through, say, a dll?

Login to post a reply

Server time is: 2026-07-07 01:53:48
Your offset time is: 2026-07-07 01:53:48