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

allow to reverseDirection without convertCubics #770

Merged
merged 1 commit into from
Jul 28, 2023
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
2 changes: 2 additions & 0 deletions Lib/ufo2ft/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .flattenComponents import FlattenComponentsFilter
from .propagateAnchors import PropagateAnchorsFilter
from .removeOverlaps import RemoveOverlapsFilter
from .reverseContourDirection import ReverseContourDirectionFilter
from .sortContours import SortContoursFilter
from .transformations import TransformationsFilter

Expand All @@ -27,6 +28,7 @@
"FlattenComponentsFilter",
"PropagateAnchorsFilter",
"RemoveOverlapsFilter",
"ReverseContourDirectionFilter",
"SortContoursFilter",
"TransformationsFilter",
"loadFilters",
Expand Down
15 changes: 15 additions & 0 deletions Lib/ufo2ft/filters/reverseContourDirection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from fontTools.pens.pointPen import ReverseContourPointPen

from ufo2ft.filters import BaseFilter


class ReverseContourDirectionFilter(BaseFilter):
def filter(self, glyph):
if not len(glyph):
return False
pen = ReverseContourPointPen(glyph.getPointPen())
contours = list(glyph)
glyph.clearContours()
for contour in contours:
contour.drawPoints(pen)
return True
14 changes: 14 additions & 0 deletions Lib/ufo2ft/preProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ def initDefaultFilters(
allQuadratic=allQuadratic,
)
)
elif reverseDirection:
from ufo2ft.filters.reverseContourDirection import (
ReverseContourDirectionFilter,
)

filters.append(ReverseContourDirectionFilter(include=lambda g: len(g)))
return filters


Expand Down Expand Up @@ -345,6 +351,14 @@ def process(self):
remember_curve_type=self._rememberCurveType and self.inplace,
all_quadratic=self.allQuadratic,
)
elif self._reverseDirection:
from ufo2ft.filters.reverseContourDirection import (
ReverseContourDirectionFilter,
)

reverseDirection = ReverseContourDirectionFilter(include=lambda g: len(g))
for ufo, glyphSet in zip(self.ufos, self.glyphSets):
reverseDirection(ufo, glyphSet)

# TrueType fonts cannot mix contours and components, so pick out all glyphs
# that have contours (`bool(len(g)) == True`) and decompose their
Expand Down
30 changes: 30 additions & 0 deletions tests/preProcessor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ def test_custom_filters_in_both_lib_and_argument_with_ellipsis(self, FontClass):
a = glyphSet["a"]
assert (a[0][0].x, a[0][0].y) == (ufo["a"][0][0].x + 10, ufo["a"][0][0].y - 10)

def test_no_convertCubics_reverseDirection(self, FontClass):
ufo = FontClass(getpath("TestFont.ufo"))

glyphSet = TTFPreProcessor(
ufo, convertCubics=False, reverseDirection=True
).process()

contours = [contour for contour in glyphSet["c"]]
points = [point for point in contours[0]]
assert points[0].segmentType == "line"
assert points[1].segmentType is None
assert points[2].segmentType is None
assert points[3].segmentType == "curve"


class TTFInterpolatablePreProcessorTest:
def test_no_inplace(self, FontClass):
Expand Down Expand Up @@ -248,6 +262,22 @@ def test_custom_filters_in_both_lib_and_argument_with_ellipsis(self, FontClass):
ufo2["a"][0][0].y - 10,
)

def test_no_convertCubics_reverseDirection(self, FontClass):
ufo1 = FontClass(getpath("TestFont.ufo"))
ufo2 = FontClass(getpath("TestFont.ufo"))

glyphSets = TTFInterpolatablePreProcessor(
[ufo1, ufo2], convertCubics=False, reverseDirection=True
).process()

for glyphSet in glyphSets:
contours = [contour for contour in glyphSet["c"]]
points = [point for point in contours[0]]
assert points[0].segmentType == "line"
assert points[1].segmentType is None
assert points[2].segmentType is None
assert points[3].segmentType == "curve"


class SkipExportGlyphsTest:
def test_skip_export_glyphs_filter(self, FontClass):
Expand Down
Loading