diff options
Diffstat (limited to 'Master/texmf-dist/scripts/epspdf/epspdf.rb')
-rwxr-xr-x | Master/texmf-dist/scripts/epspdf/epspdf.rb | 168 |
1 files changed, 119 insertions, 49 deletions
diff --git a/Master/texmf-dist/scripts/epspdf/epspdf.rb b/Master/texmf-dist/scripts/epspdf/epspdf.rb index 6c67f7eae3f..ccb3effb33f 100755 --- a/Master/texmf-dist/scripts/epspdf/epspdf.rb +++ b/Master/texmf-dist/scripts/epspdf/epspdf.rb @@ -27,17 +27,15 @@ ########################################### -# some initialization in case this file is used as main program +# some initialization -if __FILE__ == $0 +# add directory of this source to loadpath +$:.unshift( File.dirname( __FILE__ ) ) - # add directory of this source to loadpath - $:.unshift( File.dirname( __FILE__ ) ) +# turn on warnings +#$VERBOSE = 1 - # turn on warnings - #$VERBOSE = 1 - -end # if main program +$from_gui = nil ########################################### @@ -51,6 +49,9 @@ class EPCallError < StandardError; end # Can't get a valid boundingbox for an eps file class EPBBError < StandardError; end +# copying failed +class EPCopyError < StandardError; end + # system call failed class EPSystemError < StandardError; end @@ -136,13 +137,17 @@ def sliceFile( source, dest, len, offs, mode=$W ) s.binmode if ARCH == 'w32' s.seek( offs, IO::SEEK_SET ) if s.pos == offs - File.open( dest, mode ) do |d| - d.binmode if ARCH == 'w32' - tocopy = len - while tocopy>0 and s.read( [ tocopy, 16384 ].min, buffer ) - tocopy = tocopy - d.write( buffer ) - end # while - end # |d| (automatic closing) + begin + File.open( dest, mode ) do |d| + d.binmode if ARCH == 'w32' + tocopy = len + while tocopy>0 and s.read( [ tocopy, 16384 ].min, buffer ) + tocopy = tocopy - d.write( buffer ) + end # while + end # do + rescue + fail EPCopyError, "Failure to copy to #{dest}" + end end # if s.seek end # |s| (automatic closing) # return value true if anything has been copied @@ -168,6 +173,8 @@ def write_log( s ) File.open( LOGFILE, 'a' ) { |f| f.print( "#{$$} #{Time.now.strftime('%Y/%m/%d %H:%M:%S')} #{s}\n" ) } + puts( s ) if $from_gui + end ########################################### @@ -490,16 +497,26 @@ class PsPdf else @path = file @type = identify( file ) + @npages = pdf_pages if @type == 'pdf' # should we do this? + @npages = 1 if @type =~ /^eps/ end end # initialize + def file_info + if @npages and @type !~ /^eps/ + return "File type of #{@path} is #{@type} with #{@npages} pages" + else + return "File type of #{@path} is #{@type}" + end + end + ################## # debug string for EPCallError def buginfo( params = nil ) b = "Source was: " + @path + $/ - b = b + hash_prn( params ) if params + b = b + param_hash_prn( params ) if params b end @@ -756,6 +773,7 @@ class PsPdf retVal.hrbb = @hrbb.copy if @hrbb retVal.atfront = true retVal.hr_atfront = true + retVal.npages = 1 return retVal end # eps_clean @@ -928,6 +946,7 @@ class PsPdf # end fix for incorrect DSC header produced by some versions of pdftops retVal.atfront = 1 retVal.find_bb_simple + retVal.npages = 1 return retVal end # pdf_to_eps @@ -977,6 +996,7 @@ class PsPdf write_log cmd # if $DEBUG fail EPSystemError, cmd unless \ system( cmd ) and test( ?s, retVal.path ) + retVal.npages = @npages return retVal end # pdf_to_ps @@ -987,8 +1007,6 @@ class PsPdf def any_to_any( params={} ) - # $stderr.puts( hash_prn( params ) ) - type = params[ 'type' ] page = params[ 'page' ] bbox = params[ 'bbox' ] @@ -1017,7 +1035,8 @@ class PsPdf pp = pp.eps_clean if pp.type == 'epsPreview' pp = pp.eps_clean if pp.type == 'eps' and \ not ( pp.bb and pp.atfront ) # => not yet `cleaned' - pp.pdf_pages if pp.type == 'pdf' and not pp.npages + #pp.pdf_pages if pp.type == 'pdf' and not pp.npages + # now also done in initialize case pp.type when 'eps' @@ -1026,19 +1045,29 @@ class PsPdf # roundtrip guarantees bb.non_negative, necessary for fix_bb. # pdf_to_ps( sep ) improves chances of grayscaling. - pp = pp.eps_to_pdf.pdf_to_eps( 'sep' => gray ) \ - if gRAY or ( bbox and not pp.bb.non_negative ) - case type when 'eps' + pp = pp.eps_to_pdf.pdf_to_eps( 'sep' => gray ) \ + if gRAY or ( bbox and not pp.bb.non_negative ) pp = pp.eps_to_pdf( 'gray' => gray ).pdf_to_eps if gray pp = pp.fix_bb if bbox return pp when 'pdf' + pp = pp.eps_to_pdf.pdf_to_eps( 'sep' => gray ) \ + if gRAY or ( bbox and not pp.bb.non_negative ) pp = pp.fix_bb if bbox return pp.eps_to_pdf( 'gray' => gray ) + when 'ps' + if gRAY + pp = pp.eps_to_pdf.pdf_to_ps( 'sep' => gray ) + pp = pp.ps_to_pdf( 'gray' => gray ).pdf_to_ps + else + pp = pp.eps_to_pdf( 'gray' => gray ).pdf_to_ps + end + return pp + end # case type when 'pdf' @@ -1117,19 +1146,54 @@ class PsPdf end # case pp.type + #raise EPCallError fail "Unsupported conversion" + # this shouldn't happen anymore end # any_to_any end # class PsPdf ################################# -# main program when not called from GUI - -if __FILE__ == $0 +# main program require 'optparse' +def gui( action ) + case action + when 'config_w' then + puts "pdftops_prog=#{$settings.pdftops_prog}" if ARCH=='w32' + puts "pdf_viewer=#{$settings.pdf_viewer}" if ARCH=='unix' + puts "ps_viewer=#{$settings.ps_viewer}" if ARCH=='unix' + puts "defaultDir=#{$settings.defaultDir}" + puts "ignore_pdftops=#{$settings.ignore_pdftops}" + puts "pdf_target=#{$settings.pdf_target}" + puts "pdf_version=#{$settings.pdf_version}" + puts "pdf_custom=#{$settings.pdf_custom}" + puts "ps_options=#{$settings.ps_options}" + puts "ignore_hires_bb=#{$settings.ignore_hires_bb}" + puts "bb_spread=#{$settings.bb_spread}" + exit + when 'config_r' then + while line = gets + (varname, val) = line.split('=', 2) + varname.strip! if varname + val.strip! if val + if $settings.has_key?( varname ) + val = nil if val == '' + $settings[varname].val = val + # puts( "\"#{val}\" assigned to #{varname}" ) + end + end # while + $settings.write_settings + exit + when nil then + $from_gui = true + else + abort( "Action should be omitted or 'config_w' or 'config_r'" ) + end # case +end + # create a pause to examine temp files def abortt( msg ) if $DEBUG @@ -1139,8 +1203,6 @@ def abortt( msg ) fail end -begin # outer rescue block - save_settings = false opts = OptionParser.new do |opts| @@ -1208,11 +1270,6 @@ opts = OptionParser.new do |opts| $settings.pdf_version = opt end - #opts.on( "--version" ) do |opt| - # puts EPVERSION - # exit - #end - opts.on( "-I", "Ignore pdftops even if available", "(default: use if available)" ) do |opt| @@ -1253,6 +1310,10 @@ opts = OptionParser.new do |opts| $DEBUG = 1 end + opts.on( "--gui[=ACTION]", "Do not use; reserved for GUI" ) do |opt| + gui( opt ) + end + opts.separator "" opts.on( "-v", "Prints version info" ) do |opt| @@ -1276,8 +1337,10 @@ if ARGV.length == 1 and ARGV[0] == '--version' exit end -# parse options destructively +# save original command-line for later reporting +cmdline = "#{$0} #{ARGV.join( sep=' ' )}" +# parse options destructively begin opts.parse!( ARGV ) rescue OptionParser::ParseError => e @@ -1285,6 +1348,9 @@ rescue OptionParser::ParseError => e exit( -1 ) end +# log cmdline AFTER we found out whether we run from gui +write_log( cmdline ) + $options.page = 1 if $options.bbox and not $options.page $settings.write_settings if save_settings @@ -1297,7 +1363,8 @@ if ARGV.length < 1 exit end elsif $options.info - puts "File type of #{ARGV[0]} is #{PsPdf.new( 'file' => ARGV[0] ).type}" + p = PsPdf.new( 'file' => ARGV[0] ) + puts( p.file_info ) exit elsif ARGV.length > 1 and File.expand_path( ARGV[0] ) == File.expand_path( ARGV[1] ) @@ -1354,43 +1421,46 @@ end # case outfile abort "Unknown or unsupported output file extension" \ unless $options.type -abort "Output format not supported without xpdf utilities" \ - if outfile == 'ps' and not $settings.pstopdf_prog +#abort "Output format not supported without xpdf utilities" \ +# if outfile == 'ps' and not $settings.pstopdf_prog pp = PsPdf.new( 'file' => infile ) begin # inner rescue block pp = pp.any_to_any( $options ) + write_log( pp.file_info ) if $from_gui + ccp( pp.path, outfile ) rescue EPCallError => exc mess = "Wrong method call or conversion not supported or wrong page number" + $/ + exc.message + $/ + exc.backtrace.join( $/ ) write_log( mess ) puts mess - fail StandardError + exit 1 rescue EPBBError => exc mess = "Boundingbox problem" + $/ + exc.message + $/ + exc.backtrace.join( $/ ) write_log( mess ) puts mess - fail StandardError + exit 1 +rescue EPCopyError => exc + mess = "Copying problem" + $/ + + exc.message + $/ + exc.backtrace.join( $/ ) + write_log( mess ) + puts mess + exit 1 rescue EPSystemError => exc mess = "Problem with system call" + $/ + exc.message + $/ + exc.backtrace.join( $/ ) write_log( mess ) puts mess - fail StandardError + exit 1 +rescue StandardError => exc + mess = exc.message + $/ + exc.backtrace.join( $/ ) + write_log( mess ) + puts mess + exit 1 end # rescue block -ccp( pp.path, outfile ) - cleantemp unless $DEBUG == 1 - -rescue => detail -write_log detail.backtrace.join( $/ ) -puts detail.backtrace.join( $/ ) -exit -end # outer rescue block - -end # if called as main program __END__ |