File: fontgen.c

package info (click to toggle)
zapping 0.10~cvs6-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,856 kB
  • ctags: 11,841
  • sloc: ansic: 111,154; asm: 11,770; sh: 9,812; xml: 2,742; makefile: 1,283; perl: 488
file content (198 lines) | stat: -rw-r--r-- 3,941 bytes parent folder | download | duplicates (8)
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
/*
 *  libzvbi
 *
 *  Copyright (C) 2004 Michael H. Schimek
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $Id: fontgen.c,v 1.5 2005/01/27 04:15:03 mschimek Exp $ */

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>

#include "misc.h"

#include "hamm.c" /* _vbi3_bit_reverse[] */

static char *			font_name;

static unsigned int		char_height;

uint8_t *			image;

static unsigned int		image_width;
static unsigned int		image_height;

static int
pbm_getc				(FILE *			fp)
{
	int c;
	
	c = getc (fp);

	if ('#' == c) {
		do c = getc (fp);
		while ('\n' != c && '\r' != c && EOF != c);
	}

	return c;
}

static void
pbm_getint			(FILE *			fp,
				 unsigned int *		val)
{
	int c;
	unsigned int t;

	do c = pbm_getc (fp);
	while (EOF != c && isspace (c));

	t = 0;

	while (isdigit (c)) {
		t = t * 10 + c - '0';
		c = pbm_getc (fp);
	}

	*val = t;

	if (EOF == c) {
		perror ("read error");
		exit (EXIT_FAILURE);
	}
}

static void
pbm_read			(void)
{
	FILE *fp;
	size_t n;
	size_t image_size;
	uint8_t buf[2];

	fp = stdin;

	n = fread (buf, sizeof (*buf), N_ELEMENTS (buf), fp);

	if (2 != n || 'P' != buf[0] || '4' != buf[1]) {
		fprintf (stderr, "source is not a .pbm file\n");
		exit (EXIT_FAILURE);
	}

	pbm_getint (fp, &image_width);
	pbm_getint (fp, &image_height);

	image_size = image_width * image_height / 8;

	image = vbi3_malloc (image_size);

	if (NULL == image) {
		fprintf (stderr, "out of memory\n");
		exit (EXIT_FAILURE);
	}

	n = fread (image, 1, image_size, fp);

	if (n < image_size) {
		perror ("read error or unexpected eof");
		exit (EXIT_FAILURE);
	}
}

static void
xbm_write			(void)
{
	FILE *fp;
	unsigned int row;
	unsigned int line;

	fp = stdout;

	fprintf (fp, "/* Generated file, do not edit */\n"
		     "#define %s_width %u\n"
		     "#define %s_height %u\n"
		     "static const uint8_t %s_bits [] = {\n  ",
		 font_name, image_width * image_height / char_height,
		 font_name, char_height,
		 font_name);

	assert (0 == (image_width % 8));

	/* Note this de-interleaves the font image
	   (puts all chars in row 0) */

	for (line = 0; line < char_height; ++line) {
		for (row = 0; row < image_height; row += char_height) {
			unsigned int x;
			uint8_t *p;
			
			p = (image
			     + row * image_width / 8
			     + line * image_width / 8);

			for (x = 0; x < image_width / 8; ++x) {
				fprintf (fp, "0x%02x,",
					 _vbi3_bit_reverse[p[x]]);

				if (7 == (x % 8))
					fputs ("\n  ", fp);
				else
					fputc (' ', fp);
			}
		}
	}

	fputs ("};\n", fp);
}

int
main				(int			argc,
				 char **		argv)
{
	if (4 != argc) {
		fprintf (stderr, "Usage: %s format font_name char_height "
				 "<pbm-file >output\n",
				 argv[0]);
		exit (EXIT_FAILURE);
	}

	font_name = argv[2];
	char_height = atoi (argv[3]);
	
	pbm_read ();

	assert (0 == (image_height % char_height));

	if (0 == strcmp (argv[1], "xbm")) {
		/* XBM image for built-in render functions (exp-gfx.c). */
		xbm_write ();
	} else if (0 == strcmp (argv[1], "bdf")) {
		/* BDF file for X11 rendering. */
		/* TODO (s/a contrib) */
	} else if (0 == strcmp (argv[1], "ttf")) {
		/* TTF file for TT rendering of
		   G1 Block Mosaics Set and
		   G3 Smooth Mosaics and Line Drawing Set. */
		/* TODO */
	}

	exit (EXIT_SUCCESS);

	return 0;
}