summaryrefslogtreecommitdiff
path: root/lib/extensions/gradient_blocks.py
blob: 5159149fcc96d60d9a231867e146956bc6e3f19e (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
# Authors: see git history
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.

from math import degrees

from inkex import PathElement, errormsg

from ..elements import FillStitch
from ..elements.gradient_fill import gradient_shapes_and_attributes
from ..i18n import _
from ..svg import get_correction_transform
from ..svg.tags import INKSTITCH_ATTRIBS
from .base import InkstitchExtension


class GradientBlocks(InkstitchExtension):
    '''
    This will break apart fill objects with a gradient fill into solid color blocks with end_row_spacing.
    '''

    def effect(self):
        if not self.svg.selection:
            errormsg(_("Please select at least one object with a gradient fill."))
            return

        if not self.get_elements():
            return

        elements = [element for element in self.elements if (isinstance(element, FillStitch) and self.has_gradient_color(element))]
        if not elements:
            errormsg(_("Please select at least one object with a gradient fill."))
            return

        for element in elements:
            parent = element.node.getparent()
            correction_transform = get_correction_transform(element.node)
            style = element.node.style
            index = parent.index(element.node)
            fill_shapes, attributes = gradient_shapes_and_attributes(element, element.shape)
            # reverse order so we can always insert with the same index number
            fill_shapes.reverse()
            attributes.reverse()
            for i, shape in enumerate(fill_shapes):
                style['fill'] = attributes[i]['color']
                end_row_spacing = attributes[i]['end_row_spacing'] or None
                angle = degrees(attributes[i]['angle'])
                d = "M " + " ".join([f'{x}, {y}' for x, y in list(shape.exterior.coords)]) + " Z"
                block = PathElement(attrib={
                    "id": self.uniqueId("path"),
                    "style": str(style),
                    "transform": correction_transform,
                    "d": d,
                    INKSTITCH_ATTRIBS['angle']: f'{angle: .2f}'
                })
                if end_row_spacing:
                    block.set('inkstitch:end_row_spacing_mm', f'{end_row_spacing: .2f}')
                    block.set('inkstitch:underpath', False)
                parent.insert(index, block)
            parent.remove(element.node)

    def has_gradient_color(self, element):
        return element.color.startswith('url') and "linearGradient" in element.color


if __name__ == '__main__':
    e = GradientBlocks()
    e.effect()