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

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