2024-08-30 08:46:21 -07:00
|
|
|
from fontTools.ttLib import TTFont
|
2024-08-04 15:55:27 -06:00
|
|
|
from fontTools.varLib.models import VariationModel
|
2024-08-30 08:46:21 -07:00
|
|
|
from fontTools.varLib.avar import _pruneLocations, mappings_from_avar
|
|
|
|
import os
|
2024-08-02 19:32:45 -06:00
|
|
|
import unittest
|
|
|
|
import pytest
|
|
|
|
|
2024-08-04 15:55:27 -06:00
|
|
|
TESTS = [
|
|
|
|
(
|
|
|
|
[
|
|
|
|
{"wght": 1},
|
|
|
|
{"wght": 0.5},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{"wght": 0.5},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{"wght": 0.5},
|
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[
|
|
|
|
{"wght": 1, "wdth": 1},
|
|
|
|
{"wght": 0.5, "wdth": 1},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{"wght": 1, "wdth": 1},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{"wght": 1, "wdth": 1},
|
|
|
|
{"wght": 0.5, "wdth": 1},
|
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[
|
|
|
|
{"wght": 1},
|
|
|
|
{"wdth": 1},
|
|
|
|
{"wght": 0.5, "wdth": 0.5},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{"wght": 0.5, "wdth": 0.5},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{"wght": 0.5, "wdth": 0.5},
|
|
|
|
],
|
|
|
|
),
|
|
|
|
]
|
2024-08-02 19:32:45 -06:00
|
|
|
|
2024-08-04 15:55:27 -06:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("locations, poles, expected", TESTS)
|
2024-08-02 19:32:45 -06:00
|
|
|
def test_pruneLocations(locations, poles, expected):
|
|
|
|
axisTags = set()
|
|
|
|
for location in locations:
|
|
|
|
axisTags.update(location.keys())
|
|
|
|
axisTags = sorted(axisTags)
|
|
|
|
|
|
|
|
locations = [{}] + locations
|
|
|
|
|
2024-08-04 15:55:27 -06:00
|
|
|
pruned = _pruneLocations(locations, poles, axisTags)
|
|
|
|
|
|
|
|
assert pruned == expected, (pruned, expected)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("locations, poles, expected", TESTS)
|
|
|
|
def test_roundtrip(locations, poles, expected):
|
|
|
|
axisTags = set()
|
|
|
|
for location in locations:
|
|
|
|
axisTags.update(location.keys())
|
|
|
|
axisTags = sorted(axisTags)
|
|
|
|
|
|
|
|
locations = [{}] + locations
|
|
|
|
expected = [{}] + expected
|
|
|
|
|
|
|
|
model1 = VariationModel(locations, axisTags)
|
|
|
|
model2 = VariationModel(expected, axisTags)
|
|
|
|
|
|
|
|
for location in poles:
|
|
|
|
i = model1.locations.index(location)
|
|
|
|
support1 = model1.supports[i]
|
|
|
|
|
|
|
|
i = model2.locations.index(location)
|
|
|
|
support2 = model2.supports[i]
|
2024-08-02 19:32:45 -06:00
|
|
|
|
2024-08-04 15:55:27 -06:00
|
|
|
assert support1 == support2, (support1, support2)
|
2024-08-30 08:46:21 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_mappings_from_avar():
|
|
|
|
CWD = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
DATADIR = os.path.join(CWD, "..", "ttLib", "tables", "data")
|
|
|
|
varfont_path = os.path.join(DATADIR, "Amstelvar-avar2.subset.ttf")
|
|
|
|
font = TTFont(varfont_path)
|
|
|
|
mappings = mappings_from_avar(font)
|
|
|
|
|
|
|
|
assert len(mappings) == 2, mappings
|