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.
 
 
 
 
 
 

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