aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/fonttools/fontTools/varLib/interpolatable.py
blob: 74dd15b96898b881bebcfa0864f1a0343a529d20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
"""
Tool to find wrong contour order between different masters, and
other interpolatability (or lack thereof) issues.

Call as:
$ fonttools varLib.interpolatable font1 font2 ...
"""

from fontTools.pens.basePen import AbstractPen, BasePen
from fontTools.pens.pointPen import AbstractPointPen, SegmentToPointPen
from fontTools.pens.recordingPen import RecordingPen
from fontTools.pens.statisticsPen import StatisticsPen, StatisticsControlPen
from fontTools.pens.momentsPen import OpenContourError
from fontTools.varLib.models import piecewiseLinearMap, normalizeLocation
from fontTools.misc.fixedTools import floatToFixedToStr
from collections import defaultdict, deque
from functools import wraps
from pprint import pformat
from math import sqrt, copysign
import itertools
import logging

log = logging.getLogger("fontTools.varLib.interpolatable")


def _rot_list(l, k):
    """Rotate list by k items forward.  Ie. item at position 0 will be
    at position k in returned list.  Negative k is allowed."""
    return l[-k:] + l[:-k]


class PerContourPen(BasePen):
    def __init__(self, Pen, glyphset=None):
        BasePen.__init__(self, glyphset)
        self._glyphset = glyphset
        self._Pen = Pen
        self._pen = None
        self.value = []

    def _moveTo(self, p0):
        self._newItem()
        self._pen.moveTo(p0)

    def _lineTo(self, p1):
        self._pen.lineTo(p1)

    def _qCurveToOne(self, p1, p2):
        self._pen.qCurveTo(p1, p2)

    def _curveToOne(self, p1, p2, p3):
        self._pen.curveTo(p1, p2, p3)

    def _closePath(self):
        self._pen.closePath()
        self._pen = None

    def _endPath(self):
        self._pen.endPath()
        self._pen = None

    def _newItem(self):
        self._pen = pen = self._Pen()
        self.value.append(pen)


class PerContourOrComponentPen(PerContourPen):
    def addComponent(self, glyphName, transformation):
        self._newItem()
        self.value[-1].addComponent(glyphName, transformation)


class SimpleRecordingPointPen(AbstractPointPen):
    def __init__(self):
        self.value = []

    def beginPath(self, identifier=None, **kwargs):
        pass

    def endPath(self) -> None:
        pass

    def addPoint(self, pt, segmentType=None):
        self.value.append((pt, False if segmentType is None else True))


def _vdiff_hypot2(v0, v1):
    s = 0
    for x0, x1 in zip(v0, v1):
        d = x1 - x0
        s += d * d
    return s


def _vdiff_hypot2_complex(v0, v1):
    s = 0
    for x0, x1 in zip(v0, v1):
        d = x1 - x0
        s += d.real * d.real + d.imag * d.imag
    return s


def _matching_cost(G, matching):
    return sum(G[i][j] for i, j in enumerate(matching))


def min_cost_perfect_bipartite_matching_scipy(G):
    n = len(G)
    rows, cols = linear_sum_assignment(G)
    assert (rows == list(range(n))).all()
    return list(cols), _matching_cost(G, cols)


def min_cost_perfect_bipartite_matching_munkres(G):
    n = len(G)
    cols = [None] * n
    for row, col in Munkres().compute(G):
        cols[row] = col
    return cols, _matching_cost(G, cols)


def min_cost_perfect_bipartite_matching_bruteforce(G):
    n = len(G)

    if n > 6:
        raise Exception("Install Python module 'munkres' or 'scipy >= 0.17.0'")

    # Otherwise just brute-force
    permutations = itertools.permutations(range(n))
    best = list(next(permutations))
    best_cost = _matching_cost(G, best)
    for p in permutations:
        cost = _matching_cost(G, p)
        if cost < best_cost:
            best, best_cost = list(p), cost
    return best, best_cost


