Sha256: 414dcf667cbe6aef24fe197b934aed491cfb9726aec1c4a1c096c8d5d9852190

Contents?: true

Size: 1.69 KB

Versions: 31

Compression:

Stored size: 1.69 KB

Contents

require "rabbit/utils"

Rabbit::Utils.require_safe "rsvg2"

require "rabbit/image/base"

module Rabbit
  module ImageManipulable

    class SVG < Base

      unshift_loader(self)

      class << self
        def match?(filename)
          File.open(filename) do |f|
            /<svg|<!DOCTYPE\s+svg/ =~ f.read(200)
          end
        end
      end

      def draw(canvas, x, y, params={})
        if @handle and canvas.rsvg_available?
          default_params = {
            :width => width,
            :height => height,
          }
          canvas.draw_rsvg_handle(@handle, x, y, default_params.merge(params))
        else
          super
        end
      end

      def pixbuf
        @pixbuf ||= to_pixbuf(width, height)
      end

      private
      def update_size
        @handle = nil
        rsvg_environment do |name|
          if RSVG::Handle.respond_to?(:new_from_file)
            @handle = RSVG::Handle.new_from_file(name)
            dim = @handle.dimensions
            @width = dim.width
            @height = dim.height
          else
            @pixbuf = RSVG.pixbuf_from_file(name)
            @width = @pixbuf.width
            @height = @pixbuf.height
          end
        end
      end

      def filename
        File.expand_path(@filename)
      end

      def rsvg_environment
        dir = File.dirname(filename)
        name = File.basename(filename)
        Dir.chdir(dir) do
          yield(name)
        end
      end

      def to_pixbuf(w=nil, h=nil)
        rsvg_environment do |name|
          if w or h
            RSVG.pixbuf_from_file_at_size(name, w || width, h || height)
          else
            RSVG.pixbuf_from_file(name)
          end
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
rabbit-1.0.3 lib/rabbit/image/svg.rb
rabbit-1.0.2 lib/rabbit/image/svg.rb
rabbit-1.0.1 lib/rabbit/image/svg.rb
rabbit-1.0.0 lib/rabbit/image/svg.rb
rabbit-0.9.3 lib/rabbit/image/svg.rb
rabbit-0.9.2 lib/rabbit/image/svg.rb
rabbit-0.9.1 lib/rabbit/image/svg.rb
rabbit-0.9.0 lib/rabbit/image/svg.rb
rabbit-0.6.4 lib/rabbit/image/svg.rb
rabbit-0.6.3 lib/rabbit/image/svg.rb
rabbit-0.6.2 lib/rabbit/image/svg.rb