#!/usr/bin/php

<?php

// Copyright (C) 2007 Cygnus <cygnus2 at post.cz>
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA

define ("CFAIL","\033[1;31;40m !\033[0m ");
define ("COK","\033[1;32;40m *\033[0m ");
define ("CWARN","\033[1;33;40m ?\033[0m ");
define ("CTRY","\033[1;34;40m -\033[0m ");

define ("CRED","\033[1;31;40m");
define ("CGRN","\033[1;32;40m");
define ("CYELL","\033[1;33;40m");
define ("CBLUE","\033[1;34;40m");
define ("CMAGN","\033[1;35;40m");
define ("CCYAN","\033[1;36;40m");
define ("CWHITE","\033[1;37;40m");

define ("CDEF","\033[0m");

// configuration
//$cfg_infile		= './rowmans.jhf';	// filename of input file
$cfg_infile		= './rowmans_upraveny.jhf';
//$cfg_infile		= './rowmant.jhf';	// filename of input file
//$cfg_infile		= './scripts.jhf';	// filename of input file
//$cfg_infile		= './scriptc.jhf';	// filename of input file
//$cfg_infile		= './gothicger.jhf';	// filename of input file
//$cfg_infile		= './gothiceng.jhf';	// filename of input file

$cfg_xoffset		= 11;			// half of character width (monospace printing)
$cfg_yoffset		= 16;			// move character higher, because coordinates must be positive (> 0)

// --------------------------------------------------------------------------------------

function convert_font_to_z80_source ($infile) {
	global $cfg_xoffset;
	global $cfg_yoffset;

	$pen = 0;		// pen status 0 = up, 1 = down

	// read line by line
	echo "; converted Hersey font for AS compiler and Z80 \n";
	
	$char = 32;
	
	while (($line = fgets($infile)) !== false) {
		$length = strlen($line);
		if ($length > 8) {
			$glyph_number = 0;
			
			for ($n = 0; $n < 5; $n++) {
				if (($line[$n] >= '0') && ($line[$n] <= '9')) {
					$glyph_number = $glyph_number * 10;
					$glyph_number += ($line[$n] - '0');
				}
			}
			echo "; char. ".$char." glyph ".$glyph_number." ";
			
			$pairs = 0;
			
			for ($n = 6; $n < 8; $n++) {
				if (($line[$n] >= '0') && ($line[$n] <= '9')) {
					$pairs = $pairs * 10;
					$pairs += ($line[$n] - '0');
				}
			}
			
			for ($n = 0; $n < $pairs; $n++) {
				echo ($line[8+2*$n].$line[9+2*$n])." ";
			}
			echo "\n";
			
			echo "\t\tdb\t";
			echo ($pairs - 1)."\n";
			
			$pair_counter = 0;

			for ($n = 0; $n < $pairs; $n++) {
				// pair 0 is start and end of glyph for variable spacing
				if ($n > 0) {
					// AS compiler cannot compile more than ...? bytes in db pseudoinstruction
					if ($pair_counter == 0) echo "\t\tdb\t"; else echo ", ";

					if (($line[8+2*$n] == ' ') && ($line[9+2*$n] == 'R')) {
						echo "128,128";
						$pen = 0;
					} else {
						$x = -(ord ('R') - ord ($line[8+2*$n]));
						$y = ord ('R') - ord ($line[9+2*$n]);
						
						$x = $cfg_xoffset + $x;
						$y = $cfg_yoffset + $y;
						
						echo $x.",".$y;
						$pen = 1;
					}
					$pair_counter++;
					
					if ($pair_counter > 8) {
						$pair_counter = 0;
						echo "\n";
					}
				}
			}
			echo "\n";
			$pen = 0;
			$char++;
		}
	}
}

// --------------------------------------------------------------------------------------

if (file_exists ($cfg_infile)) {
	$infile = fopen ($cfg_infile, "r");

	if ($infile) {
		convert_font_to_z80_source ($infile);
		if ($infile) fclose ($infile);
	}
	else
		if (!$infile) echo CFAIL."Opening file \"$cfg_infile\" failed.\n".CDEF;
} else
	if (!$infile) echo CFAIL."File \"$cfg_infile\" does not exists.\n".CDEF;

return (0);

?>