a2d/res/convert_font.pl

69 lines
1.4 KiB
Perl
Raw Normal View History

2017-09-28 02:41:40 +00:00
#!/usr/bin/env perl
# Input: HCRG-style Font (768 bytes)
2018-01-29 05:18:00 +00:00
# Output: MGTK font definition
2017-09-28 02:41:40 +00:00
# Usage: perl convert.pl < font_data > font.bin
2017-09-28 02:41:40 +00:00
use strict;
use warnings;
$SIG{__WARN__} = sub { die @_ };
my @chars;
my $NCCS = 32;
my $NCHARS = 128;
# Fill in control characters
for (my $i = 0; $i < $NCCS; ++$i) {
$chars[$i] = "\x00" x 8;
}
for (my $i = 0; $i < $NCHARS - $NCCS; ++$i) {
read(STDIN, $chars[$i + $NCCS], 8);
}
my @out;
push @out, 0x00; # single width
push @out, 0x7f; # last character
2017-09-28 02:41:40 +00:00
push @out, 8; # height
# compute glyph widths
2017-09-28 02:41:40 +00:00
for (my $i = 0; $i < $NCHARS; ++$i) {
my @bytes = map { ord($_) & 0x7f } split('', $chars[$i]);
my $bits = 0;
for (my $b = 0; $b < 8; ++$b) {
$bits = $bits | $bytes[$b];
}
$chars[$i] = join('', map { chr } @bytes);
my $w =
$bits >= (1<<6) ? 7 :
$bits >= (1<<5) ? 6 :
$bits >= (1<<4) ? 5 :
$bits >= (1<<3) ? 4 :
$bits >= (1<<2) ? 3 :
$bits >= (1<<1) ? 2 : 1;
push @out, $w;
}
# bits
for (my $b = 0; $b < 8; ++$b) {
for (my $i = 0; $i < $NCHARS; ++$i) {
push @out, ord(substr($chars[$i], $b, 1));
}
}
# source output for ca65
#while (scalar @out) {
# my @chunk = splice(@out, 0, 8);
# @chunk = map { sprintf('$%02x', $_) } @chunk;
# print " .byte " . join(',', @chunk) . "\n";
#}
print join('', map { chr($_) } @out);