stb icon indicating copy to clipboard operation
stb copied to clipboard

Is the "DETAILED USAGE" section of `stb_truetype.h` correct/updated?

Open achalpandeyy opened this issue 1 year ago • 3 comments

Specifically, under the "Displaying a character" heading , it says:

//    Compute the bounding box of the character. It will contain signed values
//    relative to <current_point, baseline>. I.e. if it returns x0,y0,x1,y1,
//    then the character should be displayed in the rectangle from
//    <current_point+SF*x0, baseline+SF*y0> to <current_point+SF*x1,baseline+SF*y1).

If I use stbtt_GetCodepointBitmapBox to compute the bounding box of the character, the values I get back i.e. x0, x1, y0, y1 appear to be already scaled by the SF factor, so there should be no need for the multiplication with SF. I think this for the following two reasons:

  1. Doing x1-x0 and y1-y0 gives the bitmap's width and height respectively as reported by the stbtt_GetCodepointBitmap function which already takes in the SF factor and also reports different bitmap dimensions for different SF values.
  2. When I actually use the formula as specified in the docs above I get extremely small (borderline invisible characters) on the screen which makes me believe that we are multiplying by the SF value twice. See the code below for my usage:
stbtt_fontinfo font_info;
stbtt_InitFont(&font_info, file_data, stbtt_GetFontOffsetForIndex(file_data, 0));

float SF = stbtt_ScaleForPixelHeight(&font_info, 128.f);

int x0, x1, y0, y1;
stbtt_GetCodepointBitmapBox(&font_info, codepoint, SF, SF, &x0, &y0, &x1, &y1);

int width, height, x_offset, y_offset;
unsigned char *bitmap_data = stbtt_GetCodepointBitmap(&font_info, 0, SF, codepoint, &width, &height, &x_offset, &y_offset);

assert(width == (x1-x0));
assert(height == (y1-y0));

float current_point_x = 30.f + SF*x0;
float current_point_y = 40.f + SF*y0;

DrawBitmap(current_point_x, current_point_y, width, height, bitmap_data);

I just want to confirm if I am understanding the usage comment correctly, so I can be reasonably sure that I don't have a bug in this part of my code.

achalpandeyy avatar Oct 29 '23 16:10 achalpandeyy

Yes, you are correct. (The documentation was probably incorrectly derived from stbtt_GetFontBoundingBox.)

nothings avatar Oct 29 '23 16:10 nothings

Thank you, Sean, for the prompt response and all this excellent work.

I assume that this will get bundled with other doc fixes, so closing the issue now.

achalpandeyy avatar Oct 30 '23 03:10 achalpandeyy

Better to leave the issue open to make sure it gets fixed.

nothings avatar Oct 30 '23 07:10 nothings