Class: Prawn::SVG::Elements::Path

Inherits:
Base
  • Object
show all
Includes:
Calculators::ArcToBezierCurve, Pathable
Defined in:
lib/prawn/svg/elements/path.rb

Constant Summary collapse

INSIDE_SPACE_REGEXP =
/[ \t\r\n,]*/.freeze
OUTSIDE_SPACE_REGEXP =
/[ \t\r\n]*/.freeze
INSIDE_REGEXP =
/#{INSIDE_SPACE_REGEXP}(?>([+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:(?<=[0-9])[eE][+-]?[0-9]+)?))/.freeze
FLAG_REGEXP =
/#{INSIDE_SPACE_REGEXP}([01])/.freeze
COMMAND_REGEXP =
/^#{OUTSIDE_SPACE_REGEXP}([A-Za-z])((?:#{INSIDE_REGEXP})*)#{OUTSIDE_SPACE_REGEXP}/.freeze
A_PARAMETERS_REGEXP =
/^#{INSIDE_REGEXP}#{INSIDE_REGEXP}#{INSIDE_REGEXP}#{FLAG_REGEXP}#{FLAG_REGEXP}#{INSIDE_REGEXP}#{INSIDE_REGEXP}/.freeze
ONE_PARAMETER_REGEXP =
/^#{INSIDE_REGEXP}/.freeze
TWO_PARAMETER_REGEXP =
/^#{INSIDE_REGEXP}#{INSIDE_REGEXP}/.freeze
FOUR_PARAMETER_REGEXP =
/^#{INSIDE_REGEXP}#{INSIDE_REGEXP}#{INSIDE_REGEXP}#{INSIDE_REGEXP}/.freeze
SIX_PARAMETER_REGEXP =
/^#{INSIDE_REGEXP}#{INSIDE_REGEXP}#{INSIDE_REGEXP}#{INSIDE_REGEXP}#{INSIDE_REGEXP}#{INSIDE_REGEXP}/.freeze
COMMAND_MATCH_MAP =
{
  'A' => A_PARAMETERS_REGEXP,
  'C' => SIX_PARAMETER_REGEXP,
  'H' => ONE_PARAMETER_REGEXP,
  'L' => TWO_PARAMETER_REGEXP,
  'M' => TWO_PARAMETER_REGEXP,
  'Q' => FOUR_PARAMETER_REGEXP,
  'S' => FOUR_PARAMETER_REGEXP,
  'T' => TWO_PARAMETER_REGEXP,
  'V' => ONE_PARAMETER_REGEXP,
  'Z' => //
}.freeze
PARAMETERLESS_COMMANDS =
COMMAND_MATCH_MAP.select { |_, v| v == // }.map(&:first)
FLOAT_ERROR_DELTA =
1e-10

Constants inherited from Base

Base::COMMA_WSP_REGEXP, Base::MissingAttributesError, Base::PAINT_TYPES, Base::SVG_NAMESPACE, Base::SkipElementError, Base::SkipElementQuietly

Constants included from Attributes::Stroke

Attributes::Stroke::CAP_STYLE_TRANSLATIONS, Attributes::Stroke::JOIN_STYLE_TRANSLATIONS

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes, #base_calls, #calls, #document, #parent_calls, #properties, #source, #state

Instance Method Summary collapse

Methods included from Pathable

#bounding_box

Methods inherited from Base

#initialize, #name, #parse_and_apply, #process

Methods included from TransformParser

#parse_transform_attribute

Methods included from PDFMatrix

#load_matrix, #matrix_for_pdf, #rotation_matrix, #scale_matrix, #translation_matrix

Methods included from Attributes::Space

#parse_xml_space_attribute

Methods included from Attributes::Stroke

#parse_stroke_attributes_and_call

Methods included from Attributes::ClipPath

#parse_clip_path_attribute_and_call

Methods included from Attributes::Opacity

#parse_opacity_attributes_and_call

Methods included from Attributes::Transform

#parse_transform_attribute_and_call

Constructor Details

This class inherits a constructor from Prawn::SVG::Elements::Base

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



34
35
36
# File 'lib/prawn/svg/elements/path.rb', line 34

def commands
  @commands
end

Instance Method Details

#applyObject



57
58
59
60
# File 'lib/prawn/svg/elements/path.rb', line 57

def apply
  apply_commands
  apply_markers
end

#parseObject

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/prawn/svg/elements/path.rb', line 36

def parse
  require_attributes 'd'

  @commands = []
  @last_point = nil

  data = attributes['d'].gsub(/#{OUTSIDE_SPACE_REGEXP}$/, '')

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

  matched_commands.each do |(command, parameters)|
    regexp = COMMAND_MATCH_MAP[command.upcase] or break
    matched_values = match_all(parameters, regexp) or break
    values = matched_values.map { |value| value.map(&:to_f) }
    break if values.empty? && !PARAMETERLESS_COMMANDS.include?(command.upcase)

    parse_path_command(command, values)
  end
end