summaryrefslogtreecommitdiff
path: root/xform.c
blob: 19001cd25e3b594c8bfd8db3c7cc80ad80212860 (plain)
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
/*
 * Trivial ASCII to C source transformation tool
 * Public domain
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define H_MAX 32
#define W_MAX 32

int
main (int argc, char *argv[])
{
	if (argc != 3)
	{
		fprintf (stderr, "Usage: %s font_height font_width < input\n", argv[0]);
		exit (1);
	}

	char *p;
	int font_height = strtol (argv[1], &p, 10);
	if (*p || p == argv[1] || font_height <= 0 || font_height > H_MAX)
	{
		fprintf (stderr, "invalid font height\n");
		exit (1);
	}

	int bit_width = strtol (argv[2], &p, 10);
	if (*p || p == argv[2] || bit_width   <= 0 || bit_width   > W_MAX)
	{
		fprintf (stderr, "invalid font width\n");
		exit (1);
	}

	// Width in pixels to width in bits, rounded up to bytes
	bit_width = (bit_width + 7) / 8 * 8;

	int c, irow = 0, icol = 0, charw = 0;
	char row_data[H_MAX * W_MAX] = { 0 };

	printf ("struct\n");
	printf ("{\n");
	printf ("\tint width;\n");
	printf ("\tint advance;\n");
	printf ("\tint rows[%d];\n", font_height);
	printf ("}\n");
	printf ("font_data[] =\n");
	printf ("{\n");

	while ((c = getchar ()) != EOF)
	{
		if (c == '\n')
		{
			if (irow == 0)
			{
				// We expect a new character definition
				if (icol != 0)
				{
					charw = icol;

					icol = 0;
					irow++;
				}
			}
			else if (irow < font_height)
			{
				// Just checking on consistency
				if (icol != charw)
				{
					fprintf (stderr, "wrong line width\n");
					exit (1);
				}

				icol = 0;
				irow++;
			}
			else
			{
				// Flushing the character after a newline
				if (icol != 0)
				{
					fprintf (stderr, "character too high\n");
					exit (1);
				}

				// Character width, horizontal advance
				printf ("\t{ %2d, %2d, { ", charw, charw + 1);

				int y, x;
				for (y = 0; ; )
				{
					unsigned bits = 0;
					for (x = 0; x < charw; x++)
						bits = (bits << 1) | row_data[y * W_MAX + x];
					if (charw < bit_width)
						bits <<= (bit_width - charw);

					printf ("0x%0*x", (bit_width + 3) / 4, bits);
					if (++y != font_height)
						printf (", ");
					else
						break;
				}

				printf (" } },\n");

				memset (row_data, 0, sizeof row_data);
				icol = irow = charw = 0;
			}
		}
		else if (c == '#')
		{
			// Comments
			while ((c = getchar ()) != '\n' && c != EOF)
				;
			ungetc ('\n', stdin);
		}
		else
		{
			if (icol == W_MAX)
			{
				fprintf (stderr, "character too wide\n");
				exit (1);
			}

			if (c != '0' && c != '1')
			{
				fprintf (stderr, "invalid input '%c'\n", c);
				exit (1);
			}
			row_data[irow * W_MAX + icol++] = 1 - (c - '0');
		}
	}

	if (icol != 0)
	{
		fprintf (stderr, "unexpected EOF\n");
		exit (1);
	}

	printf ("};\n");
	return 0;
}