Class: Prawn::SVG::Length

Inherits:
Struct
  • Object
show all
Defined in:
lib/prawn/svg/length.rb,
lib/prawn/svg/length.rb

Constant Summary collapse

REGEXP =
/\A([+-]?\d*(?:\.\d+)?)(em|rem|ex|px|in|cm|mm|pt|pc)?\z/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#unitObject

Returns the value of attribute unit

Returns:

  • (Object)

    the current value of unit



2
3
4
# File 'lib/prawn/svg/length.rb', line 2

def unit
  @unit
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



2
3
4
# File 'lib/prawn/svg/length.rb', line 2

def value
  @value
end

Class Method Details

.parse(value, positive_only: false) ⇒ Object



7
8
9
10
11
12
# File 'lib/prawn/svg/length.rb', line 7

def self.parse(value, positive_only: false)
  if (matches = value.match(REGEXP))
    number = Float(matches[1], exception: false)
    new(number, matches[2]&.downcase&.to_sym) if number && (!positive_only || number >= 0)
  end
end

Instance Method Details

#to_fObject



14
15
16
# File 'lib/prawn/svg/length.rb', line 14

def to_f
  value
end

#to_pixels(_axis_length, font_size) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/prawn/svg/length.rb', line 22

def to_pixels(_axis_length, font_size)
  case unit
  when :em
    value * font_size
  when :rem
    value * Properties::EM
  when :ex
    value * (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 * 15 # according to http://www.w3.org/TR/SVG11/coords.html
  when :in
    value * 72
  when :cm
    value * 10 * (72 / 25.4)
  when :mm
    value * (72 / 25.4)
  else
    value
  end
end

#to_sObject



18
19
20
# File 'lib/prawn/svg/length.rb', line 18

def to_s
  "#{value}#{unit}"
end