diff --git a/Lib/extractor/formats/vfb.py b/Lib/extractor/formats/vfb.py index 8d74140..9c0239c 100644 --- a/Lib/extractor/formats/vfb.py +++ b/Lib/extractor/formats/vfb.py @@ -1,15 +1,20 @@ import os import shutil import tempfile -import subprocess from fontTools.ufoLib import UFOReader -_ufo2vfbLocation = "/usr/local/bin/vfb2ufo" +try: + from vfbLib.vfb.vfb import Vfb + from vfbLib.ufo.builder import VfbToUfoBuilder + + haveVfbLib = True +except ImportError: + haveVfbLib = False def haveVfb2ufo(): - return os.path.exists(_ufo2vfbLocation) + return haveVfbLib # ---------------- @@ -36,15 +41,30 @@ def extractFontFromVFB( doLib=True, customFunctions=[], ): + extract_minimal = True + vfb = Vfb( + pathOrFile, + minimal=extract_minimal, + drop_keys=("Encoding", "Encoding Mac"), + unicode_strings=True, + ) + vfb.decompile() + builder = VfbToUfoBuilder( + vfb, + minimal=extract_minimal, + base64=True, + pshints=False, + add_kerning_groups=False, + ) + masters = builder.get_ufo_masters(silent=True) + ufoLib_source = masters[0] ufoPath = tempfile.mkdtemp(suffix=".ufo") - cmds = [_ufo2vfbLocation, "-64", "-fo", pathOrFile, ufoPath] - cmds = subprocess.list2cmdline(cmds) - popen = subprocess.Popen(cmds, shell=True) - popen.wait() + ufoLib_source.save(ufoPath, overwrite=True) try: - # vfb2ufo writes ufo2, and has no update since 2015...so dont get to crazy here... - # dont validate as vfb2ufo writes invalid ufos - source = UFOReader(ufoPath, validate=False) + # We now use vfbLib instead of vfb2ufo, which wrote ufo2, and had no update + # since 2015, so the extracted UFOs were pretty basic. + # More data could be extracted now with vfbLib if needed. + source = UFOReader(ufoPath, validate=True) if doInfo: source.readInfo(destination.info) if doKerning: diff --git a/README.rst b/README.rst index f9bb9b9..4429389 100644 --- a/README.rst +++ b/README.rst @@ -27,7 +27,7 @@ Supported input formats: (``*.ttx``) - WOFF 1.0/2.0 (``*.woff``, ``*.woff2``) - PostScript Type1 fonts (``*.pfa``, ``*.pfb``, etc.) -- FontLab files (``*.vfb``) +- FontLab files (``*.vfb``, when installed with optional dependency "vfb") Installation ------------ @@ -38,6 +38,12 @@ You can install ``extractor`` with ``pip``: $ pip install ufo-extractor +To install with support for extracting from vfb files: + +.. code:: + + $ pip install ufo-extractor[vfb] + Note that, for historical reasons, the package is listed on the `Python Package Index `__ under the name ``ufo-extractor``, to disambiguate it from another package also called "extractor". diff --git a/setup.py b/setup.py index 28313cc..ed49a8f 100755 --- a/setup.py +++ b/setup.py @@ -37,6 +37,9 @@ "fonttools[ufo,lxml,woff,unicode,type1]>=4.17.0", "fontFeatures", ], + extras_require={ + "vfb": ["vfbLib>=0.7.1"], + }, classifiers=[ "Development Status :: 4 - Beta", "Environment :: Console",