#!/usr/bin/perl
#
#  You may need to change the above path.
#
#-----------------------------------------------------------------------------
#
#  Copyright (C) 1996 James Macnicol
#
#  This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, 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 MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
#-----------------------------------------------------------------------------
#
# type1inst : Generate a "fonts.scale" file for Type 1 fonts in PFB format
# for use with your favourite X server.  Also generate a "Fontmap" for use
# with ghostscript.
#
# THIS IS ALPHA SOFTWARE!  PLEASE READ THE "README" FILE!!!
#
# Direct all correspondence to J.Macnicol@student.anu.edu.au
#
#-----------------------------------------------------------------------------

#
# Map identifying strings in /Notice into foundry names.  Separate identifier
# from name with a :.  Someone let me know if this is a problem (i.e. foundry
# has a : in its name which really ought to be there (although I may not
# believe it) ; we'll change it to ! or something.
#
# You probably want to put foundries which license type from others near the
# top of this list (e.g. Adobe).  If the name of the original source of the
# face is listed lower down then it will be used that instead.  It's just that
# Adobe does have its own faces too, but more often than not they are
# licensed.  Doing it this way will make it work out correctly in either case.
#

@foundries = ("SoftMaker:softmaker",
	      "Adobe:adobe",
	      "International Typeface Corporation:itc",
	      "URW:urw",
	      "Monotype Corporation:monotype",
	      "Bigelow & Holmes:b&h",
	      "LETRASET:letraset",
	      "Brendel Informatik:brendel",
	      "Title Wave:titlewave",
	      "Bitstream:bitstream",
	      "IBM:ibm",
	      "Hershey:hershey",
	      "D. Rakowski:rakowski",
	      "David Rakowski:rakowski",
	      "A. Carr:carr",
	      "A.S.Meit:meit",
	      "S. G. Moye:moye",
	      "Reasonable Solutions:reasonable",
	      "FontBank:fontbank",
	      "ZSoft:zsoft",
	      "Jonathan Brecher:brecher");

# Note: Hershey is the public Hershey fonts which come with Ghostscript.
# These cause no end of problems since they look inside like funny PS
# programs rather than standard fonts.

# Note 2 : Some of these are obviously names of people only, not companies.
# They are generally public domain fonts.

#
# These are font weights.  Some are synonyms, e.g. regular for medium.  It
# has been suggested we map "thin" to "light", however there are some font
# families which have both "thin" and "light" variants.  An example is
# Linotype's Helvetica Neue.  Please let me know if you find a font where
# assuming "normal", "regular" and "medium" to be the same fails.
#

@weights = ("medium:medium",
	    "bold:bold",
	    "demi:demi",
	    "light:light",
	    "normal:medium",
	    "regular:medium",
	    "thin:thin");

#
# Likewise for slants
#

@slants = ("roman:r",
	   "italic:i",
	   "oblique:o");

#
# Style.  Wondering if we should put "serif" in here somehow....?
#

@styles = ("normal:normal",
	   "sans:sans");

#
# Print out a string with a given minimum width.  This is used to make the
# Fontmap entries look nice.
#

sub print_min_width {
    ($stream, $minwidth, $string) = @_;
    $_ = $string;
    $strlength = length($string);
    # Print the string
    print $stream $string;
    # Now pad out the rest of the space if the string is short.
    if ($strlength < $minwidth) {
	for ($i = 0; $i < ($minwidth - $strlength); $i = $i + 1) {
	    print $stream " ";
	}
    }
}

#
# Indicate progress through the directory on the command line
#

sub print_progress {
  $totalfonts = $numpffonts + $numgsfonts + $badfonts;
  if (! $silent) {
      if (($totalfonts % 10) == 0) {
	  print "[$totalfonts]\n";
      }
  }
}

#
# Put the processing stuff into a procedure since we want to do the same for
# .pfb, .pfa and .gsf files (once .pfb's are decompressed).
#
# Argument : filename.
# Returns : X font description, name of font for Fontmap
#

