summaryrefslogtreecommitdiff
path: root/lib/extensions
diff options
context:
space:
mode:
authorKaalleen <36401965+kaalleen@users.noreply.github.com>2021-10-09 18:25:29 +0200
committerGitHub <noreply@github.com>2021-10-09 18:25:29 +0200
commit5a1ad7e4e8bebb31a679674ed8b4ca526138695c (patch)
tree254f906a6524abd3cb9a685975e563f09790bbaa /lib/extensions
parent0224794a0815f22a719d2880e175077b77214513 (diff)
Letters to font extension (#1312)
Diffstat (limited to 'lib/extensions')
-rw-r--r--lib/extensions/__init__.py2
-rw-r--r--lib/extensions/input.py63
-rw-r--r--lib/extensions/lettering.py2
-rw-r--r--lib/extensions/letters_to_font.py81
4 files changed, 87 insertions, 61 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py
index 83a522f2..ec21b592 100644
--- a/lib/extensions/__init__.py
+++ b/lib/extensions/__init__.py
@@ -24,6 +24,7 @@ from .lettering import Lettering
from .lettering_custom_font_dir import LetteringCustomFontDir
from .lettering_generate_json import LetteringGenerateJson
from .lettering_remove_kerning import LetteringRemoveKerning
+from .letters_to_font import LettersToFont
from .object_commands import ObjectCommands
from .output import Output
from .params import Params
@@ -55,6 +56,7 @@ __all__ = extensions = [StitchPlanPreview,
LetteringGenerateJson,
LetteringRemoveKerning,
LetteringCustomFontDir,
+ LettersToFont,
Troubleshoot,
RemoveEmbroiderySettings,
Cleanup,
diff --git a/lib/extensions/input.py b/lib/extensions/input.py
index a8b8bee3..066b9003 100644
--- a/lib/extensions/input.py
+++ b/lib/extensions/input.py
@@ -3,70 +3,13 @@
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
-import os
-import sys
-
-import inkex
from lxml import etree
-import pyembroidery
-
-from ..i18n import _
-from ..stitch_plan import StitchPlan
-from ..svg import PIXELS_PER_MM, render_stitch_plan
-from ..svg.tags import INKSCAPE_LABEL
+from ..stitch_plan import generate_stitch_plan
class Input(object):
def run(self, args):
embroidery_file = args[0]
- self.validate_file_path(embroidery_file)
-
- pattern = pyembroidery.read(embroidery_file)
- stitch_plan = StitchPlan()
- color_block = None
-
- for raw_stitches, thread in pattern.get_as_colorblocks():
- color_block = stitch_plan.new_color_block(thread)
- for x, y, command in raw_stitches:
- if command == pyembroidery.STITCH:
- color_block.add_stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0)
- if len(color_block) > 0:
- if command == pyembroidery.TRIM:
- color_block.add_stitch(trim=True)
- elif command == pyembroidery.STOP:
- color_block.add_stitch(stop=True)
- color_block = stitch_plan.new_color_block(thread)
-
- stitch_plan.delete_empty_color_blocks()
-
- if stitch_plan.last_color_block:
- if stitch_plan.last_color_block.last_stitch:
- if stitch_plan.last_color_block.last_stitch.stop:
- # ending with a STOP command is redundant, so remove it
- del stitch_plan.last_color_block[-1]
-
- extents = stitch_plan.extents
- svg = inkex.SvgDocumentElement("svg", nsmap=inkex.NSS, attrib={
- "width": str(extents[0] * 2),
- "height": str(extents[1] * 2),
- "viewBox": "0 0 %s %s" % (extents[0] * 2, extents[1] * 2),
- })
- render_stitch_plan(svg, stitch_plan)
-
- # rename the Stitch Plan layer so that it doesn't get overwritten by Embroider
- layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']")
- layer.set(INKSCAPE_LABEL, os.path.basename(embroidery_file))
- layer.attrib.pop('id')
-
- # Shift the design so that its origin is at the center of the canvas
- # Note: this is NOT the same as centering the design in the canvas!
- layer.set('transform', 'translate(%s,%s)' % (extents[0], extents[1]))
-
- print(etree.tostring(svg).decode('utf-8'))
-
- def validate_file_path(self, path):
- # Check if the file exists
- if not os.path.isfile(path):
- inkex.errormsg(_('File does not exist and cannot be opened. Please correct the file path and try again.\r%s') % path)
- sys.exit(1)
+ stitch_plan = generate_stitch_plan(embroidery_file)
+ print(etree.tostring(stitch_plan).decode('utf-8'))
diff --git a/lib/extensions/lettering.py b/lib/extensions/lettering.py
index 312a47ce..35509eb6 100644
--- a/lib/extensions/lettering.py
+++ b/lib/extensions/lettering.py
@@ -174,7 +174,7 @@ class LetteringFrame(wx.Frame):
image.Rescale(300, 20, quality=wx.IMAGE_QUALITY_HIGH)
self.font_chooser.Append(font.marked_custom_font_name, wx.Bitmap(image))
else:
- self.font_chooser.Append(font.name)
+ self.font_chooser.Append(font.marked_custom_font_name)
def get_font_descriptions(self):
return {font.name: font.description for font in self.fonts.values()}
diff --git a/lib/extensions/letters_to_font.py b/lib/extensions/letters_to_font.py
new file mode 100644
index 00000000..158d0d9f
--- /dev/null
+++ b/lib/extensions/letters_to_font.py
@@ -0,0 +1,81 @@
+# Authors: see git history
+#
+# Copyright (c) 2021 Authors
+# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
+
+import os
+from pathlib import Path
+
+import inkex
+from inkex import errormsg
+
+from ..commands import ensure_symbol
+from ..i18n import _
+from ..stitch_plan import generate_stitch_plan
+from ..svg import get_correction_transform
+from ..svg.tags import INKSCAPE_GROUPMODE, INKSCAPE_LABEL, SVG_PATH_TAG
+from .base import InkstitchExtension
+
+
+class LettersToFont(InkstitchExtension):
+ '''
+ This extension will create a json file to store a custom directory path for additional user fonts
+ '''
+ def __init__(self, *args, **kwargs):
+ InkstitchExtension.__init__(self, *args, **kwargs)
+ self.arg_parser.add_argument("-d", "--font-dir", type=str, default="", dest="font_dir")
+ self.arg_parser.add_argument("-f", "--file-format", type=str, default="", dest="file_format")
+ self.arg_parser.add_argument("-c", "--import-commands", type=inkex.Boolean, default=False, dest="import_commands")
+
+ def effect(self):
+ font_dir = self.options.font_dir
+ file_format = self.options.file_format
+
+ if not os.path.isdir(font_dir):
+ errormsg(_("Font directory not found. Please specify an existing directory."))
+
+ glyphs = list(Path(font_dir).rglob(file_format))
+ if not glyphs:
+ glyphs = list(Path(font_dir).rglob(file_format.lower()))
+
+ document = self.document.getroot()
+ for glyph in glyphs:
+ letter = self.get_glyph_element(glyph)
+ label = "GlyphLayer-%s" % letter.get(INKSCAPE_LABEL, ' ').split('.')[0][-1]
+ group = inkex.Group(attrib={
+ INKSCAPE_LABEL: label,
+ INKSCAPE_GROUPMODE: "layer",
+ "transform": get_correction_transform(document, child=True)
+ })
+
+ # remove color block groups if we import without commands
+ # there will only be one object per color block anyway
+ if not self.options.import_commands:
+ for element in letter.iter(SVG_PATH_TAG):
+ group.insert(0, element)
+ else:
+ group.insert(0, letter)
+
+ document.insert(0, group)
+ group.set('style', 'display:none')
+
+ # users may be confused if they get an empty document
+ # make last letter visible again
+ group.set('style', None)
+
+ # In most cases trims are inserted with the imported letters.
+ # Let's make sure the trim symbol exists in the defs section
+ ensure_symbol(document, 'trim')
+
+ self.insert_baseline(document)
+
+ def get_glyph_element(self, glyph):
+ stitch_plan = generate_stitch_plan(str(glyph), self.options.import_commands)
+ # we received a stitch plan wrapped in an svg document, we only need the stitch_plan group
+ # this group carries the name of the file, so we can search for it.
+ stitch_plan = stitch_plan.xpath('.//*[@inkscape:label="%s"]' % os.path.basename(glyph), namespaces=inkex.NSS)[0]
+ stitch_plan.attrib.pop(INKSCAPE_GROUPMODE)
+ return stitch_plan
+
+ def insert_baseline(self, document):
+ document.namedview.new_guide(position=0.0, name="baseline")