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

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