25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1014 lines
35 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):
  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. return _lib.caca_blit(self, x, y, cv, mask)
  211. def set_boundaries(self, x, y, width, height):
  212. """ Set a canvas' new boundaries.
  213. x -- X coordinate of the top-left corner
  214. y -- Y coordinate of the top-left corner
  215. width -- width of the box
  216. height -- height of the box
  217. """
  218. _lib.caca_set_canvas_boundaries.argtypes = [
  219. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
  220. ]
  221. _lib.caca_set_canvas_boundaries.restype = ctypes.c_int
  222. return _lib.caca_set_canvas_boundaries(self, x, y, width, height)
  223. def disable_dirty_rect(self):
  224. """ Disable dirty rectangles.
  225. """
  226. _lib.caca_disable_dirty_rect.argtypes = [_Canvas]
  227. _lib.caca_disable_dirty_rect.restype = ctypes.c_int
  228. return _lib.caca_disable_dirty_rect(self)
  229. def enable_dirty_rect(self):
  230. """ Enable dirty rectangles.
  231. """
  232. _lib.caca_enable_dirty_rect.argtypes = [_Canvas]
  233. _lib.caca_enable_dirty_rect.restype = ctypes.c_int
  234. return _lib.caca_enable_dirty_rect(self)
  235. def get_dirty_rect_count(self):
  236. """ Get the number of dirty rectangles in the canvas.
  237. """
  238. _lib.caca_get_dirty_rect_count.argtypes = [_Canvas]
  239. _lib.caca_get_dirty_rect_count.restype = ctypes.c_int
  240. return _lib.caca_get_dirty_rect_count(self)
  241. def get_dirty_rect(self, idx):
  242. """ Get a canvas's dirty rectangle. Return python dictionnary with
  243. coords as keys: x, y, width, height.
  244. idx -- the requested rectangle index
  245. """
  246. #initialize dictionnary and pointers
  247. dct = None
  248. x = ctypes.c_int()
  249. y = ctypes.c_int()
  250. width = ctypes.c_int()
  251. height = ctypes.c_int()
  252. _lib.caca_get_dirty_rect.argtypes = [
  253. _Canvas, ctypes.c_int,
  254. ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int),
  255. ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)
  256. ]
  257. _lib.caca_get_dirty_rect.restype = ctypes.c_int
  258. if _lib.caca_get_dirty_rect(self, idx, x, y, width, height) > -1:
  259. dct = {
  260. 'x': x.value, 'y': y.value,
  261. 'width': width.value, 'height': height.value,
  262. }
  263. return dct
  264. def add_dirty_rect(self, x, y, width, height):
  265. """ Add an area to the canvas's dirty rectangle list.
  266. x -- the leftmost edge of the additional dirty rectangle
  267. y -- the topmost edge of the additional dirty rectangle
  268. width -- the width of the additional dirty rectangle
  269. height -- the height of the additional dirty rectangle
  270. """
  271. _lib.caca_add_dirty_rect.argtypes = [
  272. _Canvas, ctypes.c_int, ctypes.c_int,
  273. ctypes.c_int, ctypes.c_int
  274. ]
  275. _lib.caca_add_dirty_rect.restype = ctypes.c_int
  276. return _lib.caca_add_dirty_rect(self, x, y, width, height)
  277. def remove_dirty_rect(self, x, y, width, height):
  278. """ Remove an area from the dirty rectangle list.
  279. x -- the leftmost edge of the additional dirty rectangle
  280. y -- the topmost edge of the additional dirty rectangle
  281. width -- the width of the additional rectangle
  282. height -- the height of the additional dirty rectangle
  283. """
  284. _lib.caca_remove_dirty_rect.argtypes = [
  285. _Canvas, ctypes.c_int, ctypes.c_int,
  286. ctypes.c_int, ctypes.c_int
  287. ]
  288. _lib.caca_remove_dirty_rect.restype = ctypes.c_int
  289. return _lib.caca_remove_dirty_rect(self, x, y, height, width)
  290. def clear_dirty_rect_list(self):
  291. """ Clear a canvas's dirty rectangle list.
  292. """
  293. _lib.caca_clear_dirty_rect_list.argtypes = [_Canvas]
  294. _lib.caca_clear_dirty_rect_list.restype = ctypes.c_int
  295. return _lib.caca_clear_dirty_rect_list(self)
  296. def invert(self):
  297. """ Invert a canvas' colours.
  298. """
  299. _lib.caca_invert.argtypes = [_Canvas]
  300. _lib.caca_invert.restype = ctypes.c_int
  301. return _lib.caca_invert(self)
  302. def flip(self):
  303. """ Flip a canvas horizontally.
  304. """
  305. _lib.caca_flip.argtypes = [_Canvas]
  306. _lib.caca_flip.restype = ctypes.c_int
  307. return _lib.caca_flip(self)
  308. def flop(self):
  309. """ Flip a canvas vertically.
  310. """
  311. _lib.caca_flop.argtypes = [_Canvas]
  312. _lib.caca_flop.restype = ctypes.c_int
  313. return _lib.caca_flop(self)
  314. def rotate_180(self):
  315. """ Rotate a canvas.
  316. """
  317. _lib.caca_rotate_180.argtypes = [_Canvas]
  318. _lib.caca_rotate_180.restype = ctypes.c_int
  319. return _lib.caca_rotate_180(self)
  320. def rotate_left(self):
  321. """ Rotate a canvas, 90 degrees counterclockwise.
  322. """
  323. _lib.caca_rotate_left.argtypes = [_Canvas]
  324. _lib.caca_rotate_left.restype = ctypes.c_int
  325. return _lib.caca_rotate_left(self)
  326. def rotate_right(self):
  327. """ Rotate a canvas, 90 degrees clockwise.
  328. """
  329. _lib.caca_rotate_right.argtypes = [_Canvas]
  330. _lib.caca_rotate_right.restype = ctypes.c_int
  331. return _lib.caca_rotate_right(self)
  332. def stretch_left(self):
  333. """ Rotate and stretch a canvas, 90 degrees counterclockwise.
  334. """
  335. _lib.caca_stretch_left.argtypes = [_Canvas]
  336. _lib.caca_stretch_left.restype = ctypes.c_int
  337. return _lib.caca_stretch_left(self)
  338. def stretch_right(self):
  339. """ Rotate and stretch a canvas, 90 degrees clockwise.
  340. """
  341. _lib.caca_stretch_right.argtypes = [_Canvas]
  342. _lib.caca_stretch_right.restype = ctypes.c_int
  343. return _lib.caca_stretch_right(self)
  344. def get_attr(self, x, y):
  345. """ Get the text attribute at the given coordinates.
  346. x -- X coordinate
  347. y -- Y coordinate
  348. """
  349. _lib.caca_get_attr.argtypes = [_Canvas, ctypes.c_int, ctypes.c_int]
  350. _lib.caca_get_attr.restype = ctypes.c_uint32
  351. return _lib.caca_get_attr(self, x, y)
  352. def set_attr(self, attr):
  353. """ Set the default character attribute.
  354. attr -- the requested attribute value
  355. """
  356. _lib.caca_set_attr.argtypes = [_Canvas, ctypes.c_uint32]
  357. _lib.caca_set_attr.restype = ctypes.c_int
  358. return _lib.caca_set_attr(self, attr)
  359. def put_attr(self, x, y, attr):
  360. """ Set the character attribute at the given coordinates.
  361. x -- X coordinate
  362. y -- Y coordinate
  363. attr -- the requested attribute value
  364. """
  365. _lib.caca_put_attr.argtypes = [
  366. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  367. ]
  368. _lib.caca_put_attr.restype = ctypes.c_int
  369. return _lib.caca_put_attr(self, x, y, attr)
  370. def set_color_ansi(self, fg, bg):
  371. """ Set the default colour pair for text (ANSI version).
  372. fg -- the requested ANSI foreground colour.
  373. bg -- the requested ANSI background colour.
  374. """
  375. _lib.caca_set_color_ansi.argtypes = [_Canvas, ctypes.c_uint8, ctypes.c_uint8]
  376. _lib.caca_set_color_ansi.restype = ctypes.c_int
  377. return _lib.caca_set_color_ansi(self, fg, bg)
  378. def set_color_argb(self, fg, bg):
  379. """ Set the default colour pair for text (truecolor version).
  380. fg -- the requested ARGB foreground colour.
  381. bg -- the requested ARGB background colour.
  382. """
  383. _lib.caca_set_color_argb.argtypes = [
  384. _Canvas, ctypes.c_uint16, ctypes.c_uint16
  385. ]
  386. _lib.caca_set_color_argb.restype = ctypes.c_int
  387. return _lib.caca_set_color_argb(self, fg, bg)
  388. def draw_line(self, x1, y1, x2, y2, ch):
  389. """ Draw a line on the canvas using the given character.
  390. x1 -- X coordinate of the first point
  391. y1 -- Y coordinate of the first point
  392. x2 -- X coordinate of the second point
  393. y2 -- Y coordinate of the second point
  394. ch -- character to be used to draw the line
  395. """
  396. _lib.caca_draw_line.argtypes = [
  397. _Canvas, ctypes.c_int, ctypes.c_int,
  398. ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  399. ]
  400. _lib.caca_draw_line.restype = ctypes.c_int
  401. return _lib.caca_draw_line(self, x1, y1, x2, y2, ord(ch))
  402. def draw_polyline(self, array_x, array_y, n, ch):
  403. """ Draw a polyline.
  404. array_x -- Array of X coordinates, must have n+1 elements
  405. array-y -- Array of Y coordinates, must have n+1 elements
  406. n -- Number of lines to draw
  407. ch -- character to be used to draw the line
  408. """
  409. _lib.caca_draw_polyline.argtypes = [
  410. _Canvas, ctypes.c_int * n, ctypes.c_int * n, ctypes.c_int, ctypes.c_uint32
  411. ]
  412. _lib.caca_draw_polyline.restype = ctypes.c_int
  413. return _lib.caca_draw_polyline(self, array_x, array_y, n, ord(ch))
  414. def draw_thin_line(self, x1, y1, x2, y2):
  415. """ Draw a thin line on the canvas, using ASCII art.
  416. x1 -- X coordinate of the first point
  417. y1 -- Y coordinate of the first point
  418. x2 -- X coordinate of the second point
  419. y2 -- Y coordinate of the second point
  420. """
  421. _lib.caca_draw_thin_line.argtypes = [
  422. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
  423. ]
  424. _lib.caca_draw_thin_line.restype = ctypes.c_int
  425. return _lib.caca_draw_thin_line(self, x1, y1, x2, y2)
  426. def draw_thin_polyline(self, array_x, array_y, n):
  427. """ Draw an ASCII art thin polyline.
  428. array_x -- Array of X coordinates, must have n+1 elements
  429. array_y -- Array of Y coordinates, must have n+1 elements
  430. n -- Number of lines to draw
  431. """
  432. _lib.caca_draw_thin_polyline.argtypes = [
  433. Canvas, ctypes.c_int * n, ctypes.c_int * n, ctypes.c_int
  434. ]
  435. _lib.caca_draw_thin_polyline.restype = ctypes.c_int
  436. return _lib.caca_draw_thin_polyline(self, array_x, array_y, n)
  437. def draw_circle(self, x, y, r, ch):
  438. """ Draw a circle on the canvas using the given character.
  439. x -- center X coordinate
  440. y -- center Y coordinate
  441. r -- circle radius
  442. ch -- the UTF-32 character to be used to draw the circle outline
  443. """
  444. _lib.caca_draw_circle.argtypes = [
  445. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  446. ]
  447. _lib.caca_draw_circle.restype = ctypes.c_int
  448. return _lib.caca_draw_circle(self, x, y, r, ord(ch))
  449. def draw_ellipse(self, xo, yo, a, b, ch):
  450. """ Draw an ellipse on the canvas using the given character.
  451. xo -- center X coordinate
  452. yo -- center Y coordinate
  453. a -- ellipse x radius
  454. b -- ellipse y radius
  455. ch -- UTF-32 character to be used to draw the ellipse outline
  456. """
  457. _lib.caca_draw_ellipse.argtypes = [
  458. _Canvas, ctypes.c_int, ctypes.c_int,
  459. ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  460. ]
  461. _lib.caca_draw_ellipse.restype = ctypes.c_int
  462. return _lib.caca_draw_ellipse(self, xo, yo, a, b, ord(ch))
  463. def draw_thin_ellipse(self, xo, yo, a, b):
  464. """ Draw a thin ellipse on the canvas.
  465. xo -- center X coordinate
  466. yo -- center Y coordinate
  467. a -- ellipse X radius
  468. b -- ellipse Y radius
  469. """
  470. _lib.caca_draw_thin_ellipse.argtypes = [
  471. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
  472. ]
  473. _lib.caca_draw_thin_ellipse.restype = ctypes.c_int
  474. return _lib.caca_draw_thin_ellipse(self, xo, yo, a, b)
  475. def fill_ellipse(self, xo, yo, a, b, ch):
  476. """ Fill an ellipse on the canvas using the given character.
  477. xo -- center X coordinate
  478. yo -- center Y coordinate
  479. a -- ellipse X radius
  480. b -- ellipse Y radius
  481. ch -- UTF-32 character to be used to fill the ellipse
  482. """
  483. _lib.caca_fill_ellipse.argtypes = [
  484. _Canvas, ctypes.c_int, ctypes.c_int,
  485. ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  486. ]
  487. _lib.caca_fill_ellipse.restype = ctypes.c_int
  488. return _lib.caca_fill_ellipse(self, xo, yo, a, b, ord(ch))
  489. def draw_box(self, x, y, width, height, ch):
  490. """ Draw a box on the canvas using the given character.
  491. x -- X coordinate of the upper-left corner of the box
  492. y -- Y coordinate of the upper-left corner of the box
  493. width -- width of the box
  494. height -- height of the box
  495. ch -- character to be used to draw the box
  496. """
  497. _lib.caca_draw_box.argtypes = [
  498. Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  499. ]
  500. _lib.caca_draw_box.restype = ctypes.c_int
  501. return _lib.caca_draw_box(self, x, y, width, height, ord(ch))
  502. def draw_thin_box(self, x, y, width, height):
  503. """ Draw a thin box on the canvas.
  504. x -- X coordinate of the upper-left corner of the box
  505. y -- Y coordinate of the upper-left corner of the box
  506. width -- width of the box
  507. height -- height of the box
  508. """
  509. _lib.caca_draw_thin_box.argtypes = [
  510. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
  511. ]
  512. _lib.caca_draw_thin_box.restype = ctypes.c_int
  513. return _lib.caca_draw_thin_box(self, x, y, width, height)
  514. def draw_cp437_box(self, x, y, width, height):
  515. """ Draw a box on the canvas using CP437 characters.
  516. x -- X coordinate of the upper-left corner box
  517. y -- Y coordinate of the upper-left corner box
  518. width -- width of the box
  519. height -- height of the box
  520. """
  521. _lib.caca_draw_cp437_box.argtypes = [
  522. _Canvas, ctypes.c_int, ctypes.c_int,
  523. ctypes.c_int, ctypes.c_int
  524. ]
  525. _lib.caca_draw_cp437_box.restype = ctypes.c_int
  526. return _lib.caca_draw_cp437_box(self, x, y, width, height)
  527. def fill_box(self, x, y, width, height, ch):
  528. """ Fill a box on the canvas using the given character.
  529. x -- X coordinate of the upper-left corner of the box
  530. y -- Y coordinate of the upper-left corner of the box
  531. width -- width of the box
  532. height -- height of the box
  533. ch -- UFT-32 character to be used to fill the box
  534. """
  535. _lib.caca_fill_box.argtypes = [
  536. _Canvas, ctypes.c_int, ctypes.c_int,
  537. ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  538. ]
  539. _lib.caca_fill_box.restype = ctypes.c_int
  540. return _lib.caca_fill_box(self, x, y, width, height, ord(ch))
  541. def draw_triangle(self, x1, y1, x2, y2, x3, y3, ch):
  542. """ Draw a triangle on the canvas using the given character.
  543. x1 -- X coordinate of the first point
  544. y1 -- Y coordinate of the first point
  545. x2 -- X coordinate of the second point
  546. y2 -- Y coordinate of the second point
  547. x3 -- X coordinate of the third point
  548. y3 -- Y coordinate of the third point
  549. ch -- UTF-32 character to be used to draw the triangle outline
  550. """
  551. _lib.caca_draw_triangle.argtypes = [
  552. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int,
  553. ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint32
  554. ]
  555. _lib.caca_draw_triangle.restype = ctypes.c_int
  556. return _lib.caca_draw_triangle(self, x1, y1, x2, y2, x3, y3, ord(ch))
  557. def draw_thin_triangle(self, x1, y1, x2, y2, x3, y3):
  558. """ Draw a thin triangle on the canvas.
  559. """
  560. _lib.caca_draw_thin_triangle.argtypes = [
  561. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int,
  562. ctypes.c_int, ctypes.c_int, ctypes.c_int
  563. ]
  564. _lib.caca_draw_thin_triangle.restype = ctypes.c_int
  565. return _lib.caca_draw_thin_triangle(self, x1, y1, x2, y2, x3, y3)
  566. def fill_triangle(self, x1, y1, x2, y2, x3, y3, ch):
  567. """ Fill a triangle on the canvas using the given character.
  568. x1 -- X coordinate of the first point
  569. y1 -- Y coordinate of the first point
  570. x2 -- X coordinate of the second point
  571. y2 -- Y coordinate of the second point
  572. x3 -- X coordinate of the second point
  573. y3 -- Y coordinate of the second point
  574. ch -- UTF-32 character to be used to fill the triangle
  575. """
  576. _lib.caca_fill_triangle.argtypes = [
  577. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int,
  578. ctypes.c_int, ctypes.c_int, ctypes.c_int
  579. ]
  580. _lib.caca_fill_triangle.restype = ctypes.c_int
  581. return _lib.caca_fill_triangle(self, x1, y1, x2, y2, x3, y3, ord(ch))
  582. def fill_triangle_textured(self, coords, tex, uv):
  583. """ Fill a triangle on the canvas using an arbitrary-sized texture.
  584. coords -- coordinates of the triangle (3{x,y})
  585. tex -- the handle of the canvas texture
  586. uv -- coordinates of the texture (3{u,v})
  587. """
  588. _lib.caca_fill_triangle_textured.argtypes = [
  589. _Canvas, ctypes.c_int * 6, _Canvas, ctypes.c_int * 6
  590. ]
  591. _lib.caca_fill_triangle_textured.restype = ctypes.c_int
  592. return _lib.caca_fill_triangle_textured(self, coords, tex, uv)
  593. def get_frame_count(self):
  594. """ Get the number of frames in a canvas.
  595. """
  596. _lib.caca_get_frame_count.argtypes = [_Canvas]
  597. _lib.caca_get_frame_count.restype = ctypes.c_int
  598. return _lib.caca_get_frame_count(self)
  599. def set_frame(self, idx):
  600. """ Activate a given canvas frame.
  601. idx -- the canvas frame to activate
  602. """
  603. _lib.caca_set_frame.argtypes = [_Canvas, ctypes.c_int]
  604. _lib.caca_set_frame.restype = ctypes.c_int
  605. return _lib.caca_set_frame(self, idx)
  606. def get_frame_name(self):
  607. """ Get the current frame's name.
  608. """
  609. _lib.caca_get_frame_name.argtypes = [_Canvas]
  610. _lib.caca_get_frame_name.restype = ctypes.c_char_p
  611. return _lib.caca_get_frame_name(self)
  612. def set_frame_name(self, name):
  613. """ Set the current frame's name.
  614. name -- the name to give to the current frame
  615. """
  616. _lib.caca_set_frame_name.argtypes = [_Canvas, ctypes.c_char_p]
  617. _lib.caca_set_frame_name.restype = ctypes.c_int
  618. return _lib.caca_set_frame_name(self, name)
  619. def create_frame(self, idx):
  620. """ Add a frame to a canvas.
  621. idx -- the index where to insert the new frame
  622. """
  623. _lib.caca_create_frame.argtypes = [_Canvas, ctypes.c_int]
  624. _lib.caca_create_frame.restype = ctypes.c_int
  625. return _lib.caca_create_frame(self, idx)
  626. def free_frame(self, idx):
  627. """ Remove a frame from a canvas.
  628. idx -- the index of the frame to delete
  629. """
  630. _lib.caca_free_frame.argtypes = [_Canvas, ctypes.c_int]
  631. _lib.caca_free_frame.restype = ctypes.c_int
  632. return _lib.caca_free_frame(self, idx)
  633. def import_from_memory(self, data, fmt):
  634. """ Import a memory buffer into a canvas.
  635. data -- a memory area containing the data to be loaded into the canvas
  636. fmt -- a string describing the input format
  637. Valid values for format are:
  638. - "": attempt to autodetect the file format.
  639. - caca: import native libcaca files.
  640. - text: import ASCII text files.
  641. - ansi: import ANSI files.
  642. - utf8: import UTF-8 files with ANSI colour codes.
  643. """
  644. #set data size
  645. length = ctypes.c_size_t(len(data))
  646. _lib.caca_import_canvas_from_memory.argtypes = [
  647. Canvas, ctypes.c_char_p, ctypes.c_size_t, ctypes.c_char_p
  648. ]
  649. _lib.caca_import_canvas_from_memory.restype = ctypes.c_int
  650. return _lib.caca_import_canvas_from_memory(self, data, length, fmt)
  651. def import_from_file(self, filename, fmt):
  652. """ Import a file into a canvas.
  653. filename -- the name of the file to load
  654. fmt -- a string describing the input format
  655. Valid values for format are:
  656. - "": attempt to autodetect the file format.
  657. - caca: import native libcaca files.
  658. - text: import ASCII text files.
  659. - ansi: import ANSI files.
  660. - utf8: import UTF-8 files with ANSI colour codes.
  661. """
  662. _lib.caca_import_canvas_from_file.argtypes = [
  663. _Canvas, ctypes.c_char_p, ctypes.c_char_p
  664. ]
  665. _lib.caca_import_canvas_from_file.restype = ctypes.c_int
  666. return _lib.caca_import_canvas_from_file(self, filename, fmt)
  667. def import_area_from_memory(self, x, y, data, fmt):
  668. """ Import a memory buffer into a canvas area.
  669. x -- the leftmost coordinate of the area to import to
  670. y -- the topmost coordinate of the area to import to
  671. data -- a memory area containing the data to be loaded into the canvas
  672. fmt -- a string describing the input format
  673. Valid values for format are:
  674. - "": attempt to autodetect the file format.
  675. - caca: import native libcaca files.
  676. - text: import ASCII text files.
  677. - ansi: import ANSI files.
  678. - utf8: import UTF-8 files with ANSI colour codes.
  679. """
  680. #set data size
  681. length = ctypes.c_size_t(len(data))
  682. _lib.caca_import_area_from_memory.argtypes = [
  683. _Canvas, ctypes.c_int, ctypes.c_int,
  684. ctypes.c_char_p, ctypes.c_size_t, ctypes.c_char_p
  685. ]
  686. _lib.caca_import_area_from_memory.restype = ctypes.c_int
  687. return _lib.caca_import_area_from_memory(self, x, y, data, length, fmt)
  688. def import_area_from_file(self, x, y, filename, fmt):
  689. """ Import a file into a canvas area.
  690. x -- the leftmost coordinate of the area to import to
  691. y -- the topmost coordinate of the area to import to
  692. filename -- the name of the file to be load
  693. fmt -- a string describing the input format
  694. Valid values for format are:
  695. - "": attempt to autodetect the file format.
  696. - caca: import native libcaca files.
  697. - text: import ASCII text files.
  698. - ansi: import ANSI files.
  699. - utf8: import UTF-8 files with ANSI colour codes.
  700. """
  701. _lib.caca_import_area_from_file.argtypes = [
  702. _Canvas, ctypes.c_int, ctypes.c_int,
  703. ctypes.c_char_p, ctypes.c_char_p
  704. ]
  705. _lib.caca_import_area_from_file.restype = ctypes.c_int
  706. return _lib.caca_import_area_from_file(self, x, y, filename, fmt)
  707. def export_to_memory(self, fmt):
  708. """ Export a canvas into a foreign format.
  709. fmt -- a string describing the output format
  710. Valid values for format are:
  711. - caca: export native libcaca files.
  712. - ansi: export ANSI art (CP437 charset with ANSI colour codes).
  713. - html: export an HTML page with CSS information.
  714. - html3: export an HTML table that should be compatible with
  715. most navigators, including textmode ones.
  716. - irc: export UTF-8 text with mIRC colour codes.
  717. - ps: export a PostScript document.
  718. - svg: export an SVG vector image.
  719. - tga: export a TGA image.
  720. """
  721. #initialize pointer
  722. p_size_t = ctypes.POINTER(ctypes.c_size_t)
  723. _lib.caca_export_canvas_to_memory.argtypes = [
  724. _Canvas, ctypes.c_char_p, p_size_t
  725. ]
  726. _lib.caca_export_canvas_to_memory.restype = ctypes.POINTER(ctypes.c_char_p)
  727. p = ctypes.c_size_t()
  728. ret = _lib.caca_export_canvas_to_memory(self, fmt, p)
  729. return ctypes.string_at(ret, p.value)
  730. def export_area_to_memory(self, x, y, width, height, fmt):
  731. """ Export a canvas portion into a foreign format.
  732. x -- the leftmost coordinate of the area to export
  733. y -- the topmost coordinate of the area to export
  734. width -- the width of the area to export
  735. height -- the height of the area to export
  736. fmt -- a string describing the output format
  737. Valid values for format are:
  738. - caca: export native libcaca files.
  739. - ansi: export ANSI art (CP437 charset with ANSI colour codes).
  740. - html: export an HTML page with CSS information.
  741. - html3: export an HTML table that should be compatible with
  742. most navigators, including textmode ones.
  743. - irc: export UTF-8 text with mIRC colour codes.
  744. - ps: export a PostScript document.
  745. - svg: export an SVG vector image.
  746. - tga: export a TGA image.
  747. """
  748. #initialize pointer
  749. p_size_t = ctypes.POINTER(ctypes.c_size_t)
  750. _lib.caca_export_area_to_memory.argtypes = [
  751. _Canvas, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int,
  752. ctypes.c_char_p, p_size_t
  753. ]
  754. _lib.caca_export_area_to_memory.restype = ctypes.POINTER(ctypes.c_char_p)
  755. p = ctypes.c_size_t()
  756. ret = _lib.caca_export_area_to_memory(self, x, y, width, height, fmt, p)
  757. return ctypes.string_at(ret, p.value)
  758. def set_figfont(self, filename):
  759. """ Load a figfont and attach it to a canvas.
  760. filename -- the figfont file to load.
  761. """
  762. _lib.caca_canvas_set_figfont.argtypes = [_Canvas, ctypes.c_char_p]
  763. _lib.caca_canvas_set_figfont.restype = ctypes.c_int
  764. return _lib.caca_canvas_set_figfont(self, filename)
  765. def put_figchar(self, ch):
  766. """ Paste a character using the current figfont.
  767. ch -- the character to paste
  768. """
  769. _lib.caca_put_figchar.argtypes = [_Canvas, ctypes.c_uint32]
  770. _lib.caca_put_figchar.restype = ctypes.c_int
  771. return _lib.caca_put_figchar(self, ord(ch))
  772. def flush_figlet(self):
  773. """ Flush the figlet context
  774. """
  775. _lib.caca_flush_figlet.argtypes = [_Canvas]
  776. _lib.caca_flush_figlet.restype = ctypes.c_int
  777. return _lib.caca_flush_figlet(self)
  778. def render(self, font, buf, width, height, pitch):
  779. """ Render the canvas onto an image buffer.
  780. font -- a Font() object
  781. buf -- the image buffer
  782. width -- the width (in pixels) of the image
  783. heigth -- the height (in pixels) of the image
  784. pitch -- the pitch (in bytes) of the image
  785. """
  786. _lib.caca_render_canvas.argtypes = [
  787. _Canvas, _Font, ctypes.c_char_p,
  788. ctypes.c_int, ctypes.c_int, ctypes.c_int
  789. ]
  790. _lib.caca_render_canvas.restype = ctypes.c_int
  791. return _lib.caca_render_canvas(self, font, buf, width, height, pitch)
  792. class NullCanvas(_Canvas):
  793. """ Represent a NULL canvas_t, eg to use as canvas mask for blit operations.
  794. """
  795. def __str__(self):
  796. return "<NullCanvas>"
  797. class CanvasError(Exception):
  798. pass