Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy/move fontmake.instantiator module to ufo2ft.instantiator #825

Merged
merged 9 commits into from
Mar 15, 2024
Merged
  •  
  •  
  •  
909 changes: 909 additions & 0 deletions Lib/ufo2ft/instantiator.py

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions Lib/ufo2ft/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import importlib
import logging
import re
import sys
from copy import deepcopy
from functools import partial
from inspect import currentframe, getfullargspec
from typing import Any, Mapping, NamedTuple, Set

Expand Down Expand Up @@ -764,3 +766,70 @@ def load(cls, font):
frozenset(marks),
frozenset(components),
)


def importUfoModule():
try:
import ufoLib2
except ModuleNotFoundError:
try:
import defcon
except ModuleNotFoundError:
raise
else:
return defcon
else:
return ufoLib2


def openFontFactory(*, ufo_module=None):
if ufo_module is None:
ufo_module = importUfoModule()

if hasattr(ufo_module.Font, "open"):

def ctor(path=None):
if path is None:
return ufo_module.Font()
else:
return ufo_module.Font.open(path)

return ctor
return ufo_module.Font


def openFont(*args, ufo_module=None, **kwargs):
return openFontFactory(ufo_module=ufo_module)(*args, **kwargs)


# zip(strict=True) was added with Python 3.10, we provide a backport below
# https://docs.python.org/3/library/functions.html#zip
if sys.version_info[:2] < (3, 10):

def zip_strict(*iterables):
# https://peps.python.org/pep-0618/#reference-implementation
if not iterables:
return
iterators = tuple(iter(iterable) for iterable in iterables)
try:
while True:
items = []
for iterator in iterators:
items.append(next(iterator))
yield tuple(items)
except StopIteration:
pass
if items:
i = len(items)
plural = " " if i == 1 else "s 1-"
msg = f"zip() argument {i + 1} is shorter than argument{plural}{i}"
raise ValueError(msg)
sentinel = object()
for i, iterator in enumerate(iterators[1:], 1):
if next(iterator, sentinel) is not sentinel:
plural = " " if i == 1 else "s 1-"
msg = f"zip() argument {i + 1} is longer than argument{plural}{i}"
raise ValueError(msg)

else:
zip_strict = partial(zip, strict=True)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ compreffor==0.5.5
booleanOperations==0.9.0
cffsubr==0.3.0
skia-pathops==0.8.0.post1
fontMath==0.9.3

