217 lines
6.3 KiB

  1. <?php
  2. /*
  3. * export libcaca export test program
  4. * Copyright (c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com>
  5. *
  6. * This file is a Php port of "examples/export.c"
  7. * which is:
  8. * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
  9. * All Rights Reserved
  10. *
  11. * $Id$
  12. *
  13. * This program is free software. It comes without any warranty, to
  14. * the extent permitted by applicable law. You can redistribute it
  15. * and/or modify it under the terms of the Do What The Fuck You Want
  16. * To Public License, Version 2, as published by Sam Hocevar. See
  17. * http://sam.zoy.org/wtfpl/COPYING for more details.
  18. */
  19. define('WIDTH', 80);
  20. define('HEIGHT', 32);
  21. $pixels = imagecreatetruecolor(256, 256);
  22. $exports = caca_get_export_list();
  23. $file = isset($_FILES['file']) ? $_FILES['file']['tmp_name'] : NULL;
  24. $format = isset($_REQUEST['format']) ? $_REQUEST['format'] : NULL;
  25. if((! $format) || (! array_key_exists($format, $exports)))
  26. {
  27. header("Content-type: text/html; charset=UTF-8");
  28. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  29. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  30. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  31. <head>
  32. <title>libcaca export test program</title>
  33. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  34. <script type="text/javascript">
  35. /*<![CDATA[*/
  36. update_preview = function (select)
  37. {
  38. var iframe_map = {
  39. 'html': true,
  40. 'html3': true,
  41. 'bbfr': true,
  42. 'svg': true
  43. };
  44. var e;
  45. try
  46. {
  47. var format = select.options[select.selectedIndex].value;
  48. var newLocation = 'about:blank';
  49. if (iframe_map[format])
  50. {
  51. newLocation = self.location.pathname + '?format=' + encodeURIComponent(format);
  52. }
  53. self.frames[0].location = newLocation;
  54. }
  55. catch (e)
  56. {
  57. alert('e' + e);
  58. }
  59. return true;
  60. };
  61. /*]]>*/
  62. </script>
  63. </head>
  64. <body onload="update_preview(document.forms.exportform.elements.format);">
  65. <form id="exportform" name="exportform" action="#" enctype="multipart/form-data" method="post">
  66. <label for="file">File:</label>
  67. <input id="file" name="file" type="file" /> <em>(optional)</em>
  68. <br />
  69. <input type="submit" value="Export" />
  70. <label for="format">as</label>
  71. <select name="format" id="format" onchange="update_preview(this);">
  72. <?php
  73. foreach($exports as $format => $name)
  74. {
  75. ?><option value="<?= htmlspecialchars($format) ?>"<?=
  76. ($format == 'html') ? ' selected="selected"' : '' ?>><?=
  77. htmlspecialchars($name . " (" . $format . ")") ?></option><?php
  78. }
  79. ?>
  80. </select>
  81. </form>
  82. <?php
  83. $export_php = isset($_SERVER['SCRIPT_NAME'])
  84. ?
  85. $_SERVER['SCRIPT_NAME']
  86. :
  87. 'export.php';
  88. ?><iframe name="preview" id="preview" src="<?= htmlspecialchars($export_php) ?>?format=html" width="820" height="620" style="margin: 0; padding: 0; border: none; width: 100%"></iframe>
  89. </body>
  90. </html>
  91. <?php
  92. exit(0);
  93. }
  94. if($file)
  95. {
  96. $cv = caca_create_canvas(0, 0);
  97. if(caca_import_file($cv, $file, "") < 0)
  98. {
  99. die($argv[0] . ": `" . $file . "' has unknown format\n");
  100. }
  101. }
  102. else
  103. {
  104. $cv = caca_create_canvas(WIDTH, HEIGHT);
  105. for($y = 0; $y < 256; $y++)
  106. {
  107. for($x = 0; $x < 256; $x++)
  108. {
  109. $r = $x;
  110. $g = (255 - $y + $x) / 2;
  111. $b = $y * (255 - $x) / 256;
  112. imagesetpixel($pixels, $x, $y, imagecolorallocate($pixels, $r, $g, $b));
  113. }
  114. }
  115. $dither = caca_create_dither($pixels);
  116. if(($format == "ansi") || ($format == "utf8"))
  117. caca_set_dither_charset($dither, "shades");
  118. caca_dither_bitmap($cv, 0, 0, caca_get_canvas_width($cv),
  119. caca_get_canvas_height($cv), $dither, $pixels);
  120. caca_set_color_ansi($cv, CACA_WHITE, CACA_BLACK);
  121. caca_draw_thin_box($cv, 0, 0, WIDTH - 1, HEIGHT - 1);
  122. caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
  123. caca_fill_ellipse($cv, WIDTH / 2, HEIGHT / 2,
  124. WIDTH / 4, HEIGHT / 4, ord(' '));
  125. caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
  126. caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 6,
  127. " lightgray on black ");
  128. caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT);
  129. caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 5,
  130. " default on transparent ");
  131. caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
  132. caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 4,
  133. " black on white ");
  134. caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
  135. caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]");
  136. caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]");
  137. caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ");
  138. caca_put_str($cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>");
  139. caca_set_attr($cv, CACA_BOLD, CACA_DEFAULT);
  140. caca_put_str($cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold");
  141. caca_set_attr($cv, CACA_BLINK, CACA_DEFAULT);
  142. caca_put_str($cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink");
  143. caca_set_attr($cv, CACA_ITALICS, CACA_DEFAULT);
  144. caca_put_str($cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics");
  145. caca_set_attr($cv, CACA_UNDERLINE, CACA_DEFAULT);
  146. caca_put_str($cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline");
  147. caca_set_attr($cv, 0, CACA_DEFAULT);
  148. caca_set_color_ansi($cv, CACA_WHITE, CACA_LIGHTBLUE);
  149. caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA ");
  150. for($x = 0; $x < 16; $x++)
  151. {
  152. caca_set_color_argb($cv, 0xff00 | $x, 0xf00f | ($x << 4));
  153. caca_put_char($cv, WIDTH / 2 - 7 + $x, HEIGHT / 2 + 6, ord('#'));
  154. }
  155. }
  156. $content_type_map = array(
  157. 'ansi' => 'text/plain; charset=CP437',
  158. 'utf8' => 'text/plain; charset=UTF-8',
  159. 'utf8cr' => 'text/plain; charset=UTF-8',
  160. 'html' => 'text/html; charset=UTF-8',
  161. 'html3' => 'text/html; charset=UTF-8',
  162. 'bbfr' => 'text/plain; charset=UTF-8',
  163. 'irc' => 'text/plain; charset=UTF-8',
  164. 'ps' => 'application/postscript',
  165. 'svg' => 'image/svg+xml',
  166. 'tga' => 'image/x-targa'
  167. );
  168. $download_extension_map = array(
  169. 'caca' => 'caca',
  170. 'ansi' => 'txt',
  171. 'utf8' => 'txt',
  172. 'utf8cr' => 'txt',
  173. 'irc' => 'txt',
  174. 'tga' => 'tga'
  175. );
  176. $inline_extension_map = array(
  177. 'bbfr' => 'txt',
  178. 'ps' => 'ps',
  179. 'svg' => 'svg'
  180. );
  181. if (! array_key_exists($format, $content_type_map))
  182. $content_type = 'application/octet-stream';
  183. else
  184. $content_type = $content_type_map[$format];
  185. header('Content-Type: ' . $content_type);
  186. if (array_key_exists($format, $download_extension_map))
  187. header('Content-Disposition: attachment; filename=export.' . $download_extension_map[$format]);
  188. else if (array_key_exists($format, $inline_extension_map))
  189. header('Content-Disposition: inline; filename=export.' . $inline_extension_map[$format]);
  190. echo caca_export_string($cv, $format);
  191. ?>