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

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