Code

rename
[inkscape.git] / share / extensions / simplepath.rb
index b8f91232f304e6a7ab72f3b0e9428bda390d1b50..3d9b7b372142f823d406cb25db5a487d9475f16c 100755 (executable)
@@ -41,6 +41,7 @@ def lexPath(d)
         elsif m = scanner.scan(parameter)
             yield m, false
         else
+            #TODO: create new exception
             raise 'Invalid path data!'
         end
     end
@@ -77,29 +78,27 @@ def parsePath(d)
     lastCommand = nil
 
     lexPath(d) do |token, isCommand|
+        raise 'Invalid number of parameters' if command and isCommand
+
         unless command
             if isCommand
-                if lastCommand or token.upcase == 'M'
-                    command = token
-                else
-                    raise 'Invalid path, must begin with moveto.'
-                end
+                raise 'Invalid path, must begin with moveto.' \
+                  unless lastCommand or token.upcase == 'M'
+                command = token
             else
                 #command was omited
                 #use last command's implicit next command
-                if lastCommand
-                    if lastCommand =~ /[A-Z]/
-                        command = PATHDEFS[lastCommand].implicit_next
-                    else
-                        command = PATHDEFS[lastCommand.upcase].implicit_next.downcase
-                    end
+                raise 'Invalid path, no initial command.' unless lastCommand
+                if lastCommand =~ /[A-Z]/
+                    command = PATHDEFS[lastCommand].implicit_next
                 else
-                    raise 'Invalid path, no initial command.'    
+                    command = PATHDEFS[lastCommand.upcase].implicit_next.downcase
                 end
             end
             outputCommand = command.upcase
-        else
-            raise 'Invalid number of parameters' if isCommand
+        end
+
+        unless isCommand
             param = token.send PATHDEFS[outputCommand].casts[params.length]
             if command =~ /[a-z]/
                 case PATHDEFS[outputCommand].coord_types[params.length]
@@ -164,44 +163,44 @@ def formatPath(a)
     a.map { |cmd,params| "#{cmd} #{params.join(' ')}" }.join
 end
 
-def translatePath(p, x, y)
+def _transformPath(p)
     p.each do |cmd,params|
         coord_types = PATHDEFS[cmd].coord_types
         for i in 0...(params.length)
-            case coord_types[i]
-            when :x: params[i] += x
-            when :y: params[i] += y
-            end
+            yield params, i, coord_types[i]
+        end
+    end
+end
+
+def translatePath(p, x, y)
+    _transformPath(p) do |params, i, coord_type|
+        case coord_type
+        when :x: params[i] += x
+        when :y: params[i] += y
         end
     end
 end
 
 def scalePath(p, x, y)
-    p.each do |cmd,params|
-        coord_types = PATHDEFS[cmd].coord_types
-        for i in 0...(params.length)
-            case coord_types[i]
-            when :x: params[i] *= x
-            when :y: params[i] *= y
-            end
+    _transformPath(p) do |params, i, coord_type|
+        case coord_type
+        when :x: params[i] *= x
+        when :y: params[i] *= y
         end
     end
 end
 
 def rotatePath(p, a, cx = 0, cy = 0)
     return p if a == 0
-    p.each do |cmd,params|
-        coord_types = PATHDEFS[cmd].coord_types
-        for i in 0...(params.length)
-            if coord_types[i] == :x
-                x = params[i] - cx
-                y = params[i + 1] - cy
-                r = Math.sqrt((x**2) + (y**2))
-                unless r.zero?
-                    theta = Math.atan2(y, x) + a
-                    params[i] = (r * Math.cos(theta)) + cx
-                    params[i + 1] = (r * Math.sin(theta)) + cy
-                end
+    _transformPath(p) do |params, i, coord_type|
+        if coord_type == :x
+            x = params[i] - cx
+            y = params[i + 1] - cy
+            r = Math.sqrt((x**2) + (y**2))
+            unless r.zero?
+                theta = Math.atan2(y, x) + a
+                params[i] = (r * Math.cos(theta)) + cx
+                params[i + 1] = (r * Math.sin(theta)) + cy
             end
         end
     end