You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

101 lines
2.9 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://sam.zoy.org/wtfpl/COPYING for more details.
  13. #
  14. """ Libcaca Python bindings """
  15. import ctypes
  16. import errno
  17. from caca import _lib
  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. self._font = _lib.caca_load_font(font, size)
  55. if self._font == 0:
  56. err = ctypes.c_int.in_dll(_lib, "errno")
  57. if err.value == errno.ENOENT:
  58. raise FontError("Requested built-in font does not exist")
  59. elif err.value == errno.EINVAL:
  60. raise FontError("Invalid font data in memory area")
  61. elif err.value == errno.ENOMEM:
  62. raise FontError("Not enough memory to allocate font structure")
  63. def get_width(self):
  64. """ Get a font's standard glyph width.
  65. """
  66. _lib.caca_get_font_width.argtypes = [_Font]
  67. _lib.caca_get_font_width.restype = ctypes.c_int
  68. return _lib.caca_get_font_width(self)
  69. def get_height(self):
  70. """ Get a font's standard glyph height.
  71. """
  72. _lib.caca_get_font_height.argtypes = [_Font]
  73. _lib.caca_get_font_height.restype = ctypes.c_int
  74. return _lib.caca_get_font_height(self)
  75. def get_blocks(self):
  76. """ Get a font's list of supported glyphs.
  77. """
  78. raise FontError("Not Implemented")
  79. class FontError(Exception):
  80. pass