25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

359 satır
8.9 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, _PYTHON3, _bytes_to_str
  17. #color constants
  18. COLOR_BLACK = 0x00
  19. COLOR_BLUE = 0x01
  20. COLOR_GREEN = 0x02
  21. COLOR_CYAN = 0x03
  22. COLOR_RED = 0x04
  23. COLOR_MAGENTA = 0x05
  24. COLOR_BROWN = 0x06
  25. COLOR_LIGHTGRAY = 0x07
  26. COLOR_DARKGRAY = 0x08
  27. COLOR_LIGHTBLUE = 0x09
  28. COLOR_LIGHTGREEN = 0x0a
  29. COLOR_LIGHTCYAN = 0x0b
  30. COLOR_LIGHTRED = 0x0c
  31. COLOR_LIGHTMAGENTA = 0x0d
  32. COLOR_YELLOW = 0x0e
  33. COLOR_WHITE = 0x0f
  34. COLOR_DEFAULT = 0x10
  35. COLOR_TRANSPARENT = 0x20
  36. #styles constants
  37. STYLE_BOLD = 0x01
  38. STYLE_ITALICS = 0x02
  39. STYLE_UNDERLINE = 0x04
  40. STYLE_BLINK = 0x08
  41. #key constants
  42. EVENT_NONE = 0x0000
  43. EVENT_KEY_PRESS = 0x0001
  44. EVENT_KEY_RELEASE = 0x0002
  45. EVENT_MOUSE_PRESS = 0x0004
  46. EVENT_MOUSE_RELEASE = 0x0008
  47. EVENT_MOUSE_MOTION = 0x0010
  48. EVENT_RESIZE = 0x0020
  49. EVENT_QUIT = 0x0040
  50. EVENT_ANY = 0xffff
  51. #event constants
  52. KEY_UNKNOWN = 0x00
  53. KEY_CTRL_A = 0x01
  54. KEY_CTRL_B = 0x02
  55. KEY_CTRL_C = 0x03
  56. KEY_CTRL_D = 0x04
  57. KEY_CTRL_E = 0x05
  58. KEY_CTRL_F = 0x06
  59. KEY_CTRL_G = 0x07
  60. KEY_BACKSPACE = 0x08
  61. KEY_TAB = 0x09
  62. KEY_CTRL_J = 0x0a
  63. KEY_CTRL_K = 0x0b
  64. KEY_CTRL_L = 0x0c
  65. KEY_RETURN = 0x0d
  66. KEY_CTRL_N = 0x0e
  67. KEY_CTRL_O = 0x0f
  68. KEY_CTRL_P = 0x10
  69. KEY_CTRL_Q = 0x11
  70. KEY_CTRL_R = 0x12
  71. KEY_PAUSE = 0x13
  72. KEY_CTRL_T = 0x14
  73. KEY_CTRL_U = 0x15
  74. KEY_CTRL_V = 0x16
  75. KEY_CTRL_W = 0x17
  76. KEY_CTRL_X = 0x18
  77. KEY_CTRL_Y = 0x19
  78. KEY_CTRL_Z = 0x1a
  79. KEY_ESCAPE = 0x1b
  80. KEY_DELETE = 0x7f
  81. KEY_UP = 0x111
  82. KEY_DOWN = 0x112
  83. KEY_LEFT = 0x113
  84. KEY_RIGHT = 0x114
  85. KEY_INSERT = 0x115
  86. KEY_HOME = 0x116
  87. KEY_END = 0x117
  88. KEY_PAGEUP = 0x118
  89. KEY_PAGEDOWN = 0x119
  90. KEY_F1 = 0x11a
  91. KEY_F2 = 0x11b
  92. KEY_F3 = 0x11c
  93. KEY_F4 = 0x11d
  94. KEY_F5 = 0x11e
  95. KEY_F6 = 0x11f
  96. KEY_F7 = 0x120
  97. KEY_F8 = 0x121
  98. KEY_F9 = 0x122
  99. KEY_F10 = 0x123
  100. KEY_F11 = 0x124
  101. KEY_F12 = 0x125
  102. KEY_F13 = 0x126
  103. KEY_F14 = 0x127
  104. KEY_F15 = 0x128
  105. def get_version():
  106. """ Return string with libcaca version information.
  107. """
  108. _lib.caca_get_version.restype = ctypes.c_char_p
  109. if _PYTHON3:
  110. return _bytes_to_str(_lib.caca_get_version())
  111. else:
  112. return _lib.caca_get_version()
  113. def get_display_driver_list():
  114. """ Return a list of available drivers as tuple (name, description).
  115. """
  116. tmplst = []
  117. retlst = []
  118. _lib.caca_get_display_driver_list.restype = ctypes.POINTER(ctypes.c_char_p)
  119. for item in _lib.caca_get_display_driver_list():
  120. if item is not None and item != "":
  121. if _PYTHON3:
  122. tmplst.append(_bytes_to_str(item))
  123. else:
  124. tmplst.append(item)
  125. else:
  126. #memory error occured otherwise
  127. break
  128. for i in range(0, len(tmplst)):
  129. if i % 2 == 0:
  130. retlst.append((tmplst[i], tmplst[i+1]))
  131. del tmplst
  132. return retlst
  133. def get_export_list():
  134. """ Return list of available export formats as tuple (name, description).
  135. """
  136. tmplst = []
  137. retlst = []
  138. _lib.caca_get_export_list.restype = ctypes.POINTER(ctypes.c_char_p)
  139. for item in _lib.caca_get_export_list():
  140. if item is not None and item != "":
  141. if _PYTHON3:
  142. tmplst.append(_bytes_to_str(item))
  143. else:
  144. tmplst.append(item)
  145. else:
  146. #memory error occured otherwise
  147. break
  148. for i in range(0, len(tmplst)):
  149. if i % 2 == 0:
  150. retlst.append((tmplst[i], tmplst[i+1]))
  151. del tmplst
  152. return retlst
  153. def get_import_list():
  154. """ Return list of available import formats as tuple (name, description).
  155. """
  156. tmplst = []
  157. retlst = []
  158. _lib.caca_get_import_list.restype = ctypes.POINTER(ctypes.c_char_p)
  159. autodetect = False
  160. for item in _lib.caca_get_import_list():
  161. if item is not None:
  162. if item == "":
  163. if not autodetect:
  164. if _PYTHON3:
  165. tmplst.append(_bytes_to_str("\"\""))
  166. else:
  167. tmplst.append("\"\"")
  168. autodetect = True
  169. else:
  170. #memory error occured otherwise
  171. break
  172. else:
  173. if _PYTHON3:
  174. tmplst.append(_bytes_to_str(item))
  175. else:
  176. tmplst.append(item)
  177. else:
  178. #memory error occured otherwise
  179. break
  180. for i in range(0, len(tmplst)):
  181. if i % 2 == 0:
  182. retlst.append((tmplst[i], tmplst[i+1]))
  183. del tmplst
  184. return retlst
  185. def get_font_list():
  186. """ Return a list of available fonts.
  187. """
  188. fl = []
  189. _lib.caca_get_font_list.restype = ctypes.POINTER(ctypes.c_char_p)
  190. for item in _lib.caca_get_font_list():
  191. if item is not None and item != "":
  192. if _PYTHON3:
  193. fl.append(_bytes_to_str(item))
  194. else:
  195. fl.append(item)
  196. else:
  197. #memory error occured otherwise
  198. break
  199. return fl
  200. def rand(range_min, range_max):
  201. """ Generate a random integer within a range.
  202. range_min -- the lower bound of the integer range
  203. range_max __ the upper bound of the integer range
  204. """
  205. _lib.caca_rand.argtypes = [ctypes.c_int, ctypes.c_int]
  206. _lib.caca_rand.restype = ctypes.c_int
  207. return _lib.caca_rand(range_min, range_max)
  208. def attr_to_ansi(attr):
  209. """ Get DOS ANSI information from attribute.
  210. attr -- the requested attribute value
  211. """
  212. _lib.caca_attr_to_ansi.argtypes = [ctypes.c_uint32]
  213. _lib.caca_attr_to_ansi.restype = ctypes.c_uint8
  214. return _lib.caca_attr_to_ansi(attr)
  215. def attr_to_ansi_fg(attr):
  216. """ Get ANSI foreground information from attribute.
  217. attr -- the requested attribute value
  218. """
  219. _lib.caca_attr_to_ansi_fg.argtypes = [ctypes.c_uint32]
  220. _lib.caca_attr_to_ansi_fg.restype = ctypes.c_uint8
  221. return _lib.caca_attr_to_ansi_fg(attr)
  222. def attr_to_ansi_bg(attr):
  223. """ Get ANSI background information from attribute.
  224. attr -- the requested attribute value
  225. """
  226. _lib.caca_attr_to_ansi_bg.argtypes = [ctypes.c_uint32]
  227. _lib.caca_attr_to_ansi_bg.restype = ctypes.c_uint8
  228. return _lib.caca_attr_to_ansi_bg(attr)
  229. def attr_to_rgb12_fg(attr):
  230. """ Get 12-bit RGB foreground information from attribute.
  231. attr -- the requested attribute value
  232. """
  233. _lib.caca_attr_to_rgb12_fg.argtypes = [ctypes.c_uint32]
  234. _lib.caca_attr_to_rgb12_fg.restype = ctypes.c_uint16
  235. return _lib.caca_attr_to_rgb12_fg(attr)
  236. def attr_to_rgb12_bg(attr):
  237. """ Get 12-bit RGB background information from attribute.
  238. attr -- the requested attribute value
  239. """
  240. _lib.caca_attr_to_rgb12_bg.argtypes = [ctypes.c_uint32]
  241. _lib.caca_attr_to_rgb12_bg.restype = ctypes.c_uint16
  242. return _lib.caca_attr_to_rgb12_bg(attr)
  243. def utf8_to_utf32(ch):
  244. """ Convert a UTF-8 character to UTF-32.
  245. ch -- the character to convert
  246. """
  247. _lib.caca_utf8_to_utf32.argtypes = [ctypes.c_char_p,
  248. ctypes.POINTER(ctypes.c_size_t)
  249. ]
  250. _lib.caca_utf8_to_utf32.restype = ctypes.c_uint32
  251. return _lib.caca_utf8_to_utf32(ch, ctypes.c_ulong(0))
  252. def utf32_to_utf8(ch):
  253. """ Convert a UTF-32 character to UTF-8.
  254. ch -- the character to convert
  255. """
  256. _lib.caca_utf32_to_utf8.argtypes = [ctypes.c_char_p, ctypes.c_uint32]
  257. _lib.caca_utf32_to_utf8.restype = ctypes.c_int
  258. buf = ctypes.c_buffer(7)
  259. _lib.caca_utf32_to_utf8(buf, ch)
  260. if _PYTHON3:
  261. return _bytes_to_str(buf.raw).replace('\x00', '')
  262. else:
  263. return buf.raw.replace('\x00', '')
  264. def utf32_to_cp437(ch):
  265. """ Convert a UTF-32 character to CP437.
  266. ch -- the character to convert
  267. """
  268. _lib.caca_utf32_to_cp437.argtypes = [ctypes.c_uint32]
  269. _lib.caca_utf32_to_cp437.restype = ctypes.c_uint8
  270. return _lib.caca_utf32_to_cp437(ch)
  271. def cp437_to_utf32(ch):
  272. """ Convert a CP437 character to UTF-32.
  273. ch -- the character to convert
  274. """
  275. _lib.caca_cp437_to_utf8.argtypes = [ctypes.c_uint8]
  276. _lib.caca_cp437_to_utf8.restype = ctypes.c_uint32
  277. return _lib.caca_cp437_to_utf8(ch)
  278. def utf32_to_ascii(ch):
  279. """ Convert a UTF-32 character to ASCII.
  280. ch -- the character to convert
  281. """
  282. _lib.caca_utf32_to_ascii.argtypes = [ctypes.c_uint32]
  283. _lib.caca_utf32_to_ascii.restype = ctypes.c_uint8
  284. return _lib.caca_utf32_to_ascii(ch)
  285. def utf32_is_fullwidth(ch):
  286. """ Tell whether a UTF-32 character is fullwidth.
  287. ch -- the UTF-32 character
  288. """
  289. _lib.caca_utf32_is_fullwidth.argtypes = [ctypes.c_uint32]
  290. _lib.caca_utf32_is_fullwidth.restype = ctypes.c_int
  291. return _lib.caca_utf32_is_fullwidth(ch)