class Prawn::SVG::Calculators::Pixels::Measurement

Public Class Methods

to_pixels(value, axis_length = nil, font_size: Prawn::SVG::Properties::EM) click to toggle source
# File lib/prawn/svg/calculators/pixels.rb, line 5
def self.to_pixels(value, axis_length = nil, font_size: Prawn::SVG::Properties::EM)
  if value.is_a?(String)
    if match = value.match(/\d(em|ex|pc|cm|mm|in)$/)
      case match[1]
      when 'em'
        value.to_f * font_size
      when 'ex'
        value.to_f * (font_size / 2.0) # we don't have access to the x-height, so this is an approximation approved by the CSS spec
      when 'pc'
        value.to_f * 15 # according to http://www.w3.org/TR/SVG11/coords.html
      else
        send("#{match[1]}2pt", value.to_f)
      end
    elsif value[-1..-1] == "%"
      value.to_f * axis_length / 100.0
    else
      value.to_f
    end
  elsif value
    value.to_f
  end
end