source: libcaca/trunk/python/caca/common.py @ 4774

Last change on this file since 4774 was 4774, checked in by Alex Foulon, 10 years ago
  • Fixed utf32_to_utf8 function.
  • Property svn:keywords set to Id
File size: 8.2 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
18
19from caca import _lib
20
21#color constants
22COLOR_BLACK = 0x00
23COLOR_BLUE = 0x01
24COLOR_GREEN = 0x02
25COLOR_CYAN = 0x03
26COLOR_RED = 0x04
27COLOR_MAGENTA = 0x05
28COLOR_BROWN = 0x06
29COLOR_LIGHTGRAY = 0x07
30COLOR_DARKGRAY = 0x08
31COLOR_LIGHTBLUE = 0x09
32COLOR_LIGHTGREEN = 0x0a
33COLOR_LIGHTCYAN = 0x0b
34COLOR_LIGHTRED = 0x0c
35COLOR_LIGHTMAGENTA = 0x0d
36COLOR_YELLOW = 0x0e
37COLOR_WHITE = 0x0f
38COLOR_DEFAULT = 0x10
39COLOR_TRANSPARENT = 0x20
40
41#styles constants
42STYLE_BOLD = 0x01
43STYLE_ITALICS = 0x02
44STYLE_UNDERLINE = 0x04
45STYLE_BLINK = 0x08
46
47#key constants
48EVENT_NONE = 0x0000
49EVENT_KEY_PRESS = 0x0001
50EVENT_KEY_RELEASE = 0x0002
51EVENT_MOUSE_PRESS = 0x0004
52EVENT_MOUSE_RELEASE = 0x0008
53EVENT_MOUSE_MOTION = 0x0010
54EVENT_RESIZE = 0x0020
55EVENT_QUIT = 0x0040
56EVENT_ANY = 0xffff
57
58#event constants
59KEY_UNKNOWN = 0x00
60KEY_CTRL_A = 0x01
61KEY_CTRL_B = 0x02
62KEY_CTRL_C = 0x03
63KEY_CTRL_D = 0x04
64KEY_CTRL_E = 0x05
65KEY_CTRL_F = 0x06
66KEY_CTRL_G = 0x07
67KEY_BACKSPACE = 0x08
68KEY_TAB = 0x09
69KEY_CTRL_J = 0x0a
70KEY_CTRL_K = 0x0b
71KEY_CTRL_L = 0x0c
72KEY_RETURN = 0x0d
73KEY_CTRL_N = 0x0e
74KEY_CTRL_O = 0x0f
75KEY_CTRL_P = 0x10
76KEY_CTRL_Q = 0x11
77KEY_CTRL_R = 0x12
78KEY_PAUSE = 0x13
79KEY_CTRL_T = 0x14
80KEY_CTRL_U = 0x15
81KEY_CTRL_V = 0x16
82KEY_CTRL_W = 0x17
83KEY_CTRL_X = 0x18
84KEY_CTRL_Y = 0x19
85KEY_CTRL_Z = 0x1a
86KEY_ESCAPE = 0x1b
87KEY_DELETE = 0x7f
88KEY_UP = 0x111
89KEY_DOWN = 0x112
90KEY_LEFT = 0x113
91KEY_RIGHT = 0x114
92KEY_INSERT = 0x115
93KEY_HOME = 0x116
94KEY_END = 0x117
95KEY_PAGEUP = 0x118
96KEY_PAGEDOWN = 0x119
97KEY_F1 = 0x11a
98KEY_F2 = 0x11b
99KEY_F3 = 0x11c
100KEY_F4 = 0x11d
101KEY_F5 = 0x11e
102KEY_F6 = 0x11f
103KEY_F7 = 0x120
104KEY_F8 = 0x121
105KEY_F9 = 0x122
106KEY_F10 = 0x123
107KEY_F11 = 0x124
108KEY_F12 = 0x125
109KEY_F13 = 0x126
110KEY_F14 = 0x127
111KEY_F15 = 0x128
112
113
114def 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
121def 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
143def 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
165def 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
196def 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
212def 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
223def 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
233def 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
243def 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
253def 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
263def 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
273def 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,
279            ctypes.POINTER(ctypes.c_size_t)
280        ]
281    _lib.caca_utf8_to_utf32.restype  = ctypes.c_uint32
282
283    return _lib.caca_utf8_to_utf32(ch, ctypes.c_ulong(0))
284
285def utf32_to_utf8(ch):
286    """ Convert a UTF-32 character to UTF-8.
287
288        ch  -- the character to convert
289    """
290    _lib.caca_utf32_to_utf8.argtypes = [ctypes.c_char_p, ctypes.c_uint32]
291    _lib.caca_utf32_to_utf8.restype  = ctypes.c_int
292
293    buf = ctypes.c_buffer(7)
294    _lib.caca_utf32_to_utf8(buf, ch)
295
296    return buf.raw.rstrip('\x00')
297
298def utf32_to_cp437(ch):
299    """ Convert a UTF-32 character to CP437.
300
301        ch  -- the character to convert
302    """
303    _lib.caca_utf32_to_cp437.argtypes = [ctypes.c_uint32]
304    _lib.caca_utf32_to_cp437.restype  = ctypes.c_uint8
305
306    return _lib.caca_utf32_to_cp437(ch)
307
308def cp437_to_utf32(ch):
309    """ Convert a CP437 character to UTF-32.
310
311        ch  -- the character to convert
312    """
313    _lib.caca_cp437_to_utf8.argtypes = [ctypes.c_uint8]
314    _lib.caca_cp437_to_utf8.restype  = ctypes.c_uint32
315
316    return _lib.caca_cp437_to_utf8(ch)
317
318def utf32_to_ascii(ch):
319    """ Convert a UTF-32 character to ASCII.
320
321        ch  -- the character to convert
322    """
323    _lib.caca_utf32_to_ascii.argtypes = [ctypes.c_uint32]
324    _lib.caca_utf32_to_ascii.restype  = ctypes.c_uint8
325
326    return _lib.caca_utf32_to_ascii(ch)
327
328def utf32_is_fullwidth(ch):
329    """ Tell whether a UTF-32 character is fullwidth.
330
331        ch  -- the UTF-32 character
332    """
333    _lib.caca_utf32_is_fullwidth.argtypes = [ctypes.c_uint32]
334    _lib.caca_utf32_is_fullwidth.restype  = ctypes.c_int
335
336    return _lib.caca_utf32_is_fullwidth(ch)
337
Note: See TracBrowser for help on using the repository browser.