Debian Codegraph @

caca-php/examples/figfont.php

raw
1 #!/usr/bin/php5
2 <?php
3 /*
4 * figfont.php sample program for libcaca php binding
5 * Copyright (c) 2008 Nicolas Vion <[email protected]>
6 *
7 * This program is free software. It comes without any warranty, to
8 * the extent permitted by applicable law. You can redistribute it
9 * and/or modify it under the terms of the Do What the Fuck You Want
10 * to Public License, Version 2, as published by Sam Hocevar. See
11 * http://www.wtfpl.net/ for more details.
12 */
13
14 function unistr_to_ords($str, $encoding = 'UTF-8'){
15 $str = mb_convert_encoding($str, "UCS-4BE", $encoding);
16 $result = array();
17
18 for ($i = 0; $i < mb_strlen($str, "UCS-4BE"); $i++){
19 $c = mb_substr($str, $i, 1, "UCS-4BE");
20 $val = unpack("N", $c);
21 $result[] = $val[1];
22 }
23 return $result;
24 }
25
26 if (php_sapi_name() != "cli") {
27 die("You have to run this program with php-cli!\n");
28 }
29
30 if ($argc < 3) {
31 die("Too few arguments.\nUsage: cmd <path of font> <utf8 string>\n");
32 }
33
34 $cv = caca_create_canvas(0, 0);
35
36 if (!caca_canvas_set_figfont($cv, $argv[1])) {
37 die("Could not open font\n");
38 }
39
40 $chars = unistr_to_ords($argv[2]);
41 $color = 0;
42 foreach ($chars as $c) {
43 caca_set_color_ansi($cv, 1 + (($color += 4) % 15), CACA_TRANSPARENT);
44 caca_put_figchar($cv, $c);
45 }
46
47 echo caca_export_string($cv, "utf8");
48
49 ?>