[Gnome-print] font file utility



I'm attaching a little perl script, which writes out an xml .font file
based on the .pfb and .afm files found in a specified path.  I found
it useful for installing "custom" type 1 fonts for gnome-print.

If people think it's useful, maybe it could go into the fonts dir in
gnome-print?

-- 
Allin Cottrell
Department of Economics
Wake Forest University, NC
/*
 *  Copyright (C) Allin Cottrell
 *
 *  Authors: Allin Cottrell <cottrell@wfu.edu>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#!/usr/bin/perl

use strict;

my $pfbpath;
my $afmpath;

my @fields;
my $fontname;
my $fullname;
my $famname;
my $weight;
my $version;
my ($pfb, $afm);

my $fstart = '  <font format="type1" ';

sub usage
{
    die <<"EndUsage";
usage: afmtoxml pfbdir [ afmdir ]

afmtoxml -- Read afm files and write an xml font listing
            suitable for use with gnome-font-install.

You must give the name of a directory containing type 1 font files
(in binary pfb format).  If the corresponding afm files live in a
different directory, give the name of that directory as a second
parameter.  afmtoxml will locate the .pfb files, find the 
corresponding afms, read each of the afm files, and write out a 
listing of the font information in xml.  This listing appears
on stdout; you will probably want to redirect it to a file with
the suffix ".font".  

If afmtoxml is successful, you can then install the fonts for 
gnome-print by doing

gnome-font-install -t fontmap_path -f font_list_dir

where "fontmap_path" is the path to the gnome-print fontmap,
e.g. /opt/gnome/share/fonts/fontmap2, and "font_list_dir" is
a directory containing at least one .font file as generated by
afmtoxml.

EndUsage
}

if (@ARGV == 0) { &usage; }
if ($ARGV[0] =~ /^-+h/) { &usage; }
$pfbpath = $ARGV[0];
if (@ARGV == 1) { 
    $afmpath = $pfbpath; 
} else {
    $afmpath = $ARGV[1];
}

print "<?xml version=\"1.0\"?>\n";
print " <fontfile>\n";

foreach $pfb (glob("$pfbpath/*.pfb")) {

    @fields = split(m+/+, $pfb);
    $afm = $fields[$#fields];
    $afm =~ s/\.pfb//;
    $afm = $afmpath . "/" . $afm . ".afm";

    open (AFM, "<$afm") || die "Can't open $afm";

    $fontname = undef;
    $fullname = undef;
    $famname = undef;
    $weight = undef;
    $version = "0.1";

    while (<AFM>) {
	if (s/^FontName //) {
	    chomp;
	    s/\r//;
	    $fontname = $_;
	    next;
	}
	if (s/^FullName //) {
	    chomp;
	    s/\r//;
	    $fullname = $_;
	    next;
	}
	if (s/^FamilyName //) {
	    chomp;
	    s/\r//;
	    $famname = $_;
	    next;
	}
	if (s/^Weight //) {
	    chomp;
	    s/\r//;
	    $weight = $_;
	    next;
	}
	if (s/^Version //) {
	    chomp;
	    s/\r//;
	    $version = $_;
	}			
    }

    close(AFM);

    print $fstart;
    print "metrics=\"$afm\" ";
    print "glyphs=\"$pfb\" ";
    print "name=\"$fontname\" ";
    print "fullname=\"$fullname\" ";
    print "familyname=\"$famname\" ";
    print "weight=\"$weight\" ";
    print "version=\"$version\"/>\n";

}

print "</fontfile>\n";






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]