#!/usr/bin/env php
.
//
// See LICENSE.TXT file for more information.
// -------------------------------------------------------------------
//
// Description : This is a command line script to generate TCPDF fonts.
//
//============================================================+
/**
 * @file
 * This is a command line script to generate TCPDF fonts.
 * @package com.tecnick.tcpdf
 * @version 1.0.000
 */
if (php_sapi_name() != 'cli') {
  echo 'You need to run this command from console.';
  exit(1);
}
$tcpdf_include_dirs = array(realpath(dirname(__FILE__).'/../tcpdf.php'), '/usr/share/php/tcpdf/tcpdf.php', '/usr/share/tcpdf/tcpdf.php', '/usr/share/php-tcpdf/tcpdf.php', '/var/www/tcpdf/tcpdf.php', '/var/www/html/tcpdf/tcpdf.php', '/usr/local/apache2/htdocs/tcpdf/tcpdf.php');
foreach ($tcpdf_include_dirs as $tcpdf_include_path) {
	if (@file_exists($tcpdf_include_path)) {
		require_once($tcpdf_include_path);
		break;
	}
}
/**
 * Display help guide for this command.
 */
function showHelp() {
	$help = <<'', 'enc'=>'', 'flags'=>32, 'outpath'=>K_PATH_FONTS, 'platid'=>3, 'encid'=>1, 'addcbbox'=>false, 'link'=>false);
// short input options
$sopt = '';
$sopt .= 't:';
$sopt .= 'e:';
$sopt .= 'f:';
$sopt .= 'o:';
$sopt .= 'p:';
$sopt .= 'n:';
$sopt .= 'b';
$sopt .= 'l';
$sopt .= 'i:';
$sopt .= 'h';
// long input options
$lopt = array();
$lopt[] = 'type:';
$lopt[] = 'enc:';
$lopt[] = 'flags:';
$lopt[] = 'outpath:';
$lopt[] = 'platid:';
$lopt[] = 'encid:';
$lopt[] = 'addcbbox';
$lopt[] = 'link';
$lopt[] = 'fonts:';
$lopt[] = 'help';
// parse input options
$inopt = getopt($sopt, $lopt);
// import options (with some sanitization)
foreach ($inopt as $opt => $val) {
	switch ($opt) {
		case 't':
		case 'type': {
			if (in_array($val, array('TrueTypeUnicode', 'TrueType', 'Type1', 'CID0JP', 'CID0KR', 'CID0CS', 'CID0CT'))) {
				$options['type'] = $val;
			}
			break;
		}
		case 'e':
		case 'enc': {
			$options['enc'] = $val;
			break;
		}
		case 'f':
		case 'flags': {
			$options['flags'] = intval($val);
			break;
		}
		case 'o':
		case 'outpath': {
			$options['outpath'] = realpath($val);
			if (substr($options['outpath'], -1) != '/') {
				$options['outpath'] .= '/';
			}
			break;
		}
		case 'p':
		case 'platid': {
			$options['platid'] = min(max(1, intval($val)), 3);
			break;
		}
		case 'n':
		case 'encid': {
			$options['encid'] = min(max(0, intval($val)), 10);
			break;
		}
		case 'b':
		case 'addcbbox': {
			$options['addcbbox'] = true;
			break;
		}
		case 'l':
		case 'link': {
			$options['link'] = true;
			break;
		}
		case 'i':
		case 'fonts': {
			$options['fonts'] = explode(',', $val);
			break;
		}
		case 'h':
		case 'help':
		default: {
			showHelp();
			break;
		}
	} // end of switch
} // end of while loop
if (empty($options['fonts'])) {
	echo "ERROR: missing input fonts (try --help for usage)\n\n";
	exit(2);
}
// check the output path
if (!is_dir($options['outpath']) OR !is_writable($options['outpath'])) {
	echo "ERROR: Can't write to ".$options['outpath']."\n\n";
	exit(3);
}
echo "\n>>> Converting fonts for TCPDF:\n";
echo '*** Output dir set to '.$options['outpath']."\n";
// check if there are conversion errors
$errors = false;
foreach ($options['fonts'] as $font) {
	$fontfile = realpath($font);
	$fontname = TCPDF_FONTS::addTTFfont($fontfile, $options['type'], $options['enc'], $options['flags'], $options['outpath'], $options['platid'], $options['encid'], $options['addcbbox'], $options['link']);
	if ($fontname === false) {
		$errors = true;
		echo "--- ERROR: can't add ".$font."\n";
	} else {
		echo "+++ OK   : ".$fontfile.' added as '.$fontname."\n";
	}
}
if ($errors) {
	echo "--- Process completed with ERRORS!\n\n";
	exit(4);
}
echo ">>> Process successfully completed!\n\n";
exit(0);
//============================================================+
// END OF FILE
//============================================================+