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