class Rabbit::Element::Image

Attributes

caption[R]
filename[R]
normalized_height[R]
normalized_width[R]
relative_height[R]
relative_margin_bottom[R]
relative_margin_left[R]
relative_margin_right[R]
relative_margin_top[R]
relative_padding_bottom[R]
relative_padding_left[R]
relative_padding_right[R]
relative_padding_top[R]
relative_width[R]

Public Class Methods

new(filename, prop) click to toggle source
Calls superclass method Rabbit::ImageManipulable::new
# File lib/rabbit/element/image.rb, line 25
def initialize(filename, prop)
  @filename = filename
  prop = Utils.stringify_hash_key(prop)
  super(filename, prop)
  normalized_prop = {}
  prop.each do |name, value|
    normalized_prop[name.gsub(/-/, '_')] = value
  end
  prop = normalized_prop
  %w(caption dither_mode).each do |name|
    instance_variable_set("@#{name}", prop[name])
  end
  %w(keep_scale keep_ratio).each do |name|
    unless prop[name].nil?
      self.keep_ratio = true_value?(prop[name])
    end
  end
  %w(as_large_as_possible).each do |name|
    instance_variable_set("@#{name}", true_value?(prop[name]))
  end
  %w(width height
     x_dither y_dither
     normalized_width normalized_height
     relative_width relative_height
     relative_margin_top relative_margin_bottom
     relative_margin_left relative_margin_right
     relative_padding_top relative_padding_bottom
     relative_padding_left relative_padding_right
    ).each do |name|
    begin
      instance_variable_set("@#{name}", prop[name] && Integer(prop[name]))
    rescue ArgumentError
      raise InvalidImageSizeError.new(filename, name, prop[name])
    end
  end

  setup_draw_parameters(prop)
  resize(@width, @height)
end

Public Instance Methods

_compile(canvas, x, y, w, h)
Alias for: compile
as_large_as_possible?() click to toggle source
# File lib/rabbit/element/image.rb, line 124
def as_large_as_possible?
  @as_large_as_possible
end
compile(canvas, x, y, w, h) click to toggle source
Calls superclass method Rabbit::Element::Base#compile
# File lib/rabbit/element/image.rb, line 111
def compile(canvas, x, y, w, h)
  super
  adjust_size(canvas, @x, @y, @w, @h)
end
Also aliased as: _compile
compile_for_horizontal_centering(canvas, x, y, w, h) click to toggle source
# File lib/rabbit/element/image.rb, line 107
def compile_for_horizontal_centering(canvas, x, y, w, h)
  _compile(canvas, x, y, w, h)
end
dither_mode() click to toggle source
# File lib/rabbit/element/image.rb, line 88
def dither_mode
  @dither_mode ||= "normal"
  mode_name = "DITHER_#{@dither_mode.upcase}"
  if Gdk::RGB.const_defined?(mode_name)
    Gdk::RGB.const_get(mode_name)
  else
    Gdk::RGB::DITHER_NORMAL
  end
end
draw_element(canvas, x, y, w, h, simulation) click to toggle source
# File lib/rabbit/element/image.rb, line 65
def draw_element(canvas, x, y, w, h, simulation)
  draw_image(canvas, x, y, w, h, simulation)
end
height() click to toggle source
Calls superclass method Rabbit::Element::Base#height
# File lib/rabbit/element/image.rb, line 120
def height
  super + @padding_top + @padding_bottom
end
text() click to toggle source
# File lib/rabbit/element/image.rb, line 69
def text
  @caption.to_s
end
to_html(generator) click to toggle source
# File lib/rabbit/element/image.rb, line 77
def to_html(generator)
  src = generator.save_pixbuf(pixbuf, File.basename(@filename))
  html = "<img "
  if @caption
    alt = generator.h(@caption)
    html << "title=\"#{alt}\" alt=\"#{alt}\" "
  end
  html << "src=\"#{src}\" />"
  html
