summaryrefslogtreecommitdiffstats
path: root/tools/bdf2c
blob: 6832e5ce8f943503f09e5c21ded93093ee8dcd31 (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
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
#! /usr/bin/perl -w
#
# Convert BDF files to incore MWCFONT structure 'C' source
# Copyright (c) 2002 by Greg Haerr <greg@censoft.com>
#
# from The Microwindows Project (http://microwindows.org)
#
# modified 09/13/02 correct output when no DEFAULT_CHAR, allow numeric font name
# modified 09/12/02 added -limit <max_encode_hex_value> option
# modified on 09/10/02 by G Haerr
#	- fixed DWIDTH 0 parsing
#	- don't limit font size to 0x7e characters
#	- changed offset data to unsigned long for large fonts
#	- don't generate width table if fixed-width
#	- added defaultchar to output
#	- added bits_size member for loadable fonts
# modified on 3/26/00 by G Haerr added ascent field, fixed $IMAGE_BITS
# modified on 2/10/00 by K Harris to accept any size input character
# modified by G Haerr from bdftobogl for 16 bit MWIMAGEBITS
# originally from BOGL - Ben's Own Graphics Library <pfaffben@debian.org>.

use POSIX;

$name = (reverse split /\//, $0)[0];
$limit_char = 65535;

while (defined $ARGV[0]) {
    my $arg = shift;

    if (($arg eq "-limit") && scalar(@ARGV) > 0) {
	$limit_char = hex shift;
    } elsif ($arg =~ "-.+") {
	print "$name: unknown option '$arg'\n";
	exit 1;
    } else {
	unshift(@ARGV, $arg);
	last;
    }
}

if ($#ARGV < 0) {
    print "Usage: $name [-limit <max_encode_hex_value>] font.bdf > font.c\n";
    exit 1;
}

$IMAGE_BITS = 16;
$IMAGE_NIBBLES = $IMAGE_BITS/4;
$IMAGE_MASK = 0xffff;

$file = $ARGV[0];

$font = $file;
$font =~ s/\.bdf//;
$font =~ tr/a-zA-Z0-9_/_/cs;

print "/* Generated by $name on ", substr(`date`, 0, -1), ". */\n";
print "#include \"font.h\"\n\n";

open BDF, "<$file" || die;
while (<BDF>) {
    chop;
    $pixel_size = $1 if /^PIXEL_SIZE (\d+)$/;
    $font_ascent = $1 if /^FONT_ASCENT (\d+)$/;
    $font_descent = $1 if /^FONT_DESCENT (\d+)$/;
    $font_name = $1 if /^FONT (.*)$/;
    $default_char = $1 if /^DEFAULT_CHAR (\d+)$/;
    ($fbbw, $fbbh, $fbbx, $fbby) = ($1, $2, $3, $4) if /^FONTBOUNDINGBOX (-?\d+) (-?\d+) (-?\d+) (-?\d+)/;
    
    last if /^CHARS /;
}

$font_width = $fbbw - $fbbx;
undef $fbbw, undef $fbbh, undef $fbbx, undef $fbby;

print "/* Font information:\n\n";
print "   name: $font_name\n";
print "   pixel size: $pixel_size\n" if defined $pixel_size;
print "   ascent: $font_ascent\n";
print "   descent: $font_descent\n";
print "*/\n\n";

print "/* Font character bitmap data. */\n";
print "static MWIMAGEBITS _${font}_bits[] = {\n";

$ch_height = $font_ascent + $font_descent;
$ofs = 0;
$maxwidth = 0;
$firstchar = -1;
$lastchar = -1;
while (<BDF>) {
    chop;
    undef $encoding, undef $width, undef $bbx, undef $bby, undef $bbw, undef $bbh if /^STARTCHAR /;
    $encoding = $1 if /^ENCODING (\d+)/;
	last if defined $encoding && $encoding > $limit_char;
    $width = $1 if /^DWIDTH (-?\d+)/;
    	$width = $font_width if defined $width && $width <= 0;
    ($bbw, $bbh, $bbx, $bby) = ($1, $2, $3, $4) if /^BBX (-?\d+) (-?\d+) (-?\d+) (-?\d+)/;

   if (/^BITMAP$/) {
		next if !defined $encoding;
		$firstchar = $encoding if $firstchar < 0;
		$lastchar = $encoding if $lastchar < $encoding;
		$encoding_tab[$encoding] = $ofs;
		$width -= $bbx, $bbx = 0 if $bbx < 0;
		$width[$encoding] = $width;
		$maxwidth = $width if $width > $maxwidth;
		$ch_words  = int (($width+$IMAGE_BITS-1)/$IMAGE_BITS);
		$ch_bits   = $ch_words*$IMAGE_BITS;
		for (my $i = 0; $i < $ch_height; $i++) {
			for (my $k = 0; $k < $ch_words; $k++) {
	    		$bm[$i][$k] = 0;
			}
		}
		for (my $i = 0; ; $i++) {
	    	$_ = <BDF>;
	    	chop;
	    	last if /^ENDCHAR$/;
		
		   @hexnibbles = split //,$_;
			for (my $k=0; $k<$ch_words; $k++) {
			   $ndx = $k*$IMAGE_NIBBLES;
				$padnibbles = @hexnibbles - $ndx;
				last if $padnibbles <= 0; # if bbx pushes bits into next word
												 # and no more bits from bdf file
				$padnibbles = 0 if $padnibbles >= $IMAGE_NIBBLES;
			   $value = hex join '',@hexnibbles[$ndx..($ndx+$IMAGE_NIBBLES-1-$padnibbles)];
	   		$value = $value << ($padnibbles*$IMAGE_NIBBLES);	
	   		$bm[$ch_height - $font_descent - $bby - $bbh + $i][$k] |= 
						$value >> ($bbx);	
				if ($bbx) {	# handle overflow into next image_word
	   			$bm[$ch_height - $font_descent - $bby - $bbh + $i][$k+1] =
						($value << ($IMAGE_BITS - $bbx)) & $IMAGE_MASK;	
				}
			}
		}
	
###		printf "\n/* Character %c (0x%02x):\n", $encoding, $encoding;
		printf "\n/* Character (0x%02x):\n", $encoding;
		print "   bbw=$bbw, bbh=$bbh, bbx=$bbx, bby=$bby, width=$width\n";
		print "   +", ("-" x $ch_bits), "+\n";
		for (my $i = 0; $i < $ch_height; $i++) {
			print "   |";
			for (my $k = 0; $k < $ch_words; $k++) {
	   		for (my $j = $IMAGE_BITS - 1; $j >= 0; $j--) {
					print $bm[$i][$k] & (1 << $j) ? "*" : " ";
	   		}
			}
	   	print "|\n";
		}
		print "   +", ("-" x $ch_bits), "+ */\n";
	
		for (my $i = 0; $i < $ch_height; $i++) {
			for ($k=0; $k<$ch_words; $k++) {
   			$ofs++;
	   		printf "0x%04x, ", $bm[$i][$k];
			}
	   	printf "\n";
		}
   }
}

print "};\n\n";

##print STDERR "Maximum character width=$maxwidth\n";

$default_char = $firstchar if !defined $default_char;

print "/* Character->glyph mapping. */\n";
print "static unsigned long _${font}_offset[] = {\n";
for (my $i = $firstchar; $i <= $lastchar; $i++) {
    my $char = $i;
    my $ofs = $encoding_tab[$i];
    $ofs = $encoding_tab[$default_char], $char = $default_char if !defined $ofs;
### printf "  $ofs,\t/* %c (0x%02x) */\n", $char, $i;
    printf "  $ofs,\t/* (0x%02x) */\n", $i;
}
print "};\n\n";

$gen_width_table = 0;
for (my $i = $firstchar; $i <= $lastchar; $i++) {
    my $char = $i;
    my $width = $width[$i];
    $width = $width[$default_char] if !defined $encoding_tab[$i];
    $gen_width_table = 1 if $width != $maxwidth
}

if ($gen_width_table) {
	print "/* Character width data. */\n";
	print "static unsigned char _${font}_width[] = {\n";
	for (my $i = $firstchar; $i <= $lastchar; $i++) {
	    my $char = $i;
	    my $width = $width[$i];
	    $width = $width[$default_char], $char = $default_char if !defined $encoding_tab[$i];
	### printf "  $width,\t/* %c (0x%02x) */\n", $char, $i;
	    printf "  $width,\t/* (0x%02x) */\n", $i;
	}
	print "};\n\n";
}

$size = $lastchar - $firstchar + 1;

print "/* Exported structure definition. */\n";
print "MWCFONT font_${font} = {\n";
print "  \"$font\",\n";
print "  $maxwidth,\n";
print "  $ch_height,\n";
print "  $font_ascent,\n";
print "  $firstchar,\n";
print "  $size,\n";
print "  _${font}_bits,\n";
print "  _${font}_offset,\n";
if ($gen_width_table) {
	print "  _${font}_width,\n";
} else { print "  0,  /* fixed width*/\n"; }
print "  $default_char,\n";
print "  sizeof(_${font}_bits)/sizeof(MWIMAGEBITS),\n";
print "};\n";