diff options
Diffstat (limited to 'tools/bdf2c')
-rwxr-xr-x | tools/bdf2c | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/tools/bdf2c b/tools/bdf2c new file mode 100755 index 0000000000..ff3d5ed3b3 --- /dev/null +++ b/tools/bdf2c | |||
@@ -0,0 +1,197 @@ | |||
1 | #! /usr/bin/perl -w | ||
2 | # | ||
3 | # Convert BDF files to incore MWCFONT structure | ||
4 | # Copyright (c) 2002 by Greg Haerr <greg@censoft.com> | ||
5 | # | ||
6 | # from The Microwindows Project (http://microwindows.org) | ||
7 | # | ||
8 | # modified on 09/10/02 by G Haerr | ||
9 | # - fixed DWIDTH 0 parsing | ||
10 | # - don't limit font size to 0x7e characters | ||
11 | # - changed offset data to unsigned long for large fonts | ||
12 | # - don't generate width table if fixed-width | ||
13 | # - added defaultchar to output | ||
14 | # - added bits_size member for loadable fonts | ||
15 | # modified on 3/26/00 by G Haerr added ascent field, fixed $IMAGE_BITS | ||
16 | # modified on 2/10/00 by K Harris to accept any size input character | ||
17 | # modified by G Haerr from bdftobogl for 16 bit MWIMAGEBITS | ||
18 | # originally from BOGL - Ben's Own Graphics Library <pfaffben@debian.org>. | ||
19 | |||
20 | use POSIX; | ||
21 | |||
22 | if ($#ARGV < 0) { | ||
23 | print "Usage: convbdf font.bdf > font.c\n"; | ||
24 | exit -1; | ||
25 | } | ||
26 | |||
27 | ##$LAST_CHAR = 0x7e; | ||
28 | $IMAGE_BITS = 16; | ||
29 | $IMAGE_NIBBLES = $IMAGE_BITS/4; | ||
30 | $IMAGE_MASK = 0xffff; | ||
31 | |||
32 | $file = $ARGV[0]; | ||
33 | |||
34 | $font = $file; | ||
35 | $font =~ s/\.bdf//; | ||
36 | $font =~ tr/a-zA-Z0-9_/_/cs; | ||
37 | |||
38 | print "/* Generated by convbdf on ", substr(`date`, 0, -1), ". */\n"; | ||
39 | print "#include \"font.h\"\n\n"; | ||
40 | |||
41 | open BDF, "<$file" || die; | ||
42 | while (<BDF>) { | ||
43 | chop; | ||
44 | $pixel_size = $1 if /^PIXEL_SIZE (\d+)$/; | ||
45 | $font_ascent = $1 if /^FONT_ASCENT (\d+)$/; | ||
46 | $font_descent = $1 if /^FONT_DESCENT (\d+)$/; | ||
47 | $font_name = $1 if /^FONT (.*)$/; | ||
48 | $default_char = $1 if /^DEFAULT_CHAR (\d+)$/; | ||
49 | ($fbbw, $fbbh, $fbbx, $fbby) = ($1, $2, $3, $4) if /^FONTBOUNDINGBOX (-?\d+) (-?\d+) (-?\d+) (-?\d+)/; | ||
50 | |||
51 | last if /^CHARS /; | ||
52 | } | ||
53 | |||
54 | $font_width = $fbbw - $fbbx; | ||
55 | undef $fbbw, undef $fbbh, undef $fbbx, undef $fbby; | ||
56 | |||
57 | print "/* Font information:\n\n"; | ||
58 | print " name: $font_name\n"; | ||
59 | print " pixel size: $pixel_size\n" if defined $pixel_size; | ||
60 | print " ascent: $font_ascent\n"; | ||
61 | print " descent: $font_descent\n"; | ||
62 | print "*/\n\n"; | ||
63 | |||
64 | print "/* Font character bitmap data. */\n"; | ||
65 | print "static MWIMAGEBITS ${font}_bits[] = {\n"; | ||
66 | |||
67 | $ch_height = $font_ascent + $font_descent; | ||
68 | $ofs = 0; | ||
69 | $maxwidth = 0; | ||
70 | $firstchar = -1; | ||
71 | $lastchar = -1; | ||
72 | while (<BDF>) { | ||
73 | chop; | ||
74 | undef $encoding, undef $width, undef $bbx, undef $bby, undef $bbw, undef $bbh if /^STARTCHAR /; | ||
75 | $encoding = $1 if /^ENCODING (\d+)/; | ||
76 | ## last if defined $encoding && $encoding > $LAST_CHAR; | ||
77 | $width = $1 if /^DWIDTH (-?\d+)/; | ||
78 | $width = $font_width if defined $width && $width <= 0; | ||
79 | ($bbw, $bbh, $bbx, $bby) = ($1, $2, $3, $4) if /^BBX (-?\d+) (-?\d+) (-?\d+) (-?\d+)/; | ||
80 | |||
81 | if (/^BITMAP$/) { | ||
82 | next if !defined $encoding; | ||
83 | $firstchar = $encoding if $firstchar < 0; | ||
84 | $lastchar = $encoding if $lastchar < $encoding; | ||
85 | $encoding_tab[$encoding] = $ofs; | ||
86 | $width -= $bbx, $bbx = 0 if $bbx < 0; | ||
87 | $width[$encoding] = $width; | ||
88 | $maxwidth = $width if $width > $maxwidth; | ||
89 | $ch_words = int (($width+$IMAGE_BITS-1)/$IMAGE_BITS); | ||
90 | $ch_bits = $ch_words*$IMAGE_BITS; | ||
91 | for (my $i = 0; $i < $ch_height; $i++) { | ||
92 | for (my $k = 0; $k < $ch_words; $k++) { | ||
93 | $bm[$i][$k] = 0; | ||
94 | } | ||
95 | } | ||
96 | for (my $i = 0; ; $i++) { | ||
97 | $_ = <BDF>; | ||
98 | chop; | ||
99 | last if /^ENDCHAR$/; | ||
100 | |||
101 | @hexnibbles = split //,$_; | ||
102 | for (my $k=0; $k<$ch_words; $k++) { | ||
103 | $ndx = $k*$IMAGE_NIBBLES; | ||
104 | $padnibbles = @hexnibbles - $ndx; | ||
105 | last if $padnibbles <= 0; # if bbx pushes bits into next word | ||
106 | # and no more bits from bdf file | ||
107 | $padnibbles = 0 if $padnibbles >= $IMAGE_NIBBLES; | ||
108 | $value = hex join '',@hexnibbles[$ndx..($ndx+$IMAGE_NIBBLES-1-$padnibbles)]; | ||
109 | $value = $value << ($padnibbles*$IMAGE_NIBBLES); | ||
110 | $bm[$ch_height - $font_descent - $bby - $bbh + $i][$k] |= | ||
111 | $value >> ($bbx); | ||
112 | if ($bbx) { # handle overflow into next image_word | ||
113 | $bm[$ch_height - $font_descent - $bby - $bbh + $i][$k+1] = | ||
114 | ($value << ($IMAGE_BITS - $bbx)) & $IMAGE_MASK; | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | |||
119 | ### printf "\n/* Character %c (0x%02x):\n", $encoding, $encoding; | ||
120 | printf "\n/* Character (0x%02x):\n", $encoding; | ||
121 | print " bbw=$bbw, bbh=$bbh, bbx=$bbx, bby=$bby, width=$width\n"; | ||
122 | print " +", ("-" x $ch_bits), "+\n"; | ||
123 | for (my $i = 0; $i < $ch_height; $i++) { | ||
124 | print " |"; | ||
125 | for (my $k = 0; $k < $ch_words; $k++) { | ||
126 | for (my $j = $IMAGE_BITS - 1; $j >= 0; $j--) { | ||
127 | print $bm[$i][$k] & (1 << $j) ? "*" : " "; | ||
128 | } | ||
129 | } | ||
130 | print "|\n"; | ||
131 | } | ||
132 | print " +", ("-" x $ch_bits), "+ */\n"; | ||
133 | |||
134 | for (my $i = 0; $i < $ch_height; $i++) { | ||
135 | for ($k=0; $k<$ch_words; $k++) { | ||
136 | $ofs++; | ||
137 | printf "0x%04x, ", $bm[$i][$k]; | ||
138 | } | ||
139 | printf "\n"; | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | |||
144 | print "};\n\n"; | ||
145 | |||
146 | ##print STDERR "Maximum character width=$maxwidth\n"; | ||
147 | |||
148 | print "/* Character->glyph mapping. */\n"; | ||
149 | print "static unsigned long ${font}_offset[] = {\n"; | ||
150 | for (my $i = $firstchar; $i <= $lastchar; $i++) { | ||
151 | my $char = $i; | ||
152 | my $ofs = $encoding_tab[$i]; | ||
153 | $ofs = $encoding_tab[$default_char], $char = $default_char if !defined $ofs; | ||
154 | ### printf " $ofs,\t/* %c (0x%02x) */\n", $char, $i; | ||
155 | printf " $ofs,\t/* (0x%02x) */\n", $i; | ||
156 | } | ||
157 | print "};\n\n"; | ||
158 | |||
159 | $gen_width_table = 0; | ||
160 | for (my $i = $firstchar; $i <= $lastchar; $i++) { | ||
161 | my $char = $i; | ||
162 | my $width = $width[$i]; | ||
163 | $width = $width[$default_char], $char = $default_char if !defined $encoding_tab[$i]; | ||
164 | $gen_width_table = 1 if $width != $maxwidth | ||
165 | } | ||
166 | |||
167 | if ($gen_width_table) { | ||
168 | print "/* Character width data. */\n"; | ||
169 | print "static unsigned char ${font}_width[] = {\n"; | ||
170 | for (my $i = $firstchar; $i <= $lastchar; $i++) { | ||
171 | my $char = $i; | ||
172 | my $width = $width[$i]; | ||
173 | $width = $width[$default_char], $char = $default_char if !defined $encoding_tab[$i]; | ||
174 | ### printf " $width,\t/* %c (0x%02x) */\n", $char, $i; | ||
175 | printf " $width,\t/* (0x%02x) */\n", $i; | ||
176 | } | ||
177 | print "};\n\n"; | ||
178 | } | ||
179 | |||
180 | $size = $lastchar - $firstchar + 1; | ||
181 | |||
182 | print "/* Exported structure definition. */\n"; | ||
183 | print "MWCFONT font_${font} = {\n"; | ||
184 | print " \"$font\",\n"; | ||
185 | print " $maxwidth,\n"; | ||
186 | print " $ch_height,\n"; | ||
187 | print " $font_ascent,\n"; | ||
188 | print " $firstchar,\n"; | ||
189 | print " $size,\n"; | ||
190 | print " ${font}_bits,\n"; | ||
191 | print " ${font}_offset,\n"; | ||
192 | if ($gen_width_table) { | ||
193 | print " ${font}_width,\n"; | ||
194 | } else { print " 0, /* fixed width*/\n"; } | ||
195 | print " $default_char,\n"; | ||
196 | print " sizeof(${font}_bits)/sizeof(MWIMAGEBITS),\n"; | ||
197 | print "};\n"; | ||