diff options
| author | Kaalleen <36401965+kaalleen@users.noreply.github.com> | 2023-10-15 07:54:04 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-15 07:54:04 +0200 |
| commit | cea4f6fca893837934e3337d9e80be0495c37d82 (patch) | |
| tree | df2a2d3aa3b877ec19a589c2441998c55a2c0a81 | |
| parent | 1ff677722844e311f64d10079ff96b725b00676e (diff) | |
add outline extension (#2529)
| -rw-r--r-- | lib/extensions/__init__.py | 2 | ||||
| -rw-r--r-- | lib/extensions/outline.py | 50 | ||||
| -rw-r--r-- | templates/outline.xml | 19 |
3 files changed, 71 insertions, 0 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py index 79759c6d..1b923a44 100644 --- a/lib/extensions/__init__.py +++ b/lib/extensions/__init__.py @@ -37,6 +37,7 @@ from .lettering_update_json_glyphlist import LetteringUpdateJsonGlyphlist from .letters_to_font import LettersToFont from .object_commands import ObjectCommands from .object_commands_toggle_visibility import ObjectCommandsToggleVisibility +from .outline import Outline from .output import Output from .palette_split_text import PaletteSplitText from .palette_to_text import PaletteToText @@ -92,6 +93,7 @@ __all__ = extensions = [ApplyThreadlist, LettersToFont, ObjectCommands, ObjectCommandsToggleVisibility, + Outline, Output, PaletteSplitText, PaletteToText, diff --git a/lib/extensions/outline.py b/lib/extensions/outline.py new file mode 100644 index 00000000..d6a16e99 --- /dev/null +++ b/lib/extensions/outline.py @@ -0,0 +1,50 @@ +# Authors: see git history +# +# Copyright (c) 2010 Authors +# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. + +import inkex +from shapely import concave_hull +from shapely.geometry import LineString, MultiPolygon + +from ..i18n import _ +from ..svg.tags import SVG_PATH_TAG +from .base import InkstitchExtension + + +class Outline(InkstitchExtension): + def __init__(self, *args, **kwargs): + InkstitchExtension.__init__(self, *args, **kwargs) + self.arg_parser.add_argument("-r", "--ratio", type=float, default=0.0, dest="ratio") + self.arg_parser.add_argument("-a", "--allow-holes", type=inkex.Boolean, default=False, dest="allow_holes") + + def effect(self): + if not self.svg.selection: + inkex.errormsg(_("Please select one or more shapes to convert to their outline.")) + return + + for element in self.svg.selection: + self.element_to_outline(element) + + def element_to_outline(self, element): + if element.tag_name == 'g': + for element in element.iterdescendants(SVG_PATH_TAG): + self.element_to_outline(element) + return + + path = element.get_path() + path = path.end_points + hull = concave_hull(LineString(path), ratio=self.options.ratio, allow_holes=self.options.allow_holes) + if isinstance(hull, LineString): + return + + if not isinstance(hull, MultiPolygon): + hull = MultiPolygon([hull]) + d = '' + for geom in hull.geoms: + d += 'M ' + for x, y in geom.exterior.coords: + d += f'{x}, {y} ' + d += "Z" + + element.set('d', d) diff --git a/templates/outline.xml b/templates/outline.xml new file mode 100644 index 00000000..90b09e9d --- /dev/null +++ b/templates/outline.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> +<inkscape-extension translationdomain="inkstitch" xmlns="http://www.inkscape.org/namespace/inkscape/extension"> + <name>Outline</name> + <id>org.inkstitch.outline</id> + <param name="extension" type="string" gui-hidden="true">outline</param> + <effect> + <object-type>all</object-type> + <effects-menu> + <submenu name="Ink/Stitch" translatable="no"> + <submenu name="Tools: Stroke" /> + </submenu> + </effects-menu> + </effect> + <param name="ratio" type="float" appearance="full" gui-text="Ratio" precision="2" min="0" max="1">0</param> + <param name="allow-holes" type="boolean" gui-text="Allow Holes">false</param> + <script> + {{ command_tag | safe }} + </script> +</inkscape-extension> |
