lib/rabbit/element/image.rb in rabbit-0.6.4 vs lib/rabbit/element/image.rb in rabbit-0.9.0
- old
+ new
@@ -2,10 +2,11 @@
require 'rabbit/element/block-element'
module Rabbit
module Element
class Image
+ include Base
include BlockElement
include BlockHorizontalCentering
alias element_draw draw
include ImageManipulable
@@ -14,14 +15,23 @@
attr_reader :filename
attr_reader :caption
attr_reader :normalized_width, :normalized_height
attr_reader :relative_width, :relative_height
+ attr_reader :relative_margin_top, :relative_margin_bottom
+ attr_reader :relative_margin_left, :relative_margin_right
+ attr_reader :relative_padding_top, :relative_padding_bottom
+ attr_reader :relative_padding_left, :relative_padding_right
def initialize(filename, prop)
@filename = filename
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?
@@ -33,17 +43,22 @@
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
def draw_element(canvas, x, y, w, h, simulation)
@@ -131,12 +146,37 @@
image_draw(canvas, x, y, @draw_parameters)
end
[x, y + height, w, h - height]
end
+ 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
+
+ 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
+
def adjust_size(canvas, x, y, w, h)
base_w = w
- base_h = (@oh || h) - @padding_top - @padding_bottom
+ 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