1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#!/usr/bin/env ruby
=begin
svg2.rb - Ruby/RSVG sample script.
Copyright (c) 2006 Ruby-GNOME2 Project Team
This program is licenced under the same licence as Ruby-GNOME2.
$Id: svg2.rb,v 1.5 2006/06/17 13:30:25 mutoh Exp $
=end
require 'tempfile'
require "rsvg2"
if ARGV.size < 2
puts "usage: #{$0} input.svg output [scale_ratio]"
exit(-1)
end
input, output, ratio = ARGV
ratio ||= 1.0
ratio = ratio.to_f
output_type = File.extname(output)[1..-1].downcase
output_type = "jpeg" if /jpg/ =~ output_type
def to_pixbuf_with_cairo(input, ratio)
handle = nil
Dir.chdir(File.dirname(File.expand_path(input))) do
handle = RSVG::Handle.new_from_file(input)
end
dim = handle.dimensions
width = dim.width * ratio
height = dim.height * ratio
surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, width, height)
cr = Cairo::Context.new(surface)
cr.scale(ratio, ratio)
cr.render_rsvg_handle(handle)
temp = Tempfile.new("svg2")
cr.target.write_to_png(temp.path)
cr.target.finish
Gdk::Pixbuf.new(temp.path)
end
def to_pixbuf(input, ratio)
RSVG.pixbuf_from_file_at_zoom(input, ratio, ratio)
end
if RSVG.cairo_available?
puts "using cairo..."
pixbuf = to_pixbuf_with_cairo(input, ratio)
else
pixbuf = to_pixbuf(input, ratio)
end
if pixbuf.nil?
puts "Is it a SVG file?: #{input}"
exit(-1)
end
puts "saving to #{output}(#{pixbuf.width}x#{pixbuf.height})..."
pixbuf.save(output, output_type)
|