1 %% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
3 %% This program is free software: you can redistribute it and/or modify
4 %% it under the terms of the GNU General Public License as published by
5 %% the Free Software Foundation, either version 3 of the License, or
8 %% This program is distributed in the hope that it will be useful,
9 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
10 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 %% GNU General Public License for more details.
13 %% You should have received a copy of the GNU General Public License
14 %% along with this program. If not, see <http://www.gnu.org/licenses/>.
16 function SVGpath = SVGstrPath2SVGpath (SVGstrPath)
18 nPaths = numel (SVGstrPath);
19 SVGpath = repmat (struct('coord', [], 'closed', [], 'id', []), 1, nPaths);
22 path = SVGstrPath{ip};
25 [s e te m] = regexpi (path, 'd="(?:(?!").)*');
28 d = parsePathData (data);
29 SVGpath(ip).coord = d.coord;
30 SVGpath(ip).closed = d.closed;
33 [s e te m] = regexp (path, 'id="(?:(?!").)*');
35 SVGpath(ip).id = strtrim (m{1}(5:end));
41 function d = parsePathData (data)
43 d = struct ('coord', [], 'closed', []);
45 [s e te comm] = regexp (data, '[MmLlHhVvCcSsQqTtAaZz]{1}');
47 % This info could be used to preallocate d
48 [zcomm zpos] = ismember ({'Z','z'}, comm);
54 s(end+1) = length (data);
56 comm(zpos(zcomm)) = [];
57 ncomm = size (comm, 2);
62 [x y] = strread (data(s(ic) + 1 : s(ic + 1) - 1), ...
63 '%f%f', 'delimiter', ', ');
67 [x y] = strread( data(s(ic) + 1 : s(ic + 1) - 1), ...
68 '%f%f', 'delimiter', ', ');
72 % relative moveto at begining of data.
73 % First coordinates are absolute, the rest are relative.
74 coord = cumsum (coord);
77 % The coordinates are relative to the last one loaded
78 coord(1,:) =coord(1,:) + d.coord(end,:);
79 coord = cumsum(coord);
80 warning('svg2octWarning',['Relative moveto in path data.'...
81 ' May mean that the orginal path was not a simple polygon.' ...
82 ' If that is the case, the path will not display equally.'])
86 % Relative lineto, coordinates are relative to last point loaded.
87 [x y] = strread( data(s(ic) + 1 : s(ic + 1) - 1), ...
88 '%f%f', 'delimiter', ', ');
91 coord(1,:) =coord(1,:) + d.coord(end,:);
92 coord = cumsum(coord);
95 warning('svg2oct:Warning',...
96 'Path data command "%s" not implemented yet.',comm{ic});
100 d.coord(end+1:end+nc,:) = coord;