summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKaalleen <36401965+kaalleen@users.noreply.github.com>2021-03-22 17:06:48 +0100
committerGitHub <noreply@github.com>2021-03-22 17:06:48 +0100
commit183b2451fd6515f72c709a40b4e5ab5735427823 (patch)
tree175c7d18392632a34cb25165ac1fb03470fb8cf5 /lib
parent8c4288431644f590e127a85d1f23171fb9b2cc95 (diff)
update font tools etc (#1086)
* update font tools * fix tie error * ignore duplicated commands
Diffstat (limited to 'lib')
-rw-r--r--lib/elements/element.py14
-rw-r--r--lib/extensions/lettering_custom_font_dir.py2
-rw-r--r--lib/extensions/lettering_generate_json.py32
-rw-r--r--lib/extensions/lettering_remove_kerning.py12
-rw-r--r--lib/extensions/params.py5
-rw-r--r--lib/lettering/font.py5
-rw-r--r--lib/lettering/kerning.py25
-rw-r--r--lib/stitch_plan/stitch_plan.py4
-rw-r--r--lib/stitch_plan/ties.py2
9 files changed, 59 insertions, 42 deletions
diff --git a/lib/elements/element.py b/lib/elements/element.py
index 2d77a5d4..3468673a 100644
--- a/lib/elements/element.py
+++ b/lib/elements/element.py
@@ -84,11 +84,12 @@ class EmbroideryElement(object):
self.replace_legacy_param(attrib)
legacy_attribs = True
# convert legacy tie setting
- legacy_tie = self.get_boolean_param('ties', None)
- if legacy_tie is False:
- self.set_param('ties', 3)
- elif legacy_tie is True:
+ legacy_tie = self.get_param('ties', None)
+ if legacy_tie == "True":
self.set_param('ties', 0)
+ elif legacy_tie == "False":
+ self.set_param('ties', 3)
+
# defaut setting for fill_underlay has changed
if legacy_attribs and not self.get_param('fill_underlay', ""):
self.set_param('fill_underlay', False)
@@ -319,11 +320,8 @@ class EmbroideryElement(object):
def get_command(self, command):
commands = self.get_commands(command)
- if len(commands) == 1:
+ if commands:
return commands[0]
- elif len(commands) > 1:
- raise ValueError(_("%(id)s has more than one command of type '%(command)s' linked to it") %
- dict(id=self.node.get('id'), command=command))
else:
return None
diff --git a/lib/extensions/lettering_custom_font_dir.py b/lib/extensions/lettering_custom_font_dir.py
index 0103c7d6..a8284d38 100644
--- a/lib/extensions/lettering_custom_font_dir.py
+++ b/lib/extensions/lettering_custom_font_dir.py
@@ -28,6 +28,8 @@ class LetteringCustomFontDir(InkstitchExtension):
config_path = appdirs.user_config_dir('inkstitch')
except ImportError:
config_path = os.path.expanduser('~/.inkstitch')
+ if not os.path.exists(config_path):
+ os.makedirs(config_path)
config_path = os.path.join(config_path, 'custom_dirs.json')
with open(config_path, 'w', encoding="utf8") as font_data:
diff --git a/lib/extensions/lettering_generate_json.py b/lib/extensions/lettering_generate_json.py
index 9b44c367..446c4ce8 100644
--- a/lib/extensions/lettering_generate_json.py
+++ b/lib/extensions/lettering_generate_json.py
@@ -20,9 +20,12 @@ class LetteringGenerateJson(InkstitchExtension):
self.arg_parser.add_argument("-s", "--auto-satin", type=Boolean, default="true", dest="auto_satin")
self.arg_parser.add_argument("-r", "--reversible", type=Boolean, default="true", dest="reversible")
self.arg_parser.add_argument("-g", "--default-glyph", type=str, default="", dest="default_glyph")
- self.arg_parser.add_argument("-i", "--min-scale", type=float, default=1, dest="min_scale")
- self.arg_parser.add_argument("-a", "--max-scale", type=float, default=1, dest="max_scale")
+ self.arg_parser.add_argument("-i", "--min-scale", type=float, default=1.0, dest="min_scale")
+ self.arg_parser.add_argument("-a", "--max-scale", type=float, default=1.0, dest="max_scale")
+ self.arg_parser.add_argument("-c", "--use-custom-leading", type=Boolean, default="false", dest="use_custom_leading")
+ self.arg_parser.add_argument("-b", "--use-custom-spacing", type=Boolean, default="false", dest="use_custom_spacing")
self.arg_parser.add_argument("-l", "--leading", type=int, default=0, dest="leading")
+ self.arg_parser.add_argument("-w", "--word-spacing", type=int, default=26, dest="word_spacing")
self.arg_parser.add_argument("-p", "--font-file", type=str, default="", dest="path")
def effect(self):
@@ -38,19 +41,18 @@ class LetteringGenerateJson(InkstitchExtension):
horiz_adv_x = kerning.horiz_adv_x()
hkern = kerning.hkern()
+ custom_leading = self.options.use_custom_leading
+ custom_spacing = self.options.use_custom_spacing
word_spacing = kerning.word_spacing()
+ # use user input in case that the default word spacing is not defined
+ # in the svg file or the user forces custom values
+ if custom_spacing or not word_spacing:
+ word_spacing = self.options.word_spacing
letter_spacing = kerning.letter_spacing()
- units_per_em = kerning.units_per_em()
- # missing_glyph_spacing = kerning.missing_glyph_spacing()
-
- # if letter spacing returns 0, it hasn't been specified in the font file
- # Ink/Stitch will calculate the width of each letter automatically
- if letter_spacing == 0:
- letter_spacing = None
-
- # if leading (line height) is set to 0, the font author wants Ink/Stitch to use units_per_em
- # if units_per_em is not defined in the font file a default value will be returned
- if self.options.leading == 0:
+ units_per_em = kerning.units_per_em() or self.options.leading
+ # use units_per_em for leading (line height) if defined in the font file,
+ # unless the user wishes to overwrite the value
+ if units_per_em and not custom_leading:
leading = units_per_em
else:
leading = self.options.leading
@@ -62,8 +64,8 @@ class LetteringGenerateJson(InkstitchExtension):
'auto_satin': self.options.auto_satin,
'reversible': self.options.reversible,
'default_glyph': self.options.default_glyph,
- 'min_scale': self.options.min_scale,
- 'max_scale': self.options.max_scale,
+ 'min_scale': round(self.options.min_scale, 1),
+ 'max_scale': round(self.options.max_scale, 1),
'horiz_adv_x_default': letter_spacing,
'horiz_adv_x_space': word_spacing,
'units_per_em': units_per_em,
diff --git a/lib/extensions/lettering_remove_kerning.py b/lib/extensions/lettering_remove_kerning.py
index aec8717e..b58e4fb2 100644
--- a/lib/extensions/lettering_remove_kerning.py
+++ b/lib/extensions/lettering_remove_kerning.py
@@ -23,8 +23,10 @@ class LetteringRemoveKerning(InkstitchExtension):
with open(path, 'r+', encoding="utf-8") as fontfile:
svg = etree.parse(fontfile)
xpath = ".//svg:font[1]"
- kerning = svg.xpath(xpath, namespaces=NSS)[0]
- kerning.getparent().remove(kerning)
- fontfile.seek(0)
- fontfile.write(etree.tostring(svg).decode('utf-8'))
- fontfile.truncate()
+ kerning = svg.xpath(xpath, namespaces=NSS)
+ if kerning:
+ kerning = kerning[0]
+ kerning.getparent().remove(kerning)
+ fontfile.seek(0)
+ fontfile.write(etree.tostring(svg).decode('utf-8'))
+ fontfile.truncate()
diff --git a/lib/extensions/params.py b/lib/extensions/params.py
index b03ea973..2501f157 100644
--- a/lib/extensions/params.py
+++ b/lib/extensions/params.py
@@ -187,7 +187,10 @@ class ParamsTab(ScrolledPanel):
for name, value in preset_data.items():
if name in self.param_inputs:
- self.param_inputs[name].SetValue(value)
+ try:
+ self.param_inputs[name].SetValue(value)
+ except AttributeError:
+ self.param_inputs[name].SetSelection(int(value))
self.changed_inputs.add(self.param_inputs[name])
self.update_toggle_state()
diff --git a/lib/lettering/font.py b/lib/lettering/font.py
index 3ef99d47..03e0e47a 100644
--- a/lib/lettering/font.py
+++ b/lib/lettering/font.py
@@ -9,7 +9,6 @@ from ..elements import nodes_to_elements
from ..exceptions import InkstitchException
from ..i18n import _, get_languages
from ..stitches.auto_satin import auto_satin
-from ..svg import PIXELS_PER_MM
from ..svg.tags import INKSCAPE_LABEL, SVG_GROUP_TAG, SVG_PATH_TAG
from ..utils import Point
from .font_variant import FontVariant
@@ -103,7 +102,7 @@ class Font(object):
name = localized_font_metadata('name', '')
description = localized_font_metadata('description', '')
default_glyph = font_metadata('defalt_glyph', "�")
- leading = font_metadata('leading', 5, multiplier=PIXELS_PER_MM)
+ leading = font_metadata('leading', 100)
kerning_pairs = font_metadata('kerning_pairs', {})
auto_satin = font_metadata('auto_satin', True)
min_scale = font_metadata('min_scale', 1.0)
@@ -119,7 +118,7 @@ class Font(object):
horiz_adv_x_default = font_metadata('horiz_adv_x_default')
# Define by <glyph glyph-name="space" unicode=" " horiz-adv-x="22" />, Example font.json : "horiz_adv_x_space":22,
- word_spacing = font_metadata('horiz_adv_x_space', 0)
+ word_spacing = font_metadata('horiz_adv_x_space', 20)
reversible = font_metadata('reversible', True)
diff --git a/lib/lettering/kerning.py b/lib/lettering/kerning.py
index 920e7d59..710b02aa 100644
--- a/lib/lettering/kerning.py
+++ b/lib/lettering/kerning.py
@@ -7,7 +7,7 @@ class FontKerning(object):
This class reads kerning information from an SVG file
"""
def __init__(self, path):
- with open(path) as svg:
+ with open(path, 'r', encoding="utf-8") as svg:
self.svg = etree.parse(svg)
# horiz_adv_x defines the wdith of specific letters (distance to next letter)
@@ -46,21 +46,30 @@ class FontKerning(object):
# the space character
def word_spacing(self):
xpath = "string(.//svg:glyph[@glyph-name='space'][1]/@*[name()='horiz-adv-x'])"
- word_spacing = self.svg.xpath(xpath, namespaces=NSS) or 26
- return int(word_spacing)
+ word_spacing = self.svg.xpath(xpath, namespaces=NSS)
+ try:
+ return int(word_spacing)
+ except ValueError:
+ return None
# default letter spacing
def letter_spacing(self):
xpath = "string(.//svg:font[@horiz-adv-x][1]/@*[name()='horiz-adv-x'])"
- letter_spacing = self.svg.xpath(xpath, namespaces=NSS) or 0
- return int(letter_spacing)
+ letter_spacing = self.svg.xpath(xpath, namespaces=NSS)
+ try:
+ return int(letter_spacing)
+ except ValueError:
+ return None
# this value will be saved into the json file to preserve it for later font edits
# additionally it serves to automatically define the line height (leading)
- def units_per_em(self, default=100):
+ def units_per_em(self):
xpath = "string(.//svg:font-face[@units-per-em][1]/@*[name()='units-per-em'])"
- units_per_em = self.svg.xpath(xpath, namespaces=NSS) or default
- return int(units_per_em)
+ units_per_em = self.svg.xpath(xpath, namespaces=NSS)
+ try:
+ return int(units_per_em)
+ except ValueError:
+ return None
"""
def missing_glyph_spacing(self):
diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py
index fc725371..f3827f54 100644
--- a/lib/stitch_plan/stitch_plan.py
+++ b/lib/stitch_plan/stitch_plan.py
@@ -14,7 +14,9 @@ def patches_to_stitch_plan(patches, collapse_len=None, disable_ties=False):
* adds jump-stitches between patches if necessary
"""
- collapse_len = (collapse_len or 3.0) * PIXELS_PER_MM
+ if collapse_len is None:
+ collapse_len = 3.0
+ collapse_len = collapse_len * PIXELS_PER_MM
stitch_plan = StitchPlan()
color_block = stitch_plan.new_color_block(color=patches[0].color)
diff --git a/lib/stitch_plan/ties.py b/lib/stitch_plan/ties.py
index 916f9822..fda12f9a 100644
--- a/lib/stitch_plan/ties.py
+++ b/lib/stitch_plan/ties.py
@@ -32,7 +32,7 @@ def add_tie(stitches, tie_path):
def add_tie_off(stitches):
# tie_modus: 0 = both | 1 = before | 2 = after | 3 = neither
- if stitches[0].tie_modus not in [1, 3]:
+ if stitches[-1].tie_modus not in [1, 3]:
add_tie(stitches, stitches[-1:-3:-1])