You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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