]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/io/deprecated/private/SVGstrPath2SVGpath.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / io / deprecated / private / SVGstrPath2SVGpath.m
1 %% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
2 %% 
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
6 %%    any later version.
7 %%
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.
12 %%
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/>.
15
16 function SVGpath = SVGstrPath2SVGpath (SVGstrPath)
17
18     nPaths = numel (SVGstrPath);
19     SVGpath = repmat (struct('coord', [], 'closed', [], 'id', []), 1, nPaths);
20
21     for ip = 1:nPaths
22         path = SVGstrPath{ip};
23
24         % Match data
25         [s e te m] = regexpi (path, 'd="(?:(?!").)*');
26         data=strtrim (m{1});
27         % parse data
28         d = parsePathData (data);
29         SVGpath(ip).coord = d.coord;
30         SVGpath(ip).closed = d.closed;
31
32         % Match id
33         [s e te m] = regexp (path, 'id="(?:(?!").)*');
34         if ~isempty (m)
35             SVGpath(ip).id = strtrim (m{1}(5:end));
36         end
37     end
38
39 end
40
41 function d = parsePathData (data)
42     
43     d = struct ('coord', [], 'closed', []);
44     
45     [s e te comm] = regexp (data, '[MmLlHhVvCcSsQqTtAaZz]{1}');
46     % TODO
47     % This info could be used to preallocate d
48     [zcomm zpos] = ismember ({'Z','z'}, comm);
49     
50     if any (zcomm)
51       d.closed = true;
52     else
53       d.closed = false;
54       s(end+1) = length (data);
55     end  
56     comm(zpos(zcomm)) = [];
57     ncomm = size (comm, 2);
58     for ic = 1:ncomm
59     
60       switch comm{ic}
61           case {'M','L'}
62             [x y] = strread (data(s(ic) + 1 : s(ic + 1) - 1), ...
63                                               '%f%f', 'delimiter', ', ');
64             coord = [x y];
65
66           case 'm'
67             [x y] = strread( data(s(ic) + 1 : s(ic + 1) - 1), ...
68                                               '%f%f', 'delimiter', ', ');
69             nc = size (x, 1);
70             coord = [x y];
71             if ic == 1
72                 % relative moveto at begining of data.
73                 % First coordinates are absolute, the rest are relative.
74                 coord = cumsum (coord);
75             else
76                 % Relative moveto.
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.'])
83             end
84
85           case 'l'
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', ', ');
89             nc = size (x, 1);
90             coord = [x y]
91             coord(1,:) =coord(1,:) + d.coord(end,:);
92             coord = cumsum(coord);
93
94           otherwise
95               warning('svg2oct:Warning',...
96                       'Path data command "%s" not implemented yet.',comm{ic});
97       end
98
99       nc = size(coord,1);
100       d.coord(end+1:end+nc,:) = coord;
101
102     end
103 end