summaryrefslogtreecommitdiffstats
path: root/share/extensions/convert2dashes.py
blob: 4583ed46ae28f7d40c91c36e7528e005dd65df92 (plain)
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
#!/usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2005,2007 Aaron Spike, aaron@ekips.org
# Copyright (C) 2009 Alvin Penner, penner@vaxxine.com
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
"""
This extension converts a path into a dashed line using 'stroke-dasharray'
It is a modification of the file addnodes.py
"""
import inkex
from inkex import bezier, CubicSuperPath, Group, PathElement
from inkex.localization import inkex_gettext as _


class Dashit(inkex.EffectExtension):
    """Extension to convert paths into dash-array line"""

    def __init__(self):
        super(Dashit, self).__init__()
        self.not_converted = []

    def effect(self):
        for node in self.svg.selection:
            self.convert2dash(node)
        if self.not_converted:
            inkex.errormsg(
                _("Total number of objects not converted: {}\n").format(
                    len(self.not_converted)
                )
            )
            # return list of IDs in case the user needs to find a specific object
            inkex.debug(self.not_converted)

    def convert2dash(self, node):
        """Convert each selected node's dash array"""
        if isinstance(node, Group):
            for child in node:
                self.convert2dash(child)
        elif isinstance(node, PathElement):
            self._convert(node)
        else:
            self.not_converted.append(node.get("id"))

    @staticmethod
    def _convert(node):
        dashes = []
        offset = 0
        style = node.specified_style()
        dashes = style("stroke-dasharray")
        offset = style("stroke-dashoffset")
        if not dashes:
            return
        new = []
        for sub in node.path.to_superpath():
            idash = 0
            dash = dashes[0]
            length = float(offset)
            while dash < length:
                length = length - dash
                idash = (idash + 1) % len(dashes)
                dash = dashes[idash]
            new.append([sub[0][:]])
            i = 1
            while i < len(sub):
                dash = dash - length
                length = bezier.cspseglength(new[-1][-1], sub[i])
                while dash < length:
                    new[-1][-1], nxt, sub[i] = bezier.cspbezsplitatlength(
                        new[-1][-1], sub[i], dash / length
                    )
                    if idash % 2:  # create a gap
                        new.append([nxt[:]])
                    else:  # splice the curve
                        new[-1].append(nxt[:])
                    length = length - dash
                    idash = (idash + 1) % len(dashes)
                    dash = dashes[idash]
                if idash % 2:
                    new.append([sub[i]])
                else:
                    new[-1].append(sub[i])
                i += 1
        style.pop("stroke-dasharray")
        node.pop("sodipodi:type")
        node.path = CubicSuperPath(new).to_path(rtol=1e-10)
        node.style = style


if __name__ == "__main__":
    Dashit().run()