#!/usr/bin/perl -w use strict; use Getopt::Std; use vars qw($opt_I $opt_r $opt_c $opt_C $opt_W $opt_H $opt_w $opt_h $opt_u $opt_d $opt_o $opt_R); my %One_inch = (in => 1, mm => 25.4, cm => 2.54, pt => 72.27, pc => 72.27 / 12, bp => 72, dd => 72.27 * 1157 / 1238, cc => 72.27 * 1157 / 1238 / 12, sp => 72.27 * 63536, ); my $Default_rows = 4; my $Default_cols = 2; # 3 is more than enough for inches, and probably adequate for cm; # if you change $Units below you might want to consider what a reasonable # value for string truncation should be. sub Stringify ($) { sprintf("%.3f", $_[0]); } sub ceil ($) { $_[0] == int($_[0]) ? $_[0] : int($_[0]) + 1 } (my $basename = $0) =~ s!.*/!!; getopts('CIr:c:W:H:w:h:u:d:o:R:'); if (@ARGV == 0 || !defined($opt_W) || !defined($opt_H)) { warn < 90, f => 180, c => 270, ); my $Angle = defined($opt_R) ? (exists($Rotations{$opt_R}) ? $Rotations{$opt_R} : die "$basename: unknown rotation $opt_R, valid rotations: " . join(',',keys(%Rotations)) . "\n" ) : 0; my $Swap_width_height = ($Angle % 180 == 90); ## Relatively "fixed" variables for Latex. ## my $Units = 'in'; # Sorry. ## my $OneInch = 1; # YOU MUST CHANGE THIS IF $Units ARE CHANGED!!! # (Whenever you find yourself putting three or more exclamation points # in a comment, it's a sign you need to go back and see how you can make # the program better, and hassle the users less.) my $Topmargin = -0.5 * $One_inch; my $Sidemargin = -0.5 * $One_inch; my $Textwidth = $Out_width - 2*($One_inch + $Sidemargin); my $Textheight = $Out_height - 2*($One_inch + $Topmargin); # Figure out the $Scale for reducing the pages into snapshots. # The input height times the number of rows, divided by the actual # page height, gives a scale factor (i.e., how many times you'd have # to reduce the input pages to get an exact fit). Considering only # width might give a different scale factor, and the larger one is the # appropriate one to use. I then round up to the next multiple of 0.5. my $Width_scale = ($Swap_width_height ? $In_height : $In_width) * $Cols / $Textwidth; my $Height_scale = ($Swap_width_height ? $In_width : $In_height) * $Rows / $Textheight; my $Scale = ceil(2*(($Height_scale>$Width_scale)? ($Height_scale):($Width_scale)))/2; my $Snapshot_width = $In_width / $Scale; my $Snapshot_height = $In_height / $Scale; my $Col_space = ($Cols > 1) ? ($Textwidth - ($Swap_width_height ? $Snapshot_height : $Snapshot_width) * $Cols) / ($Cols - 1): 0; my $Row_space = ($Rows > 1) ? ($Textheight - ($Swap_width_height ? $Snapshot_width : $Snapshot_height) * $Rows) / ($Rows - 1): 0; my $Snapshot_width_str = Stringify($Snapshot_width); my $Snapshot_height_str = Stringify($Snapshot_height); my $Snapshot_XY = "$Snapshot_width_str,$Snapshot_height_str"; my $Snapshot_halfXY = Stringify($Snapshot_width/2) . "," . Stringify($Snapshot_height/2); my $XY_of_snapshot = $Swap_width_height ? sub ($) { my $x = $_[0] % $Cols; my $y = $Rows - int($_[0] / $Cols) - 1; my $X = Stringify((0.5 + $x) * $Snapshot_height + $x * $Col_space); #swap my $Y = Stringify((0.5 + $y) * $Snapshot_width + $y * $Row_space); #swap return "$X,$Y"; } : sub ($) { my $x = $_[0] % $Cols; my $y = $Rows - int($_[0] / $Cols) - 1; my $X = Stringify((0.5 + $x) * $Snapshot_width + $x * $Col_space); #swap my $Y = Stringify((0.5 + $y) * $Snapshot_height + $y * $Row_space); #swap return "$X,$Y"; }; my $File_index = $Column_major ? sub { my ($Page,$Snapshot) = @_; my $x = $Snapshot % $Cols; my $y = int($Snapshot / $Cols); $Page * $Num_per_page + $x * $Rows + $y; } : ($Invert ? sub { my ($Page,$Snapshot) = @_; my $x = $Snapshot % $Cols; my $y = int($Snapshot / $Cols); $Page * $Num_per_page + $y * $Cols + $Cols - $x - 1; } : sub { my ($Page,$Snapshot) = @_; $Page * $Num_per_page + $Snapshot; }); system('dvips', @Dvips_options, '-i', '-S', '1', '-o', "$Fileroot.${Num_per_page}.dummy", $Input_file) == 0 or die "$basename: System call to dvips failed, $?"; opendir(DIR, '.') or die "$basename: Couldn't open current directory: $!"; my @Page_files = grep /^$Fileroot\.${Num_per_page}\.\d\d\d$/, readdir DIR; closedir(DIR) or die "$basename: Couldn't close current directory: $!"; open(LATEX, ">$Fileroot.${Num_per_page}.tex") or die "$basename: Could not open .tex file for write: $!"; print LATEX <($Snapshot); my $Index = $File_index->($Page,$Snapshot); next SNAPSHOT unless $Index < @Page_files; print LATEX <