-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathfree_glyph.c
More file actions
164 lines (142 loc) · 5.38 KB
/
free_glyph.c
File metadata and controls
164 lines (142 loc) · 5.38 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <assert.h>
#include <stdbool.h>
#include "./free_glyph.h"
#define TAB_SIZE 4
void free_glyph_atlas_init(Free_Glyph_Atlas *atlas, FT_Face face)
{
// TODO: Introduction of SDF font slowed down the start up time
// We need to investigate what's up with that
FT_Int32 load_flags = FT_LOAD_RENDER;
for (int i = 32; i < 128; ++i) {
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);
}
atlas->atlas_width += face->glyph->bitmap.width;
if (atlas->atlas_height < face->glyph->bitmap.rows) {
atlas->atlas_height = face->glyph->bitmap.rows;
}
load_flags = FT_LOAD_RENDER | FT_LOAD_TARGET_(FT_RENDER_MODE_SDF);
}
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &atlas->glyphs_texture);
glBindTexture(GL_TEXTURE_2D, atlas->glyphs_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
(GLsizei) atlas->atlas_width,
(GLsizei) atlas->atlas_height,
0,
GL_RED,
GL_UNSIGNED_BYTE,
NULL);
int x = 0;
load_flags = FT_LOAD_RENDER;
for (int i = 32; i < 128; ++i) {
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);
}
if (FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL)) {
fprintf(stderr, "ERROR: could not render glyph of a character with code %d\n", i);
exit(1);
}
atlas->metrics[i].ax = face->glyph->advance.x >> 6;
atlas->metrics[i].ay = face->glyph->advance.y >> 6;
atlas->metrics[i].bw = face->glyph->bitmap.width;
atlas->metrics[i].bh = face->glyph->bitmap.rows;
atlas->metrics[i].bl = face->glyph->bitmap_left;
atlas->metrics[i].bt = face->glyph->bitmap_top;
atlas->metrics[i].tx = (float) x / (float) atlas->atlas_width;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(
GL_TEXTURE_2D,
0,
x,
0,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer);
x += face->glyph->bitmap.width;
load_flags = FT_LOAD_RENDER | FT_LOAD_TARGET_(FT_RENDER_MODE_SDF);
}
// tab hack
load_flags = FT_LOAD_RENDER;
int i = '\t';
if (FT_Load_Char(face, 32, load_flags)) {
fprintf(stderr, "ERROR: could not load glyph of a character with code %d\n", i);
exit(1);
}
if (FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL)) {
fprintf(stderr, "ERROR: could not render glyph of a character with code %d\n", i);
exit(1);
}
atlas->metrics[i].ax = (face->glyph->advance.x >> 6) * TAB_SIZE;
atlas->metrics[i].ay = face->glyph->advance.y >> 6;
atlas->metrics[i].bw = face->glyph->bitmap.width * TAB_SIZE;
atlas->metrics[i].bh = face->glyph->bitmap.rows;
atlas->metrics[i].bl = face->glyph->bitmap_left;
atlas->metrics[i].bt = face->glyph->bitmap_top;
atlas->metrics[i].tx = (float) 0 / (float) atlas->atlas_width;
}
float free_glyph_atlas_cursor_pos(const Free_Glyph_Atlas *atlas, const char *text, size_t text_size, Vec2f pos, size_t col)
{
for (size_t i = 0; i < text_size; ++i) {
if (i == col) {
return pos.x;
}
size_t glyph_index = text[i];
if (glyph_index >= GLYPH_METRICS_CAPACITY) {
glyph_index = '?';
}
Glyph_Metric metric = atlas->metrics[glyph_index];
pos.x += metric.ax;
pos.y += metric.ay;
}
return pos.x;
}
void free_glyph_atlas_measure_line_sized(Free_Glyph_Atlas *atlas, const char *text, size_t text_size, Vec2f *pos)
{
for (size_t i = 0; i < text_size; ++i) {
size_t glyph_index = text[i];
// TODO: support for glyphs outside of ASCII range
if (glyph_index >= GLYPH_METRICS_CAPACITY) {
glyph_index = '?';
}
Glyph_Metric metric = atlas->metrics[glyph_index];
pos->x += metric.ax;
pos->y += metric.ay;
}
}
void free_glyph_atlas_render_line_sized(Free_Glyph_Atlas *atlas, Simple_Renderer *sr, const char *text, size_t text_size, Vec2f *pos, Vec4f color)
{
for (size_t i = 0; i < text_size; ++i) {
size_t glyph_index = text[i];
// TODO: support for glyphs outside of ASCII range
if (glyph_index >= GLYPH_METRICS_CAPACITY) {
glyph_index = '?';
}
Glyph_Metric metric = atlas->metrics[glyph_index];
float x2 = pos->x + metric.bl;
float y2 = -pos->y - metric.bt;
float w = metric.bw;
float h = metric.bh;
pos->x += metric.ax;
pos->y += metric.ay;
simple_renderer_image_rect(
sr,
vec2f(x2, -y2),
vec2f(w, -h),
vec2f(metric.tx, 0.0f),
vec2f(metric.bw / (float) atlas->atlas_width, metric.bh / (float) atlas->atlas_height),
color);
}
}