#!/usr/bin/perl -w
#
# chfont - do stuff to TrueType fonts
# ~~~~~~   (c) Andrew Flegg 2002. Released under the Artistic Licence.

use strict;

die "syntax: chfont <file.ttf> ...\n" unless @ARGV;

my $smooth_find    = "\x00\x01\xff\xff\x00\x03";
my $smooth_replace = "\x00\x03\xff\xff\x00\x03";

foreach my $file (@ARGV) {
    print "$file... ";

    warn "$file doesn't end .ttf\n", next unless $file =~ /.ttf$/i;
    warn "no such file: $file\n", next unless -f $file;

    rename($file, "$file.bak");
    open(IN, "<$file.bak") or die "unable to open $file.bak: $!\n";
    open(OUT, ">$file") or die "unable to write $file: $!\n";

    my ($done_vendor, $done_smoothing);
    while (<IN>) {
	$done_vendor    ||= s/MS  /FONT/;
	$done_smoothing ||= s/$smooth_find/$smooth_replace/;
	print OUT $_;
    }
    print "(failed to change vendor) " unless $done_vendor;
    print "(failed to change smoothing) " unless $done_smoothing;

    close(OUT) or die "unable to close $file: $!\n";
    close(IN) or die "unable to close $file.bak: $!\n";
    print "done.\n";
}
exit;
