|
Glyph spacing |
dthompson
Member #5,749
April 2005
|
I have a bitmap font and am wondering whether it's possible to get Allegro to add 1px spacing between the glyphs when rendering the font, rather than having to add 1px spacing to the right of each character in the source bitmap. With an unkerned TTF font, I'm aware that it'd be possible to cycle through every glyph and alter its kerning value in order to achieve this. Is this possible with the current API or will I need to render each glyph manually? In the case of the latter, could I request an al_set_bitmap_font_spacing, or perhaps an even more generic function that can apply extra letter spacing to TTF fonts too? ______________________________________________________ |
Edgar Reynaldo
Major Reynaldo
May 2007
|
You can have your cake and eat it too. Have allegro do it for you. Use al_get_glyph. Although it takes a pair of codepoints, must be to do with the kerning. From an ALLEGRO_GLYPH* you can get the font's bitmap source rectangle for that codepoint, and then you draw it yourself. I'll whip up a quick example for you. Actually, all you need are these two functions here : 1int GetTextWidth(ALLEGRO_FONT* f , const char* s , int xspacing) {
2 int prev = 0;
3 int current = 0;
4 int i = 0;
5 int w = 0;
6 while ((current = s[i])) {
7 ALLEGRO_GLYPH g;
8 memset(&g , 0 , sizeof(g));
9 if (al_get_glyph(f , prev , current , &g)) {
10 if (i) {
11 w += xspacing;
12 }
13 w += g.advance;
14 }
15 prev = current;
16 ++i;
17 }
18 return w;
19}
20
21
22
23void DrawSpacedText(ALLEGRO_FONT* font , const char* str , int cx , int cy , ALLEGRO_COLOR col , int xspacing) {
24 int w = GetTextWidth(font , str , xspacing);
25
26 int x = cx - w/2;
27 int y = cy - al_get_font_line_height(font)/2;
28 int i = 0;
29 int prev = 0;
30 int current = 0;
31 while ((current = str[i])) {
32 ALLEGRO_GLYPH g;
33 memset(&g , 0 , sizeof(g));
34 if (al_get_glyph(font , prev , current , &g)) {
35 if (g.bitmap) {
36 al_draw_tinted_bitmap_region(g.bitmap , col , g.x , g.y , g.w , g.h , x , y , 0);
37 }
38 x += g.advance;
39 x += xspacing;
40 }
41 prev = current;
42 ++i;
43 }
44
45}
It took me about 10 minutes to add in horizontal spacing. What you can do is take and load your bitmap font, then draw that font to a blank with the additional spacing, then save it and from now on reload the second one. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
SiegeLord
Member #7,827
October 2006
|
In addition to the above, we're adding support for the bmfont format, which has this padding a little more explicit. Obviously it's a new format, but I think it's a superset of the current one. Also, there's al_draw_glyph, which might be preferable to drawing the bitmap yourself. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Edgar Reynaldo
Major Reynaldo
May 2007
|
I came across a quirk in a ttf font I use. When I call al_get_glyph on the space character (ascii 32), it returns true, but the bitmap is null. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
SiegeLord
Member #7,827
October 2006
|
Edgar Reynaldo said: I came across a quirk in a ttf font I use. When I call al_get_glyph on the space character (ascii 32), it returns true, but the bitmap is null. That's normal. I've updated the documentation to mention this. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
dthompson
Member #5,749
April 2005
|
Edgar Reynaldo said: You can have your cake and eat it too. Nice that works well for a single line of text; reimplementing all of A5's font routines with that extra parameter might be more of a pain though... SiegeLord said: we're adding support for the bmfont format This'd probably be a better solution; looking forward to it. I'll probably just go and alter the font's image file for now; it's just a 5x5 pixel font so won't require much effort. ______________________________________________________ |
|