Skip to content

Instantly share code, notes, and snippets.

@roman-d
Created June 20, 2017 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roman-d/4b70bcf0034f90c384b6d6bdcb587d7d to your computer and use it in GitHub Desktop.
Save roman-d/4b70bcf0034f90c384b6d6bdcb587d7d to your computer and use it in GitHub Desktop.
hhmacos
stbtt_fontinfo Font;
stbtt_InitFont(&Font, (u8 *)TTFFile.Contents, stbtt_GetFontOffsetForIndex((u8 *)TTFFile.Contents, 0));
int Width, Height, XOffset, YOffset;
r32 Scale = stbtt_ScaleForPixelHeight(&Font, 128.0f); // 64.0f
u8 *MonoBitmap = stbtt_GetCodepointBitmap(&Font, 0, Scale, Codepoint,
&Width, &Height, &XOffset, &YOffset);
int Ascent, Descent, LineGap;
stbtt_GetFontVMetrics(&Font, &Ascent, &Descent, &LineGap);
int X0, X1, Y0, Y1;
stbtt_GetCodepointBox(&Font, Codepoint, &X0, &Y0, &X1, &Y1);
r32 TotalHeight = ((Ascent - Descent + LineGap) * Scale);
r32 MaxY = (Ascent * Scale - Y0 * Scale);
Result.Width = Width + 1;
Result.Height = Height + 1;
Result.Pitch = Result.Width * BITMAP_BYTES_PER_PIXEL;
Result.Memory = malloc(Result.Height * Result.Pitch);
Result.Free = Result.Memory;
u8 *DestRow = (u8 *) Result.Memory + (Height - 1) * Result.Pitch;
u8 *Source = MonoBitmap;
for(s32 Y = 0;
Y < Height;
++Y)
{
u32 *Dest = (u32 *) DestRow;
for(s32 X = 0;
X < Width;
++X)
{
u32 Pixel = *Source;
r32 Gray = (r32)(Pixel & 0xFF);
v4 Texel = {255.0f, 255.0f, 255.0f, Gray};
Texel = SRGB255ToLinear1(Texel);
Texel.rgb *= Texel.a;
Texel = Linear1ToSRGB255(Texel);
*Dest++ = (((uint32)(Texel.a + 0.5f) << 24) |
((uint32)(Texel.r + 0.5f) << 16) |
((uint32)(Texel.g + 0.5f) << 8) |
((uint32)(Texel.b + 0.5f) << 0));
++Source;
}
DestRow -= Result.Pitch;
}
stbtt_FreeBitmap(MonoBitmap, 0);
free(TTFFile.Contents);
Asset->Bitmap.AlignPercentage[0] = 1.0f / (r32) Result.Width;
Asset->Bitmap.AlignPercentage[1] = (1.0f + MaxY - (TotalHeight + Descent*Scale)) / (r32) Result.Height;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment