Class: Prawn::SVG::Calculators::AspectRatio
- Inherits:
-
Object
- Object
- Prawn::SVG::Calculators::AspectRatio
- Defined in:
- lib/prawn/svg/calculators/aspect_ratio.rb
Instance Attribute Summary collapse
-
#align ⇒ Object
readonly
Returns the value of attribute align.
-
#defer ⇒ Object
readonly
Returns the value of attribute defer.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
-
#initialize(value, container_dimensions, object_dimensions) ⇒ AspectRatio
constructor
A new instance of AspectRatio.
- #inspect ⇒ Object
- #meet? ⇒ Boolean
- #slice? ⇒ Boolean
Constructor Details
#initialize(value, container_dimensions, object_dimensions) ⇒ AspectRatio
Returns a new instance of AspectRatio.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 |
Instance Attribute Details
#align ⇒ Object (readonly)
Returns the value of attribute align.
3 4 5 |
# File 'lib/prawn/svg/calculators/aspect_ratio.rb', line 3 def align @align end |
#defer ⇒ Object (readonly)
Returns the value of attribute defer.
3 4 5 |
# File 'lib/prawn/svg/calculators/aspect_ratio.rb', line 3 def defer @defer end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
4 5 6 |
# File 'lib/prawn/svg/calculators/aspect_ratio.rb', line 4 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
4 5 6 |
# File 'lib/prawn/svg/calculators/aspect_ratio.rb', line 4 def width @width end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
4 5 6 |
# File 'lib/prawn/svg/calculators/aspect_ratio.rb', line 4 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
4 5 6 |
# File 'lib/prawn/svg/calculators/aspect_ratio.rb', line 4 def y @y end |
Instance Method Details
#inspect ⇒ Object
56 57 58 |
# File 'lib/prawn/svg/calculators/aspect_ratio.rb', line 56 def inspect "[AspectRatio: #{@width},#{@height} offset #{@x},#{@y}]" end |
#meet? ⇒ Boolean
52 53 54 |
# File 'lib/prawn/svg/calculators/aspect_ratio.rb', line 52 def meet? @meet_or_slice != 'slice' end |
#slice? ⇒ Boolean
48 49 50 |
# File 'lib/prawn/svg/calculators/aspect_ratio.rb', line 48 def slice? @meet_or_slice == 'slice' end |