From 940db0f9c78ecb8f8b0b29196a742980b76ac253 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Thu, 16 Sep 2021 19:00:56 -0400 Subject: [PATCH] Added in underline as well. --- src/api/renderer.c | 6 ++++-- src/renderer.c | 6 ++++-- src/renderer.h | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/api/renderer.c b/src/api/renderer.c index 3d89f041..be06bce0 100644 --- a/src/api/renderer.c +++ b/src/api/renderer.c @@ -21,7 +21,6 @@ static int f_font_load(lua_State *L) { } } } - lua_pop(L, 1); lua_getfield(L, 3, "hinting"); if (lua_isstring(L, -1)) { const char *hinting = lua_tostring(L, -1); @@ -43,7 +42,10 @@ static int f_font_load(lua_State *L) { lua_getfield(L, 3, "bold"); if (lua_toboolean(L, -1)) font_style |= FONT_STYLE_BOLD; - lua_pop(L, 1); + lua_getfield(L, 3, "underline"); + if (lua_toboolean(L, -1)) + font_style |= FONT_STYLE_UNDERLINE; + lua_pop(L, 5); } RenFont** font = lua_newuserdata(L, sizeof(RenFont*)); *font = ren_font_load(filename, size, subpixel, font_hinting, font_style); diff --git a/src/renderer.c b/src/renderer.c index 435fdeb9..91cd82fc 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -237,9 +237,9 @@ int ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor colo GlyphMetric* metric = &set->metrics[codepoint % 256]; int bitmap_index = font->subpixel ? (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED) : 0; unsigned char* source_pixels = set->surface[bitmap_index]->pixels; - int start_x = pen_x + metric->bitmap_left, endX = metric->x1 - metric->x0 + pen_x; + int start_x = pen_x + metric->bitmap_left, end_x = metric->x1 - metric->x0 + pen_x; int glyph_end = metric->x1, glyph_start = metric->x0; - if (color.a > 0 && endX >= clip.x && start_x <= clip_end_x) { + if (color.a > 0 && end_x >= clip.x && start_x <= clip_end_x) { for (int line = metric->y0; line < metric->y1; ++line) { int target_y = line + y - metric->y0 - metric->bitmap_top + font->size; if (target_y < clip.y) @@ -274,6 +274,8 @@ int ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor colo } pen_x += metric->xadvance ? metric->xadvance : font->space_advance; } + if (font->style & FONT_STYLE_UNDERLINE) + ren_draw_rect((RenRect){ x, y + ren_font_get_height(font) - 1, pen_x - x, 1 }, color); return pen_x; } diff --git a/src/renderer.h b/src/renderer.h index f07f4411..b798c2b5 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -7,7 +7,7 @@ typedef struct RenFont RenFont; typedef enum { FONT_HINTING_NONE, FONT_HINTING_SLIGHT, FONT_HINTING_FULL } ERenFontHinting; -typedef enum { FONT_STYLE_BOLD = 1, FONT_STYLE_ITALIC = 2 } ERenFontStyle; +typedef enum { FONT_STYLE_BOLD = 1, FONT_STYLE_ITALIC = 2, FONT_STYLE_UNDERLINE = 4 } ERenFontStyle; typedef struct { uint8_t b, g, r, a; } RenColor; typedef struct { int x, y, width, height; } RenRect;