-
Notifications
You must be signed in to change notification settings - Fork 13
/
image.py
81 lines (63 loc) · 2.21 KB
/
image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from __future__ import annotations
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, ClassVar, Iterator, Optional, Tuple
from attrs import define, field
from fontTools.misc.transform import Identity, Transform
from ufoLib2.serde import serde
from .misc import _convert_transform
# For Python 3.7 compatibility.
if TYPE_CHECKING:
ImageMapping = Mapping[str, Any]
else:
ImageMapping = Mapping
@serde
@define
class Image(ImageMapping):
"""Represents a background image reference.
See http://unifiedfontobject.org/versions/ufo3/images/ and
http://unifiedfontobject.org/versions/ufo3/glyphs/glif/#image.
"""
fileName: Optional[str] = None
"""The filename of the image."""
transformation: Transform = field(default=Identity, converter=_convert_transform)
"""The affine transformation applied to the image."""
color: Optional[str] = None
"""The color applied to the image."""
def clear(self) -> None:
"""Resets the image reference to factory settings."""
self.fileName = None
self.transformation = Identity
self.color = None
def __bool__(self) -> bool:
"""Indicates whether fileName is set."""
return self.fileName is not None
# implementation of collections.abc.Mapping abstract methods.
# the fontTools.ufoLib.validators.imageValidator requires that image is a
# subclass of Mapping...
_transformation_keys_: ClassVar[Tuple[str, str, str, str, str, str]] = (
"xScale",
"xyScale",
"yxScale",
"yScale",
"xOffset",
"yOffset",
)
_valid_keys_: ClassVar[Tuple[str, str, str, str, str, str, str, str]] = (
"fileName",
*_transformation_keys_,
"color",
)
def __getitem__(self, key: str) -> Any:
try:
i = self._transformation_keys_.index(key)
except ValueError:
try:
return getattr(self, key)
except AttributeError:
raise KeyError(key)
else:
return self.transformation[i]
def __len__(self) -> int:
return len(self._valid_keys_)
def __iter__(self) -> Iterator[str]:
return iter(self._valid_keys_)