add kerning

main
cosmonaut 2022-04-13 13:29:20 -07:00
parent 6c8043760a
commit 5ee517eba5
1 changed files with 21 additions and 5 deletions

View File

@ -123,11 +123,13 @@ typedef struct CharRange
stbtt_packedchar *data; stbtt_packedchar *data;
uint32_t firstCodepoint; uint32_t firstCodepoint;
uint32_t charCount; uint32_t charCount;
float fontSize;
} CharRange; } CharRange;
typedef struct Packer typedef struct Packer
{ {
uint8_t *fontBytes; uint8_t *fontBytes;
stbtt_fontinfo fontInfo;
stbtt_pack_context *context; stbtt_pack_context *context;
uint8_t *pixels; uint8_t *pixels;
@ -210,6 +212,7 @@ Wellspring_Packer* Wellspring_CreatePacker(
packer->fontBytes = Wellspring_malloc(fontBytesLength); packer->fontBytes = Wellspring_malloc(fontBytesLength);
Wellspring_memcpy(packer->fontBytes, fontBytes, fontBytesLength); Wellspring_memcpy(packer->fontBytes, fontBytes, fontBytesLength);
stbtt_InitFont(&packer->fontInfo, packer->fontBytes, 0);
packer->context = Wellspring_malloc(sizeof(stbtt_pack_context)); packer->context = Wellspring_malloc(sizeof(stbtt_pack_context));
packer->pixels = Wellspring_malloc(sizeof(uint8_t) * width * height); packer->pixels = Wellspring_malloc(sizeof(uint8_t) * width * height);
@ -268,9 +271,12 @@ uint32_t Wellspring_PackFontRanges(
currentCharRange->data = stbPackRanges[i].chardata_for_range; currentCharRange->data = stbPackRanges[i].chardata_for_range;
currentCharRange->firstCodepoint = stbPackRanges[i].first_unicode_codepoint_in_range; currentCharRange->firstCodepoint = stbPackRanges[i].first_unicode_codepoint_in_range;
currentCharRange->charCount = stbPackRanges[i].num_chars; currentCharRange->charCount = stbPackRanges[i].num_chars;
currentCharRange->fontSize = stbPackRanges[i].font_size;
} }
myPacker->rangeCount += numRanges; myPacker->rangeCount += numRanges;
Wellspring_free(stbPackRanges);
return 1; return 1;
} }
@ -319,9 +325,10 @@ uint8_t Wellspring_Draw(
Packer *myPacker = batch->currentPacker; Packer *myPacker = batch->currentPacker;
uint32_t decodeState = 0; uint32_t decodeState = 0;
uint32_t codepoint; uint32_t codepoint;
int32_t glyphIndex; uint32_t previousCodepoint;
int32_t previousGlyphIndex; int32_t rangeIndex;
stbtt_packedchar *rangeData; stbtt_packedchar *rangeData;
float rangeFontSize;
stbtt_aligned_quad charQuad; stbtt_aligned_quad charQuad;
uint32_t vertexBufferIndex; uint32_t vertexBufferIndex;
uint32_t indexBufferIndex; uint32_t indexBufferIndex;
@ -350,7 +357,8 @@ uint8_t Wellspring_Draw(
codepoint < myPacker->ranges[j].firstCodepoint + myPacker->ranges[j].charCount codepoint < myPacker->ranges[j].firstCodepoint + myPacker->ranges[j].charCount
) { ) {
rangeData = myPacker->ranges[j].data; rangeData = myPacker->ranges[j].data;
glyphIndex = codepoint - myPacker->ranges[j].firstCodepoint; rangeIndex = codepoint - myPacker->ranges[j].firstCodepoint;
rangeFontSize = myPacker->ranges[j].fontSize;
break; break;
} }
} }
@ -361,15 +369,21 @@ uint8_t Wellspring_Draw(
return 0; return 0;
} }
if (i > 0)
{
float scale = stbtt_ScaleForPixelHeight(&myPacker->fontInfo, rangeFontSize);
x += scale * stbtt_GetCodepointKernAdvance(&myPacker->fontInfo, previousCodepoint, codepoint);
}
stbtt_GetPackedQuad( stbtt_GetPackedQuad(
rangeData, rangeData,
myPacker->width, myPacker->width,
myPacker->height, myPacker->height,
glyphIndex, rangeIndex,
&x, &x,
&y, &y,
&charQuad, &charQuad,
1 0
); );
if (batch->vertexCount >= batch->vertexCapacity) if (batch->vertexCount >= batch->vertexCapacity)
@ -438,6 +452,8 @@ uint8_t Wellspring_Draw(
batch->vertexCount += 4; batch->vertexCount += 4;
batch->indexCount += 6; batch->indexCount += 6;
previousCodepoint = codepoint;
} }
return 1; return 1;