+
Skip to content
Merged
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
33 changes: 21 additions & 12 deletions Lib/fontTools/pens/pointPen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
For instance, whether or not a point is smooth, and its name.
"""

from __future__ import annotations

import math
from typing import Any, Optional, Tuple, Dict
from typing import Any, Dict, List, Optional, Tuple

from fontTools.misc.loggingTools import LogMixin
from fontTools.pens.basePen import AbstractPen, MissingComponentError, PenError
from fontTools.misc.transform import DecomposedTransform, Identity
from fontTools.pens.basePen import AbstractPen, MissingComponentError, PenError

__all__ = [
"AbstractPointPen",
Expand All @@ -28,6 +30,14 @@
"ReverseContourPointPen",
]

# Some type aliases to make it easier below
Point = Tuple[float, float]
PointName = Optional[str]
# [(pt, smooth, name, kwargs)]
SegmentPointList = List[Tuple[Optional[Point], bool, PointName, Any]]
SegmentType = Optional[str]
SegmentList = List[Tuple[SegmentType, SegmentPointList]]


class AbstractPointPen:
"""Baseclass for all PointPens."""
Expand Down Expand Up @@ -88,15 +98,15 @@ class BasePointToSegmentPen(AbstractPointPen):
care of all the edge cases.
"""

def __init__(self):
def __init__(self) -> None:
self.currentPath = None

def beginPath(self, identifier=None, **kwargs):
if self.currentPath is not None:
raise PenError("Path already begun.")
self.currentPath = []

def _flushContour(self, segments):
def _flushContour(self, segments: SegmentList) -> None:
"""Override this method.

It will be called for each non-empty sub path with a list
Expand Down Expand Up @@ -124,7 +134,7 @@ def _flushContour(self, segments):
"""
raise NotImplementedError

def endPath(self):
def endPath(self) -> None:
if self.currentPath is None:
raise PenError("Path not begun.")
points = self.currentPath
Expand All @@ -134,7 +144,7 @@ def endPath(self):
if len(points) == 1:
# Not much more we can do than output a single move segment.
pt, segmentType, smooth, name, kwargs = points[0]
segments = [("move", [(pt, smooth, name, kwargs)])]
segments: SegmentList = [("move", [(pt, smooth, name, kwargs)])]
self._flushContour(segments)
return
segments = []
Expand Down Expand Up @@ -162,7 +172,7 @@ def endPath(self):
else:
points = points[firstOnCurve + 1 :] + points[: firstOnCurve + 1]

currentSegment = []
currentSegment: SegmentPointList = []
for pt, segmentType, smooth, name, kwargs in points:
currentSegment.append((pt, smooth, name, kwargs))
if segmentType is None:
Expand All @@ -189,7 +199,7 @@ class PointToSegmentPen(BasePointToSegmentPen):
and kwargs.
"""

def __init__(self, segmentPen, outputImpliedClosingLine=False):
def __init__(self, segmentPen, outputImpliedClosingLine: bool = False) -> None:
BasePointToSegmentPen.__init__(self)
self.pen = segmentPen
self.outputImpliedClosingLine = outputImpliedClosingLine
Expand Down Expand Up @@ -271,14 +281,14 @@ class SegmentToPointPen(AbstractPen):
PointPen protocol.
"""

def __init__(self, pointPen, guessSmooth=True):
def __init__(self, pointPen, guessSmooth=True) -> None:
if guessSmooth:
self.pen = GuessSmoothPointPen(pointPen)
else:
self.pen = pointPen
self.contour = None
self.contour: Optional[List[Tuple[Point, SegmentType]]] = None

def _flushContour(self):
def _flushContour(self) -> None:
pen = self.pen
pen.beginPath()
for pt, segmentType in self.contour:
Expand Down Expand Up @@ -594,7 +604,6 @@ def addComponent(self, baseGlyphName, transformation, identifier=None, **kwargs)
# if the transformation has a negative determinant, it will
# reverse the contour direction of the component
a, b, c, d = transformation[:4]
det = a * d - b * c
if a * d - b * c < 0:
pen = ReverseContourPointPen(pen)
glyph.drawPoints(pen)
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载