#!/usr/bin/perl
# 
# Copyright (C) Koji Nakamaru
#
# Author: Koji Nakamaru (nakamaru at gmail.com)
# Modified: Apr 30 2005
#   * changed the contact information.
# Modified: May 13 2002
# Created/Modified: Apr 19 2002
# Keywords: xdvi, ghostscript, metapost
#
# This file is part of xdvigs.
#
# 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs; see the file COPYING.  If not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

$xdvigs = '/usr/local/tex/lib/xdvigs';

open FO, "|/usr/bin/gs -I$xdvigs @ARGV";
$old = select(FO); $| = 1; select($old);
while (<STDIN>) {
    $line = $_;
    if (/^\(([^)]+)\).*run/) {
	$name = $1;
	print FO "{\n";
	print FO "/fshow {exch findfont exch scalefont setfont show} bind def\n";
	open FI, "<$name";
	while (<FI>) {
	    if (/^%.*/) {
		s+^%\*Font: ([A-z][A-z0-9]*) .*$+/$1 /$1 def+;
		print FO;
	    } elsif (/^[^%]/) {
		last;
	    }
	}
	close FI;
	print FO $line;
	print FO "} stopped { (failed to render $name) = } if\n"
    } else {
	print FO $line;
    }
}
close FO;
