<?php
/* charset to javascript compiler
	because I am basically insane */

$infile=get_option('cli_gc_gcfont');
$scheme=get_option('cli_gc_scheme');
$scale=get_option('cli_gc_scale');

$jscachedata=CLI_DIR.'/var/cache/fonts/'.$infile.'.data.js';
$csscachedata=CLI_DIR.'/var/cache/fonts/'.$infile.'.data.css';
$csscachegif=CLI_DIR.'/var/cache/fonts/'.$infile.'.gif.css';
$gifbase=CLI_DIR.'/var/cache/fonts/'.$infile.'.gif';
$ugifbase=CLI_URI.'/var/cache/fonts/'.$infile.'.gif';
$blank='/var/cache/fonts/'.$infile.'.blank.gif';
if(
	file_exists($jscachedata) 
	&& file_exists($csscachedata)
	&& file_exists($csscachegif)
){
	switch($scheme){
		case 'js/data':
			include($jscachedata);
			break;
		case 'css/data':
			include($csscachedata);
			break;
		case 'css/gif':
			include($csscachegif);
			break;
		default:
			die('No scheme specified');
	}
}else{
	$fhjd=fopen($jscachedata,"w");
	$fhcd=fopen($csscachedata,"w");
	$fhcg=fopen($csscachegif,"w");
	if(!$fhjd || !$fhcd || !$fhcg){
		echo('</script>Please make the var/cache/fonts directory writeable.');	
	 	die;
	}
	include(CLI_DIR.'/usr/share/fonts/'.$infile.'.inc.php');
	if(!isset($charset) || !is_array($charset)){
		fclose($fhjd);
		fclose($fhcd);
		fclose($fhcg);
		die("File $filename does not contain character set information");
	}
	fwrite($fhjd,"var cset=new Array();\n");
	fwrite($fhjd,"var csetrvs=new Array();\n");

	fwrite($fhcd,".gc{height:".$v*$scale."px"
		.";width:".$h*$scale."px"
		.";background-image:url(".$ugifbase.")"
		.";background-repeat:no-repeat}");
	fwrite($fhcg,".gc{height:".$v*$scale."px"
		.";width:".$h*$scale."px"
		.";background-image:url(".$ugifbase.")"
		.";background-repeat:no-repeat}");
		
	if(!isset($scale))$scale=1;
	
	$im = imagecreate($h*$scale, $v*$scale);
	$rim = imagecreate($h*$scale, $v*$scale);
	$allim = imagecreate($h*$scale*count($charset), 2*$v*$scale);

	$nfigure = imagecolorallocate($im,
		get_option('cli_gc_fg_colour_red'),
		get_option('cli_gc_fg_colour_green'),
		get_option('cli_gc_fg_colour_blue')
	);
	$nground = imagecolorallocate($im,
		get_option('cli_gc_bg_colour_red'),
		get_option('cli_gc_bg_colour_green'),
		get_option('cli_gc_bg_colour_blue')
	);
	$rfigure = imagecolorallocate($rim,
		get_option('cli_gc_bg_colour_red'),
		get_option('cli_gc_bg_colour_green'),
		get_option('cli_gc_bg_colour_blue')
	);
	$rground = imagecolorallocate($rim,
		get_option('cli_gc_fg_colour_red'),
		get_option('cli_gc_fg_colour_green'),
		get_option('cli_gc_fg_colour_blue')
	);
	$afigure = imagecolorallocate($allim,
		get_option('cli_gc_fg_colour_red'),
		get_option('cli_gc_fg_colour_green'),
		get_option('cli_gc_fg_colour_blue')
	);
	$aground = imagecolorallocate($allim,
		get_option('cli_gc_bg_colour_red'),
		get_option('cli_gc_bg_colour_green'),
		get_option('cli_gc_bg_colour_blue')
	);
	
	$cix = -1;
	foreach($charset as $c => $data){
		$cix++;
		//imagefill($im, 0, 0, $ground);
		if($scale==1){
			for($y = 0; $y < $v; $y++){
				for($x = 0; $x < $h; $x++){
					if($charset[$c][$y]{$x}!=' '){
						imagesetpixel($im, $x, $y, $nfigure);
						imagesetpixel($rim, $x, $y, $rfigure);
						imagesetpixel($allim, $cix*$h+$x, $y, $afigure);
						imagesetpixel($allim, $cix*$h+$x, $v+$y, $aground);
					}else{
						imagesetpixel($im, $x, $y, $nground);
						imagesetpixel($rim, $x, $y, $rground);
						imagesetpixel($allim, $cix*$h+$x, $y, $aground);
						imagesetpixel($allim, $cix*$h+$x, $v+$y, $afigure);
					}
				}
			}
		}else{
			for($y = 0; $y < $v; $y++){
				for($x = 0; $x < $h; $x++){
					if($charset[$c][$y]{$x}!=' '){
						imagefilledrectangle(
							$im,
							$x*$scale,
							$y*$scale,
							$x*$scale+$scale-1,
							$y*$scale+$scale-1,
							$nfigure
						);
						imagefilledrectangle(
							$rim,
							$x*$scale,
							$y*$scale,
							$x*$scale+$scale-1,
							$y*$scale+$scale-1,
							$rfigure
						);
						imagefilledrectangle(
							$allim,
							$cix*$h*$scale+$x*$scale,
							$y*$scale,
							$cix*$h*$scale+$x*$scale+$scale-1,
							$y*$scale+$scale-1,
							$afigure
						);
						imagefilledrectangle(
							$allim,
							$cix*$h*$scale+$x*$scale,
							$v*$scale+$y*$scale,
							$cix*$h*$scale+$x*$scale+$scale-1,
							$v*$scale+$y*$scale+$scale-1,
							$aground
						);
					}else{
						imagefilledrectangle(
							$im,
							$x*$scale,
							$y*$scale,
							$x*$scale+$scale-1,
							$y*$scale+$scale-1,
							$nground
						);
						imagefilledrectangle(
							$rim,
							$x*$scale,
							$y*$scale,
							$x*$scale+$scale-1,
							$y*$scale+$scale-1,
							$rground
						);
						imagefilledrectangle(
							$allim,
							$cix*$h*$scale+$x*$scale,
							$y*$scale,
							$cix*$h*$scale+$x*$scale+$scale-1,
							$y*$scale+$scale-1,
							$aground
						);
						imagefilledrectangle(
							$allim,
							$cix*$h*$scale+$x*$scale,
							$v*$scale+$y*$scale,
							$cix*$h*$scale+$x*$scale+$scale-1,
							$v*$scale+$y*$scale+$scale-1,
							$afigure
						);
					}
				}
			}
		}
		if(!is_numeric($c) || $c<10) $c=ord($c);
		$jnd = "cset[".$c."]='data:image/gif;base64,";
		$cnd = ".gc".$c."n{background-image:url(data:image/gif;base64,";
		$cng = ".gc".$c."n{background-position:-".$h*$scale*$cix."px 0px;}";
		ob_start();
		imagegif($im);
		$normal = ob_get_clean();
		$jnd .= base64_encode($normal)."';\n";
		$cnd .= base64_encode($normal).");}";
		fwrite($fhjd,$jnd);
		fwrite($fhcd,$cnd);
		fwrite($fhcg,$cng);
		$jrd = "csetrvs[".$c."]='data:image/gif;base64,";
		$crd = ".gc".$c."r{background-image:url(data:image/gif;base64,";
		$crg = ".gc".$c."r{background-position:-".$h*$scale*$cix."px -".$v*$scale."px;}";
		ob_start();
		imagegif($rim);
		$reverse = ob_get_clean();
		$jrd .= base64_encode($reverse)."';\n";
		$crd .= base64_encode($reverse).");}";
		fwrite($fhjd,$jrd);
		fwrite($fhcd,$crd);
		fwrite($fhcg,$crg);
	}
	$bim = imagecreate($h*$scale, $v*$scale);
	$tc=imagecolorallocate($bim,0,0,0);
	$trans=imagecolortransparent($bim,$tc);
	ob_start();
	imagegif($bim);
	$data=ob_get_clean();
	$bfh=fopen(CLI_DIR.'/'.$blank,"w");
	fwrite($bfh,$data);
	fclose($bfh);

	ob_start();
	imagegif($allim);
	$data=ob_get_clean();
	$afh=fopen($gifbase,"w");
	fwrite($afh,$data);
	fclose($afh);

	fwrite($fhcd,"\n</style>");
	fwrite($fhcg,"\n</style>");
	fwrite($fhjd,"var BLANK='data:image/gif;base64,".base64_encode($blank)."';");
	fclose($fhjd);
	fclose($fhcd);
	fclose($fhcg);

	switch($scheme){
		case 'js/data':
			include($jscachedata);
			break;
		case 'css/data':
			include($csscachedata);
			break;
		case 'css/gif':
			include($csscachegif);
			break;
		default:
			echo('No scheme specified');
	}
}
?>
