[3129] | 1 | #!/usr/bin/php5 |
---|
| 2 | <?php |
---|
[3172] | 3 | /* |
---|
| 4 | * figfont.php sample program for libcaca php binding |
---|
| 5 | * Copyright (c) 2008 Nicolas Vion <nico@yojik.eu> |
---|
| 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://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 12 | */ |
---|
[3129] | 13 | |
---|
[3133] | 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 | |
---|
[3177] | 26 | if (php_sapi_name() != "cli") { |
---|
[3175] | 27 | die("You have to run this program with php-cli!\n"); |
---|
[3177] | 28 | } |
---|
[3133] | 29 | |
---|
[3129] | 30 | if ($argc < 3) { |
---|
[3133] | 31 | die("Too few arguments.\nUsage: cmd <path of font> <utf8 string>\n"); |
---|
[3129] | 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 | |
---|
[3133] | 40 | $chars = unistr_to_ords($argv[2]); |
---|
[3129] | 41 | $color = 0; |
---|
[3133] | 42 | foreach ($chars as $c) { |
---|
[3129] | 43 | caca_set_color_ansi($cv, 1 + (($color += 4) % 15), CACA_TRANSPARENT); |
---|
[3133] | 44 | caca_put_figchar($cv, $c); |
---|
[3129] | 45 | } |
---|
| 46 | |
---|
| 47 | echo caca_export_string($cv, "utf8"); |
---|
| 48 | |
---|
[3135] | 49 | ?> |
---|