source: libcaca/trunk/python/caca/font.py @ 4802

Last change on this file since 4802 was 4802, checked in by alxf, 3 years ago

Add support for python3 to python bindings.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
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
15""" Libcaca Python bindings """
16
17import ctypes
18import errno
19
20from caca import _lib, _PYTHON3, _str_to_bytes
21
22class _Font(object):
23    """ Model for Font object.
24    """
25    def __init__(self):
26        self._font = 0
27
28    def from_param(self):
29        """ Required by ctypes module to call object as parameter of
30            a C function.
31        """
32        return self._font
33
34    def __del__(self):
35        if hasattr(self, "_font"):
36            if self._font > 0:
37                self._free()
38
39    def __str__(self):
40        return "<CacaFont>"
41
42    def _free(self):
43        """ Free a libcaca font.
44        """
45        _lib.caca_free_font.argtypes = [_Font]
46        _lib.caca_free_font.restype = ctypes.c_int
47
48        return _lib.caca_free_font(self)
49
50class Font(_Font):
51    """ Font object, methods are libcaca functions with caca_font_t as first
52        argument.
53    """
54    def __init__(self, font, size=0):
55        """ Font constructor
56
57            font    -- the memory area containing the font or its name
58            size    -- the size of the memory area, or 0 if the font name is given
59        """
60        if size == 0:
61            _lib.caca_load_font.argtypes = [ctypes.c_char_p, ctypes.c_int]
62        else:
63            raise FontError("Unsupported method")
64
65        _lib.caca_load_font.restype = ctypes.c_int
66
67        if _PYTHON3:
68            font = _str_to_bytes(font)
69
70        self._font = _lib.caca_load_font(font, size)
71        if self._font == 0:
72            err = ctypes.c_int.in_dll(_lib, "errno")
73            if err.value == errno.ENOENT:
74                raise FontError("Requested built-in font does not exist")
75            elif err.value == errno.EINVAL:
76                raise FontError("Invalid font data in memory area")
77            elif err.value == errno.ENOMEM:
78                raise FontError("Not enough memory to allocate font structure")
79
80    def get_width(self):
81        """ Get a font's standard glyph width.
82        """
83        _lib.caca_get_font_width.argtypes = [_Font]
84        _lib.caca_get_font_width.restype  = ctypes.c_int
85
86        return _lib.caca_get_font_width(self)
87
88    def get_height(self):
89        """ Get a font's standard glyph height.
90        """
91        _lib.caca_get_font_height.argtypes = [_Font]
92        _lib.caca_get_font_height.restype  = ctypes.c_int
93
94        return _lib.caca_get_font_height(self)
95
96    def get_blocks(self):
97        """ Get a font's list of supported glyphs.
98        """
99        raise FontError("Not Implemented")
100
101class FontError(Exception):
102    pass
103
Note: See TracBrowser for help on using the repository browser.