#!ruby -Ks # -*- mode: ruby; encoding: sjis -*- # Last updated: <2014/06/03 13:51:24 +0900> require 'rsvg2' require 'stringio' require 'dxruby' # SVGをrsvg2とcairoで描画してDXRubyのImageにする # # @note Ruby 1.9.x の場合は、 # glib2、gdk_pixbuf2、rsvg2 のバージョンを 1.2.6 に、 # cairo のバージョンを 1.12.4 に落とす必要有。 # class Svg2Image attr_accessor :handle attr_accessor :width attr_accessor :height attr_accessor :ratio attr_accessor :image def initialize(infile, ratio = 1.0) self.handle = RSVG::Handle.new_from_file(infile) make_image(ratio) end def make_image(ratio = 1.0) self.ratio = ratio.to_f self.width, self.height, = self.handle.dimensions.to_a self.width *= ratio self.height *= ratio Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, self.width, self.height) do |surface| context = Cairo::Context.new(surface) context.scale(self.ratio, self.ratio) context.render_rsvg_handle(self.handle) temp = StringIO.new("", "w+") surface.write_to_png(temp) temp.rewind self.image.dispose if self.image self.image = Image.loadFromFileInMemory(temp.read) temp.close end end end if $0 == __FILE__ # ---------------------------------------- # 動作テスト infile = "Ghostscript_Tiger.svg" scale = 0.5 scale_add = 0.1 svg = Svg2Image.new(infile, 0.5) font = Font.new(14) Window.bgcolor = [64, 64, 64] Window.loop do break if Input.keyPush?(K_ESCAPE) # Rキーで拡大縮小再描画 if Input.keyDown?(K_R) svg.make_image(scale) scale_add *= -1 if scale <= 0.25 or scale >= 1.5 scale += scale_add end Window.draw(0, 0, svg.image) s = "#{Window.real_fps.to_i} FPS #{Window.getLoad.to_i} %" s += " #{svg.image.width} x #{svg.image.height}" Window.drawFont(4, 4, s, font) end end