#!/usr/bin/env ruby # This file is copyright 2007 by Vincent Fourmond # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # The goal of this file is to establish a correspondance between # PNG images used inside the documentation and figures inside the # files in the sample directory. # This script creates an images/ directory which should be copied to the # directory generated by rdoc, so that the images display properly. require 'optparse' require 'fileutils' # Which gs to use gs = 'gs' pdftops = false border = "6" # Another option: use pdftoppm, which seems to have better aa than gs opts = OptionParser.new opts.on("","--gs GS", "Path for the gs program") do |p| gs = p end opts.on("","--[no-]pdftops [PATH] ", "Use pdftops") do |p| if p.is_a? String pdftops = p elsif p pdftops = "pdftops" else pdftops = false end end opts.on("","--border BORDER", "Size of the white border around the image") do |p| border = p end opts.parse!(ARGV) # The correspondance file:figure -> target. IMAGES = { # "samples/plots/plots.rb:Blues" => "blues", # "samples/plots/plots.rb:Reds" => "reds", # "samples/plots/plots.rb:Greens" => "greens", # "samples/figures/figures.rb:Arc" => 'append_arc', # "samples/figures/figures.rb:Curve" => 'append_curve', # "samples/figures/figures.rb:Subfigures" => 'subfigures', } FIGURES = {} def get_figure_index(file, figure) if FIGURES.key?(file) return FIGURES[file][figure] else f = IO.popen("tioga #{file} -l") h = {} for l in f if l =~ /(\d+)\s+(.*)/ h[$2] = $1 end end f.close FIGURES[file] = h return get_figure_index(file, figure) end end density = 120 for s,t in IMAGES puts "Creating image #{t}.png from #{s}" s =~ /(.*):(.*)/ file = $1 figure = $2 idx = get_figure_index(file, figure) `tioga #{file} -m #{idx}` =~ /\s*(?:\d+\s+)?(.*\.pdf)/ pdf = $1 if pdftops target = "#{pdf}.temp.ps" system "#{pdftops} #{pdf} #{target}" FileUtils.rm pdf pdf = target end system "#{gs} " + "-q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=png16m " + " -dTextAlphaBits=4 -dGraphicsAlphaBits=4 " + " -sOutputFile=images/#{t}.temp.png -r#{density}x#{density} #{pdf}" # Then, we could have fun with convert and -trim and -border system "convert images/#{t}.temp.png -trim " + " -bordercolor white -border #{border} images/#{t}.png" FileUtils.rm [pdf, "images/#{t}.temp.png"] end