tcpdf_addfont.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/usr/bin/env php
  2. <?php
  3. //============================================================+
  4. // File name : tcpdf_addfont.php
  5. // Version : 1.0.002
  6. // Begin : 2013-05-13
  7. // Last Update : 2013-08-05
  8. // Authors : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
  9. // Remi Collet
  10. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  11. // -------------------------------------------------------------------
  12. // Copyright (C) 2011-2013 Nicola Asuni - Tecnick.com LTD
  13. //
  14. // This file is part of TCPDF software library.
  15. //
  16. // TCPDF is free software: you can redistribute it and/or modify it
  17. // under the terms of the GNU Lesser General Public License as
  18. // published by the Free Software Foundation, either version 3 of the
  19. // License, or (at your option) any later version.
  20. //
  21. // TCPDF is distributed in the hope that it will be useful, but
  22. // WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. // See the GNU Lesser General Public License for more details.
  25. //
  26. // You should have received a copy of the License
  27. // along with TCPDF. If not, see
  28. // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
  29. //
  30. // See LICENSE.TXT file for more information.
  31. // -------------------------------------------------------------------
  32. //
  33. // Description : This is a command line script to generate TCPDF fonts.
  34. //
  35. //============================================================+
  36. /**
  37. * @file
  38. * This is a command line script to generate TCPDF fonts.<br>
  39. * @package com.tecnick.tcpdf
  40. * @version 1.0.000
  41. */
  42. if (php_sapi_name() != 'cli') {
  43. echo 'You need to run this command from console.';
  44. exit(1);
  45. }
  46. $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');
  47. foreach ($tcpdf_include_dirs as $tcpdf_include_path) {
  48. if (@file_exists($tcpdf_include_path)) {
  49. require_once($tcpdf_include_path);
  50. break;
  51. }
  52. }
  53. /**
  54. * Display help guide for this command.
  55. */
  56. function showHelp() {
  57. $help = <<<EOD
  58. tcpdf_addfont - command line tool to convert fonts for the TCPDF library.
  59. Usage: tcpdf_addfont.php [ options ] -i fontfile[,fontfile]...
  60. Options:
  61. -t
  62. --type Font type. Leave empty for autodetect mode.
  63. Valid values are:
  64. TrueTypeUnicode
  65. TrueType
  66. Type1
  67. CID0JP = CID-0 Japanese
  68. CID0KR = CID-0 Korean
  69. CID0CS = CID-0 Chinese Simplified
  70. CID0CT = CID-0 Chinese Traditional
  71. -e
  72. --enc Name of the encoding table to use. Leave empty for
  73. default mode. Omit this parameter for TrueType Unicode
  74. and symbolic fonts like Symbol or ZapfDingBats.
  75. -f
  76. --flags Unsigned 32-bit integer containing flags specifying
  77. various characteristics of the font (PDF32000:2008 -
  78. 9.8.2 Font Descriptor Flags): +1 for fixed font; +4 for
  79. symbol or +32 for non-symbol; +64 for italic. Fixed and
  80. Italic mode are generally autodetected so you have to
  81. set it to 32 = non-symbolic font (default) or 4 =
  82. symbolic font.
  83. -o
  84. --outpath Output path for generated font files (must be writeable
  85. by the web server). Leave empty for default font folder.
  86. -p
  87. --platid Platform ID for CMAP table to extract (when building a
  88. Unicode font for Windows this value should be 3, for
  89. Macintosh should be 1).
  90. -n
  91. --encid Encoding ID for CMAP table to extract (when building a
  92. Unicode font for Windows this value should be 1, for
  93. Macintosh should be 0). When Platform ID is 3, legal
  94. values for Encoding ID are: 0=Symbol, 1=Unicode,
  95. 2=ShiftJIS, 3=PRC, 4=Big5, 5=Wansung, 6=Johab,
  96. 7=Reserved, 8=Reserved, 9=Reserved, 10=UCS-4.
  97. -b
  98. --addcbbox Includes the character bounding box information on the
  99. php font file.
  100. -l
  101. --link Link to system font instead of copying the font data #
  102. (not transportable) - Note: do not work with Type1 fonts.
  103. -i
  104. --fonts Comma-separated list of input font files.
  105. -h
  106. --help Display this help and exit.
  107. EOD;
  108. echo $help."\n\n";
  109. exit(0);
  110. }
  111. // remove the name of the executing script
  112. array_shift($argv);
  113. // no options chosen
  114. if (!is_array($argv)) {
  115. showHelp();
  116. }
  117. // initialize the array of options
  118. $options = array('type'=>'', 'enc'=>'', 'flags'=>32, 'outpath'=>K_PATH_FONTS, 'platid'=>3, 'encid'=>1, 'addcbbox'=>false, 'link'=>false);
  119. // short input options
  120. $sopt = '';
  121. $sopt .= 't:';
  122. $sopt .= 'e:';
  123. $sopt .= 'f:';
  124. $sopt .= 'o:';
  125. $sopt .= 'p:';
  126. $sopt .= 'n:';
  127. $sopt .= 'b';
  128. $sopt .= 'l';
  129. $sopt .= 'i:';
  130. $sopt .= 'h';
  131. // long input options
  132. $lopt = array();
  133. $lopt[] = 'type:';
  134. $lopt[] = 'enc:';
  135. $lopt[] = 'flags:';
  136. $lopt[] = 'outpath:';
  137. $lopt[] = 'platid:';
  138. $lopt[] = 'encid:';
  139. $lopt[] = 'addcbbox';
  140. $lopt[] = 'link';
  141. $lopt[] = 'fonts:';
  142. $lopt[] = 'help';
  143. // parse input options
  144. $inopt = getopt($sopt, $lopt);
  145. // import options (with some sanitization)
  146. foreach ($inopt as $opt => $val) {
  147. switch ($opt) {
  148. case 't':
  149. case 'type': {
  150. if (in_array($val, array('TrueTypeUnicode', 'TrueType', 'Type1', 'CID0JP', 'CID0KR', 'CID0CS', 'CID0CT'))) {
  151. $options['type'] = $val;
  152. }
  153. break;
  154. }
  155. case 'e':
  156. case 'enc': {
  157. $options['enc'] = $val;
  158. break;
  159. }
  160. case 'f':
  161. case 'flags': {
  162. $options['flags'] = intval($val);
  163. break;
  164. }
  165. case 'o':
  166. case 'outpath': {
  167. $options['outpath'] = realpath($val);
  168. if (substr($options['outpath'], -1) != '/') {
  169. $options['outpath'] .= '/';
  170. }
  171. break;
  172. }
  173. case 'p':
  174. case 'platid': {
  175. $options['platid'] = min(max(1, intval($val)), 3);
  176. break;
  177. }
  178. case 'n':
  179. case 'encid': {
  180. $options['encid'] = min(max(0, intval($val)), 10);
  181. break;
  182. }
  183. case 'b':
  184. case 'addcbbox': {
  185. $options['addcbbox'] = true;
  186. break;
  187. }
  188. case 'l':
  189. case 'link': {
  190. $options['link'] = true;
  191. break;
  192. }
  193. case 'i':
  194. case 'fonts': {
  195. $options['fonts'] = explode(',', $val);
  196. break;
  197. }
  198. case 'h':
  199. case 'help':
  200. default: {
  201. showHelp();
  202. break;
  203. }
  204. } // end of switch
  205. } // end of while loop
  206. if (empty($options['fonts'])) {
  207. echo "ERROR: missing input fonts (try --help for usage)\n\n";
  208. exit(2);
  209. }
  210. // check the output path
  211. if (!is_dir($options['outpath']) OR !is_writable($options['outpath'])) {
  212. echo "ERROR: Can't write to ".$options['outpath']."\n\n";
  213. exit(3);
  214. }
  215. echo "\n>>> Converting fonts for TCPDF:\n";
  216. echo '*** Output dir set to '.$options['outpath']."\n";
  217. // check if there are conversion errors
  218. $errors = false;
  219. foreach ($options['fonts'] as $font) {
  220. $fontfile = realpath($font);
  221. $fontname = TCPDF_FONTS::addTTFfont($fontfile, $options['type'], $options['enc'], $options['flags'], $options['outpath'], $options['platid'], $options['encid'], $options['addcbbox'], $options['link']);
  222. if ($fontname === false) {
  223. $errors = true;
  224. echo "--- ERROR: can't add ".$font."\n";
  225. } else {
  226. echo "+++ OK : ".$fontfile.' added as '.$fontname."\n";
  227. }
  228. }
  229. if ($errors) {
  230. echo "--- Process completed with ERRORS!\n\n";
  231. exit(4);
  232. }
  233. echo ">>> Process successfully completed!\n\n";
  234. exit(0);
  235. //============================================================+
  236. // END OF FILE
  237. //============================================================+