#!/usr/bin/perl -w # # Syntax: adjust_eps_bbox [-h] [-l delta_left] [-r delta_right] [-t delta_top] # [-b delta_bottom] filename [...] # # Purpose: Adjusts the bounding box of eps files (which can be useful # to adjust the results of dvi2epspdf or replace_psfrag). # The flags -l, -r, -t, and -b serve to specify the margin # adjustment at respectively the left, right, top and bottom # of the figure, where a positive delta corresponds to # increasing the size of the bounding box. # If the input file is file.eps, then the output file is # file_adjusted.eps # The -h flag shows the help info for this command. # # See also: replace_psfrag, dvi2epspdf # Created: Mar 14, 2009 by Bart De Schutter # Last revised: Sep 18, 2011 by Bart De Schutter # # See http://www.deschutter.info/util/scripts.html for the latest version of # this script. # Status: public # Category: postscript, latex ############################################################################### # # Main program. # ############################################################################### use strict; use Inout; my ($left, $right, $top, $bottom); $| = 1; # Set autoflush on. ($left, $right, $top, $bottom) = process_options (); #print $left."\n"; #print $right."\n"; #print $top."\n"; #print $bottom."\n"; foreach my $eps_file (@ARGV) { adjust_bbox ($eps_file, $left, $right, $top, $bottom); } exit; ############################################################################### # # FUNCTION DEFINITIONS # ############################################################################### ############################################################################### # # ($left, $right, $top, $bottom) = process_options () # # Processes the options flags, if any. # # Inputs: none # # Outputs: $left left margin adjustment # $right right margin adjustment # $top top margin adjustment # $bottom bottom margin adjustment # # Global variables: @ARGV list of input arguments # ############################################################################### sub process_options { my ($option, $left, $right, $top, $bottom); my ($command, $margin); ( $command = $0 ) =~ s/.*\///; $left = 0; $right = 0; $top = 0; $bottom = 0; while ( ( $#ARGV >= 0 ) && ( $ARGV[0] =~ /^-(.*)/ ) ) { $option = $1; if ( $option eq "h" ) { system("sed '/^\$/q;/^#!/d;s/^# //;s/^#//' $0 >&2"); exit 1; } elsif ( $option =~ /^([lrtb])(.*)$/ ) { $option = $1; $margin = $2; if ( $margin eq "" ) { shift @ARGV; if ( $#ARGV >= 0 ) { $margin = $ARGV[0]; } else { syntax_error ("Option $option requires an extra argument."); } } if ( $margin !~ /^[0-9]+$/ ) { syntax_error ("The margin argument should be a number."); } $left = $margin if ( $option eq "l" ); $right = $margin if ( $option eq "r" ); $top = $margin if ( $option eq "t" ); $bottom = $margin if ( $option eq "b" ); } else { syntax_error ("Unknown option $option in $command."); } shift @ARGV; } if ( $#ARGV < 0 ) { syntax_error ("$command requires at least 1 input argument."); } return ($left, $right, $top, $bottom); } ############################################################################### # # adjust_bbox ($eps_file, $left, $right, $top, $bottom) # # Adjusts the bounding box of the given eps file. # # Inputs: $eps_file string # $left left margin adjustment # $right right margin adjustment # $top top margin adjustment # $bottom bottom margin adjustment # # Outputs: none # ############################################################################### sub adjust_bbox { my ($eps_file, $left, $right, $top, $bottom) = @_; my ($base_name, $extension, $dest_file, $line, $bbox); my ($bb_left, $bb_right, $bb_top, $bb_bottom); ($base_name, $extension) = get_extension ($eps_file, 0); $eps_file .= ".eps" if ( $extension eq "" ); $dest_file = $base_name."_adjusted.eps"; open (EPS_FILE, "<$eps_file") || die ("Cannot open $eps_file"); open (DEST_FILE, ">$dest_file") || die ("Cannot open $dest_file"); $bbox = 0; while ( defined( $line = ) ) { chomp ($line); if ( $line =~ /^%%BoundingBox: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)$/ ) { $bb_left = $1-$left; $bb_bottom = $2-$bottom; $bb_right = $3+$right; $bb_top = $4+$top; print DEST_FILE "%%BoundingBox: $bb_left $bb_bottom $bb_right ". "$bb_top\n"; $bbox = 1; } else { print DEST_FILE $line."\n"; } } print_warning ("No bounding box found in file $eps_file.") if ( !$bbox ); close (DEST_FILE); close (EPS_FILE); }