<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -w

use strict;

my $me = "lilyfont";
my $ver = "0.1";
my $prefix = "lily";

my $today;

my $src;
my $texmf;

my %file;
my @family;
my %sz;

########################################

sub Usage() {
    print
"Name:
	$me - take lilypond fonts (feta, parmesan, ...) and make them available for latex
Usage:
	$me src texmf
Options:
	src    source directory, where the postscript fonts (*.pfb, *.tfm) are
	texmf  texmf target directory, where files suitable for latex be placed
";
    exit;
}

sub examine_srcdir($) {
    my @file = sort  map { s|^$src/||; $_; } glob "$src/*.pf?";

    for (@file) {
	if (m/^(([^0-9]+)(\d+))\.pf[ab]$/) {
	    next if (defined($file{$1}));  # if booth .pfa and .pfb
	    $file{$1} = $_;
	    #print "$_\n";
	    if (!defined($sz{$2})) {
		$sz{$2} = [ ];
	    }
	    push @{$sz{$2}}, $3;
	} elsif (m/^feta-braces-/) {
	    # ignore theese
	} else {
	    print "strange name &lt;$_&gt;\n";
	}
    }
    @family = sort keys %sz;
    for my $k (@family) {
	@{$sz{$k}} = sort { $a &lt;=&gt; $b } @{$sz{$k}};
    }
}

sub init_output() {
    open(FSTY, "&gt;$me.sty") or die("could not open $me.sty");
    open(FMAP, "&gt;$me.map") or die("could not open $me.map");

    print FSTY
"\\NeedsTeXFormat{LaTeX2e}
\\ProvidesPackage{$me}[$today $me version $ver]
";
}

sub fini_output() {
    print FSTY "\n\\endinput\n";
    close(FSTY);
    close(FMAP);
}

sub examine_file($$$) {
    my $name = shift;
    my $sz   = shift;
    my $flag = shift;

    my $stem = "$name$sz";
    my $fontname = $stem;

    my $file = "$src/$file{$stem}";
    # yes, I can read pfb files as well as pfa ones
    open(FI, $file) or die("could not open $file");

    while(&lt;FI&gt;) {
	if (m|/FontName /(\S+) def|) {
	    $fontname = $1;
	    #print "$file: $fontname\n";
	    last;
	}
    }
    if ($flag) {
	my $tag = "$me$name";
	$tag =~ tr/-//d;
	print FSTY "\n%% glyph names from: $file\n";
	#print FSTY "%\\newcommand\\\@select\@${tag}{\\fontencoding{U}\\fontfamily{$name}\\selectfont}\n";
	print FSTY "\\newcommand\\\@${tag}{\\fontencoding{U}\\fontfamily{$name}\\selectfont}\n\n";
	while (&lt;FI&gt;) {
	    if (m|/Encoding \d+ array|) {
		last;
	    }
	}
	while (&lt;FI&gt;) {
	    if (m|readonly def|) {
		last;
	    }
	    if (m|dup (\d+)/(.+) put|) {
		my $code = $1;
		my $name = $2;
		$name =~ tr/\._*/XYZ/;
		$name =~ tr/0-9/A-J/;
		print FSTY "\\newcommand\\$prefix$name\{\{\\\@$tag\\char$code\}\}\n";
	    } else {
		# ignore
	    }
	}
    }
    close(FI);

    $fontname;
}

sub run($) {
    my $name = shift;

    my $filefd = "U$name.fd";
    open(FFD, "&gt;$filefd" ) or die("could not open $filefd" );
    print FFD "\\ProvidesFile{U$name.fd}[$today $name font definitions version $ver]
\\DeclareFontFamily{U}{$name}{}
\\DeclareFontShape{U}{$name}{m}{n}{%
";

    #my @sz = map glob "$src/${name}*.pfb";
    my @sz =
	map { s|^$src/${name}||; s|\.pfb$||; $_; }
	    grep { m/${name}\d+.pfb/ }
		glob "$src/${name}*.pfb";
    #print join(" ", @sz), "\n";

    #my $stem = "$name$sz";

    my $flag = 1;
    for my $sz (@sz) {
	my $stem = "$name$sz";
	my $fontname = examine_file($name, $sz, $flag);
	$flag = 0;
	print FMAP "$stem $fontname &lt;$stem.pfb\n";
	print FFD " &lt;$sz&gt; $stem\n";
    }

    print FFD "}{}\n\\endinput\n";
    close(FFD);
}

sub install() {
    `mkdir -p          $texmf/fonts/type1/$me`;
    `ln -sf $src/*.pfb $texmf/fonts/type1/$me/`;

    `mkdir -p          $texmf/fonts/tfm/$me`;
    `ln -sf $src/*.tfm $texmf/fonts/tfm/$me/`;

    #`mkdir -p            $texmf/fonts/source/$me`;
    #`ln -sf $src/../*.mf $texmf/fonts/source/$me`;

    `mkdir -p $texmf/fonts/map/$me`;
    `cp *.map $texmf/fonts/map/$me/`;

    `mkdir -p   $texmf/tex/latex/$me`;
    `cp $me.sty $texmf/tex/latex/$me/`;
    `cp U*.fd $texmf/tex/latex/$me/`;

    print "don't forget to run mktexlsr\n";
    print "add 'Map $me.map' to your updmap config file,\n";
    print " mine is at /usr/local/share/texmf/web2c/updmap.cfg\n";
    print " and run updmap/updmap-sys\n";


}

sub main() {
    if (@ARGV != 2) { Usage(); }
    $src   = $ARGV[0]; # e.g. "/Net/git/lilypond/mf/out/"
    $texmf = $ARGV[1]; # e.g. "/usr/local/share/texmf"

    $today = `date +%Y/%m/%d`;
    chomp $today;

    examine_srcdir($src);

    init_output();
    for my $k (@family) {
	#print "$k: [ ", join(", ", @{$sz{$k}}), " ]\n";
	run($k);
    }
    fini_output();

    install();
}

main();

__END__
</pre></body></html>