Added in underline as well.

This commit is contained in:
Adam Harrison 2021-09-16 19:00:56 -04:00
parent 16deedc8a3
commit 940db0f9c7
3 changed files with 9 additions and 5 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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;