No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

108 líneas
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 _FontStruct(ctypes.Structure):
  19. pass
  20. class _Font(object):
  21. """ Model for Font object.
  22. """
  23. def __init__(self):
  24. self._font = None
  25. def from_param(self):
  26. """ Required by ctypes module to call object as parameter of
  27. a C function.
  28. """
  29. return self._font
  30. def __del__(self):
  31. if hasattr(self, "_font"):
  32. if self._font:
  33. self._free()
  34. def __str__(self):
  35. return "<CacaFont>"
  36. def _free(self):
  37. """ Free a libcaca font.
  38. """
  39. _lib.caca_free_font.argtypes = [_Font]
  40. _lib.caca_free_font.restype = ctypes.c_int
  41. return _lib.caca_free_font(self)
  42. class Font(_Font):
  43. """ Font object, methods are libcaca functions with caca_font_t as first
  44. argument.
  45. """
  46. def __init__(self, font, size=0):
  47. """ Font constructor
  48. font -- the memory area containing the font or its name
  49. size -- the size of the memory area, or 0 if the font name is given
  50. """
  51. if size == 0:
  52. _lib.caca_load_font.argtypes = [ctypes.c_char_p, ctypes.c_int]
  53. else:
  54. raise FontError("Unsupported method")
  55. _lib.caca_load_font.restype = ctypes.POINTER(_FontStruct)
  56. if _PYTHON3:
  57. font = _str_to_bytes(font)
  58. self._font = _lib.caca_load_font(font, size)
  59. if self._font == 0:
  60. err = ctypes.c_int.in_dll(_lib, "errno")
  61. if err.value == errno.ENOENT:
  62. raise FontError("Requested built-in font does not exist")
  63. elif err.value == errno.EINVAL:
  64. raise FontError("Invalid font data in memory area")
  65. elif err.value == errno.ENOMEM:
  66. raise FontError("Not enough memory to allocate font structure")
  67. def get_width(self):
  68. """ Get a font's standard glyph width.
  69. """
  70. _lib.caca_get_font_width.argtypes = [_Font]
  71. _lib.caca_get_font_width.restype = ctypes.c_int
  72. return _lib.caca_get_font_width(self)
  73. def get_height(self):
  74. """ Get a font's standard glyph height.
  75. """
  76. _lib.caca_get_font_height.argtypes = [_Font]
  77. _lib.caca_get_font_height.restype = ctypes.c_int
  78. return _lib.caca_get_font_height(self)
  79. def get_blocks(self):
  80. """ Get a font's list of supported glyphs.
  81. """
  82. raise FontError("Not Implemented")
  83. class FontError(Exception):
  84. pass