sub process_font {
  ($fname) = @_;
  local($xline);

  # Check to see if this is a ghostscript font
  if ($fname =~ /\.gsf\s*$/) {
      $gsfont = 1;
  } else {
      $gsfont = 0;
  }

  open(IN, $fname) || die "cannot open $file for reading";
  # An unlikely name to check to see we get a fontname out of the file.
  $fontname = "abcXYZ:!@#";
  while(<IN>) {
      if (/\/isFixedPitch\s+(.+)\s+def\s*/) {
	  if ($1 =~ /true/) {
	      $fixedpitch = "m";
	  } else {
	      $fixedpitch = "p";
	  }
      }
      # Note : some fonts have a suspect /FontName declaration where there
      # is no space between /FontName and the name of the font itself....
      if (/\/FontName\s*\/(.+)\s+def\s*/) {
	  $fontname = $1;

	  # Remove any embedded spaces
	  $fontname =~ s/\s//g;

	  # Save a copy of original full name for later
	  $fontnamecopy = $fontname;

	  # Remove -s
	  $fontname =~ s/-//g;

	  # Convert to lower case
	  $fontname =~ tr/A-Z/a-z/;

	  # Check for weight modifiers (medium, bold, demi, light etc.)
	  $weight = "medium";
	  $numweights = @weights;
	  for ($x = 0; $x < $numweights; $x = $x + 1) {
	      $ident = $weights[$x];
	      @fields = split(/:/, $ident);
	      $numfields = @fields;
	      if ($numfields != 2) {
		  printf STDERR "The weight identification \"$ident\" is \
bad\n";
		  die();
	      }
	      if ($fontname =~ /$fields[0]/) {
		  $weight = $fields[1];
	      }
	      # Remove matched word from the font's name
	      $fontname =~ s/$fields[0]//;
	  }

	  # Check for slant (italic, oblique)
	  $slant = "r";

	  $numslants = @slants;
	  for ($x = 0; $x < $numslants; $x = $x + 1) {
	      $ident = $slants[$x];
	      @fields = split(/:/, $ident);
	      $numfields = @fields;
	      if ($numfields != 2) {
		  die("The slant identification \"$ident\" is bad\n");
	      }
	      if ($fontname =~ /$fields[0]/) {
		  $slant = $fields[1];
	      }
	      # Remove matched word from the font's name
	      $fontname =~ s/$fields[0]//;
	  }

	  # Check for style (normal or sans)
	  $style = "normal";

          $numstyles = @styles;
          for ($x = 0; $x < $numstyles; $x = $x + 1) {
              $ident = $styles[$x];
              @fields = split(/:/, $ident);
              $numfields = @fields;
              if ($numfields != 2) {
                  die("The style identification \"$ident\" is bad\n");
              }
              if ($fontname =~ /$fields[0]/) {
                  $style = $fields[1];
              }
              # Remove matched word from the font's name
              $fontname =~ s/$fields[0]//;
          }

      }
      if (/\/Encoding\s+(\S+)\s*/) {
	  if ($1 =~ /StandardEncoding/) {
	      $encoding = "iso8859-1";
	  } else {
	      # This needs work
	      $encoding = "adobe-fontspecific";
	  }
      }
      if (/\/Notice\s+\((.*)\)\s*/) {
	  $notice = $1;
	  $foundry = "unknown";

	  $numfoundries = @foundries;
	  for ($x = 0; $x < $numfoundries; $x = $x + 1) {
	      $ident = $foundries[$x];
	      @fields = split(/:/, $ident);
	      $numfields = @fields;
	      if ($numfields != 2) {
		  die("The foundry identification \"$ident\" is bad\n");
	      }
	      if ($notice =~ /$fields[0]/) {
		  $foundry = $fields[1];
	      }
	  }
      }
      # Break out of loop if we've passed the interesting stuff.
      if ((! $gsfont) && (/currentfile\s+eexec/)) {
	  # This is for .pfa and .pfb fonts
	  last;
      } elsif (($gsfont) && (/currentdict\s+end/)) {
	  # This is for ghostscript .gsf fonts.  Why don't all these have a
	  # currentfile eexec ?
	  last;
      }
  }
  close(IN);

  if ($fontname =~ /abcXYZ\:\!\@\#/) {
      print LOG "\n";
      print LOG "$filename : could not determine font name\n";
      print LOG "\n";
      $badfonts = $badfonts + 1;
      &print_progress();
      return;
  }

  if (($dox) && (! $gsfont) && ($foundry =~ /unknown/)) {
      $nofoundry = $nofoundry + 1;
      print LOG "\n";
      print LOG "$filename ($fontnamecopy) : foundry not matched\n";
      print LOG "  /Notice said : \"$notice\"\n";
      print LOG "\n";
  } elsif ($dox) {
#      print LOG "$filename ($fontnamecopy) : okay\n";
  }

  if (($dox) && (! $gsfont)) {
      $xline = "-$foundry-$fontname-$weight-$slant-$style--0-0-0-0-$fixedpitch-0-$encoding";
  }

  # Update count of each type
  if ($gsfont) {
      $numgsfonts = $numgsfonts + 1;
  } else {
      $numpffonts = $numpffonts + 1;
  }

  &print_progress();

  ($xline, $fontnamecopy);
}

