Skip to content

Commit adc7c38

Browse files
authored
Merge pull request #194 from anthrotype/add-scale-component-transform-option
mathGlyph: add scaleComponentTransform option
2 parents e44a951 + 30d8826 commit adc7c38

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

Lib/fontMath/mathGlyph.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,19 @@ class MathGlyph(object):
7070
same order as the original.
7171
"""
7272

73-
def __init__(self, glyph):
73+
def __init__(self, glyph, scaleComponentTransform=True):
74+
"""Initialize a new MathGlyph object.
75+
76+
Args:
77+
glyph: Input defcon or defcon-like Glyph object to copy from. Set to None to
78+
to make an empty MathGlyph.
79+
scaleComponentTransform (bool): when performing multiplication or division, by
80+
default all elements of a component's affine transformation matrix are
81+
multiplied by the given scalar. If scaleComponentTransform is False, then
82+
only the component's xOffset and yOffset attributes are scaled, whereas the
83+
xScale, xyScale, yxScale and yScale attributes are kept unchanged.
84+
"""
85+
self.scaleComponentTransform = scaleComponentTransform
7486
self.contours = []
7587
self.components = []
7688
if glyph is None:
@@ -220,7 +232,9 @@ def _processMathTwo(self, copiedGlyph, factor, ptFunc, func):
220232
# components
221233
copiedGlyph.components = []
222234
if self.components:
223-
copiedGlyph.components = _processMathTwoComponents(self.components, factor, ptFunc)
235+
copiedGlyph.components = _processMathTwoComponents(
236+
self.components, factor, ptFunc, scaleComponentTransform=self.scaleComponentTransform
237+
)
224238
# anchors
225239
copiedGlyph.anchors = []
226240
if self.anchors:
@@ -689,11 +703,13 @@ def _processMathOneComponents(componentPairs, func):
689703
result.append(component)
690704
return result
691705

692-
def _processMathTwoComponents(components, factor, func):
706+
def _processMathTwoComponents(components, factor, func, scaleComponentTransform=True):
693707
result = []
694708
for component in components:
695709
component = dict(component)
696-
component["transformation"] = _processMathTwoTransformation(component["transformation"], factor, func)
710+
component["transformation"] = _processMathTwoTransformation(
711+
component["transformation"], factor, func, doScale=scaleComponentTransform
712+
)
697713
result.append(component)
698714
return result
699715

@@ -761,10 +777,11 @@ def _processMathOneTransformation(transformation1, transformation2, func):
761777
xOffset, yOffset = func((xOffset1, yOffset1), (xOffset2, yOffset2))
762778
return (xScale, xyScale, yxScale, yScale, xOffset, yOffset)
763779

764-
def _processMathTwoTransformation(transformation, factor, func):
780+
def _processMathTwoTransformation(transformation, factor, func, doScale=True):
765781
xScale, xyScale, yxScale, yScale, xOffset, yOffset = transformation
766-
xScale, yScale = func((xScale, yScale), factor)
767-
xyScale, yxScale = func((xyScale, yxScale), factor)
782+
if doScale:
783+
xScale, yScale = func((xScale, yScale), factor)
784+
xyScale, yxScale = func((xyScale, yxScale), factor)
768785
xOffset, yOffset = func((xOffset, yOffset), factor)
769786
return (xScale, xyScale, yxScale, yScale, xOffset, yOffset)
770787

Lib/fontMath/test/test_mathGlyph.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -895,15 +895,32 @@ def test_processMathOneComponents(self):
895895

896896
def test_processMathTwoComponents(self):
897897
components = [
898-
dict(baseGlyph="A", transformation=(1, 2, 3, 4, 5, 6),
899-
identifier="1"),
898+
dict(baseGlyph="A", transformation=(1, 2, 3, 4, 5, 6), identifier="1")
899+
]
900+
scaled_components = [
901+
dict(baseGlyph="A", transformation=(2, 4, 4.5, 6, 10, 9), identifier="1")
900902
]
901903
self.assertEqual(
902904
_processMathTwoComponents(components, (2, 1.5), mulPt),
905+
scaled_components
906+
)
907+
self.assertEqual(
908+
_processMathTwoComponents(
909+
components, (2, 1.5), mulPt, scaleComponentTransform=True
910+
),
911+
scaled_components
912+
)
913+
self.assertEqual(
914+
_processMathTwoComponents(
915+
components, (2, 1.5), mulPt, scaleComponentTransform=False
916+
),
903917
[
904-
dict(baseGlyph="A", transformation=(2, 4, 4.5, 6, 10, 9),
905-
identifier="1")
906-
]
918+
dict(
919+
baseGlyph="A",
920+
transformation=(1, 2, 3, 4, 10, 9),
921+
identifier="1"
922+
)
923+
],
907924
)
908925

909926
def test_expandImage(self):
@@ -979,6 +996,14 @@ def test_processMathTwoTransformation(self):
979996
_processMathTwoTransformation(transformation, (2, 1.5), mulPt),
980997
(2, 4, 4.5, 6, 10, 9)
981998
)
999+
self.assertEqual(
1000+
_processMathTwoTransformation(transformation, (2, 1.5), mulPt, doScale=True),
1001+
(2, 4, 4.5, 6, 10, 9)
1002+
)
1003+
self.assertEqual(
1004+
_processMathTwoTransformation(transformation, (2, 1.5), mulPt, doScale=False),
1005+
(1, 2, 3, 4, 10, 9)
1006+
)
9821007

9831008
def test_roundContours(self):
9841009
contour = [

0 commit comments

Comments
 (0)