#!/usr/bin/perl5.6.1
# $Id: addttfont,v 1.11 2002/11/18 02:52:44 taoka Exp $

# option for mkres.pl
$MKRES_OPTION = "";
## Notice: CIDFnmap.jp for Ghostscript is not suppoted Oblique style
## $MKRES_OPTION = "-I";

# option for mkttfdir
$MKTTFDIR_OPTION = "-j";
##
##$MKTTFDIR_OPTION = "-j -o";

$LOCAL_BASE = "/usr/local";
$X11_BASE = "/usr/X11R6";
$GS_CIDFNMAP = $LOCAL_BASE . "/share/ghostscript/7.05/lib/CIDFnmap.jp";
$VFONTCAP = $LOCAL_BASE . "/share/VFlib/2.25.6/vfontcap";
$XTT_FONT_DIR = "./";
$TGIF_XDEF = $X11_BASE . "/lib/X11/ja_JP.eucJP/app-defaults/Tgif";
# For Ghostscript with VFlib
$GS_VTPSFONT_DIR = $LOCAL_BASE . "/share/ghostscript/VTPSfont";
$GS_FONTMAP = $LOCAL_BASE . "/share/ghostscript/5.50vflib/Fontmap";

$START = "AUTO_ADD_TT_BY_addttfont";

require 'getopts.pl';

Getopts('hVR:G:t:c:v:g:p:');

die "Usage: $0 [-h] [-R font_file] [-G font_file]
            [-t /some/where/Tgif] [-v /some/where/vfontcap]
            [-c /some/where/CIDFnmap.jp] [-p /some/where ]

    -h: show this help
    -R font_file: a truetype font file for Ryumin-Light
    -G font_file: a truetype font file for GothicBBB-Medium
    -t file: X resource file for tgif with the directory
             (default $TGIF_XDEF)
    -c file: Ghostscript's file with the directory
             (default $GS_CIDFNMAP)
    -v file: file of VFlib with the directory
             (default $VFONTCAP)

    -V: make resources for Ghostscript with VFlib instead of CIDFnmap.jp
    -p directory: direcrtory for virtual PS fonts produced by VFlib
             (default $GS_VTPSFONT_DIR)
    -g file: file of Ghostscript with VFlib with the directory
             (default $GS_FONTMAP)
" if ($opt_h);

$TGIF_XDEF = $opt_t if defined $opt_t;
$VFONTCAP = $opt_v if defined $opt_v;
$GS_CIDFNMAP = $opt_c if defined $opt_c;
$GS_FONTMAP = $opt_g if defined $opt_g;
$GS_VTPSFONT_DIR = $opt_p if defined $opt_p;