end
to_rd() click to toggle source
# File lib/rabbit/element/image.rb, line 73
def to_rd
  text
end
width() click to toggle source
Calls superclass method Rabbit::Element::Base#width
# File lib/rabbit/element/image.rb, line 116
def width
  super + @padding_left + @padding_right
end
x_dither() click to toggle source
# File lib/rabbit/element/image.rb, line 98
def x_dither
  @x_dither || 0
end
y_dither() click to toggle source
# File lib/rabbit/element/image.rb, line 102
def y_dither
  @y_dither || 0
end

Private Instance Methods

adjust_margin(w, h) click to toggle source
# File lib/rabbit/element/image.rb, line 152
def adjust_margin(w, h)
  @margin_top =
    make_relative_size(@relative_margin_top, h) || @margin_top
  @margin_bottom =
    make_relative_size(@relative_margin_bottom, h) || @margin_bottom
  @margin_left =
    make_relative_size(@relative_margin_left, w) || @margin_left
  @margin_right =
    make_relative_size(@relative_margin_right, w) || @margin_right
end
adjust_padding(w, h) click to toggle source
# File lib/rabbit/element/image.rb, line 163
def adjust_padding(w, h)
  @padding_top =
    make_relative_size(@relative_padding_top, h) || @padding_top
  @padding_bottom =
    make_relative_size(@relative_padding_bottom, h) || @padding_bottom
  @padding_left =
    make_relative_size(@relative_padding_left, w) || @padding_left
  @padding_right =
    make_relative_size(@relative_padding_right, w) || @padding_right
end
adjust_size(canvas, x, y, w, h) click to toggle source
# File lib/rabbit/element/image.rb, line 174
def adjust_size(canvas, x, y, w, h)
  base_w = w
  base_h = @oh || h
  adjust_margin(base_w, base_h)
  adjust_padding(base_w, base_h)
  base_h = base_h - @padding_top - @padding_bottom
  if @as_large_as_possible
    iw = base_w - x
    ih = base_h - y
    if iw.to_f / original_width > ih.to_f / original_height
      iw = nil
    else
      ih = nil
    end
  else
    nw = make_normalized_size(@normalized_width)
    nh = make_normalized_size(@normalized_height)
    rw = make_relative_size(@relative_width, base_w)
    rh = make_relative_size(@relative_height, base_h)
    iw = nw || rw
    ih = nh || rh
  end
  resize(iw, ih)
end
draw_image(canvas, x, y, w, h, simulation) click to toggle source
# File lib/rabbit/element/image.rb, line 145
def draw_image(canvas, x, y, w, h, simulation)
  unless simulation
    image_draw(canvas, x, y, @draw_parameters)
  end
  [x, y + height, w, h - height]
end
make_normalized_size(size) click to toggle source
# File lib/rabbit/element/image.rb, line 199
def make_normalized_size(size)
  size && screen_size(size)
end
make_relative_size(size, parent_size) click to toggle source
# File lib/rabbit/element/image.rb, line 203
def make_relative_size(size, parent_size)
  size && parent_size && ((size / 100.0) * parent_size).ceil
end
setup_draw_parameters(prop) click to toggle source
# File lib/rabbit/element/image.rb, line 129
def setup_draw_parameters(prop)
  @draw_parameters = {}

  @draw_parameters[:reflect] = {} if true_value?(prop["reflect"])
  [:ratio, :alpha].each do |key|
    name = "reflect_#{key}"
    value = prop[name]
    next unless value
    @draw_parameters[:reflect] ||= {}
    @draw_parameters[:reflect][key] = Float(value)
  end

  alpha = prop["alpha"]
  @draw_parameters[:alpha] = Float(alpha) if alpha
end
true_value?(value) click to toggle source
# File lib/rabbit/element/image.rb, line 207
def true_value?(value)
  value == true or value == "true"
end