#
# Makes associative array out of current entries in fonts.scale
#

sub read_fonts_scale {
    local($finish, %rv, $line, $filename, $fontname);

    $finish = open(SCALE, "fonts.scale") ? 0 : 1;
    if ($finish == 1) {
	%rv;
    }

    print LOG "Reading fonts.scale ....\n";

    # First line should be an integer saying how many fonts there are.
    # Discard.
    $line = <SCALE>;
    if (! $line =~ /\s*[0-9]+\s*/) {
	print LOG "Warning : first line of fonts.scale is bad\n";
    }

    while (<SCALE>) {
	# Very rough pattern
	if (/\s*(\S+)\s+(.+)\s*/) {
	    $filename = $1;
	    $fontname = $2;
	    if ($rv{$filename}) {
		print LOG "Warning : fonts.scale contains multiple lines for file $filename\n";
	    } else {
		$rv{$filename} = $fontname;
	    }
	} else {
	    print LOG " Couldn't parse line : \n";
	    print LOG "    \"$_\"\n";
	}
    }
    close(SCALE);

    print LOG "Done.\n";

    %rv;
}

#
# Write out an associative array into fonts.scale, making a backup copy
# first.
#

sub write_fonts_scale {
    (%fontdata) = @_;
    local($numentries, $key);

    # First, make backup copy
    if (-e "fonts.scale") {
	system ("cp -f fonts.scale fonts.scale.bak");
    }

    print LOG "Writing fonts.scale....";

    $numentries = keys(%fontdata);
    open(SCALE, ">fonts.scale") || die("Can't open fonts.scale!\n");
    print SCALE "$numentries\n";
    foreach $key (sort(keys %fontdata)) {
	print_min_width(SCALE, 12, $key);
	print SCALE " ";
	print SCALE "$fontdata{$key}\n";
    }
    close(SCALE);
    system ("chmod a+r fonts.scale");

    print LOG " Done.\n";
}

#
# Read the current Fontmap and return associative array with data.
#

sub read_fontmap {
    local(%rv, $finish, $fontname, $filename);

    $finish = open(FONTMAP, "Fontmap") ? 0 : 1;
    if ($finish) {
	%rv;
    }

    print LOG "Reading Fontmap ....\n";

    while (<FONTMAP>) {
	if (/(\S+)\s+\((.*)\)\s+;\s+/) {
	    $fontname = $1;
	    $filename = $2;
	    if ($rv{$filename}) {
		# Entry already exists
		print LOG "Warning : Fontmap contains multiple lines for file $filename\n";
	    } else {
		$rv{$filename} = $fontname;
	    }
	} else {
	    print LOG "Couldn't understand line :\n";
	    print LOG "  $_\n";
	}
    }

    close(FONTMAP);

    print LOG "Done.\n";

    %rv;
}

#
# Write associative array containing font data to Fontmap
#

sub write_fontmap {
    (%fontdata) = @_;
    local($numentries, $key);

    # First, make backup copy
    if (-e "Fontmap") {
	system ("cp -f Fontmap Fontmap.bak");
    }

    print LOG "Writing Fontmap....";

    $numentries = keys(%fontdata);
    open(FONTMAP, ">Fontmap") || die("Couldn't open Fontmap!\n");
    foreach $key (sort(keys %fontdata)) {
	print_min_width(FONTMAP, 40, $fontdata{$key});
	print FONTMAP " ";
	print FONTMAP "($key)\t;\n";
    }
    close(FONTMAP);
    system ("chmod a+r Fontmap");

    print LOG " Done.\n";
}

#
# Add a font (either X or gs) to hash table 
#

sub add_font_to_aarray {
    ($fname, $text, %aa) = @_;

    if (($text =~ /^\s*$/) || ($fname =~ /^\s*$/)) {
	print "add_font_to_aarray: $fname, $text\n";
	die("Bug: Bad argument(s) to add_font_to_aarray()!\n");
    }

    if (! $aa{$fname}) {
	$aa{$fname} = $text;
    }

    %aa;
}

