Module: Postsvg::Svg::AttributeParser
- Defined in:
- lib/postsvg/svg/attribute_parser.rb
Overview
Helpers for parsing presentation attributes into typed values. Used by Element.from_node factories.
Class Method Summary collapse
-
.length(text) ⇒ Object
Parse a length attribute ("10", "10px", "10pt", "50%").
- .number(text, default: nil) ⇒ Object
- .number_list(text) ⇒ Object
Class Method Details
.length(text) ⇒ Object
Parse a length attribute ("10", "10px", "10pt", "50%"). Returns a Float. Percentage values resolve to nil — the caller must know the reference dimension.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/postsvg/svg/attribute_parser.rb', line 13 def length(text) return nil if text.nil? || text.empty? match = text.strip.match(/\A(-?\d+(?:\.\d+)?)(px|pt|in|cm|mm|em|ex|%)?\z/) return nil unless match return nil if match[2] == "%" value = match[1].to_f case match[2] when "pt" then value * (1.0 / 72.0) * 72.0 # PS uses points natively when "in" then value * 72.0 when "cm" then value * (72.0 / 2.54) when "mm" then value * (72.0 / 25.4) else value end end |
.number(text, default: nil) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/postsvg/svg/attribute_parser.rb', line 37 def number(text, default: nil) return default if text.nil? || text.empty? match = text.strip.match(/\A(-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?)\z/) match ? match[1].to_f : default end |
.number_list(text) ⇒ Object
31 32 33 34 35 |
# File 'lib/postsvg/svg/attribute_parser.rb', line 31 def number_list(text) return [] if text.nil? || text.empty? text.scan(/-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?/).map(&:to_f) end |