Class: Prawn::Markup::SizeConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/markup/support/size_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max) ⇒ SizeConverter

Returns a new instance of SizeConverter.



6
7
8
# File 'lib/prawn/markup/support/size_converter.rb', line 6

def initialize(max)
  @max = max
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



4
5
6
# File 'lib/prawn/markup/support/size_converter.rb', line 4

def max
  @max
end

Instance Method Details

#convert(string) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/prawn/markup/support/size_converter.rb', line 17

def convert(string)
  value = string.to_f
  if string.end_with?('%')
    value * max / 100.0
  elsif string.end_with?('cm')
    value.cm
  elsif string.end_with?('mm')
    value.mm
  else
    value
  end
end

#parse(width) ⇒ Object



10
11
12
13
14
15
# File 'lib/prawn/markup/support/size_converter.rb', line 10

def parse(width)
  return nil if width.to_s.strip.empty? || width.to_s == 'auto'

  points = convert(width)
  max ? [points, max].min : points
end