commit 99e9318086623dae931cb16ec4283ae5d2324474
parent a12a857fbfc27178d67de6b61653b0311a79f913
Author: Máté Homolya <mate.homolya@gmail.com>
Date: Thu, 9 Feb 2023 19:18:11 -0800
Distance field font rendering
Diffstat:
2 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/shaders/simple_text.frag b/shaders/simple_text.frag
@@ -6,5 +6,8 @@ in vec4 out_color;
in vec2 out_uv;
void main() {
- gl_FragColor = texture(image, out_uv).x*out_color;
+ float d = texture(image, out_uv).r;
+ float aaf = fwidth(d);
+ float alpha = smoothstep(0.5 - aaf, 0.5 + aaf, d);
+ gl_FragColor = vec4(out_color.rgb, alpha);
}
diff --git a/src/free_glyph.c b/src/free_glyph.c
@@ -4,8 +4,9 @@
void free_glyph_atlas_init(Free_Glyph_Atlas *atlas, FT_Face face)
{
+ FT_Int32 load_flags = FT_LOAD_RENDER | FT_LOAD_TARGET_(FT_RENDER_MODE_SDF);
for (int i = 32; i < 128; ++i) {
- if (FT_Load_Char(face, i, FT_LOAD_RENDER)) {
+ if (FT_Load_Char(face, i, load_flags)) {
fprintf(stderr, "ERROR: could not load glyph of a character with code %d\n", i);
exit(1);
}
@@ -39,7 +40,7 @@ void free_glyph_atlas_init(Free_Glyph_Atlas *atlas, FT_Face face)
int x = 0;
for (int i = 32; i < 128; ++i) {
- if (FT_Load_Char(face, i, FT_LOAD_RENDER)) {
+ if (FT_Load_Char(face, i, load_flags)) {
fprintf(stderr, "ERROR: could not load glyph of a character with code %d\n", i);
exit(1);
}