-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.c
89 lines (66 loc) · 1.48 KB
/
font.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdlib.h>
#include <stdio.h>
#include "font.h"
#include "texture.h"
static struct glyph *
load_glyph(const char *name, char ch)
{
struct glyph *g;
char path[256];
g = malloc(sizeof *g);
g->character = ch;
snprintf(path, sizeof path, "fonts/%s/%c.png", name, ch);
g->texture = load_texture_from_png(path, &g->width, &g->height);
return g;
}
static void
load_glyph_range(const char *name, int i, int j, struct list *glyphs)
{
struct glyph *g;
int ch;
for (ch = i; ch <= j; ch++) {
g = load_glyph(name, ch);
list_add(glyphs, (void *) g);
}
}
static struct list *
load_glyphs(const char *name)
{
struct list *glyphs;
glyphs = list_new();
load_glyph_range(name, '0', '9', glyphs);
load_glyph_range(name, 'a', 'z', glyphs);
load_glyph_range(name, 'A', 'Z', glyphs);
return glyphs;
}
static void
glyph_destroy(struct glyph *g)
{
glDeleteTextures(1, &g->texture);
free(g);
}
struct font *
font_new(const char *name)
{
struct font *f;
f = malloc(sizeof *f);
f->glyphs = load_glyphs(name);
f->spacing = 10;
return f;
}
struct glyph *
font_get_glyph(struct font *f, char ch)
{
struct node *n;
for (n = f->glyphs->first; n; n = n->next) {
struct glyph *g = (struct glyph *) n->data;
if (g->character == ch)
return g;
}
return NULL;
}
void font_destroy(struct font *f)
{
list_free(f->glyphs, (list_free_func) glyph_destroy);
free(f);
}