#!/usr/bin/perl -w
#
#	gropdf		: PDF post processor for groff
#
# Copyright (C) 2011-2014  Free Software Foundation, Inc.
#      Written by Deri James <deri@chuzzlewit.demon.co.uk>
#
# This file is part of groff.
#
# groff 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 3 of the License, or
# (at your option) any later version.
#
# groff 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 this program. If not, see <http://www.gnu.org/licenses/>.

use strict;
use Getopt::Long qw(:config bundling);
use Compress::Zlib;

my %cfg;

$cfg{GROFF_VERSION}='1.22.3';
$cfg{GROFF_FONT_PATH}='/usr/share/groff/site-font:/usr/share/groff/1.22.3/font:/usr/lib/font';
$cfg{RT_SEP}=':';
binmode(STDOUT);

my @obj;	# Array of PDF objects
my $objct=0;	# Count of Objects
my $fct=0;	# Output count
my %fnt;	# Used fonts
my $lct=0;	# Input Line Count
my $src_name='';
my %env;	# Current environment
my %fontlst;	# Fonts Loaded
my $rot=0;	# Portrait
my %desc;	# Contents of DESC
my %download;	# Contents of downlopad file
my $pages;	# Pointer to /Pages object
my $devnm='devpdf';
my $cpage;	# Pointer to current pages
my $cpageno=0;	# Object no of current page
my $cat;	# Pointer to catalogue
my $dests;	# Pointer to Dests
my @mediabox=(0,0,595,842);
my @defaultmb=(0,0,595,842);
my $stream='';	# Current Text/Graphics stream
my $cftsz=10;	# Current font sz
my $cft;	# Current Font
my $lwidth=1;	# current linewidth
my $linecap=1;
my $linejoin=1;
my $textcol='';	# Current groff text
my $fillcol='';	# Current groff fill
my $curfill='';	# Current PDF fill
my $strkcol='';
my $curstrk='';
my @lin=();	# Array holding current line of text
my @ahead=();	# Buffer used to hol the next line
my $mode='g';	# Graphic (g) or Text (t) mode;
my $xpos=0;	# Current X position
my $ypos=0;	# Current Y position
my $tmxpos=0;
my $kernadjust=0;
my $curkern=0;
my $widtbl;	# Pointer to width table for current font size
my $origwidtbl; # Pointer to width table
my $krntbl;	# Pointer to kern table
my $matrix="1 0 0 1";
my $whtsz;	# Current width of a space
my $poschg=0;	# V/H pending
my $fontchg=0;	# font change pending
my $tnum=2;	# flatness of B-Spline curve
my $tden=3;	# flatness of B-Spline curve
my $linewidth=40;
my $w_flg=0;
my $nomove=0;
my $pendmv=0;
my $gotT=0;
my $suppress=0;	# Suppress processing?
my %incfil;	# Included Files
my @outlev=([0,undef,0,0]);	# Structure pdfmark /OUT entries
my $curoutlev=\@outlev;
my $curoutlevno=0;	# Growth point for @curoutlev
my $Foundry='';
my $xrev=0;	# Reverse x direction of font
my $matrixchg=0;
my $wt=-1;
my $thislev=1;
my $mark=undef;
my $suspendmark=undef;
my $n_flg=1;
my $pginsert=-1;    # Growth point for kids array
my %pgnames;        # 'names' of pages for switchtopage
my @outlines=();    # State of Bookmark Outlines at end of each page
my $custompaper=0;  # Has there been an X papersize
my $textenccmap=''; # CMap for groff text.enc encoding

my %ppsz=(	'ledger'=>[1224,792],
	'legal'=>[612,1008],
	'letter'=>[612,792],
	'a0'=>[2384,3370],
	'a1'=>[1684,2384],
	'a2'=>[1191,1684],
	'a3'=>[842,1191],
	'a4'=>[595,842],
	'a5'=>[420,595],
	'a6'=>[297,420],
	'a7'=>[210,297],
	'a8'=>[148,210],
	'a9'=>[105,148],
	'a10'=>[73,105],
	'isob0'=>[2835,4008],
	'isob1'=>[2004,2835],
	'isob2'=>[1417,2004],
	'isob3'=>[1001,1417],
	'isob4'=>[709,1001],
	'isob5'=>[499,709],
	'isob6'=>[354,499],
	'c0'=>[2599,3677],
	'c1'=>[1837,2599],
	'c2'=>[1298,1837],
	'c3'=>[918,1298],
	'c4'=>[649,918],
	'c5'=>[459,649],
	'c6'=>[323,459] );

