class Rabbit::ImageManipulable::Base

Attributes

height[R]
original_height[R]
original_width[R]
width[R]

Public Class Methods

new(filename, props) click to toggle source
# File lib/rabbit/image/base.rb, line 13
def initialize(filename, props)
  @filename = filename
  @props = normalize_props(props)
  update_size
  @original_width = @width
  @original_height = @height
end

Public Instance Methods

[](key) click to toggle source
# File lib/rabbit/image/base.rb, line 21
def [](key)
  @props[normalize_prop_key(key)]
end
[]=(key, value) click to toggle source
# File lib/rabbit/image/base.rb, line 25
def []=(key, value)
  @props[normalize_prop_key(key)] = value
end
draw(canvas, x, y, params={}) click to toggle source
# File lib/rabbit/image/base.rb, line 62
def draw(canvas, x, y, params={})
  default_params = {
    :width => width,
    :height => height,
  }
  canvas.draw_pixbuf(pixbuf, x, y, default_params.merge(params))
end
keep_ratio() click to toggle source
# File lib/rabbit/image/base.rb, line 29
def keep_ratio
  self["keep_ratio"]
end
keep_ratio=(value) click to toggle source
# File lib/rabbit/image/base.rb, line 33
def keep_ratio=(value)
  self["keep_ratio"] = value
end
pixbuf() click to toggle source
# File lib/rabbit/image/base.rb, line 37
def pixbuf
  @pixbuf
end
resize(w, h) click to toggle source
# File lib/rabbit/image/base.rb, line 41
def resize(w, h)
  if w.nil? and h.nil?
    return
  elsif keep_ratio
    if w and h.nil?
      h = (original_height * w.to_f / original_width).ceil
    elsif w.nil? and h
      w = (original_width * h.to_f / original_height).ceil
    end
  else
    w ||= width
    h ||= height
  end
  w = w.ceil if w
  h = h.ceil if h
  if w and w > 0 and h and h > 0 and [w, h] != [width, height]
    @width = w
    @height = h
  end
end

Private Instance Methods

load_by_pixbuf_loader(data) click to toggle source
# File lib/rabbit/image/base.rb, line 87
def load_by_pixbuf_loader(data)
  loader = Gdk::PixbufLoader.new
  id = loader.signal_connect("size_prepared") do |l, width, height|
    @width = width
    @height = height
  end
  begin
    loader.last_write(data)
  rescue Gdk::PixbufError
    loader.close rescue Gdk::PixbufError
    raise ImageLoadError.new("#{@filename}: #{$!.message}")
  end
  loader.signal_handler_disconnect(id)
  loader
end
normalize_prop_key(key) click to toggle source
# File lib/rabbit/image/base.rb, line 83
def normalize_prop_key(key)
  key.to_s.gsub(/-/, "_")
end
normalize_props(props) click to toggle source
# File lib/rabbit/image/base.rb, line 71
def normalize_props(props)
  normalized_props = {}
  (props || {}).each do |key, value|
    normalized_props[normalize_prop_key(key)] = value
  end
  keep_ratio_key = normalize_prop_key("keep_ratio")
  unless normalized_props.has_key?(keep_ratio_key)
    normalized_props[keep_ratio_key] = true
  end
  normalized_props
end