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>.
14 print "Usage: convbdf font.bdf > font.c\n";
20 $IMAGE_NIBBLES = $IMAGE_BITS/4;
28 $font =~ tr/a-zA-Z0-9_/_/cs;
30 print "/* Generated by convbdf on ", substr(`date`, 0, -1), ". */\n";
31 print "#include <gfx/font.h>\n\n";
33 open BDF, "<$file" || die;
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+)$/;
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";
52 print "/* Font character bitmap data. */\n";
53 print "static const PROGMEM uint8_t font_${font}_glyphs[] = {\n";
55 $ch_height = $font_ascent + $font_descent;
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+)/;
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++) {
83 for (my $i = 0; ; $i++) {
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] |=
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;
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";
113 if ($IMAGE_VERTICAL) {
115 for (my $k = 0; $k < int(($ch_height + $IMAGE_BITS - 1) / $IMAGE_BITS); $k++) {
116 for (my $x = 0; $x < $width; $x++) {
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 ? '*' : ' ';
124 printf "0x%02x, ", $v;
127 printf "/* $bitstring */\n";
131 else { # IMAGE_HORIZONTAL
134 print " +", ("-" x $ch_bits), "+\n";
135 for (my $i = 0; $i < $ch_height; $i++) {
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) ? "*" : " ";
144 print " +", ("-" x $ch_bits), "+ */\n";
146 for (my $i = 0; $i < $ch_height; $i++) {
147 for (my $k=0; $k<$ch_words; $k++) {
149 printf "0x%02x, ", $bm[$i][$k];
159 #print STDERR "Maximum character width=$maxwidth\n";
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++) {
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;
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++) {
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;
184 $font_offset = "${font}_offset";
185 $font_width = "${font}_width";
187 $font_offset = "NULL";
188 $font_width = "NULL";
191 $lastchar = $LAST_CHAR;
192 #$size = $lastchar - $firstchar + 1;
195 /* Font structure definition. */
196 EXTERN_CONST Font font_${font} =
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,