Sign in to follow this  
LittleJimmy

Simple vertical position for editable text using freetype

Recommended Posts

I'm trying to render a simple editable textbox and I'm struggling to vertically position the text rendered with freetype.

I've first tried obtaining the character in the typed string with the largest bitmap_top value, and then render everything relative to that, that works and correctly aligns the text, but it results in text that shifts up and down as the user types and larger characters come and go.

Next I tried simply offsetting the vertical position of the text with the global font metric height minus the bitmap_top value. This also worked well and solved the issue of text jumping up and down, but it resulted in an unnecessary gap above all the text, and all characters that went below the origin being rendered outside the text box.

Is there a standardized way for ensuring that all text rendered with freetype does so along a global baseline for the font and font size? and then, given this global baseline and a rectangular region, work out how to print the text centered to the region?

Thanks, 

Share this post


Link to post
Share on other sites

There are pixels of letters above and below the baseline, eg "j" typically extend to below the baseline. Freetype thus not only have a baseline and ascend, but also a negative descent. Its manual explains all about character layout and meaning of variables.

The proper way imho is thus to keep font max ascend and font min descend space around the baseline for the characters. It might waste some space, but you never know when a user will add a very high or very low character, unless you have restrictions on the allowed symbols that can be entered.

Share this post


Link to post
Share on other sites
Yeah I was looking for the ascender and descender info for each glyph but I could only find those properties for the face itself, and the tutorials don't seem to use ascender and descender so I wasn't sure how to use them.

How do I go about getting the ascender and descender for each glyph?

Thanks

Share this post


Link to post
Share on other sites

How do I go about getting the ascender and descender for each glyph?
By actually reading the documentation carefully, mostly, with a bit of browsing returned data structures.  Load_Glyph sounds to me like the key routine to get such data.

https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_Load_Glyph

 

I coded a text rendering routine for OpenGL using FreeType some time ago. I don't remember all the details (at the level you're asking), I don't have the code nearby, nor could I give it to you, but the FreeType documentation is clearly written, and getting information like you want was not that complicated after searching the documentation a bit, as far as I remember.

 

note that your current way of working is not very efficient. If you started reading the documentation yesterday instead of posting questions about it, you would have found and implemented the answer by now already.

Share this post


Link to post
Share on other sites

I don't know if I'm missing something here, but, I can't find any info on getting the ascender and descender for individual glyphs. I can get it for the face object, but not each glyph, unless its a calculation I need to do based on other properties of the glyph (horizontal bearing Y for ascender and height - Horizontal bearing Y for descender)?

Besides that though, obtaining ascender and descender info for every glyph in the font seems very inefficient.

Share this post


Link to post
Share on other sites

Don't search or find, read. Just read the entire tutorial. Yes, it's more than 2 lines of text, but it contains a lot of details, and you'll learn more about rendering glyphs in that hour, then you found out so far in the past day(s?).

You are asking questions that are simply explained in the manual / toturial. Do you expect someone to do your homework? It's much more complicated than you think to draw a letter properly. The manual explains it all, in more details than you want to handle, probably.

 

As for efficiency, why do you think it's inefficient? Are you absolutely sure there is no better way to find that information? Did you check with the manual/tutorial how you are advised to handle that problem? Have you coded and measured its speed? What results do you get, and how is that not fast enough?

Edited by Alberth

Share this post


Link to post
Share on other sites

Please stop assuming what I have and haven't read and cut it out with the personal attacks, no one is forcing you to answer this question.

I've read the tutorial and the other documentation, its how I got to the point where I was able to render text. I have no problems rendering and positioning static text, I'm having problems with dynamic text and you saying the info is somewhere in the documentation or insinuating that its too much for me to handle or I'm trying to get other people to do my homework isn't helping, rather don't attempt to answer the question.

As for the inefficiency part, because I don't know in advance the font face or the text size or the characters used, it means that I will need to loop through all glyphs in a font for a given text size (which could change on the fly if the end user so chooses) to get their metrics. I never said I was sure that there was no better way to find that information, it just seems that particular way may not be the most efficient way to do it, but I don't know, maybe it is.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this