try:
    from scipy.optimize import linear_sum_assignment

    min_cost_perfect_bipartite_matching = min_cost_perfect_bipartite_matching_scipy
except ImportError:
    try:
        from munkres import Munkres

        min_cost_perfect_bipartite_matching = (
            min_cost_perfect_bipartite_matching_munkres
        )
    except ImportError:
        min_cost_perfect_bipartite_matching = (
            min_cost_perfect_bipartite_matching_bruteforce
        )


def _contour_vector_from_stats(stats):
    size = sqrt(abs(stats.area))
    return (
        copysign((size), stats.area),
        stats.meanX,
        stats.meanY,
        stats.stddevX * 2,
        stats.stddevY * 2,
        stats.correlation * size,
    )


def _points_characteristic_bits(points):
    bits = 0
    for pt, b in reversed(points):
        bits = (bits << 1) | b
    return bits


def _points_complex_vector(points):
    vector = []
    points = [complex(*pt) for pt, _ in points]
    n = len(points)
    points.extend(points[:2])
    for i in range(n):
        p0 = points[i]

        # The point itself
        vector.append(p0)

        # The distance to the next point;
        # Emphasized by 2 empirically
        p1 = points[i + 1]
        d0 = p1 - p0
        vector.append(d0 * 2)

        """
        # The angle to the next point, as a cross product;
        # Square root of, to match dimentionality of distance.
        p2 = points[i + 2]
        d1 = p2 - p1
        cross = d0.real * d1.imag - d0.imag * d1.real
        cross = copysign(sqrt(abs(cross)), cross)
        vector.append(cross)
        """

    return vector


def _add_isomorphisms(points, isomorphisms, reverse):
    reference_bits = _points_characteristic_bits(points)
    n = len(points)

    # if points[0][0] == points[-1][0]:
    #   abort

    if reverse:
        points = points[::-1]
        bits = _points_characteristic_bits(points)
    else:
        bits = reference_bits

    vector = _points_complex_vector(points)

    assert len(vector) % n == 0
    mult = len(vector) // n
    mask = (1 << n) - 1

    for i in range(n):
        b = ((bits << (n - i)) & mask) | (bits >> i)
        if b == reference_bits:
            isomorphisms.append(
                (_rot_list(vector, -i * mult), n - 1 - i if reverse else i, reverse)
            )


def _find_parents_and_order(glyphsets, locations):
    parents = [None] + list(range(len(glyphsets) - 1))
    order = list(range(len(glyphsets)))
    if locations:
        # Order base master first
        bases = (i for i, l in enumerate(locations) if all(v == 0 for v in l.values()))
        if bases:
            base = next(bases)
            logging.info("Base master index %s, location %s", base, locations[base])
        else:
            base = 0
            logging.warning("No base master location found")

        # Form a minimum spanning tree of the locations
        try:
            from scipy.sparse.csgraph import minimum_spanning_tree

            graph = [[0] * len(locations) for _ in range(len(locations))]
            axes = set()
            for l in locations:
                axes.update(l.keys())
            axes = sorted(axes)
            vectors = [tuple(l.get(k, 0) for k in axes) for l in locations]
            for i, j in itertools.combinations(range(len(locations)), 2):
                graph[i][j] = _vdiff_hypot2(vectors[i], vectors[j])

            tree = minimum_spanning_tree(graph)
            rows, cols = tree.nonzero()
            graph = defaultdict(set)
            for row, col in zip(rows, cols):
                graph[row].add(col)
                graph[col].add(row)

            # Traverse graph from the base and assign parents
            parents = [None] * len(locations)
            order = []
            visited = set()
            queue = deque([base])
            while queue:
                i = queue.popleft()
                visited.add(i)
                order.append(i)
                for j in sorted(graph[i]):
                    if j not in visited:
                        parents[j] = i
                        queue.append(j)

        except ImportError:
            pass

        log.info("Parents: %s", parents)
        log.info("Order: %s", order)
    return parents, order


def test_gen(
    glyphsets,
    glyphs=None,
    names=None,
    ignore_missing=False,
    *,
    locations=None,
    tolerance=0.95,
):
    if names is None:
        names = glyphsets

    if glyphs is None:
        # `glyphs = glyphsets[0].keys()` is faster, certainly, but doesn't allow for sparse TTFs/OTFs given out of order
        # ... risks the sparse master being the first one, and only processing a subset of the glyphs
        glyphs = {g for glyphset in glyphsets for g in glyphset.keys()}

    parents, order = _find_parents_and_order(glyphsets, locations)

    def grand_parent(i, glyphname):
        if i is None:
            return None
        i = parents[i]
        if i is None:
            return None
        while parents[i] is not None and glyphsets[i][glyphname] is None:
            i = parents[i]
        return i

    for glyph_name in glyphs:
        log.info("Testing glyph %s", glyph_name)
        allGreenVectors = []
        allControlVectors = []
        allNodeTypes = []
        allContourIsomorphisms = []
        allGlyphs = [glyphset[glyph_name] for glyphset in glyphsets]
        if len([1 for glyph in allGlyphs if glyph is not None]) <= 1:
            continue
        for glyph, glyphset, name in zip(allGlyphs, glyphsets, names):
            if glyph is None:
                if not ignore_missing:
                    yield (glyph_name, {"type": "missing", "master": name})
                allNodeTypes.append(None)
                allControlVectors.append(None)
                allGreenVectors.append(None)
                allContourIsomorphisms.append(None)
                continue

            perContourPen = PerContourOrComponentPen(RecordingPen, glyphset=glyphset)
            try:
                glyph.draw(perContourPen, outputImpliedClosingLine=True)
            except TypeError:
                glyph.draw(perContourPen)
            contourPens = perContourPen.value
            del perContourPen

            contourControlVectors = []
            contourGreenVectors = []
            contourIsomorphisms = []
            nodeTypes = []
            allNodeTypes.append(nodeTypes)
            allControlVectors.append(contourControlVectors)
            allGreenVectors.append(contourGreenVectors)
            allContourIsomorphisms.append(contourIsomorphisms)
            for ix, contour in enumerate(contourPens):
                contourOps = tuple(op for op, arg in contour.value)
                nodeTypes.append(contourOps)

                greenStats = StatisticsPen(glyphset=glyphset)
                controlStats = StatisticsControlPen(glyphset=glyphset)
                try:
                    contour.replay(greenStats)
                    contour.replay(controlStats)
                except OpenContourError as e:
                    yield (
                        glyph_name,
                        {"master": name, "contour": ix, "type": "open_path"},
                    )
                    continue
                contourGreenVectors.append(_contour_vector_from_stats(greenStats))
                contourControlVectors.append(_contour_vector_from_stats(controlStats))

                # Check starting point
                if contourOps[0] == "addComponent":
                    continue
                assert contourOps[0] == "moveTo"
                assert contourOps[-1] in ("closePath", "endPath")
                points = SimpleRecordingPointPen()
                converter = SegmentToPointPen(points, False)
                contour.replay(converter)
                # points.value is a list of pt,bool where bool is true if on-curve and false if off-curve;
                # now check all rotations and mirror-rotations of the contour and build list of isomorphic
                # possible starting points.

                isomorphisms = []
                contourIsomorphisms.append(isomorphisms)

                # Add rotations
                _add_isomorphisms(points.value, isomorphisms, False)
                # Add mirrored rotations
                _add_isomorphisms(points.value, isomorphisms, True)

        matchings = [None] * len(allControlVectors)

        for m1idx in order:
            if allNodeTypes[m1idx] is None:
                continue
            m0idx = grand_parent(m1idx, glyph_name)
            if m0idx is None:
                continue
            if allNodeTypes[m0idx] is None:
                continue

            m1 = allNodeTypes[m1idx]
            m0 = allNodeTypes[m0idx]
            if len(m0) != len(m1):
                yield (
                    glyph_name,
                    {
                        "type": "path_count",
                        "master_1": names[m0idx],
                        "master_2": names[m1idx],
                        "value_1": len(m0),
                        "value_2": len(m1),
                    },
                )
                continue

            if m0 != m1:
                for pathIx, (nodes1, nodes2) in enumerate(zip(m0, m1)):
                    if nodes1 == nodes2:
                        continue
                    if len(nodes1) != len(nodes2):
                        yield (
                            glyph_name,
                            {
                                "type": "node_count",
                                "path": pathIx,
                                "master_1": names[m0idx],
                                "master_2": names[m1idx],
                                "value_1": len(nodes1),
                                "value_2": len(nodes2),
                            },
                        )
                        continue
                    for nodeIx, (n1, n2) in enumerate(zip(nodes1, nodes2)):
                        if n1 != n2:
                            yield (
                                glyph_name,
                                {
                                    "type": "node_incompatibility",
                                    "path": pathIx,
                                    "node": nodeIx,
                                    "master_1": names[m0idx],
                                    "master_2": names[m1idx],
                                    "value_1": n1,
                                    "value_2": n2,
                                },
                            )
                            continue

            m1Control = allControlVectors[m1idx]
            m1Green = allGreenVectors[m1idx]
            m0Control = allControlVectors[m0idx]
            m0Green = allGreenVectors[m0idx]
            if len(m1Control) > 1:
                identity_matching = list(range(len(m0Control)))

                # We try matching both the StatisticsControlPen vector
                # and the StatisticsPen vector.
                # If either method found a identity matching, accept it.
                # This is crucial for fonts like Kablammo[MORF].ttf and
                # Nabla[EDPT,EHLT].ttf, since they really confuse the
                # StatisticsPen vector because of their area=0 contours.
                #
                # TODO: Optimize by only computing the StatisticsPen vector
                # and then checking if it is the identity vector. Only if
                # not, compute the StatisticsControlPen vector and check both.

                costsControl = [
                    [_vdiff_hypot2(v0, v1) for v1 in m1Control] for v0 in m0Control
                ]
                (
                    matching_control,
                    matching_cost_control,
                ) = min_cost_perfect_bipartite_matching(costsControl)
                identity_cost_control = sum(
                    costsControl[i][i] for i in range(len(m0Control))
                )
                done = matching_cost_control == identity_cost_control

                if not done:
                    costsGreen = [
                        [_vdiff_hypot2(v0, v1) for v1 in m1Green] for v0 in m0Green
                    ]
                    (
                        matching_green,
                        matching_cost_green,
                    ) = min_cost_perfect_bipartite_matching(costsGreen)
                    identity_cost_green = sum(
                        costsGreen[i][i] for i in range(len(m0Control))
                    )
                    done = matching_cost_green == identity_cost_green

                if not done:
                    # Otherwise, use the worst of the two matchings.
                    if (
                        matching_cost_control / identity_cost_control
                        < matching_cost_green / identity_cost_green
                    ):
                        matching = matching_control
                        matching_cost = matching_cost_control
                        identity_cost = identity_cost_control
                    else:
                        matching = matching_green
                        matching_cost = matching_cost_green
                        identity_cost = identity_cost_green

                    if matching_cost < identity_cost * tolerance:
                        # print(matching_cost_control / identity_cost_control, matching_cost_green / identity_cost_green)

                        yield (
                            glyph_name,
                            {
                                "type": "contour_order",
                                "master_1": names[m0idx],
                                "master_2": names[m1idx],
                                "value_1": list(range(len(m0Control))),
                                "value_2": matching,
                            },
                        )
                        matchings[m1idx] = matching

            m1 = allContourIsomorphisms[m1idx]
            m0 = allContourIsomorphisms[m0idx]

            for ix, (contour0, contour1) in enumerate(zip(m0, m1)):
                if len(contour0) == 0 or len(contour0) != len(contour1):
                    # We already reported this; or nothing to do
                    continue

                c0 = contour0[0]
                costs = [_vdiff_hypot2_complex(c0[0], c1[0]) for c1 in contour1]
                min_cost_idx, min_cost = min(enumerate(costs), key=lambda x: x[1])
                first_cost = costs[0]
                if min_cost < first_cost * tolerance:
                    reverse = contour1[min_cost_idx][2]

                    # If contour-order is wrong, don't report a reversing
                    if (
                        reverse
                        and matchings[m1idx] is not None
                        and matchings[m1idx][ix] != ix
                    ):
                        continue

                    yield (
                        glyph_name,
                        {
                            "type": "wrong_start_point",
                            "contour": ix,
                            "master_1": names[m0idx],
                            "master_2": names[m1idx],
                            "value_1": 0,
                            "value_2": contour1[min_cost_idx][1],
                            "reversed": reverse,
                        },
                    )


@wraps(test_gen)
def test(*args, **kwargs):
    problems = defaultdict(list)
    for glyphname, problem in test_gen(*args, **kwargs):
        problems[glyphname].append(problem)
    return problems


def recursivelyAddGlyph(glyphname, glyphset, ttGlyphSet, glyf):
    if glyphname in glyphset:
        return
    glyphset[glyphname] = ttGlyphSet[glyphname]

    for component in getattr(glyf[glyphname], "components", []):
        recursivelyAddGlyph(component.glyphName, glyphset, ttGlyphSet, glyf)


def main(args=None):
    """Test for interpolatability issues between fonts"""
    import argparse
    import sys

    parser = argparse.ArgumentParser(
        "fonttools varLib.interpolatable",
        description=main.__doc__,
    )
    parser.add_argument(
        "--glyphs",
        action="store",
        help="Space-separate name of glyphs to check",
    )
    parser.add_argument(
        "--tolerance",
        action="store",
        type=float,
        help="Error tolerance. Default 0.95",
    )
    parser.add_argument(
        "--json",
        action="store_true",
        help="Output report in JSON format",
    )
    parser.add_argument(
        "--pdf",
        action="store",
        help="Output report in PDF format",
    )
    parser.add_argument(
        "--html",
        action="store",
        help="Output report in HTML format",
    )
    parser.add_argument(
        "--quiet",
        action="store_true",
        help="Only exit with code 1 or 0, no output",
    )
    parser.add_argument(
        "--output",
        action="store",
        help="Output file for the problem report; Default: stdout",
    )
    parser.add_argument(
        "--ignore-missing",
        action="store_true",
        help="Will not report glyphs missing from sparse masters as errors",
    )
    parser.add_argument(
        "inputs",
        metavar="FILE",
        type=str,
        nargs="+",
        help="Input a single variable font / DesignSpace / Glyphs file, or multiple TTF/UFO files",
    )
    parser.add_argument("-v", "--verbose", action="store_true", help="Run verbosely.")

    args = parser.parse_args(args)

    from fontTools import configLogger

    configLogger(level=("INFO" if args.verbose else "ERROR"))

    glyphs = args.glyphs.split() if args.glyphs else None

    from os.path import basename

    fonts = []
    names = []
    locations = []

    if len(args.inputs) == 1:
        designspace = None
        if args.inputs[0].endswith(".designspace"):
            from fontTools.designspaceLib import DesignSpaceDocument

            designspace = DesignSpaceDocument.fromfile(args.inputs[0])
            args.inputs = [master.path for master in designspace.sources]
            locations = [master.location for master in designspace.sources]
            axis_triples = {
                a.name: (a.minimum, a.default, a.maximum) for a in designspace.axes
            }
            axis_mappings = {a.name: a.map for a in designspace.axes}
            axis_triples = {
                k: tuple(piecewiseLinearMap(v, dict(axis_mappings[k])) for v in vv)
                for k, vv in axis_triples.items()
            }

        elif args.inputs[0].endswith(".glyphs"):
            from glyphsLib import GSFont, to_designspace

            gsfont = GSFont(args.inputs[0])
            designspace = to_designspace(gsfont)
            fonts = [source.font for source in designspace.sources]
            names = ["%s-%s" % (f.info.familyName, f.info.styleName) for f in fonts]
            args.inputs = []
            locations = [master.location for master in designspace.sources]
            axis_triples = {
                a.name: (a.minimum, a.default, a.maximum) for a in designspace.axes
            }
            axis_mappings = {a.name: a.map for a in designspace.axes}
            axis_triples = {
                k: tuple(piecewiseLinearMap(v, dict(axis_mappings[k])) for v in vv)
                for k, vv in axis_triples.items()
            }

        elif args.inputs[0].endswith(".ttf"):
            from fontTools.ttLib import TTFont

            font = TTFont(args.inputs[0])
            if "gvar" in font:
                # Is variable font

                axisMapping = {}
                fvar = font["fvar"]
                for axis in fvar.axes:
                    axisMapping[axis.axisTag] = {
                        -1: axis.minValue,
                        0: axis.defaultValue,
                        1: axis.maxValue,
                    }
                if "avar" in font:
                    avar = font["avar"]
                    for axisTag, segments in avar.segments.items():
                        fvarMapping = axisMapping[axisTag].copy()
                        for location, value in segments.items():
                            axisMapping[axisTag][value] = piecewiseLinearMap(
                                location, fvarMapping
                            )

                gvar = font["gvar"]
                glyf = font["glyf"]
                # Gather all glyphs at their "master" locations
                ttGlyphSets = {}
                glyphsets = defaultdict(dict)

                if glyphs is None:
                    glyphs = sorted(gvar.variations.keys())
                for glyphname in glyphs:
                    for var in gvar.variations[glyphname]:
                        locDict = {}
                        loc = []
                        for tag, val in sorted(var.axes.items()):
                            locDict[tag] = val[1]
                            loc.append((tag, val[1]))

                        locTuple = tuple(loc)
                        if locTuple not in ttGlyphSets:
                            ttGlyphSets[locTuple] = font.getGlyphSet(
                                location=locDict, normalized=True
                            )

                        recursivelyAddGlyph(
                            glyphname, glyphsets[locTuple], ttGlyphSets[locTuple], glyf
                        )

                names = ["''"]
                fonts = [font.getGlyphSet()]
                locations = [{}]
                axis_triples = {a: (-1, 0, +1) for a in sorted(axisMapping.keys())}
                for locTuple in sorted(glyphsets.keys(), key=lambda v: (len(v), v)):
                    name = (
                        "'"
                        + " ".join(
                            "%s=%s"
                            % (
                                k,
                                floatToFixedToStr(
                                    piecewiseLinearMap(v, axisMapping[k]), 14
                                ),
                            )
                            for k, v in locTuple
                        )
                        + "'"
                    )
                    names.append(name)
                    fonts.append(glyphsets[locTuple])
                    locations.append(dict(locTuple))
                args.ignore_missing = True
                args.inputs = []

    if not locations:
        locations = [{} for _ in fonts]

    for filename in args.inputs:
        if filename.endswith(".ufo"):
            from fontTools.ufoLib import UFOReader

            fonts.append(UFOReader(filename))
        else:
            from fontTools.ttLib import TTFont

            fonts.append(TTFont(filename))

        names.append(basename(filename).rsplit(".", 1)[0])

    glyphsets = []
    for font in fonts:
        if hasattr(font, "getGlyphSet"):
            glyphset = font.getGlyphSet()
        else:
            glyphset = font
        glyphsets.append({k: glyphset[k] for k in glyphset.keys()})

    if len(glyphsets) == 1:
        return None

    if not glyphs:
        glyphs = sorted(set([gn for glyphset in glyphsets for gn in glyphset.keys()]))

    glyphsSet = set(glyphs)
    for glyphset in glyphsets:
        glyphSetGlyphNames = set(glyphset.keys())
        diff = glyphsSet - glyphSetGlyphNames
        if diff:
            for gn in diff:
                glyphset[gn] = None

    # Normalize locations
    locations = [normalizeLocation(loc, axis_triples) for loc in locations]

    log.info("Running on %d glyphsets", len(glyphsets))
    log.info("Locations: %s", pformat(locations))
    problems_gen = test_gen(
        glyphsets,
        glyphs=glyphs,
        names=names,
        locations=locations,
        ignore_missing=args.ignore_missing,
        tolerance=args.tolerance or 0.95,
    )
    problems = defaultdict(list)

    f = sys.stdout if args.output is None else open(args.output, "w")

    if not args.quiet:
        if args.json:
            import json

            for glyphname, problem in problems_gen:
                problems[glyphname].append(problem)

            print(json.dumps(problems), file=f)
        else:
            last_glyphname = None
            for glyphname, p in problems_gen:
                problems[glyphname].append(p)

                if glyphname != last_glyphname:
                    print(f"Glyph {glyphname} was not compatible:", file=f)
                    last_glyphname = glyphname
                    last_masters = None

                masters = (
                    (p["master"]) if "master" in p else (p["master_1"], p["master_2"])
                )
                if masters != last_masters:
                    print(f"  Masters: %s:" % ", ".join(masters), file=f)
                    last_masters = masters

                if p["type"] == "missing":
                    print("    Glyph was missing in master %s" % p["master"], file=f)
                if p["type"] == "open_path":
                    print(
                        "    Glyph has an open path in master %s" % p["master"], file=f
                    )
                if p["type"] == "path_count":
                    print(
                        "    Path count differs: %i in %s, %i in %s"
                        % (p["value_1"], p["master_1"], p["value_2"], p["master_2"]),
                        file=f,
                    )
                if p["type"] == "node_count":
                    print(
                        "    Node count differs in path %i: %i in %s, %i in %s"
                        % (
                            p["path"],
                            p["value_1"],
                            p["master_1"],
                            p["value_2"],
                            p["master_2"],
                        ),
                        file=f,
                    )
                if p["type"] == "node_incompatibility":
                    print(
                        "    Node %o incompatible in path %i: %s in %s, %s in %s"
                        % (
                            p["node"],
                            p["path"],
                            p["value_1"],
                            p["master_1"],
                            p["value_2"],
                            p["master_2"],
                        ),
                        file=f,
                    )
                if p["type"] == "contour_order":
                    print(
                        "    Contour order differs: %s in %s, %s in %s"
                        % (
                            p["value_1"],
                            p["master_1"],
                            p["value_2"],
                            p["master_2"],
                        ),
                        file=f,
                    )
                if p["type"] == "wrong_start_point":
                    print(
                        "    Contour %d start point differs: %s in %s, %s in %s; reversed: %s"
                        % (
                            p["contour"],
                            p["value_1"],
                            p["master_1"],
                            p["value_2"],
                            p["master_2"],
                            p["reversed"],
                        ),
                        file=f,
                    )
    else:
        for glyphname, problem in problems_gen:
            problems[glyphname].append(problem)

    if args.pdf:
        log.info("Writing PDF to %s", args.pdf)
        from .interpolatablePlot import InterpolatablePDF

        with InterpolatablePDF(args.pdf, glyphsets=glyphsets, names=names) as pdf:
            pdf.add_problems(problems)
            if not problems and not args.quiet:
                pdf.draw_cupcake()

    if args.html:
        log.info("Writing HTML to %s", args.html)
        from .interpolatablePlot import InterpolatableSVG

        svgs = []
        with InterpolatableSVG(svgs, glyphsets=glyphsets, names=names) as svg:
            svg.add_problems(problems)
            if not problems and not args.quiet:
                svg.draw_cupcake()

        import base64

        with open(args.html, "wb") as f:
            f.write(b"<!DOCTYPE html>\n")
            f.write(b"<html><body align=center>\n")
            for svg in svgs:
                f.write("<img src='data:image/svg+xml;base64,".encode("utf-8"))
                f.write(base64.b64encode(svg))
                f.write(b"' />\n")
                f.write(b"<hr>\n")
            f.write(b"</body></html>\n")

    if problems:
        return problems


if __name__ == "__main__":
    import sys

    problems = main()
    sys.exit(int(bool(problems)))