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

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