summaryrefslogtreecommitdiff
path: root/lib/extensions
diff options
context:
space:
mode:
authorKaalleen <36401965+kaalleen@users.noreply.github.com>2022-11-22 10:21:45 +0100
committerGitHub <noreply@github.com>2022-11-22 10:21:45 +0100
commite761d8b42c178d3e9a940963234d4cd62c777f59 (patch)
tree71c07c3181a0c0504787744b4c4c60966a0c96b9 /lib/extensions
parent4c845df846889e50016bcc0a7d56df5d01102fdf (diff)
Convert to gradient blocks extension (#1844)
Diffstat (limited to 'lib/extensions')
-rw-r--r--lib/extensions/__init__.py2
-rw-r--r--lib/extensions/gradient_blocks.py69
2 files changed, 71 insertions, 0 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py
index f1837c59..5c702ce8 100644
--- a/lib/extensions/__init__.py
+++ b/lib/extensions/__init__.py
@@ -21,6 +21,7 @@ from .embroider_settings import EmbroiderSettings
from .flip import Flip
from .generate_palette import GeneratePalette
from .global_commands import GlobalCommands
+from .gradient_blocks import GradientBlocks
from .input import Input
from .install import Install
from .install_custom_palette import InstallCustomPalette
@@ -79,6 +80,7 @@ __all__ = extensions = [StitchPlanPreview,
RemoveEmbroiderySettings,
Cleanup,
BreakApart,
+ GradientBlocks,
ApplyThreadlist,
InstallCustomPalette,
GeneratePalette,
diff --git a/lib/extensions/gradient_blocks.py b/lib/extensions/gradient_blocks.py
new file mode 100644
index 00000000..5159149f
--- /dev/null
+++ b/lib/extensions/gradient_blocks.py
@@ -0,0 +1,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()