class Rabbit::Element::Image

Public Class Methods

new(filename, props) click to toggle source
Calls superclass method Rabbit::ImageManipulable::new
# File lib/rabbit/element/image.rb, line 32
def initialize(filename, props)
  super(filename, props)
  setup_draw_parameters
  resize(properties.get_size("width", @filename),
         properties.get_size("height", @filename))
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?
  properties.get_boolean("as_large_as_possible")
end
caption() click to toggle source
# File lib/rabbit/element/image.rb, line 43
def caption
  self["caption"]
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
draw_element(canvas, x, y, w, h, simulation) click to toggle source
# File lib/rabbit/element/image.rb, line 39
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
relative_height() click to toggle source
# File lib/rabbit/element/image.rb, line 70
def relative_height
  properties.get_relative_size("relative_height", @filename)
end
relative_margin_bottom() click to toggle source
# File lib/rabbit/element/image.rb, line 78
def relative_margin_bottom
  properties.get_relative_size("relative_margin_bottom", @filename)
end
relative_margin_left() click to toggle source
# File lib/rabbit/element/image.rb, line 82
def relative_margin_left
  properties.get_relative_size("relative_margin_left", @filename)
end
relative_margin_right() click to toggle source
# File lib/rabbit/element/image.rb, line 86
def relative_margin_right
  properties.get_relative_size("relative_margin_right", @filename)
end
relative_margin_top() click to toggle source
# File lib/rabbit/element/image.rb, line 74
def relative_margin_top
  properties.get_relative_size("relative_margin_top", @filename)
end
relative_padding_bottom() click to toggle source
# File lib/rabbit/element/image.rb, line 94
def relative_padding_bottom
  properties.get_relative_size("relative_padding_bottom", @filename)
end
relative_padding_left() click to toggle source
# File lib/rabbit/element/image.rb, line 98
def relative_padding_left
  properties.get_relative_size("relative_padding_left", @filename)
end
relative_padding_right() click to toggle source
# File lib/rabbit/element/image.rb, line 102
def relative_padding_right
  properties.get_relative_size("relative_padding_right", @filename)
end
relative_padding_top() click to toggle source
# File lib/rabbit/element/image.rb, line 90
def relative_padding_top
  properties.get_relative_size("relative_padding_top", @filename)
end
relative_width() click to toggle source
# File lib/rabbit/element/image.rb, line 66
def relative_width
  properties.get_relative_size("relative_width", @filename)
end
text() click to toggle source
# File lib/rabbit/element/image.rb, line 47
def text
  caption.to_s
end
to_html(generator) click to toggle source
# File lib/rabbit/element/image.rb, line 55
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 51
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

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 =
    relative_margin_top&.resolve(h) || @margin_top
  @margin_bottom =
    relative_margin_bottom&.resolve(h) || @margin_bottom
  @margin_left =
    relative_margin_left&.resolve(w) || @margin_left
  @margin_right =
    relative_margin_right&.resolve(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 =
    relative_padding_top&.resolve(h) || @padding_top
  @padding_bottom =
    relative_padding_bottom&.resolve(h) || @padding_bottom
  @padding_left =
    relative_padding_left&.resolve(w) || @padding_left
  @padding_right =
    relative_padding_right&.resolve(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
    iw = relative_width&.resolve(base_w)
    ih = relative_height&.resolve(base_h)
  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
setup_draw_parameters() click to toggle source
# File lib/rabbit/element/image.rb, line 129
def setup_draw_parameters
  @draw_parameters = {}

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

  alpha = self["alpha"]
  @draw_parameters[:alpha] = Float(alpha) if alpha
end