class Rabbit::Element::Image

Public Class Methods

new(filename, props, canvas: nil) click to toggle source
Calls superclass method Rabbit::ImageManipulable::new
# File lib/rabbit/element/image.rb, line 32
def initialize(filename, props, canvas: nil)
  super(filename, props, canvas: canvas)
  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 132
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 45
def caption
  self["caption"]
end
caption_font_size() click to toggle source
# File lib/rabbit/element/image.rb, line 49
def caption_font_size
  properties.get_float("caption-font-size")
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 117
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 113
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)
  result = draw_image(canvas, x, y, w, h, simulation)
  draw_properties(canvas, x, y, w, h) unless simulation
  result
end
height() click to toggle source
Calls superclass method Rabbit::Element::Base#height
# File lib/rabbit/element/image.rb, line 128
def height
  super + @padding_top + @padding_bottom
end
Also aliased as: height_without_padding
height_without_padding()
Alias for: height
relative_height() click to toggle source
# File lib/rabbit/element/image.rb, line 76
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 84
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 88
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 92
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 80
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 100
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 104
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 108
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 96
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 72
def relative_width
  properties.get_relative_size("relative_width", @filename)
end
text() click to toggle source
# File lib/rabbit/element/image.rb, line 53
def text
  caption.to_s
end
to_html(generator) click to toggle source
# File lib/rabbit/element/image.rb, line 61
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 57
def to_rd
  text
end
width() click to toggle source
Calls superclass method Rabbit::Element::Base#width
# File lib/rabbit/element/image.rb, line 123
def width
  super + @padding_left + @padding_right
end
Also aliased as: width_without_padding
width_without_padding()
Alias for: width

Private Instance Methods

adjust_margin(w, h) click to toggle source
# File lib/rabbit/element/image.rb, line 160
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 171
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 182
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 153
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
draw_properties(canvas, base_x, base_y, base_w, base_h) click to toggle source
# File lib/rabbit/element/image.rb, line 203
def draw_properties(canvas, base_x, base_y, base_w, base_h)
  properties.draws.each do |type, *args|
    case type
    when "line"
      if args.last.is_a?(Hash)
        params = args.pop
      else
        params = nil
      end
      points = args.each_slice(2).collect do |x, y|
        x = (x * width_without_padding) + base_x
        y = (y * height_without_padding) + base_y
        [x, y]
      end
      params = normalize_params(params)
      color = params.delete(:color) || "black"
      canvas.draw_lines(points, color, params)
    when "rectangle"
      filled, x, y, w, h, params = args
      x = (x * width_without_padding) + base_x
      y = (y * height_without_padding) + base_y
      w = (w * width_without_padding)
      h = (h * height_without_padding)
      params = normalize_params(params)
      color = params.delete(:color) || "black"
      canvas.draw_rectangle(filled, x, y, w, h, color, params)
    when "text"
      text, x, y, params = args
      params = normalize_params(params)
      layout = canvas.make_layout(markup_text(text, params))
      x = (x * width_without_padding) + base_x
      y = (y * height_without_padding) + base_y
      color = params.delete(:color) || "black"
      canvas.draw_layout(layout, x, y, color, params)
    end
  end
end
markup_text(text, props) click to toggle source
# File lib/rabbit/element/image.rb, line 250
def markup_text(text, props)
  props.each do |name, value|
    formatter_name = Utils.to_class_name(name.to_s)
    next unless Format.const_defined?(formatter_name)
    formatter = Format.const_get(formatter_name).new(value)
    next unless formatter.text_formatter?
    text = formatter.format(text)
  end
  text
end
normalize_params(params) click to toggle source
# File lib/rabbit/element/image.rb, line 241
def normalize_params(params)
  return {} if params.nil?
  normalized_params = {}
  params.each do |key, value|
    normalized_params[key.to_sym] = value
  end
  normalized_params
end
setup_draw_parameters() click to toggle source
# File lib/rabbit/element/image.rb, line 137
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