my $ucmap=<<'EOF';
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (UCS)
/Supplement 0
>> def
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
2 beginbfrange
<008b> <008f> [<00660066> <00660069> <0066006c> <006600660069> <00660066006C>]
<00ad> <00ad> <002d>
endbfrange
endcmap
CMapName currentdict /CMap defineresource pop
end
end
EOF

my $fd;
my $frot;
my $fpsz;
my $embedall=0;
my $debug=0;
my $version=0;
my $stats=0;
my $unicodemap;

#Load_Config();

GetOptions("F=s" => \$fd, 'l' => \$frot, 'p=s' => \$fpsz, 'd!' => \$debug, 'v' => \$version, 'e' => \$embedall, 'y=s' => \$Foundry, 's' => \$stats, 'u:s' => \$unicodemap);

if ($version)
{
    print "GNU gropdf (groff) version $cfg{GROFF_VERSION}\n";
    exit;
}

if (defined($unicodemap))
{
    if ($unicodemap eq '')
    {
	$ucmap='';
    }
    elsif (-r $unicodemap)
    {
	local $/;
	open(F,"<$unicodemap") or die "gropdf: Failed to open '$unicodemap'";
	($ucmap)=(<F>);
	close(F);
    }
    else
    {
	Msg(0,"Failed to find '$unicodemap' - ignoring");
    }
}

# Search for 'font directory': paths in -f opt, shell var GROFF_FONT_PATH, default paths

my $fontdir=$cfg{GROFF_FONT_PATH};
$fontdir=$ENV{GROFF_FONT_PATH}.$cfg{RT_SEP}.$fontdir if exists($ENV{GROFF_FONT_PATH});
$fontdir=$fd.$cfg{RT_SEP}.$fontdir if defined($fd);

$rot=90 if $frot;
$matrix="0 1 -1 0" if $frot;

LoadDownload();
LoadDesc();

my $unitwidth=$desc{unitwidth};
my $papersz=$desc{papersize};
$papersz=lc($fpsz) if $fpsz;

$env{FontHT}=0;
$env{FontSlant}=0;
MakeMatrix();

if (substr($papersz,0,1) eq '/' and -r $papersz)
{
    if (open(P,"<$papersz"))
    {
	while (<P>)
	{
	    chomp;
	    s/# .*//;
	    next if $_ eq '';
	    $papersz=$_;
	    last
	}

	close(P);
    }
}

if ($papersz=~m/([\d.]+)([cipP]),([\d.]+)([cipP])/)
{
    @defaultmb=@mediabox=(0,0,ToPoints($3,$4),ToPoints($1,$2));
}
elsif (exists($ppsz{$papersz}))
{
    @defaultmb=@mediabox=(0,0,$ppsz{$papersz}->[0],$ppsz{$papersz}->[1]);
}

my (@dt)=gmtime($ENV{SOURCE_DATE_EPOCH} || time);
my $dt=PDFDate(\@dt);

my %info=('Creator' => "(groff version $cfg{GROFF_VERSION})",
				'Producer' => "(gropdf version $cfg{GROFF_VERSION})",
				'ModDate' => "($dt)",
				'CreationDate' => "($dt)");

