-
Notifications
You must be signed in to change notification settings - Fork 54
/
ttf2ugui.c
547 lines (437 loc) · 13.2 KB
/
ttf2ugui.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* Copyright (c) 2015, Ari Suutari <ari@stonepile.fi>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* 19.10.2017 jkpublic@kartech.biz - Added support for 8BPP fonts ( anti alliased)
*
*/
#include <ft2build.h>
#include <getopt.h>
#include <stdio.h>
#include FT_FREETYPE_H
#define SCREEN_WIDTH 132
#define SCREEN_HEIGHT 40
#include "ugui.h"
static UG_GUI gui;
static float fontSize = 0;
static int dpi = 0;
static int bpp = 1;
static int minChar = 32;
static int maxChar = 126;
/*
* "draw" a pixel using ansi escape sequences.
* Used for printing a ascii art sample of font.
*/
static void drawPixel(UG_S16 x, UG_S16 y, UG_COLOR col) {
printf("\033[%d;%dH", y + 1, x + 1);
if (col == C_WHITE)
printf("\x1B[0m ");
else {
if (col == C_BLACK) {
printf("\x1B[0m*");
} else {
printf("\x1B[34m*");
}
}
fflush(stdout);
}
/*
* Convert unicode to UTF-8 string
*/
int utf8_encode(char* out, uint32_t utf) {
if (utf <= 0x7F) {
// Plain ASCII
out[0] = (char) utf;
out[1] = 0;
return 1;
} else if (utf <= 0x07FF) {
// 2-byte unicode
out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
out[2] = 0;
return 2;
} else if (utf <= 0xFFFF) {
// 3-byte unicode
out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
out[3] = 0;
return 3;
} else if (utf <= 0x10FFFF) {
// 4-byte unicode
out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
out[4] = 0;
return 4;
} else {
// error - use replacement character
out[0] = (char) 0xEF;
out[1] = (char) 0xBF;
out[2] = (char) 0xBD;
out[3] = 0;
return 0;
}
}
static int max(int a, int b) {
if (a > b)
return a;
return b;
}
/*
* Output C-language code that can be used to include
* converted font into uGUI application.
*/
static void dumpFont(const UG_FONT* font, const char* fontFile, float fontSize, int bitsPerPixel) {
int bytesPerChar;
int ch;
int current;
int b;
char fontName[80];
char fileNameBuf[80];
const char* baseName;
char outFileName[80];
char* ptr;
FILE* out;
/*
* Generate name for font by stripping path and suffix from filename.
*/
baseName = fontFile;
ptr = strrchr(baseName, '/');
if (ptr)
baseName = ptr + 1;
strcpy(fileNameBuf, baseName);
baseName = fileNameBuf;
ptr = strchr(baseName, '.');
if (ptr)
*ptr = '\0';
sprintf(fontName, "%s_%dX%d", baseName, font->char_width, font->char_height);
sprintf(outFileName, "%s_%dX%d.c", baseName, font->char_width, font->char_height);
out = fopen(outFileName, "w");
if (!out) {
perror(outFileName);
exit(2);
}
/*
* First output character bitmaps.
*/
switch (bitsPerPixel) {
case 1: {
// Round up to full bytes
bytesPerChar = font->char_height * ((font->char_width + 7) / 8);
} break;
case 8: {
bytesPerChar = font->char_height * font->char_width;
} break;
}
fprintf(out, "// Converted from %s\n", fontFile);
fprintf(out, "// --size %d\n", (int) fontSize);
if (dpi > 0)
fprintf(out, "// --dpi %d\n", dpi);
fprintf(out, "// --bpp %d\n", (int) bitsPerPixel);
fprintf(out, "// For copyright, see original font file.\n");
fprintf(out, "\n#include \"ugui.h\"\n\n");
fprintf(out, "static __UG_FONT_DATA unsigned char fontBits_%s[%d][%d] = {\n", fontName,
font->end_char - font->start_char + 1, bytesPerChar);
fprintf(out, "%*c// Hex Dec Char\n", (bytesPerChar * 5) - 1 + 7, ' ');
current = 0;
for (ch = font->start_char; ch <= font->end_char; ch++) {
fprintf(out, " {");
for (b = 0; b < bytesPerChar; b++) {
if (b)
fprintf(out, ",");
fprintf(out, "0x%02X", font->p[current]);
++current;
}
fprintf(out, " }");
if (ch <= font->end_char - 1)
fprintf(out, ",");
else
fprintf(out, " ");
char utf8[5];
utf8_encode(utf8, ch);
fprintf(out, " // 0x%-4X %-4d '%s'\n", ch, ch, utf8);
}
fprintf(out, "};\n");
/*
* Next output character widths.
*/
fprintf(out, "static const UG_U8 fontWidths_%s[] = {\n", fontName);
for (ch = font->start_char; ch <= font->end_char; ch++) {
if (ch != font->start_char)
fprintf(out, ",");
fprintf(out, "%d", font->widths[ch - font->start_char]);
}
fprintf(out, "};\n");
/*
* Last, output UG_FONT structure.
*/
fprintf(out,
"const UG_FONT font_%s = { (unsigned char*)fontBits_%s, FONT_TYPE_%dBPP, %d, %d, %d, %d, fontWidths_%s };\n",
fontName, fontName, bitsPerPixel, font->char_width, font->char_height, font->start_char, font->end_char,
fontName);
fclose(out);
sprintf(outFileName, "%s_%dX%d.h", baseName, font->char_width, font->char_height);
out = fopen(outFileName, "w");
if (!out) {
perror(outFileName);
exit(2);
}
/*
* Output extern declaration to header file.
*/
fprintf(out, "extern const UG_FONT font_%s;\n", fontName);
fclose(out);
}
static UG_FONT newFont;
static UG_FONT* convertFont(const char* font, int dpi, float fontSize, int bitsPerPixel) {
int error;
FT_Face face;
FT_Library library;
if (bitsPerPixel != 1 && bitsPerPixel != 8) {
fprintf(stderr, "Bits per pixel must be 1 or 8, not %d!!\n", bitsPerPixel);
exit(1);
}
/*
* Initialize freetype library, load the font
* and set output character size.
*/
error = FT_Init_FreeType(&library);
if (error) {
fprintf(stderr, "ft init err %d\n", error);
exit(1);
}
error = FT_New_Face(library, font, 0, &face);
if (error) {
fprintf(stderr, "ew faceerr %d\n", error);
exit(1);
}
/*
* If DPI is not given, use pixes to specify the size.
*/
if (dpi > 0)
error = FT_Set_Char_Size(face, 0, fontSize * 64, dpi, dpi);
else
error = FT_Set_Pixel_Sizes(face, 0, fontSize);
if (error) {
fprintf(stderr, "set pixel sizes err %d\n", error);
exit(1);
}
int i, j;
int coverage;
int ch;
int maxWidth = 0;
int maxHeight = 0;
int maxAscent = 0;
int maxDescent = 0;
int bytesPerChar;
int bytesPerRow;
/*
* First found out how big character bitmap is needed. Every character
* must fit into it so that we can obtain correct character positioning.
*/
int flags = FT_LOAD_RENDER;
if (bitsPerPixel == 1) {
flags = flags | FT_LOAD_TARGET_MONO;
}
for (ch = minChar; ch <= maxChar; ch++) {
int ascent;
int descent;
error = FT_Load_Char(face, ch, flags);
if (error) {
fprintf(stderr, "load char err %d\n", error);
exit(1);
}
descent = max(0, face->glyph->bitmap.rows - face->glyph->bitmap_top);
ascent = max(0, max(face->glyph->bitmap_top, face->glyph->bitmap.rows) - descent);
if (descent > maxDescent)
maxDescent = descent;
if (ascent > maxAscent)
maxAscent = ascent;
if (face->glyph->bitmap.width > maxWidth)
maxWidth = face->glyph->bitmap.width;
}
maxHeight = maxAscent + maxDescent;
switch (bitsPerPixel) {
case 1: {
// Round up to full bytes
bytesPerRow = (maxWidth + 7) / 8;
} break;
case 8: {
bytesPerRow = maxWidth;
} break;
}
bytesPerChar = bytesPerRow * maxHeight;
newFont.p = malloc(bytesPerChar * (maxChar - minChar + 1));
memset(newFont.p, '\0', bytesPerChar * (maxChar - minChar + 1));
switch (bitsPerPixel) {
case 1:
newFont.font_type = FONT_TYPE_1BPP;
break;
case 8:
newFont.font_type = FONT_TYPE_8BPP;
break;
}
newFont.char_width = maxWidth;
newFont.char_height = maxHeight;
newFont.start_char = minChar;
newFont.end_char = maxChar;
newFont.widths = malloc(maxChar - minChar + 1);
/*
* Render each character.
*/
for (ch = minChar; ch <= maxChar; ch++) {
error = FT_Load_Char(face, ch, flags);
if (error) {
fprintf(stderr, "load char err %d\n", error);
exit(1);
}
for (i = 0; i < face->glyph->bitmap.rows; i++) {
for (j = 0; j < face->glyph->bitmap.width; j++) {
coverage = 0;
uint8_t* bits = (uint8_t*) face->glyph->bitmap.buffer;
if (bitsPerPixel == 8) {
coverage = bits[(i * face->glyph->bitmap.pitch + j)];
} else {
uint8_t b = bits[i * face->glyph->bitmap.pitch + (j / 8)];
if (b & (1 << (7 - (j % 8)))) {
coverage = 1;
}
}
/*
* Output character to correct position in bitmap
*/
int xpos, ypos, ind;
xpos = j + face->glyph->bitmap_left;
ypos = maxAscent + i - face->glyph->bitmap_top;
switch (bitsPerPixel) {
case 1: {
ind = ypos * bytesPerRow;
ind += xpos / 8;
if (coverage != 0)
newFont.p[((ch - minChar) * bytesPerChar) + ind] |= (1 << ((xpos % 8)));
} break;
case 8: {
ind = ypos * bytesPerRow;
ind += xpos;
newFont.p[((ch - minChar) * bytesPerChar) + ind] = coverage;
} break;
}
}
}
newFont.widths[ch - minChar] = maxWidth;
}
return &newFont;
}
/*
* Draw a simple sample of new font with uGUI.
*/
static void showFont(const UG_FONT* font, char* text) {
UG_Init(&gui, drawPixel, SCREEN_WIDTH, SCREEN_HEIGHT);
UG_FillScreen(C_WHITE);
UG_DrawFrame(0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, C_BLACK);
UG_FontSelect(font);
UG_SetBackcolor(C_WHITE);
UG_SetForecolor(C_BLACK);
UG_PutString(2, 2, text);
UG_DrawPixel(0, SCREEN_HEIGHT - 1, C_WHITE);
UG_Update();
printf("\n");
}
static int dump;
static char* fontFile = NULL;
static char* showText = NULL;
/* options descriptor */
static struct option longopts[] = {{"show", required_argument, NULL, 'a'},
{"dump", no_argument, &dump, 1},
{"dpi", required_argument, NULL, 'd'},
{"minchar", required_argument, NULL, 'z'},
{"maxchar", required_argument, NULL, 'e'},
{"size", required_argument, NULL, 's'},
{"font", required_argument, NULL, 'f'},
{"bpp", optional_argument, NULL, 'b'},
{NULL, 0, NULL, 0}};
static void usage() {
fprintf(stderr, "ttf2ugui {--show text|--dump} --font=fontfile [--dpi=displaydpi] --size=fontsize "
"[--bpp=bitsperpixel] [--minchar=charnumber] [--maxchar=charnumber]\n");
fprintf(stderr, "If --dpi is not given, font size is assumed to be pixels.\n");
fprintf(stderr, "Bits per pixel must be 1 or 8. Default is 1.\n");
fprintf(stderr,
"Char numbers use unicode or ascii encoding in decimal representation (ex. 'a' = 97 ), default is 32-126.\n");
}
int main(int argc, char** argv) {
int ch;
while ((ch = getopt_long(argc, argv, "", longopts, NULL)) != -1) {
switch (ch) {
case 'f':
fontFile = optarg;
break;
case 'a':
showText = optarg;
break;
case 's':
sscanf(optarg, "%f", &fontSize);
break;
case 'd':
dpi = atoi(optarg);
break;
case 'b':
bpp = atoi(optarg);
if ((bpp != 1) && (bpp != 8)) {
fprintf(stderr, "Bits per pixel must be 1 or 8. Default is 1.\n");
exit(1);
}
break;
case 'z':
minChar = atoi(optarg);
break;
case 'e':
maxChar = atoi(optarg);
break;
case 0:
break;
default:
usage();
exit(1);
}
}
argc -= optind;
argv += optind;
if ((!dump && showText == NULL) || fontFile == NULL || fontSize == 0) {
usage();
exit(1);
}
const UG_FONT* font;
font = convertFont(fontFile, dpi, fontSize, bpp);
if (showText)
showFont(font, showText);
if (dump)
dumpFont(font, fontFile, fontSize, bpp);
}