diff options
author | Norbert Preining <preining@logic.at> | 2008-12-01 10:53:36 +0000 |
---|---|---|
committer | Norbert Preining <preining@logic.at> | 2008-12-01 10:53:36 +0000 |
commit | bfb41e79480ab266a057ce00b90eb563ba373efb (patch) | |
tree | 3ce05babaf271ba24e5371d9032bfd1b6fef569b | |
parent | 61d8e28a3ba900559f514aa8a76735614927b66e (diff) |
add dvips perl parsing code
git-svn-id: svn://tug.org/texlive/trunk@11493 c570f23f-e606-0410-a88d-b1316a301751
-rw-r--r-- | Master/tlpkg/etc/test-code-for-papersize-in-perl.pl | 174 |
1 files changed, 159 insertions, 15 deletions
diff --git a/Master/tlpkg/etc/test-code-for-papersize-in-perl.pl b/Master/tlpkg/etc/test-code-for-papersize-in-perl.pl index c563c45da0a..f91cdd4630e 100644 --- a/Master/tlpkg/etc/test-code-for-papersize-in-perl.pl +++ b/Master/tlpkg/etc/test-code-for-papersize-in-perl.pl @@ -10,8 +10,11 @@ my %pdftex_papersize = ( 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'; @@ -75,19 +78,56 @@ if (!$what) { exit 1; } +# all subs do_* work the same way +# do_foobar --list +# list all available sizes with the selected first +# do_foobar +# informational string which paper has been selected +# doo_foobar <paper> +# selects the given <paper> + if ($what eq "dvips") { - do_dvips(); + do_dvips(@ARGV); } elsif ($what eq "dvipdfm") { - do_dvipdfm(); + do_dvipdfm(@ARGV); } elsif ($what eq "xdvi") { - do_xdvi(); + do_xdvi(@ARGV); } else { print "Unknown what $what!\n"; exit 1; } exit 0; +# +# 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 do_dvips { + my $newpaper = shift; my $configps = `kpsewhich --progname=dvips --format='dvips config' config.ps`; chomp($configps); print "file used: $configps\n"; @@ -96,14 +136,90 @@ sub do_dvips { my @lines = <FOO>; close(FOO); - my @papersizes = map { +s/^@ (\w+) .*$/$1/; $_; } grep (m/^@ /, @lines); + 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; + } + } - chomp(@papersizes); + #for my $p (@papersizes) { + # print "papersize $p from idx $startidx{$p} to $endidx{$p}\n"; + #} - for (@papersizes) { print "...$_...\n"; } + if (defined($newpaper)) { + if ($newpaper eq "--list") { + for my $p (@papersizes) { + print "$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]; + } + # output should go to $texmfsysconfig/dvips/config/config.ps TODO TODO + for (@newlines) { print; } + } else { + warn "Not a valid paper size for dvips: $newpaper\n"; + } + } + } else { + # just print the current value + print "current dvips paper size: $papersizes[0]\n"; + } } +# +# xdvi format: +# /--- XDvi --- +# |... +# |*paper: <NAME> +# |... +# \------------ +# +# Reading is done via --progname=xdvi --format='other text files' XDvi +# Writing is done to TEXMFSYSCONFIG/xdvi/XDvi +# sub do_xdvi { + my $newpaper = shift; my $xdvi = `kpsewhich --progname=xdvi --format='other text files' XDvi`; chomp($xdvi); print "file used: $xdvi\n"; @@ -112,17 +228,45 @@ sub do_xdvi { my @lines = <FOO>; close(FOO); - my @hit = map { +s/^\*paper:\s+(\w+)\s*$/$1/; $_; } grep (m/^\*paper: /,@lines); - my $hit; - if (!@hit) { - print "paper config not found\n"; + my $currentpaper; + my @paperlines = grep (m/^\*paper: /,@lines); + if (!@paperlines) { + $currentpaper = "(undefined)"; } else { - if ($#hit > 0) { - print "Strange, more than one paper definiton, using the first one!\n"; + if ($#paperlines > 0) { + warn "Strange, more than one paper definiton, using the first one!\n"; } - $hit = $hit[0]; - chomp($hit); - print "current xdvi paper size: $hit\n"; + $currentpaper = $paperlines[0]; + chomp($currentpaper); + $currentpaper =~ s/^\*paper:\s+(\w+)\s*$/$1/; + } + + # change value + if (defined($newpaper)) { + if ($newpaper eq "--list") { + print "$currentpaper\n"; + for my $p (keys %xdvi_papersize) { + print "$p\n" unless ($p eq $currentpaper); + } + } else { + my $found = 0; + for my $p (keys %xdvi_papersize) { + if ($p eq $newpaper) { + $found = 1; + last; + } + } + if ($found) { + map { +s/^\*paper:\s+(\w+)\s*$/\*paper: $newpaper\n/; } @lines; + # output should go to $texmfsysconfig/xdvi/XDvi TODO TODO + for (@lines) { print; } + } else { + warn "Not a valid paper size for xdvi: $newpaper\n"; + } + } + } else { + # just print the current value + print "current xdvi paper size: $currentpaper\n"; } } |