-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttf2dumbfont.c
127 lines (120 loc) · 3.79 KB
/
ttf2dumbfont.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include "dumbfont.h"
#define PROGNAME "ttf2dumbfont"
#define NCODEPOINT 256
#define MAXIMAGEBYTESIZE (64 * 64 / 8)
#define UNISIGSIZE 32
static unsigned char pixels[NCODEPOINT * MAXIMAGEBYTESIZE];
static void
usage(void)
{
fprintf(stderr, "usage: %s %s\n", PROGNAME, "<ttf-file> 8|16|32|64");
exit(1);
}
int
main(int argc, char **argv)
{
static unsigned char imagebytes[MAXIMAGEBYTESIZE];
const char *ttf_file;
const unsigned char *unisig;
FT_Library library;
FT_Face face;
FT_GlyphSlot glyph;
FT_Bitmap *bitmap;
size_t imagebytesize;
int side, baseline, codepoint, yphys, xphys, y, x;
unsigned int ibyte, ibit;
int bestbaseline, leastmissing;
binary_stdout();
if (argc != 3) {
usage();
}
ttf_file = argv[1];
switch ((side = atoi(argv[2]))) {
case 8:
unisig = unisig8;
break;
case 16:
unisig = unisig16;
break;
case 32:
unisig = unisig32;
break;
case 64:
unisig = unisig64;
break;
default:
usage();
}
imagebytesize = side * side / 8;
if (FT_Init_FreeType(&library)) {
die("FT_Init_FreeType");
}
if (FT_New_Face(library, ttf_file, 0, &face)) {
die("FT_New_Face");
}
if (FT_Set_Pixel_Sizes(face, side, side)) {
die("FT_Set_Pixel_Sizes");
}
bestbaseline = 0;
leastmissing = 65535;
for (baseline = side - 1; baseline >= 0; baseline--) {
int missingpixels = 0;
for (codepoint = 0; codepoint < NCODEPOINT; codepoint++) {
if (FT_Load_Char(face, codepoint, FT_LOAD_RENDER)) {
die("FT_Load_Char");
}
glyph = face->glyph;
bitmap = &glyph->bitmap;
for (yphys = 0; yphys < (int)bitmap->rows; yphys++) {
for (xphys = 0; xphys < (int)bitmap->width; xphys++) {
if (bitmap->buffer[yphys * bitmap->pitch + xphys]) {
x = xphys + (int)glyph->bitmap_left;
y = yphys + side - baseline - (int)glyph->bitmap_top;
if (!((x >= 0) && (x < side) && (y >= 0) &&
(y < side))) {
missingpixels++;
}
}
}
}
}
if (leastmissing > missingpixels) {
leastmissing = missingpixels;
bestbaseline = baseline;
}
}
baseline = bestbaseline;
fprintf(stderr, "automatically selected baseline %d\n", baseline);
for (codepoint = 0; codepoint < NCODEPOINT; codepoint++) {
if (FT_Load_Char(face, codepoint, FT_LOAD_RENDER)) {
die("FT_Load_Char");
}
glyph = face->glyph;
bitmap = &glyph->bitmap;
memset(imagebytes, 0, sizeof(imagebytes));
for (yphys = 0; yphys < (int)bitmap->rows; yphys++) {
for (xphys = 0; xphys < (int)bitmap->width; xphys++) {
if (bitmap->buffer[yphys * bitmap->pitch + xphys]) {
x = xphys + (int)glyph->bitmap_left;
y = yphys + side - baseline - (int)glyph->bitmap_top;
if ((x >= 0) && (x < side) && (y >= 0) && (y < side)) {
ibyte = y * (side / 8) + (x / 8);
ibit = x % 8;
imagebytes[ibyte] |= 1 << ibit;
}
}
}
}
memcpy(pixels + imagebytesize * codepoint, imagebytes, imagebytesize);
}
if ((1 != fwrite(unisig, UNISIGSIZE, 1, stdout)) ||
(1 != fwrite(pixels, NCODEPOINT * imagebytesize, 1, stdout))) {
die("cannot write to stdout");
}
return 0;
}