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.

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