# alternative UFO implementation
ufoLib2==0.16.0
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"fonttools[ufo]>=4.47.2",
"cffsubr>=0.3.0",
"booleanOperations>=0.9.0",
"fontMath>=0.9.3",
],
extras_require={
"pathops": ["skia-pathops>=0.8.0"],
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path

import py
import pytest
Expand Down Expand Up @@ -34,6 +35,11 @@ def datadir():
return py.path.local(py.path.local(__file__).dirname).join("data")


@pytest.fixture(scope="session")
def data_dir():
return Path(__file__).parent / "data"


def getpath(filename):
dirname = os.path.dirname(__file__)
return os.path.join(dirname, "data", filename)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version='1.0' encoding='UTF-8'?>
<designspace format="4.0">
<axes>
<axis tag="wght" name="weight" minimum="300" maximum="700" default="300">
<map input="300" output="50"/>
<map input="400" output="100"/>
<map input="700" output="150"/>
</axis>
</axes>
<sources>
<source filename="MyFont-Light.ufo" name="MyFont Light" familyname="MyFont" stylename="Light">
<info copy="1"/>
<location>
<dimension name="weight" xvalue="50"/>
</location>
</source>
<source filename="MyFont-Bold.ufo" name="MyFont Bold" familyname="MyFont" stylename="Bold">
<location>
<dimension name="weight" xvalue="150"/>
</location>
</source>
</sources>
<instances>
<instance familyname="MyFont" stylename="Light ASDF" filename="MyFont-Light.ufo">
<location>
<dimension name="weight" xvalue="50"/>
</location>
</instance>
<instance familyname="MyFont" stylename="Bold ASDF" filename="MyFont-Bold.ufo">
<location>
<dimension name="weight" xvalue="150"/>
</location>
</instance>
</instances>
</designspace>
32 changes: 32 additions & 0 deletions tests/data/DesignspaceBrokenTest/DesignspaceTest.designspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version='1.0' encoding='UTF-8'?>
<designspace format="4.0">
<axes>
<axis tag="wght" name="weight" minimum="300" maximum="700" default="300">
<map input="300" output="50"/>
<map input="400" output="100"/>
<map input="700" output="150"/>
</axis>
</axes>
<sources>
<source filename="MyFont-Light.ufo" name="MyFont Light" familyname="MyFont" stylename="Light">
<info copy="1"/>
<location>
<dimension name="weight" xvalue="50"/>
</location>
</source>
<source filename="MyFont-Bold.ufo" name="MyFont Bold" familyname="MyFont" stylename="Bold">
<location>
<dimension name="weight" xvalue="150"/>
</location>
</source>
</sources>
<instances>
<instance familyname="MyFont" stylename="Regular" filename="MyFont-Regular.ufo">
<location>
<dimension name="weight" xvalue="100"/>
</location>
<kerning/>
<info/>
</instance>
</instances>
</designspace>
34 changes: 34 additions & 0 deletions tests/data/DesignspaceBrokenTest/MyFont-Bold.ufo/fontinfo.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ascender</key>
<integer>0</integer>
<key>capHeight</key>
<integer>0</integer>
<key>descender</key>
<integer>0</integer>
<key>familyName</key>
<string>MyFont</string>
<key>guidelines</key>
<array/>
<key>postscriptBlueValues</key>
<array/>
<key>postscriptFamilyBlues</key>
<array/>
<key>postscriptFamilyOtherBlues</key>
<array/>
<key>postscriptOtherBlues</key>
<array/>
<key>postscriptStemSnapH</key>
<array/>
<key>postscriptStemSnapV</key>
<array/>
<key>styleName</key>
<string>Bold</string>
<key>unitsPerEm</key>
<integer>1000</integer>
<key>xHeight</key>
<integer>0</integer>
</dict>
</plist>
15 changes: 15 additions & 0 deletions tests/data/DesignspaceBrokenTest/MyFont-Bold.ufo/glyphs/asas.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="asas" format="2">
<advance width="600"/>
<outline>
<contour>
<point x="240" y="447" type="line"/>
<point x="240" y="381"/>
<point x="77" y="257"/>
<point x="240" y="257" type="curve" smooth="yes"/>
<point x="403" y="257"/>
<point x="396" y="275"/>
<point x="396" y="380" type="curve"/>
</contour>
</outline>
</glyph>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>asas</key>
<string>asas.glif</string>
<key>l</key>
<string>l.glif</string>
</dict>
</plist>
13 changes: 13 additions & 0 deletions tests/data/DesignspaceBrokenTest/MyFont-Bold.ufo/glyphs/l.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="l" format="2">
<advance width="280"/>
<unicode hex="006C"/>
<outline>
<contour>
<point x="250" y="0" type="line"/>
<point x="250" y="750" type="line"/>
<point x="30" y="750" type="line"/>
<point x="30" y="0" type="line"/>
</contour>
</outline>
</glyph>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<array>
<string>public.default</string>
<string>glyphs</string>
</array>
</array>
</plist>
11 changes: 11 additions & 0 deletions tests/data/DesignspaceBrokenTest/MyFont-Bold.ufo/lib.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>public.glyphOrder</key>
<array>
<string>l</string>
<string>asas</string>
</array>
</dict>
</plist>
10 changes: 10 additions & 0 deletions tests/data/DesignspaceBrokenTest/MyFont-Bold.ufo/metainfo.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>creator</key>
<string>com.github.fonttools.ufoLib</string>
<key>formatVersion</key>
<integer>3</integer>
</dict>
</plist>
34 changes: 34 additions & 0 deletions tests/data/DesignspaceBrokenTest/MyFont-Light.ufo/fontinfo.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ascender</key>
<integer>0</integer>
<key>capHeight</key>
<integer>0</integer>
<key>descender</key>
<integer>0</integer>
<key>familyName</key>
<string>MyFont</string>
<key>guidelines</key>
<array/>
<key>postscriptBlueValues</key>
<array/>
<key>postscriptFamilyBlues</key>
<array/>
<key>postscriptFamilyOtherBlues</key>
<array/>
<key>postscriptOtherBlues</key>
<array/>
<key>postscriptStemSnapH</key>
<array/>
<key>postscriptStemSnapV</key>
<array/>
<key>styleName</key>
<string>Light</string>
<key>unitsPerEm</key>
<integer>1000</integer>
<key>xHeight</key>
<integer>0</integer>
</dict>
</plist>
16 changes: 16 additions & 0 deletions tests/data/DesignspaceBrokenTest/MyFont-Light.ufo/glyphs/asas.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="asas" format="2">
<advance width="600"/>
<outline>
<contour>
<point x="227" y="568" type="line"/>
<point x="182" y="248" type="line"/>
<point x="508" y="157" type="line"/>
<point x="516" y="563" type="line"/>
<point x="459" y="540"/>
<point x="361" y="403"/>
<point x="353" y="496" type="curve"/>
<point x="276" y="562" type="line"/>
</contour>
</outline>
</glyph>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>asas</key>
<string>asas.glif</string>
<key>l</key>
<string>l.glif</string>
</dict>
</plist>
13 changes: 13 additions & 0 deletions tests/data/DesignspaceBrokenTest/MyFont-Light.ufo/glyphs/l.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="l" format="2">
<advance width="160"/>
<unicode hex="006C"/>
<outline>
<contour>
<point x="90" y="0" type="line"/>
<point x="90" y="750" type="line"/>
<point x="70" y="750" type="line"/>
<point x="70" y="0" type="line"/>
</contour>
</outline>
</glyph>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<array>
<string>public.default</string>
<string>glyphs</string>
</array>
</array>
</plist>
11 changes: 11 additions & 0 deletions tests/data/DesignspaceBrokenTest/MyFont-Light.ufo/lib.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>public.glyphOrder</key>
<array>
<string>l</string>
<string>asas</string>
</array>
</dict>
</plist>
Loading
Loading