#
# Create sample text using each font
#

sub font_sample {
    ($filename, $fontname, $height) = @_;
    local($text, $alltext, $samplefile);

    if (($filename =~ /^\s*$/) || ($fontname =~ /^s*$/)) {
	print "font_sample: $filename, $fontname\n";
	die("Bug: Bad argument(s) to font_sample()!\n");
    }

    $text = <<"TEXT";
%!
%%EndComments
/Times-Roman findfont 
18 scalefont
setfont
newpath
200 720 moveto
(File : $filename) show
200 700 moveto
(Font Name : $fontname) show
% t1embed : $filename $fontname
closepath

/$fontname findfont 
60 scalefont        
setfont             
newpath        
30 640 moveto
(ABCDE) show
30 575 moveto
(FGHIJK) show
30 510 moveto
(LMNOP) show
30 445 moveto
(QRSTU) show
30 380 moveto
(VWXYZ) show
30 305 moveto
(abcdefghijklm) show
30 240 moveto
(nopqrstuvwxyz) show
30 165 moveto           
(1234567890) show
closepath

/$fontname findfont 
12 scalefont        
setfont             
newpath        
30 140 moveto
(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) show
30 120 moveto
(a b c d e f g h i j k l m n o p q r s t u v w x y z) show
30 100 moveto           
(1 2 3 4 5 6 7 8 9 0 \! \$ \% \& \\\( \\\) \; \: \< \> ) show
closepath
showpage
TEXT

    $samplefile = $fontname . ".ps";
    open(SAMPLE, ">samples/$samplefile") || 
    die("Couldn't open samples/$samplefile\n");
    print SAMPLE "$text\n";
    close(SAMPLE);
    system("chmod a+r samples/$samplefile");

    $alltext = <<"ALLTEXT";

/$fontname findfont 
18 scalefont        
setfont             
newpath        
30 $height moveto
($fontname : AbCdEfGhIjKlMnOpQrStUvWxYz 0123456789) show
closepath
ALLTEXT

    print ALLSAMPLE "$alltext\n";
    $height = $height - 20;
    if ($height < 90) {
      print ALLSAMPLE "showpage\n";
      $height = 720;
    }
}

#
# Start of program proper
#

# Process command line arguments
$dox = 1;
$dogs = 1;
$silent = 0;
$samples = 0;
@argvcopy = (@ARGV);
$numargs = @ARGV;
for ($x = 0; $x < $numargs; $x = $x + 1) {
    $arg = $ARGV[$x];
    if ($arg =~ /-nox/) {
	$dox = 0;
    } elsif ($arg =~ /-nogs/) {
	$dogs = 0;
    } elsif ($arg =~ /-silent/) {
	$silent = 1;
    } elsif ($arg =~ /-samples/) {
	$samples = 1;
    } else {
	die("Usage: $0 [-silent] [-nox] [-nogs] [-samples]\n");
    }
    if ((! $dox) && (! $dogs)) {
	die("$0: Nothing to do!\n");
    }
}

# Open logfile
open(LOG, ">type1inst.log") || die "cannot open type1inst.log";

# Setup directory for font samples
if ($samples) {
    if (! -e "samples") {
	# Create directory for sample text PS files
	print LOG "Creating directory for samples ...\n";
	system("mkdir samples");
	system("chmod a+rx samples");
	
    } elsif (-f "samples") {
	die("$0: remove file \"samples\" or do not use -samples option\n");
    } else {
	print LOG "Clearing samples directory\n";
	system("rm -f samples/*.ps");

	$height = 720;
	$allsample = "samples/allfonts.ps";
	open(ALLSAMPLE, ">$allsample") || 
	die("Couldn't open $allsample\n");
	print ALLSAMPLE "%!\n";
    }
}


# Counts how many fonts we come across
$numpffonts = 0;
$numgsfonts = 0;
$nofoundry = 0;
$badfonts = 0;
$numskipped = 0;

if ($dox) {
    %fs = &read_fonts_scale();
}
if ($dogs) {
    %fm = &read_fontmap();
}

