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.
 
 
 
 
 
 

569 lines
12 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 AnsiColor
  15. {
  16. const BLACK = CACA_BLACK;
  17. const BLUE = CACA_BLUE;
  18. const GREEN = CACA_GREEN;
  19. const CYAN = CACA_CYAN;
  20. const RED = CACA_RED;
  21. const MAGENTA = CACA_MAGENTA;
  22. const BROWN = CACA_BROWN;
  23. const LIGHTGRAY = CACA_LIGHTGRAY;
  24. const DARKGRAY = CACA_DARKGRAY;
  25. const LIGHTBLUE = CACA_LIGHTBLUE;
  26. const LIGHTGREEN = CACA_LIGHTGREEN;
  27. const LIGHTCYAN = CACA_LIGHTCYAN;
  28. const LIGHTRED = CACA_LIGHTRED;
  29. const LIGHTMAGENTA = CACA_LIGHTMAGENTA;
  30. const YELLOW = CACA_YELLOW;
  31. const WHITE = CACA_WHITE;
  32. /* NOTE: We can't call this one DEFAULT because that's a reserved
  33. * token in PHP. */
  34. const DEFAULTCOLOR = CACA_DEFAULT;
  35. const TRANSPARENT = CACA_TRANSPARENT;
  36. }
  37. class Canvas {
  38. private $cv;
  39. function importFile($path, $codec) {
  40. return caca_import_file($this->cv, $path, $codec);
  41. }
  42. function importString($codec) {
  43. return caca_import_string($this->cv, $codec);
  44. }
  45. function exportString($codec) {
  46. return caca_export_string($this->cv, $codec);
  47. }
  48. function freeFrame($id) {
  49. return caca_free_frame($this->cv, $id);
  50. }
  51. function frameCount() {
  52. return caca_get_frame_count($this->cv);
  53. }
  54. function createFrame($id) {
  55. return caca_create_frame($this->cv, $id);
  56. }
  57. function setFrameName($name) {
  58. return caca_set_frame_name($this->cv, $name);
  59. }
  60. function setFrame($id) {
  61. return caca_set_frame($this->cv, $id);
  62. }
  63. function putFigchar($char) {
  64. return caca_put_figchar($this->cv, $char);
  65. }
  66. function setFigfont($path) {
  67. return caca_canvas_set_figfont($this->cv, $path);
  68. }
  69. function putAttr($attr) {
  70. return caca_put_attr($this->cv, $attr);
  71. }
  72. function stretchRight() {
  73. return caca_stretch_right($this->cv);
  74. }
  75. function stretchLeft() {
  76. return caca_stretch_left($this->cv);
  77. }
  78. function setBoundaries($width, $height) {
  79. return caca_set_canvas_boundaries($this->cv, $width, $height);
  80. }
  81. function setHandle($x, $y) {
  82. return caca_set_canvas_handle($this->cv, $x, $y);
  83. }
  84. function getHandleX() {
  85. return caca_get_canvas_handle_x($this->cv);
  86. }
  87. function getHandleY() {
  88. return caca_get_canvas_handle_y($this->cv);
  89. }
  90. function getCursorX() {
  91. return caca_get_cursor_x($this->cv);
  92. }
  93. function getCursorY() {
  94. return caca_get_cursor_y($this->cv);
  95. }
  96. function getChars() {
  97. return caca_get_canvas_chars($this->cv);
  98. }
  99. function getAttrs() {
  100. return caca_get_canvas_attrs($this->cv);
  101. }
  102. function setSize($width, $height) {
  103. return caca_set_canvas_size($this->cv, $width, $height);
  104. }
  105. function getWidth() {
  106. return caca_get_canvas_width($this->cv);
  107. }
  108. function getHeight() {
  109. return caca_get_canvas_height($this->cv);
  110. }
  111. function getAttr($x, $y) {
  112. return caca_get_attr($this->cv, $x, $y);
  113. }
  114. function setAttr($attr) {
  115. return caca_set_attr($this->cv, $x, $y, $attr);
  116. }
  117. function setColorANSI($foreground, $background) {
  118. return caca_set_color_ansi($this->cv, $foreground, $background);
  119. }
  120. function setColorARGB($foreground, $background) {
  121. return caca_set_color_argb($this->cv, $foreground, $background);
  122. }
  123. function putChar($x, $y, $c) {
  124. return caca_put_char($this->cv, $x, $y, $c);
  125. }
  126. function getChar($x, $y) {
  127. return caca_get_char($this->cv, $x, $y);
  128. }
  129. function putStr($x, $y, $str) {
  130. return caca_put_str($this->cv, $x, $y, $str);
  131. }
  132. function Clear() {
  133. return caca_clear_canvas($this->cv);
  134. }
  135. function Blit($x, $y, $canvas, $mask = false) {
  136. return caca_blit($this->cv, $x, $y, $canvas->get_resource(), ($mask != false) ? $mask->get_resource() : false );
  137. }
  138. function Invert() {
  139. return caca_invert($this->cv);
  140. }
  141. function Flip() {
  142. return caca_flip($this->cv);
  143. }
  144. function Flop() {
  145. return caca_flop($this->cv);
  146. }
  147. function Rotate180() {
  148. return caca_rotate_180($this->cv);
  149. }
  150. function RotateLeft() {
  151. return caca_rotate_left($this->cv);
  152. }
  153. function RotateRight() {
  154. return caca_rotate_right($this->cv);
  155. }
  156. function drawLine($x1, $y1, $x2, $y2, $char) {
  157. return caca_draw_line($this->cv, $x1, $y1, $x2, $y2, $char);
  158. }
  159. function drawPolyline($points, $char) {
  160. return caca_draw_polyline($this->cv, $points, $char);
  161. }
  162. function drawThinLine($x1, $y1, $x2, $y2) {
  163. return caca_draw_thin_line($this->cv, $x1, $y1, $x2, $y2);
  164. }
  165. function drawThinPolyline($points) {
  166. return caca_draw_thin_polyline($this->cv, $points);
  167. }
  168. function drawCircle($x, $y, $radius, $char) {
  169. return caca_draw_circle($this->cv, $x, $y, $radius, $char);
  170. }
  171. function drawEllipse($x1, $y1, $x2, $y2, $char) {
  172. return caca_draw_ellipse($this->cv, $x1, $y1, $x2, $y2, $char);
  173. }
  174. function drawThinEllipse($x1, $y1, $x2, $y2) {
  175. return caca_draw_thin_ellipse($this->cv, $x1, $y1, $x2, $y2);
  176. }
  177. function fillEllipse($x1, $y1, $x2, $y2, $char) {
  178. return caca_fill_ellipse($this->cv, $x1, $y1, $x2, $y2, $char);
  179. }
  180. function drawBox($x1, $y1, $x2, $y2, $char) {
  181. return caca_draw_box($this->cv, $x1, $y1, $x2, $y2, $char);
  182. }
  183. function drawThinBox($x1, $y1, $x2, $y2) {
  184. return caca_draw_thin_box($this->cv, $x1, $y1, $x2, $y2);
  185. }
  186. function drawCP437Box($x1, $y1, $x2, $y2) {
  187. return caca_draw_cp437_box($this->cv, $x1, $y1, $x2, $y2);
  188. }
  189. function fillBox($x1, $y1, $x2, $y2, $char) {
  190. return caca_fill_box($this->cv, $x1, $y1, $x2, $y2, $char);
  191. }
  192. function drawTriangle($x1, $y1, $x2, $y2, $x3, $y3, $char) {
  193. return caca_draw_triangle($this->cv, $x1, $y1, $x2, $y2, $x3, $y3, $char);
  194. }
  195. function drawThinTriangle($x1, $y1, $x2, $y2, $x3, $y3) {
  196. return caca_draw_thin_triangle($this->cv, $x1, $y1, $x2, $y2, $x3, $y3);
  197. }
  198. function fillTriangle($x1, $y1, $x2, $y2, $x3, $y3, $char) {
  199. return caca_fill_triangle($this->cv, $x1, $y1, $x2, $y2, $x3, $y3, $char);
  200. }
  201. function __construct($width = 0, $height = 0) {
  202. $this->cv = caca_create_canvas($width, $height);
  203. }
  204. function get_resource() {
  205. return $this->cv;
  206. }
  207. }
  208. class EventType
  209. {
  210. const NONE = CACA_EVENT_NONE;
  211. const KEY_PRESS = CACA_EVENT_KEY_PRESS;
  212. const KEY_RELEASE = CACA_EVENT_KEY_RELEASE;
  213. const MOUSE_PRESS = CACA_EVENT_MOUSE_PRESS;
  214. const MOUSE_RELEASE = CACA_EVENT_MOUSE_RELEASE;
  215. const MOUSE_MOTION = CACA_EVENT_MOUSE_MOTION;
  216. const RESIZE = CACA_EVENT_RESIZE;
  217. const QUIT = CACA_EVENT_QUIT;
  218. const ANY = CACA_EVENT_ANY;
  219. }
  220. class EventKey
  221. {
  222. const UNKNOWN = CACA_KEY_UNKNOWN;
  223. const CTRL_A = CACA_KEY_CTRL_A;
  224. const CTRL_B = CACA_KEY_CTRL_B;
  225. const CTRL_C = CACA_KEY_CTRL_C;
  226. const CTRL_D = CACA_KEY_CTRL_D;
  227. const CTRL_E = CACA_KEY_CTRL_E;
  228. const CTRL_F = CACA_KEY_CTRL_F;
  229. const CTRL_G = CACA_KEY_CTRL_G;
  230. const BACKSPACE = CACA_KEY_BACKSPACE;
  231. const TAB = CACA_KEY_TAB;
  232. const CTRL_J = CACA_KEY_CTRL_J;
  233. const CTRL_K = CACA_KEY_CTRL_K;
  234. const CTRL_L = CACA_KEY_CTRL_L;
  235. /* NOTE: We can't call this one RETURN because that's a
  236. * reserved token in PHP */
  237. const RETURN_KEY = CACA_KEY_RETURN;
  238. const CTRL_N = CACA_KEY_CTRL_N;
  239. const CTRL_O = CACA_KEY_CTRL_O;
  240. const CTRL_P = CACA_KEY_CTRL_P;
  241. const CTRL_Q = CACA_KEY_CTRL_Q;
  242. const CTRL_R = CACA_KEY_CTRL_R;
  243. const PAUSE = CACA_KEY_PAUSE;
  244. const CTRL_T = CACA_KEY_CTRL_T;
  245. const CTRL_U = CACA_KEY_CTRL_U;
  246. const CTRL_V = CACA_KEY_CTRL_V;
  247. const CTRL_W = CACA_KEY_CTRL_W;
  248. const CTRL_X = CACA_KEY_CTRL_X;
  249. const CTRL_Y = CACA_KEY_CTRL_Y;
  250. const CTRL_Z = CACA_KEY_CTRL_Z;
  251. const ESCAPE = CACA_KEY_ESCAPE;
  252. const DELETE = CACA_KEY_DELETE;
  253. const UP = CACA_KEY_UP;
  254. const DOWN = CACA_KEY_DOWN;
  255. const LEFT = CACA_KEY_LEFT;
  256. const RIGHT = CACA_KEY_RIGHT;
  257. const INSERT = CACA_KEY_INSERT;
  258. const HOME = CACA_KEY_HOME;
  259. const END = CACA_KEY_END;
  260. const PAGEUP = CACA_KEY_PAGEUP;
  261. const PAGEDOWN = CACA_KEY_PAGEDOWN;
  262. const F1 = CACA_KEY_F1;
  263. const F2 = CACA_KEY_F2;
  264. const F3 = CACA_KEY_F3;
  265. const F4 = CACA_KEY_F4;
  266. const F5 = CACA_KEY_F5;
  267. const F6 = CACA_KEY_F6;
  268. const F7 = CACA_KEY_F7;
  269. const F8 = CACA_KEY_F8;
  270. const F9 = CACA_KEY_F9;
  271. const F10 = CACA_KEY_F10;
  272. const F11 = CACA_KEY_F11;
  273. const F12 = CACA_KEY_F12;
  274. const F13 = CACA_KEY_F13;
  275. const F14 = CACA_KEY_F14;
  276. const F15 = CACA_KEY_F15;
  277. }
  278. class Event {
  279. private $ev;
  280. function getType() {
  281. return caca_get_event_type($this->ev);
  282. }
  283. function getKeyCh() {
  284. return caca_get_event_key_ch($this->ev);
  285. }
  286. function getMouseX() {
  287. return caca_get_event_mouse_x($this->ev);
  288. }
  289. function getResizeWidth() {
  290. return caca_get_event_resize_width($this->ev);
  291. }
  292. function getResizeHeight() {
  293. return caca_get_event_resize_height($this->ev);
  294. }
  295. function __construct($_ev) {
  296. $this->ev = $_ev;
  297. }
  298. function get_resource() {
  299. return $this->ev;
  300. }
  301. }
  302. class Display {
  303. private $dp;
  304. function setCursor($visible) {
  305. return caca_set_cursor($this->dp, $visible);
  306. }
  307. function refresh() {
  308. return caca_refresh_display($this->dp);
  309. }
  310. function getDriver() {
  311. return caca_get_display_driver($this->dp);
  312. }
  313. function setDriver($name) {
  314. return caca_set_display_driver($this->dp, $name);
  315. }
  316. function setDisplayTime($time) {
  317. return caca_set_display_time($this->dp, $time);
  318. }
  319. function getDisplayTime() {
  320. return caca_get_display_time($this->dp);
  321. }
  322. function getWidth() {
  323. return caca_get_display_width($this->dp);
  324. }
  325. function getHeight() {
  326. return caca_get_display_height($this->dp);
  327. }
  328. function setTitle($title) {
  329. return caca_set_display_title($this->dp, $title);
  330. }
  331. function gotoXY($x, $y) {
  332. return caca_gotoxy($this->dp, $x, $y);
  333. }
  334. function getMouseX() {
  335. return caca_get_mouse_x($this->dp);
  336. }
  337. function getMouseY() {
  338. return caca_get_mouse_y($this->dp);
  339. }
  340. function setMouse($state) {
  341. return caca_set_mouse($this->dp, $state);
  342. }
  343. function getEvent($t, $timeout) {
  344. $ev = caca_get_event($this->dp, $t, $timeout);
  345. if(! $ev) {
  346. return NULL;
  347. }
  348. return new Event($ev);
  349. }
  350. function __construct($canvas, $driver = false) {
  351. if ($driver)
  352. $this->dp = caca_create_display_with_driver($canvas->get_resource(), $driver);
  353. else
  354. $this->dp = caca_create_display($canvas->get_resource());
  355. }
  356. function get_resource() {
  357. return $this->dp;
  358. }
  359. }
  360. class Dither {
  361. private $dt;
  362. private $img;
  363. function setPalette($colors) {
  364. return caca_set_dither_palette($this->dt, $colors);
  365. }
  366. function setBrightness($value) {
  367. return caca_set_dither_brightness($this->dt, $value);
  368. }
  369. function getBrightness() {
  370. return caca_get_dither_brightness($this->dt);
  371. }
  372. function setGamme($value) {
  373. return caca_set_dither_gamma($this->dt, $value);
  374. }
  375. function getGamma() {
  376. return caca_get_dither_gamma($this->dt);
  377. }
  378. function setContrast($value) {
  379. return caca_set_dither_contrast($this->dt, $value);
  380. }
  381. function getContrast() {
  382. return caca_get_dither_contrast($this->dt);
  383. }
  384. function setAntialias($value) {
  385. return caca_set_dither_antialias($this->dt, $value);
  386. }
  387. function getAntialiasList() {
  388. return caca_get_dither_antialias_list($this->dt);
  389. }
  390. function getAntialias() {
  391. return caca_get_dither_antialias($this->dt);
  392. }
  393. function setColor($color) {
  394. return caca_set_dither_color($this->dt, $color);
  395. }
  396. function getColorList() {
  397. return caca_get_dither_color_list($this->dt);
  398. }
  399. function getColor() {
  400. return caca_get_dither_color($this->dt);
  401. }
  402. function setCharset($value) {
  403. return caca_set_dither_charset($this->dt, $value);
  404. }
  405. function getCharsetList() {
  406. return caca_get_dither_charset_list($this->dt);
  407. }
  408. function getCharset() {
  409. return caca_get_dither_charset($this->dt);
  410. }
  411. function setAlgorithm($name) {
  412. return caca_set_dither_algorithm($this->dt, $name);
  413. }
  414. function getAlgorithmList() {
  415. return caca_get_dither_algorithm_list($this->dt);
  416. }
  417. function getAlgorithm() {
  418. return caca_get_dither_algorithm($this->dt);
  419. }
  420. function bitmap($canvas, $x, $y, $width, $height, $load_palette = true) {
  421. return caca_dither_bitmap($canvas->get_resource(), $x, $y, $width, $height, $this->dt, $this->img, $load_palette);
  422. }
  423. function __construct($image) {
  424. $this->dt = caca_create_dither($image);
  425. $this->img = $image;
  426. }
  427. }
  428. class Font {
  429. private $f;
  430. function getWidth() {
  431. return caca_get_font_width($this->f);
  432. }
  433. function getHeight() {
  434. return caca_get_font_height($this->f);
  435. }
  436. function getBlocks() {
  437. return caca_get_font_blocks($this->f);
  438. }
  439. function Render($cv, $image) {
  440. return caca_render_canvas($cv->get_resource(), $this->f, $image);
  441. }
  442. static function getList() {
  443. return caca_get_font_list();
  444. }
  445. function __construct($name) {
  446. $this->f = caca_load_builtin_font($name);
  447. }
  448. }