1 |
673 |
markom |
#! /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 |
|
|
# originally from BOGL - Ben's Own Graphics Library .
|
8 |
|
|
#
|
9 |
|
|
|
10 |
|
|
use POSIX;
|
11 |
|
|
|
12 |
|
|
if ($#ARGV < 0) {
|
13 |
|
|
print "Usage: convbdf font.bdf > font.c\n";
|
14 |
|
|
exit -1;
|
15 |
|
|
}
|
16 |
|
|
|
17 |
|
|
$LAST_CHAR = 0x7e;
|
18 |
|
|
$IMAGE_BITS = 16;
|
19 |
|
|
$IMAGE_NIBBLES = $IMAGE_BITS/4;
|
20 |
|
|
$IMAGE_MASK = 0xffff;
|
21 |
|
|
|
22 |
|
|
$file = $ARGV[0];
|
23 |
|
|
|
24 |
|
|
$font = $file;
|
25 |
|
|
$font =~ s/\.bdf//;
|
26 |
|
|
$font =~ tr/a-zA-Z0-9_/_/cs;
|
27 |
|
|
|
28 |
|
|
print "/* Generated by convbdf on ", substr(`date`, 0, -1), ". */\n";
|
29 |
|
|
print "#include \"device.h\"\n\n";
|
30 |
|
|
|
31 |
|
|
open BDF, "<$file" || die;
|
32 |
|
|
while () {
|
33 |
|
|
chop;
|
34 |
|
|
$pixel_size = $1 if /^PIXEL_SIZE (\d+)$/;
|
35 |
|
|
$font_ascent = $1 if /^FONT_ASCENT (\d+)$/;
|
36 |
|
|
$font_descent = $1 if /^FONT_DESCENT (\d+)$/;
|
37 |
|
|
$font_name = $1 if /^FONT (.*)$/;
|
38 |
|
|
$default_char = $1 if /^DEFAULT_CHAR (\d+)$/;
|
39 |
|
|
|
40 |
|
|
last if /^CHARS /;
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
print "/* Font information:\n\n";
|
44 |
|
|
print " name: $font_name\n";
|
45 |
|
|
print " pixel size: $pixel_size\n";
|
46 |
|
|
print " ascent: $font_ascent\n";
|
47 |
|
|
print " descent: $font_descent\n";
|
48 |
|
|
print "*/\n\n";
|
49 |
|
|
|
50 |
|
|
print "/* Font character bitmap data. */\n";
|
51 |
|
|
print "static MWIMAGEBITS ${font}_bits[] = {\n";
|
52 |
|
|
|
53 |
|
|
$ch_height = $font_ascent + $font_descent;
|
54 |
|
|
$ofs = 0;
|
55 |
|
|
$maxwidth = 0;
|
56 |
|
|
$firstchar = -1;
|
57 |
|
|
while () {
|
58 |
|
|
chop;
|
59 |
|
|
undef $encoding, undef $width, undef $bbx, undef $bby, undef $bbw, undef $bbh if /^STARTCHAR /;
|
60 |
|
|
$encoding = $1 if /^ENCODING (\d+)/;
|
61 |
|
|
last if defined $encoding && $encoding > $LAST_CHAR;
|
62 |
|
|
$width = $1 if /^DWIDTH (-?\d+)/;
|
63 |
|
|
($bbw, $bbh, $bbx, $bby) = ($1, $2, $3, $4) if /^BBX (-?\d+) (-?\d+) (-?\d+) (-?\d+)/;
|
64 |
|
|
|
65 |
|
|
if (/^BITMAP$/) {
|
66 |
|
|
next if !defined $encoding;
|
67 |
|
|
$firstchar = $encoding if $firstchar < 0;
|
68 |
|
|
$encoding_tab[$encoding] = $ofs;
|
69 |
|
|
$width -= $bbx, $bbx = 0 if $bbx < 0;
|
70 |
|
|
$width[$encoding] = $width;
|
71 |
|
|
$maxwidth = $width if $width > $maxwidth;
|
72 |
|
|
$ch_words = int (($width+$IMAGE_BITS-1)/$IMAGE_BITS);
|
73 |
|
|
$ch_bits = $ch_words*$IMAGE_BITS;
|
74 |
|
|
for (my $i = 0; $i < $ch_height; $i++) {
|
75 |
|
|
for (my $k = 0; $k < $ch_words; $k++) {
|
76 |
|
|
$bm[$i][$k] = 0;
|
77 |
|
|
}
|
78 |
|
|
}
|
79 |
|
|
for (my $i = 0; ; $i++) {
|
80 |
|
|
$_ = ;
|
81 |
|
|
chop;
|
82 |
|
|
last if /^ENDCHAR$/;
|
83 |
|
|
|
84 |
|
|
@hexnibbles = split //,$_;
|
85 |
|
|
for (my $k=0; $k<$ch_words; $k++) {
|
86 |
|
|
$ndx = $k*$IMAGE_NIBBLES;
|
87 |
|
|
$padnibbles = @hexnibbles - $ndx;
|
88 |
|
|
last if $padnibbles <= 0; # if bbx pushes bits into next word
|
89 |
|
|
# and no more bits from bdf file
|
90 |
|
|
$padnibbles = 0 if $padnibbles >= $IMAGE_NIBBLES;
|
91 |
|
|
$value = hex join '',@hexnibbles[$ndx..($ndx+$IMAGE_NIBBLES-1-$padnibbles)];
|
92 |
|
|
$value = $value << ($padnibbles*$IMAGE_NIBBLES);
|
93 |
|
|
$bm[$ch_height - $font_descent - $bby - $bbh + $i][$k] |=
|
94 |
|
|
$value >> ($bbx);
|
95 |
|
|
if ($bbx) { # handle overflow into next image_word
|
96 |
|
|
$bm[$ch_height - $font_descent - $bby - $bbh + $i][$k+1] =
|
97 |
|
|
($value << ($IMAGE_BITS - $bbx)) & $IMAGE_MASK;
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
### printf "\n/* Character %c (0x%02x):\n", $encoding, $encoding;
|
103 |
|
|
printf "\n/* Character (0x%02x):\n", $encoding;
|
104 |
|
|
print " bbw=$bbw, bbh=$bbh, bbx=$bbx, bby=$bby, width=$width\n";
|
105 |
|
|
print " +", ("-" x $ch_bits), "+\n";
|
106 |
|
|
for (my $i = 0; $i < $ch_height; $i++) {
|
107 |
|
|
print " |";
|
108 |
|
|
for (my $k = 0; $k < $ch_words; $k++) {
|
109 |
|
|
for (my $j = $IMAGE_BITS - 1; $j >= 0; $j--) {
|
110 |
|
|
print $bm[$i][$k] & (1 << $j) ? "*" : " ";
|
111 |
|
|
}
|
112 |
|
|
}
|
113 |
|
|
print "|\n";
|
114 |
|
|
}
|
115 |
|
|
print " +", ("-" x $ch_bits), "+ */\n";
|
116 |
|
|
|
117 |
|
|
for (my $i = 0; $i < $ch_height; $i++) {
|
118 |
|
|
for ($k=0; $k<$ch_words; $k++) {
|
119 |
|
|
$ofs++;
|
120 |
|
|
printf "0x%04x, ", $bm[$i][$k];
|
121 |
|
|
}
|
122 |
|
|
printf "\n";
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
print "};\n\n";
|
128 |
|
|
|
129 |
|
|
#print STDERR "Maximum character width=$maxwidth\n";
|
130 |
|
|
|
131 |
|
|
print "/* Character->glyph data. */\n";
|
132 |
|
|
print "static unsigned short ${font}_offset[] = {\n";
|
133 |
|
|
for (my $i = $firstchar; $i <= $LAST_CHAR; $i++) {
|
134 |
|
|
my $char = $i;
|
135 |
|
|
my $ofs = $encoding_tab[$i];
|
136 |
|
|
$ofs = $encoding_tab[$default_char], $char = $default_char if !defined $ofs;
|
137 |
|
|
### printf " $ofs,\t/* %c (0x%02x) */\n", $char, $i;
|
138 |
|
|
printf " $ofs,\t/* (0x%02x) */\n", $i;
|
139 |
|
|
}
|
140 |
|
|
print "};\n\n";
|
141 |
|
|
|
142 |
|
|
print "/* Character width data. */\n";
|
143 |
|
|
print "static unsigned char ${font}_width[] = {\n";
|
144 |
|
|
for (my $i = $firstchar; $i <= $LAST_CHAR; $i++) {
|
145 |
|
|
my $char = $i;
|
146 |
|
|
my $width = $width[$i];
|
147 |
|
|
$width = $width[$default_char], $char = $default_char if !defined $encoding_tab[$i];
|
148 |
|
|
### printf " $width,\t/* %c (0x%02x) */\n", $char, $i;
|
149 |
|
|
printf " $width,\t/* (0x%02x) */\n", $i;
|
150 |
|
|
}
|
151 |
|
|
print "};\n\n";
|
152 |
|
|
|
153 |
|
|
$size = $LAST_CHAR - $firstchar + 1;
|
154 |
|
|
|
155 |
|
|
print "/* Exported structure definition. */\n";
|
156 |
|
|
print "MWCFONT font_${font} = {\n";
|
157 |
|
|
print " \"$font\",\n";
|
158 |
|
|
print " $maxwidth,\n";
|
159 |
|
|
print " $ch_height,\n";
|
160 |
|
|
print " $font_ascent,\n";
|
161 |
|
|
print " $firstchar,\n";
|
162 |
|
|
print " $size,\n";
|
163 |
|
|
print " ${font}_bits,\n";
|
164 |
|
|
print " ${font}_offset,\n";
|
165 |
|
|
print " ${font}_width,\n";
|
166 |
|
|
print "};\n";
|