Class: Prawn::Svg::Parser::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/svg/parser/path.rb

Constant Summary collapse

InvalidError =

Raised if the SVG path cannot be parsed.

Class.new(StandardError)
INSIDE_SPACE_REGEXP =
/[ \t\r\n,]*/
OUTSIDE_SPACE_REGEXP =
/[ \t\r\n]*/
INSIDE_REGEXP =
/#{INSIDE_SPACE_REGEXP}([+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:(?<=[0-9])e[+-]?[0-9]+)?)/
VALUES_REGEXP =
/^#{INSIDE_REGEXP}/
COMMAND_REGEXP =
/^#{OUTSIDE_SPACE_REGEXP}([A-Za-z])((?:#{INSIDE_REGEXP})*)#{OUTSIDE_SPACE_REGEXP}/

Instance Method Summary collapse

Instance Method Details

#parse(data) ⇒ Object

Parses an SVG path and returns a Prawn-compatible call tree.

Raises:


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/prawn/svg/parser/path.rb', line 17

def parse(data)
  @subpath_initial_point = @last_point = nil
  @previous_control_point = @previous_quadratic_control_point = nil
  @calls = []

  data = data.gsub(/#{OUTSIDE_SPACE_REGEXP}$/, '')

  matched_commands = match_all(data, COMMAND_REGEXP)
  raise InvalidError, "Invalid/unsupported syntax for SVG path data" if matched_commands.nil?

  matched_commands.each do |matched_command|
    command = matched_command[1]
    matched_values = match_all(matched_command[2], VALUES_REGEXP)
    raise "should be impossible to have invalid inside data, but we ended up here" if matched_values.nil?
    values = matched_values.collect {|value| value[1].to_f}
    run_path_command(command, values)
  end

  @calls
end