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

common.py 8.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #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. return _lib.caca_get_version()
  110. def get_display_driver_list():
  111. """ Return a list of available drivers as tuple (name, description).
  112. """
  113. tmplst = []
  114. retlst = []
  115. _lib.caca_get_display_driver_list.restype = ctypes.POINTER(ctypes.c_char_p)
  116. for item in _lib.caca_get_display_driver_list():
  117. if item is not None and item != "":
  118. tmplst.append(item)
  119. else:
  120. #memory error occured otherwise
  121. break
  122. for i in xrange(0, len(tmplst)):
  123. if i % 2 == 0:
  124. retlst.append((tmplst[i], tmplst[i+1]))
  125. del tmplst
  126. return retlst
  127. def get_export_list():
  128. """ Return list of available export formats as tuple (name, description).
  129. """
  130. tmplst = []
  131. retlst = []
  132. _lib.caca_get_export_list.restype = ctypes.POINTER(ctypes.c_char_p)
  133. for item in _lib.caca_get_export_list():
  134. if item is not None and item != "":
  135. tmplst.append(item)
  136. else:
  137. #memory error occured otherwise
  138. break
  139. for i in xrange(0, len(tmplst)):
  140. if i % 2 == 0:
  141. retlst.append((tmplst[i], tmplst[i+1]))
  142. del tmplst
  143. return retlst
  144. def get_import_list():
  145. """ Return list of available import formats as tuple (name, description).
  146. """
  147. tmplst = []
  148. retlst = []
  149. _lib.caca_get_import_list.restype = ctypes.POINTER(ctypes.c_char_p)
  150. autodetect = False
  151. for item in _lib.caca_get_import_list():
  152. if item is not None:
  153. if item == "":
  154. if not autodetect:
  155. tmplst.append("\"\"")
  156. autodetect = True
  157. else:
  158. #memory error occured otherwise
  159. break
  160. else:
  161. tmplst.append(item)
  162. else:
  163. #memory error occured otherwise
  164. break
  165. for i in xrange(0, len(tmplst)):
  166. if i % 2 == 0:
  167. retlst.append((tmplst[i], tmplst[i+1]))
  168. del tmplst
  169. return retlst
  170. def get_font_list():
  171. """ Return a list of available fonts.
  172. """
  173. fl = []
  174. _lib.caca_get_font_list.restype = ctypes.POINTER(ctypes.c_char_p)
  175. for item in _lib.caca_get_font_list():
  176. if item is not None and item != "":
  177. fl.append(item)
  178. else:
  179. #memory error occured otherwise
  180. break
  181. return fl
  182. def rand(range_min, range_max):
  183. """ Generate a random integer within a range.
  184. range_min -- the lower bound of the integer range
  185. range_max __ the upper bound of the integer range
  186. """
  187. _lib.caca_rand.argtypes = [ctypes.c_int, ctypes.c_int]
  188. _lib.caca_rand.restype = ctypes.c_int
  189. return _lib.caca_rand(range_min, range_max)
  190. def attr_to_ansi(attr):
  191. """ Get DOS ANSI information from attribute.
  192. attr -- the requested attribute value
  193. """
  194. _lib.caca_attr_to_ansi.argtypes = [ctypes.c_uint32]
  195. _lib.caca_attr_to_ansi.restype = ctypes.c_uint8
  196. return _lib.caca_attr_to_ansi(attr)
  197. def attr_to_ansi_fg(attr):
  198. """ Get ANSI foreground information from attribute.
  199. attr -- the requested attribute value
  200. """
  201. _lib.caca_attr_to_ansi_fg.argtypes = [ctypes.c_uint32]
  202. _lib.caca_attr_to_ansi_fg.restype = ctypes.c_uint8
  203. return _lib.caca_attr_to_ansi_fg(attr)
  204. def attr_to_ansi_bg(attr):
  205. """ Get ANSI background information from attribute.
  206. attr -- the requested attribute value
  207. """
  208. _lib.caca_attr_to_ansi_bg.argtypes = [ctypes.c_uint32]
  209. _lib.caca_attr_to_ansi_bg.restype = ctypes.c_uint8
  210. return _lib.caca_attr_to_ansi_bg(attr)
  211. def attr_to_rgb12_fg(attr):
  212. """ Get 12-bit RGB foreground information from attribute.
  213. attr -- the requested attribute value
  214. """
  215. _lib.caca_attr_to_rgb12_fg.argtypes = [ctypes.c_uint32]
  216. _lib.caca_attr_to_rgb12_fg.restype = ctypes.c_uint16
  217. return _lib.caca_attr_to_rgb12_fg(attr)
  218. def attr_to_rgb12_bg(attr):
  219. """ Get 12-bit RGB background information from attribute.
  220. attr -- the requested attribute value
  221. """
  222. _lib.caca_attr_to_rgb12_bg.argtypes = [ctypes.c_uint32]
  223. _lib.caca_attr_to_rgb12_bg.restype = ctypes.c_uint16
  224. return _lib.caca_attr_to_rgb12_bg(attr)
  225. def utf8_to_utf32(ch):
  226. """ Convert a UTF-8 character to UTF-32.
  227. ch -- the character to convert
  228. """
  229. _lib.caca_utf8_to_utf32.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_size_t)]
  230. _lib.caca_utf8_to_utf32.restype = ctypes.c_uint32
  231. return _lib.caca_utf8_to_utf32(ch, ctypes.c_ulong(0))
  232. def utf32_to_utf8(ch):
  233. """ Convert a UTF-32 character to UTF-8.
  234. ch -- the character to convert
  235. """
  236. _lib.caca_utf32_to_utf8.argtypes = [ctypes.c_char_p, ctypes.c_uint32]
  237. _lib.caca_utf32_to_utf8.restype = ctypes.c_int
  238. buf = ctypes.c_buffer(2)
  239. _lib.caca_utf32_to_utf8(buf, ch)
  240. return buf
  241. def utf32_to_cp437(ch):
  242. """ Convert a UTF-32 character to CP437.
  243. ch -- the character to convert
  244. """
  245. _lib.caca_utf32_to_cp437.argtypes = [ctypes.c_uint32]
  246. _lib.caca_utf32_to_cp437.restype = ctypes.c_uint8
  247. return _lib.caca_utf32_to_cp437(ch)
  248. def cp437_to_utf32(ch):
  249. """ Convert a CP437 character to UTF-32.
  250. ch -- the character to convert
  251. """
  252. _lib.caca_cp437_to_utf8.argtypes = [ctypes.c_uint8]
  253. _lib.caca_cp437_to_utf8.restype = ctypes.c_uint32
  254. return _lib.caca_cp437_to_utf8(ch)
  255. def utf32_to_ascii(ch):
  256. """ Convert a UTF-32 character to ASCII.
  257. ch -- the character to convert
  258. """
  259. _lib.caca_utf32_to_ascii.argtypes = [ctypes.c_uint32]
  260. _lib.caca_utf32_to_ascii.restype = ctypes.c_uint8
  261. return _lib.caca_utf32_to_ascii(ch)
  262. def utf32_is_fullwidth(ch):
  263. """ Tell whether a UTF-32 character is fullwidth.
  264. ch -- the UTF-32 character
  265. """
  266. _lib.caca_utf32_is_fullwidth.argtypes = [ctypes.c_uint32]
  267. _lib.caca_utf32_is_fullwidth.restype = ctypes.c_int
  268. return _lib.caca_utf32_is_fullwidth(ch)