Newline support for rasterized fonts
There's currently no spacing being used.
@@ -6,6 +6,8 @@ typedef struct {
typedef struct {
FONT* font;
float scale;
+ int height;
+ int spacing;
bool antialias;
int32_t offsetY;
@@ -48,6 +50,11 @@ FONT_RASTER_allocate(WrenVM* vm) {
ASSERT_SLOT_TYPE(vm, 2, NUM, "font size");
raster->scale = stbtt_ScaleForMappingEmToPixels(&font->info, wrenGetSlotDouble(vm, 2));
+ int ascent, descent, linegap;
+ stbtt_GetFontVMetrics(&font->info, &ascent, &descent, &linegap);
+ raster->height = (ascent - descent) * (raster->scale);
+ raster->spacing = linegap * (raster->scale);
+
int32_t x0, x1, y0, y1;
stbtt_GetFontBoundingBox(&font->info, &x0, &x1, &y0, &y1);
raster->offsetY = (-y0) * raster->scale;
@@ -82,6 +89,10 @@ FONT_RASTER_print(WrenVM* vm) {
unsigned char *bitmap;
int w, h;
+ int fontHeight = raster->height;
+ int newlines = 0;
+ //int spacing = raster->spacing;
+
float scale = raster->scale;
int32_t offsetY = raster->offsetY;
@@ -92,6 +103,12 @@ FONT_RASTER_print(WrenVM* vm) {
utf8_int32_t codepoint;
void* v = utf8codepoint(text, &codepoint);
for (int charIndex = 0; charIndex < len; charIndex++) {
+ if (text[charIndex] == '\n') {
+ newlines++;
+ v = utf8codepoint(v, &codepoint);
+ posX = x;
+ continue;
+ }
int ax;
int lsb;
int oY, oX;
@@ -110,7 +127,7 @@ FONT_RASTER_print(WrenVM* vm) {
} else {
outColor = bitmap[j * w + i] > 0 ? color : 0;
}
- ENGINE_pset(engine, posX + i, posY + j, outColor);
+ ENGINE_pset(engine, posX + i, posY + (fontHeight * newlines) + j, outColor);
}
}
posX += ax * scale;
