Debian Codegraph @

python/caca/font.py

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