Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

104 рядки
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # libcaca Colour ASCII-Art library
  4. # Python language bindings
  5. # Copyright (c) 2010 Alex Foulon <alxf@lavabit.com>
  6. # All Rights Reserved
  7. #
  8. # This library is free software. It comes without any warranty, to
  9. # the extent permitted by applicable law. You can redistribute it
  10. # and/or modify it under the terms of the Do What the Fuck You Want
  11. # to Public License, Version 2, as published by Sam Hocevar. See
  12. # http://www.wtfpl.net/ for more details.
  13. #
  14. """ Libcaca Python bindings """
  15. import ctypes
  16. import errno
  17. from caca import _lib, _PYTHON3, _str_to_bytes
  18. class _Font(object):
  19. """ Model for Font object.
  20. """
  21. def __init__(self):
  22. self._font = 0
  23. def from_param(self):
  24. """ Required by ctypes module to call object as parameter of
  25. a C function.
  26. """
  27. return self._font
  28. def __del__(self):
  29. if hasattr(self, "_font"):
  30. if self._font > 0:
  31. self._free()
  32. def __str__(self):
  33. return "<CacaFont>"
  34. def _free(self):
  35. """ Free a libcaca font.
  36. """
  37. _lib.caca_free_font.argtypes = [_Font]
  38. _lib.caca_free_font.restype = ctypes.c_int
  39. return _lib.caca_free_font(self)
  40. class Font(_Font):
  41. """ Font object, methods are libcaca functions with caca_font_t as first
  42. argument.
  43. """
  44. def __init__(self, font, size=0):
  45. """ Font constructor
  46. font -- the memory area containing the font or its name
  47. size -- the size of the memory area, or 0 if the font name is given
  48. """
  49. if size == 0:
  50. _lib.caca_load_font.argtypes = [ctypes.c_char_p, ctypes.c_int]
  51. else:
  52. raise FontError("Unsupported method")
  53. _lib.caca_load_font.restype = ctypes.c_int
  54. if _PYTHON3:
  55. font = _str_to_bytes(font)
  56. self._font = _lib.caca_load_font(font, size)
  57. if self._font == 0:
  58. err = ctypes.c_int.in_dll(_lib, "errno")
  59. if err.value == errno.ENOENT:
  60. raise FontError("Requested built-in font does not exist")
  61. elif err.value == errno.EINVAL:
  62. raise FontError("Invalid font data in memory area")
  63. elif err.value == errno.ENOMEM:
  64. raise FontError("Not enough memory to allocate font structure")
  65. def get_width(self):
  66. """ Get a font's standard glyph width.
  67. """
  68. _lib.caca_get_font_width.argtypes = [_Font]
  69. _lib.caca_get_font_width.restype = ctypes.c_int
  70. return _lib.caca_get_font_width(self)
  71. def get_height(self):
  72. """ Get a font's standard glyph height.
  73. """
  74. _lib.caca_get_font_height.argtypes = [_Font]
  75. _lib.caca_get_font_height.restype = ctypes.c_int
  76. return _lib.caca_get_font_height(self)
  77. def get_blocks(self):
  78. """ Get a font's list of supported glyphs.
  79. """
  80. raise FontError("Not Implemented")
  81. class FontError(Exception):
  82. pass