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.
 
 
 
 
 
 

468 lines
9.7 KiB

  1. <?php
  2. /*
  3. * php-caca Php binding for Libcaca
  4. * caca.php object layer for caca-php
  5. * Copyright (c) 2008 Vion Nicolas <nico@picapo.net>
  6. *
  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. class Canvas {
  15. private $cv;
  16. function importFile($path, $codec) {
  17. return caca_import_file($this->cv, $path, $codec);
  18. }
  19. function importString($codec) {
  20. return caca_import_string($this->cv, $codec);
  21. }
  22. function exportString($codec) {
  23. return caca_export_string($this->cv, $codec);
  24. }
  25. function freeFrame($id) {
  26. return caca_free_frame($this->cv, $id);
  27. }
  28. function frameCount() {
  29. return caca_get_frame_count($this->cv);
  30. }
  31. function createFrame($id) {
  32. return caca_create_frame($this->cv, $id);
  33. }
  34. function setFrameName($name) {
  35. return caca_set_frame_name($this->cv, $name);
  36. }
  37. function setFrame($id) {
  38. return caca_set_frame($this->cv, $id);
  39. }
  40. function putFigchar($char) {
  41. return caca_put_figchar($this->cv, $char);
  42. }
  43. function setFigfont($path) {
  44. return caca_canvas_set_figfont($this->cv, $path);
  45. }
  46. function putAttr($attr) {
  47. return caca_put_attr($this->cv, $attr);
  48. }
  49. function stretchRight() {
  50. return caca_stretch_right($this->cv);
  51. }
  52. function stretchLeft() {
  53. return caca_stretch_left($this->cv);
  54. }
  55. function setBoundaries($width, $height) {
  56. return caca_set_canvas_boundaries($this->cv, $width, $height);
  57. }
  58. function setHandle($x, $y) {
  59. return caca_set_canvas_handle($this->cv, $x, $y);
  60. }
  61. function getHandleX() {
  62. return caca_get_canvas_handle_x($this->cv);
  63. }
  64. function getHandleY() {
  65. return caca_get_canvas_handle_y($this->cv);
  66. }
  67. function getCursorX() {
  68. return caca_get_cursor_x($this->cv);
  69. }
  70. function getCursorY() {
  71. return caca_get_cursor_y($this->cv);
  72. }
  73. function getChars() {
  74. return caca_get_canvas_chars($this->cv);
  75. }
  76. function getAttrs() {
  77. return caca_get_canvas_attrs($this->cv);
  78. }
  79. function setSize($width, $height) {
  80. return caca_set_canvas_size($this->cv, $width, $height);
  81. }
  82. function getWidth() {
  83. return caca_get_canvas_width($this->cv);
  84. }
  85. function getHeight() {
  86. return caca_get_canvas_height($this->cv);
  87. }
  88. function getAttr($x, $y) {
  89. return caca_get_attr($this->cv, $x, $y);
  90. }
  91. function setAttr($attr) {
  92. return caca_set_attr($this->cv, $x, $y, $attr);
  93. }
  94. function setColorANSI($foreground, $background) {
  95. return caca_set_color_ansi($this->cv, $foreground, $background);
  96. }
  97. function setColorARGB($foreground, $background) {
  98. return caca_set_color_argb($this->cv, $foreground, $background);
  99. }
  100. function putChar($x, $y, $c) {
  101. return caca_put_char($this->cv, $x, $y, $c);
  102. }
  103. function getChar($x, $y) {
  104. return caca_get_char($this->cv, $x, $y);
  105. }
  106. function putStr($x, $y, $str) {
  107. return caca_put_str($this->cv, $x, $y, $str);
  108. }
  109. function Clear() {
  110. return caca_clear_canvas($this->cv);
  111. }
  112. function Blit($x, $y, $canvas, $mask = false) {
  113. return caca_blit($this->cv, $x, $y, $canvas->get_resource(), ($mask != false) ? $mask->get_resource() : false );
  114. }
  115. function Invert() {
  116. return caca_invert($this->cv);
  117. }
  118. function Flip() {
  119. return caca_flip($this->cv);
  120. }
  121. function Flop() {
  122. return caca_flop($this->cv);
  123. }
  124. function Rotate180() {
  125. return caca_rotate_180($this->cv);
  126. }
  127. function RotateLeft() {
  128. return caca_rotate_left($this->cv);
  129. }
  130. function RotateRight() {
  131. return caca_rotate_right($this->cv);
  132. }
  133. function drawLine($x1, $y1, $x2, $y2, $char) {
  134. return caca_draw_line($this->cv, $x1, $y1, $x2, $y2, $char);
  135. }
  136. function drawPolyline($points, $char) {
  137. return caca_draw_polyline($this->cv, $points, $char);
  138. }
  139. function drawThinLine($x1, $y1, $x2, $y2) {
  140. return caca_draw_thin_line($this->cv, $x1, $y1, $x2, $y2);
  141. }
  142. function drawThinPolyline($points) {
  143. return caca_draw_thin_polyline($this->cv, $points);
  144. }
  145. function drawCircle($x, $y, $radius, $char) {
  146. return caca_draw_circle($this->cv, $x, $y, $radius, $char);
  147. }
  148. function drawEllipse($x1, $y1, $x2, $y2, $char) {
  149. return caca_draw_ellipse($this->cv, $x1, $y1, $x2, $y2, $char);
  150. }
  151. function drawThinEllipse($x1, $y1, $x2, $y2) {
  152. return caca_draw_thin_ellipse($this->cv, $x1, $y1, $x2, $y2);
  153. }
  154. function fillEllipse($x1, $y1, $x2, $y2, $char) {
  155. return caca_fill_ellipse($this->cv, $x1, $y1, $x2, $y2, $char);
  156. }
  157. function drawBox($x1, $y1, $x2, $y2, $char) {
  158. return caca_draw_box($this->cv, $x1, $y1, $x2, $y2, $char);
  159. }
  160. function drawThinBox($x1, $y1, $x2, $y2) {
  161. return caca_draw_thin_box($this->cv, $x1, $y1, $x2, $y2);
  162. }
  163. function drawCP437Box($x1, $y1, $x2, $y2) {
  164. return caca_draw_cp437_box($this->cv, $x1, $y1, $x2, $y2);
  165. }
  166. function fillBox($x1, $y1, $x2, $y2, $char) {
  167. return caca_fill_box($this->cv, $x1, $y1, $x2, $y2, $char);
  168. }
  169. function drawTriangle($x1, $y1, $x2, $y2, $x3, $y3, $char) {
  170. return caca_draw_triangle($this->cv, $x1, $y1, $x2, $y2, $x3, $y3, $char);
  171. }
  172. function drawThinTriangle($x1, $y1, $x2, $y2, $x3, $y3) {
  173. return caca_draw_thin_triangle($this->cv, $x1, $y1, $x2, $y2, $x3, $y3);
  174. }
  175. function fillTriangle($x1, $y1, $x2, $y2, $x3, $y3, $char) {
  176. return caca_fill_triangle($this->cv, $x1, $y1, $x2, $y2, $x3, $y3, $char);
  177. }
  178. function __construct($width = 0, $height = 0) {
  179. $this->cv = caca_create_canvas($width, $height);
  180. }
  181. function get_resource() {
  182. return $this->cv;
  183. }
  184. }
  185. class Event {
  186. private $ev;
  187. function getType() {
  188. return caca_get_event_type($this->ev);
  189. }
  190. function getKeyCh() {
  191. return caca_get_event_key_ch($this->ev);
  192. }
  193. function getMouseX() {
  194. return caca_get_event_mouse_x($this->ev);
  195. }
  196. function getResizeWidth() {
  197. return caca_get_event_resize_width($this->ev);
  198. }
  199. function getResizeHeight() {
  200. return caca_get_event_resize_height($this->ev);
  201. }
  202. function __construct($_ev) {
  203. $this->ev = $_ev;
  204. }
  205. function get_resource() {
  206. return $this->ev;
  207. }
  208. }
  209. class Display {
  210. private $dp;
  211. function setCursor($visible) {
  212. return caca_set_cursor($this->dp, $visible);
  213. }
  214. function refresh() {
  215. return caca_refresh_display($this->dp);
  216. }
  217. function getDriver() {
  218. return caca_get_display_driver($this->dp);
  219. }
  220. function setDriver($name) {
  221. return caca_set_display_driver($this->dp, $name);
  222. }
  223. function setDisplayTime($time) {
  224. return caca_set_display_time($this->dp, $time);
  225. }
  226. function getDisplayTime() {
  227. return caca_get_display_time($this->dp);
  228. }
  229. function getWidth() {
  230. return caca_get_display_width($this->dp);
  231. }
  232. function getHeight() {
  233. return caca_get_display_height($this->dp);
  234. }
  235. function setTitle($title) {
  236. return caca_set_display_title($this->dp, $title);
  237. }
  238. function gotoXY($x, $y) {
  239. return caca_gotoxy($this->dp, $x, $y);
  240. }
  241. function getMouseX() {
  242. return caca_get_mouse_x($this->dp);
  243. }
  244. function getMouseY() {
  245. return caca_get_mouse_y($this->dp);
  246. }
  247. function setMouse($state) {
  248. return caca_set_mouse($this->dp, $state);
  249. }
  250. function getEvent($t, $timeout) {
  251. $ev = caca_get_event($this->dp, $t, $timeout);
  252. if(! $ev) {
  253. return NULL;
  254. }
  255. return new Event($ev);
  256. }
  257. function __construct($canvas, $driver = false) {
  258. if ($driver)
  259. $this->dp = caca_create_display_with_driver($canvas->get_resource(), $driver);
  260. else
  261. $this->dp = caca_create_display($canvas->get_resource());
  262. }
  263. function get_resource() {
  264. return $this->dp;
  265. }
  266. }
  267. class Dither {
  268. private $dt;
  269. private $img;
  270. function setPalette($colors) {
  271. return caca_set_dither_palette($this->dt, $colors);
  272. }
  273. function setBrightness($value) {
  274. return caca_set_dither_brightness($this->dt, $value);
  275. }
  276. function getBrightness() {
  277. return caca_get_dither_brightness($this->dt);
  278. }
  279. function setGamme($value) {
  280. return caca_set_dither_gamma($this->dt, $value);
  281. }
  282. function getGamma() {
  283. return caca_get_dither_gamma($this->dt);
  284. }
  285. function setContrast($value) {
  286. return caca_set_dither_contrast($this->dt, $value);
  287. }
  288. function getContrast() {
  289. return caca_get_dither_contrast($this->dt);
  290. }
  291. function setAntialias($value) {
  292. return caca_set_dither_antialias($this->dt, $value);
  293. }
  294. function getAntialiasList() {
  295. return caca_get_dither_antialias_list($this->dt);
  296. }
  297. function getAntialias() {
  298. return caca_get_dither_antialias($this->dt);
  299. }
  300. function setColor($color) {
  301. return caca_set_dither_color($this->dt, $color);
  302. }
  303. function getColorList() {
  304. return caca_get_dither_color_list($this->dt);
  305. }
  306. function getColor() {
  307. return caca_get_dither_color($this->dt);
  308. }
  309. function setCharset($value) {
  310. return caca_set_dither_charset($this->dt, $value);
  311. }
  312. function getCharsetList() {
  313. return caca_get_dither_charset_list($this->dt);
  314. }
  315. function getCharset() {
  316. return caca_get_dither_charset($this->dt);
  317. }
  318. function setAlgorithm($name) {
  319. return caca_set_dither_algorithm($this->dt, $name);
  320. }
  321. function getAlgorithmList() {
  322. return caca_get_dither_algorithm_list($this->dt);
  323. }
  324. function getAlgorithm() {
  325. return caca_get_dither_algorithm($this->dt);
  326. }
  327. function bitmap($canvas, $x, $y, $width, $height, $load_palette = true) {
  328. return caca_dither_bitmap($canvas->get_resource(), $x, $y, $width, $height, $this->dt, $this->img, $load_palette);
  329. }
  330. function __construct($image) {
  331. $this->dt = caca_create_dither($image);
  332. $this->img = $image;
  333. }
  334. }
  335. class Font {
  336. private $f;
  337. function getWidth() {
  338. return caca_get_font_width($this->f);
  339. }
  340. function getHeight() {
  341. return caca_get_font_height($this->f);
  342. }
  343. function getBlocks() {
  344. return caca_get_font_blocks($this->f);
  345. }
  346. function Render($cv, $image) {
  347. return caca_render_canvas($cv->get_resource(), $this->f, $image);
  348. }
  349. static function getList() {
  350. return caca_get_font_list();
  351. }
  352. function __construct($name) {
  353. $this->f = caca_load_builtin_font($name);
  354. }
  355. }