Module: Emfsvg::Svg::AttributeParser

Defined in:
lib/emfsvg/svg/attribute_parser.rb

Overview

Helpers for parsing common SVG attribute formats (numbers, points, path data). Used by Element subclasses.

Class Method Summary collapse

Class Method Details

.float(value, default: 0.0) ⇒ Object



10
11
12
13
14
15
# File 'lib/emfsvg/svg/attribute_parser.rb', line 10

def float(value, default: 0.0)
  return default if value.nil? || value.strip.empty?

  match = value.strip.match(/\A(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/)
  match ? match[1].to_f : default
end

.integer(value, default: 0) ⇒ Object



17
18
19
# File 'lib/emfsvg/svg/attribute_parser.rb', line 17

def integer(value, default: 0)
  float(value, default: default.to_f).to_i
end

.points(value) ⇒ Object

Parse a points list like "1,2 3,4 5,6" into an array of [x, y] pairs.



23
24
25
26
27
28
# File 'lib/emfsvg/svg/attribute_parser.rb', line 23

def points(value)
  return [] if value.nil? || value.strip.empty?

  nums = value.strip.split(/[\s,]+/).map(&:to_f)
  nums.each_slice(2).to_a
end