#!/usr/bin/env php * @copyright 2011-2024 Nicola Asuni - Tecnick.com LTD * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) * @link https://github.com/tecnickcom/tc-lib-pdf-font * * This file is part of tc-lib-pdf-font software library. * * Command-line tool to convert fonts data for the tc-lib-pdf-font library. */ if (php_sapi_name() != 'cli') { fwrite(STDERR, 'You need to run this command from console.'."\n"); exit(1); } /** * Display help guide for this command. */ function showHelp() { $help = << './', 'type' => '', 'encoding' => '', 'flags' => 32, 'platform_id' => 3, 'encoding_id' => 1, 'linked' => false ); // short input options $sopt = 't:e:f:o:p:n:li:h'; // long input options $lopt = array( 'outpath:', 'type:', 'encoding:', 'flags:', 'platform_id:', 'encoding_id:', 'linked', 'fonts:', 'help' ); // parse input options $inopt = getopt($sopt, $lopt); // import options (with some sanitization) foreach ($inopt as $opt => $val) { switch ($opt) { case 'o': case 'outpath': $options['outpath'] = realpath($val); if (substr($options['outpath'], -1) != '/') { $options['outpath'] .= '/'; } break; case 't': case 'type': if (in_array($val, array('TrueTypeUnicode', 'TrueType', 'Type1', 'CID0JP', 'CID0KR', 'CID0CS', 'CID0CT'))) { $options['type'] = $val; } break; case 'e': case 'encoding': $options['encoding'] = $val; break; case 'f': case 'flags': $options['flags'] = intval($val); break; case 'p': case 'platform_id': $options['platform_id'] = min(max(1, intval($val)), 3); break; case 'n': case 'encoding_id': $options['encoding_id'] = min(max(0, intval($val)), 10); break; case 'l': case 'linked': $options['linked'] = true; break; case 'i': case 'fonts': $options['fonts'] = explode(',', $val); break; case 'h': case 'help': default: showHelp(); break; } } // check input values if (!is_dir($options['outpath']) || !is_writable($options['outpath'])) { fwrite(STDERR, 'ERROR: Can\'t write to '.$options['outpath']."\n\n"); exit(2); } if (empty($options['fonts'])) { fwrite(STDERR, 'ERROR: missing input fonts (try --help for usage)'."\n\n"); exit(3); } fwrite(STDOUT, "\n".'>>> Converting fonts:'."\n".'*** Output directory set to '.$options['outpath']."\n"); // count conversions $convert_errors = 0; $convert_success = 0; require_once (dirname(dirname(__DIR__)).'/vendor/autoload.php'); foreach ($options['fonts'] as $font) { try { $import = new \Com\Tecnick\Pdf\Font\Import( realpath($font), $options['outpath'], $options['type'], $options['encoding'], $options['flags'], $options['platform_id'], $options['encoding_id'], $options['linked'] ); $fontname = $import->getFontName(); fwrite(STDOUT, "\033[32m".'+++ OK : '.$font.' added as '.$fontname."\033[m\n"); ++$convert_success; } catch (\Exception $exc) { ++$convert_errors; fwrite(STDERR, "\033[31m".'--- ERROR: can\'t add '.$font."\n ".$exc->getMessage()."\033[m\n"); } } $endmsg = '>>> PROCESS COMPLETED: '.$convert_success.' CONVERTED FONT(S), '.$convert_errors.' ERROR(S)!'."\n\n"; if ($convert_errors > 0) { fwrite(STDERR, "\033[31m".$endmsg.'ERROR'."\033[m"); exit(4); } fwrite(STDOUT, "\033[32m".$endmsg."\033[m"); exit(0);