Skip to content

Fix inconsistent MathKerning ordering - #333

Open
fbirds5230 wants to merge 1 commit into
robotools:masterfrom
fbirds5230:fix/kerning-comparison-order
Open

fbirds5230 wants to merge 1 commit into
robotools:masterfrom
fbirds5230:fix/kerning-comparison-order

Conversation

@fbirds5230

Copy link
Copy Markdown

MathKerning can report both a < b and b < a as true when successive kerning values differ in opposite directions. Sorting the same two objects can then produce a different order depending on their input order.

_isLessish already stops at a smaller value, but continues past a larger one. Add the matching False return so the first differing value determines the comparison. This also fixes comparisons of kerning group members and keeps group data from overriding a kerning-value difference.

The regression tests cover both comparison directions, sorting from either input order, both group sides, and the priority of kerning values over groups. They fail on the original implementation.

tox -e py-cov passes on Python 3.12.14 with the pinned fonttools 4.60.2: 134 tests and 2 subtests, including doctests. Coverage combination and XML generation also pass. Other Python versions and operating systems were not run locally.

@typesupply

Copy link
Copy Markdown
Member

Could you show a brief example of where this is a problem, please? A short script showing just the kerning pairs will probably be enough. That will help me evaluate the patch.

@fbirds5230

Copy link
Copy Markdown
Author

Sure — two kerning pairs are enough, with no groups involved:

from fontMath.mathKerning import MathKerning

a = MathKerning({('A', 'V'): -80, ('T', 'o'): -40})
b = MathKerning({('A', 'V'): -70, ('T', 'o'): -50})

print('a < b:', a < b)
print('b < a:', b < a)
print('sorted([a, b]):', [dict(k.items()) for k in sorted([a, b])])
print('sorted([b, a]):', [dict(k.items()) for k in sorted([b, a])])

On current master this prints:

a < b: True
b < a: True
sorted([a, b]): [{('A', 'V'): -70, ('T', 'o'): -50}, {('A', 'V'): -80, ('T', 'o'): -40}]
sorted([b, a]): [{('A', 'V'): -80, ('T', 'o'): -40}, {('A', 'V'): -70, ('T', 'o'): -50}]

The first pair establishes a < b. In the reverse comparison, the code continues past -70 > -80 and then returns true for the second pair (-50 < -40).

I ran the same script against the PR head: a < b is true, b < a is false, and both sorts return [a, b].

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants