summaryrefslogtreecommitdiff
path: root/text.c
blob: 19d62a234fa7a795326dbb011d4c74bdd08b068d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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) {
}