main.c revision 65e01fca
1#include <stdio.h>
2#include <ctype.h>
3
4#include <ft2build.h>
5#include FT_FREETYPE_H
6
7char cvr[] = " .-+oaOX";
8FT_Library library;
9FT_Face face;
10int baseline, above = 0, below = 0, advance = 0;
11
12int push_size(int);
13
14int
15push_size(int letter)
16{
17	int glyph_index, error;
18	int new_above, new_below, new_advance;
19
20	glyph_index = FT_Get_Char_Index(face, letter);
21	printf("idx: %d\n", glyph_index);
22	error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
23	if (error) {
24		printf("wtf?!\n");
25		return -1;
26	}
27	FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
28	printf("%d x %d\n", face->glyph->bitmap.width, face->glyph->bitmap.rows);
29	printf("offset: %d %d\n", face->glyph->bitmap_left, face->glyph->bitmap_top);
30	new_advance = (int)(face->glyph->advance.x >> 6);
31	printf("advance: %d\n", new_advance);
32	new_above = face->glyph->bitmap_top;
33	new_below = face->glyph->bitmap.rows - face->glyph->bitmap_top;
34	if (new_above > above) above = new_above;
35	if (new_below > below) below = new_below;
36	if (new_advance > advance) advance = new_advance;
37	return 0;
38}
39
40int
41main(int argc, char *argv[])
42{
43	int error, glyph_index;
44	int x, y, idx, width_in_bytes, height = 22, cell_height;
45	int width, datalen, didx, i, start, end;
46	FILE *output;
47	uint8_t *fontdata;
48	char fontname[128], filename[128];
49
50	if (argc != 3) {
51		printf("usage: ttf2wsfont some_font.ttf height\n");
52		return 0;
53	}
54
55	sscanf(argv[2], "%d", &height);
56
57	error = FT_Init_FreeType( &library );
58	if (error) {
59		printf("Failed to initialize freefont2\n");
60		return -1;
61	}
62	error = FT_New_Face(library, argv[1], 0, &face );
63	if ( error == FT_Err_Unknown_File_Format ) {
64		printf("unsupported font format\n");
65		return -1;
66	}
67	error = FT_Set_Pixel_Sizes(face, /* handle to face object */
68				   0,    /* pixel_width */
69				   height - (height / 10) ); /* pixel_height */
70	if (error) {
71		printf("couldn't set character cell size\n");
72	}
73
74	push_size('W');
75	push_size('g');
76	push_size(192);
77	printf("above: %d below: %d advance: %d\n", above, below, advance);
78	width = advance;
79	baseline = above;
80	cell_height = above + below;
81	datalen = 256 * width * cell_height;
82	fontdata = malloc(datalen);
83
84
85	for (i = 0; i < 256; i++) {
86		glyph_index = FT_Get_Char_Index(face, i);
87		FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
88		FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
89		width_in_bytes =  face->glyph->bitmap.width;
90		start = baseline - face->glyph->bitmap_top;
91		end = start + face->glyph->bitmap.rows;
92		if (end > cell_height)
93			end = cell_height;
94		idx = 0;
95		if (start < 0) {
96			idx += (0 - start) * width_in_bytes;
97			start = 0;
98		}
99		didx = i * width * cell_height + /* character cell */
100		       start * width + /* pixels above baseline */
101		       face->glyph->bitmap_left; /* pixels left from border */
102		memset(&fontdata[i * width * cell_height], 0, width * cell_height);
103		for (y = start; y < end; y++) {
104			for (x = 0; x < width_in_bytes; x++) {
105				fontdata[didx + x] = face->glyph->bitmap.buffer[idx + x];
106			}
107			idx += width_in_bytes;
108			didx += width;
109		}
110	}
111
112	/* now output as a header file */
113	snprintf(fontname, 128, "%s_%dx%d", face->family_name, width, cell_height);
114	for (i = 0; i < strlen(fontname); i++) {
115		if (isblank(fontname[i]))
116			fontname[i]='_';
117	}
118	snprintf(filename, 128, "%s.h", fontname);
119	if ((output = fopen(filename, "w")) == NULL) {
120		fprintf(stderr, "Can't open output file %s\n", filename);
121		return -1;
122	}
123	fprintf(output, "static u_char %s_data[];\n", fontname);
124	fprintf(output, "\n");
125	fprintf(output, "static struct wsdisplay_font %s = {\n", fontname);
126	fprintf(output, "\t\"%s\",\t\t\t/* typeface name */\n", face->family_name);
127	fprintf(output, "\t0,\t\t\t\t/* firstchar */\n");
128	fprintf(output, "\t255 - 0 + 1,\t\t\t/* numchar */\n");
129	fprintf(output, "\tWSDISPLAY_FONTENC_ISO,\t\t/* encoding */\n");
130	fprintf(output, "\t%d,\t\t\t\t/* width */\n", width);
131	fprintf(output, "\t%d,\t\t\t\t/* height */\n", cell_height);
132	fprintf(output, "\t%d,\t\t\t\t/* stride */\n", width);
133	fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* bit order */\n");
134	fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* byte order */\n");
135	fprintf(output, "\t%s_data\t\t/* data */\n", fontname);
136	fprintf(output, "};\n\n");
137	fprintf(output, "static u_char %s_data[] = {\n", fontname);
138	for (i = 0; i < 256; i++) {
139		fprintf(output, "\t/* %d */\n", i);
140		idx = i * width * cell_height;
141		for (y = 0; y < cell_height; y++) {
142			for (x = 0; x < width; x++) {
143				fprintf(output, "0x%02x, ",fontdata[idx + x]);
144			}
145			fprintf(output, "/* ");
146			for (x = 0; x < width; x++) {
147				fprintf(output, "%c",cvr[fontdata[idx + x] >> 5]);
148			}
149			fprintf(output, " */\n");
150
151			idx += width;
152		}
153	}
154	fprintf(output, "};\n");
155	fclose(output);
156	free(fontdata);
157	FT_Done_Face(face);
158	FT_Done_FreeType(library);
159}
160