Skip to content

Commit

Permalink
Revise annotations.py with new generic container types. (#755)
Browse files Browse the repository at this point in the history
* Update normalizers.rst

* Update normalizers.rst

* Update point.rst

add .. autoclass:: BasePoint to Reference section.

* Update point.rst

Correct last commit to autoclass:: BasePoint

* Update conf.py

Add intersphinx mapping to python 3 inventory.

* Add new module for type annotation definitions.

* Add type annotations and edit/add documentation.

* Update font.py

Correct import error.

* fixed name discrepancy in defaultLayer properties.

* Fix name discrepancy in defaultLayer properties.

* Add overview and reference items.

* Edit minor details.

* Change  to .

* Revert "Change  to ."

This reverts commit e7152b0.

i#

* Changed type.py to types.py and updtated font.py accordingly.

* Recommitting due to error.

* - Update _getReverseComponentMapping and _getReverseComponentMapping methods in base/layer.py and fontshell/layer.py to return mappings to tuple.
- Update base/layer.py with doc revisions and type annotations.
- Update layer.rst accordingly.
- Fix link to fontParts.world in various .rst files.

* Revise documentation.

* Add annotations.py.

* Remove types.py.

* Revise documentation.

* Fixed type annotation.

* Add documentation tools folder and module for docstring generation.

* vert "Add documentation tools folder and module for docstring generation."

This reverts commit fa52b70.

* Add docstring generation module.

* Refacor extraction of exceptions and normalizers with new CodeAnalyzer class.

* Add type annotation.

* Update fonttools from 4.53.1 to 4.54.1 (#746)

* Revise and add annotation and documentation.

- add Interpolatable protocol and compatible type variable to `annotations.py`
- add and improve annotations and documentation in `base.py`

* correct naming error.

* Add/improve documentation.

* Add documentation regarding review to `rererence` function.

* Add/improve documentation.

* Update base.py (#750)

* update docstrings of `base.reference` and `BaseBPoint.type`.

* Revise `annotations.py` with new generic container types.

* Revert "Update documentation (#739)"

This reverts commit 3d67a1d.

* Resolve merge conflicts.

* Add Sublime Text project folders.

* Add Sublime Text project folders.

* Remove unnecessary list comprehensions.

* Remove Sublime Text project files.

* Solve merge conflict.

* Resolve merge conflicts.

* Add Sublime Text project folders.

* Resolve merge conflicts.

* Resolve merge conflicts.

* Resolve merge conflicts.

* Add type aliases and improve names.

* Revise type annotation in accordance with changes in `annotations.py`

* Correct naming errors.

* Tweak error message language

* Fix type

* Remove list comprehensions

---------

Co-authored-by: pyup.io bot <[email protected]>
Co-authored-by: Ben Kiel <[email protected]>
Co-authored-by: Ben Kiel <[email protected]>
  • Loading branch information
4 people authored Nov 2, 2024
1 parent c9a8b7b commit 38f834d
Show file tree
Hide file tree
Showing 7 changed files with 748 additions and 538 deletions.
56 changes: 30 additions & 26 deletions Lib/fontParts/base/annotations.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
# pylint: disable=C0103, C0114

from __future__ import annotations
from typing import Dict, List, Protocol, Tuple, TypeVar, Union

from fontTools.pens.basePen import AbstractPen
from fontTools.pens.pointPen import AbstractPointPen

# ------------
# Type Aliases
# ------------

# Builtins

# Generic
T = TypeVar('T')

PairType = Tuple[T, T]
QuadrupleType = Tuple[T, T, T, T]
QuintupleType = Tuple[T, T, T, T, T]
SextupleType = Tuple[T, T, T, T, T, T]
CollectionType = Union[List[T], Tuple[T, ...]]
IntFloatType = Union[int, float]
PairCollectionType = Union[List[T], PairType[T]]
QuadrupleCollectionType = Union[List[T], QuadrupleType[T]]
SextupleCollectionType = Union[List[T], SextupleType[T]]

# FontTools
# Builtins
IntFloatType = Union[int, float]

# Pens
PenType = AbstractPen
PointPenType = AbstractPointPen

# FontParts

BoundsType = Tuple[IntFloatType, IntFloatType, IntFloatType, IntFloatType]
# Mapping
CharacterMappingType = Dict[int, Tuple[str, ...]]
ColorType = Tuple[IntFloatType, IntFloatType, IntFloatType, IntFloatType]
CoordinateType = Tuple[IntFloatType, IntFloatType]
FactorType = Union[IntFloatType, Tuple[IntFloatType, IntFloatType]]
InterpolatableType = TypeVar('InterpolatableType', bound='Interpolatable')
KerningKeyType = Tuple[str, str]
KerningDictType = Dict[KerningKeyType, IntFloatType]
ReverseComponentMappingType = Dict[str, Tuple[str, ...]]
ScaleType = Tuple[IntFloatType, IntFloatType]
TransformationMatrixType = Tuple[
IntFloatType, IntFloatType, IntFloatType,
IntFloatType, IntFloatType, IntFloatType
]

# Kerning
KerningDictType = Dict[PairType[str], PairType[str]]

# Transformation
TransformationType = Union[IntFloatType, List[IntFloatType], PairType[IntFloatType]]

# Interpolation
InterpolatableType = TypeVar('InterpolatableType', bound='Interpolatable')


class Interpolatable(Protocol):
"""Represent a protocol for interpolatable types."""

def __add__(self, other: InterpolatableType) -> InterpolatableType: ...
def __sub__(self, other: InterpolatableType) -> InterpolatableType: ...
def __mul__(self, other: FactorType) -> InterpolatableType: ...
def __add__(self, other: InterpolatableType) -> InterpolatableType:
...

def __sub__(self, other: InterpolatableType) -> InterpolatableType:
...

def __mul__(self, other: TransformationType) -> InterpolatableType:
...
45 changes: 26 additions & 19 deletions Lib/fontParts/base/bPoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
from fontParts.base import normalizers
from fontParts.base.deprecated import DeprecatedBPoint, RemovedBPoint
from fontParts.base.annotations import (
CoordinateType,
TransformationMatrixType
PairType,
PairCollectionType,
SextupleCollectionType
)
if TYPE_CHECKING:
from fontParts.base.contour import BaseContour
Expand Down Expand Up @@ -241,16 +242,16 @@ def _get_font(self) -> Optional[BaseFont]:
"""
)

def _get_base_anchor(self) -> CoordinateType:
def _get_base_anchor(self) -> PairType[IntFloatType]:
value = self._get_anchor()
value = normalizers.normalizeCoordinateTuple(value)
return value

def _set_base_anchor(self, value: CoordinateType) -> None:
def _set_base_anchor(self, value: PairCollectionType[IntFloatType]) -> None:
value = normalizers.normalizeCoordinateTuple(value)
self._set_anchor(value)

def _get_anchor(self) -> CoordinateType:
def _get_anchor(self) -> PairType[IntFloatType]:
"""Get the the bPoint's anchor point.
This is the environment implementation of the :attr:`BaseBPoint.anchor`
Expand All @@ -268,7 +269,7 @@ def _get_anchor(self) -> CoordinateType:
point = self._point
return (point.x, point.y)

def _set_anchor(self, value: CoordinateType) -> None:
def _set_anchor(self, value: PairCollectionType[IntFloatType]) -> None:
"""Set the the bPoint's anchor point.
This is the environment implementation of the :attr:`BaseBPoint.anchor`
Expand Down Expand Up @@ -303,16 +304,16 @@ def _set_anchor(self, value: CoordinateType) -> None:
"""
)

def _get_base_bcpIn(self) -> CoordinateType:
def _get_base_bcpIn(self) -> PairType[IntFloatType]:
value = self._get_bcpIn()
value = normalizers.normalizeCoordinateTuple(value)
return value

def _set_base_bcpIn(self, value: CoordinateType) -> None:
def _set_base_bcpIn(self, value: PairCollectionType[IntFloatType]) -> None:
value = normalizers.normalizeCoordinateTuple(value)
self._set_bcpIn(value)

def _get_bcpIn(self) -> CoordinateType:
def _get_bcpIn(self) -> PairType[IntFloatType]:
"""Get the bPoint's incoming off-curve.
This is the environment implementation of the :attr:`BaseBPoint.bcpIn`
Expand All @@ -336,7 +337,7 @@ def _get_bcpIn(self) -> CoordinateType:
x = y = 0
return (x, y)

def _set_bcpIn(self, value: CoordinateType) -> None:
def _set_bcpIn(self, value: PairCollectionType[IntFloatType]) -> None:
"""Set the bPoint's incoming off-curve.
This is the environment implementation of the :attr:`BaseBPoint.bcpIn`
Expand Down Expand Up @@ -390,16 +391,16 @@ def _set_bcpIn(self, value: CoordinateType) -> None:
"""
)

def _get_base_bcpOut(self) -> CoordinateType:
def _get_base_bcpOut(self) -> PairType[IntFloatType]:
value = self._get_bcpOut()
value = normalizers.normalizeCoordinateTuple(value)
return value

def _set_base_bcpOut(self, value: CoordinateType) -> None:
def _set_base_bcpOut(self, value: PairCollectionType[IntFloatType]) -> None:
value = normalizers.normalizeCoordinateTuple(value)
self._set_bcpOut(value)

def _get_bcpOut(self) -> CoordinateType:
def _get_bcpOut(self) -> PairType[IntFloatType]:
"""Get the bPoint's outgoing off-curve.
This is the environment implementation of the :attr:`BaseBPoint.bcpOut`
Expand All @@ -423,7 +424,7 @@ def _get_bcpOut(self) -> CoordinateType:
x = y = 0
return (x, y)

def _set_bcpOut(self, value: CoordinateType) -> None:
def _set_bcpOut(self, value: PairCollectionType[IntFloatType]) -> None:
"""Set the bPoint's outgoing off-curve.
This is the environment implementation of the :attr:`BaseBPoint.bcpOut`
Expand Down Expand Up @@ -612,7 +613,9 @@ def _get_index(self) -> Optional[int]:
# Transformation
# --------------

def _transformBy(self, matrix: TransformationMatrixType, **kwargs: Any) -> None:
def _transformBy(self,
matrix: SextupleCollectionType[IntFloatType],
**kwargs: Any) -> None:
r"""Transform the native bPoint.
This is the environment implementation of :meth:`BaseBPoint.transformBy`.
Expand Down Expand Up @@ -666,7 +669,8 @@ def round(self) -> None:
normalizers.normalizeVisualRounding(y))


def relativeBCPIn(anchor: CoordinateType, BCPIn: CoordinateType) -> CoordinateType:
def relativeBCPIn(anchor: PairCollectionType[IntFloatType],
BCPIn: PairCollectionType[IntFloatType]) -> PairType[IntFloatType]:
"""convert absolute incoming bcp value to a relative value.
:param anchor: The anchor reference point from which to measure the relative
Expand All @@ -679,7 +683,8 @@ def relativeBCPIn(anchor: CoordinateType, BCPIn: CoordinateType) -> CoordinateTy
return (BCPIn[0] - anchor[0], BCPIn[1] - anchor[1])


def absoluteBCPIn(anchor: CoordinateType, BCPIn: CoordinateType) -> CoordinateType:
def absoluteBCPIn(anchor: PairCollectionType[IntFloatType],
BCPIn: PairCollectionType[IntFloatType]) -> PairType[IntFloatType]:
"""convert relative incoming bcp value to an absolute value.
:param anchor: The anchor reference point from which the relative BCP value
Expand All @@ -692,7 +697,8 @@ def absoluteBCPIn(anchor: CoordinateType, BCPIn: CoordinateType) -> CoordinateTy
return (BCPIn[0] + anchor[0], BCPIn[1] + anchor[1])


def relativeBCPOut(anchor: CoordinateType, BCPOut: CoordinateType) -> CoordinateType:
def relativeBCPOut(anchor: PairCollectionType[IntFloatType],
BCPOut: PairCollectionType[IntFloatType]) -> PairType[IntFloatType]:
"""convert absolute outgoing bcp value to a relative value.
:param anchor: The anchor reference point from which to measure the relative
Expand All @@ -705,7 +711,8 @@ def relativeBCPOut(anchor: CoordinateType, BCPOut: CoordinateType) -> Coordinate
return (BCPOut[0] - anchor[0], BCPOut[1] - anchor[1])


def absoluteBCPOut(anchor: CoordinateType, BCPOut: CoordinateType) -> CoordinateType:
def absoluteBCPOut(anchor: PairCollectionType[IntFloatType],
BCPOut: PairCollectionType[IntFloatType]) -> PairType[IntFloatType]:
"""convert relative outgoing bcp value to an absolute value.
:param anchor: The anchor reference point from which the relative BCP value
Expand Down
Loading

0 comments on commit 38f834d

Please sign in to comment.