<?php
/*
			  char/	char/	char/
	Font	: inch	6inch
	Normal	: 	10	 51
	Font A	: 	12	 64
	Big		: 	 6	 32
	Super	: 	12	 64
	Italic	: 	12	 64

*/

if (!defined("_ESCPOS"))
{
	define("_ESCPOS", 1);

	define("_ESC", chr(0x1B));
	define("_GS", chr(0x1D));
	define("_DLE", chr(0x10));
	define("_FS", chr(0x1C));

	define("_FF", chr(0x0C));
	define("_SP", chr(0x20));
	define("_EOT", chr(0x04));
	define("_DC4", chr(0x14));

	define("_ESC_INIT", _ESC . "@");

	define("_ALIGN_LEFT", _ESC . "a0");
	define("_ALIGN_CENTER", _ESC . "a1");
	define("_ALIGN_RIGHT", _ESC . "a2");
	define("_ALIGN_JUSTIFY", _ESC . "a3");

	define("_FNTA", _ESC . "M" . chr(0x00));
	define("_FNTB", _ESC . "M" . chr(0x01));
	define("_FNTC", _ESC . "M" . chr(0x02));
	
	define("_SANSSERIF", _ESC . "k1");
	define("_ROMAN", _ESC . "k0");

	define("_FNTNORMAL", _ESC . "E" . chr(0x00));

	define("_FNTBOLD_ON", _ESC . "E");
	define("_FNTBOLD_OFF", _ESC . "F");

	define("_FNTITALIC_ON", _ESC . "4");
	define("_FNTITALIC_OFF", _ESC . "5");

	define("_FNTUNDERLINE_ON", _ESC . "-1");
	define("_FNTUNDERLINE_OFF", _ESC . "-0");

	define("_FNTDOUBLE_ON", _ESC . "G");
	define("_FNTDOUBLE_OFF", _ESC . "H");

	define("_FNTBIG_ON", _ESC . "W1");
	define("_FNTBIG_OFF", _ESC . "W0");
	// return chr(27) . chr(14) . $str . chr(27) . chr(20);

	define("_FNTSUP_ON", _ESC . "S0");
	define("_FNTSUP_OFF", _ESC . "T");

	define("_FNTSUB_ON", _ESC . "S1");
	define("_FNTSUB_OFF", _ESC . "T");

	define("_JUMP", _ESC . "J");
	define("_JUMPR", _ESC . "j");
}

class EscPos
{
	static $CharLen = 73; // 80

	static $lines = 66; // 66

	static function init()
	{
		return _ESC_INIT;
	}

	static function FontA($str="")
	{
		return _FNTA . $str . _FNTNORMAL;
	}

	static function FontB($str="")
	{
		return _FNTB . $str . _FNTNORMAL;
	}

	static function FontC($str="")
	{
		return _FNTC . $str . _FNTNORMAL;
	}

	static function Normal($str="")
	{
		return _FNTNORMAL . $str;
	}

	static function Bold($str="")
	{
		return _FNTBOLD_ON . $str . _FNTBOLD_OFF;
		// return _ESC . "E" . $str . _ESC . "F";
	}

	static function Italic($str="")
	{
		return _FNTITALIC_ON . $str . _FNTITALIC_OFF;
	}

	static function Big($str="")
	{
		return _FNTBIG_ON . $str . _FNTBIG_OFF;
	}

	static function DoubleStrike($str="")
	{
		return _FNTDOUBLE_ON . $str . _FNTDOUBLE_OFF;
	}

	static function SuperScript($str="")
	{
		return _FNTSUP_ON . $str . _FNTSUP_OFF;
	}

	static function SubScript($str="")
	{
		return _FNTSUB_ON . $str . _FNTSUB_OFF;
	}

	static function Center($str="")
	{
		return _ALIGN_CENTER . $str . _ALIGN_LEFT;
	}

	static function DrawLine($str="-", $wrap=false)
	{
		$ret = "";
		if ($wrap)
		{
			$ret .= PHP_EOL;
		}

		$ret .= str_repeat("-", self::$CharLen);

		if ($wrap)
		{
			$ret .= PHP_EOL;
		}

		return $ret;
	}

	static function Feed($inch=1, $isReverse=false)
	{
		$units = round(216 * $inch);

		$ret = "";
		if ($units > 216)
		{
			for ($i=0; $i<floor($units / 216); $i++)
			{
				$ret .= ($isReverse ? _JUMPR : _JUMP) . chr(216);
			}

			$ret .= ($isReverse ? _JUMPR : _JUMP) . chr($units % 216);
		}
		else
		{
			$ret .= ($isReverse ? _JUMPR : _JUMP) . chr($units);
		}

		return $ret;
	}

