Better render script

This commit is contained in:
Mattia Basaglia 2015-08-15 23:59:09 +02:00
parent e3a390b9f2
commit f907bbd605

View file

@ -1,98 +1,267 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
defined( "ENT_XML1") or define("ENT_XML1",16); defined( "ENT_XML1") or define("ENT_XML1",16);
function color_ansi2svg($col)
class Color
{ {
$formats = explode(';',$col); public $color = 7;
$color_n = 7; public $bright = false;
$bold = false;
foreach($formats as $format) function __construct($color, $bright=false)
{ {
$format = (int)$format; $this->color = $color;
if ( $format >= 30 && $format < 38 ) $this->bright = $bright;
{ }
$color_n = $format-30;
} static $names_to_int = array(
else if ( $format >= 90 && $format < 98 ) 'black' => 0,
{ 'red' => 1,
$color_n = $format-90; 'green' => 2,
$bold = true; 'yellow' => 3,
} 'blue' => 4,
else if ( $format == 1 ) 'magenta' => 5,
$bold = true; 'cyan' => 6,
} 'white' => 7
);
if ( $bold )
{ static function from_ansi($col)
switch($color_n) {
{ $formats = explode(';',$col);
case 0: return 'gray'; $color_n = 7;
case 1: return 'red'; $bold = false;
case 2: return 'lime'; foreach($formats as $format)
case 3: return 'yellow'; {
case 4: return 'blue'; $format = (int)$format;
case 5: return 'magenta'; if ( $format >= 30 && $format < 38 )
case 6: return 'cyan'; {
case 7: return 'white'; $color_n = $format-30;
} }
} else if ( $format >= 90 && $format < 98 )
switch($color_n) {
{ $color_n = $format-90;
case 0: return 'black'; $bold = true;
case 1: return 'maroon'; }
case 2: return 'green'; else if ( $format == 1 )
case 3: return 'orange'; {
case 4: return 'navy'; $bold = true;
case 5: return 'purple'; }
case 6: return 'teal'; }
case 7: return 'silver';
} return new Color( $color_n, $bold );
return "silver"; }
} }
function color_ansi2irc($col) interface ColorOutput
{ {
$formats = explode(';',$col); function begin();
$color_n = 7; function end();
$bold = false; function begin_line();
foreach($formats as $format) function end_line();
{ function character( $char );
$format = (int)$format;
if ( $format >= 30 && $format < 38 ) }
{
$color_n = $format-30; class SvgColorOutput implements ColorOutput
} {
else if ( $format == 1 ) private $font_size;
$bold = true; private $font_width;
} private $width, $height;
private $y = 0, $x = 0;
if ( $bold ) function __construct($width_in_characters, $height_in_characters, $font_size = 12)
{ {
switch($color_n) $this->font_size = $font_size;
{ $this->font_width = $font_size/2;
case 0: return '14'; $this->width = $width_in_characters * $this->font_width;
case 1: return '04'; $this->height = $height_in_characters * $this->font_size;
case 2: return '09'; }
case 3: return '08';
case 4: return '12'; function begin()
case 5: return '13'; {
case 6: return '11'; echo "<?xml version='1.0' encoding='UTF-8' ?>\n";
case 7: return '00'; echo "<svg xmlns='http://www.w3.org/2000/svg' width='{$this->width}' height='{$this->height}'>\n";
} echo "<rect style='fill:black;stroke:none;' width='{$this->width}' height='{$this->height}' x='0' y='0' />\n";
} echo "<text y='0' x='0' style='font-family:monospace;font-size:{$this->font_size}px;font-weight:bold;'>";
switch($color_n) }
{
case 0: return '01'; function end()
case 1: return '05'; {
case 2: return '03'; echo "</text></svg>";
case 3: return '07'; }
case 4: return '02';
case 5: return '06'; function begin_line()
case 6: return '10'; {
case 7: return '15'; $this->y += $this->font_size;
} $this->x = 0;
return ''; }
function end_line()
{
echo "\n";
}
function character( $char )
{
if ( !is_null($char) )
{
echo "<tspan x='{$this->x}' y='{$this->y}' style='fill:".$this->color($char['color']).";'>".
htmlspecialchars($char['char'],ENT_XML1)."</tspan>\n";
}
$this->x += $this->font_width;
}
function color( Color $color )
{
if ( $color->bright )
{
switch( $color->color )
{
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->color )
{
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";
}
}
class PlaintextColorOutput implements ColorOutput
{
function begin() {}
function end() {}
function begin_line() {}
function end_line()
{
echo "\n";
}
function character( $char )
{
if ( is_null($char) )
echo ' ';
else
echo $this->color($char['color']).$this->escape($char['char']);
}
function color( Color $color )
{
return "";
}
function escape( $char )
{
return $char;
}
}
class IRCColorOutput extends PlaintextColorOutput
{
private $width_in_characters;
private $x;
function __construct( $width_in_characters )
{
$this->width_in_characters = $width_in_characters;
}
function begin_line()
{
echo "\x0301,01";
$this->x = 0;
}
function end_line()
{
echo str_repeat(' ',$this->width_in_characters-$this->x)."\x03";
echo "\n";
}
function character( $char )
{
$this->x++;
parent::character( $char );
}
function color( Color $color )
{
if ( $color->bright )
{
switch( $color->color )
{
case 0: return '14';
case 1: return '04';
case 2: return '09';
case 3: return '08';
case 4: return '12';
case 5: return '13';
case 6: return '11';
case 7: return '00';
}
}
switch( $color->color )
{
case 0: return '01';
case 1: return '05';
case 2: return '03';
case 3: return '07';
case 4: return '02';
case 5: return '06';
case 6: return '10';
case 7: return '15';
}
return '15';
}
}
class AnsiColorOutput extends PlainTextColorOutput
{
function end()
{
echo "\x1b[0m\n";
}
function color( Color $color )
{
$bold = $color->bright ? "1" : "22";
return "\x1b[{$color->color};{$bold}m";
}
}
class BashColorOutput extends AnsiColorOutput
{
function begin()
{
echo "#!/bin/bash\n";
echo "read -r -d '' Heredoc_var <<'Heredoc_var'\n\\x1b[0m";
}
function end()
{
echo "\\x1b[0m\nHeredoc_var\necho -e \"\$Heredoc_var\"\n";
}
function escape( $char )
{
return $char == '\\' ? '\\\\' : $char;
}
} }
$dir = isset($argv[1]) ? $argv[1] : getcwd(); $dir = isset($argv[1]) ? $argv[1] : getcwd();
@ -143,90 +312,44 @@ foreach ( $files as $color => $lines )
for ( $j = 0, $l = strlen($lines[$i]); $j < $l; $j++ ) for ( $j = 0, $l = strlen($lines[$i]); $j < $l; $j++ )
{ {
if ( $lines[$i][$j] != ' ' ) if ( $lines[$i][$j] != ' ' )
$chars[$i][$j] = array("color"=>$color,"char"=>$lines[$i][$j]); $chars[$i][$j] = array("color"=>Color::from_ansi($color),"char"=>$lines[$i][$j]);
} }
} }
/// \todo factory
if ( $output_type == SVG ) if ( $output_type == SVG )
{ {
$font_size = 12; $output = new SvgColorOutput($maxw, $maxh);
$font_width = $font_size/2; }
echo "<?xml version='1.0' encoding='UTF-8' ?>\n"; else if ( $output_type == BASH )
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"; $output = new BashColorOutput();
echo "<text y='0' x='0' style='font-family:monospace;font-size:${font_size}px;font-weight:bold;'>"; }
$y = 0; else if ( $output_type == IRC_TEXT )
{
$output = new IrcColorOutput($maxw);
foreach($chars as $line) }
{ else if ( $output_type == COLORED_TEXT )
$y += $font_size; {
$x = 0; $output = new AnsiColorOutput();
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 else
{ {
$output = new PlainTextColorOutput();
if ( $output_type == BASH )
{
echo "#!/bin/bash\n";
echo "read -r -d '' Heredoc_var <<'Heredoc_var'\n\\x1b[0m";
}
foreach($chars as $line)
{
if ( $output_type == IRC_TEXT )
echo "\x0301,01";
foreach($line as $char)
{
if ( is_null($char) )
echo ' ';
else if ( $output_type == COLORED_TEXT )
echo "\x1b[$char[color]m$char[char]";
else if ( $output_type == BASH )
{
$c = $char['char'];
if ( $c == '\\')
$c = '\\\\';
echo "\\x1b[$char[color]m$c";
}
else if ( $output_type == IRC_TEXT )
{
$col = color_ansi2irc($char['color']);
echo "\x03$col{$char['char']}";
}
else
echo $char['char'];
}
if ( $output_type == IRC_TEXT )
echo str_repeat(' ',$maxw-count($line))."\x03";
echo "\n";
}
if ( $output_type == COLORED_TEXT )
echo "\x1b[0m\n";
if ( $output_type == BASH )
{
echo "\\x1b[0m\nHeredoc_var\necho -e \"\$Heredoc_var\"\n";
}
} }
$output->begin();
foreach($chars as $line)
{
$output->begin_line();
foreach($line as $char)
{
$output->character( $char );
}
$output->end_line();
}
$output->end();