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

add property setter for Font.kerning to coerce dict to Kerning class #189

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/ufoLib2/objects/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def _convert_Kerning(value: Mapping[KerningPair, float]) -> Kerning:
return value if isinstance(value, Kerning) else Kerning(value)


def _get_kerning(self: Font) -> Kerning:
return self._kerning


def _set_kerning(self: Font, value: Mapping[KerningPair, float]) -> None:
self._kerning = _convert_Kerning(value)


@define(kw_only=True)
class Font:
"""A data class representing a single Unified Font Object (UFO).
Expand Down Expand Up @@ -141,7 +149,7 @@ class Font:
groups: Dict[str, List[str]] = field(factory=dict)
"""Dict[str, List[str]]: A mapping of group names to a list of glyph names."""

kerning: Kerning = field(factory=Kerning, converter=_convert_Kerning)
_kerning: Kerning = field(factory=Kerning, converter=_convert_Kerning)
"""Dict[Tuple[str, str], float]: A mapping of a tuple of first and second kerning
pair to a kerning value."""

Expand Down Expand Up @@ -359,6 +367,8 @@ def guidelines(self, value: Iterable[Guideline | Mapping[str, Any]]) -> None:
for guideline in value:
self.appendGuideline(guideline)

kerning = property(_get_kerning, _set_kerning)

lib = property(_get_lib, _set_lib)

def objectLib(self, object: HasIdentifier) -> dict[str, Any]:
Expand Down
21 changes: 20 additions & 1 deletion tests/test_ufoLib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

from copy import deepcopy
from pathlib import Path
from typing import Any, Type

import pytest
from fontTools import ufoLib

import ufoLib2
import ufoLib2.objects
from ufoLib2.objects import Features, Font, Layer, LayerSet
from ufoLib2.objects import Features, Font, Glyph, Kerning, Layer, LayerSet, Lib
from ufoLib2.objects.layerSet import _LAYER_NOT_LOADED
from ufoLib2.objects.misc import _DATA_NOT_LOADED

Expand Down Expand Up @@ -265,3 +266,21 @@ def test_woff_metadata(datadir: Path, tmp_path: Path) -> None:

def test_features_normalize_newlines() -> None:
assert Features("a\r\nb\rc\n").normalize_newlines().text == "a\nb\nc\n"


@pytest.mark.parametrize(
"klass, attr_name, attr_type, obj",
[
(Font, "lib", Lib, {"foo": 1}),
(Font, "kerning", Kerning, {("a", "b"): -10}),
(Glyph, "lib", Lib, {"bar": [2, 3]}),
],
)
def test_convert_on_setattr(
klass: Type[Any], attr_name: str, attr_type: Type[Any], obj: Any
) -> None:
o = klass()
assert isinstance(getattr(o, attr_name), attr_type)
assert not isinstance(obj, attr_type)
setattr(o, attr_name, obj)
assert isinstance(getattr(o, attr_name), attr_type)