diff options
| author | Kaalleen <36401965+kaalleen@users.noreply.github.com> | 2021-03-04 18:40:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-04 18:40:53 +0100 |
| commit | e84a86d4ac0caf29d6074728376ff0a594243fec (patch) | |
| tree | 888c79ed0094ba2916a1d329861a85515959913c /lib/extensions/lettering_generate_json.py | |
| parent | b39575a50191307b3b56eab6455626398eec6397 (diff) | |
Update for Inkscape 1.0 (#880)
* update for inkscape 1.0
* add about extension
* Build improvements for the inkscape1.0 branch (#985)
* zip: export real svg not stitch plan
* #411 and #726
* Tools for Font Creators (#1018)
* ignore very small holes in fills
* remove embroider (#1026)
* auto_fill: ignore shrink_or_grow if result is empty (#589)
* break apart: do not ignore small fills
Co-authored-by: Hagen Fritsch <rumpeltux-github@irgendwo.org>
Co-authored-by: Lex Neva <github.com@lexneva.name>
Diffstat (limited to 'lib/extensions/lettering_generate_json.py')
| -rw-r--r-- | lib/extensions/lettering_generate_json.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/extensions/lettering_generate_json.py b/lib/extensions/lettering_generate_json.py new file mode 100644 index 00000000..9b44c367 --- /dev/null +++ b/lib/extensions/lettering_generate_json.py @@ -0,0 +1,76 @@ +import json +import os +import sys + +from inkex import Boolean + +from ..i18n import _ +from ..lettering.kerning import FontKerning +from .base import InkstitchExtension + + +class LetteringGenerateJson(InkstitchExtension): + ''' + This extension helps font creators to generate the json file for the lettering tool + ''' + def __init__(self, *args, **kwargs): + InkstitchExtension.__init__(self, *args, **kwargs) + self.arg_parser.add_argument("-n", "--font-name", type=str, default="Font", dest="font_name") + self.arg_parser.add_argument("-d", "--font-description", type=str, default="Description", dest="font_description") + 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("-l", "--leading", type=int, default=0, dest="leading") + self.arg_parser.add_argument("-p", "--font-file", type=str, default="", dest="path") + + def effect(self): + # file paths + path = self.options.path + if not os.path.isfile(path): + print(_("Please specify a font file."), file=sys.stderr) + return + output_path = os.path.join(os.path.dirname(path), 'font.json') + + # kerning + kerning = FontKerning(path) + + horiz_adv_x = kerning.horiz_adv_x() + hkern = kerning.hkern() + word_spacing = kerning.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: + leading = units_per_em + else: + leading = self.options.leading + + # collect data + data = {'name': self.options.font_name, + 'description': self.options.font_description, + 'leading': leading, + '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, + 'horiz_adv_x_default': letter_spacing, + 'horiz_adv_x_space': word_spacing, + 'units_per_em': units_per_em, + 'horiz_adv_x': horiz_adv_x, + 'kerning_pairs': hkern + } + + # write data to font.json into the same directory as the font file + with open(output_path, 'w', encoding="utf8") as font_data: + json.dump(data, font_data, indent=4, ensure_ascii=False) |
