您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

813 行
27 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 set_size(self, width, height):
  56. """ Resize a canvas.
  57. width -- the desired canvas width
  58. height -- the desired canvas height
  59. """
  60. _lib.caca_set_canvas_size.argtypes = [
  61. _Canvas, ctypes.c_int, ctypes.c_int
  62. ]
  63. _lib.caca_set_canvas_size.restype = ctypes.c_int
  64. return _lib.caca_set_canvas_size(self, width, height)
  65. def get_width(self):
  66. """ Get the canvas width.
  67. """
  68. _lib.caca_get_canvas_width.argtypes = [_Canvas]
  69. _lib.caca_get_canvas_width.restype = ctypes.c_int
  70. return _lib.caca_get_canvas_width(self)
  71. def get_height(self):
  72. """ Get the canvas height.
  73. """
  74. _lib.caca_get_canvas_height.argtypes = [_Canvas]
  75. _lib.caca_get_canvas_height.restype = ctypes.c_int
  76. return _lib.caca_get_canvas_height(self)
  77. def get_chars(self):
  78. """ Get the canvas character array, return python list.
  79. """
  80. chlist = []
  81. #get canvas size
  82. w, h = self.get_width(), self.get_height()
  83. _lib.caca_get_canvas_chars.argtypes = [_Canvas]
  84. _lib.caca_get_canvas_chars.restype = \
  85. ctypes.POINTER(ctypes.c_uint8 * (w * h))
  86. plist = _lib.caca_get_canvas_chars(self)
  87. #build character list
  88. for item in plist.contents:
  89. if item != 0:
  90. chlist.append(chr(item))
  91. return chlist
  92. def gotoxy(self, x, y):
  93. """ Set cursor position.
  94. x -- X cursor coordinate
  95. y -- Y cursor coordinate
  96. """
  97. _lib.caca_gotoxy.argtypes = [_Canvas, ctypes.c_int]
  98. _lib.caca_gotoxy.restyoe = ctypes.c_int
  99. return _lib.caca_gotoxy(self, x, y)
  100. def wherex(self):
  101. """ Get X cursor position.
  102. """
  103. _lib.caca_wherex.argtypes = [_Canvas]
  104. _lib.caca_wherex.restype = ctypes.c_int
  105. return _lib.caca_wherex(self)
  106. def wherey(self):
  107. """ Get Y cursor position.
  108. """
  109. _lib.caca_wherey.argtypes = [_Canvas]
  110. _lib.caca_wherey.restype = ctypes.c_int
  111. return _lib.caca_wherey(self)
  112. def put_char(self, x, y, ch):
  113. """ Print an ASCII or Unicode character.
  114. x -- X coordinate
  115. y -- Y coordinate
  116. ch -- the character to print
  117. """
  118. _lib.caca_put_char.argtypes = [
  119. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  120. ]
  121. _lib.caca_put_char.restype = ctypes.c_int
  122. return _lib.caca_put_char(self, x, y, ord(ch))
  123. def get_char(self, x, y):
  124. """ Get the Unicode character at the given coordinates.
  125. x -- X coordinate
  126. y -- Y coordinate
  127. """
  128. _lib.caca_get_char.argtypes = [
  129. _Canvas, ctypes.c_int, ctypes.c_int
  130. ]
  131. _lib.caca_get_char.restype = ctypes.c_uint32
  132. return _lib.caca_get_char(self, x, y)
  133. def put_str(self, x, y, s):
  134. """ Print a string.
  135. x -- X coordinate
  136. y -- Y coordinate
  137. s -- the string to print
  138. """
  139. _lib.caca_put_str.argtypes = [
  140. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_char_p
  141. ]
  142. _lib.caca_put_str.restype = ctypes.c_int
  143. return _lib.caca_put_str(self, x, y, s)
  144. def printf(self, x, y, fmt, *args):
  145. """ Print a formated string.
  146. x -- X coordinate
  147. y -- Y coordinate
  148. fmt -- the format string to print
  149. args -- Arguments to the format string
  150. """
  151. _lib.caca_printf.argtypes = [
  152. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_char_p
  153. ]
  154. _lib.caca_printf.restype = ctypes.c_int
  155. return _lib.caca_printf(self, x, y, fmt, *args)
  156. def clear(self):
  157. """ Clear the canvas.
  158. """
  159. _lib.caca_clear_canvas.argtypes = [_Canvas]
  160. _lib.caca_clear_canvas.restype = ctypes.c_int
  161. return _lib.caca_clear_canvas(self)
  162. def set_handle(self, x, y):
  163. """ Set cursor handle. Blitting method will use the handle value to
  164. put the canvas at the proper coordinates.
  165. x -- X handle coordinate
  166. y -- Y handle coordinate
  167. """
  168. _lib.caca_set_canvas_handle.argtypes = [
  169. _Canvas, ctypes.c_int, ctypes.c_int
  170. ]
  171. _lib.caca_set_canvas_handle.restype = ctypes.c_int
  172. return _lib.caca_set_canvas_handle(self, x, y)
  173. def get_handle_x(self):
  174. """ Get X handle position.
  175. """
  176. _lib.caca_get_canvas_handle_x.argtypes = [_Canvas]
  177. _lib.caca_get_canvas_handle_x.restype = ctypes.c_int
  178. return _lib.caca_get_canvas_handle_x(self)
  179. def get_handle_y(self):
  180. """ Get Y handle position.
  181. """
  182. _lib.caca_get_canvas_handle_y.argtypes = [_Canvas]
  183. _lib.caca_get_canvas_handle_y.restype = ctypes.c_int
  184. return _lib.caca_get_canvas_handle_y(self)
  185. def blit(self, x, y, cv, mask):
  186. """ Blit canvas onto another one.
  187. x -- X coordinate
  188. y -- Y coordinate
  189. cv -- the source canvas
  190. mask -- the mask canvas
  191. """
  192. _lib.caca_blit.argtypes = [
  193. _Canvas, ctypes.c_int, ctypes.c_int, _Canvas, _Canvas
  194. ]
  195. _lib.caca_blit.restype = ctypes.c_int
  196. return _lib.caca_blit(self, x, y, cv, mask)
  197. def set_boundaries(self, x, y, width, height):
  198. """ Set a canvas' new boundaries.
  199. x -- X coordinate of the top-left corner
  200. y -- Y coordinate of the top-left corner
  201. width -- width of the box
  202. height -- height of the box
  203. """
  204. _lib.caca_set_canvas_boundaries.argtypes = [
  205. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
  206. ]
  207. _lib.caca_set_canvas_boundaries.restype = ctypes.c_int
  208. return _lib.caca_set_canvas_boundaries(self, x, y, width, height)
  209. def disable_dirty_rect(self):
  210. """ Disable dirty rectangles.
  211. """
  212. _lib.caca_disable_dirty_rect.argtypes = [_Canvas]
  213. _lib.caca_disable_dirty_rect.restype = ctypes.c_int
  214. return _lib.caca_disable_dirty_rect(self)
  215. def enable_dirty_rect(self):
  216. """ Enable dirty rectangles.
  217. """
  218. _lib.caca_enable_dirty_rect.argtypes = [_Canvas]
  219. _lib.caca_enable_dirty_rect.restype = ctypes.c_int
  220. return _lib.caca_enable_dirty_rect(self)
  221. def get_dirty_rect_count(self):
  222. """ Get the number of dirty rectangles in the canvas.
  223. """
  224. _lib.caca_get_dirty_rect_count.argtypes = [_Canvas]
  225. _lib.caca_get_dirty_rect_count.restype = ctypes.c_int
  226. return _lib.caca_get_dirty_rect_count(self)
  227. def get_dirty_rect(self, idx):
  228. """ Get a canvas's dirty rectangle.
  229. idx -- the requested rectangle index
  230. """
  231. x = ctypes.POINTER(ctypes.c_int)
  232. y = ctypes.POINTER(ctypes.c_int)
  233. w = ctypes.POINTER(ctypes.c_int)
  234. h = ctypes.POINTER(ctypes.c_int)
  235. _lib.caca_get_dirty_rect.argtypes = [
  236. _Canvas, ctypes.c_int,
  237. ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int),
  238. ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)
  239. ]
  240. _lib.caca_get_dirty_rect.restype = ctypes.c_int
  241. _lib.caca_get_dirty_rect(self, idx, x, y, w, h)
  242. return [x.contents.value, y.contents.value,
  243. w.contents.value, h.contents.value]
  244. def add_dirty_rect(self, x, y, width, height):
  245. """ Add an area to the canvas's dirty rectangle list.
  246. x -- the leftmost edge of the additional dirty rectangle
  247. y -- the topmost edge of the additional dirty rectangle
  248. width -- the width of the additional dirty rectangle
  249. height -- the height of the additional dirty rectangle
  250. """
  251. _lib.caca_add_dirty_rect.argtypes = [
  252. _Canvas, ctypes.c_int, ctypes.c_int,
  253. ctypes.c_int, ctypes.c_int
  254. ]
  255. _lib.caca_add_dirty_rect.restype = ctypes.c_int
  256. return _lib.caca_add_dirty_rect(self, x, y, width, height)
  257. def remove_dirty_rect(self, x, y, width, height):
  258. """ Remove an area from the dirty rectangle list.
  259. x -- the leftmost edge of the additional dirty rectangle
  260. y -- the topmost edge of the additional dirty rectangle
  261. width -- the width of the additional rectangle
  262. height -- the height of the additional dirty rectangle
  263. """
  264. _lib.caca_remove_dirty_rect.argtypes = [
  265. _Canvas, ctypes.c_int, ctypes.c_int,
  266. ctypes.c_int, ctypes.c_int
  267. ]
  268. _lib.caca_remove_dirty_rect.restype = ctypes.c_int
  269. return _lib.caca_remove_dirty_rect(self, x, y, height, width)
  270. def clear_dirty_rect_list(self):
  271. """ Clear a canvas's dirty rectangle list.
  272. """
  273. _lib.caca_clear_dirty_rect_list.argtypes = [_Canvas]
  274. _lib.caca_clear_dirty_rect_list.restype = ctypes.c_int
  275. return _lib.caca_clear_dirty_rect_list(self)
  276. def invert(self):
  277. """ Invert a canvas' colours.
  278. """
  279. _lib.caca_invert.argtypes = [_Canvas]
  280. _lib.caca_invert.restype = ctypes.c_int
  281. return _lib.caca_invert(self)
  282. def flip(self):
  283. """ Flip a canvas horizontally.
  284. """
  285. _lib.caca_flip.argtypes = [_Canvas]
  286. _lib.caca_flip.restype = ctypes.c_int
  287. return _lib.caca_flip(self)
  288. def flop(self):
  289. """ Flip a canvas vertically.
  290. """
  291. _lib.caca_flop.argtypes = [_Canvas]
  292. _lib.caca_flop.restype = ctypes.c_int
  293. return _lib.caca_flop(self)
  294. def rotate_180(self):
  295. """ Rotate a canvas.
  296. """
  297. _lib.caca_rotate_180.argtypes = [_Canvas]
  298. _lib.caca_rotate_180.restype = ctypes.c_int
  299. return _lib.caca_rotate_180(self)
  300. def rotate_left(self):
  301. """ Rotate a canvas, 90 degrees counterclockwise.
  302. """
  303. _lib.caca_rotate_left.argtypes = [_Canvas]
  304. _lib.caca_rotate_left.restype = ctypes.c_int
  305. return _lib.caca_rotate_left(self)
  306. def rotate_right(self):
  307. """ Rotate a canvas, 90 degrees clockwise.
  308. """
  309. _lib.caca_rotate_right.argtypes = [_Canvas]
  310. _lib.caca_rotate_right.restype = ctypes.c_int
  311. return _lib.caca_rotate_right(self)
  312. def stretch_left(self):
  313. """ Rotate and stretch a canvas, 90 degrees counterclockwise.
  314. """
  315. _lib.caca_stretch_left.argtypes = [_Canvas]
  316. _lib.caca_stretch_left.restype = ctypes.c_int
  317. return _lib.caca_stretch_left(self)
  318. def stretch_right(self):
  319. """ Rotate and stretch a canvas, 90 degrees clockwise.
  320. """
  321. _lib.caca_stretch_right.argtypes = [_Canvas]
  322. _lib.caca_stretch_right.restype = ctypes.c_int
  323. return _lib.caca_stretch_right(self)
  324. def get_attr(self, x, y):
  325. """ Get the text attribute at the given coordinates.
  326. x -- X coordinate
  327. y -- Y coordinate
  328. """
  329. _lib.caca_get_attr.argtypes = [_Canvas, ctypes.c_int, ctypes.c_int]
  330. _lib.caca_get_attr.restype = ctypes.c_uint32
  331. return _lib.caca_get_attr(self, x, y)
  332. def set_attr(self, attr):
  333. """ Set the default character attribute.
  334. attr -- the requested attribute value
  335. """
  336. _lib.caca_set_attr.argtypes = [_Canvas, ctypes.c_uint32]
  337. _lib.caca_set_attr.restype = ctypes.c_int
  338. return _lib.caca_set_attr(self, attr)
  339. def put_attr(self, x, y, attr):
  340. """ Set the character attribute at the given coordinates.
  341. x -- X coordinate
  342. y -- Y coordinate
  343. attr -- the requested attribute value
  344. """
  345. _lib.caca_put_attr.argtypes = [
  346. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  347. ]
  348. _lib.caca_put_attr.restype = ctypes.c_int
  349. return _lib.caca_put_attr(self, x, y, attr)
  350. def set_color_ansi(self, fg, bg):
  351. """ Set the default colour pair for text (ANSI version).
  352. fg -- the requested ANSI foreground colour.
  353. bg -- the requested ANSI background colour.
  354. """
  355. _lib.caca_set_color_ansi.argtypes = [_Canvas, ctypes.c_uint8, ctypes.c_uint8]
  356. _lib.caca_set_color_ansi.restype = ctypes.c_int
  357. return _lib.caca_set_color_ansi(self, fg, bg)
  358. def set_color_argb(self, fg, bg):
  359. """ Set the default colour pair for text (truecolor version).
  360. fg -- the requested ARGB foreground colour.
  361. bg -- the requested ARGB background colour.
  362. """
  363. _lib.caca_set_color_argb.argtypes = [
  364. _Canvas, ctypes.c_uint16, ctypes.c_uint16
  365. ]
  366. _lib.caca_set_color_argb.restype = ctypes.c_int
  367. return _lib.caca_set_color_argb(self, fg, bg)
  368. def draw_line(self, x1, y1, x2, y2, ch):
  369. """ Draw a line on the canvas using the given character.
  370. x1 -- X coordinate of the first point
  371. y1 -- Y coordinate of the first point
  372. x2 -- X coordinate of the second point
  373. y2 -- Y coordinate of the second point
  374. ch -- character to be used to draw the line
  375. """
  376. _lib.caca_draw_line.argtypes = [
  377. _Canvas, ctypes.c_int, ctypes.c_int,
  378. ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  379. ]
  380. _lib.caca_draw_line.restype = ctypes.c_int
  381. return _lib.caca_draw_line(self, x1, y1, x2, y2, ord(ch))
  382. def draw_polyline(self, array_x, array_y, n, ch):
  383. """ Draw a polyline.
  384. array_x -- Array of X coordinates, must have n+1 elements
  385. array-y -- Array of Y coordinates, must have n+1 elements
  386. n -- Number of lines to draw
  387. ch -- character to be used to draw the line
  388. """
  389. _lib.caca_draw_polyline.argtypes = [
  390. _Canvas, ctypes.c_int * n, ctypes.c_int * n, ctypes.c_int, ctypes.c_uint32
  391. ]
  392. _lib.caca_draw_polyline.restype = ctypes.c_int
  393. return _lib.caca_draw_polyline(self, array_x, array_y, n, ord(ch))
  394. def draw_thin_line(self, x1, y1, x2, y2):
  395. """ Draw a thin line on the canvas, using ASCII art.
  396. x1 -- X coordinate of the first point
  397. y1 -- Y coordinate of the first point
  398. x2 -- X coordinate of the second point
  399. y2 -- Y coordinate of the second point
  400. """
  401. _lib.caca_draw_thin_line.argtypes = [
  402. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
  403. ]
  404. _lib.caca_draw_thin_line.restype = ctypes.c_int
  405. return _lib.caca_draw_thin_line(self, x1, y1, x2, y2)
  406. def draw_thin_polyline(self, array_x, array_y, n):
  407. """ Draw an ASCII art thin polyline.
  408. array_x -- Array of X coordinates, must have n+1 elements
  409. array_y -- Array of Y coordinates, must have n+1 elements
  410. n -- Number of lines to draw
  411. """
  412. _lib.caca_draw_thin_polyline.argtypes = [
  413. Canvas, ctypes.c_int * n, ctypes.c_int * n, ctypes.c_int
  414. ]
  415. _lib.caca_draw_thin_polyline.restype = ctypes.c_int
  416. return _lib.caca_draw_thin_polyline(self, array_x, array_y, n)
  417. def draw_circle(self, x, y, r, ch):
  418. """ Draw a circle on the canvas using the given character.
  419. x -- center X coordinate
  420. y -- center Y coordinate
  421. r -- circle radius
  422. ch -- the UTF-32 character to be used to draw the circle outline
  423. """
  424. _lib.caca_draw_circle.argtypes = [
  425. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  426. ]
  427. _lib.caca_draw_circle.restype = ctypes.c_int
  428. return _lib.caca_draw_circle(self, x, y, r, ord(ch))
  429. def draw_ellipse(self, xo, yo, a, b, ch):
  430. """ Draw an ellipse on the canvas using the given character.
  431. xo -- center X coordinate
  432. yo -- center Y coordinate
  433. a -- ellipse x radius
  434. b -- ellipse y radius
  435. ch -- UTF-32 character to be used to draw the ellipse outline
  436. """
  437. _lib.caca_draw_ellipse.argtypes = [
  438. _Canvas, ctypes.c_int, ctypes.c_int,
  439. ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  440. ]
  441. _lib.caca_draw_ellipse.restype = ctypes.c_int
  442. return _lib.caca_draw_ellipse(self, xo, yo, a, b, ord(ch))
  443. def draw_thin_ellipse(self, xo, yo, a, b):
  444. """ Draw a thin ellipse on the canvas.
  445. xo -- center X coordinate
  446. yo -- center Y coordinate
  447. a -- ellipse X radius
  448. b -- ellipse Y radius
  449. """
  450. _lib.caca_draw_thin_ellipse.argtypes = [
  451. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
  452. ]
  453. _lib.caca_draw_thin_ellipse.restype = ctypes.c_int
  454. return _lib.caca_draw_thin_ellipse(self, xo, yo, a, b)
  455. def fill_ellipse(self, xo, yo, a, b, ch):
  456. """ Fill an ellipse on the canvas using the given character.
  457. xo -- center X coordinate
  458. yo -- center Y coordinate
  459. a -- ellipse X radius
  460. b -- ellipse Y radius
  461. ch -- UTF-32 character to be used to fill the ellipse
  462. """
  463. _lib.caca_fill_ellipse.argtypes = [
  464. _Canvas, ctypes.c_int, ctypes.c_int,
  465. ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  466. ]
  467. _lib.caca_fill_ellipse.restype = ctypes.c_int
  468. return _lib.caca_fill_ellipse(self, xo, yo, a, b, ord(ch))
  469. def draw_box(self, x, y, width, height, ch):
  470. """ Draw a box on the canvas using the given character.
  471. x -- X coordinate of the upper-left corner of the box
  472. y -- Y coordinate of the upper-left corner of the box
  473. width -- width of the box
  474. height -- height of the box
  475. ch -- character to be used to draw the box
  476. """
  477. _lib.caca_draw_box.argtypes = [
  478. Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  479. ]
  480. _lib.caca_draw_box.restype = ctypes.c_int
  481. return _lib.caca_draw_box(self, x, y, width, height, ord(ch))
  482. def draw_thin_box(self, x, y, width, height):
  483. """ Draw a thin box on the canvas.
  484. x -- X coordinate of the upper-left corner of the box
  485. y -- Y coordinate of the upper-left corner of the box
  486. width -- width of the box
  487. height -- height of the box
  488. """
  489. _lib.caca_draw_thin_box.argtypes = [
  490. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
  491. ]
  492. _lib.caca_draw_thin_box.restype = ctypes.c_int
  493. return _lib.caca_draw_thin_box(self, x, y, width, height)
  494. def draw_cp437_box(self, x, y, width, height):
  495. """ Draw a box on the canvas using CP437 characters.
  496. x -- X coordinate of the upper-left corner box
  497. y -- Y coordinate of the upper-left corner box
  498. width -- width of the box
  499. height -- height of the box
  500. """
  501. _lib.caca_draw_cp437_box.argtypes = [
  502. _Canvas, ctypes.c_int, ctypes.c_int,
  503. ctypes.c_int, ctypes.c_int
  504. ]
  505. _lib.caca_draw_cp437_box.restype = ctypes.c_int
  506. return _lib.caca_draw_cp437_box(self, x, y, width, height)
  507. def fill_box(self, x, y, width, height, ch):
  508. """ Fill a box on the canvas using the given character.
  509. x -- X coordinate of the upper-left corner of the box
  510. y -- Y coordinate of the upper-left corner of the box
  511. width -- width of the box
  512. height -- height of the box
  513. ch -- UFT-32 character to be used to fill the box
  514. """
  515. _lib.caca_fill_box.argtypes = [
  516. _Canvas, ctypes.c_int, ctypes.c_int,
  517. ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  518. ]
  519. _lib.caca_fill_box.restype = ctypes.c_int
  520. return _lib.caca_fill_box(self, x, y, width, height, ord(ch))
  521. def draw_triangle(self, x1, y1, x2, y2, x3, y3, ch):
  522. """ Draw a triangle on the canvas using the given character.
  523. x1 -- X coordinate of the first point
  524. y1 -- Y coordinate of the first point
  525. x2 -- X coordinate of the second point
  526. y2 -- Y coordinate of the second point
  527. x3 -- X coordinate of the third point
  528. y3 -- Y coordinate of the third point
  529. ch -- UTF-32 character to be used to draw the triangle outline
  530. """
  531. _lib.caca_draw_triangle.argtypes = [
  532. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int,
  533. ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  534. ]
  535. _lib.caca_draw_triangle.restype = ctypes.c_int
  536. return _lib.caca_draw_triangle(self, x1, y1, x2, y2, x3, y3, ord(ch))
  537. def draw_thin_triangle(self, x1, y1, x2, y2, x3, y3):
  538. """ Draw a thin triangle on the canvas.
  539. """
  540. _lib.caca_draw_thin_triangle.argtypes = [
  541. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int,
  542. ctypes.c_int, ctypes.c_int, ctypes.c_int
  543. ]
  544. _lib.caca_draw_thin_triangle.restype = ctypes.c_int
  545. return _lib.caca_draw_thin_triangle(self, x1, y1, x2, y2, x3, y3)
  546. def fill_triangle(self, x1, y1, x2, y2, x3, y3, ch):
  547. """ Fill a triangle on the canvas using the given character.
  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. x3 -- X coordinate of the second point
  553. y3 -- Y coordinate of the second point
  554. ch -- UTF-32 character to be used to fill the triangle
  555. """
  556. _lib.caca_fill_triangle.argtypes = [
  557. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int,
  558. ctypes.c_int, ctypes.c_int, ctypes.c_int
  559. ]
  560. _lib.caca_fill_triangle.restype = ctypes.c_int
  561. return _lib.caca_fill_triangle(self, x1, y1, x2, y2, x3, y3, ord(ch))
  562. def fill_triangle_textured(self, coords, tex, uv):
  563. """ Fill a triangle on the canvas using an arbitrary-sized texture.
  564. coords -- coordinates of the triangle (3{x,y})
  565. tex -- the handle of the canvas texture
  566. uv -- coordinates of the texture (3{u,v})
  567. """
  568. _lib.caca_fill_triangle_textured.argtypes = [
  569. _Canvas, ctypes.c_int * 6, _Canvas, ctypes.c_int * 6
  570. ]
  571. _lib.caca_fill_triangle_textured.restype = ctypes.c_int
  572. return _lib.caca_fill_triangle_textured(self, coords, tex, uv)
  573. def get_frame_count(self):
  574. """ Get the number of frames in a canvas.
  575. """
  576. _lib.caca_get_frame_count.argtypes = [_Canvas]
  577. _lib.caca_get_frame_count.restype = ctypes.c_int
  578. return _lib.caca_get_frame_count(self)
  579. def set_frame(self, idx):
  580. """ Activate a given canvas frame.
  581. idx -- the canvas frame to activate
  582. """
  583. _lib.caca_set_frame.argtypes = [_Canvas, ctypes.c_int]
  584. _lib.caca_set_frame.restype = ctypes.c_int
  585. return _lib.caca_set_frame(self, idx)
  586. def get_frame_name(self):
  587. """ Get the current frame's name.
  588. """
  589. _lib.caca_get_frame_name.argtypes = [_Canvas]
  590. _lib.caca_get_frame_name.restype = ctypes.c_char_p
  591. return _lib.caca_get_frame_name(self)
  592. def set_frame_name(self, name):
  593. """ Set the current frame's name.
  594. name -- the name to give to the current frame
  595. """
  596. _lib.caca_set_frame_name.argtypes = [_Canvas, ctypes.c_char_p]
  597. _lib.caca_set_frame_name.restype = ctypes.c_int
  598. return _lib.caca_set_frame_name(self, name)
  599. def create_frame(self, idx):
  600. """ Add a frame to a canvas.
  601. idx -- the index where to insert the new frame
  602. """
  603. _lib.caca_create_frame.argtypes = [_Canvas, ctypes.c_int]
  604. _lib.caca_create_frame.restype = ctypes.c_int
  605. return _lib.caca_create_frame(self, idx)
  606. def free_frame(self, idx):
  607. """ Remove a frame from a canvas.
  608. idx -- the index of the frame to delete
  609. """
  610. _lib.caca_free_frame.argtypes = [_Canvas, ctypes.c_int]
  611. _lib.caca_free_frame.restype = ctypes.c_int
  612. return _lib.caca_free_frame(self, idx)
  613. def import_from_memory(self, data, length, fmt):
  614. """ Import a memory buffer into the given libcaca canvas's current frame.
  615. The current frame is resized accordingly and its contents are replaced
  616. with the imported data.
  617. data -- a memory area containing the data to be loaded into the canvas
  618. length -- the size in bytes of the memory area
  619. fmt -- a string describing the input format
  620. """
  621. _lib.caca_import_canvas_from_memory.argtypes = [
  622. Canvas, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p
  623. ]
  624. _lib.caca_import_canvas_from_memory.restype = ctypes.c_int
  625. return _lib.caca_import_canvas_from_memory(self, data, length, fmt)
  626. class NullCanvas(_Canvas):
  627. """ Represent a NULL canvas_t, eg to use as canvas mask for blit operations.
  628. """
  629. def __str__(self):
  630. return "<NullCanvas>"
  631. class CanvasError(Exception):
  632. pass