while (<>)
{
    chomp;
    s/\r$//;
    $lct++;

    do 	# The ahead buffer behaves like 'ungetc'
    {{
	if (scalar(@ahead))
	{
	    $_=shift(@ahead);
	}


	my $cmd=substr($_,0,1);
	next if $cmd eq '#';	# just a comment
	my $lin=substr($_,1);

	while ($cmd eq 'w')
	{
	    $cmd=substr($lin,0,1);
	    $lin=substr($lin,1);
	    $w_flg=1 if $gotT;
	}

	$lin=~s/^\s+//;
#		$lin=~s/\s#.*?$//;	# remove comment
	$stream.="\% $_\n" if $debug;

	do_x($lin),next if ($cmd eq 'x');
	next if $suppress;
	do_p($lin),next if ($cmd eq 'p');
	do_f($lin),next if ($cmd eq 'f');
	do_s($lin),next if ($cmd eq 's');
	do_m($lin),next if ($cmd eq 'm');
	do_D($lin),next if ($cmd eq 'D');
	do_V($lin),next if ($cmd eq 'V');
	do_v($lin),next if ($cmd eq 'v');
	do_t($lin),next if ($cmd eq 't');
	do_u($lin),next if ($cmd eq 'u');
	do_C($lin),next if ($cmd eq 'C');
	do_c($lin),next if ($cmd eq 'c');
	do_N($lin),next if ($cmd eq 'N');
	do_h($lin),next if ($cmd eq 'h');
	do_H($lin),next if ($cmd eq 'H');
	do_n($lin),next if ($cmd eq 'n');

	my $tmp=scalar(@ahead);
    }} until scalar(@ahead) == 0;

}


if ($cpageno > 0)
{
    $cpage->{MediaBox}=\@mediabox if $custompaper;
    PutObj($cpageno);
    OutStream($cpageno+1);
}


PutOutlines(\@outlev);

PutObj(1);

my $info=BuildObj(++$objct,\%info);

PutObj($objct);

foreach my $fontno (sort keys %fontlst)
{
    my $o=$fontlst{$fontno}->{FNT};
    my $p=GetObj($fontlst{$fontno}->{OBJ});

    if (exists($p->{LastChar}) and $p->{LastChar} > 255)
    {
	$p->{LastChar} = 255;
	splice(@{$o->{GNO}},256);
	splice(@{$o->{WID}},256);
    }
}

foreach my $o (3..$objct)
{
    PutObj($o) if (!exists($obj[$o]->{XREF}));
}

#my $encrypt=BuildObj(++$objct,{'Filter' => '/Standard', 'V' => 1, 'R' => 2, 'P' => 252});
#PutObj($objct);
PutObj(2);

my $xrefct=$fct;

$objct+=1;
print "xref\n0 $objct\n0000000000 65535 f \n";

foreach my $xr (@obj)
{
    next if !defined($xr);
    printf("%010d 00000 n \n",$xr->{XREF});
}

print "trailer\n<<\n/Info $info\n/Root 1 0 R\n/Size $objct\n>>\nstartxref\n$fct\n\%\%EOF\n";
print "\% Pages=$pages->{Count}\n" if $stats;


sub MakeMatrix
{
    my $fontxrev=shift||0;
    my @mat=($frot)?(0,1,-1,0):(1,0,0,1);

    if (!$frot)
    {
	if ($env{FontHT} != 0)
	{
	    $mat[3]=sprintf('%.3f',$env{FontHT}/$cftsz);
	}

	if ($env{FontSlant} != 0)
	{
	    my $slant=$env{FontSlant};
	    $slant*=$env{FontHT}/$cftsz if $env{FontHT} != 0;
	    my $ang=rad($slant);

	    $mat[2]=sprintf('%.3f',sin($ang)/cos($ang));
	}

	if ($fontxrev)
	{
	    $mat[0]=-$mat[0];
	}
    }

    $matrix=join(' ',@mat);
    $matrixchg=1;
}

