STB libraries»Forums
Simon Anciaux
1337 posts
How to position character with a negative left side bearing ?
Hi, I'm using stb_truetype.h and I've got some questions on how to properly position characters.

I'm using y+ goes up, I've got x0, y0, x1, y1 from stbtt_GetCodepointBox and scaled them. Are the following lines correct ?

1. bitmapY = baseLine + y0;
2. bitmapX = x + leftSideBearing; (Or x + x0 since x0 and leftSideBearing are always the same);
3. Some character like 'j' in the Verdana font have a negative left side bearing. Do I need to not use the left side bearing if it's the first character in the "line" since it will draw "out of bounds" ? This freetype.org page seems to indicate that the left side bearing should always be positive.
4. nextX = x + advanceWidth + kerning( current, next )
Sean Barrett
17 posts / 1 project
former pro game developer (Thief etc. @ LookingGlass) current indie game dev (Promesst etc.) creator of stb libraries (stb_image.h etc.)
How to position character with a negative left side bearing ?
Edited by Sean Barrett on
This is technically outside the scope of stb_truetype; what the font defines the values to and what you do with those values is up to you.

I believe what I've done in the past is used the LSB (or maybe just the left bounding box of the character) to determine where to set the leftmost cursor position.

I.e. something like

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
start_line()
{
   //curpos = 0;   // don't actually do this yet
   first_char = true;
}

display_char(c)
{
   if (first_char) {
      curpos = -lsb(c);
      first_char = false;
   }
   ...
}

Simon Anciaux
1337 posts
How to position character with a negative left side bearing ?
Thanks, I wanted to be sure I wasn't misunderstanding what the lfb was.