errors.py 2.4 KB

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