# Fonts of Ryumin-Light and GothicBBB-Medium
if ( defined $opt_R && ! -f $opt_R ){
  print "I cannot find \"$opt_R\"\n";
  exit 0;
}
if ( defined $opt_G && ! -f $opt_G ){
  print "I cannot find $opt_G\n";
  exit 0;
}
# get previous Ryumin and Gothic font names from vfontcap
if (! defined $opt_R){
  open(VFC, "cat $VFONTCAP |");
  foreach $lines (grep { /min:fc=.*: \#$START/ } <VFC>){
    chop $lines;
    $RYUMIN=$lines;
    $RYUMIN=~ s/min:fc=(.*): \#$START/$1/;
  }
}
else{
  $RYUMIN = $opt_R;
  $RYUMIN =~ s/\.tt[fc]$//;
}
if (! defined $opt_G){
  open(VFC, "cat $VFONTCAP |");
  foreach $lines (grep { /goth:fc=.*: \#$START/ } <VFC>){
    chop $lines;
    $GOTH=$lines;
    $GOTH=~ s/goth:fc=(.*): \#$START/$1/;
  }
}
else{
  $GOTH = $opt_G;
  $GOTH =~ s/\.tt[fc]$//;
}

$dif = $ARGV[0];

######################################################################
# X Window System
open(MKTTFDIR, "mkttfdir -d $MKTTFDIR_OPTION $dir |") ||
    die "Couldn't run mkttfdir $! \n";
foreach (<MKTTFDIR>){
    chop;
    s/^[0-9]+$//;  # number of fonts in fonts.dir
    next if ($_ eq "" || !/.*jisx0208.1983-0$/); # Now only jisx0208.1983 fonts
    /^([^\s]+) (.+)$/;
    $filename = $1;
    $xled = $2;
    $filename =~ s/^.*[:](.*\.tt[fc])/$1/;
    $basename = $filename;
    $basename =~ s/\.tt[fc]//;
    $_ = $xled;
    ($dummy, $foundry, $family_name, $weight_name, $slant,
     $setwidth_name, $add_style_name) = split('-');
#    $weight_name = ucfirst $weight_name;
    $nb_family_name = $family_name;
    $nb_family_name =~ s/\s+//g; # no blank
    $ttf_id = "$family_name-$weight_name";
    $nb_ttf_id = "$nb_family_name-$weight_name";
    #####
    $X_Font_Ryumin = "-$foundry-$family_name-$weight_name-$slant" if defined $RYUMIN && ($RYUMIN eq $basename);
    $GS_Font_Ryumin = "$nb_ttf_id" if defined $RYUMIN && ($RYUMIN eq $basename);
    $X_Font_Gothic = "-$foundry-$family_name-$weight_name-$slant" if defined $GOTH && ($GOTH eq $basename);
    $GS_Font_Gothic = "$nb_ttf_id" if defined $GOTH && ($GOTH eq $basename);
    #####
}

######################################################################
# Ghostscript

## Ghostscript CIDFnmap.jp
unless (defined $opt_V) {
    print "#################################################\n";
    print "Now adding Entries to CIDFnmap.jp for Ghostscript ....\n";
    $BEGIN_ADD_MESSAGE = "%" . $START;
    $END_ADD_MESSAGE = "%END_" . $START;
    system "sed '/$BEGIN_ADD_MESSAGE/,/$END_ADD_MESSAGE/d' $GS_CIDFNMAP > tmp_CIDFnmap";
    system "echo $BEGIN_ADD_MESSAGE >> tmp_CIDFnmap";
    if (defined $RYUMIN) {
	$res_ryumin = "/Adobe-Japan1           /$GS_Font_Ryumin ;
/Ryumin-Light           /$GS_Font_Ryumin ;
/Ryumin-Light-H         /$GS_Font_Ryumin ;
/Ryumin-Light-V         /$GS_Font_Ryumin ;
/HeiseiMin-W3H          /$GS_Font_Ryumin ;
/FutoMinA101-Bold       /$GS_Font_Ryumin ;
/FutoMinA101-Bold-H     /$GS_Font_Ryumin ;
/FutoMinA101-Bold-V     /$GS_Font_Ryumin ;";
	system "echo \"$res_ryumin\" >>  tmp_CIDFnmap";
    }
    if (defined $GOTH) {
	$res_goth = "/GothicBBB-Medium       /$GS_Font_Gothic ;
/GothicBBB-Medium-H     /$GS_Font_Gothic ;
/GothicBBB-Medium-V     /$GS_Font_Gothic ;
/HeiseiKakuGo-W5H       /$GS_Font_Gothic ;
/FutoGoB101-Bold        /$GS_Font_Gothic ;
/FutoGoB101-Bold-H      /$GS_Font_Gothic ;
/FutoGoB101-Bold-V      /$GS_Font_Gothic ;";
	system "echo \"$res_goth\" >>  tmp_CIDFnmap";
    }
    system "mkres.pl $MKRES_OPTION -f CIDFnmap.jp `pwd` >> tmp_CIDFnmap";
    system "echo $END_ADD_MESSAGE >> tmp_CIDFnmap";
    system "mv tmp_CIDFnmap $GS_CIDFNMAP";
    print "done\n\n";
}
else{
## Ghostscript with VFlib
    print "#################################################\n";
    print "Now making Virtual PS fonts for Ghostscript ....\n";
    $BEGIN_ADD_MESSAGE = "%" . $START;
    $END_ADD_MESSAGE = "%END_" . $START;
    system "sed '/$BEGIN_ADD_MESSAGE/,/$END_ADD_MESSAGE/d' $GS_FONTMAP > tmp_Fontmap";
    system "echo $BEGIN_ADD_MESSAGE >> tmp_Fontmap";

    $UniqueID=4300000;
    open(MKTTFDIR, "mkttfdir -d $MKTTFDIR_OPTION $dir |") ||
	die "Couldn't run mkttfdir $! \n";
    foreach (<MKTTFDIR>){
	chop;
	s/^[0-9]+$//;  # number of fonts in fonts.dir
	next if ($_ eq "" || !/.*jisx0208.1983-0$/); # Now only jisx0208.1983 fonts
	/^([^\s]+) (.+)$/;
	$filename = $1;
	$xled = $2;
	$filename =~ s/^.*[:](.*\.tt[fc])/$1/;
	$basename = $filename;
	$basename =~ s/\.tt[fc]//;
	$_ = $xled;
	($dummy, $foundry, $family_name, $weight_name, $slant,
	 $setwidth_name, $add_style_name) = split('-');
#    $weight_name = ucfirst $weight_name;
	$nb_family_name = $family_name;
	$nb_family_name =~ s/\s+//g; # no blank
	$ttf_id = "$family_name-$weight_name";
	$nb_ttf_id = "$nb_family_name-$weight_name";
#####
	$X_Font_Ryumin = "-$foundry-$family_name-$weight_name-$slant" if defined $RYUMIN && ($RYUMIN eq $basename);
	$X_Font_Gothic = "-$foundry-$family_name-$weight_name-$slant" if defined $GOTH && ($GOTH eq $basename);
#####
print "VTPSfont $GS_VTPSFONT_DIR $nb_ttf_id $nb_ttf_id $basename tmp_Fontmap $UniqueID";
	$UniqueID=`VTPSfont $GS_VTPSFONT_DIR $nb_ttf_id $nb_ttf_id $basename tmp_Fontmap $UniqueID`;
    }
    system "echo $END_ADD_MESSAGE >> tmp_Fontmap";
    system "mv tmp_Fontmap $GS_FONTMAP";
    print "done\n\n";
}

######################################################################
# VFlib
print "#################################################\n";
print "Now adding Entries to vfontcap for VFlib ....\n";
$BEGIN_ADD_MESSAGE = '\#' . $START;
$END_ADD_MESSAGE = '\#END_' . $START;
system "sed '/$BEGIN_ADD_MESSAGE/,/$END_ADD_MESSAGE/d' $VFONTCAP > org_vfontcap";
system "echo $BEGIN_ADD_MESSAGE > tmp_vfontcap";
system "echo \"min:fc=$RYUMIN: #$START\" >>  tmp_vfontcap" if defined $RYUMIN;
system "echo \"goth:fc=$GOTH: #$START\" >>  tmp_vfontcap" if defined $GOTH;
system "mkres.pl $MKRES_OPTION -f vfontcap `pwd` >> tmp_vfontcap";
system "echo $END_ADD_MESSAGE >> tmp_vfontcap";
system "cat tmp_vfontcap org_vfontcap > $VFONTCAP";
system "rm tmp_vfontcap org_vfontcap";
print "done\n\n";


######################################################################
# Tgif
print "#################################################\n";
print "Now adding Entries to Tgif for tgif ....\n";
$BEGIN_ADD_MESSAGE = '\!' . $START;
$END_ADD_MESSAGE = '\!END_' . $START;
$MKRES_OPTION = $MKRES_OPTION . "-R \"$X_Font_Ryumin\" " if defined $RYUMIN;
$MKRES_OPTION = $MKRES_OPTION . "-G \"$X_Font_Gothic\" " if defined $GOTH;
system "sed '/$BEGIN_ADD_MESSAGE/,/$END_ADD_MESSAGE/d' $TGIF_XDEF > tmp_Tgif";
system "echo $BEGIN_ADD_MESSAGE >> tmp_Tgif";
system "mkres.pl $MKRES_OPTION -f tgif >> tmp_Tgif";
system "echo $END_ADD_MESSAGE >> tmp_Tgif";
system "mv tmp_Tgif $TGIF_XDEF";
print "done\n\n";

######################################################################
# X-TT
print "#################################################\n";
print "Now doing mkttfdir ....\n";
system "mkttfdir $MKTTFDIR_OPTION";
print "done\n";
