Merge pull request #2200 from justvanrossum/fix-issue-2198

Revert ABC changes to Pens, fixes #2198
This commit is contained in:
Cosimo Lupo 2021-02-26 19:38:08 +00:00 committed by GitHub
commit 25b4dd57ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 2 additions and 92 deletions

View File

@ -36,7 +36,6 @@ Coordinates are usually expressed as (x, y) tuples, but generally any
sequence of length 2 will do.
"""
import abc
from typing import Any, Tuple
from fontTools.misc.loggingTools import LogMixin
@ -45,42 +44,18 @@ __all__ = ["AbstractPen", "NullPen", "BasePen",
"decomposeSuperBezierSegment", "decomposeQuadraticSegment"]
class AbstractPen(abc.ABC):
@classmethod
def __subclasshook__(cls, subclass: Any) -> bool:
if cls is not AbstractPen:
return NotImplemented
return (
hasattr(subclass, "moveTo")
and callable(subclass.moveTo)
and hasattr(subclass, "lineTo")
and callable(subclass.lineTo)
and hasattr(subclass, "curveTo")
and callable(subclass.curveTo)
and hasattr(subclass, "qCurveTo")
and callable(subclass.qCurveTo)
and hasattr(subclass, "closePath")
and callable(subclass.closePath)
and hasattr(subclass, "endPath")
and callable(subclass.endPath)
and hasattr(subclass, "addComponent")
and callable(subclass.addComponent)
or NotImplemented
)
class AbstractPen:
@abc.abstractmethod
def moveTo(self, pt: Tuple[float, float]) -> None:
"""Begin a new sub path, set the current point to 'pt'. You must
end each sub path with a call to pen.closePath() or pen.endPath().
"""
raise NotImplementedError
@abc.abstractmethod
def lineTo(self, pt: Tuple[float, float]) -> None:
"""Draw a straight line from the current point to 'pt'."""
raise NotImplementedError
@abc.abstractmethod
def curveTo(self, *points: Tuple[float, float]) -> None:
"""Draw a cubic bezier with an arbitrary number of control points.
@ -102,7 +77,6 @@ class AbstractPen(abc.ABC):
"""
raise NotImplementedError
@abc.abstractmethod
def qCurveTo(self, *points: Tuple[float, float]) -> None:
"""Draw a whole string of quadratic curve segments.
@ -120,21 +94,18 @@ class AbstractPen(abc.ABC):
"""
raise NotImplementedError
@abc.abstractmethod
def closePath(self) -> None:
"""Close the current sub path. You must call either pen.closePath()
or pen.endPath() after each sub path.
"""
pass
@abc.abstractmethod
def endPath(self) -> None:
"""End the current sub path, but don't close it. You must call
either pen.closePath() or pen.endPath() after each sub path.
"""
pass
@abc.abstractmethod
def addComponent(
self,
glyphName: str,

View File

@ -12,7 +12,6 @@ This allows the caller to provide more data for each point.
For instance, whether or not a point is smooth, and its name.
"""
import abc
import math
from typing import Any, List, Optional, Tuple
@ -28,36 +27,17 @@ __all__ = [
]
class AbstractPointPen(abc.ABC):
class AbstractPointPen:
"""Baseclass for all PointPens."""
@classmethod
def __subclasshook__(cls, subclass: Any) -> bool:
if cls is not AbstractPointPen:
return NotImplemented
return (
hasattr(subclass, "beginPath")
and callable(subclass.beginPath)
and hasattr(subclass, "endPath")
and callable(subclass.endPath)
and hasattr(subclass, "addPoint")
and callable(subclass.addPoint)
and hasattr(subclass, "addComponent")
and callable(subclass.addComponent)
or NotImplemented
)
@abc.abstractmethod
def beginPath(self, identifier: Optional[str] = None, **kwargs: Any) -> None:
"""Start a new sub path."""
raise NotImplementedError
@abc.abstractmethod
def endPath(self) -> None:
"""End the current sub path."""
raise NotImplementedError
@abc.abstractmethod
def addPoint(
self,
pt: Tuple[float, float],
@ -70,7 +50,6 @@ class AbstractPointPen(abc.ABC):
"""Add a point to the current sub path."""
raise NotImplementedError
@abc.abstractmethod
def addComponent(
self,
baseGlyphName: str,

View File

@ -5,29 +5,6 @@ from fontTools.misc.loggingTools import CapturingLogHandler
import unittest
def test_subclasshook():
class NullPen:
def moveTo(self, pt):
pass
def lineTo(self, pt):
pass
def curveTo(self, *points):
pass
def qCurveTo(self, *points):
pass
def closePath(self):
pass
def endPath(self):
pass
def addComponent(self, glyphName, transformation):
pass
assert issubclass(NullPen, AbstractPen)
assert isinstance(NullPen(), AbstractPen)
assert not issubclass(NullPen, AbstractPointPen)
assert not isinstance(NullPen(), AbstractPointPen)
class _TestPen(BasePen):
def __init__(self):
BasePen.__init__(self, glyphSet={})

View File

@ -5,23 +5,6 @@ from fontTools.pens.pointPen import AbstractPointPen, PointToSegmentPen, \
SegmentToPointPen, GuessSmoothPointPen, ReverseContourPointPen
def test_subclasshook():
class NullPen:
def beginPath(self, identifier, **kwargs) -> None:
pass
def endPath(self) -> None:
pass
def addPoint(self, pt, segmentType, smooth, name, identifier, **kwargs) -> None:
pass
def addComponent(self, baseGlyphName, transformation, identifier, **kwargs) -> None:
pass
assert issubclass(NullPen, AbstractPointPen)
assert isinstance(NullPen(), AbstractPointPen)
assert not issubclass(NullPen, AbstractPen)
assert not isinstance(NullPen(), AbstractPen)
class _TestSegmentPen(AbstractPen):
def __init__(self):