958 lines
33 KiB

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