Changeset 4770 for libcaca


Ignore:
Timestamp:
Feb 13, 2011, 8:07:20 PM (8 years ago)
Author:
Alex Foulon
Message:
  • Bind font functions.
  • Bind caca_render_canvas function.
  • Add example font.
Location:
libcaca/trunk/python
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/python/caca/canvas.py

    r4710 r4770  
    1818
    1919from caca import _lib
     20from caca.font import _Font
    2021
    2122class _Canvas(object):
     
    980981        return _lib.caca_flush_figlet(self)
    981982
     983    def render(self, font, buf, width, height, pitch):
     984        """ Render the canvas onto an image buffer.
     985
     986            font    -- a Font() object
     987            buf     -- the image buffer
     988            width   -- the width (in pixels) of the image
     989            heigth  -- the height (in pixels) of the image
     990            pitch   -- the pitch (in bytes) of the image
     991        """
     992        _lib.caca_render_canvas.argtypes = [
     993            _Canvas, _Font, ctypes.c_char_p,
     994            ctypes.c_int, ctypes.c_int, ctypes.c_int
     995        ]
     996        _lib.caca_render_canvas.restype  = ctypes.c_int
     997
     998        return _lib.caca_render_canvas(self, font, buf, width, height, pitch)
     999
    9821000class NullCanvas(_Canvas):
    9831001    """ Represent a NULL canvas_t, eg to use as canvas mask for blit operations.
  • libcaca/trunk/python/caca/font.py

    r4398 r4770  
    1616
    1717import ctypes
     18import errno
    1819
    1920from caca import _lib
     
    3233
    3334    def __del__(self):
    34         if self._font > 0:
    35             self._free()
     35        if hasattr(self, "_font"):
     36            if self._font > 0:
     37                self._free()
    3638
    3739    def __str__(self):
     
    5658            size    -- the size of the memory area, or 0 if the font name is given
    5759        """
    58         pass
     60        if size == 0:
     61            _lib.caca_load_font.argtypes = [ctypes.c_char_p, ctypes.c_int]
     62        else:
     63            raise FontError("Unsupported method")
    5964
     65        _lib.caca_load_font.restype = ctypes.c_int
     66
     67        self._font = _lib.caca_load_font(font, size)
     68        if self._font == 0:
     69            err = ctypes.c_int.in_dll(_lib, "errno")
     70            if err.value == errno.ENOENT:
     71                raise FontError("Requested built-in font does not exist")
     72            elif err.value == errno.EINVAL:
     73                raise FontError("Invalid font data in memory area")
     74            elif err.value == errno.ENOMEM:
     75                raise FontError("Not enough memory to allocate font structure")
     76
     77    def get_width(self):
     78        """ Get a font's standard glyph width.
     79        """
     80        _lib.caca_get_font_width.argtypes = [_Font]
     81        _lib.caca_get_font_width.restype  = ctypes.c_int
     82
     83        return _lib.caca_get_font_width(self)
     84
     85    def get_height(self):
     86        """ Get a font's standard glyph height.
     87        """
     88        _lib.caca_get_font_height.argtypes = [_Font]
     89        _lib.caca_get_font_height.restype  = ctypes.c_int
     90
     91        return _lib.caca_get_font_height(self)
     92
     93    def get_blocks(self):
     94        """ Get a font's list of supported glyphs.
     95        """
     96        raise FontError("Not Implemented")
     97
     98class FontError(Exception):
     99    pass
     100
Note: See TracChangeset for help on using the changeset viewer.