1#!/usr/local/bin/python3.8
2#
3# Copyright (C) 2016 su_v, <suv-sf@users.sf.net>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18#
19"""
20Convert path to mesh gradient
21"""
22
23import inkex
24from inkex.paths import Line, Curve
25from inkex.elements import MeshGradient
26
27class PathToMesh(inkex.EffectExtension):
28    """Convert path data to mesh geometry."""
29    def add_arguments(self, pars):
30        pars.add_argument("--tab", help="The selected UI-tab")
31
32    def add_mesh(self, meshgradient):
33        """Add meshgradient definition to current document."""
34        self.svg.defs.append(meshgradient)
35        meshgradient.set_random_id('meshgradient')
36        return meshgradient.get_id()
37
38    def effect(self):
39        """Main routine to convert path data to mesh geometry."""
40        # loop through selection
41        for node in self.svg.selection.filter(inkex.PathElement):
42            csp = None
43            meshgradient = None
44            mesh_id = None
45            # parse path data
46            csp = node.path.to_superpath()
47            # convert csp to meshgradient definition
48            if csp is not None:
49                # TODO: check length of path data / csp
50                meshgradient = self.to_mesh(node, csp)
51            # add meshgradient to document
52            if meshgradient is not None:
53                mesh_id = self.add_mesh(meshgradient)
54            # apply meshgradient to node
55            if mesh_id is not None:
56                node.style['fill'] = 'url(#{})'.format(mesh_id)
57
58    def to_mesh(self, node, csp, subpath=0):
59        """Convert csp to meshgradient geometry."""
60        # mesh data
61        corners, edges = self.to_meshdata(csp[subpath])
62        # alternating stop colors
63        colors = [node.style.get('fill'), '#ffffff']
64        # define meshgradient with first corner as initial point
65        meshgradient = MeshGradient.new_mesh(pos=corners[0], rows=1, cols=1)
66        # define stops (stop-color, path) for first meshpatch
67        meshgradient[0][0].stops(edges[0:4], colors)
68        return meshgradient
69
70    @staticmethod
71    def to_meshdata(subpath):
72        """Convert csp subpath to corners, edge path data."""
73        if len(subpath) >= 5:
74            corners = []
75            edges = []
76            for i, corner in enumerate(subpath[:4]):
77                corners.append(corner[1])
78                edge = [list(subpath[i]), list(subpath[i+1])]
79                edge[0][0] = list(edge[0][1])
80                edge[1][2] = list(edge[1][1])
81                if inkex.CubicSuperPath.is_line(edge[0], edge[1]):
82                    edges.append(Line(*edge[1][1]))
83                else:
84                    edges.append(Curve(*(edge[0][2] + edge[1][0] + edge[1][1])))
85
86        return corners, edges
87
88if __name__ == '__main__':
89    PathToMesh().run()
90