prawn-svg 0.12.0.9 → 0.12.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 794edc93815eb1be1232a47f423a2674e3954ef2
4
- data.tar.gz: 32b09e40134dfe543a9a4ca1fcf8f4d23264d307
3
+ metadata.gz: d0b52746c9532e73303a557f3036a453670acc1f
4
+ data.tar.gz: 2f60c07e2e83617b5068da3ed62aa88882964baf
5
5
  SHA512:
6
- metadata.gz: b96820f0566d903c2fa4234ab8a400c022fa34f77685912c17deb864bab7236918d57acf4bc8b03caca304e07b973e5c6eff07d84f5fdcfe38397419122d3fbe
7
- data.tar.gz: 4e013c2b63dbe5472117c820f342668e79e38bcbeb5551088ffebe23293aeecd87207a722822ae906ab05e752eacedbd2bc4f6a9c444143366391f298a7069cb
6
+ metadata.gz: ec07a8b000e4564f0a13c442028cf20d388ba8d3a15309ee48e1b2858415e79d1f13a8e4e74ef4070e04dcaf6a489dfe54226b3cb58002e74a71d34ae344e3cd
7
+ data.tar.gz: 141d6c320f073e83a3360d83abf19ad71e0da9de6f6dec11af4665826f94e174075b553bf9abd08af42a4da8600f502214238ca53e0b2686866ae41d7fe4754d
@@ -4,43 +4,33 @@ module Prawn
4
4
  # Raised if the SVG path cannot be parsed.
5
5
  InvalidError = Class.new(StandardError)
6
6
 
7
+ INSIDE_SPACE_REGEXP = /[ \t\r\n,]*/
8
+ OUTSIDE_SPACE_REGEXP = /[ \t\r\n]*/
9
+ INSIDE_REGEXP = /#{INSIDE_SPACE_REGEXP}([+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:(?<=[0-9])e[+-]?[0-9]+)?)/
10
+ VALUES_REGEXP = /^#{INSIDE_REGEXP}/
11
+ COMMAND_REGEXP = /^#{OUTSIDE_SPACE_REGEXP}([A-Za-z])((?:#{INSIDE_REGEXP})*)#{OUTSIDE_SPACE_REGEXP}/
12
+
13
+
7
14
  #
8
15
  # Parses an SVG path and returns a Prawn-compatible call tree.
9
16
  #
10
17
  def parse(data)
11
- cmd = values = nil
12
- value = ""
13
18
  @subpath_initial_point = @last_point = nil
14
19
  @previous_control_point = @previous_quadratic_control_point = nil
15
20
  @calls = []
16
21
 
17
- data.each_char do |c|
18
- if c >= 'A' && c <= 'Z' || (c >= 'a' && c <= 'z' && c != 'e')
19
- values << value.to_f if value != ""
20
- run_path_command(cmd, values) if cmd
21
- cmd = c
22
- values = []
23
- value = ""
24
- elsif c >= '0' && c <= '9' || c == '.' || c == "-" || c == "e" # handle scientific notation, e.g. 10e-4
25
- unless cmd
26
- raise InvalidError, "Numerical value specified before character command in SVG path data"
27
- end
28
- value << c
29
- elsif c == ' ' || c == "\t" || c == "\r" || c == "\n" || c == ","
30
- if value != ""
31
- values << value.to_f
32
- value = ""
33
- end
34
- elsif c == '-'
35
- values << value.to_f
36
- value = c
37
- else
38
- raise InvalidError, "Invalid character '#{c}' in SVG path data"
39
- end
40
- end
22
+ data = data.gsub(/#{OUTSIDE_SPACE_REGEXP}$/, '')
41
23
 
42
- values << value.to_f if value != ""
43
- run_path_command(cmd, values) if cmd
24
+ matched_commands = match_all(data, COMMAND_REGEXP)
25
+ raise InvalidError, "Invalid/unsupported syntax for SVG path data" if matched_commands.nil?
26
+
27
+ matched_commands.each do |matched_command|
28
+ command = matched_command[1]
29
+ matched_values = match_all(matched_command[2], VALUES_REGEXP)
30
+ raise "should be impossible to have invalid inside data, but we ended up here" if matched_values.nil?
31
+ values = matched_values.collect {|value| value[1].to_f}
32
+ run_path_command(command, values)
33
+ end
44
34
 
45
35
  @calls
46
36
  end
@@ -183,6 +173,17 @@ module Prawn
183
173
  @previous_control_point = nil unless %w(C S).include?(upcase_command)
184
174
  @previous_quadratic_control_point = nil unless %w(Q T).include?(upcase_command)
185
175
  end
176
+
177
+ def match_all(string, regexp) # regexp must start with ^
178
+ result = []
179
+ while string != ""
180
+ matches = string.match(regexp)
181
+ result << matches
182
+ return if matches.nil?
183
+ string = matches.post_match
184
+ end
185
+ result
186
+ end
186
187
  end
187
188
  end
188
189
  end
@@ -1,5 +1,5 @@
1
1
  module Prawn
2
2
  module Svg
3
- VERSION = '0.12.0.9'
3
+ VERSION = '0.12.0.10'
4
4
  end
5
5
  end
@@ -9,11 +9,11 @@ describe Prawn::Svg::Parser::Path do
9
9
  it "correctly parses a valid path" do
10
10
  calls = []
11
11
  @path.stub!(:run_path_command) {|*args| calls << args}
12
- @path.parse("A12.34 -56.78 89B4 5 c31,-2e-5C 6,7 T QX 0 Z")
12
+ @path.parse("A12.34 -56.78 89B4 5 12-34 -.5.7+3 2.3e3 4e4 4e+4 c31,-2e-5C 6,7 T QX 0 Z")
13
13
 
14
14
  calls.should == [
15
15
  ["A", [12.34, -56.78, 89]],
16
- ["B", [4, 5]],
16
+ ["B", [4, 5, 12, -34, -0.5, 0.7, 3, 2.3e3, 4e4, 4e4]],
17
17
  ["c", [31, -2e-5]],
18
18
  ["C", [6, 7]],
19
19
  ["T", []],
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-svg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0.9
4
+ version: 0.12.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roger Nesbitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-04 00:00:00.000000000 Z
11
+ date: 2013-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prawn