ASCII-Pony/render_parts.php

177 lines
4.3 KiB
PHP
Raw Normal View History

2013-11-06 16:40:27 +01:00
#!/usr/bin/php
<?php
2013-11-08 19:48:53 +01:00
defined( "ENT_XML1") or define("ENT_XML1",16);
function color_ansi2svg($col)
{
$formats = explode(';',$col);
$color_n = 7;
$bold = false;
foreach($formats as $format)
{
$format = (int)$format;
if ( $format >= 30 && $format < 38 )
{
$color_n = $format-30;
}
else if ( $format == 1 )
$bold = true;
}
if ( $bold )
{
switch($color_n)
{
case 0: return 'gray';
case 1: return 'red';
case 2: return 'lime';
case 3: return 'yellow';
case 4: return 'blue';
case 5: return 'magenta';
case 6: return 'cyan';
case 7: return 'white';
}
}
switch($color_n)
{
case 0: return 'black';
case 1: return 'maroon';
case 2: return 'green';
case 3: return 'orange';
case 4: return 'navy';
case 5: return 'purple';
case 6: return 'teal';
case 7: return 'silver';
}
return "silver";
}
2013-11-06 16:48:33 +01:00
$dir = isset($argv[1]) ? $argv[1] : getcwd();
2013-11-08 19:48:53 +01:00
const COLORED_TEXT = 0;
const PLAIN_TEXT = 1;
const SVG = 2;
2013-11-09 08:58:29 +01:00
const BASH = 3;
2013-11-08 19:48:53 +01:00
if ( !isset($argv[2]) )
$output_type = COLORED_TEXT;
else if ( $argv[2] == 'nocolor' )
$output_type = PLAIN_TEXT;
else if ( $argv[2] == 'svg' )
$output_type = SVG;
2013-11-09 08:58:29 +01:00
else if ( $argv[2] == 'bash' )
$output_type = BASH;
2013-11-08 19:48:53 +01:00
2013-11-06 16:40:27 +01:00
$dir_files = scandir($dir);
$files = array();
$maxw = 0;
$maxh = 0;
foreach($dir_files as $file)
{
2013-11-08 10:57:46 +01:00
if ( preg_match("/([0-9]+;)*[0-9]+/",$file) !== false )
2013-11-06 16:40:27 +01:00
{
$curr_file = file("$dir/$file",FILE_IGNORE_NEW_LINES);
$files[$file] = $curr_file;
2013-11-06 16:43:48 +01:00
foreach($curr_file as &$line)
2013-11-06 16:40:27 +01:00
{
2013-11-06 16:43:48 +01:00
$line = rtrim($line);
2013-11-06 16:40:27 +01:00
$w = strlen($line);
if ( $w> $maxw )
$maxw = $w;
}
$h = count($curr_file);
if ( $h > $maxh )
$maxh = $h;
}
}
$chars=array_fill(0,$maxh,array_fill(0,$maxw,null));
foreach ( $files as $color => $lines )
2013-11-06 17:27:36 +01:00
for ( $i = 0; $i < count($lines); $i++ )
2013-11-06 16:40:27 +01:00
{
for ( $j = 0, $l = strlen($lines[$i]); $j < $l; $j++ )
{
if ( $lines[$i][$j] != ' ' )
$chars[$i][$j] = array("color"=>$color,"char"=>$lines[$i][$j]);
}
}
2013-11-08 19:48:53 +01:00
if ( $output_type == SVG )
2013-11-06 16:40:27 +01:00
{
2013-11-08 19:48:53 +01:00
$font_size = 12;
$font_width = $font_size/2;
echo "<?xml version='1.0' encoding='UTF-8' ?>\n";
echo "<svg xmlns='http://www.w3.org/2000/svg' width='".($maxw*$font_width)."' height='".($maxh*$font_size)."'>\n";
echo "<rect style='fill:black;stroke:none;' width='".($maxw*$font_width)."' height='".($maxh*$font_size)."' x='0' y='0' />\n";
echo "<text y='0' x='0' style='font-family:monospace;font-size:${font_size}px;font-weight:bold;'>";
$y = 0;
foreach($chars as $line)
2013-11-06 16:40:27 +01:00
{
2013-11-08 19:48:53 +01:00
if ( $output_type == SVG )
{
$y += $font_size;
$x = 0;
}
foreach($line as $char)
{
if ( !is_null($char) )
{
echo "<tspan x='$x' y='$y' style='fill:".color_ansi2svg($char['color']).";'>".
htmlspecialchars($char['char'],ENT_XML1)."</tspan>\n";
}
$x += $font_width;
}
echo "\n";
}
echo "</text></svg>";
}
else
{
2013-11-09 08:58:29 +01:00
if ( $output_type == BASH )
{
echo "#!/bin/bash\n";
echo "read -r -d '' Heredoc_var <<'Heredoc_var'\n\\x1b[0m";
}
2013-11-08 19:48:53 +01:00
foreach($chars as $line)
{
if ( $output_type == SVG )
{
$y += $font_size;
$x = 0;
}
foreach($line as $char)
{
if ( is_null($char) )
echo ' ';
else if ( $output_type == COLORED_TEXT )
echo "\x1b[$char[color]m$char[char]";
2013-11-09 08:58:29 +01:00
else if ( $output_type == BASH )
{
$c = $char['char'];
if ( $c == '\\')
$c = '\\\\';
echo "\\x1b[$char[color]m$c";
}
2013-11-08 19:48:53 +01:00
else
echo $char['char'];
}
echo "\n";
2013-11-06 16:40:27 +01:00
}
2013-11-09 08:58:29 +01:00
2013-11-08 19:48:53 +01:00
if ( $output_type == COLORED_TEXT )
echo "\x1b[0m\n";
2013-11-09 08:58:29 +01:00
if ( $output_type == BASH )
{
echo "\\x1b[0m\nHeredoc_var\necho -e \"\$Heredoc_var\"\n";
}
2013-11-06 16:40:27 +01:00
}