File: simplepath.rb

package info (click to toggle)
inkscape 0.47.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 122,072 kB
  • ctags: 46,938
  • sloc: cpp: 349,791; ansic: 31,306; python: 14,868; xml: 6,613; sh: 5,041; makefile: 1,773; perl: 1,486; asm: 675; ruby: 148
file content (207 lines) | stat: -rwxr-xr-x 6,259 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env ruby

# simplepath.rb
# functions for digesting paths into a simple list structure
#
# Ruby port by MenTaLguY
#
# Copyright (C) 2005 Aaron Spike  <aaron@ekips.org>
# Copyright (C) 2006 MenTaLguY  <mental@rydia.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

require 'strscan'

def lexPath(d)
    # iterator which breaks path data 
    # identifies command and parameter tokens

    scanner = StringScanner.new(d)

    delim = /[ \t\r\n,]+/
    command = /[MLHVCSQTAZmlhvcsqtaz]/
    parameter = /(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)/

    until scanner.eos?
        scanner.skip(delim)
        if m = scanner.scan(command)
            yield m, true
        elsif m = scanner.scan(parameter)
            yield m, false
        else
            #TODO: create new exception
            raise 'Invalid path data!'
        end
    end
end

PathDef = Struct.new :implicit_next, :param_count, :casts, :coord_types
PATHDEFS = {
    'M' => PathDef['L', 2, [:to_f, :to_f], [:x,:y]], 
    'L' => PathDef['L', 2, [:to_f, :to_f], [:x,:y]], 
    'H' => PathDef['H', 1, [:to_f], [:x]], 
    'V' => PathDef['V', 1, [:to_f], [:y]], 
    'C' => PathDef['C', 6, [:to_f, :to_f, :to_f, :to_f, :to_f, :to_f], [:x,:y,:x,:y,:x,:y]], 
    'S' => PathDef['S', 4, [:to_f, :to_f, :to_f, :to_f], [:x,:y,:x,:y]], 
    'Q' => PathDef['Q', 4, [:to_f, :to_f, :to_f, :to_f], [:x,:y,:x,:y]], 
    'T' => PathDef['T', 2, [:to_f, :to_f], [:x,:y]], 
    '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]], 
    'Z' => PathDef['L', 0, [], []]
}

def parsePath(d)
    # Parse SVG path and return an array of segments.
    # Removes all shorthand notation.
    # Converts coordinates to absolute.

    retval = []

    command = nil
    outputCommand = nil
    params = []

    pen = [0.0,0.0]
    subPathStart = pen
    lastControl = pen
    lastCommand = nil

    lexPath(d) do |token, isCommand|
        raise 'Invalid number of parameters' if command and isCommand

        unless command
            if isCommand
                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
                raise 'Invalid path, no initial command.' unless lastCommand
                if lastCommand =~ /[A-Z]/
                    command = PATHDEFS[lastCommand].implicit_next
                else
                    command = PATHDEFS[lastCommand.upcase].implicit_next.downcase
                end
            end
            outputCommand = command.upcase
        end

        unless isCommand
            param = token.send PATHDEFS[outputCommand].casts[params.length]
            if command =~ /[a-z]/
                case PATHDEFS[outputCommand].coord_types[params.length]
                when :x: param += pen[0]
                when :y: param += pen[1]
                end
            end
            params.push param
        end

        if params.length == PATHDEFS[outputCommand].param_count

            #Flesh out shortcut notation
            case outputCommand
            when 'H','V'
                case outputCommand
                when 'H': params.push pen[1]
                when 'V': params.unshift pen[0]
                end
                outputCommand = 'L'
            when 'S','T'
                params.unshift(pen[1]+(pen[1]-lastControl[1]))
                params.unshift(pen[0]+(pen[0]-lastControl[0]))
                case outputCommand
                when 'S': outputCommand = 'C'
                when 'T': outputCommand = 'Q'
                end
            end

            #current values become "last" values
            case outputCommand
            when 'M'
                subPathStart = params[0,2]
                pen = subPathStart
            when 'Z'
                pen = subPathStart
            else
                pen = params[-2,2]
            end

            case outputCommand
            when 'Q','C'
                lastControl = params[-4,2]
            else
                lastControl = pen
            end

            lastCommand = command
            retval.push [outputCommand,params]
            command = nil
            params = []
        end
    end

    raise 'Unexpected end of path' if command

    return retval
end

def formatPath(a)
    # Format SVG path data from an array
    a.map { |cmd,params| "#{cmd} #{params.join(' ')}" }.join
end

def _transformPath(p)
    p.each do |cmd,params|
        coord_types = PATHDEFS[cmd].coord_types
        for i in 0...(params.length)
            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)
    _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
    _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
end