60 function MakeFont($fontfile, $fmfile, $embedded=
true,
$enc=
'cp1252', $patch=array()) {
62 set_magic_quotes_runtime(0);
63 ini_set(
'auto_detect_line_endings',
'1');
64 if (!file_exists($fontfile)) {
65 die(
'Error: file not found: '.$fontfile);
67 if (!file_exists($fmfile)) {
68 die(
'Error: file not found: '.$fmfile);
74 $ffext = strtolower(substr($fontfile, -3));
75 $fmext = strtolower(substr($fmfile, -3));
76 if ($fmext ==
'afm') {
77 if (($ffext ==
'ttf') OR ($ffext ==
'otf')) {
79 } elseif ($ffext ==
'pfb') {
82 die(
'Error: unrecognized font file extension: '.$ffext);
86 foreach ($patch as $cc => $gn) {
91 if (isset($widths[
'.notdef'])) {
92 $dw = $widths[
'.notdef'];
98 } elseif ($fmext ==
'ufm') {
100 if (($ffext ==
'ttf') OR ($ffext ==
'otf')) {
101 $type =
'TrueTypeUnicode';
103 die(
'Error: not a TrueType font: '.$ffext);
105 $fm =
ReadUFM($fmfile, $cidtogidmap);
106 $dw = $fm[
'MissingWidth'];
111 $s .=
'$type=\''.$type.
"';\n";
112 $s .=
'$name=\''.$fm[
'FontName'].
"';\n";
113 $s .=
'$desc='.$fd.
";\n";
114 if (!isset($fm[
'UnderlinePosition'])) {
115 $fm[
'UnderlinePosition'] = -100;
117 if (!isset($fm[
'UnderlineThickness'])) {
118 $fm[
'UnderlineThickness'] = 50;
120 $s .=
'$up='.$fm[
'UnderlinePosition'].
";\n";
121 $s .=
'$ut='.$fm[
'UnderlineThickness'].
";\n";
123 if (isset($fm[
'Widths'][32]) AND ($fm[
'Widths'][32] > 0)) {
125 $dw = $fm[
'Widths'][32];
130 $s .=
'$dw='.$dw.
";\n";
132 $s .=
'$cw='.$w.
";\n";
133 $s .=
'$enc=\''.$enc.
"';\n";
134 $s .=
'$diff=\''.$diff.
"';\n";
135 $basename = substr(basename($fmfile), 0, -4);
138 if ((
$type ==
'TrueType') OR (
$type ==
'TrueTypeUnicode')) {
141 $f = fopen($fontfile,
'rb');
143 die(
'Error: Unable to open '.$fontfile);
145 $file = fread(
$f, filesize($fontfile));
147 if (
$type ==
'Type1') {
149 $header = (ord(
$file{0}) == 128);
156 die(
'Error: font file does not seem to be valid Type1');
159 if ($header AND (ord(
$file{$size1}) == 128)) {
165 die(
'Error: font file does not seem to be valid Type1');
167 $size2 =
$pos - $size1;
170 $basename = strtolower($basename);
171 if (function_exists(
'gzcompress')) {
172 $cmp = $basename.
'.z';
174 $s .=
'$file=\''.$cmp.
"';\n";
175 print
"Font file compressed (".$cmp.
")\n";
176 if (!empty($cidtogidmap)) {
177 $cmp = $basename.
'.ctg.z';
178 SaveToFile($cmp, gzcompress($cidtogidmap, 9),
'b');
179 print
"CIDToGIDMap created and compressed (".$cmp.
")\n";
180 $s .=
'$ctg=\''.$cmp.
"';\n";
183 $s .=
'$file=\''.basename($fontfile).
"';\n";
184 print
"Notice: font file could not be compressed (zlib extension not available)\n";
185 if (!empty($cidtogidmap)) {
186 $cmp = $basename.
'.ctg';
187 $f = fopen($cmp,
'wb');
188 fwrite(
$f, $cidtogidmap);
190 print
"CIDToGIDMap created (".$cmp.
")\n";
191 $s .=
'$ctg=\''.$cmp.
"';\n";
194 if(
$type ==
'Type1') {
195 $s .=
'$size1='.$size1.
";\n";
196 $s .=
'$size2='.$size2.
";\n";
198 $s.=
'$originalsize='.filesize($fontfile).
";\n";
202 $s .=
'$file='.
"'';\n";
204 $s .=
'// --- EOF ---';
206 print
"Font definition file generated (".$basename.
".php)\n";
215 $file = dirname(__FILE__).
'/enc/'.strtolower(
$enc).
'.map';
218 die(
'Error: encoding not found: '.
$enc);
223 $e = preg_split(
'/[ \\t]+/',rtrim($l));
224 $cc = hexdec(substr($e[0],1));
229 for($i = 0; $i <= 255; $i++) {
230 if(!isset($cc2gn[$i])) {
231 $cc2gn[$i] =
'.notdef';
242 $cidtogidmap = str_pad(
'', (256 * 256 * 2),
"\x00");
246 die(
'File not found');
251 $e = explode(
' ',chop($l));
266 if($cc == ord(
'X')) {
267 $fm[
'CapXHeight'] = $e[13];
270 if (($cc >= 0) AND ($cc < 0xFFFF) AND $glyph) {
271 $cidtogidmap{($cc * 2)} = chr($glyph >> 8);
272 $cidtogidmap{(($cc * 2) + 1)} = chr($glyph & 0xFF);
275 if((isset($gn) AND ($gn ==
'.notdef')) AND (!isset($fm[
'MissingWidth']))) {
276 $fm[
'MissingWidth'] = $w;
278 } elseif($code ==
'FontName') {
279 $fm[
'FontName'] = $param;
280 } elseif($code ==
'Weight') {
281 $fm[
'Weight'] = $param;
282 } elseif($code ==
'ItalicAngle') {
283 $fm[
'ItalicAngle'] = (double)$param;
284 } elseif($code ==
'Ascender') {
285 $fm[
'Ascender'] = (int)$param;
286 } elseif($code ==
'Descender') {
287 $fm[
'Descender'] = (int)$param;
288 } elseif($code ==
'UnderlineThickness') {
289 $fm[
'UnderlineThickness'] = (int)$param;
290 } elseif($code ==
'UnderlinePosition') {
291 $fm[
'UnderlinePosition'] = (int)$param;
292 } elseif($code ==
'IsFixedPitch') {
293 $fm[
'IsFixedPitch'] = ($param ==
'true');
294 } elseif($code ==
'FontBBox') {
295 $fm[
'FontBBox'] = array($e[1], $e[2], $e[3], $e[4]);
296 } elseif($code ==
'CapHeight') {
297 $fm[
'CapHeight'] = (int)$param;
298 } elseif($code ==
'StdVW') {
299 $fm[
'StdVW'] = (int)$param;
302 if(!isset($fm[
'MissingWidth'])) {
303 $fm[
'MissingWidth'] = 600;
305 if(!isset($fm[
'FontName'])) {
306 die(
'FontName not found');
308 $fm[
'Widths'] = $widths;
319 die(
'File not found');
324 'Edot'=>
'Edotaccent',
325 'edot'=>
'edotaccent',
326 'Idot'=>
'Idotaccent',
327 'Zdot'=>
'Zdotaccent',
328 'zdot'=>
'zdotaccent',
329 'Odblacute' =>
'Ohungarumlaut',
330 'odblacute' =>
'ohungarumlaut',
331 'Udblacute'=>
'Uhungarumlaut',
332 'udblacute'=>
'uhungarumlaut',
333 'Gcedilla'=>
'Gcommaaccent'
334 ,
'gcedilla'=>
'gcommaaccent',
335 'Kcedilla'=>
'Kcommaaccent',
336 'kcedilla'=>
'kcommaaccent',
337 'Lcedilla'=>
'Lcommaaccent',
338 'lcedilla'=>
'lcommaaccent',
339 'Ncedilla'=>
'Ncommaaccent',
340 'ncedilla'=>
'ncommaaccent',
341 'Rcedilla'=>
'Rcommaaccent',
342 'rcedilla'=>
'rcommaaccent',
343 'Scedilla'=>
'Scommaaccent',
344 'scedilla'=>
'scommaaccent',
345 'Tcedilla'=>
'Tcommaaccent',
346 'tcedilla'=>
'tcommaaccent',
351 'combininggraveaccent'=>
'gravecomb',
352 'combininghookabove'=>
'hookabovecomb',
353 'combiningtildeaccent'=>
'tildecomb',
354 'combiningacuteaccent'=>
'acutecomb',
355 'combiningdotbelow'=>
'dotbelowcomb',
359 $e = explode(
' ', rtrim($l));
370 if (substr($gn, -4) ==
'20AC') {
373 if (isset($fix[$gn])) {
375 foreach ($map as $c => $n) {
376 if ($n == $fix[$gn]) {
387 $fm[
'CapXHeight'] = $e[13];
390 if($gn ==
'.notdef') {
391 $fm[
'MissingWidth'] = $w;
393 } elseif($code ==
'FontName') {
394 $fm[
'FontName'] = $param;
395 } elseif($code ==
'Weight') {
396 $fm[
'Weight'] = $param;
397 } elseif($code ==
'ItalicAngle') {
398 $fm[
'ItalicAngle'] = (double)$param;
399 } elseif($code ==
'Ascender') {
400 $fm[
'Ascender'] = (int)$param;
401 } elseif($code ==
'Descender') {
402 $fm[
'Descender'] = (int)$param;
403 } elseif($code ==
'UnderlineThickness') {
404 $fm[
'UnderlineThickness'] = (int)$param;
405 } elseif($code ==
'UnderlinePosition') {
406 $fm[
'UnderlinePosition'] = (int)$param;
407 } elseif($code ==
'IsFixedPitch') {
408 $fm[
'IsFixedPitch'] = ($param ==
'true');
409 } elseif($code ==
'FontBBox') {
410 $fm[
'FontBBox'] = array($e[1], $e[2], $e[3], $e[4]);
411 } elseif($code ==
'CapHeight') {
412 $fm[
'CapHeight'] = (int)$param;
413 } elseif($code ==
'StdVW') {
414 $fm[
'StdVW'] = (int)$param;
417 if (!isset($fm[
'FontName'])) {
418 die(
'FontName not found');
421 if (!isset($widths[
'.notdef'])) {
422 $widths[
'.notdef'] = 600;
424 if (!isset($widths[
'Delta']) AND isset($widths[
'increment'])) {
425 $widths[
'Delta'] = $widths[
'increment'];
428 for ($i = 0; $i <= 255; $i++) {
429 if (!isset($widths[$map[$i]])) {
430 print
"Warning: character ".$map[$i].
" is missing\n";
431 $widths[$i] = $widths[
'.notdef'];
433 $widths[$i] = $widths[$map[$i]];
437 $fm[
'Widths'] = $widths;
443 $asc = (isset($fm[
'Ascender']) ? $fm[
'Ascender'] : 1000);
444 $fd =
"array('Ascent'=>".$asc;
446 $desc = (isset($fm[
'Descender']) ? $fm[
'Descender'] : -200);
447 $fd .=
",'Descent'=>".$desc;
449 if (isset($fm[
'CapHeight'])) {
450 $ch = $fm[
'CapHeight'];
451 } elseif (isset($fm[
'CapXHeight'])) {
452 $ch = $fm[
'CapXHeight'];
456 $fd .=
",'CapHeight'=>".$ch;
459 if (isset($fm[
'IsFixedPitch']) AND $fm[
'IsFixedPitch']) {
467 if (isset($fm[
'ItalicAngle']) AND ($fm[
'ItalicAngle'] != 0)) {
470 $fd .=
",'Flags'=>".$flags;
472 if (isset($fm[
'FontBBox'])) {
473 $fbb = $fm[
'FontBBox'];
475 $fbb = array(0, (
$desc - 100), 1000, ($asc + 100));
477 $fd .=
",'FontBBox'=>'[".$fbb[0].
' '.$fbb[1].
' '.$fbb[2].
' '.$fbb[3].
"]'";
479 $ia = (isset($fm[
'ItalicAngle']) ? $fm[
'ItalicAngle'] : 0);
480 $fd .=
",'ItalicAngle'=>".$ia;
482 if (isset($fm[
'StdVW'])) {
483 $stemv = $fm[
'StdVW'];
484 } elseif (isset($fm[
'Weight']) AND preg_match(
'/(bold|black)/i', $fm[
'Weight'])) {
489 $fd .=
",'StemV'=>".$stemv;
491 if(isset($fm[
'MissingWidth'])) {
492 $fd .=
",'MissingWidth'=>".$fm[
'MissingWidth'];
504 foreach (
$cw as $i => $w) {
505 if (is_numeric($i)) {
506 $els[] = (((($c++)%10) == 0) ?
"\n" :
'').$i.
'=>'.$w;
509 $s .= implode(
',', $els);
519 for ($i = 32; $i <= 255; $i++) {
520 if ($map[$i] != $ref[$i]) {
525 $s .=
'/'.$map[$i].
' ';
534 die(
'Can\'t write to file '.
$file);
536 fwrite(
$f, $s, strlen($s));
541 $a = unpack(
'n1n', fread(
$f, 2));
546 $a = unpack(
'N1N', fread(
$f, 4));
554 die(
'Error: unable to open '.
$file);
557 fseek(
$f, 4, SEEK_CUR);
559 fseek(
$f, 6, SEEK_CUR);
562 for ($i = 0; $i < $nb; $i++) {
563 if (fread(
$f, 4) ==
'OS/2') {
567 fseek(
$f, 12, SEEK_CUR);
573 fseek(
$f, 4, SEEK_CUR);
575 fseek(
$f, $offset, SEEK_SET);
577 fseek(
$f, 8, SEEK_CUR);
579 $rl = ($fsType & 0x02) != 0;
580 $pp = ($fsType & 0x04) != 0;
581 $e = ($fsType & 0x08) != 0;
583 if($rl AND (!$pp) AND (!$e)) {
584 print
"Warning: font license does not allow embedding\n";
589 if (count(
$arg) >= 3) {
592 if (
sizeof(
$arg) == 3) {
596 if (!isset(
$arg[2])) {
599 if (!isset(
$arg[3])) {
603 if (!isset(
$arg[4])) {
608 print preg_replace(
'!<BR( /)?>!i',
"\n",
$t);
610 print
"Usage: makefont.php <ttf/otf/pfb file> <afm/ufm file> <encoding> <patch>\n";