diff options
Diffstat (limited to 'lib/lettering/font.py')
| -rw-r--r-- | lib/lettering/font.py | 44 |
1 files changed, 27 insertions, 17 deletions
diff --git a/lib/lettering/font.py b/lib/lettering/font.py index 0974a1cf..3ef99d47 100644 --- a/lib/lettering/font.py +++ b/lib/lettering/font.py @@ -1,10 +1,9 @@ -# -*- coding: UTF-8 -*- - import json import os from copy import deepcopy -import inkex +from inkex import styles +from lxml import etree from ..elements import nodes_to_elements from ..exceptions import InkstitchException @@ -80,14 +79,14 @@ class Font(object): def _load_metadata(self): try: - with open(os.path.join(self.path, "font.json")) as metadata_file: + with open(os.path.join(self.path, "font.json"), encoding="utf-8") as metadata_file: self.metadata = json.load(metadata_file) except IOError: pass def _load_license(self): try: - with open(os.path.join(self.path, "LICENSE")) as license_file: + with open(os.path.join(self.path, "LICENSE"), encoding="utf-8") as license_file: self.license = license_file.read() except IOError: pass @@ -101,13 +100,9 @@ class Font(object): # we'll deal with missing variants when we apply lettering pass - def _check_variants(self): - if self.variants.get(self.default_variant) is None: - raise FontError("font not found or has no default variant") - name = localized_font_metadata('name', '') description = localized_font_metadata('description', '') - default_glyph = font_metadata('default_glyph', u"�") + default_glyph = font_metadata('defalt_glyph', "�") leading = font_metadata('leading', 5, multiplier=PIXELS_PER_MM) kerning_pairs = font_metadata('kerning_pairs', {}) auto_satin = font_metadata('auto_satin', True) @@ -149,10 +144,13 @@ class Font(object): return None def has_variants(self): + # returns available variants font_variants = [] for variant in FontVariant.VARIANT_TYPES: if os.path.isfile(os.path.join(self.path, "%s.svg" % variant)): font_variants.append(variant) + if not font_variants: + raise FontError(_("The font '%s' has no variants.") % self.name) return font_variants def render_text(self, text, destination_group, variant=None, back_and_forth=True, trim=False): @@ -182,12 +180,23 @@ class Font(object): if self.auto_satin and len(destination_group) > 0: self._apply_auto_satin(destination_group, trim) - else: - # set stroke width because it is almost invisible otherwise (why?) - for element in destination_group.iterdescendants(SVG_PATH_TAG): - style = ['stroke-width:1px' if s.startswith('stroke-width') else s for s in element.get('style').split(';')] - style = ';'.join(style) - element.set('style', '%s' % style) + + # make sure font stroke styles have always a similar look + for element in destination_group.iterdescendants(SVG_PATH_TAG): + dash_array = "" + stroke_width = "" + style = styles.Style(element.get('style')) + + if style.get('fill') == 'none': + stroke_width = ";stroke-width:1px" + if style.get('stroke-width'): + style.pop('stroke-width') + + if style.get('stroke-dasharray') and style.get('stroke-dasharray') != 'none': + stroke_width = ";stroke-width:0.5px" + dash_array = ";stroke-dasharray:3, 1" + + element.set('style', '%s%s%s' % (style.to_str(), stroke_width, dash_array)) return destination_group @@ -209,7 +218,8 @@ class Font(object): Returns: An svg:g element containing the rendered text. """ - group = inkex.etree.Element(SVG_GROUP_TAG, { + + group = etree.Element(SVG_GROUP_TAG, { INKSCAPE_LABEL: line }) |