# Process ASCII PS fonts
foreach $filename (<*.pfa>) {
    if (!(($fs{$filename}) && ($fm{$filename}))) {
	($x, $gs) = &process_font($filename);
	%fs = &add_font_to_aarray($filename, $x, %fs);
	%fm = &add_font_to_aarray($filename, $gs, %fm);
    } else {
	$numpffonts = $numpffonts + 1;
	$numskipped = $numskipped + 1;
	&print_progress();
    }
    if ($samples) {
	&font_sample($filename, $fm{$filename}, $height);
    }
}

# Process binary PS fonts
foreach $filename (<*.pfb>) {
    if (!(($fs{$filename}) && ($fm{$filename}))) {
	system("pfbtops $filename > foo");
	($x, $gs) = &process_font("foo");
	system("rm foo");
	%fs = &add_font_to_aarray($filename, $x, %fs);
	%fm = &add_font_to_aarray($filename, $gs, %fm);
    } else {
	$numpffonts = $numpffonts + 1;
	$numskipped = $numskipped + 1;
	&print_progress();
    }
    if ($samples) {
	&font_sample($filename, $fm{$filename}, $height);
    }
}

# Process Ghostscript fonts
if ($dogs) {
    foreach $filename (<*.gsf>) {
	if (! $fm{$filename}) {
	    ($x, $gs) = &process_font($filename);
	    %fm = &add_font_to_aarray($filename, $gs, %fm);
	} else {
	    $numgsfonts = $numgsfonts + 1;
	    $numskipped = $numskipped + 1;
	    &print_progress();
	}
	if ($samples) {
	    &font_sample($filename, $fm{$filename}, $height);
	}
    }
}

if ($dox) {
    &write_fonts_scale(%fs);
}
if ($dogs) {
    &write_fontmap(%fm);
}

# Generate fonts.dir
if ($dox) {
    system("mkfontdir");
    system("chmod a+r fonts.dir");
}

# Finish up the all font sample file
if ($samples) {
    print LOG "Finished font sample files\n";
    if ($height < 720) {
        print ALLSAMPLE "showpage\n";
    }
    close(ALLSAMPLE);
    system("chmod a+r $allsample");
}

# Report
if (! $silent) {
    $totalfonts = $numpffonts + $numgsfonts + $badfonts;

    # List statistics
    print "-------------------------------------------------------\n";
    if ($totalfonts == 0) {
	print "No fonts were found in this directory\n";
    } elsif ($totalfonts == 1) {
	print "1 font was found in this directory\n";
    } else {
	print "$totalfonts fonts found\n";
    }
    if ($numpffonts == 1) {
	print "1 was a PostScript font\n";
    } elsif ($numpffonts > 1) {
	print "$numpffonts were standard PostScript fonts\n";
    }
    if ($numgsfonts == 1) {
	print "1 was a Ghostscript font\n";
    } elsif ($numgsfonts > 1) {
	print "$numgsfonts were Ghostscript fonts\n";
    }
    if ($numskipped == 1) {
	print "\n";
	print "I skipped one of these fonts because it already had\n";
	print "an overriding entry in both fonts.scale and Fontmap\n";
	print "(PostScript font) or just Fontmap (Ghostscript font).\n";
    } elsif ($numskipped > 1) {
	print "\n";
	print "I skipped $numskipped of these fonts because they already\n";
	print "had overriding entries in both fonts.scale and Fontmap\n";
	print "(PostScript fonts) or just Fontmap (Ghostscript fonts).\n";
    }

    # Print error messages
    $wereerrors = 0;
    if ($badfonts > 0) {
	$wereerrors = 1;
	print "-------------------------------------------------------\n";
	if ($badfonts == 1) {
	    print "I couldn't extract a font name for 1 font in\n";
	} else {
	    print "I couldn't extract font names for $ badfonts fonts in\n";
	}
	print "this directory.  This means the font file had a non-standard\n";
	print "format which this program doesn't know about or cannot do\n";
	print "anything with.  Check the README file to find out more.\n";
    }
    if ($dox) {
	if ($nofoundry > 0) {
	    $wereerrors = 1;
	    print "-------------------------------------------------------\n";
	    print "For $nofoundry of these I couldn't figure out which foundry\n";
	    print "the font is from.  Thus, these fonts will appear under the\n";
	    print "foundry unknown, i.e. X font name -unknown-*.\n";
	    print "Please consult the README file to see what this means.\n";
	}
    }

    if ($wereerrors) {
	print "-------------------------------------------------------\n";
	print "\n";
	print "A log of errors is located in the file \"type1inst.log\"\n";
	print "\n";
    }
}