sub PutOutlines
{
    my $o=shift;
    my $outlines;

    if ($#{$o} > 0)
    {
	# We've got Outlines to deal with
	my $openct=$curoutlev->[0]->[2];

	while ($thislev-- > 1)
	{
	    my $nxtoutlev=$curoutlev->[0]->[1];
	    $nxtoutlev->[0]->[2]+=$openct if $curoutlev->[0]->[3]==1;
	    $openct=0 if $nxtoutlev->[0]->[3]==-1;
	    $curoutlev=$nxtoutlev;
	}

	$cat->{Outlines}=BuildObj(++$objct,{'Count' => abs($o->[0]->[0])+$o->[0]->[2]});
	$outlines=$obj[$objct]->{DATA};
    }
    else
    {
	return;
    }

    SetOutObj($o);

    $outlines->{First}=$o->[1]->[2];
    $outlines->{Last}=$o->[$#{$o}]->[2];

    LinkOutObj($o,$cat->{Outlines});
}

sub SetOutObj
{
    my $o=shift;

    for my $j (1..$#{$o})
    {
	my $ono=BuildObj(++$objct,$o->[$j]->[0]);
	$o->[$j]->[2]=$ono;

	SetOutObj($o->[$j]->[1]) if $#{$o->[$j]->[1]} > -1;
    }
}

sub LinkOutObj
{
    my $o=shift;
    my $parent=shift;

    for my $j (1..$#{$o})
    {
	my $op=GetObj($o->[$j]->[2]);

	$op->{Next}=$o->[$j+1]->[2] if ($j < $#{$o});
	$op->{Prev}=$o->[$j-1]->[2] if ($j > 1);
	$op->{Parent}=$parent;

	if ($#{$o->[$j]->[1]} > -1)
	{
	    $op->{Count}=$o->[$j]->[1]->[0]->[2]*$o->[$j]->[1]->[0]->[3];# if exists($op->{Count}) and $op->{Count} > 0;
	    $op->{First}=$o->[$j]->[1]->[1]->[2];
	    $op->{Last}=$o->[$j]->[1]->[$#{$o->[$j]->[1]}]->[2];
	    LinkOutObj($o->[$j]->[1],$o->[$j]->[2]);
	}
    }
}

sub GetObj
{
    my $ono=shift;
    ($ono)=split(' ',$ono);
    return($obj[$ono]->{DATA});
}



sub PDFDate
{
    my $dt=shift;
    return(sprintf("D:%04d%02d%02d%02d%02d%02d+00'00'",$dt->[5]+1900,$dt->[4]+1,$dt->[3],$dt->[2],$dt->[1],$dt->[0]));
}

sub ToPoints
{
    my $num=shift;
    my $unit=shift;

    if ($unit eq 'i')
    {
	return($num*72);
    }
    elsif ($unit eq 'c')
    {
	return int($num*72/2.54);
    }
    elsif ($unit eq 'm')	# millimetres
    {
	return int($num*72/25.4);
    }
    elsif ($unit eq 'p')
    {
	return($num);
    }
    elsif ($unit eq 'P')
    {
	return($num*6);
    }
    elsif ($unit eq 'z')
    {
	return($num/$unitwidth);
    }
    else
    {
	Msg(1,"Unknown scaling factor '$unit'");
    }
}

sub Load_Config
{
    open(CFG,"<gropdf_config") or die "Can't open config file: $!";

    while (<CFG>)
    {
	chomp;
	my ($key,$val)=split(/ ?= ?/);

	$cfg{$key}=$val;
    }

    close(CFG);
}

sub LoadDownload
{
    my $f;

    OpenFile(\$f,$fontdir,"download");
    Msg(1,"Failed to open 'download'") if !defined($f);

    while (<$f>)
    {
	chomp;
	s/#.*$//;
	next if $_ eq '';
	my ($foundry,$name,$file)=split(/\t+/);
	if (substr($file,0,1) eq '*')
	{
	    next if !$embedall;
	    $file=substr($file,1);
	}

	$download{"$foundry $name"}=$file;
    }

    close($f);
}

sub OpenFile
{
    my $f=shift;
    my $dirs=shift;
    my $fnm=shift;

    if (substr($fnm,0,1)  eq '/' or substr($fnm,1,1) eq ':') # dos
    {
	return if -r "$fnm" and open($$f,"<$fnm");
    }

    my (@dirs)=split($cfg{RT_SEP},$dirs);

    foreach my $dir (@dirs)
    {
	last if -r "$dir/$devnm/$fnm" and open($$f,"<$dir/$devnm/$fnm");
    }
}

sub LoadDesc
{
    my $f;

    OpenFile(\$f,$fontdir,"DESC");
    Msg(1,"Failed to open 'DESC'") if !defined($f);

    while (<$f>)
    {
	chomp;
	s/#.*$//;
	next if $_ eq '';
	my ($name,$prms)=split(' ',$_,2);
	$desc{lc($name)}=$prms;
    }

    close($f);
}

sub rad  { $_[0]*3.14159/180 }

my $InPicRotate=0;

sub do_x
{
    my $l=shift;
    my ($xcmd,@xprm)=split(' ',$l);
    $xcmd=substr($xcmd,0,1);

    if ($xcmd eq 'T')
    {
	Msg(0,"Expecting a pdf pipe (got $xprm[0])") if $xprm[0] ne substr($devnm,3);
    }
    elsif ($xcmd eq 'f')	# Register Font
    {
	$xprm[1]="${Foundry}-$xprm[1]" if $Foundry ne '';
	LoadFont($xprm[0],$xprm[1]);
    }
    elsif ($xcmd eq 'F')	# Source File (for errors)
    {
	$env{SourceFile}=$xprm[0];
    }
    elsif ($xcmd eq 'H')	# FontHT
    {
	$xprm[0]/=$unitwidth;
	$xprm[0]=0 if $xprm[0] == $cftsz;
	$env{FontHT}=$xprm[0];
	MakeMatrix();
    }
    elsif ($xcmd eq 'S')	# FontSlant
    {
	$env{FontSlant}=$xprm[0];
	MakeMatrix();
    }
    elsif ($xcmd eq 'i')	# Initialise
    {
	if ($objct == 0)
	{
	    $objct++;
	    @defaultmb=@mediabox;
	    BuildObj($objct,{'Pages' => BuildObj($objct+1,
				{'Kids' => [],
				'Count' => 0,
				'Type' => '/Pages',
				'Rotate' => $rot,
				'MediaBox' => \@defaultmb,
				'Resources' =>
				    {'Font' => {},
				    'ProcSet' => ['/PDF', '/Text', '/ImageB', '/ImageC', '/ImageI']}
				}
				),
		'Type' =>  '/Catalog'});

	    $cat=$obj[$objct]->{DATA};
	    $objct++;
	    $pages=$obj[2]->{DATA};
	    Put("%PDF-1.4\n\x25\xe2\xe3\xcf\xd3\n");
	}
    }
    elsif ($xcmd eq 'X')
    {
	# There could be extended args
	do
	{{
	    LoadAhead(1);
	    if (substr($ahead[0],0,1) eq '+')
	    {
		$l.="\n".substr($ahead[0],1);
		shift(@ahead);
	    }
	}} until $#ahead==0;

	($xcmd,@xprm)=split(' ',$l);
	$xcmd=substr($xcmd,0,1);

	if ($xprm[0]=~m/^(.+:)(.+)/)
	{
	    splice(@xprm,1,0,$2);
	    $xprm[0]=$1;
	}

	my $par=join(' ',@xprm[1..$#xprm]);

	if ($xprm[0] eq 'ps:')
	{
	    if ($xprm[1] eq 'invis')
	    {
		$suppress=1;
	    }
	    elsif ($xprm[1] eq 'endinvis')
	    {
		$suppress=0;
	    }
	    elsif ($par=~m/exec gsave currentpoint 2 copy translate (.+) rotate neg exch neg exch translate/)
	    {
		# This is added by gpic to rotate a single object

		my $theta=-rad($1);

		IsGraphic();
		my ($curangle,$hyp)=RtoP($xpos,GraphY($ypos));
		my ($x,$y)=PtoR($theta+$curangle,$hyp);
		$stream.="q\n".sprintf("%.3f %.3f %.3f %.3f %.3f %.3f cm",cos($theta),sin($theta),-sin($theta),cos($theta),$xpos-$x,GraphY($ypos)-$y)."\n";
		$InPicRotate=1;
	    }
	    elsif ($par=~m/exec grestore/ and $InPicRotate)
	    {
		IsGraphic();
		$stream.="Q\n";
		$InPicRotate=0;
	    }
	    elsif ($par=~m/exec (\d) setlinejoin/)
	    {
		IsGraphic();
		$linejoin=$1;
		$stream.="$linejoin j\n";
	    }
	    elsif ($par=~m/exec (\d) setlinecap/)
	    {
		IsGraphic();
		$linecap=$1;
		$stream.="$linecap J\n";
	    }
	    elsif ($par=~m/\[(.+) pdfmark/)
	    {
		my $pdfmark=$1;
		$pdfmark=~s((\d{4,6}) u)(sprintf("%.1f",$1/$desc{sizescale}))eg;
		$pdfmark=~s(\\\[u00(..)\])(chr(hex($1)))eg;

		if ($pdfmark=~m/(.+) \/DOCINFO/)
		{
		    my @xwds=split(' ',"<< $1 >>,(q(\\\[u00(..)\])(chr])(chr]r(hex($1' ',"<< )k=~m/(.+) \/.0 1],$hr]r(hex
pace", 0 1],$|a   }
	    el'i')
    {R => j\n";
	O   }
	,curoutlev'i'.muine :mark=~min eq    }
	,curoutlev'i'.muin~min eq':uin~min ewq':uinmrxy=~m/uine :mark=~min eq  ";
	O  ("%PDq  ";
	O #inmrxy=~m/uine o}); y=~m/uine o}); y=~m/uine o}); y=~m/uine o}); y=~m/uine o}); y=~m/uine o}); y=~m/uiat[3]=d $@Ihr]r(hex(.1' ', $xr o}); y=~mne o}n1y=~m/u$1'e o}n1y=~currentpoe`11 $7B6", "62D4",
  " o}n1y=~currentghinvis'y=~m3]=d rentpon`1 $7B6",  "62D 
    {1y=~uine o y=~m/uine os/bineine o e os/bin1f",$~m/u$1n1y=~cuxr o}='1+(.uine   "2FA04]=d ru$1n1cuxr o}1y=~cuxrintr($file,0,1) e)0          N

    <{ !$embedals;
	    $file=substr($file,1);
	}

	$download{"$foundry $name"} eq 'P')
    {
	return($num*6);
    }
    elsif ($unit eq 'z')
    {
	return($$num*6);
    }
0,\/.0 2f ($ued{"$foundry $namlsif ($unit equm*6+,($ued{"$d{,1(' 'ile, eq  eq  eq  eq  eq  et  et  et  et  e[   eXZ*6)t equm*6/);
	t equm*6/#;
	t eq;P')
    {
retur<{ !$embe~e,7   elviffile=substr($file,1);
	}

	$download{"$foundry$objn1$97P}+$objcaf Objects
my $fct=0;	# Output count
my %fnt;	# Used fonts
my $N($fdirs)ount
my %f, '/Te5prefit
my   mthomp;
	my /jcaf Objects
my=,=1>mne');
	hsche!(;0)
	{
		# Use
	0 (substr($file,0e/#.*$//;
	nexte Hmy   dirspPunt
my %f, '/Te5prefit
my   mthomp;
	my /jcaf Objects
my=,=1>mne');
	hsche!(;0)
	{
		# Use
	0 (substr($file,0e/#.*$//;
	nexte Hmy   dirspPunt
my %f, '/Te5prefit
my   mthomp;
	my /jcaf Objects
my=,=1>mne');
	hsche!(;0)
	{
		# Use
	0 (substr($file,0e/#.*$//;
	nexte Hminter)
	{
		#'deal%6 mtho`\/.0 2ein1f",$~m/ut
my',0e/#.*r  st=shift||0:
     |0hift|'09
);
	ects
my=,=1,1);
PU$it
my   mthomp;1(ft|'01f",$~m/ut
msicen}e!(;0 e[ 5BE7",
  "FrspPunt
my 1.cfts
my=,=1,1)y',0e/#.*r  
my',0
print "x
	{
		#'deal%6 mthOpenF}bjecthem_N
PU$it
my  $/
    "macron  "Fr1.cfts
my1n($$f,"<0e/#~m/ut
msicm)=stmy %ff("%48cm)=stmy %ff("=stmy %ff("( \@def!(#,"<< $1' %ff("%48cm)=stmy %ff("=stmymy %ff("=rs)
    {
	$env{SourceFile}=$xprm[0]^F=stmy %fn("=rs)ymy %ff("=rs)
    {
	$env{SourceFile}=$xprm[0]^F=stmy %fn("=rs)ymy %ff("=rs)
    {
	$env{SourceFile}=$xprm[0]^F=stmy %fn("=rs)ymy %ff("=rs)
    {
	$env{SourceFile}=$xprm[0]^F=stmy %fn("=rs)ymy %ff("=rs)
    {
	$env{SourceFile}=$xprm[0]^F=stmy %fn("=rs)ymy %ff("=rs)
    {
	^F=stmy %fn("=)("=rs)
    {
	$env{SourceFile}=$xprm[0]^F=stmy %fn("=rs)]^F=stmy %fo]));
s)]^F=stmy'f %f]^F=ss)]^F=sd[2] iceFile}=$xpr);
s)]^F=rs)ymy %
	$download{ymy %
	$downops%
	$do	$strmy %fn("=rs)]ceFile}=)Gs)ymyi};
	:/ 2"=r>{Pr%
	$downopsrmy %fn("=rs)]ceH". ~();
L   m"=rs)]ceFifonts)
 %%
	$downopr    my $te{vrn($num*6r%
	$downopsrmy %fn("=rs)]ceH". ~();
L   m"=rs)]ceFifonts)
 %%
	$dowm();
L   m"=rs)]0".substr($ahead[".substp{.s|$do	$st0",.e", "337F",
  "cosquare",    my $te{vr intes)]ceFile}=,
	spe", "337F",
  "ioCE",
  "2F  my $te{vr intes)]c@File}=,
	spe", "337F",
  "ioCE",
  "2F
  "iF
,",
  "2F
  "iF
,",
  "2F
  "iF
,",
  "2F
  "iF
,",
  "2F
  "iF
,",
  "2F
"cosqu2F
)
{
 \
  "iF
,"  "iF
troff
	    }
	    elsetf("h14eFi
,",
nm,3);
    }
  1$outlines->{First}=$o->[1]->[2];
    $oi;
    $oi;
    $oE",   {    $oh  elsif ($xcmd eqr   $oi;
XoE", qu2F
)
{
14eFi
 8$60(r in[2];
 0)0(  {    i;
    $oE",   {    $oh  elsif ($xcmd eqr   $oi;
XoE", qu2F
)
{
14eFi
 8$60(r in[2];
 0)0(  {  $ i;
(   $oE"$   {8    $oh  elst ($pa\[(.(   $oE"]@   $oE"]@ 4oE"]@   $oE"]@ 15BT  ($ono10$ $"33BE",
  "lh my $jsp]@ P     return"lh my $js@Dzrop
    $oE", Kp
   hip]@ P  jsp]@ P 'F
  " $oE"]@ xec +4e P 'F
  " $oHip]@ P  r%
* "in}e!(;0 e[ 5BE7",
  "FrspPunt
my 1.cft xec +4e P 'F
  "
* "in}e  " $oH        ("=$oH     js@Dzrop
    $o* "in, "008,F\n"008(;0 e[$oH     js@Dzrot-0;
	x;0 e[   $in, "0Punt
+ $in, "0Pu1E5",
oH  #-H       0 e[(5BE7",
  "FrspPunt
my 1.cft xec +4e P 'F
  "
* "ioH   `lwsDRzrot-0;
	x;0 e[  %1in, "0Punt
+ $in, "0Pu1E5",
oH  #-H          unt
+   nt
+/$ the
	    #   firse   {nt
+   nte   7 ./ nte unt
+   ncoun       0 e[ 7 ./ e[ <  0 e[ 74eFi
          #   ncoun  firlinejp,  lo  nten, "0  }=,
	sli`lwsD^ nten     0 DF%.3f %"> <%.		$
+ nten, "ntp .not 0 e[ 7",
 'Type' <>    #   ncoun  firlioun   # 'unt
+[ 7",
 +[ unt
+   ncoun @7", # 'unt
@ xel +4e P'F
  " $PHip",
 +[ nnt
+     firliirlioun my   $o* "in,'|p",
 +[",
 +[ nnt
	F
  " $0,=~m/uine o}); y=~m/uine o});irlioun    #$' "in,'|p",
 +[",
 +[ nnt
	F
  " $0,=~m/uine o}); y=~m/uine o});ire
    {
	i
	F
  "0Xm  ncuine o});ire
     o}); y=4e P'});ire
ncuine o});ire
 ncuine o});ire
`dcire
     o}re
ncuine yo); y=~m/uio"e o}):2gall\ "in,js@Dzrot-0;
	xor|D); y=~m/uine oen connrt0)me=~m/u";

    while (<CFG>F\n"0o}):2gall\ "in,e~e,:);ire)me=~m/u";

 eq '*o rotatmu";

 eq '*/2uine oen connrt0 "in,e~e,:);ire)me=~m (<CFG>F\n"0o}):2gall\ "in,e~e,:);ire)me=~m/,";

 eq '*o rotatmu";

 eq '*/2uine oen connrt0 "in,e~e,:);ire)me=~m (<CFG>F\n"0o}):2gall\ "in,e~e,:);ire)me=~m/,";

 eq '*o rotatmu";

 eq '*/2uine oen connrt0 "in,e~e,:);ire)me=~m (<CFG>F\n"0o}):2gall\ "in,e~e,:);ire)me=~m/,";

 eq '*o rotatmu";

 eq '*/2uine oen cote)
	    tHT
    {
   tHT
    {
   tHTG>F\n=
   oen c{
   tHT
 2.;
	    $cu connrc0@in1eaTG>F\ eq '*/2uine   {
   tHTG8*
+[ 7".;
	    $cu+[)),.$pka6! eq  {
(tHTG>F\nap#+2(/tHTG>I $cu+[)),.$pka6! eq  { ,*o rot  $cu+[)),.$pka6! eq  {
(tHTG>}); h=;

 >{Fiot  $cx[)),.$p {
 "in); h=;>{Fioi  $cx[(),.$p {
 "ix); p {
+2(/tHTG>I,5$cx[)),/tHTG>I,5$cbh+2(, p {
+2($pka6! ,5$$y)=Pto=Pto=Pto=Pto=Pcp  CPto=Pcp cpif (  Pt  my $pif (  >{Fior h=;

  $pif
 {
 "in); h=;>te|ln|ee Pt  =;

  .)\])(c  $pif
 {
to=Pto=Pcp   =;

  .)\])*c  $pif
 {
to=Pto=Pcp   =;

  .)\])*c  $pG>F\n"0o}):2gal}o=Pcp   =;

 @{y^epG>F\n"])*c  $pG>F\n"0o}):2gal}o=Pcp   =;

 @{y^epG>F\n"])*c  $pG>F\n"0o}):2galot 0 e[ =\@mediahe[y^epG>F\n"] the
	 =;

 @{f
 @{y^e=;

 @{y^eedsan"0o})h-2((ot 0 e[ =\@mediahe[y^epG>F\n"] the
	 =;

 @{f
 @{y^e=;

 @{y^eedsan"0o})h-2((ot 0 e[ =\@mediahe[y^epG>F\n"] the
	 =;

 @{f
 @{y^e=;

 @{y^eedsan"0o})h-2((ot 0 e[ =\@mediahe[y^epG>F\n"] the
	 =;

 @{f
 @{y^e=;

 @{y^eedsan"0o})h-2((ot 0 e[ =\@mediahe[y^epG>F\n"] the
	 =;

 p{f
 @{y^e=;

 @{y^
 @{y^e=;5)s)ymyi$xprm[;

 p{f
 @ehy^etp;

 p{)
	 1;

(c  $ @{y^
 @{=;

 @{(c the
	

 @{(c hxPzdi @{y^
hxPz";

y^epthe
	 =;

 ps)ymy@mediahe[yeFilesrmediaheTG>}); h=;

 >{Fiot  $cx[)),.$ppG>F\n),y^e )."sg:$),y^e )."sg:,y^e )iot  $cx[)"."sg:$),y^e )."sg:,y^e )iot ."sg:$),y

y^epthe
	 =;iot  $cv.0( hxPzdi\n"] CEE)."sg:,Hmi

y^epthe
	 pG>F\n),y	   subst0( hxPzdi\n"]
$fonemif ($j =( pG>FFD0( hxPznt}=$parent-

	if (#i =                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             