source: libcaca/trunk/python/caca/canvas.py @ 4771

Last change on this file since 4771 was 4771, checked in by Alex Foulon, 9 years ago
  • method Canvas.put_char now handle utf8 char.
  • Property svn:keywords set to Id
File size: 34.8 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, utf8_to_utf32
20from caca.font import _Font
21
22class _Canvas(object):
23    """ Model for Canvas objects.
24    """
25
26    def __init__(self):
27        self._cv = 0
28
29    def from_param(self):
30        """ Required by ctypes module to call object as parameter of
31            a C function.
32        """
33        return self._cv
34
35    def __str__(self):
36        return "<CacaCanvas %dx%d>" % (self.get_width(), self.get_height())
37
38    def __del__(self):
39        if self._cv > 0:
40            self._free()
41
42    def _free(self):
43        """ Free a libcaca canvas.
44        """
45        _lib.caca_free_canvas.argtypes = [_Canvas]
46        _lib.caca_free_canvas.restype  = ctypes.c_int
47
48        return _lib.caca_free_canvas(self)
49
50    def get_width(self):
51        raise CanvasError, "You can't use model canvas directly"
52
53    def get_height(self):
54        raise CanvasError, "You can't use model canvas directly"
55
56class Canvas(_Canvas):
57    """ Canvas object, methods are libcaca functions with canvas_t as
58        first parameter.
59    """
60    def __init__(self, width=0, height=0, pointer=None):
61        """ Canvas constructor.
62
63            width   -- the desired canvas width
64            height  -- the desired canvas height
65            pointer -- pointer to libcaca canvas
66        """
67        _lib.caca_create_canvas.argtypes = [ctypes.c_int, ctypes.c_int]
68
69        if pointer is None:
70            self._cv = _lib.caca_create_canvas(width, height)
71            if self._cv == 0:
72                raise CanvasError, "Failed to create canvas"
73        else:
74            self._cv = pointer
75
76    def manage(self, *args, **kw):
77        """ Not implemented.
78        """
79        raise CanvasError, "Not implemented"
80
81    def unmanage(self, *args, **kw):
82        """ Not implemented.
83        """
84        raise CanvasError, "Not implemented"
85
86    def set_size(self, width, height):
87        """ Resize a canvas.
88
89            width   -- the desired canvas width
90            height  -- the desired canvas height
91        """
92        _lib.caca_set_canvas_size.argtypes  = [
93            _Canvas, ctypes.c_int, ctypes.c_int
94            ]
95        _lib.caca_set_canvas_size.restype   = ctypes.c_int
96
97        return _lib.caca_set_canvas_size(self, width, height)
98
99    def get_width(self):
100        """ Get the canvas width.
101        """
102        _lib.caca_get_canvas_width.argtypes = [_Canvas]
103        _lib.caca_get_canvas_width.restype  = ctypes.c_int
104
105        return _lib.caca_get_canvas_width(self)
106
107    def get_height(self):
108        """ Get the canvas height.
109        """
110        _lib.caca_get_canvas_height.argtypes = [_Canvas]
111        _lib.caca_get_canvas_height.restype  = ctypes.c_int
112
113        return _lib.caca_get_canvas_height(self)
114
115    def get_chars(self, *args, **kw):
116        """ Not implemented.
117        """
118        raise CanvasError, "Not implemented"
119
120    def get_attrs(self, *args, **kw):
121        """ Not implemented.
122        """
123        raise CanvasError, "Not implemented"
124
125    def gotoxy(self, x, y):
126        """ Set cursor position.
127
128            x   -- X cursor coordinate
129            y   -- Y cursor coordinate
130        """
131        _lib.caca_gotoxy.argtypes = [_Canvas, ctypes.c_int]
132        _lib.caca_gotoxy.restyoe  = ctypes.c_int
133
134        return _lib.caca_gotoxy(self, x, y)
135
136    def wherex(self):
137        """ Get X cursor position.
138        """
139        _lib.caca_wherex.argtypes = [_Canvas]
140        _lib.caca_wherex.restype  = ctypes.c_int
141
142        return _lib.caca_wherex(self)
143
144    def wherey(self):
145        """ Get Y cursor position.
146        """
147        _lib.caca_wherey.argtypes = [_Canvas]
148        _lib.caca_wherey.restype  = ctypes.c_int
149
150        return _lib.caca_wherey(self)
151
152    def put_char(self, x, y, ch):
153        """ Print an ASCII or Unicode character.
154
155            x   -- X coordinate
156            y   -- Y coordinate
157            ch  -- the character to print
158        """
159        _lib.caca_put_char.argtypes = [
160            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
161            ]
162        _lib.caca_put_char.restype  = ctypes.c_int
163
164        try:
165            ch = ord(ch)
166        except TypeError:
167            ch = utf8_to_utf32(ch)
168
169        return _lib.caca_put_char(self, x, y, ch)
170
171    def get_char(self, x, y):
172        """ Get the Unicode character at the given coordinates.
173
174            x   -- X coordinate
175            y   -- Y coordinate
176        """
177        _lib.caca_get_char.argtypes = [
178            _Canvas, ctypes.c_int, ctypes.c_int
179            ]
180        _lib.caca_get_char.restype  = ctypes.c_uint32
181
182        return _lib.caca_get_char(self, x, y)
183
184    def put_str(self, x, y, s):
185        """ Print a string.
186
187            x   -- X coordinate
188            y   -- Y coordinate
189            s   -- the string to print
190        """
191        _lib.caca_put_str.argtypes = [
192            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_char_p
193            ]
194        _lib.caca_put_str.restype  = ctypes.c_int
195
196        return _lib.caca_put_str(self, x, y, s)
197
198    def printf(self, x, y, fmt, *args):
199        """ Print a formated string.
200
201            x       -- X coordinate
202            y       -- Y coordinate
203            fmt     -- the format string to print
204            args    -- Arguments to the format string
205        """
206        _lib.caca_printf.argtypes = [
207            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_char_p
208            ]
209        _lib.caca_printf.restype  = ctypes.c_int
210
211        return _lib.caca_printf(self, x, y, fmt, *args)
212
213    def vprintf(self, *args, **kw):
214        """ Not implemented.
215        """
216        raise CanvasError, "Not implemented"
217
218    def clear(self):
219        """ Clear the canvas.
220        """
221        _lib.caca_clear_canvas.argtypes = [_Canvas]
222        _lib.caca_clear_canvas.restype  = ctypes.c_int
223
224        return _lib.caca_clear_canvas(self)
225
226    def set_handle(self, x, y):
227        """ Set cursor handle. Blitting method will use the handle value to
228            put the canvas at the proper coordinates.
229
230            x   -- X handle coordinate
231            y   -- Y handle coordinate
232        """
233        _lib.caca_set_canvas_handle.argtypes = [
234            _Canvas, ctypes.c_int, ctypes.c_int
235            ]
236        _lib.caca_set_canvas_handle.restype  = ctypes.c_int
237
238        return _lib.caca_set_canvas_handle(self, x, y)
239
240    def get_handle_x(self):
241        """ Get X handle position.
242        """
243        _lib.caca_get_canvas_handle_x.argtypes = [_Canvas]
244        _lib.caca_get_canvas_handle_x.restype  = ctypes.c_int
245
246        return _lib.caca_get_canvas_handle_x(self)
247
248    def get_handle_y(self):
249        """ Get Y handle position.
250        """
251        _lib.caca_get_canvas_handle_y.argtypes = [_Canvas]
252        _lib.caca_get_canvas_handle_y.restype  = ctypes.c_int
253
254        return _lib.caca_get_canvas_handle_y(self)
255
256    def blit(self, x, y, cv, mask):
257        """ Blit canvas onto another one.
258
259            x       -- X coordinate
260            y       -- Y coordinate
261            cv      -- the source canvas
262            mask    -- the mask canvas
263        """
264        _lib.caca_blit.argtypes = [
265            _Canvas, ctypes.c_int, ctypes.c_int, _Canvas, _Canvas
266            ]
267        _lib.caca_blit.restype  = ctypes.c_int
268
269        return _lib.caca_blit(self, x, y, cv, mask)
270
271    def set_boundaries(self, x, y, width, height):
272        """ Set a canvas' new boundaries.
273
274            x       -- X coordinate of the top-left corner
275            y       -- Y coordinate of the top-left corner
276            width   -- width of the box
277            height  -- height of the box
278        """
279        _lib.caca_set_canvas_boundaries.argtypes = [
280            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
281            ]
282        _lib.caca_set_canvas_boundaries.restype  = ctypes.c_int
283
284        return _lib.caca_set_canvas_boundaries(self, x, y, width, height)
285
286    def disable_dirty_rect(self):
287        """ Disable dirty rectangles.
288        """
289        _lib.caca_disable_dirty_rect.argtypes = [_Canvas]
290        _lib.caca_disable_dirty_rect.restype  = ctypes.c_int
291
292        return _lib.caca_disable_dirty_rect(self)
293
294    def enable_dirty_rect(self):
295        """ Enable dirty rectangles.
296        """
297        _lib.caca_enable_dirty_rect.argtypes = [_Canvas]
298        _lib.caca_enable_dirty_rect.restype  = ctypes.c_int
299
300        return _lib.caca_enable_dirty_rect(self)
301
302    def get_dirty_rect_count(self):
303        """ Get the number of dirty rectangles in the canvas.
304        """
305        _lib.caca_get_dirty_rect_count.argtypes = [_Canvas]
306        _lib.caca_get_dirty_rect_count.restype  = ctypes.c_int
307
308        return _lib.caca_get_dirty_rect_count(self)
309
310    def get_dirty_rect(self, idx):
311        """ Get a canvas's dirty rectangle. Return python dictionnary with
312            coords as keys: x, y, width, height.
313
314            idx -- the requested rectangle index
315        """
316        #initialize dictionnary and pointers
317        dct = None
318        x = ctypes.c_int()
319        y = ctypes.c_int()
320        width  = ctypes.c_int()
321        height = ctypes.c_int()
322
323        _lib.caca_get_dirty_rect.argtypes = [
324            _Canvas, ctypes.c_int,
325            ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int),
326            ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)
327            ]
328        _lib.caca_get_dirty_rect.restype  = ctypes.c_int
329
330        if _lib.caca_get_dirty_rect(self, idx, x, y, width, height) > -1:
331            dct = {
332                'x': x.value, 'y': y.value,
333                'width': width.value, 'height': height.value,
334            }
335
336        return dct
337
338    def add_dirty_rect(self, x, y, width, height):
339        """ Add an area to the canvas's dirty rectangle list.
340
341            x       -- the leftmost edge of the additional dirty rectangle
342            y       -- the topmost edge of the additional dirty rectangle
343            width   -- the width of the additional dirty rectangle
344            height  -- the height of the additional dirty rectangle
345        """
346        _lib.caca_add_dirty_rect.argtypes = [
347            _Canvas, ctypes.c_int, ctypes.c_int,
348            ctypes.c_int, ctypes.c_int
349        ]
350        _lib.caca_add_dirty_rect.restype  = ctypes.c_int
351
352        return _lib.caca_add_dirty_rect(self, x, y, width, height)
353
354    def remove_dirty_rect(self, x, y, width, height):
355        """ Remove an area from the dirty rectangle list.
356
357            x       -- the leftmost edge of the additional dirty rectangle
358            y       -- the topmost edge of the additional dirty rectangle
359            width   -- the width of the additional rectangle
360            height  -- the height of the additional dirty rectangle
361        """
362        _lib.caca_remove_dirty_rect.argtypes = [
363            _Canvas, ctypes.c_int, ctypes.c_int,
364            ctypes.c_int, ctypes.c_int
365        ]
366        _lib.caca_remove_dirty_rect.restype  = ctypes.c_int
367
368        return _lib.caca_remove_dirty_rect(self, x, y, height, width)
369
370    def clear_dirty_rect_list(self):
371        """ Clear a canvas's dirty rectangle list.
372        """
373        _lib.caca_clear_dirty_rect_list.argtypes = [_Canvas]
374        _lib.caca_clear_dirty_rect_list.restype  = ctypes.c_int
375
376        return _lib.caca_clear_dirty_rect_list(self)
377
378    def invert(self):
379        """ Invert a canvas' colours.
380        """
381        _lib.caca_invert.argtypes = [_Canvas]
382        _lib.caca_invert.restype  = ctypes.c_int
383
384        return _lib.caca_invert(self)
385
386    def flip(self):
387        """ Flip a canvas horizontally.
388        """
389        _lib.caca_flip.argtypes = [_Canvas]
390        _lib.caca_flip.restype  = ctypes.c_int
391
392        return _lib.caca_flip(self)
393
394    def flop(self):
395        """ Flip a canvas vertically.
396        """
397        _lib.caca_flop.argtypes = [_Canvas]
398        _lib.caca_flop.restype  = ctypes.c_int
399
400        return _lib.caca_flop(self)
401
402    def rotate_180(self):
403        """ Rotate a canvas.
404        """
405        _lib.caca_rotate_180.argtypes = [_Canvas]
406        _lib.caca_rotate_180.restype  = ctypes.c_int
407
408        return _lib.caca_rotate_180(self)
409
410    def rotate_left(self):
411        """ Rotate a canvas, 90 degrees counterclockwise.
412        """
413        _lib.caca_rotate_left.argtypes = [_Canvas]
414        _lib.caca_rotate_left.restype  = ctypes.c_int
415
416        return _lib.caca_rotate_left(self)
417
418    def rotate_right(self):
419        """ Rotate a canvas, 90 degrees clockwise.
420        """
421        _lib.caca_rotate_right.argtypes = [_Canvas]
422        _lib.caca_rotate_right.restype  = ctypes.c_int
423
424        return _lib.caca_rotate_right(self)
425
426    def stretch_left(self):
427        """ Rotate and stretch a canvas, 90 degrees counterclockwise.
428        """
429        _lib.caca_stretch_left.argtypes = [_Canvas]
430        _lib.caca_stretch_left.restype  = ctypes.c_int
431
432        return _lib.caca_stretch_left(self)
433
434    def stretch_right(self):
435        """ Rotate and stretch a canvas, 90 degrees clockwise.
436        """
437        _lib.caca_stretch_right.argtypes = [_Canvas]
438        _lib.caca_stretch_right.restype  = ctypes.c_int
439
440        return _lib.caca_stretch_right(self)
441
442    def get_attr(self, x, y):
443        """ Get the text attribute at the given coordinates.
444
445            x   -- X coordinate
446            y   -- Y coordinate
447        """
448        _lib.caca_get_attr.argtypes = [_Canvas, ctypes.c_int, ctypes.c_int]
449        _lib.caca_get_attr.restype  = ctypes.c_uint32
450        return _lib.caca_get_attr(self, x, y)
451
452    def set_attr(self, attr):
453        """ Set the default character attribute.
454
455            attr    -- the requested attribute value
456        """
457        _lib.caca_set_attr.argtypes = [_Canvas, ctypes.c_uint32]
458        _lib.caca_set_attr.restype  = ctypes.c_int
459
460        return _lib.caca_set_attr(self, attr)
461
462    def put_attr(self, x, y, attr):
463        """ Set the character attribute at the given coordinates.
464
465            x       -- X coordinate
466            y       -- Y coordinate
467            attr    -- the requested attribute value
468        """
469        _lib.caca_put_attr.argtypes = [
470            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
471        ]
472        _lib.caca_put_attr.restype  = ctypes.c_int
473
474        return _lib.caca_put_attr(self, x, y, attr)
475
476    def set_color_ansi(self, fg, bg):
477        """ Set the default colour pair for text (ANSI version).
478
479            fg  -- the requested ANSI foreground colour.
480            bg  -- the requested ANSI background colour.
481        """
482        _lib.caca_set_color_ansi.argtypes = [_Canvas, ctypes.c_uint8, ctypes.c_uint8]
483        _lib.caca_set_color_ansi.restype  = ctypes.c_int
484
485        return _lib.caca_set_color_ansi(self, fg, bg)
486
487    def set_color_argb(self, fg, bg):
488        """ Set the default colour pair for text (truecolor version).
489
490            fg  -- the requested ARGB foreground colour.
491            bg  -- the requested ARGB background colour.
492        """
493        _lib.caca_set_color_argb.argtypes = [
494            _Canvas, ctypes.c_uint16, ctypes.c_uint16
495        ]
496        _lib.caca_set_color_argb.restype  = ctypes.c_int
497
498        return _lib.caca_set_color_argb(self, fg, bg)
499
500    def draw_line(self, x1, y1, x2, y2, ch):
501        """ Draw a line on the canvas using the given character.
502
503            x1  -- X coordinate of the first point
504            y1  -- Y coordinate of the first point
505            x2  -- X coordinate of the second point
506            y2  -- Y coordinate of the second point
507            ch  -- character to be used to draw the line
508        """
509        _lib.caca_draw_line.argtypes = [
510            _Canvas, ctypes.c_int, ctypes.c_int,
511            ctypes.c_int, ctypes.c_int, ctypes.c_uint32
512        ]
513        _lib.caca_draw_line.restype  = ctypes.c_int
514
515        return _lib.caca_draw_line(self, x1, y1, x2, y2, ord(ch))
516
517    def draw_polyline(self, array_x, array_y, n, ch):
518        """ Draw a polyline.
519
520            array_x -- Array of X coordinates, must have n+1 elements
521            array-y -- Array of Y coordinates, must have n+1 elements
522            n       -- Number of lines to draw
523            ch      -- character to be used to draw the line
524        """
525        _lib.caca_draw_polyline.argtypes = [
526            _Canvas, ctypes.c_int * n, ctypes.c_int * n, ctypes.c_int, ctypes.c_uint32
527        ]
528        _lib.caca_draw_polyline.restype  = ctypes.c_int
529
530        return _lib.caca_draw_polyline(self, array_x, array_y, n, ord(ch))
531
532    def draw_thin_line(self, x1, y1, x2, y2):
533        """ Draw a thin line on the canvas, using ASCII art.
534
535            x1  -- X coordinate of the first point
536            y1  -- Y coordinate of the first point
537            x2  -- X coordinate of the second point
538            y2  -- Y coordinate of the second point
539        """
540        _lib.caca_draw_thin_line.argtypes = [
541            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
542        ]
543        _lib.caca_draw_thin_line.restype  = ctypes.c_int
544
545        return _lib.caca_draw_thin_line(self, x1, y1, x2, y2)
546
547    def draw_thin_polyline(self, array_x, array_y, n):
548        """ Draw an ASCII art thin polyline.
549
550            array_x -- Array of X coordinates, must have n+1 elements
551            array_y -- Array of Y coordinates, must have n+1 elements
552            n       -- Number of lines to draw
553        """
554        _lib.caca_draw_thin_polyline.argtypes = [
555            Canvas, ctypes.c_int * n, ctypes.c_int * n, ctypes.c_int
556        ]
557        _lib.caca_draw_thin_polyline.restype  = ctypes.c_int
558
559        return _lib.caca_draw_thin_polyline(self, array_x, array_y, n)
560
561    def draw_circle(self, x, y, r, ch):
562        """ Draw a circle on the canvas using the given character.
563
564            x   -- center X coordinate
565            y   -- center Y coordinate
566            r   -- circle radius
567            ch  -- the UTF-32 character to be used to draw the circle outline
568        """
569        _lib.caca_draw_circle.argtypes = [
570            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
571        ]
572        _lib.caca_draw_circle.restype  = ctypes.c_int
573
574        return _lib.caca_draw_circle(self, x, y, r, ord(ch))
575
576    def draw_ellipse(self, xo, yo, a, b, ch):
577        """ Draw an ellipse on the canvas using the given character.
578
579            xo  -- center X coordinate
580            yo  -- center Y coordinate
581            a   -- ellipse x radius
582            b   -- ellipse y radius
583            ch  -- UTF-32 character to be used to draw the ellipse outline
584        """
585        _lib.caca_draw_ellipse.argtypes = [
586            _Canvas, ctypes.c_int, ctypes.c_int,
587            ctypes.c_int, ctypes.c_int, ctypes.c_uint32
588        ]
589        _lib.caca_draw_ellipse.restype  = ctypes.c_int
590
591        return _lib.caca_draw_ellipse(self, xo, yo, a, b, ord(ch))
592
593    def draw_thin_ellipse(self, xo, yo, a, b):
594        """ Draw a thin ellipse on the canvas.
595
596            xo  -- center X coordinate
597            yo  -- center Y coordinate
598            a   -- ellipse X radius
599            b   -- ellipse Y radius
600        """
601        _lib.caca_draw_thin_ellipse.argtypes = [
602            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
603        ]
604        _lib.caca_draw_thin_ellipse.restype  = ctypes.c_int
605
606        return _lib.caca_draw_thin_ellipse(self, xo, yo, a, b)
607
608    def fill_ellipse(self, xo, yo, a, b, ch):
609        """ Fill an ellipse on the canvas using the given character.
610
611            xo  -- center X coordinate
612            yo  -- center Y coordinate
613            a   -- ellipse X radius
614            b   -- ellipse Y radius
615            ch  -- UTF-32 character to be used to fill the ellipse
616        """
617        _lib.caca_fill_ellipse.argtypes = [
618            _Canvas, ctypes.c_int, ctypes.c_int,
619            ctypes.c_int, ctypes.c_int, ctypes.c_uint32
620        ]
621        _lib.caca_fill_ellipse.restype  = ctypes.c_int
622
623        return _lib.caca_fill_ellipse(self, xo, yo, a, b, ord(ch))
624
625    def draw_box(self, x, y, width, height, ch):
626        """ Draw a box on the canvas using the given character.
627
628            x       -- X coordinate of the upper-left corner of the box
629            y       -- Y coordinate of the upper-left corner of the box
630            width   -- width of the box
631            height  -- height of the box
632            ch      -- character to be used to draw the box
633        """
634        _lib.caca_draw_box.argtypes = [
635            Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
636        ]
637        _lib.caca_draw_box.restype  = ctypes.c_int
638
639        return _lib.caca_draw_box(self, x, y, width, height, ord(ch))
640
641    def draw_thin_box(self, x, y, width, height):
642        """ Draw a thin box on the canvas.
643
644            x       -- X coordinate of the upper-left corner of the box
645            y       -- Y coordinate of the upper-left corner of the box
646            width   -- width of the box
647            height  -- height of the box
648        """
649        _lib.caca_draw_thin_box.argtypes = [
650            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
651        ]
652        _lib.caca_draw_thin_box.restype  = ctypes.c_int
653
654        return _lib.caca_draw_thin_box(self, x, y, width, height)
655
656    def draw_cp437_box(self, x, y, width, height):
657        """ Draw a box on the canvas using CP437 characters.
658
659            x       -- X coordinate of the upper-left corner box
660            y       -- Y coordinate of the upper-left corner box
661            width   -- width of the box
662            height  -- height of the box
663        """
664        _lib.caca_draw_cp437_box.argtypes = [
665            _Canvas, ctypes.c_int, ctypes.c_int,
666            ctypes.c_int, ctypes.c_int
667        ]
668        _lib.caca_draw_cp437_box.restype  = ctypes.c_int
669
670        return _lib.caca_draw_cp437_box(self, x, y, width, height)
671
672    def fill_box(self, x, y, width, height, ch):
673        """ Fill a box on the canvas using the given character.
674
675            x       -- X coordinate of the upper-left corner of the box
676            y       -- Y coordinate of the upper-left corner of the box
677            width   -- width of the box
678            height  -- height of the box
679            ch      -- UFT-32 character to be used to fill the box
680        """
681        _lib.caca_fill_box.argtypes = [
682            _Canvas, ctypes.c_int, ctypes.c_int,
683            ctypes.c_int, ctypes.c_int, ctypes.c_uint32
684        ]
685        _lib.caca_fill_box.restype  = ctypes.c_int
686
687        return _lib.caca_fill_box(self, x, y, width, height, ord(ch))
688
689    def draw_triangle(self, x1, y1, x2, y2, x3, y3, ch):
690        """ Draw a triangle on the canvas using the given character.
691
692            x1  -- X coordinate of the first point
693            y1  -- Y coordinate of the first point
694            x2  -- X coordinate of the second point
695            y2  -- Y coordinate of the second point
696            x3  -- X coordinate of the third point
697            y3  -- Y coordinate of the third point
698            ch  -- UTF-32 character to be used to draw the triangle outline
699        """
700        _lib.caca_draw_triangle.argtypes = [
701            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int,
702            ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
703        ]
704        _lib.caca_draw_triangle.restype  = ctypes.c_int
705
706        return _lib.caca_draw_triangle(self, x1, y1, x2, y2, x3, y3, ord(ch))
707
708    def draw_thin_triangle(self, x1, y1, x2, y2, x3, y3):
709        """ Draw a thin triangle on the canvas.
710        """
711        _lib.caca_draw_thin_triangle.argtypes = [
712            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int,
713            ctypes.c_int, ctypes.c_int, ctypes.c_int
714        ]
715        _lib.caca_draw_thin_triangle.restype  = ctypes.c_int
716
717        return _lib.caca_draw_thin_triangle(self, x1, y1, x2, y2, x3, y3)
718
719    def fill_triangle(self, x1, y1, x2, y2, x3, y3, ch):
720        """ Fill a triangle on the canvas using the given character.
721
722            x1  -- X coordinate of the first point
723            y1  -- Y coordinate of the first point
724            x2  -- X coordinate of the second point
725            y2  -- Y coordinate of the second point
726            x3  -- X coordinate of the second point
727            y3  -- Y coordinate of the second point
728            ch  -- UTF-32 character to be used to fill the triangle
729        """
730        _lib.caca_fill_triangle.argtypes = [
731            _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int,
732            ctypes.c_int, ctypes.c_int, ctypes.c_int
733        ]
734        _lib.caca_fill_triangle.restype  = ctypes.c_int
735
736        return _lib.caca_fill_triangle(self, x1, y1, x2, y2, x3, y3, ord(ch))
737
738    def fill_triangle_textured(self, coords, tex, uv):
739        """ Fill a triangle on the canvas using an arbitrary-sized texture.
740
741            coords  -- coordinates of the triangle (3{x,y})
742            tex     -- the handle of the canvas texture
743            uv      -- coordinates of the texture  (3{u,v})
744        """
745        _lib.caca_fill_triangle_textured.argtypes = [
746            _Canvas, ctypes.c_int * 6, _Canvas, ctypes.c_int * 6
747        ]
748        _lib.caca_fill_triangle_textured.restype  = ctypes.c_int
749
750        return _lib.caca_fill_triangle_textured(self, coords, tex, uv)
751
752    def get_frame_count(self):
753        """ Get the number of frames in a canvas.
754        """
755        _lib.caca_get_frame_count.argtypes = [_Canvas]
756        _lib.caca_get_frame_count.restype  = ctypes.c_int
757
758        return _lib.caca_get_frame_count(self)
759
760    def set_frame(self, idx):
761        """ Activate a given canvas frame.
762
763            idx -- the canvas frame to activate
764        """
765        _lib.caca_set_frame.argtypes = [_Canvas, ctypes.c_int]
766        _lib.caca_set_frame.restype  = ctypes.c_int
767
768        return _lib.caca_set_frame(self, idx)
769
770    def get_frame_name(self):
771        """ Get the current frame's name.
772        """
773        _lib.caca_get_frame_name.argtypes = [_Canvas]
774        _lib.caca_get_frame_name.restype  = ctypes.c_char_p
775
776        return _lib.caca_get_frame_name(self)
777
778    def set_frame_name(self, name):
779        """ Set the current frame's name.
780
781            name    -- the name to give to the current frame
782        """
783        _lib.caca_set_frame_name.argtypes = [_Canvas, ctypes.c_char_p]
784        _lib.caca_set_frame_name.restype  = ctypes.c_int
785
786        return _lib.caca_set_frame_name(self, name)
787
788    def create_frame(self, idx):
789        """ Add a frame to a canvas.
790
791            idx -- the index where to insert the new frame
792        """
793        _lib.caca_create_frame.argtypes = [_Canvas, ctypes.c_int]
794        _lib.caca_create_frame.restype  = ctypes.c_int
795
796        return _lib.caca_create_frame(self, idx)
797
798    def free_frame(self, idx):
799        """ Remove a frame from a canvas.
800
801            idx -- the index of the frame to delete
802        """
803        _lib.caca_free_frame.argtypes = [_Canvas, ctypes.c_int]
804        _lib.caca_free_frame.restype  = ctypes.c_int
805
806        return _lib.caca_free_frame(self, idx)
807
808    def import_from_memory(self, data, fmt):
809        """ Import a memory buffer into a canvas.
810
811            data -- a memory area containing the data to be loaded into the canvas
812            fmt  -- a string describing the input format
813
814            Valid values for format are:
815              - "": attempt to autodetect the file format.
816              - caca: import native libcaca files.
817              - text: import ASCII text files.
818              - ansi: import ANSI files.
819              - utf8: import UTF-8 files with ANSI colour codes.
820        """
821        #set data size
822        length = ctypes.c_size_t(len(data))
823        _lib.caca_import_canvas_from_memory.argtypes = [
824            Canvas, ctypes.c_char_p, ctypes.c_size_t, ctypes.c_char_p
825        ]
826        _lib.caca_import_canvas_from_memory.restype  = ctypes.c_int
827
828        return _lib.caca_import_canvas_from_memory(self, data, length, fmt)
829
830    def import_from_file(self, filename, fmt):
831        """ Import a file into a canvas.
832
833            filename -- the name of the file to load
834            fmt      -- a string describing the input format
835
836            Valid values for format are:
837              - "": attempt to autodetect the file format.
838              - caca: import native libcaca files.
839              - text: import ASCII text files.
840              - ansi: import ANSI files.
841              - utf8: import UTF-8 files with ANSI colour codes.
842        """
843        _lib.caca_import_canvas_from_file.argtypes = [
844            _Canvas, ctypes.c_char_p, ctypes.c_char_p
845        ]
846        _lib.caca_import_canvas_from_file.restype  = ctypes.c_int
847
848        return _lib.caca_import_canvas_from_file(self, filename, fmt)
849
850    def import_area_from_memory(self, x, y, data, fmt):
851        """ Import a memory buffer into a canvas area.
852
853            x    -- the leftmost coordinate of the area to import to
854            y    -- the topmost coordinate of the area to import to
855            data -- a memory area containing the data to be loaded into the canvas
856            fmt  -- a string describing the input format
857
858            Valid values for format are:
859              - "": attempt to autodetect the file format.
860              - caca: import native libcaca files.
861              - text: import ASCII text files.
862              - ansi: import ANSI files.
863              - utf8: import UTF-8 files with ANSI colour codes.
864        """
865        #set data size
866        length = ctypes.c_size_t(len(data))
867        _lib.caca_import_area_from_memory.argtypes = [
868            _Canvas, ctypes.c_int, ctypes.c_int,
869            ctypes.c_char_p, ctypes.c_size_t, ctypes.c_char_p
870        ]
871        _lib.caca_import_area_from_memory.restype  = ctypes.c_int
872
873        return _lib.caca_import_area_from_memory(self, x, y, data, length, fmt)
874
875    def import_area_from_file(self, x, y, filename, fmt):
876        """ Import a file into a canvas area.
877
878            x        -- the leftmost coordinate of the area to import to
879            y        -- the topmost coordinate of the area to import to
880            filename -- the name of the file to be load
881            fmt      -- a string describing the input format
882
883            Valid values for format are:
884              - "": attempt to autodetect the file format.
885              - caca: import native libcaca files.
886              - text: import ASCII text files.
887              - ansi: import ANSI files.
888              - utf8: import UTF-8 files with ANSI colour codes.
889        """
890        _lib.caca_import_area_from_file.argtypes = [
891            _Canvas, ctypes.c_int, ctypes.c_int,
892            ctypes.c_char_p, ctypes.c_char_p
893        ]
894        _lib.caca_import_area_from_file.restype  = ctypes.c_int
895
896        return _lib.caca_import_area_from_file(self, x, y, filename, fmt)
897
898    def export_to_memory(self, fmt):
899        """ Export a canvas into a foreign format.
900
901            fmt -- a string describing the output format
902
903            Valid values for format are:
904              - caca: export native libcaca files.
905              - ansi: export ANSI art (CP437 charset with ANSI colour codes).
906              - html: export an HTML page with CSS information.
907              - html3: export an HTML table that should be compatible with
908                       most navigators, including textmode ones.
909              - irc: export UTF-8 text with mIRC colour codes.
910              - ps: export a PostScript document.
911              - svg: export an SVG vector image.
912              - tga: export a TGA image.
913        """
914        #initialize pointer
915        p_size_t = ctypes.POINTER(ctypes.c_size_t)
916        _lib.caca_export_canvas_to_memory.argtypes = [
917                _Canvas, ctypes.c_char_p, p_size_t
918        ]
919        _lib.caca_export_canvas_to_memory.restype  = ctypes.POINTER(ctypes.c_char_p)
920
921        p = ctypes.c_size_t()
922        ret = _lib.caca_export_canvas_to_memory(self, fmt, p)
923
924        return ctypes.string_at(ret, p.value)
925
926    def export_area_to_memory(self, x, y, width, height, fmt):
927        """ Export a canvas portion into a foreign format.
928
929            x       -- the leftmost coordinate of the area to export
930            y       -- the topmost coordinate of the area to export
931            width   -- the width of the area to export
932            height  -- the height of the area to export
933            fmt     -- a string describing the output format
934
935            Valid values for format are:
936              - caca: export native libcaca files.
937              - ansi: export ANSI art (CP437 charset with ANSI colour codes).
938              - html: export an HTML page with CSS information.
939              - html3: export an HTML table that should be compatible with
940                       most navigators, including textmode ones.
941              - irc: export UTF-8 text with mIRC colour codes.
942              - ps: export a PostScript document.
943              - svg: export an SVG vector image.
944              - tga: export a TGA image.
945        """
946        #initialize pointer
947        p_size_t = ctypes.POINTER(ctypes.c_size_t)
948
949        _lib.caca_export_area_to_memory.argtypes = [
950                _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int,
951                ctypes.c_char_p, p_size_t
952        ]
953        _lib.caca_export_area_to_memory.restype  = ctypes.POINTER(ctypes.c_char_p)
954
955        p = ctypes.c_size_t()
956        ret = _lib.caca_export_area_to_memory(self, x, y, width, height, fmt, p)
957
958        return ctypes.string_at(ret, p.value)
959
960    def set_figfont(self, filename):
961        """     Load a figfont and attach it to a canvas.
962
963            filename    -- the figfont file to load.
964        """
965        _lib.caca_canvas_set_figfont.argtypes = [_Canvas, ctypes.c_char_p]
966        _lib.caca_canvas_set_figfont.restype  = ctypes.c_int
967
968        return _lib.caca_canvas_set_figfont(self, filename)
969
970    def put_figchar(self, ch):
971        """ Paste a character using the current figfont.
972
973            ch  -- the character to paste
974        """
975        _lib.caca_put_figchar.argtypes = [_Canvas, ctypes.c_uint32]
976        _lib.caca_put_figchar.restype  = ctypes.c_int
977
978        return _lib.caca_put_figchar(self, ord(ch))
979
980    def flush_figlet(self):
981        """ Flush the figlet context
982        """
983        _lib.caca_flush_figlet.argtypes = [_Canvas]
984        _lib.caca_flush_figlet.restype  = ctypes.c_int
985
986        return _lib.caca_flush_figlet(self)
987
988    def render(self, font, buf, width, height, pitch):
989        """ Render the canvas onto an image buffer.
990
991            font    -- a Font() object
992            buf     -- the image buffer
993            width   -- the width (in pixels) of the image
994            heigth  -- the height (in pixels) of the image
995            pitch   -- the pitch (in bytes) of the image
996        """
997        _lib.caca_render_canvas.argtypes = [
998            _Canvas, _Font, ctypes.c_char_p,
999            ctypes.c_int, ctypes.c_int, ctypes.c_int
1000        ]
1001        _lib.caca_render_canvas.restype  = ctypes.c_int
1002
1003        return _lib.caca_render_canvas(self, font, buf, width, height, pitch)
1004
1005class NullCanvas(_Canvas):
1006    """ Represent a NULL canvas_t, eg to use as canvas mask for blit operations.
1007    """
1008    def __str__(self):
1009        return "<NullCanvas>"
1010
1011class CanvasError(Exception):
1012    pass
1013
Note: See TracBrowser for help on using the repository browser.