diff options
-rw-r--r-- | Master/texmf/scripts/texlive/papersize-setting.pl | 515 | ||||
-rwxr-xr-x | Master/texmf/scripts/texlive/tlmgr2.pl | 63 |
2 files changed, 569 insertions, 9 deletions
diff --git a/Master/texmf/scripts/texlive/papersize-setting.pl b/Master/texmf/scripts/texlive/papersize-setting.pl new file mode 100644 index 00000000000..8f19d680db8 --- /dev/null +++ b/Master/texmf/scripts/texlive/papersize-setting.pl @@ -0,0 +1,515 @@ +# +# TODO TODO TODO +# if no paperconfig line is found in paper_do_simple it adds to the end of the +# file the new line, but this does not work on context because it +# has an \endinput at the end ... needs to be fixed +# +# output is done to stdout, not the right config file +# + +$^W = 1; +use strict; + +our $localtlpdb; + +my %pdftex_papersize = ( + "letter" => [ '8.5 true in', '11 true in' ], + "a4" => [ '210 true mm', '297 true mm' ], +); + +my %xdvi_papersize; +$xdvi_papersize{'us'}='8.5x11'; +$xdvi_papersize{'letter'}='8.5x11'; +$xdvi_papersize{'usr'}='11x8.5'; +$xdvi_papersize{'legal'}='8.5x14'; +$xdvi_papersize{'ledger'}='17.0x11.0'; +$xdvi_papersize{'tabloid'}= '11.0x17.0'; +$xdvi_papersize{'foolscap'}='13.5x17.0'; +$xdvi_papersize{'a1'}='59.4x84.0cm'; +$xdvi_papersize{'a2'}='42.0x59.4cm'; +$xdvi_papersize{'a3'}='29.7x42.0cm'; +$xdvi_papersize{'a4'}='21.0x29.7cm'; +$xdvi_papersize{'a5'}='14.85x21.0cm'; +$xdvi_papersize{'a6'}='10.5x14.85cm'; +$xdvi_papersize{'a7'}='7.42x10.5cm'; +$xdvi_papersize{'a1r'}='84.0x59.4cm'; +$xdvi_papersize{'a2r'}='59.4x42.0cm'; +$xdvi_papersize{'a3r'}='42.0x29.7cm'; +$xdvi_papersize{'a4r'}='29.7x21.0cm'; +$xdvi_papersize{'a5r'}='21.0x14.85cm'; +$xdvi_papersize{'a6r'}='14.85x10.5cm'; +$xdvi_papersize{'a7r'}='10.5x7.42cm'; +$xdvi_papersize{'b1'}='70.6x100.0cm'; +$xdvi_papersize{'b2'}='50.0x70.6cm'; +$xdvi_papersize{'b3'}='35.3x50.0cm'; +$xdvi_papersize{'b4'}='25.0x35.3cm'; +$xdvi_papersize{'b5'}='17.6x25.0cm'; +$xdvi_papersize{'b6'}='13.5x17.6cm'; +$xdvi_papersize{'b7'}='8.8x13.5cm'; +$xdvi_papersize{'b1r'}='100.0x70.6cm'; +$xdvi_papersize{'b2r'}='70.6x50.0cm'; +$xdvi_papersize{'b3r'}='50.0x35.3cm'; +$xdvi_papersize{'b4r'}='35.3x25.0cm'; +$xdvi_papersize{'b5r'}='25.0x17.6cm'; +$xdvi_papersize{'b6r'}='17.6x13.5cm'; +$xdvi_papersize{'b7r'}='13.5x8.8cm'; +$xdvi_papersize{'c1'}='64.8x91.6cm'; +$xdvi_papersize{'c2'}='45.8x64.8cm'; +$xdvi_papersize{'c3'}='32.4x45.8cm'; +$xdvi_papersize{'c4'}='22.9x32.4cm'; +$xdvi_papersize{'c5'}='16.2x22.9cm'; +$xdvi_papersize{'c6'}='11.46x16.2cm'; +$xdvi_papersize{'c7'}='8.1x11.46cm'; +$xdvi_papersize{'c1r'}='91.6x64.8cm'; +$xdvi_papersize{'c2r'}='64.8x45.8cm'; +$xdvi_papersize{'c3r'}='45.8x32.4cm'; +$xdvi_papersize{'c4r'}='32.4x22.9cm'; +$xdvi_papersize{'c5r'}='22.9x16.2cm'; +$xdvi_papersize{'c6r'}='16.2x11.46cm'; +$xdvi_papersize{'c7r'}='11.46x8.1cm'; + +my %dvipdfm_papersize = ( 'letter' => 1, 'legal' => 1, 'ledger' => 1, + 'tabloid' => 1, 'a4' => 1, 'a3' => 1 ); + + +our %papersetting_subs; + +sub paper_all { + for my $p (keys %papersetting_subs) { + info("setting paper size for $p to @_\n"); + &{$papersetting_subs{$p}}(@_); + } +} + +# +# pdftex pdftexconfig.tex format +# /--- pdftexconfig.tex --- +# |... +# |\pdfpagewidth=NNN true <unit> +# |\pdfpageheight=NNN true <unit> +# |... +# \------------------------ +# +# Reading is done via --progname=pdftex --format='tex' pdftexconfig.tex +# Writing is done to TEXMFSYSCONFIG/tex/generic/config/pdftexconfig.tex +# +sub paper_pdftex { + my $outtree = shift; + my $newpaper = shift; + my $cf = `kpsewhich --progname=pdftex --format=tex pdftexconfig.tex`; + chomp($cf); + debug("file used for pdftex: $cf\n"); + + open(FOO, "<$cf") or die("cannot open file: $!"); + my @lines = <FOO>; + close(FOO); + + my ($cpw, $cph); + my ($cpwidx, $cphidx); + my $endinputidx; + # read the lines and the last pdfpageswidth/height wins + for my $idx (0..$#lines) { + my $l = $lines[$idx]; + if ($l =~ m/^\s*\\pdfpagewidth\s*=?\s*(.+)\s*$/) { + $cpw = $1; + $cpwidx = $idx; + next; + } + if ($l =~ m/^\s*\\pdfpageheight\s*=?\s*(.+)\s*$/) { + $cph = $1; + $cphidx = $idx; + next; + } + if ($l =~ m/^\s*\\endinput\s*/) { + $endinputidx = $idx; + next; + } + } + # trying to find the right papersize + # + my $currentpaper; + if (defined($cpw) && defined($cph)) { + for my $pname (keys %pdftex_papersize) { + my ($w, $h) = @{$pdftex_papersize{$pname}}; + if (($w eq $cpw) && ($h eq $cph)) { + $currentpaper = $pname; + last; + } + } + } else { + $currentpaper = "(undefined)"; + } + $currentpaper || ($currentpaper = "$cpw x $cph"); + if (defined($newpaper)) { + if ($newpaper eq "--list") { + info("$currentpaper\n"); + for my $p (keys %pdftex_papersize) { + info("$p\n") unless ($p eq $currentpaper); + } + } else { + my $found = 0; + for my $p (keys %pdftex_papersize) { + if ($p eq $newpaper) { + $found = 1; + last; + } + } + if ($found) { + my $newwline = + '\pdfpagewidth=' . ${$pdftex_papersize{$newpaper}}[0] . "\n"; + my $newhline = + '\pdfpageheight=' . ${$pdftex_papersize{$newpaper}}[1] . "\n"; + if (defined($cpwidx)) { + $lines[$cpwidx] = $newwline; + } else { + if (defined($endinputidx)) { + $lines[$endinputidx] = $newwline . $lines[$endinputidx]; + } else { + $lines[$#lines] = $newwline; + } + } + if (defined($cphidx)) { + $lines[$cphidx] = $newhline; + } else { + if (defined($endinputidx)) { + $lines[$endinputidx] = $newhline . $lines[$endinputidx]; + } else { + $lines[$#lines] = $newwline; + } + } + # TODO + warn "Output should go to $outtree/tex/generic/config/pdftexconfig.tex"; + for (@lines) { print; } + } else { + warn "Not a valid paper size for pdftex: $newpaper\n"; + } + } + } else { + # just print the current value + info("current pdftex paper size: $currentpaper\n"); + } +} +$papersetting_subs{"pdftex"} = \&paper_pdftex; + +############################################### +# +# dvips config.ps format: +# /--- config.ps --- +# |... +# |stuff not related to paper sizes +# |... +# | <empty line> +# |% some comments +# |% more comments +# |@ <NAME> <WIDTH> <HEIGHT> +# |@+ ...definition line +# |@+ ...definition line +# |... more definition lines +# |@+ %%EndPaperSize +# | +# |@ <NAME> <WIDTH> <HEIGHT> +# |... +# \------------ +# +# the first paper definition is the default +# selecting paper is done like with texconfig which used ed to move the +# selected part between @ $selected_paper .... @ /-1 (the line before the +# next @ line) to the line before the first @ line. +# (what a tricky ed invocation te created there, impressive!!!) +# +# Reading is done via --progname=dvips --format='dvips config' config.ps +# Writing is done to TEXMFSYSCONFIG/dvips/config/config.ps +# +sub paper_dvips { + my $outtree = shift; + my $newpaper = shift; + my $configps = `kpsewhich --progname=dvips --format='dvips config' config.ps`; + chomp($configps); + debug("file used for dvips: $configps\n"); + + open(FOO, "<$configps") or die("cannot open file: $!"); + my @lines = <FOO>; + close(FOO); + + my @papersizes; + my $firstpaperidx; + my %startidx; + my %endidx; + my $in_block = ""; + my $idx = 0; + for my $idx (0 .. $#lines) { + if ($lines[$idx] =~ m/^@ (\w+)/) { + $startidx{$1} = $idx; + $firstpaperidx || ($firstpaperidx = $idx-1); + $in_block = $1; + push @papersizes, $1; + next; + } + # empty lines or comments stop a block + if ($in_block) { + if ($lines[$idx] =~ m/^\s*(%.*)?\s*$/) { + $endidx{$in_block} = $idx-1; + $in_block = ""; + } + next; + } + } + + if (defined($newpaper)) { + if ($newpaper eq "--list") { + for my $p (@papersizes) { + info("$p\n"); # first is already the selected one + } + } else { + my $found = 0; + for my $p (@papersizes) { + if ($p eq $newpaper) { + $found = 1; + last; + } + } + if ($found) { + my @newlines; + for my $idx (0..$#lines) { + if ($idx < $firstpaperidx) { + push @newlines, $lines[$idx]; + next; + } + if ($idx == $firstpaperidx) { + # insert the selected paper definition + push @newlines, @lines[$startidx{$newpaper}..$endidx{$newpaper}]; + push @newlines, $lines[$idx]; + next; + } + if ($idx >= $startidx{$newpaper} && $idx <= $endidx{$newpaper}) { + next; + } + push @newlines, $lines[$idx]; + } + # TODO + warn "Output should go to $outtree/dvips/config/config.ps"; + for (@newlines) { print; } + } else { + warn "Not a valid paper size for dvips: $newpaper\n"; + } + } + } else { + # just print the current value + info("current dvips paper size: $papersizes[0]\n"); + } +} +$papersetting_subs{"dvips"} = \&paper_dvips; + +# +# context format: +# /--- cont-sys.{tex,rme} +# |... +# |\setuppapersize[letter][letter] +# |... +# \------------ +# +# Reading is done first via kpsewhich -prog=tex -format=tex cont-sys.tex +# if that fails via -prog=tex -format='other text files' cont-sys.rme +# +# Writing is done to TEXMFSYSCONFIG/tex/context/user/cont-sys.tex +# +sub paper_context { + my $outtree = shift; + my $newpaper = shift; + # check that context is installed at all + if (!defined($localtlpdb->get_package("context"))) { + debug("context not installed, not setting paper size\n"); + return; + } + my $inp = `kpsewhich --progname=tex --format=tex cont-sys.tex`; + chomp($inp); + if (!$inp) { + # we didn't get anything, so retry with .rme + $inp = `kpsewhich --progname=tex --format='other text files' cont-sys.rme`; + chomp($inp); + } + my @bla = keys %pdftex_papersize; + paper_do_simple($inp, "context", '^\s*\\setuppapersize\s*', + '^\s*\\setuppapersize\s*\[([^][]*)\]', + sub { + my ($ll,$np) = @_; + if ($ll =~ m/^\s*\\setuppapersize\s*/) { + return("\\setuppapersize[$np][$np]\n"); + } else { + return($ll); + } + }, "$outtree/tex/context/user/cont-sys.rme", \@bla, + 'a4', '\setuppapersize[a4][a4]', $newpaper); +} +$papersetting_subs{"context"} = \&paper_context; + + +# +# xdvi format: +# /--- XDvi --- +# |... +# |*paper: <NAME> +# |... +# \------------ +# +# Reading is done via --progname=xdvi --format='other text files' XDvi +# Writing is done to TEXMFSYSCONFIG/xdvi/XDvi +# +sub paper_xdvi { + my $outtree = shift; + my $newpaper = shift; + my $xdvi = `kpsewhich --progname=xdvi --format='other text files' XDvi`; + chomp($xdvi); + my @bla = keys %xdvi_papersize; + paper_do_simple($xdvi, "xdvi", '^\*paper: ', '^\*paper:\s+(\w+)\s*$', + sub { + my ($ll,$np) = @_; + $ll =~ s/^\*paper:\s+(\w+)\s*$/\*paper: $np\n/; + return($ll); + }, "$outtree/xdvi/XDvi", \@bla, + '(not defined)', '*paper: a4', $newpaper); +} +$papersetting_subs{"xdvi"} = \&paper_xdvi; + + +# +# dvipdfm(x) format: +# /--- dvipdfm/config, dvipdfmx/dvipdfmx.cfg --- +# |... +# |p <NAME> +# |... +# \------------ +# +# Reading is done +# for dvipdfm via --progname=dvipdfm --format='other text files' config +# for dvipdfmx via --progname=dvipdfmx --format='other text files' dvipdfmx.cfg +# Writing is done to TEXMFSYSCONFIG/dvipdfm/config/config +# and /dvipdfmx/dvipdfmx.cfg +# +# +sub do_dvipdfm_and_x { + my ($inp, $prog, $outp, $paplist, $newpaper) = @_; + paper_do_simple($inp, $prog, '^p\s+', '^p\s+(\w+)\s*$', + sub { + my ($ll,$np) = @_; + $ll =~ s/^p\s+(\w+)\s*$/p $np\n/; + return($ll); + }, $outp, $paplist, '(not defined)', 'p a4', $newpaper); +} + +sub paper_dvipdfm { + my $outtree = shift; + my $newpaper = shift; + my $inp = `kpsewhich --progname=dvipdfm --format='other text files' config`; + chomp($inp); + my @bla = keys %dvipdfm_papersize; + do_dvipdfm_and_x($inp, "dvipdfm", "$outtree/dvipdfm/config/config", + \@bla, $newpaper); +} +$papersetting_subs{"dvipdfm"} = \&paper_dvipdfm; +sub paper_dvipdfmx { + my $outtree = shift; + my $newpaper = shift; + my $inp = `kpsewhich --progname=dvipdfmx --format='other text files' dvipdfmx.cfg`; + chomp($inp); + my @bla = keys %dvipdfm_papersize; + do_dvipdfm_and_x($inp, "dvipdfmx", "$outtree/dvipdfmx/dvipdfmx.cfg", + \@bla, $newpaper); +} +$papersetting_subs{"dvipdfmx"} = \&paper_dvipdfmx; + + + +# +# paper_do_simple does the work for single line config files +# (xdvi, dvipdfm, dvipdfmx) +# arguments: +# $inp, $prog, $firstre, $secondre, $bl, $outp, $paplist, $newpaper +# with +# $inp .. input file location +# $prog .. program name +# $firstre .. re that searches for paper lines +# $secondre .. re that extracts the paper from a paper line +# $bl .. block/sub taking two args, one paper line and the new paper, and +# returns the line with the paper configured, only lines mathing +# $firstre are shipped over to $bl +# $outp .. location of the output file +# $paplist .. ref to an array with the list of admissible paper sizes +# $defaultpaper .. default papersize (arbitrary string) if the $firstre is +# not found in the config file +# $defaultline .. the line to be added at the bottom of the file if +# no line has been found +# $newpaper .. --list, new paper, or undef +sub paper_do_simple { + my ($inp, $prog, $firstre, $secondre, $bl, $outp, $paplist, $defaultpaper, $defaultline, $newpaper) = @_; + + debug("file used for $prog: $inp\n"); + + open(FOO, "<$inp") or die("cannot open file: $!"); + my @lines = <FOO>; + close(FOO); + + my $currentpaper; + my @paperlines = grep (m/$firstre/,@lines); + if (!@paperlines) { + $currentpaper = $defaultpaper; + } else { + if ($#paperlines > 0) { + warn "Strange, more than one paper definiton, using the first one!\n"; + } + $currentpaper = $paperlines[0]; + chomp($currentpaper); + $currentpaper =~ s/$secondre/$1/; + } + + # change value + if (defined($newpaper)) { + if ($newpaper eq "--list") { + info("$currentpaper\n"); + for my $p (@$paplist) { + info("$p\n") unless ($p eq $currentpaper); + } + } else { + my $found = 0; + for my $p (@$paplist) { + if ($p eq $newpaper) { + $found = 1; + last; + } + } + if ($found) { + my @newlines; + my $foundcfg = 0; + for my $l (@lines) { + if ($l =~ m/$firstre/) { + push @newlines, &$bl($l, $newpaper); + $foundcfg = 1; + } else { + push @newlines, $l; + } + } + # what to do if no default line found??? + if (!$foundcfg) { + push @newlines, &$bl($defaultline, $newpaper); + } + # should save to $outf + warn "Output should go to $outp"; + for (@newlines) { print; } + } else { + warn "Not a valid paper size for $prog: $newpaper\n"; + } + } + } else { + # just return the current value + info("Current paper setting for $prog: $currentpaper\n"); + } +} + +1; + +### Local Variables: +### perl-indent-level: 2 +### tab-width: 2 +### indent-tabs-mode: nil +### End: +# vim:set tabstop=2 expandtab: # diff --git a/Master/texmf/scripts/texlive/tlmgr2.pl b/Master/texmf/scripts/texlive/tlmgr2.pl index a867befc720..591ca70931b 100755 --- a/Master/texmf/scripts/texlive/tlmgr2.pl +++ b/Master/texmf/scripts/texlive/tlmgr2.pl @@ -57,6 +57,7 @@ BEGIN { $Master = `kpsewhich -var-value=SELFAUTOPARENT`; chomp($Master); unshift (@INC, "$Master/tlpkg"); + unshift (@INC, "$Master/texmf/scripts/texlive"); } use TeXLive::TLPOBJ; @@ -73,6 +74,8 @@ use Pod::Usage; use Getopt::Long qw(:config no_autoabbrev require_order); use strict; +require("papersize-setting.pl"); + binmode(STDOUT, ":utf8"); binmode(STDERR, ":utf8"); @@ -92,6 +95,10 @@ my %options; # the location from where we install our $location; +# +# stubs for setting paper sizes +our %papersetting_subs; + # each action uses this. my @getopt_configure_subopts = qw(no_pass_through permute); @@ -266,15 +273,8 @@ sub execute_action { if ($opt_gui) { action_gui("config"); } - init_local_db(); - my $TEXMFMAIN = `kpsewhich -var-value=TEXMFMAIN`; - chomp $TEXMFMAIN; - my @cmdline = ("texlua", "$TEXMFMAIN/scripts/texlive/texconf.tlu", "--prog", "tlmgr","--sys"); - if (!$localtlpdb->option_create_formats) { - push @cmdline, "--noformat"; - } - push @cmdline, $action, @ARGV; - system(@cmdline); + @ARGV = ( $action, @ARGV); + merge_into(\%ret, action_paper()); } elsif ($action =~ m/^uninstall$/i) { merge_into(\%ret, action_uninstall()); finish(0); @@ -607,6 +607,51 @@ sub action_remove { } } +# action_paper +# ARGV can look like: +# paper a4 +# paper letter +# [xdvi|dvips|pdftex|dvipdfm|dvipdfmx|context] paper [help|papersize|--list] +# +sub action_paper { + my %ret; + if ($opt_gui) { + action_gui("config"); + } + init_local_db(); + my $texmfsysvar = `kpsewhich -var-value=TEXMFSYSVAR`; + chomp($texmfsysvar); + my $texmfsysconfig = `kpsewhich -var-value=TEXMFSYSCONFIG`; + chomp($texmfsysconfig); + + # we are doing -sys variants + $ENV{"TEXMFVAR"} = $texmfsysvar; + $ENV{"TEXMFCONFIG"} = $texmfsysconfig; + + my $action = shift @ARGV; + if ($action =~ m/^paper$/i) { + my $newpaper = shift @ARGV; + if (!defined($newpaper) || + (($newpaper ne "a4") && ($newpaper ne "letter"))) { + tlwarn("tlmgr paper needs an argument: a4 | letter\n"); + return; + } + paper_all($texmfsysconfig, $newpaper); + } else { + # now we have as first argument a program + my $prog = $action; + # pass on everything to th paper_* subs + # get rid of useless "paper" argument + shift @ARGV; + &{$papersetting_subs{$prog}}($texmfsysconfig, @ARGV); + } + if ($localtlpdb->option_create_formats) { + $ret{"format"} = 1; + } + return \%ret; +} + + sub action_show { if ($opt_gui) { action_gui("config"); |