nothings / stb

stb single-file public domain libraries for C/C++
https://twitter.com/nothings
Other
25.83k stars 7.66k forks source link

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

Open achalpandeyy opened 8 months ago

achalpandeyy commented 8 months ago

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 + SFx0; float current_point_y = 40.f + SFy0;

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.
nothings commented 8 months ago

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

achalpandeyy commented 8 months ago

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.

nothings commented 8 months ago

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