-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.h
277 lines (236 loc) · 8.94 KB
/
text.h
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#ifndef ICYLIB_TEXT_H
#define ICYLIB_TEXT_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "thirdparty/stb_truetype.h"
#include "icylib.h"
typedef struct icylib_FontCacheEntry {
const char* name;
stbtt_fontinfo* font;
} icylib_FontCacheEntry;
typedef struct icylib_FontCache {
icylib_FontCacheEntry* data;
int length;
} icylib_FontCache;
typedef enum icylib_HorizontalAlignment {
ICYLIB_HORIZONTAL_ALIGNMENT_LEFT,
ICYLIB_HORIZONTAL_ALIGNMENT_CENTER,
ICYLIB_HORIZONTAL_ALIGNMENT_RIGHT
} icylib_HorizontalAlignment;
typedef enum icylib_VerticalAlignment {
ICYLIB_VERTICAL_ALIGNMENT_TOP,
ICYLIB_VERTICAL_ALIGNMENT_CENTER,
ICYLIB_VERTICAL_ALIGNMENT_BOTTOM
} icylib_VerticalAlignment;
icylib_FontCache* icylib_get_font_cache();
void icylib_set_font_cache(icylib_FontCache* fonts);
char* icylib_get_default_font_path();
char* icylib_get_font_variant_path(const char* font_path, const char* variant);
void icylib_read_bytes(const char* path, unsigned char** font_file);
void icylib_measure_text_size(const char* text, const char* font_path, int pixel_size, double* result_x, double* result_y);
#ifdef ICYLIB_IMPLEMENTATION
icylib_FontCache* icylib_get_font_cache() {
static icylib_FontCache* fonts = NULL;
if (!fonts) {
fonts = ICYLIB_MALLOC(sizeof(icylib_FontCache));
fonts->data = ICYLIB_MALLOC(1);
fonts->length = 0;
}
return fonts;
}
void icylib_set_font_cache(icylib_FontCache* fonts) {
icylib_FontCache* old_fonts = icylib_get_font_cache();
if (old_fonts->data) {
ICYLIB_FREE(old_fonts->data);
}
old_fonts->data = fonts->data;
}
char* icylib_get_default_font_path() {
// TODO: Improve this
#ifdef _WIN32
return "C:/Windows/Fonts/arial.ttf";
#elif __APPLE__
return "/Library/Fonts/Arial.ttf";
#elif __ANDROID__
// taken from the V standard library
char* xml_files[] = { "/system/etc/system_fonts.xml", "/system/etc/fonts.xml", "/etc/system_fonts.xml", "/etc/fonts.xml", "/data/fonts/fonts.xml", "/etc/fallback_fonts.xml" };
char* font_locations[] = { "/system/fonts", "/data/fonts" };
for (int i = 0; i < 6; ++i) {
FILE* file = fopen(xml_files[i], "r");
if (file) {
char* xml = NULL;
size_t n = 0;
ssize_t read;
while ((read = getline(&xml, &n, file)) != -1) {
char* candidate_font = NULL;
if (strstr(xml, "<font")) {
candidate_font = strstr(xml, ">") + 1;
candidate_font = strtok(candidate_font, "<");
while (*candidate_font == ' ' || *candidate_font == '\t' || *candidate_font == '\n' || *candidate_font == '\r') {
candidate_font++;
}
char* end = candidate_font + strlen(candidate_font) - 1;
while (end > candidate_font && (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')) {
*end = '\0';
end--;
}
if (strstr(candidate_font, ".ttf")) {
for (int j = 0; j < 2; ++j) {
char* candidate_path = ICYLIB_MALLOC_ATOMIC(strlen(font_locations[j]) + strlen(candidate_font) + 2);
strcpy(candidate_path, font_locations[j]);
strcat(candidate_path, "/");
strcat(candidate_path, candidate_font);
if (access(candidate_path, F_OK) == 0) {
return candidate_path;
}
ICYLIB_FREE(candidate_path);
}
}
}
}
fclose(file);
}
}
ICYLIB_ERROR("No default font found");
#else
return "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf";
#endif
}
// This is a modified version of os.font.get_path_variant() of the V standard library
char* icylib_get_font_variant_path(const char* font_path, const char* variant) {
// Find some way to make this shorter and more eye-pleasant
// NotoSans, LiberationSans, DejaVuSans, Arial, and SFNS should work
char* file = strrchr(font_path, '/');
char* fpath = ICYLIB_MALLOC_ATOMIC(file - font_path + 2);
strncpy(fpath, font_path, file - font_path + 1);
fpath[file - font_path + 1] = '\0';
file++;
file = strrchr(font_path, '/');
file = file ? file + 1 : (char*)font_path;
char* file_copy = ICYLIB_MALLOC_ATOMIC(strlen(file) + 1);
strcpy(file_copy, file);
char* dot_position = strrchr(file_copy, '.');
if (dot_position != NULL) {
*dot_position = '\0';
}
if (strcmp(variant, "normal") == 0) {
// No changes for normal variant
}
else if (strcmp(variant, "bold") == 0) {
if (strstr(fpath, "-Regular") != NULL || strstr(file_copy, "-Regular") != NULL) {
strcpy(file_copy, strstr(file_copy, "-Regular") ? strstr(file_copy, "-Regular") + 1 : strstr(fpath, "-Regular") + 1);
strcat(file_copy, "-Bold");
}
else if (strncmp(file_copy, "DejaVuSans", 10) == 0 || strncmp(file_copy, "DroidSans", 9) == 0) {
strcat(file_copy, "-Bold");
}
else if (strncasecmp(file_copy, "arial", 5) == 0) {
strcat(file_copy, "bd");
}
else {
strcat(file_copy, "-bold");
}
#ifdef __APPLE__
if (access("SFNS-bold", F_OK) == 0) {
strcpy(file_copy, "SFNS-bold");
}
#endif
}
else if (strcmp(variant, "italic") == 0) {
if (strstr(file_copy, "-Regular") != NULL) {
strcpy(file_copy, strstr(file_copy, "-Regular") + 1);
strcat(file_copy, "-Italic");
}
else if (strncmp(file_copy, "DejaVuSans", 10) == 0) {
strcat(file_copy, "-Oblique");
}
else if (strncasecmp(file_copy, "arial", 5) == 0) {
strcat(file_copy, "i");
}
else {
strcat(file_copy, "Italic");
}
}
else if (strcmp(variant, "mono") == 0) {
if (strcmp(file_copy, "Mono-Regular") != 0 && strstr(file_copy, "-Regular") != NULL) {
strcpy(file_copy, strstr(file_copy, "-Regular") + 1);
strcat(file_copy, "Mono-Regular");
}
else if (strncasecmp(file_copy, "arial", 5) != 0) {
strcat(file_copy, "Mono");
}
}
char* result = ICYLIB_MALLOC_ATOMIC(strlen(fpath) + strlen(file_copy) + 5); // 5 for ".ttf" and null terminator
strcpy(result, fpath);
strcat(result, file_copy);
strcat(result, ".ttf");
ICYLIB_FREE(fpath);
ICYLIB_FREE(file_copy);
return result;
}
void icylib_read_bytes(const char* path, unsigned char** font_file) {
FILE* file = fopen(path, "rb");
fseek(file, 0, SEEK_END);
long length = ftell(file);
fseek(file, 0, SEEK_SET);
*font_file = ICYLIB_MALLOC_ATOMIC(length);
fread(*font_file, 1, length, file);
fclose(file);
}
void icylib_measure_text_size(const char* text, const char* font_path, int pixel_size, double* result_x, double* result_y) {
stbtt_fontinfo* font = NULL;
icylib_FontCache* fonts = icylib_get_font_cache();
for (int i = 0; i < fonts->length; ++i) {
if (strcmp(fonts->data[i].name, font_path) == 0) {
font = fonts->data[i].font;
break;
}
}
if (font == NULL) {
unsigned char* font_file;
icylib_read_bytes(font_path, &font_file);
font = ICYLIB_MALLOC(sizeof(stbtt_fontinfo));
int offset = stbtt_GetFontOffsetForIndex(font_file, 0);
stbtt_InitFont(font, font_file, offset);
fonts->data = ICYLIB_REALLOC(fonts->data, sizeof(icylib_FontCacheEntry) * (fonts->length + 1));
fonts->data[fonts->length].name = font_path;
fonts->data[fonts->length].font = font;
fonts->length++;
}
float scale = stbtt_ScaleForPixelHeight(font, pixel_size);
int ascent, zero;
stbtt_GetFontVMetrics(font, &ascent, &zero, &zero);
int advance, lsb, x0, y0, x1, y1;
double x_cursor = 0.0;
double max_x = 0.0;
double y_cursor = 0.0;
size_t text_len = strlen(text);
for (size_t i = 0; i < text_len; ++i) {
if (text[i] == '\n') {
if (x_cursor > max_x) {
max_x = x_cursor;
}
y_cursor += ascent * scale;
x_cursor = 0;
continue;
}
int c = (int)text[i];
stbtt_GetCodepointHMetrics(font, c, &advance, &lsb);
stbtt_GetCodepointBitmapBox(font, c, scale, scale, &x0, &y0, &x1, &y1);
if (i < text_len - 1) {
x_cursor += advance * scale;
x_cursor += scale * stbtt_GetCodepointKernAdvance(font, c, (int)text[i + 1]);
}
}
if (text_len >= 1) {
x_cursor += advance * scale;
}
if (x_cursor > max_x) {
max_x = x_cursor;
}
*result_x = max_x;
*result_y = y_cursor + ascent * scale;
}
#endif
#endif