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
15class Error(Exception):
16    """Base Cu2Qu exception class for all other errors."""
17
18
19class ApproxNotFoundError(Error):
20    def __init__(self, curve):
21        message = "no approximation found: %s" % curve
22        super().__init__(message)
23        self.curve = curve
24
25
26class UnequalZipLengthsError(Error):
27    pass
28
29
30class IncompatibleGlyphsError(Error):
31    def __init__(self, glyphs):
32        assert len(glyphs) > 1
33        self.glyphs = glyphs
34        names = set(repr(g.name) for g in glyphs)
35        if len(names) > 1:
36            self.combined_name = "{%s}" % ", ".join(sorted(names))
37        else:
38            self.combined_name = names.pop()
39
40    def __repr__(self):
41        return "<%s %s>" % (type(self).__name__, self.combined_name)
42
43
44class IncompatibleSegmentNumberError(IncompatibleGlyphsError):
45    def __str__(self):
46        return "Glyphs named %s have different number of segments" % (
47            self.combined_name
48        )
49
50
51class IncompatibleSegmentTypesError(IncompatibleGlyphsError):
52    def __init__(self, glyphs, segments):
53        IncompatibleGlyphsError.__init__(self, glyphs)
54        self.segments = segments
55
56    def __str__(self):
57        lines = []
58        ndigits = len(str(max(self.segments)))
59        for i, tags in sorted(self.segments.items()):
60            lines.append(
61                "%s: (%s)" % (str(i).rjust(ndigits), ", ".join(repr(t) for t in tags))
62            )
63        return "Glyphs named %s have incompatible segment types:\n  %s" % (
64            self.combined_name,
65            "\n  ".join(lines),
66        )
67
68
69class IncompatibleFontsError(Error):
70    def __init__(self, glyph_errors):
71        self.glyph_errors = glyph_errors
72
73    def __str__(self):
74        return "fonts contains incompatible glyphs: %s" % (
75            ", ".join(repr(g) for g in sorted(self.glyph_errors.keys()))
76        )
77