use Env;
use lib $srcdir;
require 'psfonts.ph'; require 'pswidth.ph';
use Fcntl;
%psconf = (
pagewidth => 595, pageheight => 792, lmarg => 100, rmarg => 50, topmarg => 100, botmarg => 100, plmarg => 50, prmarg => 0, pymarg => 50, startcopyright => 75, bulladj => 12, tocind => 12, tocpnz => 24, tocdots => 8, idxspace => 24, idxindent => 24, idxgutter => 24, idxcolumns => 2, );
%psbool = (
colorlinks => 0, );
%papersizes = (
'a5' => [421, 595], 'b5' => [501, 709], 'a4' => [595, 842], 'letter' => [612, 792], 'pa4' => [595, 792], 'b4' => [709,1002], 'legal' => [612,1008], 'a3' => [842,1190], '11x17' => [792,1224], );
undef $input;
while ( $arg = shift(@ARGV) ) {
if ( $arg =~ /^\-(|no\-)(.*)$/ ) {
$parm = $2;
$true = ($1 eq '') ? 1 : 0;
if ( $true && defined($papersizes{$parm}) ) {
$psconf{pagewidth} = $papersizes{$parm}->[0];
$psconf{pageheight} = $papersizes{$parm}->[1];
} elsif ( defined($psbool{$parm}) ) {
$psbool{$parm} = $true;
} elsif ( $true && defined($psconf{$parm}) ) {
$psconf{$parm} = shift(@ARGV);
} elsif ( $parm =~ /^(title|subtitle|year|author|license)$/ ) {
$metadata{$parm} = shift(@ARGV);
} else {
die "$0: Unknown option: $arg\n";
}
} else {
$input = $arg;
}
}
$paraskip = 6; $chapstart = 30; $chapskip = 24; $tocskip = 6;
%skiparray = ('chap' => $chapskip, 'appn' => $chapstart,
'head' => $paraskip, 'subh' => $paraskip,
'norm' => $paraskip, 'bull' => $paraskip,
'code' => $paraskip, 'toc0' => $tocskip,
'toc1' => $tocskip, 'toc2' => $tocskip);
@NASMEncoding =
(
undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
undef, undef, undef, undef, undef, undef, 'dotlessi', 'grave',
'acute', 'circumflex', 'tilde', 'macron', 'breve', 'dotaccent',
'dieresis', undef, 'ring', 'cedilla', undef, 'hungarumlaut',
'ogonek', 'caron', 'space', 'exclam', 'quotedbl', 'numbersign',
'dollar', 'percent', 'ampersand', 'quoteright', 'parenleft',
'parenright', 'asterisk', 'plus', 'comma', 'minus', 'period',
'slash', 'zero', 'one', 'two', 'three', 'four', 'five', 'six',
'seven', 'eight', 'nine', 'colon', 'semicolon', 'less', 'equal',
'greater', 'question', 'at', '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', 'bracketleft', 'backslash', 'bracketright',
'asciicircum', 'underscore', 'quoteleft', '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', 'braceleft', 'bar', 'braceright',
'asciitilde', undef, undef, undef, 'quotesinglbase', 'florin',
'quotedblbase', 'ellipsis', 'dagger', 'dbldagger', 'circumflex',
'perthousand', 'Scaron', 'guilsinglleft', 'OE', undef, 'Zcaron',
undef, undef, 'grave', 'quotesingle', 'quotedblleft',
'quotedblright', 'bullet', 'endash', 'emdash', 'tilde', 'trademark',
'scaron', 'guilsignlright', 'oe', undef, 'zcaron', 'Ydieresis',
'space', 'exclamdown', 'cent', 'sterling', 'currency', 'yen',
'brokenbar', 'section', 'dieresis', 'copyright', 'ordfeminine',
'guillemotleft', 'logicalnot', 'hyphen', 'registered', 'macron',
'degree', 'plusminus', 'twosuperior', 'threesuperior', 'acute', 'mu',
'paragraph', 'periodcentered', 'cedilla', 'onesuperior',
'ordmasculine', 'guillemotright', 'onequarter', 'onehalf',
'threequarters', 'questiondown', 'Agrave', 'Aacute', 'Acircumflex',
'Atilde', 'Adieresis', 'Aring', 'AE', 'Ccedilla', 'Egrave', 'Eacute',
'Ecircumflex', 'Edieresis', 'Igrave', 'Iacute', 'Icircumflex',
'Idieresis', 'Eth', 'Ntilde', 'Ograve', 'Oacute', 'Ocircumflex',
'Otilde', 'Odieresis', 'multiply', 'Oslash', 'Ugrave', 'Uacute',
'Ucircumflex', 'Udieresis', 'Yacute', 'Thorn', 'germandbls',
'agrave', 'aacute', 'acircumflex', 'atilde', 'adieresis', 'aring',
'ae', 'ccedilla', 'egrave', 'eacute', 'ecircumflex', 'edieresis',
'igrave', 'iacute', 'icircumflex', 'idieresis', 'eth', 'ntilde',
'ograve', 'oacute', 'ocircumflex', 'otilde', 'odieresis', 'divide',
'oslash', 'ugrave', 'uacute', 'ucircumflex', 'udieresis', 'yacute',
'thorn', 'ydieresis'
);
%charcode = ();
for ( $i = 0 ; $i < 256 ; $i++ ) {
$charcode{$NASMEncoding[$i]} = chr($i);
}
if ( defined($input) ) {
sysopen(PARAS, $input, O_RDONLY) or
die "$0: cannot open $input: $!\n";
} else {
open(PARAS, "<&STDIN") or die "$0: $!\n";
}
while ( defined($line = <PARAS>) ) {
chomp $line;
$data = <PARAS>;
chomp $data;
if ( $line =~ /^meta :(.*)$/ ) {
$metakey = $1;
$metadata{$metakey} = $data;
} elsif ( $line =~ /^indx :(.*)$/ ) {
$ixentry = $1;
push(@ixentries, $ixentry);
$ixterms{$ixentry} = [split(/\037/, $data)];
if ( $data =~ /^(.*)\,\037sp\037/ ) {
$ixprefix = $1;
$ixprefix =~ s/\037n $//; # Discard possible font change at end
$ixhasprefix{$ixentry} = $ixprefix;
if ( !$ixprefixes{$ixprefix} ) {
$ixcommafirst{$ixentry}++;
}
$ixprefixes{$ixprefix}++;
} else {
$ixprefixes{$data}++;
}
} else {
push(@ptypes, $line);
push(@paras, [split(/\037/, $data)]);
}
}
close(PARAS);
sub int2base($$) {
my($i,$b) = @_;
my($s) = '';
my($n) = '';
my($z) = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
return '0' if ($i == 0);
if ( $i < 0 ) { $n = '-'; $i = -$i; }
while ( $i ) {
$s = substr($z,$i%$b,1) . $s;
$i = int($i/$b);
}
return $n.$s;
}
sub string2array($)
{
my($s) = @_;
my(@a) = ();
$s =~ s/ \- / $charcode{'endash'} /g;
while ( $s =~ /^(\s+|\S+)(.*)$/ ) {
push(@a, [0,$1]);
$s = $2;
}
return @a;
}
sub ps_xref($) {
return @_[0];
}
$ps_space_squeeze = 0.00; sub ps_flow_lines($$$@) {
my($wid, $fontset, $type, @data) = @_;
my($fonts) = $$fontset{fonts};
my($e);
my($w) = 0; my($sw) = 0; my(@l) = (); my(@ls) = (); my(@xd) = (); my $hasmarker = 0; my $pastmarker = 0;
foreach $e ( @data ) {
$hasmarker = 1 if ( $$e[0] == -6 );
}
$w = 0;
foreach $e ( @data ) {
if ( $$e[0] < 0 ) {
if ( $$e[0] == -6 ) {
$pastmarker = 1;
}
if ( $$e[0] == -1 || $$e[0] == -6 ) {
push(@l, $e);
} else {
push(@xd, $e);
}
} else {
my $ew = ps_width($$e[1], $fontset->{fonts}->[$$e[0]][1],
\@NASMEncoding) *
($fontset->{fonts}->[$$e[0]][0]/1000);
my $sp = $$e[1];
$sp =~ tr/[^ ]//d; # Delete nonspaces
my $esw = ps_width($sp, $fontset->{fonts}->[$$e[0]][1],
\@NASMEncoding) *
($fontset->{fonts}->[$$e[0]][0]/1000);
if ( ($w+$ew) - $ps_space_squeeze*($sw+$esw) > $wid ) {
my $lx = scalar(@l)-1;
my @rm = ();
while ( $lx >= 0 ) {
while ( $lx >= 0 && $l[$lx]->[0] < 0 ) {
$pastmarker = 0 if ( $l[$lx]->[0] == -6 );
$lx--;
};
if ( $lx >= 0 ) {
if ( $l[$lx]->[1] eq ' ' ) {
splice(@l, $lx, 1);
@rm = splice(@l, $lx);
last; } else {
$lx--;
}
}
}
my $lkref = undef;
foreach my $lc ( @l ) {
if ( $$lc[0] == -2 || $$lc[0] == -3 || $lc[0] == -7 ) {
$lkref = $lc;
} elsif ( $$lc[0] == -1 ) {
undef $lkref;
}
}
if ( defined($lkref) ) {
push(@l, [-1,undef]); unshift(@rm, $lkref); }
if ( $hasmarker ) {
if ( $pastmarker ) {
unshift(@rm,[-6,undef]); } else {
push(@l,[-6,undef]); }
}
push(@ls, [[$type,0,undef,$fontset,0,0],[@l]]);
@l = @rm;
$w = $sw = 0;
for my $le ( @l ) {
if ( $$le[0] >= 0 ) {
my $xew = ps_width($$le[1],
$fontset->{fonts}->[$$le[0]][1],
\@NASMEncoding) *
($fontset->{fonts}->[$$le[0]][0]/1000);
my $xsp = $$le[1];
$xsp =~ tr/[^ ]//d; # Delete nonspaces
my $xsw = ps_width($xsp,
$fontset->{fonts}->[$$le[0]][1],
\@NASMEncoding) *
($fontset->{fonts}->[$$le[0]][0]/1000);
$w += $xew; $sw += $xsw;
}
}
}
push(@l, @xd); @xd = ();
if ( $$e[1] ne '' ) {
push(@l, $e);
$w += $ew; $sw += $esw;
}
}
}
push(@l,@xd);
if ( scalar(@l) ) {
push(@ls, [[$type,0,undef,$fontset,0,0],[@l]]); }
if ( scalar(@ls) ) {
$ls[0]->[0]->[1] |= 1; $ls[-1]->[0]->[1] |= 2; }
return @ls;
}
sub ps_merge_chunks(@) {
my(@ci) = @_;
my($c, $lc);
my(@co, $eco);
undef $lc;
@co = ();
$eco = -1; foreach $c ( @ci ) {
if ( defined($lc) && $$c[0] == $lc && $$c[0] >= 0 ) {
$co[$eco]->[1] .= $$c[1];
} else {
push(@co, $c); $eco++;
$lc = $$c[0];
}
}
return @co;
}
sub mkparaarray($@) {
my($ptype, @chunks) = @_;
my @para = ();
my $in_e = 0;
my $chunk;
if ( $ptype =~ /^code/ ) {
foreach $chunk ( @chunks ) {
push(@para, [2, $chunk]);
}
} else {
foreach $chunk ( @chunks ) {
my $type = substr($chunk,0,2);
my $text = substr($chunk,2);
if ( $type eq 'sp' ) {
push(@para, [$in_e?1:0, ' ']);
} elsif ( $type eq 'da' ) {
push(@para, [$in_e?1:0, $charcode{'endash'}]);
} elsif ( $type eq 'n ' ) {
push(@para, [0, $text]);
$in_e = 0;
} elsif ( $type =~ '^e' ) {
push(@para, [1, $text]);
$in_e = ($type eq 'es' || $type eq 'e ');
} elsif ( $type eq 'c ' ) {
push(@para, [2, $text]);
$in_e = 0;
} elsif ( $type eq 'x ' ) {
push(@para, [-2, ps_xref($text)]);
} elsif ( $type eq 'xe' ) {
push(@para, [-1, undef]);
} elsif ( $type eq 'wc' || $type eq 'w ' ) {
$text =~ /\<(.*)\>(.*)$/;
my $link = $1; $text = $2;
push(@para, [-3, $link]);
push(@para, [($type eq 'wc') ? 2:0, $text]);
push(@para, [-1, undef]);
$in_e = 0;
} elsif ( $type eq 'i ' ) {
push(@para, [-4, $text]);
} else {
die "Unexpected paragraph chunk: $chunk";
}
}
}
return @para;
}
$npara = scalar(@paras);
for ( $i = 0 ; $i < $npara ; $i++ ) {
$paras[$i] = [mkparaarray($ptypes[$i], @{$paras[$i]})];
}
sub ps_arraytostr(@) {
my $s = '';
my $c;
foreach $c ( @_ ) {
$s .= $$c[1] if ( $$c[0] >= 0 );
}
return $s;
}
sub ps_dup_para(@) {
my(@i) = @_;
my(@o) = ();
my($c);
foreach $c ( @i ) {
my @cc = @{$c};
push(@o, [@cc]);
}
return @o;
}
sub ps_dup_para_noanchor(@) {
my(@i) = @_;
my(@o) = ();
my($c);
foreach $c ( @i ) {
my @cc = @{$c};
push(@o, [@cc]) unless ( $cc[0] == -4 || $cc[0] == -5 );
}
return @o;
}
@tocparas = ([[-5, 'contents'], [0,'Contents']]);
@tocptypes = ('chap');
@bookmarks = (['title', 0, 'Title'], ['contents', 0, 'Contents']);
%bookref = ();
for ( $i = 0 ; $i < $npara ; $i++ ) {
my $xtype = $ptypes[$i];
my $ptype = substr($xtype,0,4);
my $str;
my $book;
if ( $ptype eq 'chap' || $ptype eq 'appn' ) {
unless ( $xtype =~ /^\S+ (\S+) :(.*)$/ ) {
die "Bad para";
}
my $secn = $1;
my $sech = $2;
my $xref = ps_xref($sech);
my $chap = ($ptype eq 'chap')?'Chapter':'Appendix';
$book = [$xref, 0, ps_arraytostr(@{$paras[$i]})];
push(@bookmarks, $book);
$bookref{$secn} = $book;
push(@tocparas, [ps_dup_para_noanchor(@{$paras[$i]})]);
push(@tocptypes, 'toc0'.' :'.$sech.':'.$chap.' '.$secn.':');
unshift(@{$paras[$i]},
[-5, $xref], [0,$chap.' '.$secn.':'], [0, ' ']);
} elsif ( $ptype eq 'head' || $ptype eq 'subh' ) {
unless ( $xtype =~ /^\S+ (\S+) :(.*)$/ ) {
die "Bad para";
}
my $secn = $1;
my $sech = $2;
my $xref = ps_xref($sech);
my $pref;
$pref = $secn; $pref =~ s/\.[^\.]+$//; # Find parent node
$book = [$xref, 0, ps_arraytostr(@{$paras[$i]})];
push(@bookmarks, $book);
$bookref{$secn} = $book;
$bookref{$pref}->[1]--;
push(@tocparas, [ps_dup_para_noanchor(@{$paras[$i]})]);
push(@tocptypes,
(($ptype eq 'subh') ? 'toc2':'toc1').' :'.$sech.':'.$secn);
unshift(@{$paras[$i]}, [-5, $xref]);
}
}
unshift(@paras, @tocparas); undef @tocparas;
unshift(@ptypes, @tocptypes); undef @tocptypes;
unshift(@paras,
[[0, $charcode{'copyright'}], [0, ' '], [0,$metadata{'year'}],
[0, ' '], string2array($metadata{'author'})],
[string2array($metadata{'license'})]);
unshift(@ptypes, 'norm', 'norm');
$npara = scalar(@paras);
@pslines = ();
$AuxStr = 1; $AuxPage = 2; $AuxPageStr = 3; $AuxXRef = 4; $AuxNum = 5;
sub ps_break_lines($$) {
my ($paras,$ptypes) = @_;
my $linewidth = $psconf{pagewidth}-$psconf{lmarg}-$psconf{rmarg};
my $bullwidth = $linewidth-$psconf{bulladj};
my $indxwidth = ($linewidth-$psconf{idxgutter})/$psconf{idxcolumns}
-$psconf{idxspace};
my $npara = scalar(@{$paras});
my $i;
for ( $i = 0 ; $i < $npara ; $i++ ) {
my $xtype = $ptypes->[$i];
my $ptype = substr($xtype,0,4);
my @data = @{$paras->[$i]};
my @ls = ();
if ( $ptype eq 'code' ) {
my $p;
foreach $p ( @data ) {
push(@ls, [[$ptype,0,undef,\%BodyFont,0,0],[$p]]);
}
$ls[0]->[0]->[1] |= 1; $ls[-1]->[0]->[1] |= 2; } elsif ( $ptype eq 'chap' || $ptype eq 'appn' ) {
@ls = ps_flow_lines($linewidth, \%ChapFont, $ptype, @data);
} elsif ( $ptype eq 'head' || $ptype eq 'subh' ) {
unless ( $xtype =~ /^\S+ (\S+) :(.*)$/ ) {
die "Bad para";
}
my $secn = $1;
my $sech = $2;
my $font = ($ptype eq 'head') ? \%HeadFont : \%SubhFont;
@ls = ps_flow_lines($linewidth, $font, $ptype, @data);
$ls[0]->[0]->[2] = [[$AuxStr,$secn]];
} elsif ( $ptype eq 'norm' ) {
@ls = ps_flow_lines($linewidth, \%BodyFont, $ptype, @data);
} elsif ( $ptype eq 'bull' ) {
@ls = ps_flow_lines($bullwidth, \%BodyFont, $ptype, @data);
} elsif ( $ptype =~ /^toc/ ) {
unless ( $xtype =~/^\S+ :([^:]*):(.*)$/ ) {
die "Bad para";
}
my $xref = $1;
my $refname = $2.' ';
my $ntoc = substr($ptype,3,1)+0;
my $refwidth = ps_width($refname, $BodyFont{fonts}->[0][1],
\@NASMEncoding) *
($BodyFont{fonts}->[0][0]/1000);
@ls = ps_flow_lines($linewidth-$ntoc*$psconf{tocind}-
$psconf{tocpnz}-$refwidth,
\%BodyFont, $ptype, @data);
my $nl = scalar(@ls);
$ls[0]->[0]->[2] = [[$AuxStr,$refname], [$AuxXRef,$xref]];
for ( $j = 1 ; $j < $nl ; $j++ ) {
$ls[$j]->[0]->[2] = [[$AuxNum,$refwidth]];
}
push(@{$ls[$nl-1]->[0]->[2]}, [$AuxPageStr,$xref]);
} elsif ( $ptype =~ /^idx/ ) {
my $lvl = substr($ptype,3,1)+0;
@ls = ps_flow_lines($indxwidth-$lvl*$psconf{idxindent},
\%BodyFont, $ptype, @data);
} else {
die "Unknown para type: $ptype";
}
foreach $l ( @ls ) {
@{$$l[1]} = ps_merge_chunks(@{$$l[1]});
}
push(@pslines,@ls);
}
}
ps_break_lines(\@paras, \@ptypes);
$curpage = 2; $curypos = $psconf{pageheight}-$psconf{topmarg}-$psconf{botmarg}-
$psconf{startcopyright};
undef $columnstart; undef $curcolumn; $nlines = scalar(@pslines);
sub ps_break_pages($$) {
my($startline, $endline) = @_;
my $nobreakregexp = "^(chap|appn|head|subh|toc.|idx.)\$";
my $nobreakafter = "^(chap|appn|head|subh)\$";
my $nobreakbefore = "^idx[1-9]\$";
my $columnregexp = "^idx.\$";
my $upageheight = $psconf{pageheight}-$psconf{topmarg}-$psconf{botmarg};
my $i;
for ( $i = $startline ; $i < $endline ; $i++ ) {
my $linfo = $pslines[$i]->[0];
if ( ($$linfo[0] eq 'chap' || $$linfo[0] eq 'appn' )
&& ($$linfo[1] & 1) ) {
undef $columnstart;
$curpage++ if ( $curypos > 0 || defined($columnstart) );
$curypos = $chapstart;
} elsif ( defined($columnstart) && $$linfo[0] !~ /$columnregexp/o ) {
undef $columnstart;
$curpage++;
$curypos = 0;
}
if ( $$linfo[0] =~ /$columnregexp/o && !defined($columnstart) ) {
$columnstart = $curypos;
$curcolumn = 0;
}
$curypos += $$linfo[3]->{leading};
$$linfo[4] = $curpage;
$$linfo[5] = $curypos;
$$linfo[6] = $curcolumn if ( defined($columnstart) );
if ( $curypos > $upageheight ) {
my $broken = 0; while ( !$broken && $pslines[$i]->[0]->[4] == $curpage ) {
my $linfo = $pslines[$i]->[0];
my $pinfo = $pslines[$i-1]->[0];
if ( $$linfo[1] == 2 ) {
} elsif ( $$linfo[1] & 1 ) {
$broken = 1 if ( $$pinfo[0] !~ /$nobreakafter/o &&
$$linfo[0] !~ /$nobreakbefore/o );
} else {
$broken = 1 if ( $$linfo[0] !~ /$nobreakregexp/o &&
$$pinfo[1] != 1 );
}
$i--;
}
die "Nowhere to break page $curpage\n" if ( !$broken );
if ( defined($columnstart) &&
++$curcolumn < $psconf{idxcolumns} ) {
$curypos = $columnstart;
} else {
undef $columnstart;
$curpage++;
$curypos = 0;
}
next;
}
if ( $$linfo[1] & 2 ) {
$curypos += $skiparray{$$linfo[0]};
}
}
}
ps_break_pages(0,$nlines);
%ps_xref_page = (); %ps_index_pages = (); $nlines = scalar(@pslines);
for ( $i = 0 ; $i < $nlines ; $i++ ) {
my $linfo = $pslines[$i]->[0];
foreach my $c ( @{$pslines[$i]->[1]} ) {
if ( $$c[0] == -4 ) {
if ( !defined($ps_index_pages{$$c[1]}) ) {
$ps_index_pages{$$c[1]} = [];
} elsif ( $ps_index_pages{$$c[1]}->[-1] eq $$linfo[4] ) {
next; }
push(@{$ps_index_pages{$$c[1]}}, $$linfo[4]);
} elsif ( $$c[0] == -5 ) {
$ps_xref_page{$$c[1]} = $$linfo[4];
}
}
}
$startofindex = scalar(@pslines);
@ixparas = ([[-5,'index'],[0,'Index']]);
@ixptypes = ('chap');
foreach $k ( @ixentries ) {
my $n,$i;
my $ixptype = 'idx0';
my $prefix = $ixhasprefix{$k};
my @ixpara = mkparaarray($ixptype,@{$ixterms{$k}});
my $commapos = undef;
if ( defined($prefix) && $ixprefixes{$prefix} > 1 ) {
for ( $i = 0 ; $i < scalar(@ixpara)-1 ; $i++ ) {
if ( substr($ixpara[$i]->[1],-1,1) eq ',' &&
$ixpara[$i+1]->[1] eq ' ' ) {
$commapos = $i;
last;
}
}
}
if ( defined($commapos) ) {
if ( $ixcommafirst{$k} ) {
my @precomma = splice(@ixpara,0,$commapos);
if ( $ixpara[0]->[1] eq ',' ) {
shift(@ixpara); } else {
$ixpara[0]->[1] =~ s/\,$//;
push(@precomma,shift(@ixpara));
}
push(@precomma, [-6,undef]);
push(@ixparas, [@precomma]);
push(@ixptypes, $ixptype);
shift(@ixpara); } else {
splice(@ixpara,0,$commapos+2);
}
$ixptype = 'idx1';
}
push(@ixpara, [-6,undef]); $i = 1; $n = scalar(@{$ps_index_pages{$k}});
foreach $p ( @{$ps_index_pages{$k}} ) {
if ( $i++ == $n ) {
push(@ixpara,[-7,$p],[0,"$p"],[-1,undef]);
} else {
push(@ixpara,[-7,$p],[0,"$p,"],[-1,undef],[0,' ']);
}
}
push(@ixparas, [@ixpara]);
push(@ixptypes, $ixptype);
}
ps_break_lines(\@ixparas, \@ixptypes);
$nlines = scalar(@pslines);
ps_break_pages($startofindex, $nlines);
push(@bookmarks, ['index', 0, 'Index']);
%ps_all_fonts = ();
foreach $fset ( @AllFonts ) {
foreach $font ( @{$fset->{fonts}} ) {
$ps_all_fonts{$font->[1]->{name}}++;
}
}
print "%!PS-Adobe-3.0\n";
print "%%Pages: $curpage\n";
print "%%BoundingBox: 0 0 ", $psconf{pagewidth}, ' ', $psconf{pageheight}, "\n";
print "%%Creator: (NASM psflow.pl)\n";
print "%%DocumentData: Clean7Bit\n";
print "%%DocumentFonts: ", join(' ', keys(%ps_all_fonts)), "\n";
print "%%DocumentNeededFonts: ", join(' ', keys(%ps_all_fonts)), "\n";
print "%%Orientation: Portrait\n";
print "%%PageOrder: Ascend\n";
print "%%EndComments\n";
print "%%BeginProlog\n";
foreach $c ( keys(%psconf) ) {
print "/$c ", $psconf{$c}, " def\n";
}
foreach $c ( keys(%psbool) ) {
print "/$c ", ($psbool{$c}?'true':'false'), " def\n";
}
$zstr = '/NASMEncoding [ ';
foreach $c ( @NASMEncoding ) {
my $z = '/'.(defined($c)?$c:'.notdef ').' ';
if ( length($zstr)+length($z) > 72 ) {
print $zstr,"\n";
$zstr = ' ';
}
$zstr .= $z;
}
print $zstr, "] def\n";
print "/nasmenc {\n";
print " findfont dup length dict begin\n";
print " { 1 index /FID ne {def}{pop pop} ifelse } forall\n";
print " /Encoding NASMEncoding def\n";
print " currentdict\n";
print " end\n";
print " definefont pop\n";
print "} def\n";
foreach $font ( keys(%ps_all_fonts) ) {
print '/',$font,'-NASM /',$font," nasmenc\n";
}
foreach $fset ( @AllFonts ) {
my $i = 0;
my @zfonts = ();
foreach $font ( @{$fset->{fonts}} ) {
print '/', $fset->{name}, $i, ' ',
'/', $font->[1]->{name}, '-NASM findfont ',
$font->[0], " scalefont def\n";
push(@zfonts, $fset->{name}.$i);
$i++;
}
print '/', $fset->{name}, ' [', join(' ',@zfonts), "] def\n";
}
print "/bullet [",ps_string($charcode{'bullet'}),"] def\n";
open(PSHEAD, "< head.ps");
while ( defined($line = <PSHEAD>) ) {
print $line;
}
close(PSHEAD);
print "%%EndProlog\n";
sub ps_string($) {
my ($s) = @_;
my ($i,$c);
my ($o) = '(';
my ($l) = length($s);
for ( $i = 0 ; $i < $l ; $i++ ) {
$c = substr($s,$i,1);
if ( ord($c) < 32 || ord($c) > 126 ) {
$o .= sprintf("\\%03o", ord($c));
} elsif ( $c eq '(' || $c eq ')' || $c eq "\\" ) {
$o .= "\\".$c;
} else {
$o .= $c;
}
}
return $o.')';
}
print "%%BeginSetup\n";
foreach $b ( @bookmarks ) {
print '[/Title ', ps_string($b->[2]), "\n";
print '/Count ', $b->[1], ' ' if ( $b->[1] );
print '/Dest /',$b->[0]," /OUT pdfmark\n";
}
print "setpagesize\n";
print "%%EndSetup\n";
sub ps_start_page() {
$ps_page++;
print "%%Page: $ps_page $ps_page\n";
print "%%BeginPageSetup\n";
print "save\n";
print "%%EndPageSetup\n";
print '/', $ps_page, " pa\n";
}
sub ps_end_page($) {
my($pn) = @_;
if ( $pn ) {
print "($ps_page)", (($ps_page & 1) ? 'pageodd' : 'pageeven'), "\n";
}
print "restore showpage\n";
}
$ps_page = 0;
ps_start_page();
$title = $metadata{'title'} || '';
$title =~ s/ \- / $charcode{'emdash'} /;
$subtitle = $metadata{'subtitle'} || '';
$subtitle =~ s/ \- / $charcode{'emdash'} /;
print "/ti ", ps_string($title), " def\n";
print "/sti ", ps_string($subtitle), " def\n";
print "lmarg pageheight 2 mul 3 div moveto\n";
print "tfont0 setfont\n";
print "/title linkdest ti show\n";
print "lmarg pageheight 2 mul 3 div 10 sub moveto\n";
print "0 setlinecap 3 setlinewidth\n";
print "pagewidth lmarg sub rmarg sub 0 rlineto currentpoint stroke moveto\n";
print "hfont1 setfont sti stringwidth pop neg ",
-$HeadFont{leading}, " rmoveto\n";
print "sti show\n";
if ( defined($metadata{epslogo}) &&
sysopen(EPS, $metadata{epslogo}, O_RDONLY) ) {
my @eps = ();
my ($bbllx,$bblly,$bburx,$bbury) = (undef,undef,undef,undef);
my $line;
my $scale = 1;
my $maxwidth = $psconf{pagewidth}-$psconf{lmarg}-$psconf{rmarg};
my $maxheight = $psconf{pageheight}/3-40;
my $width, $height;
my $x, $y;
while ( defined($line = <EPS>) ) {
last if ( $line =~ /^%%EOF/ );
if ( !defined($bbllx) &&
$line =~ /^\%\%BoundingBox\:\s*([0-9\.]+)\s+([0-9\.]+)\s+([0-9\.]+)\s+([0-9\.]+)/i ) {
$bbllx = $1+0; $bblly = $2+0;
$bburx = $3+0; $bbury = $4+0;
}
push(@eps,$line);
}
close(EPS);
if ( defined($bbllx) ) {
$width = $bburx-$bbllx;
$height = $bbury-$bblly;
if ( $width > $maxwidth ) {
$scale = $maxwidth/$width;
}
if ( $height*$scale > $maxheight ) {
$scale = $maxheight/$height;
}
$x = ($psconf{pagewidth}-$width*$scale)/2;
$y = ($psconf{pageheight}-$height*$scale)/2;
print "BeginEPSF\n";
print $x, ' ', $y, " translate\n";
print $scale, " dup scale\n" unless ( $scale == 1 );
print -$bbllx, ' ', -$bblly, " translate\n";
print "$bbllx $bblly moveto\n";
print "$bburx $bblly lineto\n";
print "$bburx $bbury lineto\n";
print "$bbllx $bbury lineto\n";
print "$bbllx $bblly lineto clip newpath\n";
print "%%BeginDocument: ",ps_string($metadata{epslogo}),"\n";
print @eps;
print "%%EndDocument\n";
print "EndEPSF\n";
}
}
ps_end_page(0);
$curpage = 2;
ps_start_page();
foreach $line ( @pslines ) {
my $linfo = $line->[0];
if ( $$linfo[4] != $curpage ) {
ps_end_page($curpage > 2);
ps_start_page();
$curpage = $$linfo[4];
}
print '[';
my $curfont = 0;
foreach my $c ( @{$line->[1]} ) {
if ( $$c[0] >= 0 ) {
if ( $curfont != $$c[0] ) {
print ($curfont = $$c[0]);
}
print ps_string($$c[1]);
} elsif ( $$c[0] == -1 ) {
print '{el}'; } elsif ( $$c[0] == -2 ) {
print '{/',$$c[1],' xl}'; } elsif ( $$c[0] == -3 ) {
print '{',ps_string($$c[1]),'wl}'; } elsif ( $$c[0] == -4 ) {
} elsif ( $$c[0] == -5 ) {
print '{/',$$c[1],' xa}'; } elsif ( $$c[0] == -6 ) {
print ']['; $curfont = 0;
} elsif ( $$c[0] == -7 ) {
print '{/',$$c[1],' pl}'; } else {
die "Unknown annotation";
}
}
print ']';
if ( defined($$linfo[2]) ) {
foreach my $x ( @{$$linfo[2]} ) {
if ( $$x[0] == $AuxStr ) {
print ps_string($$x[1]);
} elsif ( $$x[0] == $AuxPage ) {
print $ps_xref_page{$$x[1]},' ';
} elsif ( $$x[0] == $AuxPageStr ) {
print ps_string($ps_xref_page{$$x[1]});
} elsif ( $$x[0] == $AuxXRef ) {
print '/',ps_xref($$x[1]),' ';
} elsif ( $$x[0] == $AuxNum ) {
print $$x[1],' ';
} else {
die "Unknown auxilliary data type";
}
}
}
print ($psconf{pageheight}-$psconf{topmarg}-$$linfo[5]);
print ' ', $$linfo[6] if ( defined($$linfo[6]) );
print ' ', $$linfo[0].$$linfo[1], "\n";
}
ps_end_page(1);
print "%%EOF\n";