	static function CleanChars($str="")
	{
		return str_replace(array(
			_ESC_INIT,
			_FNTBOLD_ON,
			_FNTBOLD_OFF,
			_FNTBIG_ON,
			_FNTBIG_OFF,
			_FNTDOUBLE_ON,
			_FNTDOUBLE_OFF,
			_FNTSUP_ON,
			_FNTSUP_OFF,
			_FNTSUB_ON,
			_FNTSUB_OFF
		), "", $str);
	}

	static function DrawTable($tableData=array())
	{
		// $tableData
	}

	static function toHTML($str="")
	{
		$str = $str;

		$str = str_replace(array(
			_ESC_INIT,
			_ALIGN_LEFT, _ALIGN_CENTER, _ALIGN_RIGHT, _ALIGN_JUSTIFY,
			_FNTA, _FNTB, _FNTC,
			_SANSSERIF, _ROMAN,
			_FNTNORMAL,
			_JUMP, _JUMPR
		), "", $str);

		$str = str_replace(array(
			_FNTBOLD_ON, _FNTBOLD_OFF,
			_FNTITALIC_ON, _FNTITALIC_OFF,
			_FNTUNDERLINE_ON, _FNTUNDERLINE_OFF,
			_FNTDOUBLE_ON, _FNTDOUBLE_OFF,
			_FNTBIG_ON, _FNTBIG_OFF,
			_FNTSUP_ON, _FNTSUP_OFF,
			_FNTSUB_ON, _FNTSUB_OFF,
		), array(
			"<b>", "</b>",
			"<i>", "</i>",
			"<u>", "</u>",
			"<big>", "</big>",
			"<h3 style=\"margin:0;\">", "</h3>",
			"<sup>", "</sup>",
			"<sub>", "</sub>",
		), $str);

		return $str;
	}

	// EscPos::testPrint();
	static function testPrint()
	{
		$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$chars .= strtolower($chars);
		$chars .= "0123456789";
		$line = str_repeat($chars, 2);

		$return = "";
		$return .= EscPos::init();
		$return .= EscPos::Normal("NORMAL" . $line) . PHP_EOL;
		$return .= EscPos::FontA("FONTA" . $line) . PHP_EOL;
		$return .= EscPos::FontB("FONTB" . $line) . PHP_EOL;
		$return .= EscPos::FontC("FONTC" . $line) . PHP_EOL;
		$return .= EscPos::Bold("Bold" . $line) . PHP_EOL;
		$return .= EscPos::Italic("Italic" . $line) . PHP_EOL;
		$return .= EscPos::Big("Big" . $line) . PHP_EOL;
		$return .= EscPos::SuperScript("SuperScript" . $line) . PHP_EOL;
		$return .= EscPos::SubScript("SubScript" . $line) . PHP_EOL;
		$return .= str_repeat("-", strlen($line));

		return $return;
	}
}
/*

$content = "";
$content .= EscPos::Init();
$content .= _ESC . "j9";
// $content .= _ESC . "J5";
// $content .= "Hello WORLD" . PHP_EOL;
// $content .= EscPos::Bold("Bold Hello WORLD") . PHP_EOL;
// $content .= EscPos::Bold(EscPos::Big(EscPos::Center("RAGA Pvt Ltd"))) . PHP_EOL;
// $content .= EscPos::Center("143 South Car Street, Sivakasi-626123") . PHP_EOL;
// $content .= EscPos::DoubleStrike(EscPos::Big("TN20AR1500")) . PHP_EOL;
$content .= EscPos::Bold(EscPos::DoubleStrike(EscPos::Big("TN20AR1500"))) . PHP_EOL;


// $content .= EscPos::DoubleStrike("Double Hello WORLD") . PHP_EOL;

file_put_contents("//localhost/EPSON LX-300+II ESCP", $content);

*/


/*
// Total 80 Column

	|No |Item Name         | Qty    | Rate | Amount |
	|001|Item Name Computer|0012.567|

Width

S.No = 4
HSN/SAC = 8
Qty = 12
Rate = 12
Tax = 12
Discount = 8
Amount = 12

Item Name = ?


Total Col = 73

6 + 4 + 12 + 12 + 12


$table = array(
	"columns" => array(
		"sNo" => array("text"=>"No", "align"=>"right", "wid"=>4),
		"itemName" => array("text"=>"Item Name", "align"=>"left", "wid"=>27),
		"qty" => array("text"=>"Qty (ton)", "align"=>"right", "wid"=>12),
		"rate" => array("text"=>"Rate (INR)", "align"=>"right", "wid"=>12),
		"totAmount" => array("text"=>"Amount", "align"=>"right", "wid"=>12)
	),
	"tbody" => array(
		array("sNo" => "1", "itemName" => "M-Sand", "qty" => "12.457", "rate" => "700.00", "totAmount" => "8719.9"),
		array("sNo" => "2", "itemName" => "P-Sand", "qty" => "12.457", "rate" => "700.00", "totAmount" => "8719.9"),
		array("sNo" => "3", "itemName" => "K-Sand", "qty" => "12.457", "rate" => "700.00", "totAmount" => "8719.9")
	)
);

*/

