1 #!/usr/bin/env ruby
3 # simplepath.rb
4 # functions for digesting paths into a simple list structure
5 #
6 # Ruby port by MenTaLguY
7 #
8 # Copyright (C) 2005 Aaron Spike <aaron@ekips.org>
9 # Copyright (C) 2006 MenTaLguY <mental@rydia.net>
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 require 'strscan'
27 def lexPath(d)
28 # iterator which breaks path data
29 # identifies command and parameter tokens
31 scanner = StringScanner.new(d)
33 delim = /[ \t\r\n,]+/
34 command = /[MLHVCSQTAZmlhvcsqtaz]/
35 parameter = /(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)/
37 until scanner.eos?
38 scanner.skip(delim)
39 if m = scanner.scan(command)
40 yield m, true
41 elsif m = scanner.scan(parameter)
42 yield m, false
43 else
44 #TODO: create new exception
45 raise 'Invalid path data!'
46 end
47 end
48 end
50 PathDef = Struct.new :implicit_next, :param_count, :casts, :coord_types
51 PATHDEFS = {
52 'M' => PathDef['L', 2, [:to_f, :to_f], [:x,:y]],
53 'L' => PathDef['L', 2, [:to_f, :to_f], [:x,:y]],
54 'H' => PathDef['H', 1, [:to_f], [:x]],
55 'V' => PathDef['V', 1, [:to_f], [:y]],
56 'C' => PathDef['C', 6, [:to_f, :to_f, :to_f, :to_f, :to_f, :to_f], [:x,:y,:x,:y,:x,:y]],
57 'S' => PathDef['S', 4, [:to_f, :to_f, :to_f, :to_f], [:x,:y,:x,:y]],
58 'Q' => PathDef['Q', 4, [:to_f, :to_f, :to_f, :to_f], [:x,:y,:x,:y]],
59 'T' => PathDef['T', 2, [:to_f, :to_f], [:x,:y]],
60 'A' => PathDef['A', 7, [:to_f, :to_f, :to_f, :to_i, :to_i, :to_f, :to_f], [0,0,0,0,0,:x,:y]],
61 'Z' => PathDef['L', 0, [], []]
62 }
64 def parsePath(d)
65 # Parse SVG path and return an array of segments.
66 # Removes all shorthand notation.
67 # Converts coordinates to absolute.
69 retval = []
71 command = nil
72 outputCommand = nil
73 params = []
75 pen = [0.0,0.0]
76 subPathStart = pen
77 lastControl = pen
78 lastCommand = nil
80 lexPath(d) do |token, isCommand|
81 unless command
82 if isCommand
83 raise 'Invalid path, must begin with moveto.' \
84 unless lastCommand or token.upcase == 'M'
85 command = token
86 else
87 #command was omited
88 #use last command's implicit next command
89 raise 'Invalid path, no initial command.' unless lastCommand
90 if lastCommand =~ /[A-Z]/
91 command = PATHDEFS[lastCommand].implicit_next
92 else
93 command = PATHDEFS[lastCommand.upcase].implicit_next.downcase
94 end
95 end
96 outputCommand = command.upcase
97 else
98 raise 'Invalid number of parameters' if isCommand
99 param = token.send PATHDEFS[outputCommand].casts[params.length]
100 if command =~ /[a-z]/
101 case PATHDEFS[outputCommand].coord_types[params.length]
102 when :x: param += pen[0]
103 when :y: param += pen[1]
104 end
105 end
106 params.push param
107 end
109 if params.length == PATHDEFS[outputCommand].param_count
111 #Flesh out shortcut notation
112 case outputCommand
113 when 'H','V'
114 case outputCommand
115 when 'H': params.push pen[1]
116 when 'V': params.unshift pen[0]
117 end
118 outputCommand = 'L'
119 when 'S','T'
120 params.unshift(pen[1]+(pen[1]-lastControl[1]))
121 params.unshift(pen[0]+(pen[0]-lastControl[0]))
122 case outputCommand
123 when 'S': outputCommand = 'C'
124 when 'T': outputCommand = 'Q'
125 end
126 end
128 #current values become "last" values
129 case outputCommand
130 when 'M'
131 subPathStart = params[0,2]
132 pen = subPathStart
133 when 'Z'
134 pen = subPathStart
135 else
136 pen = params[-2,2]
137 end
139 case outputCommand
140 when 'Q','C'
141 lastControl = params[-4,2]
142 else
143 lastControl = pen
144 end
146 lastCommand = command
147 retval.push [outputCommand,params]
148 command = nil
149 params = []
150 end
151 end
153 raise 'Unexpected end of path' if command
155 return retval
156 end
158 def formatPath(a)
159 # Format SVG path data from an array
160 a.map { |cmd,params| "#{cmd} #{params.join(' ')}" }.join
161 end
163 def _transformPath(p)
164 p.each do |cmd,params|
165 coord_types = PATHDEFS[cmd].coord_types
166 for i in 0...(params.length)
167 yield params, i, coord_types[i]
168 end
169 end
170 end
172 def translatePath(p, x, y)
173 _transformPath(p) do |params, i, coord_type|
174 case coord_type
175 when :x: params[i] += x
176 when :y: params[i] += y
177 end
178 end
179 end
181 def scalePath(p, x, y)
182 _transformPath(p) do |params, i, coord_type|
183 case coord_type
184 when :x: params[i] *= x
185 when :y: params[i] *= y
186 end
187 end
188 end
190 def rotatePath(p, a, cx = 0, cy = 0)
191 return p if a == 0
192 _transformPath(p) do |params, i, coord_type|
193 if coord_type == :x
194 x = params[i] - cx
195 y = params[i + 1] - cy
196 r = Math.sqrt((x**2) + (y**2))
197 unless r.zero?
198 theta = Math.atan2(y, x) + a
199 params[i] = (r * Math.cos(theta)) + cx
200 params[i + 1] = (r * Math.sin(theta)) + cy
201 end
202 end
203 end
204 end