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