summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rwxr-xr-xtools/bdf2c197
-rwxr-xr-xtools/bdf2fnt39
2 files changed, 236 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
20use POSIX;
21
22if ($#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
38print "/* Generated by convbdf on ", substr(`date`, 0, -1), ". */\n";
39print "#include \"font.h\"\n\n";
40
41open BDF, "<$file" || die;
42while (<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;
55undef $fbbw, undef $fbbh, undef $fbbx, undef $fbby;
56
57print "/* Font information:\n\n";
58print " name: $font_name\n";
59print " pixel size: $pixel_size\n" if defined $pixel_size;
60print " ascent: $font_ascent\n";
61print " descent: $font_descent\n";
62print "*/\n\n";
63
64print "/* Font character bitmap data. */\n";
65print "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;
72while (<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
144print "};\n\n";
145
146##print STDERR "Maximum character width=$maxwidth\n";
147
148print "/* Character->glyph mapping. */\n";
149print "static unsigned long ${font}_offset[] = {\n";
150for (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}
157print "};\n\n";
158
159$gen_width_table = 0;
160for (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
167if ($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
182print "/* Exported structure definition. */\n";
183print "MWCFONT font_${font} = {\n";
184print " \"$font\",\n";
185print " $maxwidth,\n";
186print " $ch_height,\n";
187print " $font_ascent,\n";
188print " $firstchar,\n";
189print " $size,\n";
190print " ${font}_bits,\n";
191print " ${font}_offset,\n";
192if ($gen_width_table) {
193 print " ${font}_width,\n";
194} else { print " 0, /* fixed width*/\n"; }
195print " $default_char,\n";
196print " sizeof(${font}_bits)/sizeof(MWIMAGEBITS),\n";
197print "};\n";
diff --git a/tools/bdf2fnt b/tools/bdf2fnt
new file mode 100755
index 0000000000..97b8292ba4
--- /dev/null
+++ b/tools/bdf2fnt
@@ -0,0 +1,39 @@
1#!/bin/bash
2#
3# bdf2fnt - shell script to convert a BDF file to RBF format
4#
5# usage: bdf2fnt bdffile (don't use .bdf extension!)
6#
7# Example: bdf2fnt courB08
8# creates ./courB08.fnt and /tmp/courB08.c
9# the .fnt file can be renamed /system.fnt for loading
10# the .c file can be moved to firmware dir to compile-in font
11#
12
13# convert from bdf to C source
14./bdf2c $1.bdf > /tmp/$1.c
15
16# compile writerbf with linked C source font
17gcc -DFONT=font_$1 -I../firmware -o /tmp/writerbf writerbf.c /tmp/$1.c
18
19# run writerbf, will write linked incore font to .rbf format
20/tmp/writerbf
21rm /tmp/writerbf
22
23# load .rbf font and display it for test
24gcc -DMAX_FONT_SIZE=500000 -o /tmp/loadrbf loadrbf.c
25/tmp/loadrbf $1.fnt > /tmp/$1.1
26rm /tmp/loadrbf
27
28# link .c font and diff with .fnt load for test
29gcc -DFONT=font_$1 -I../firmware -o /tmp/loadrbf loadrbf.c /tmp/$1.c
30/tmp/loadrbf > /tmp/$1.2
31rm /tmp/loadrbf
32
33#
34# we diff the output to ensure correctness
35diff /tmp/$1.1 /tmp/$1.2
36
37# clean up
38rm /tmp/$1.1 /tmp/$1.2
39#rm /tmp/$1.c