Many changes:
[Marmot.git] / hexfont2data.pl
blobfdb2a79b79b2c37af2b80e690f1ade9094768782
1 #!/usr/bin/perl
3 # hexfont2data.pl --
5 # Convert a font hex file (http://unifoundry.com/unifont.html) to
6 # a .data section suitable for assembly by gas.
8 use strict;
9 use warnings;
11 my $in = *STDIN;
12 my $out = *STDOUT;
14 if (@ARGV == 0) {
15 # nop
16 } elsif (@ARGV == 2) {
17 open $in, "<$ARGV[0]" or die "Unable to read $ARGV[0]: $!\n";
18 open $out, ">$ARGV[1]" or die "Unable to write $ARGV[1]: $!\n";
19 } elsif (@ARGV == 1) {
20 my $type;
22 if (-e $ARGV[0] and (stat($ARGV[0]))[7]) {
23 $type = 'input';
24 open $in, "<$ARGV[0]" or die "Unable to read $ARGV[0]: $!\n";
25 } else {
26 $type = 'output';
27 open $out, ">$ARGV[0]" or die "Unable to write $ARGV[0]: $!\n";
30 print STDERR "Warning: ambiguous arguments. Assuming $ARGV[0] is $type\n";
31 } else {
32 die <<"USAGE";
33 Usage: hexfont2data.pl [input] [output]
34 USAGE
37 my $table = '';
38 my $data = '';
39 my $c = 0;
41 while (my $line = <$in>) {
42 chomp $line;
43 $c++;
45 unless ($line =~ m/^(....):(.+)/) {
46 print STDERR "Warning: Invalid line $c\n";
47 next;
50 my ($n, $bmp) = ($1, $2);
51 my ($width, $height);
53 my $length = length($bmp) / 2;
55 # XXX: I count 32 and 64, but Perl counts 16 and 32(!). I'm baffled.
56 if ($length == 16) {
57 $width = 8;
58 $height = 16;
59 } elsif ($length == 32) {
60 $width = 16;
61 $height = 16;
64 $table .= <<"TABLE_ENTRY";
65 .long G$n
66 .word $width, $height
67 TABLE_ENTRY
69 my $bytes = "G$n: .byte ";
70 while ($bmp =~ m/(..)/g) {
71 $bytes .= "0x$1,";
73 chop $bytes; # remove trailing comma
75 $data .= "$bytes\n";
78 print $out <<"DATA";
79 .section .data
80 .align 8
81 .global fontTable
82 fontTable:
83 $table
85 .align 4
86 $data
87 DATA