diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-11-20 16:58:59 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-11-20 16:58:59 -0500 |
commit | c3fd9dfa23969c842991da4cd852330288889463 (patch) | |
tree | 1a2e390ae57109febea9a5853ef9f04559d1f820 /text.c | |
parent | c376f6f53f737d6220206baa323c0bc62d48a9f0 (diff) |
started text rendering
Diffstat (limited to 'text.c')
-rw-r--r-- | text.c | 71 |
1 files changed, 71 insertions, 0 deletions
@@ -0,0 +1,71 @@ +#include "base.h" +no_warn_start +#include "stb_truetype.h" +no_warn_end +#include <stdarg.h> +#include <stdlib.h> + +typedef struct { + float char_height; + u32 nchars; + GLuint texture; + stbtt_bakedchar chars[]; +} Font; + +static char text_err[200]; +static void text_clear_err(void) { + text_err[0] = '\0'; +} +static void text_set_err(char const *fmt, ...) { + va_list args; + va_start(args, fmt); + vsnprintf(text_err, sizeof text_err - 1, fmt, args); + va_end(args); +} + +static bool text_has_err(void) { + return text_err[0] != '\0'; +} + +char const *text_get_err(void) { + return text_err; +} + +Font *text_font_load(char const *ttf_filename, float font_size) { + Font *font = NULL; + u32 nchars = 128; + FILE *ttf_file = fopen(ttf_filename, "rb"); + + text_clear_err(); + + if (ttf_file) { + fseek(ttf_file, 0, SEEK_END); + u32 file_size = (u32)ftell(ttf_file); + if (file_size < (50UL<<20)) { // fonts aren't usually bigger than 50 MB + char *file_data = calloc(1, file_size); + Font *font = calloc(1, sizeof *font + nchars * sizeof *font->chars); + if (file_data && font) { + font->nchars = nchars; + font->char_height = font_size; + stbtt_ + } else { + text_set_err("Not enough memory for font."); + } + free(file_data); + if (text_has_err()) + free(font); + fclose(ttf_file); + } else { + text_set_err("Font file too big (%u megabytes).", (uint)(file_size >> 20)); + } + } else { + text_set_err("Couldn't open font file: %s.", ttf_filename); + } + return NULL; +} + +void text_render(Font *font, char const *text, float x, float y) { +} + +void text_get_size(Font *font, char const *text, float *width, float *height) { +} |