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 | |
---|
17 | import ctypes |
---|
18 | |
---|
19 | from caca import _lib |
---|
20 | |
---|
21 | #color constants |
---|
22 | COLOR_BLACK = 0x00 |
---|
23 | COLOR_BLUE = 0x01 |
---|
24 | COLOR_GREEN = 0x02 |
---|
25 | COLOR_CYAN = 0x03 |
---|
26 | COLOR_RED = 0x04 |
---|
27 | COLOR_MAGENTA = 0x05 |
---|
28 | COLOR_BROWN = 0x06 |
---|
29 | COLOR_LIGHTGRAY = 0x07 |
---|
30 | COLOR_DARKGRAY = 0x08 |
---|
31 | COLOR_LIGHTBLUE = 0x09 |
---|
32 | COLOR_LIGHTGREEN = 0x0a |
---|
33 | COLOR_LIGHTCYAN = 0x0b |
---|
34 | COLOR_LIGHTRED = 0x0c |
---|
35 | COLOR_LIGHTMAGENTA = 0x0d |
---|
36 | COLOR_YELLOW = 0x0e |
---|
37 | COLOR_WHITE = 0x0f |
---|
38 | COLOR_DEFAULT = 0x10 |
---|
39 | COLOR_TRANSPARENT = 0x20 |
---|
40 | |
---|
41 | #styles constants |
---|
42 | STYLE_BOLD = 0x01 |
---|
43 | STYLE_ITALICS = 0x02 |
---|
44 | STYLE_UNDERLINE = 0x04 |
---|
45 | STYLE_BLINK = 0x08 |
---|
46 | |
---|
47 | #key constants |
---|
48 | EVENT_NONE = 0x0000 |
---|
49 | EVENT_KEY_PRESS = 0x0001 |
---|
50 | EVENT_KEY_RELEASE = 0x0002 |
---|
51 | EVENT_MOUSE_PRESS = 0x0004 |
---|
52 | EVENT_MOUSE_RELEASE = 0x0008 |
---|
53 | EVENT_MOUSE_MOTION = 0x0010 |
---|
54 | EVENT_RESIZE = 0x0020 |
---|
55 | EVENT_QUIT = 0x0040 |
---|
56 | EVENT_ANY = 0xffff |
---|
57 | |
---|
58 | #event constants |
---|
59 | KEY_UNKNOWN = 0x00 |
---|
60 | KEY_CTRL_A = 0x01 |
---|
61 | KEY_CTRL_B = 0x02 |
---|
62 | KEY_CTRL_C = 0x03 |
---|
63 | KEY_CTRL_D = 0x04 |
---|
64 | KEY_CTRL_E = 0x05 |
---|
65 | KEY_CTRL_F = 0x06 |
---|
66 | KEY_CTRL_G = 0x07 |
---|
67 | KEY_BACKSPACE = 0x08 |
---|
68 | KEY_TAB = 0x09 |
---|
69 | KEY_CTRL_J = 0x0a |
---|
70 | KEY_CTRL_K = 0x0b |
---|
71 | KEY_CTRL_L = 0x0c |
---|
72 | KEY_RETURN = 0x0d |
---|
73 | KEY_CTRL_N = 0x0e |
---|
74 | KEY_CTRL_O = 0x0f |
---|
75 | KEY_CTRL_P = 0x10 |
---|
76 | KEY_CTRL_Q = 0x11 |
---|
77 | KEY_CTRL_R = 0x12 |
---|
78 | KEY_PAUSE = 0x13 |
---|
79 | KEY_CTRL_T = 0x14 |
---|
80 | KEY_CTRL_U = 0x15 |
---|
81 | KEY_CTRL_V = 0x16 |
---|
82 | KEY_CTRL_W = 0x17 |
---|
83 | KEY_CTRL_X = 0x18 |
---|
84 | KEY_CTRL_Y = 0x19 |
---|
85 | KEY_CTRL_Z = 0x1a |
---|
86 | KEY_ESCAPE = 0x1b |
---|
87 | KEY_DELETE = 0x7f |
---|
88 | KEY_UP = 0x111 |
---|
89 | KEY_DOWN = 0x112 |
---|
90 | KEY_LEFT = 0x113 |
---|
91 | KEY_RIGHT = 0x114 |
---|
92 | KEY_INSERT = 0x115 |
---|
93 | KEY_HOME = 0x116 |
---|
94 | KEY_END = 0x117 |
---|
95 | KEY_PAGEUP = 0x118 |
---|
96 | KEY_PAGEDOWN = 0x119 |
---|
97 | KEY_F1 = 0x11a |
---|
98 | KEY_F2 = 0x11b |
---|
99 | KEY_F3 = 0x11c |
---|
100 | KEY_F4 = 0x11d |
---|
101 | KEY_F5 = 0x11e |
---|
102 | KEY_F6 = 0x11f |
---|
103 | KEY_F7 = 0x120 |
---|
104 | KEY_F8 = 0x121 |
---|
105 | KEY_F9 = 0x122 |
---|
106 | KEY_F10 = 0x123 |
---|
107 | KEY_F11 = 0x124 |
---|
108 | KEY_F12 = 0x125 |
---|
109 | KEY_F13 = 0x126 |
---|
110 | KEY_F14 = 0x127 |
---|
111 | KEY_F15 = 0x128 |
---|
112 | |
---|
113 | |
---|
114 | def get_version(): |
---|
115 | """ Return string with libcaca version information. |
---|
116 | """ |
---|
117 | _lib.caca_get_version.restype = ctypes.c_char_p |
---|
118 | |
---|
119 | return _lib.caca_get_version() |
---|
120 | |
---|
121 | def get_display_driver_list(): |
---|
122 | """ Return a list of available drivers as tuple (name, description). |
---|
123 | """ |
---|
124 | tmplst = [] |
---|
125 | retlst = [] |
---|
126 | |
---|
127 | _lib.caca_get_display_driver_list.restype = ctypes.POINTER(ctypes.c_char_p) |
---|
128 | |
---|
129 | for item in _lib.caca_get_display_driver_list(): |
---|
130 | if item is not None and item != "": |
---|
131 | tmplst.append(item) |
---|
132 | else: |
---|
133 | #memory error occured otherwise |
---|
134 | break |
---|
135 | |
---|
136 | for i in xrange(0, len(tmplst)): |
---|
137 | if i % 2 == 0: |
---|
138 | retlst.append((tmplst[i], tmplst[i+1])) |
---|
139 | |
---|
140 | del tmplst |
---|
141 | return retlst |
---|
142 | |
---|
143 | def get_export_list(): |
---|
144 | """ Return list of available export formats as tuple (name, description). |
---|
145 | """ |
---|
146 | tmplst = [] |
---|
147 | retlst = [] |
---|
148 | |
---|
149 | _lib.caca_get_export_list.restype = ctypes.POINTER(ctypes.c_char_p) |
---|
150 | |
---|
151 | for item in _lib.caca_get_export_list(): |
---|
152 | if item is not None and item != "": |
---|
153 | tmplst.append(item) |
---|
154 | else: |
---|
155 | #memory error occured otherwise |
---|
156 | break |
---|
157 | |
---|
158 | for i in xrange(0, len(tmplst)): |
---|
159 | if i % 2 == 0: |
---|
160 | retlst.append((tmplst[i], tmplst[i+1])) |
---|
161 | |
---|
162 | del tmplst |
---|
163 | return retlst |
---|
164 | |
---|
165 | def get_import_list(): |
---|
166 | """ Return list of available import formats as tuple (name, description). |
---|
167 | """ |
---|
168 | tmplst = [] |
---|
169 | retlst = [] |
---|
170 | |
---|
171 | _lib.caca_get_import_list.restype = ctypes.POINTER(ctypes.c_char_p) |
---|
172 | |
---|
173 | autodetect = False |
---|
174 | for item in _lib.caca_get_import_list(): |
---|
175 | if item is not None: |
---|
176 | if item == "": |
---|
177 | if not autodetect: |
---|
178 | tmplst.append("\"\"") |
---|
179 | autodetect = True |
---|
180 | else: |
---|
181 | #memory error occured otherwise |
---|
182 | break |
---|
183 | else: |
---|
184 | tmplst.append(item) |
---|
185 | else: |
---|
186 | #memory error occured otherwise |
---|
187 | break |
---|
188 | |
---|
189 | for i in xrange(0, len(tmplst)): |
---|
190 | if i % 2 == 0: |
---|
191 | retlst.append((tmplst[i], tmplst[i+1])) |
---|
192 | |
---|
193 | del tmplst |
---|
194 | return retlst |
---|
195 | |
---|
196 | def get_font_list(): |
---|
197 | """ Return a list of available fonts. |
---|
198 | """ |
---|
199 | fl = [] |
---|
200 | |
---|
201 | _lib.caca_get_font_list.restype = ctypes.POINTER(ctypes.c_char_p) |
---|
202 | |
---|
203 | for item in _lib.caca_get_font_list(): |
---|
204 | if item is not None and item != "": |
---|
205 | fl.append(item) |
---|
206 | else: |
---|
207 | #memory error occured otherwise |
---|
208 | break |
---|
209 | |
---|
210 | return fl |
---|
211 | |
---|
212 | def rand(range_min, range_max): |
---|
213 | """ Generate a random integer within a range. |
---|
214 | |
---|
215 | range_min -- the lower bound of the integer range |
---|
216 | range_max __ the upper bound of the integer range |
---|
217 | """ |
---|
218 | _lib.caca_rand.argtypes = [ctypes.c_int, ctypes.c_int] |
---|
219 | _lib.caca_rand.restype = ctypes.c_int |
---|
220 | |
---|
221 | return _lib.caca_rand(range_min, range_max) |
---|
222 | |
---|
223 | def attr_to_ansi(attr): |
---|
224 | """ Get DOS ANSI information from attribute. |
---|
225 | |
---|
226 | attr -- the requested attribute value |
---|
227 | """ |
---|
228 | _lib.caca_attr_to_ansi.argtypes = [ctypes.c_uint32] |
---|
229 | _lib.caca_attr_to_ansi.restype = ctypes.c_uint8 |
---|
230 | |
---|
231 | return _lib.caca_attr_to_ansi(attr) |
---|
232 | |
---|
233 | def attr_to_ansi_fg(attr): |
---|
234 | """ Get ANSI foreground information from attribute. |
---|
235 | |
---|
236 | attr -- the requested attribute value |
---|
237 | """ |
---|
238 | _lib.caca_attr_to_ansi_fg.argtypes = [ctypes.c_uint32] |
---|
239 | _lib.caca_attr_to_ansi_fg.restype = ctypes.c_uint8 |
---|
240 | |
---|
241 | return _lib.caca_attr_to_ansi_fg(attr) |
---|
242 | |
---|
243 | def attr_to_ansi_bg(attr): |
---|
244 | """ Get ANSI background information from attribute. |
---|
245 | |
---|
246 | attr -- the requested attribute value |
---|
247 | """ |
---|
248 | _lib.caca_attr_to_ansi_bg.argtypes = [ctypes.c_uint32] |
---|
249 | _lib.caca_attr_to_ansi_bg.restype = ctypes.c_uint8 |
---|
250 | |
---|
251 | return _lib.caca_attr_to_ansi_bg(attr) |
---|
252 | |
---|
253 | def attr_to_rgb12_fg(attr): |
---|
254 | """ Get 12-bit RGB foreground information from attribute. |
---|
255 | |
---|
256 | attr -- the requested attribute value |
---|
257 | """ |
---|
258 | _lib.caca_attr_to_rgb12_fg.argtypes = [ctypes.c_uint32] |
---|
259 | _lib.caca_attr_to_rgb12_fg.restype = ctypes.c_uint16 |
---|
260 | |
---|
261 | return _lib.caca_attr_to_rgb12_fg(attr) |
---|
262 | |
---|
263 | def attr_to_rgb12_bg(attr): |
---|
264 | """ Get 12-bit RGB background information from attribute. |
---|
265 | |
---|
266 | attr -- the requested attribute value |
---|
267 | """ |
---|
268 | _lib.caca_attr_to_rgb12_bg.argtypes = [ctypes.c_uint32] |
---|
269 | _lib.caca_attr_to_rgb12_bg.restype = ctypes.c_uint16 |
---|
270 | |
---|
271 | return _lib.caca_attr_to_rgb12_bg(attr) |
---|
272 | |
---|
273 | def utf8_to_utf32(ch): |
---|
274 | """ Convert a UTF-8 character to UTF-32. |
---|
275 | |
---|
276 | ch -- the character to convert |
---|
277 | """ |
---|
278 | _lib.caca_utf8_to_utf32.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_size_t)] |
---|
279 | _lib.caca_utf8_to_utf32.restype = ctypes.c_uint32 |
---|
280 | |
---|
281 | return _lib.caca_utf8_to_utf32(ch, ctypes.c_ulong(0)) |
---|
282 | |
---|
283 | def utf32_to_utf8(ch): |
---|
284 | """ Convert a UTF-32 character to UTF-8. |
---|
285 | |
---|
286 | ch -- the character to convert |
---|
287 | """ |
---|
288 | _lib.caca_utf32_to_utf8.argtypes = [ctypes.c_char_p, ctypes.c_uint32] |
---|
289 | _lib.caca_utf32_to_utf8.restype = ctypes.c_int |
---|
290 | |
---|
291 | buf = ctypes.c_buffer(2) |
---|
292 | _lib.caca_utf32_to_utf8(buf, ch) |
---|
293 | |
---|
294 | return buf |
---|
295 | |
---|
296 | def utf32_to_cp437(ch): |
---|
297 | """ Convert a UTF-32 character to CP437. |
---|
298 | |
---|
299 | ch -- the character to convert |
---|
300 | """ |
---|
301 | _lib.caca_utf32_to_cp437.argtypes = [ctypes.c_uint32] |
---|
302 | _lib.caca_utf32_to_cp437.restype = ctypes.c_uint8 |
---|
303 | |
---|
304 | return _lib.caca_utf32_to_cp437(ch) |
---|
305 | |
---|
306 | def cp437_to_utf32(ch): |
---|
307 | """ Convert a CP437 character to UTF-32. |
---|
308 | |
---|
309 | ch -- the character to convert |
---|
310 | """ |
---|
311 | _lib.caca_cp437_to_utf8.argtypes = [ctypes.c_uint8] |
---|
312 | _lib.caca_cp437_to_utf8.restype = ctypes.c_uint32 |
---|
313 | |
---|
314 | return _lib.caca_cp437_to_utf8(ch) |
---|
315 | |
---|
316 | def utf32_to_ascii(ch): |
---|
317 | """ Convert a UTF-32 character to ASCII. |
---|
318 | |
---|
319 | ch -- the character to convert |
---|
320 | """ |
---|
321 | _lib.caca_utf32_to_ascii.argtypes = [ctypes.c_uint32] |
---|
322 | _lib.caca_utf32_to_ascii.restype = ctypes.c_uint8 |
---|
323 | |
---|
324 | return _lib.caca_utf32_to_ascii(ch) |
---|
325 | |
---|
326 | def utf32_is_fullwidth(ch): |
---|
327 | """ Tell whether a UTF-32 character is fullwidth. |
---|
328 | |
---|
329 | ch -- the UTF-32 character |
---|
330 | """ |
---|
331 | _lib.caca_utf32_is_fullwidth.argtypes = [ctypes.c_uint32] |
---|
332 | _lib.caca_utf32_is_fullwidth.restype = ctypes.c_int |
---|
333 | |
---|
334 | return _lib.caca_utf32_is_fullwidth(ch) |
---|
335 | |
---|