summaryrefslogtreecommitdiff
path: root/src/render.c
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik.ramaekers@protonmail.com>2020-02-03 18:55:17 +0100
committerAldrik Ramaekers <aldrik.ramaekers@protonmail.com>2020-02-03 18:55:17 +0100
commit83cbdcc8c3d1c900417457f2ef44eafde123db93 (patch)
tree2623f1056b2191c9e48f22811018042ee0a6f55b /src/render.c
parent7e17e6bb7e7d7547bc01fa92090f59699e937a72 (diff)
fix text rendering
Diffstat (limited to 'src/render.c')
-rw-r--r--src/render.c85
1 files changed, 52 insertions, 33 deletions
diff --git a/src/render.c b/src/render.c
index e126133..666b2f9 100644
--- a/src/render.c
+++ b/src/render.c
@@ -88,19 +88,22 @@ s32 render_text_ellipsed(font *font, s32 x, s32 y, s32 maxw, char *text, color t
s32 width = g.width;
+ int advance, lsb, kern;
+ stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb);
+
s32 y_ = y + font->px_h + g.yoff;
+ s32 x_to_render = x_ + (lsb*font->scale);
- glTexCoord2i(0, 0); glVertex3i(x_,y_, render_depth);
- glTexCoord2i(0, 1); glVertex3i(x_,y_+g.height, render_depth);
- glTexCoord2i(1, 1); glVertex3i(x_+g.width,y_+g.height, render_depth);
- glTexCoord2i(1, 0); glVertex3i(x_+g.width,y_, render_depth);
+ glTexCoord2i(0, 0); glVertex3i(x_to_render,y_, render_depth);
+ glTexCoord2i(0, 1); glVertex3i(x_to_render,y_+g.height, render_depth);
+ glTexCoord2i(1, 1); glVertex3i(x_to_render+g.width,y_+g.height, render_depth);
+ glTexCoord2i(1, 0); glVertex3i(x_to_render+g.width,y_, render_depth);
glEnd();
/* add kerning */
- int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
- if (kern != 0) x_ += kern * font->scale;
- x_ += g.width+g.xoff;
+ kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
+ x_ += (advance*font->scale)+(kern*font->scale);
if (!in_ellipse && (x_-x) > maxw-(font->glyphs['.'].width*3))
{
@@ -141,19 +144,22 @@ s32 render_text(font *font, s32 x, s32 y, char *text, color tint)
s32 width = g.width;
+ int advance, lsb, kern;
+ stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb);
+
s32 y_ = y + font->px_h + g.yoff;
+ s32 x_to_render = x_ + (lsb*font->scale);
- glTexCoord2i(0, 0); glVertex3i(x_,y_, render_depth);
- glTexCoord2i(0, 1); glVertex3i(x_,y_+g.height, render_depth);
- glTexCoord2i(1, 1); glVertex3i(x_+g.width,y_+g.height, render_depth);
- glTexCoord2i(1, 0); glVertex3i(x_+g.width,y_, render_depth);
+ glTexCoord2i(0, 0); glVertex3i(x_to_render,y_, render_depth);
+ glTexCoord2i(0, 1); glVertex3i(x_to_render,y_+g.height, render_depth);
+ glTexCoord2i(1, 1); glVertex3i(x_to_render+g.width,y_+g.height, render_depth);
+ glTexCoord2i(1, 0); glVertex3i(x_to_render+g.width,y_, render_depth);
glEnd();
/* add kerning */
- int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
- if (kern != 0) x_ += kern * font->scale;
- x_ += g.width+g.xoff;
+ kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
+ x_ += (advance*font->scale)+(kern*font->scale);
}
glDisable(GL_TEXTURE_2D);
@@ -209,19 +215,23 @@ s32 render_text_cutoff(font *font, s32 x, s32 y, char *text, color tint, u16 cut
s32 width = g.width;
+ int advance, lsb, kern;
+ stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb);
+
s32 y__ = y_ + font->px_h + g.yoff;
+ s32 x_to_render = x_ + (lsb*font->scale);
- glTexCoord2i(0, 0); glVertex3i(x_,y__, render_depth);
- glTexCoord2i(0, 1); glVertex3i(x_,y__+g.height, render_depth);
- glTexCoord2i(1, 1); glVertex3i(x_+g.width,y__+g.height, render_depth);
- glTexCoord2i(1, 0); glVertex3i(x_+g.width,y__, render_depth);
+ glTexCoord2i(0, 0); glVertex3i(x_to_render,y__, render_depth);
+ glTexCoord2i(0, 1); glVertex3i(x_to_render,y__+g.height, render_depth);
+ glTexCoord2i(1, 1); glVertex3i(x_to_render+g.width,y__+g.height, render_depth);
+ glTexCoord2i(1, 0); glVertex3i(x_to_render+g.width,y__, render_depth);
glEnd();
- int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
- if (kern != 0) x_ += kern * font->scale;
+ /* add kerning */
+ kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
+ x_ += (advance*font->scale)+(kern*font->scale);
- x_ += g.width+g.xoff;
if (x_ > x+cutoff_width)
{
x_ = x;
@@ -260,10 +270,13 @@ s32 calculate_cursor_position(font *font, char *text, s32 click_x)
s32 width = g.width;
s32 width_next = font->glyphs[ch_next].width;
- int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
- if (kern != 0) x += kern * font->scale;
- x += g.width+g.xoff;
+ int advance, lsb, kern;
+ stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb);
+
+ /* add kerning */
+ kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
+ x += (advance*font->scale)+(kern*font->scale);
if (x - (width_next/5) > click_x)
{
@@ -301,10 +314,12 @@ s32 calculate_text_width_from_upto(font *font, char *text, s32 from, s32 index)
if (i >= from)
{
- int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
- if (kern != 0) x += kern * font->scale;
+ int advance, lsb, kern;
+ stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb);
- x += g.width+g.xoff;
+ /* add kerning */
+ kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
+ x += (advance*font->scale)+(kern*font->scale);
}
i++;
@@ -336,10 +351,12 @@ s32 calculate_text_width_upto(font *font, char *text, s32 index)
glyph g = font->glyphs[ch];
s32 width = g.width;
- int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
- if (kern != 0) x += kern * font->scale;
+ int advance, lsb, kern;
+ stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb);
- x += g.width+g.xoff;
+ /* add kerning */
+ kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
+ x += (advance*font->scale)+(kern*font->scale);
i++;
}
@@ -367,10 +384,12 @@ s32 calculate_text_width(font *font, char *text)
glyph g = font->glyphs[ch];
s32 width = g.width;
- int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
- if (kern != 0) x += kern * font->scale;
+ int advance, lsb, kern;
+ stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb);
- x += g.width+g.xoff;
+ /* add kerning */
+ kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next);
+ x += (advance*font->scale)+(kern*font->scale);
}
return x;