class Rabbit::ImageManipulable::Base

Attributes

animation[R]
filename[R]
original_height[R]
original_width[R]
properties[R]

Public Class Methods

new(filename, props) click to toggle source
# File lib/rabbit/image/base.rb, line 34
def initialize(filename, props)
  @filename = filename
  @properties = Properties.new(props)
  initialize_keep_ratio
  @animation = nil
  @animation_iterator = nil
  @animation_timeout = nil
  update_size
  @original_width = @width
  @original_height = @height
end

Public Instance Methods

[](key) click to toggle source
# File lib/rabbit/image/base.rb, line 46
def [](key)
  @properties[key]
end
[]=(key, value) click to toggle source
# File lib/rabbit/image/base.rb, line 50
def []=(key, value)
  @properties[key] = value
end
draw(canvas, x, y, params={}) click to toggle source
# File lib/rabbit/image/base.rb, line 115
def draw(canvas, x, y, params={})
  default_params = default_draw_params(x, y)
  target_pixbuf = pixbuf
  if @animation_iterator
    @animation_iterator.advance
    target_pixbuf = @animation_iterator.pixbuf
    update_animation_timeout(canvas)
  end
  canvas.draw_pixbuf(target_pixbuf, x, y, default_params.merge(params))
end
height() click to toggle source
# File lib/rabbit/image/base.rb, line 73
def height
  (relative_clip_height&.resolve(@height) || @height) -
    (relative_clip_y&.resolve(@height) || 0)
end
keep_ratio()

For backward compatibility

Alias for: keep_ratio?
keep_ratio=(value) click to toggle source
# File lib/rabbit/image/base.rb, line 60
def keep_ratio=(value)
  @properties.keep_ratio = value
end
keep_ratio?() click to toggle source
# File lib/rabbit/image/base.rb, line 54
def keep_ratio?
  @properties.keep_ratio
end
Also aliased as: keep_ratio
pixbuf() click to toggle source
# File lib/rabbit/image/base.rb, line 64
def pixbuf
  @pixbuf
end
relative_clip_height() click to toggle source
# File lib/rabbit/image/base.rb, line 90
def relative_clip_height
  @properties.get_relative_size("relative_clip_height", @filename)
end
relative_clip_width() click to toggle source
# File lib/rabbit/image/base.rb, line 86
def relative_clip_width
  @properties.get_relative_size("relative_clip_width", @filename)
end
relative_clip_x() click to toggle source
# File lib/rabbit/image/base.rb, line 78
def relative_clip_x
  @properties.get_relative_size("relative_clip_x", @filename)
end
relative_clip_y() click to toggle source
# File lib/rabbit/image/base.rb, line 82
def relative_clip_y
  @properties.get_relative_size("relative_clip_y", @filename)
end
resize(w, h) click to toggle source
# File lib/rabbit/image/base.rb, line 94
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
width() click to toggle source
# File lib/rabbit/image/base.rb, line 68
def width
  (relative_clip_width&.resolve(@width) || @width) -
    (relative_clip_x&.resolve(@width) || 0)
end

Private Instance Methods

default_draw_params(x, y) click to toggle source
# File lib/rabbit/image/base.rb, line 173
def default_draw_params(x, y)
  _relative_clip_x = relative_clip_x
  _relative_clip_y = relative_clip_y
  _relative_clip_width = relative_clip_width
  _relative_clip_height = relative_clip_height
  if _relative_clip_x or
     _relative_clip_y or
     _relative_clip_width or
     _relative_clip_height
    clip_x = _relative_clip_x&.resolve(@width) || 0
    clip_y = _relative_clip_y&.resolve(@height) || 0
    clip_width = _relative_clip_width&.resolve(@width) || @width
    clip_height = _relative_clip_height&.resolve(@height) || @height
    uncliped_width = width - (clip_width - clip_x) + @width
    uncliped_height = height - (clip_height - clip_y) + @height
    {
      width: uncliped_width,
      height: uncliped_height,
      clip_x: x + clip_x,
      clip_y: y + clip_y,
      clip_width: clip_width,
      clip_height: clip_height,
    }
  else
    {
      width: width,
      height: height,
    }
  end
end
initialize_keep_ratio() click to toggle source
# File lib/rabbit/image/base.rb, line 127
def initialize_keep_ratio
  return unless @properties["keep_ratio"].nil?
  # For backward compatibility
  keep_scale = @properties["keep_scale"]
  if keep_scale.nil?
    @properties["keep_ratio"] = true
  else
    @properties["keep_ratio"] = keep_scale
  end
end
load_data(data) click to toggle source
# File lib/rabbit/image/base.rb, line 138
def load_data(data)
  loader = ImageDataLoader.new(data)
  begin
    loader.load
  rescue ImageLoadError
    raise ImageLoadError.new("#{@filename}: #{$!.message}")
  end

  @width = loader.width
  @height = loader.height
  @pixbuf = loader.pixbuf
  @animation = loader.animation
  if @animation and not @animation.static_image?
    @animation_iterator = @animation.get_iter
  else
    @animation_iterator = nil
  end
  if @animation_timeout
    GLib::Source.remove(@animation_timeout)
    @animation_timeout = nil
  end
end
update_animation_timeout(canvas) click to toggle source
# File lib/rabbit/image/base.rb, line 161
def update_animation_timeout(canvas)
  delay_time = @animation_iterator.delay_time
  if delay_time > 0 and @animation_timeout.nil?
    @animation_timeout = GLib::Timeout.add(delay_time) do
      canvas.redraw
      @animation_timeout = nil
      # update_animation_timeout(canvas)
      GLib::Source::REMOVE
    end
  end
end