summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/extensions/__init__.py6
-rw-r--r--lib/extensions/generate_palette.py85
-rw-r--r--lib/extensions/palette_split_text.py43
3 files changed, 133 insertions, 1 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py
index 83dd19c1..2a2af2ef 100644
--- a/lib/extensions/__init__.py
+++ b/lib/extensions/__init__.py
@@ -15,6 +15,7 @@ from .cutwork_segmentation import CutworkSegmentation
from .duplicate_params import DuplicateParams
from .embroider_settings import EmbroiderSettings
from .flip import Flip
+from .generate_palette import GeneratePalette
from .global_commands import GlobalCommands
from .import_threadlist import ImportThreadlist
from .input import Input
@@ -23,12 +24,13 @@ from .install_custom_palette import InstallCustomPalette
from .layer_commands import LayerCommands
from .lettering import Lettering
from .lettering_custom_font_dir import LetteringCustomFontDir
+from .lettering_force_lock_stitches import LetteringForceLockStitches
from .lettering_generate_json import LetteringGenerateJson
from .lettering_remove_kerning import LetteringRemoveKerning
-from .lettering_force_lock_stitches import LetteringForceLockStitches
from .letters_to_font import LettersToFont
from .object_commands import ObjectCommands
from .output import Output
+from .palette_split_text import PaletteSplitText
from .params import Params
from .print_pdf import Print
from .remove_embroidery_settings import RemoveEmbroiderySettings
@@ -66,6 +68,8 @@ __all__ = extensions = [StitchPlanPreview,
BreakApart,
ImportThreadlist,
InstallCustomPalette,
+ GeneratePalette,
+ PaletteSplitText,
Simulator,
Reorder,
DuplicateParams,
diff --git a/lib/extensions/generate_palette.py b/lib/extensions/generate_palette.py
new file mode 100644
index 00000000..4255cfba
--- /dev/null
+++ b/lib/extensions/generate_palette.py
@@ -0,0 +1,85 @@
+# Authors: see git history
+#
+# Copyright (c) 2022 Authors
+# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
+
+import os
+import sys
+
+import inkex
+
+from ..i18n import _
+from ..utils import guess_inkscape_config_path
+from .base import InkstitchExtension
+
+
+class GeneratePalette(InkstitchExtension):
+ # Generate a custom color palette in object related order
+ def __init__(self, *args, **kwargs):
+ InkstitchExtension.__init__(self, *args, **kwargs)
+ self.arg_parser.add_argument("-n", "--palette_name", type=str, default=None, dest="palette_name")
+ self.arg_parser.add_argument("-f", "--palette_folder", type=str, default=None, dest="palette_folder")
+ self.arg_parser.add_argument("-o", "--options", type=str, default=None, dest="page_options")
+ self.arg_parser.add_argument("-i", "--info", type=str, default=None, dest="page_help")
+
+ def effect(self):
+ path = self.options.palette_folder
+ brand = self.options.palette_name
+ file_name = "InkStitch %s.gpl" % brand
+ color_palette_name = '\nName: Ink/Stitch: %s' % brand
+
+ if not brand:
+ inkex.errormsg(_("Please specify a name for your color palette."))
+ sys.exit()
+
+ if path:
+ if not os.path.isdir(path):
+ inkex.errormsg(_("Unkown directory path."))
+ sys.exit()
+ else:
+ path = os.path.join(guess_inkscape_config_path(), 'palettes')
+ if not os.path.isdir(path):
+ inkex.errormsg(_("Ink/Stitch cannot find your palette folder automatically. Please enter the path manually."))
+ sys.exit()
+
+ elements = self.svg.selected.rendering_order()
+
+ if not elements:
+ inkex.errormsg(_("No element selected.\n\nPlease select at least one text element with a fill color."))
+ sys.exit()
+
+ colors = self._get_color_from_elements(elements)
+
+ if not colors:
+ inkex.errormsg(_("We couldn't find any fill colors on your text elements. Please read the instructions on our website."))
+ sys.exit()
+
+ colors = ['GIMP Palette', color_palette_name, '\nColumns: 4', '\n# RGB Value\t Color Name Number'] + colors
+
+ file_path = os.path.join(path, file_name)
+ with open(file_path, 'w', encoding='utf-8') as gpl:
+ gpl.writelines(colors)
+
+ def _get_color_from_elements(self, elements):
+ colors = []
+ for element in elements:
+ if 'fill' not in element.style.keys() or type(element) != inkex.TextElement:
+ continue
+
+ color = inkex.Color(element.style['fill']).to_rgb()
+ color_name = element.get_text().split(' ')
+ if len(color_name) > 1 and color_name[-1].isdigit():
+ number = color_name[-1]
+ name = ' '.join(color_name[:-1])
+ else:
+ number = 0
+ name = ' '.join(color_name)
+ color = "\n%s\t%s\t%s\t%s %s" % (str(color[0]).rjust(3), str(color[1]).rjust(3), str(color[2]).rjust(3), name.rjust(30), number)
+ colors.append(color)
+
+ return colors
+
+
+if __name__ == '__main__':
+ e = GeneratePalette()
+ e.affect()
diff --git a/lib/extensions/palette_split_text.py b/lib/extensions/palette_split_text.py
new file mode 100644
index 00000000..829425b4
--- /dev/null
+++ b/lib/extensions/palette_split_text.py
@@ -0,0 +1,43 @@
+# Authors: see git history
+#
+# Copyright (c) 2022 Authors
+# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
+
+import inkex
+
+from ..i18n import _
+from .base import InkstitchExtension
+
+
+class PaletteSplitText(InkstitchExtension):
+ # Splits sublines of text into it's own text elements in order to color them with the color picker
+ def __init__(self, *args, **kwargs):
+ InkstitchExtension.__init__(self, *args, **kwargs)
+ self.arg_parser.add_argument("-l", "--line-height", type=int, default=6, dest="line_height")
+
+ def effect(self):
+ if not self.svg.selected:
+ inkex.errormsg(_("Please select one or more text elements to split lines."))
+ return
+
+ line_height = self.options.line_height
+
+ for text in self.svg.selected:
+ if type(text) == inkex.elements.TextElement:
+ parent = text.getparent()
+ content = text.get_text()
+ lines = content.split('\n')
+ lines.reverse()
+ style = text.get('style')
+ x = text.get('x')
+ y = text.get('y')
+ y = float(y) + (len(lines) - 1) * line_height
+ for line in lines:
+ element = inkex.TextElement()
+ element.text = line
+ element.set('style', style)
+ element.set('x', x)
+ element.set('y', str(y))
+ y = float(y) - line_height
+ parent.insert(0, element)
+ parent.remove(text)