class Rabbit::ImageManipulable::Base

Attributes

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

Public Class Methods

delegate() click to toggle source
# File lib/rabbit/image/base.rb, line 31
def delegate
  extend Forwardable

  def_delegators(:@delegated_loader,
                 :draw,
                 :ensure_resize,
                 :height,
                 :internal_pixbuf,
                 :keep_ratio,
                 :keep_ratio=,
                 :keep_ratio?,
                 :original_height,
                 :original_width,
                 :pixbuf,
                 :resize,
                 :update_size,
                 :width)
end
new(filename, props, canvas: nil) click to toggle source
# File lib/rabbit/image/base.rb, line 57
def initialize(filename, props, canvas: nil)
  @filename = filename
  @properties = Properties.new(props)
  @canvas = canvas
  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 70
def [](key)
  @properties[key]
end
[]=(key, value) click to toggle source
# File lib/rabbit/image/base.rb, line 74
def []=(key, value)
  @properties[key] = value
end
draw(canvas, x, y, params={}) click to toggle source
# File lib/rabbit/image/base.rb, line 139
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 97
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 84
def keep_ratio=(value)
  @properties.keep_ratio = value
end
keep_ratio?() click to toggle source
# File lib/rabbit/image/base.rb, line 78
def keep_ratio?
  @properties.keep_ratio
end
Also aliased as: keep_ratio
pixbuf() click to toggle source
# File lib/rabbit/image/base.rb, line 88
def pixbuf
  @pixbuf
end
relative_clip_height() click to toggle source
# File lib/rabbit/image/base.rb, line 114
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 110
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 102
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 106
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 118
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 92
def width
  (relative_clip_width&.resolve(@width) || @width) -
    (relative_clip_x&.resolve(@width) || 0)
end

Private Instance Methods

cache_processed_data(canvas, input, extension) { || ... } click to toggle source

TODO: Move to more suitable location

# File lib/rabbit/image/base.rb, line 229
def cache_processed_data(canvas, input, extension)
  tmp_dir_name = canvas&.tmp_dir_name
  return yield unless tmp_dir_name
  hash = compute_hash(input)
  cached_path = File.join(tmp_dir_name, "#{hash}.#{extension}")
  unless File.exist?(cached_path)
    processed_path = yield
    FileUtils.cp(processed_path, cached_path)
  end
  cached_path
end
compute_hash(input) click to toggle source
# File lib/rabbit/image/base.rb, line 241
def compute_hash(input)
  digest = Digest::SHA2.new
  add_data = lambda do |data|
    case data
    when Array
      data.each do |element|
        add_data.call(element)
      end
    when Hash
      data.each do |key, value|
        add_data.call(key)
        add_data.call(value)
      end
    when String
      digest << data
    when IO
      loop do
        chunk = data.read(4096)
        break if chunk.nil?
        digest << chunk
      end
    end
  end
  add_data.call(input)
  digest.hexdigest
end
default_draw_params(x, y) click to toggle source
# File lib/rabbit/image/base.rb, line 197
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 151
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 162
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 185
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