class Prawn::SVG::Calculators::AspectRatio
Attributes
align[R]
defer[R]
height[R]
width[R]
x[R]
y[R]
Public Class Methods
new(value, container_dimensions, object_dimensions)
click to toggle source
# File lib/prawn/svg/calculators/aspect_ratio.rb, line 6 def initialize(value, container_dimensions, object_dimensions) values = (value || 'xMidYMid meet').split @x = @y = 0 if values.first == 'defer' @defer = true values.shift end @align, @meet_or_slice = values w_container, h_container = container_dimensions w_object, h_object = object_dimensions container_ratio = w_container / h_container.to_f object_ratio = w_object / h_object.to_f if @align == 'none' @width, @height = container_dimensions else matches = @align.to_s.strip.match(/\Ax(Min|Mid|Max)Y(Min|Mid|Max)\z/i) || [nil, 'Mid', 'Mid'] if (container_ratio > object_ratio) == slice? @width = w_container @height = w_container / object_ratio @y = case matches[2].downcase when 'min' then 0 when 'mid' then (h_container - (w_container / object_ratio)) / 2 when 'max' then h_container - (w_container / object_ratio) end else @width = h_container * object_ratio @height = h_container @x = case matches[1].downcase when 'min' then 0 when 'mid' then (w_container - (h_container * object_ratio)) / 2 when 'max' then w_container - (h_container * object_ratio) end end end end
Public Instance Methods
inspect()
click to toggle source
# File lib/prawn/svg/calculators/aspect_ratio.rb, line 56 def inspect "[AspectRatio: #{@width},#{@height} offset #{@x},#{@y}]" end
meet?()
click to toggle source
# File lib/prawn/svg/calculators/aspect_ratio.rb, line 52 def meet? @meet_or_slice != 'slice' end
slice?()
click to toggle source
# File lib/prawn/svg/calculators/aspect_ratio.rb, line 48 def slice? @meet_or_slice == 'slice' end