grimfang4 / SDL_FontCache

A generic font caching C library with loading and rendering support for SDL.
MIT License
305 stars 66 forks source link

Text not centered vertically inside rects? #14

Open ghost opened 8 years ago

ghost commented 8 years ago

Would it be possible to add functionality for centering text inside a rect both vertically and horizontally?

Right now when you use FC_DrawBox with FC_ALIGN_CENTER it only centers the text horizontally inside the rect you provide.

What do you think about renaming FC_ALIGN_CENTER to FC_ALIGN_CENTERH, and adding FC_ALIGN_CENTERV? Then the new FC_ALIGN_CENTER would then center both vertically and horizontally while the other two would do one or the other.

To do the vertical alignmentent of text inside a rect I guess you would first need to know the total number of lines that are in the text. Then use that number to calculate the y position to start drawing the lines.

grimfang4 commented 8 years ago

Vertical alignment would be nice. I think I had held off on implementing it because I hadn't fixed the font metrics (and still haven't). The vertical measurement of the font (i.e. FC_GetHeight()) isn't actually right. It returns something bigger than the full height of the glyphs and doesn't quite match the ascent+descent that I would expect. If I remember correctly, I traced this back to something SDL_ttf was doing (in TTF_FontHeight()), but I haven't looked at it in a long time.

As for the interface, I think the vertical alignment could be an extra parameter for FC_Effect. Adding it would affect only that struct and FC_MakeEffect().