]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/io/deprecated/private/getSVGPaths_py.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / io / deprecated / private / getSVGPaths_py.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 Paths = getSVGPaths_py (svg, varargin)
17
18   %% Call python script
19   if exist (svg,'file')
20   % read from file  
21     [st str]=system (sprintf ('python parsePath.py %s', svg));
22     
23   else
24   % inline SVG  
25     [st str]=system (sprintf ('python parsePath.py < %s', svg));
26   end
27   
28   %% Parse ouput
29   strpath = strsplit (str(1:end-1), '$', true);
30
31   npaths = numel (strpath);
32
33   %% Convert path data to polygons
34   for ip = 1:npaths
35
36     eval (strpath{ip});
37     %% FIXME: intialize struct with cell field
38     svgpath2.cmd = svgpath(1).cmd;
39     svgpath2.data = {svgpath.data};
40     
41     nD = length(svgpath2.cmd);
42     pathdata = cell (nD-1,1);
43     
44     point_end=[];
45     %% If the path is closed, last command is Z and we set initial point == final
46     if svgpath2.cmd(end) == 'Z'
47       nD -= 1;
48       point_end = svgpath2.data{1};
49     end
50
51     %% Initial point
52     points(1,:) = svgpath2.data{1};
53     
54     for jp = 2:nD
55       switch svgpath2.cmd(jp)
56         case 'L'
57           %% Straigth segment to polygon
58           points(2,:) = svgpath2.data{jp};
59           pp = [(points(2,:)-points(1,:))' points(1,:)'];
60           clear points
61           points(1,:) = [polyval(pp(1,:),1) polyval(pp(2,:),1)];
62           
63         case 'C'
64           %% Cubic bezier to polygon
65           points(2:4,:) = reshape (svgpath2.data{jp}, 2, 3).';
66           pp = cbezier2poly (points);
67           clear points
68           points(1,:) = [polyval(pp(1,:),1) polyval(pp(2,:),1)];
69       end
70       
71       pathdata{jp-1} = pp;
72     end
73     
74     if ~isempty(point_end)
75       %% Straight segmet to close the path
76       points(2,:) = point_end;
77       pp = [(points(2,:)-points(1,:))' points(1,:)'];
78       
79       pathdata{end} = pp;
80     end
81     
82     Paths.(svgpathid).data = pathdata;
83   end
84 endfunction
85
86 %!test
87 %! figure(1)
88 %! hold on
89 %! paths = getSVGPaths_py ('../drawing.svg');
90 %!
91 %! % Get path ids
92 %! ids = fieldnames(paths);
93 %! npath = numel(ids);
94 %!
95 %! t = linspace (0, 1, 64);
96 %!
97 %! for i = 1:npath
98 %!    x = []; y = [];
99 %!    data = paths.(ids(i)).data;
100 %!
101 %!    for j = 1:numel(data)
102 %!     x = cat (2, x, polyval (data{j}(1,:),t));
103 %!     y = cat (2, y, polyval (data{j}(2,:),t));
104 %!    end
105 %!
106 %!    plot(x,y,'-');
107 %! end
108 %! axis ij
109 %! if strcmpi(input('You should see drawing.svg [y/n] ','s'),'n')
110 %!  error ("didn't get what was expected.");
111 %! end
112 %! close 
113