bb
This commit is contained in:
parent
a32b211d20
commit
5018ccbbb0
13 changed files with 5549 additions and 0 deletions
66
text_rendering/font.c
Normal file
66
text_rendering/font.c
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
#define _POSIX_C_SOURCE 200809l
|
||||
#define STB_TRUETYPE_IMPLEMENTATION
|
||||
#define STBTT_STATIC
|
||||
|
||||
#include <grapheme.h>
|
||||
|
||||
#include "stb_truetype.h"
|
||||
#include "util.h"
|
||||
#include "font.h"
|
||||
|
||||
|
||||
#define UTF8(c) (c&0x80)
|
||||
|
||||
|
||||
// Generates a cached atlas of font glyphs encoded usign a signed distance field
|
||||
// https://www.youtube.com/watch?v=1b5hIMqz_wM
|
||||
// https://github.com/pjako/msdf_c
|
||||
// this way the texture atlas for the font will be bigger but we save up the space
|
||||
// needed for rendering the font in multiple sizes
|
||||
|
||||
|
||||
struct font_atlas {
|
||||
unsigned int glyphs, width, height;
|
||||
unsigned char *atlas;
|
||||
unsigned int glyph_max_w, glyph_max_h;
|
||||
|
||||
struct {
|
||||
stbtt_fontinfo info;
|
||||
float scale;
|
||||
} stb;
|
||||
int file_size;
|
||||
unsigned char *file;
|
||||
};
|
||||
|
||||
|
||||
// loads a font into memory, storing all the ASCII characters in the atlas
|
||||
int load_font(struct font_atlas *atlas, const char *path, int height)
|
||||
{
|
||||
if (!atlas || !path)
|
||||
return -1;
|
||||
|
||||
dump_file(path, &(atlas->file), &(atlas->file_size));
|
||||
|
||||
stbtt_InitFont(&(atlas->stb.info), atlas->file, 0);
|
||||
atlas->stb.scale = stbtt_ScaleForPixelHeight(&(atlas->stb.info), height);
|
||||
int ascent, descent, linegap, baseline;
|
||||
int x0,y0,x1,y1;
|
||||
stbtt_GetFontVMetrics(&(atlas->stb.info), &ascent, &descent, &linegap);
|
||||
stbtt_GetFontBoundingBox(&(atlas->stb.info), &x0, &y0, &x1, &y1);
|
||||
|
||||
baseline = atlas->stb.scale * -y0;
|
||||
atlas->glyph_max_w = (atlas->stb.scale*x1) - (atlas->stb.scale*x0);
|
||||
atlas->glyph_max_h = (baseline+atlas->stb.scale*y1) - (baseline+atlas->stb.scale*y0);
|
||||
|
||||
atlas->atlas = emalloc(atlas->glyph_max_w*atlas->glyph_max_h*CACHE_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int free_font(struct font_atlas *atlas)
|
||||
{
|
||||
efree(atlas->atlas);
|
||||
efree(atlas->file);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue