xref: /aosp_15_r20/external/fonttools/Lib/fontTools/cu2qu/errors.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1# Copyright 2016 Google Inc. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16class Error(Exception):
17    """Base Cu2Qu exception class for all other errors."""
18
19
20class ApproxNotFoundError(Error):
21    def __init__(self, curve):
22        message = "no approximation found: %s" % curve
23        super().__init__(message)
24        self.curve = curve
25
26
27class UnequalZipLengthsError(Error):
28    pass
29
30
31class IncompatibleGlyphsError(Error):
32    def __init__(self, glyphs):
33        assert len(glyphs) > 1
34        self.glyphs = glyphs
35        names = set(repr(g.name) for g in glyphs)
36        if len(names) > 1:
37            self.combined_name = "{%s}" % ", ".join(sorted(names))
38        else:
39            self.combined_name = names.pop()
40
41    def __repr__(self):
42        return "<%s %s>" % (type(self).__name__, self.combined_name)
43
44
45class IncompatibleSegmentNumberError(IncompatibleGlyphsError):
46    def __str__(self):
47        return "Glyphs named %s have different number of segments" % (
48            self.combined_name
49        )
50
51
52class IncompatibleSegmentTypesError(IncompatibleGlyphsError):
53    def __init__(self, glyphs, segments):
54        IncompatibleGlyphsError.__init__(self, glyphs)
55        self.segments = segments
56
57    def __str__(self):
58        lines = []
59        ndigits = len(str(max(self.segments)))
60        for i, tags in sorted(self.segments.items()):
61            lines.append(
62                "%s: (%s)" % (str(i).rjust(ndigits), ", ".join(repr(t) for t in tags))
63            )
64        return "Glyphs named %s have incompatible segment types:\n  %s" % (
65            self.combined_name,
66            "\n  ".join(lines),
67        )
68
69
70class IncompatibleFontsError(Error):
71    def __init__(self, glyph_errors):
72        self.glyph_errors = glyph_errors
73
74    def __str__(self):
75        return "fonts contains incompatible glyphs: %s" % (
76            ", ".join(repr(g) for g in sorted(self.glyph_errors.keys()))
77        )
78