-
Notifications
You must be signed in to change notification settings - Fork 4
/
textlabel.c
427 lines (372 loc) · 11.1 KB
/
textlabel.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include <fontconfig/fontconfig.h>
#include <inttypes.h>
typedef struct /*_TLABEL_OPTS*/ {
char* fontspec;
char** lines;
unsigned width;
} OPTIONS;
int usage(char* fn){
fprintf(stderr, "textlabel - generate bitmap data from text\n");
fprintf(stderr, "Usage: %s [<options>] <text>\n", fn);
fprintf(stderr, "Recognized options:\n");
fprintf(stderr, "\t--font <fontspec>\tSet font to use by fontconfig name\n");
fprintf(stderr, "\t--width <width>\tSet output width (default: 64)\n");
fprintf(stderr, "\t--\t\t\tEnd option parsing\n");
return -1;
}
size_t stored_lines(char** lines){
size_t u;
if(!lines){
return 0;
}
for(u = 0; lines[u]; u++){
}
return u;
}
#define max(a,b) (( (a) > (b) ) ? (a) : (b))
int line_store(char*** lines, char* line){
size_t size = 0;
//expand array
size = stored_lines(*lines);
*lines = realloc(*lines, (size + 2) * sizeof(char*));
if(!(*lines)){
return -1;
}
//allocate
*((*lines) + size + 1) = NULL;
*((*lines) + size) = calloc(strlen(line) + 1, sizeof(char));
if(!((*lines) + size)){
return -1;
}
//store
strncpy(*((*lines) + size), line, strlen(line));
return 0;
}
int args_parse(OPTIONS* opts, int argc, char** argv){
size_t i, c;
bool stop_parsing = false;
char* current_line = NULL;
int current_offset = 0;
for(i = 0; i < argc; i++){
if(!stop_parsing && !strcmp(argv[i], "--font")){
if(argc > i + 1){
opts->fontspec = argv[++i];
}
else{
return -1;
}
}
else if(!stop_parsing && !strcmp(argv[i], "--width")){
if(argc > i + 1){
opts->width = strtoul(argv[++i], NULL, 10);
}
else{
return -1;
}
}
else if(!stop_parsing && !strcmp(argv[i], "--")){
stop_parsing = true;
}
else{
//add current token to line
current_line = realloc(current_line, current_offset + strlen(argv[i]) + 2);
if(current_offset != 0){
current_line[current_offset]=' ';
current_offset++;
}
strncpy(current_line + current_offset, argv[i], strlen(argv[i]) + 1);
current_offset += strlen(argv[i]);
}
}
if(!current_line){
fprintf(stderr, "No text given\n");
return -1;
}
//convert escaped characters
c = 0;
for(i = 0; current_line[i]; i++){
if(current_line[i] == '\\'){
switch(current_line[i + 1]){
case 'n':
current_line[c] = '\n';
break;
default:
current_line[c] = current_line[i + 1];
}
i++;
}
else{
current_line[c] = current_line[i];
}
c++;
}
current_line[c] = 0;
//split into lines
c = 0;
for(i = 0; current_line[i]; i++){
if(current_line[i] == '\n'){
current_line[i] = 0;
if(line_store(&(opts->lines), current_line + c) < 0){
fprintf(stderr, "Failed to store line\n");
free(current_line);
return -1;
}
c = i + 1;
}
}
if(line_store(&(opts->lines), current_line + c) < 0){
fprintf(stderr, "Failed to store line\n");
free(current_line);
return -1;
}
free(current_line);
return 0;
}
FcPattern* match_font(char* fontspec){
FcPattern* font_pattern = NULL;
FcPattern* match = NULL;
FcResult fc_result = 0;
font_pattern = FcNameParse((FcChar8*)fontspec);
if(!font_pattern){
return NULL;
}
FcDefaultSubstitute(font_pattern);
if(FcConfigSubstitute(NULL, font_pattern, FcMatchFont)){
match = FcFontMatch(NULL, font_pattern, &fc_result);
FcPatternDestroy(font_pattern);
switch(fc_result){
case FcResultMatch:
return match;
case FcResultNoMatch:
fprintf(stderr, "No Match: %s\n", FcNameUnparse(font_pattern));
break;
case FcResultTypeMismatch:
fprintf(stderr, "Type Mismatch: %s\n", FcNameUnparse(match));
return match;
case FcResultNoId:
fprintf(stderr, "No ID: %s\n", FcNameUnparse(match));
break;
case FcResultOutOfMemory:
fprintf(stderr, "FontConfig error: out of memory\n");
return NULL;
}
}
fprintf(stderr, "Failed to match font to config\n");
return NULL;
}
bool load_font(FT_Library ft, FcPattern* pattern, unsigned char_height, FT_Face* face){
char* font_file = NULL;
if(!pattern){
fprintf(stderr, "No pattern provided\n");
return false;
}
switch(FcPatternGetString(pattern, FC_FILE, 0, (FcChar8**)&font_file)){
case FcResultMatch:
//fprintf(stderr, "Font file %s\n", font_file);
break;
default:
fprintf(stderr, "Failed to find font location\n");
FcPatternDestroy(pattern);
return false;
}
switch(FT_New_Face(ft, font_file, 0, face)){
case FT_Err_Unknown_File_Format:
fprintf(stderr, "Unknown font file format\n");
FcPatternDestroy(pattern);
return false;
case 0:
FcPatternDestroy(pattern);
break;
default:
fprintf(stderr, "Unknown freetype error\n");
FcPatternDestroy(pattern);
return false;
}
//fprintf(stderr, "Setting glyph sizes to %d pixels\n", char_height);
if(FT_Set_Pixel_Sizes(*face, 0, char_height)){
fprintf(stderr, "Failed to set glyph size %d - font might not offer that size or might not be scalable\n", char_height);
return false;
}
return true;
}
bool load_glyphs(char** lines, FT_Face font_face, FT_BitmapGlyph** glyphs, FT_Glyph_Metrics** metrics){
//render all strings into glyph arrays
size_t i, c;
for(i = 0; i < stored_lines(lines); i++){
for(c = 0; c < strlen(lines[i]); c++){
//get glyph
if(FT_Load_Glyph(font_face, FT_Get_Char_Index(font_face, lines[i][c]), FT_LOAD_DEFAULT)){
fprintf(stderr, "Font has no glyph for %c, aborting\n", lines[i][c]);
return false;
}
//copy glyph to array
if(FT_Get_Glyph(font_face->glyph, (FT_Glyph*)(glyphs[i]) + c)){
fprintf(stderr, "Failed to copy glyph, aborting\n");
return false;
}
//calculate base offset from metrics because extracted glyphs do not carry them
metrics[i][c] = font_face->glyph->metrics;
//render to bitmap
if(FT_Glyph_To_Bitmap((FT_Glyph*)(glyphs[i]) + c, FT_RENDER_MODE_MONO, 0, 1)){
fprintf(stderr, "Failed to render glyph, aborting\n");
return false;
}
//test pixel format
if(glyphs[i][c]->bitmap.pixel_mode != FT_PIXEL_MODE_MONO){
fprintf(stderr, "Got unsupported pixel format\n");
return false;
}
}
}
return true;
}
void render(char** lines, FT_Face font_face, FT_BitmapGlyph** glyphs, FT_Glyph_Metrics** glyph_metrics, unsigned line_height, unsigned extra_filler){
ssize_t i, c;
unsigned baseline = abs(font_face->size->metrics.descender / 64);
bool done_rendering;
int* current_character = calloc(stored_lines(lines), sizeof(int));
int* current_rasterline = calloc(stored_lines(lines), sizeof(int));
if(!current_character || !current_rasterline){
fprintf(stderr, "Failed to allocate memory");
return;
}
//TODO kerning (see http://www.freetype.org/freetype2/docs/tutorial/step2.html)
//actual rendering portion
do{
//process one rasterline
done_rendering = true;
//start at the lowest line and work upwards
for(i = stored_lines(lines) - 1; i >= 0; i--){
//line already ended, fill with zeroes
if(current_character[i] >= strlen(lines[i])){
for(c = 0; c < line_height; c++){
fputc('0', stdout);
}
continue;
}
done_rendering = false;
FT_BitmapGlyph current = glyphs[i][current_character[i]];
unsigned baseline_offset = max(0, baseline - (glyph_metrics[i][current_character[i]].height - glyph_metrics[i][current_character[i]].horiBearingY) / 64);
uint8_t mask = 1 << (7 - (current_rasterline[i] % 8));
//fprintf(stderr, "line %d of char %d of line %d, mask %2X, offset %d, rows %d, width %d, pitch %d\n", current_rasterline[i], current_character[i], i, mask, current_rasterline[i]/8, current->bitmap.rows, current->bitmap.width, current->bitmap.pitch);
if(current_rasterline[i] >= current->bitmap.width){
if(current_rasterline[i] >= (glyph_metrics[i][current_character[i]].horiAdvance / 64) && current_character[i] < strlen(lines[i])){
//character done, advance
current_character[i]++;
current_rasterline[i] = 0;
}
else{
//bitmap smaller than advance, print empty line but do not advance
current_rasterline[i]++;
}
//print separator line
for(c = 0; c < line_height; c++){
fputc('0', stdout);
}
continue;
}
//print bottom offset
for(c = 0; c < baseline_offset; c++){
fputc('0', stdout);
}
//print pixels
for(c = current->bitmap.rows - 1; c >= 0; c--){
uint8_t cell_index = (c * current->bitmap.pitch) + (current_rasterline[i] / 8);
uint8_t cell_value = current->bitmap.buffer[cell_index];
//fprintf(stderr, "\nTesting cell %d with value %2X -> %2X -> ", cell_index, cell_value, cell_value&mask);
fputc(((cell_value & mask) > 0) ? '1' : '0', stdout);
}
//print filler
for(c = current->bitmap.rows + baseline_offset; c < line_height; c++){
fputc('0', stdout);
}
current_rasterline[i]++;
}
for(c = 0; c < extra_filler; c++){
fputc('0', stdout);
}
fputc('\n', stdout);
} while (!done_rendering);
free(current_character);
free(current_rasterline);
}
int main(int argc, char** argv){
OPTIONS opts = {
.fontspec = "FreeSerif",
.lines = NULL,
.width = 64
};
size_t i, c;
FT_Library ft_handle;
FT_BitmapGlyph** glyphs = NULL;
FT_Glyph_Metrics** glyph_metrics = NULL;
FT_Face font_face;
unsigned line_height = 64, extra_filler;
if(argc < 2){
exit(usage(argv[0]));
}
if(FT_Init_FreeType(&ft_handle)){
fprintf(stderr, "Failed to initialize FreeType\n");
return EXIT_FAILURE;
}
if(!FcInit()){
fprintf(stderr, "Failed to initialize FontConfig\n");
return EXIT_FAILURE;
}
//args_parse
if(args_parse(&opts, argc - 1, argv + 1) < 0){
fprintf(stderr, "Failed to parse arguments\n");
exit(usage(argv[0]));
}
//sanity check
if(!opts.lines || !opts.lines[0] || opts.width == 0){
fprintf(stderr, "Invalid invocation (%zu lines, width %d)\n", stored_lines(opts.lines), opts.width);
exit(usage(argv[0]));
}
//calculate required sizes
line_height = opts.width / stored_lines(opts.lines);
extra_filler = opts.width - (line_height * stored_lines(opts.lines));
//allocate glyph buffer
glyphs = calloc(stored_lines(opts.lines), sizeof(FT_BitmapGlyph*));
glyph_metrics = calloc(stored_lines(opts.lines), sizeof(FT_Glyph_Metrics*));
if(!glyphs || !glyph_metrics){
fprintf(stderr, "Failed to allocate memory\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < stored_lines(opts.lines); i++){
glyphs[i] = calloc(strlen(opts.lines[i]), sizeof(FT_BitmapGlyph));
glyph_metrics[i] = calloc(strlen(opts.lines[i]), sizeof(FT_Glyph_Metrics));
if(!glyphs[i] || !glyph_metrics[i]){
fprintf(stderr, "Failed to allocate memory for glyph array\n");
exit(EXIT_FAILURE);
}
}
//load the font & render
if(load_font(ft_handle, match_font(opts.fontspec), line_height, &font_face)
&& load_glyphs(opts.lines, font_face, glyphs, glyph_metrics)){
//baseline of the font is the absolute of the lowest descender
render(opts.lines, font_face, glyphs, glyph_metrics, line_height, extra_filler);
}
//clean up
for(i = 0; opts.lines[i]; i++){
for(c = 0; c < strlen(opts.lines[i]); c++){
FT_Done_Glyph((FT_Glyph)(glyphs[i][c]));
}
free(glyph_metrics[i]);
free(glyphs[i]);
free(opts.lines[i]);
}
free(glyphs);
free(glyph_metrics);
free(opts.lines);
FT_Done_Face(font_face);
FT_Done_FreeType(ft_handle);
FcFini();
return EXIT_SUCCESS;
}