Use the same format for fonts and rasters.
[bertos.git] / fonts / convbdf
1 #! /usr/bin/perl -w
2 #
3 # Convert BDF files to nano-X font files
4 # modified by G Haerr from bdftobogl for 16 bit MWIMAGEBITS
5 # modified on 2/10/00 by K Harris to accept any size input character
6 # modified on 3/26/00 by G Haerr added ascent field, fixed $IMAGE_BITS
7 # modified on 2/15/06 by <bernie@develer.com> for DevLib.
8 # originally from BOGL - Ben's Own Graphics Library <pfaffben@debian.org>.
9 #
10
11 use POSIX;
12
13 if ($#ARGV < 0) {
14         print "Usage: convbdf font.bdf > font.c\n";
15         exit -1;
16 }
17
18 $LAST_CHAR = 0x7e;
19 $IMAGE_BITS = 8;
20 $IMAGE_NIBBLES = $IMAGE_BITS/4;
21 $IMAGE_MASK = 0xff;
22 $IMAGE_VERTICAL = 1;
23
24 $file = $ARGV[0];
25
26 $font = $file;
27 $font =~ s/\.bdf//;
28 $font =~ tr/a-zA-Z0-9_/_/cs;
29
30 print "/* Generated by convbdf on ", substr(`date`, 0, -1), ". */\n";
31 print "#include <gfx/font.h>\n\n";
32
33 open BDF, "<$file" || die;
34 while (<BDF>) {
35         chop;
36         $pixel_size = $1 if /^PIXEL_SIZE (\d+)$/;
37         $font_ascent = $1 if /^FONT_ASCENT (\d+)$/;
38         $font_descent = $1 if /^FONT_DESCENT (\d+)$/;
39         $font_name = $1 if /^FONT (.*)$/;
40         $default_char = $1 if /^DEFAULT_CHAR (\d+)$/;
41
42         last if /^CHARS /;
43 }
44
45 print "/* Font information:\n\n";
46 print "   name: $font_name\n";
47 print "   pixel size: $pixel_size\n";
48 print "   ascent: $font_ascent\n";
49 print "   descent: $font_descent\n";
50 print "*/\n\n";
51
52 print "/* Font character bitmap data. */\n";
53 print "static const PROGMEM uint8_t font_${font}_glyphs[] = {\n";
54
55 $ch_height = $font_ascent + $font_descent;
56 $ofs = 0;
57 $maxwidth = 0;
58 $proportional = 0;
59 $firstchar = -1;
60 while (<BDF>) {
61         chop;
62         undef $encoding, undef $width, undef $bbx, undef $bby, undef $bbw, undef $bbh if /^STARTCHAR /;
63         $encoding = $1 if /^ENCODING (\d+)/;
64         last if defined $encoding && $encoding > $LAST_CHAR;
65         $width = $1 if /^DWIDTH (-?\d+)/;
66         ($bbw, $bbh, $bbx, $bby) = ($1, $2, $3, $4) if /^BBX (-?\d+) (-?\d+) (-?\d+) (-?\d+)/;
67
68         if (/^BITMAP$/) {
69                 next if !defined $encoding;
70                 $firstchar = $encoding if $firstchar < 0;
71                 $encoding_tab[$encoding] = $ofs;
72                 $width -= $bbx, $bbx = 0 if $bbx < 0;
73                 $width[$encoding] = $width;
74                 $maxwidth != 0 and $width != $maxwidth and $proportional = 1;
75                 $maxwidth = $width if $width > $maxwidth;
76                 $ch_words  = int (($width+$IMAGE_BITS-1)/$IMAGE_BITS);
77                 $ch_bits   = $ch_words*$IMAGE_BITS;
78                 for (my $i = 0; $i < $ch_height; $i++) {
79                         for (my $k = 0; $k < $ch_words; $k++) {
80                                 $bm[$i][$k] = 0;
81                         }
82                 }
83                 for (my $i = 0; ; $i++) {
84                         $_ = <BDF>;
85                         chop;
86                         last if /^ENDCHAR$/;
87
88                         @hexnibbles = split //,$_;
89                         for (my $k=0; $k<$ch_words; $k++) {
90                                 $ndx = $k*$IMAGE_NIBBLES;
91                                 $padnibbles = @hexnibbles - $ndx;
92                                 # if bbx pushes bits into next word
93                                 # and no more bits from bdf file
94                                 last if $padnibbles <= 0;
95                                 $padnibbles = 0 if $padnibbles >= $IMAGE_NIBBLES;
96                                 $value = hex join '',@hexnibbles[$ndx..($ndx+$IMAGE_NIBBLES-1-$padnibbles)];
97                                 $value = $value << ($padnibbles*$IMAGE_NIBBLES);        
98                                 $bm[$ch_height - $font_descent - $bby - $bbh + $i][$k] |= 
99                                         $value >> ($bbx);       
100                                 if ($bbx) {     # handle overflow into next image_word
101                                         $bm[$ch_height - $font_descent - $bby - $bbh + $i][$k+1] =
102                                                 ($value << ($IMAGE_BITS - $bbx)) & $IMAGE_MASK; 
103                                 }
104                         }
105                 }
106
107                 # GLYPH PREVIEW
108 ###             printf "\n/* Character %c (0x%02x): ", $encoding, $encoding;
109                 printf "\n/* Character (0x%02x): ", $encoding;
110                 print "bbw=$bbw, bbh=$bbh, bbx=$bbx, bby=$bby, width=$width */\n";
111         
112                 # GLYPH DATA
113                 if ($IMAGE_VERTICAL) {
114                         my $bitstring = "";
115                         for (my $k = 0; $k < int(($ch_height + $IMAGE_BITS - 1) / $IMAGE_BITS); $k++) {
116                                 for (my $x = 0; $x < $width; $x++) {
117                                         my $v = 0;
118                                         for (my $y = 0; $y < $IMAGE_BITS && ($y + $k * $IMAGE_BITS < $ch_height); ++$y) {
119                                                 my $bit = ($bm[$k * $IMAGE_BITS + $y][int($x / $IMAGE_BITS)] & (1 << ($IMAGE_BITS - ($x % $IMAGE_BITS) - 1))) ? 1 : 0;
120                                                 $bitstring .= $bit ? '*' : ' ';
121                                                 $v |= $bit << $y;
122                                         }
123                                         $ofs++;
124                                         printf "0x%02x, ", $v;
125                                 }
126                                 # Preview
127                                 printf "/* $bitstring */\n";
128                         }
129                         
130                 }
131                 else { # IMAGE_HORIZONTAL
132
133                         # Preview
134                         print "   +", ("-" x $ch_bits), "+\n";
135                         for (my $i = 0; $i < $ch_height; $i++) {
136                                 print "   |";
137                                 for (my $k = 0; $k < $ch_words; $k++) {
138                                         for (my $j = $IMAGE_BITS - 1; $j >= 0; $j--) {
139                                                 print $bm[$i][$k] & (1 << $j) ? "*" : " ";
140                                         }
141                                 }
142                                 print "|\n";
143                         }
144                         print "   +", ("-" x $ch_bits), "+ */\n";
145
146                         for (my $i = 0; $i < $ch_height; $i++) {
147                                 for (my $k=0; $k<$ch_words; $k++) {
148                                         $ofs++;
149                                         printf "0x%02x, ", $bm[$i][$k];
150                                 }
151                                 printf "\n";
152                         }
153                 }
154    }
155 }
156
157 print "};\n\n";
158
159 #print STDERR "Maximum character width=$maxwidth\n";
160
161 if ($proportional) {
162         print "/* Character->glyph data. */\n";
163         print "static const PROGMEM uint16_t ${font}_offset[] = {\n";
164         for (my $i = $firstchar; $i <= $LAST_CHAR; $i++) {
165                 my $char = $i;
166                 my $ofs = $encoding_tab[$i];
167                 $ofs = $encoding_tab[$default_char], $char = $default_char if !defined $ofs;
168                 ### printf "  $ofs,\t/* %c (0x%02x) */\n", $char, $i;
169                 printf "  $ofs,\t/* (0x%02x) */\n", $i;
170         }
171         print "};\n\n";
172
173         print "/* Character width data. */\n";
174         print "static const PROGMEM uint8_t ${font}_width[] = {\n";
175         for (my $i = $firstchar; $i <= $LAST_CHAR; $i++) {
176                 my $char = $i;
177                 my $width = $width[$i];
178                 $width = $width[$default_char], $char = $default_char if !defined $encoding_tab[$i];
179                 ### printf "  $width,\t/* %c (0x%02x) */\n", $char, $i;
180                 printf "  $width,\t/* (0x%02x) */\n", $i;
181         }
182         print "};\n\n";
183
184         $font_offset = "${font}_offset";
185         $font_width = "${font}_width";
186 } else {
187         $font_offset = "NULL";
188         $font_width = "NULL";
189 }
190
191 $lastchar = $LAST_CHAR;
192 #$size = $lastchar - $firstchar + 1;
193
194 print <<EOF
195 /* Font structure definition. */
196 EXTERN_CONST Font font_${font} =
197 {
198         /* .glyph  = */ font_${font}_glyphs,
199         /* .name   =    "$font", */
200         /* .width  = */ $maxwidth,
201         /* .height = */ $ch_height,
202         /* .ascent =    $font_ascent, */
203         /* .first  = */ $firstchar,
204         /* .last   = */ $lastchar,
205         /* .offset = */ $font_offset,
206         /* .width  = */ $font_width,
207 };
208 EOF