al_get_glyph_advance(3)

πŸ“¦
version 5.2.11.3 Β· 3 versions
, in arch rolling

πŸ’– al_get_glyph_advance(3) β€” Allegro 5 API πŸ’–

okay bestie, so this one lives in the Library Functions Manual and it's genuinely one of those little functions that makes text look correct instead of cursed. let's get into it ✨


✨ SYNOPSIS ✨

#include <allegro5/allegro_font.h>

int al_get_glyph_advance(const ALLEGRO_FONT *f, int codepoint1, int codepoint2)

πŸŽ€ DESCRIPTION πŸŽ€

so this function tells you by how much the x position should be advanced when you're drawing left-to-right text. specifically: you've just drawn the glyph that corresponds to codepoint1, and the glyph for codepoint2 is the next one you're gonna draw. it hands you back the number you should bump your x by. πŸ’…

and it's not just doing one thing, it's taking into account two things:

  • the horizontal advance width of the glyph matching codepoint1, and
  • the kerning between the glyphs of codepoint1 and codepoint2.

🌸 so what even is kerning 🌸

kerning is literally the process of adjusting the spacing between glyphs in a font so the result looks more visually pleasing. like, it adjusts the space between two individual glyphs using an offset that the author of the font decided on. the font author is the main character here, we're just respecting their choices 🫢

πŸ¦‹ the ALLEGRO_NO_KERNING situations πŸ¦‹

  • if you pass ALLEGRO_NO_KERNING as codepoint1, then al_get_glyph_advance will return 0. this is highkey useful when you're drawing the first character of a string inside a loop. πŸŽ€
  • if you pass ALLEGRO_NO_KERNING as codepoint2, you get the horizontal advance width of the glyph for codepoint1 without any kerning factored in. also super handy β€” for example when you're drawing the last character of a string in a loop. ⭐

πŸ’€ the "glyph missing" lore πŸ’€

  • if the glyph for codepoint1 is not present in the font, this function returns zero. periodt.
  • if the glyph for codepoint2 is not present in the font, you get the horizontal advance width of the glyph for codepoint1, without any kerning taken into consideration. no cap.

πŸ‘‘ the actual loop technique πŸ‘‘

when you're drawing a string one glyph at a time, left to right, with kerning, the x position of the glyph should be incremented by the result of al_get_glyph_advance(3) applied to the previous glyph drawn and the next glyph to draw. that's the whole trick, iykyk. 🌈

so here's the vibe check on the return value: it's a recommended advance for optimal readability of left-to-right text, as determined by the author of the font. but like β€” it's your world. if you wanna draw the glyphs narrower or wider apart than what al_get_glyph_advance(3) returns, for style or for effect, that's completely your call. slay. πŸ’…

🌟 the figure (aka the visual aid) 🌟

in the figure below is an example of what the result of al_get_glyph_advance may look like for two glyphs A and l of the same font that has kerning for the β€œAl” pair β€” without and with the ALLEGRO_NO_KERNING flag. 🧚

al_get_glyph_advance(font, 'A', 'l')
     ___|___
    /       \
    -------------
        /\   -|
       /  \   |
      /____\  |
     /      \ |
    /        \ \_
    -------------
al_get_glyph_advance(font, 'A', ALLEGRO_NO_KERNING)
     ____|____
    /         \
    ---------------
        /\     -|
       /  \     |
      /____\    |
     /      \   |
    /        \   \_
    ---------------

like, see how the little | marker moved? that's the kerning doing its thing. obsessed. 😭πŸ”₯


⭐ SINCE ⭐

5.1.12


πŸ’œ SEE ALSO πŸ’œ

al_draw_glyph(3), al_get_glyph_width(3), al_get_glyph_dimensions(3)


and that's the whole page, bestie πŸ“ β€” brought to you by the Allegro reference manual and al_get_glyph_advance(3). go forth and kern responsibly. πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from
  • arch rolling: allegro 5.2.11.3-2 Β· Zlib AND BSD-3-Clause AND OFL-1.1 AND LicenseRef-Custom
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms