function MakeFontEncoding($map) { //Build differences from reference encoding $ref = ReadMap('cp1252'); $s = ''; $last = 0; for ($i = 32; $i <= 255; $i++) { if ($map[$i] != $ref[$i]) { if ($i != $last + 1) { $s .= $i . ' '; } $last = $i; $s .= '/' . $map[$i] . ' '; } } return rtrim($s); }
function MakeFont($fontfile, $afmfile, $enc = 'cp1252', $patch = array(), $type = 'TrueType') { //Generate a font definition file set_magic_quotes_runtime(0); ini_set('auto_detect_line_endings', '1'); if ($enc) { $map = ReadMap($enc); foreach ($patch as $cc => $gn) { $map[$cc] = $gn; } } else { $map = array(); } if (!file_exists($afmfile)) { die('<B>Error:</B> AFM file not found: ' . $afmfile); } $fm = ReadAFM($afmfile, $map); if ($enc) { $diff = MakeFontEncoding($map); } else { $diff = ''; } $fd = MakeFontDescriptor($fm, empty($map)); //Find font type if ($fontfile) { $ext = strtolower(substr($fontfile, -3)); if ($ext == 'ttf') { $type = 'TrueType'; } elseif ($ext == 'pfb') { $type = 'Type1'; } else { die('<B>Error:</B> unrecognized font file extension: ' . $ext); } } else { if ($type != 'TrueType' and $type != 'Type1') { die('<B>Error:</B> incorrect font type: ' . $type); } } //Start generation $s = '<?php' . "\n"; $s .= '$type=\'' . $type . "';\n"; $s .= '$name=\'' . $fm['FontName'] . "';\n"; $s .= '$desc=' . $fd . ";\n"; if (!isset($fm['UnderlinePosition'])) { $fm['UnderlinePosition'] = -100; } if (!isset($fm['UnderlineThickness'])) { $fm['UnderlineThickness'] = 50; } $s .= '$up=' . $fm['UnderlinePosition'] . ";\n"; $s .= '$ut=' . $fm['UnderlineThickness'] . ";\n"; $w = MakeWidthArray($fm); $s .= '$cw=' . $w . ";\n"; $s .= '$enc=\'' . $enc . "';\n"; $s .= '$diff=\'' . $diff . "';\n"; $basename = substr(basename($afmfile), 0, -4); if ($fontfile) { //Embedded font if (!file_exists($fontfile)) { die('<B>Error:</B> font file not found: ' . $fontfile); } if ($type == 'TrueType') { CheckTTF($fontfile); } $f = fopen($fontfile, 'rb'); if (!$f) { die('<B>Error:</B> Can\'t open ' . $fontfile); } $file = fread($f, filesize($fontfile)); fclose($f); if ($type == 'Type1') { //Find first two sections and discard third one $header = ord($file[0]) == 128; if ($header) { //Strip first binary header $file = substr($file, 6); } $pos = strpos($file, 'eexec'); if (!$pos) { die('<B>Error:</B> font file does not seem to be valid Type1'); } $size1 = $pos + 6; if ($header and ord($file[$size1]) == 128) { //Strip second binary header $file = substr($file, 0, $size1) . substr($file, $size1 + 6); } $pos = strpos($file, '00000000'); if (!$pos) { die('<B>Error:</B> font file does not seem to be valid Type1'); } $size2 = $pos - $size1; $file = substr($file, 0, $size1 + $size2); } if (function_exists('gzcompress')) { $cmp = $basename . '.z'; SaveToFile($cmp, gzcompress($file), 'b'); $s .= '$file=\'' . $cmp . "';\n"; echo 'Font file compressed (' . $cmp . ')<BR>'; } else { $s .= '$file=\'' . basename($fontfile) . "';\n"; echo '<B>Notice:</B> font file could not be compressed (zlib extension not available)<BR>'; } if ($type == 'Type1') { $s .= '$size1=' . $size1 . ";\n"; $s .= '$size2=' . $size2 . ";\n"; } else { $s .= '$originalsize=' . filesize($fontfile) . ";\n"; } } else { //Not embedded font $s .= '$file=' . "'';\n"; } $s .= "?>\n"; SaveToFile($basename . '.php', $s); echo 'Font definition file generated (' . $basename . '.php' . ')<BR>'; }
function MakeFont($fontfile, $afmfile, $enc = 'cp1252', $patch = array()) { //Generate a font definition file set_magic_quotes_runtime(0); if ($enc) { $map = ReadMap($enc); foreach ($patch as $cc => $gn) { $map[$cc] = $gn; } } else { $map = array(); } if (!file_exists($afmfile)) { die('<B>Error:</B> AFM file not found: ' . $afmfile); } $fm = ReadAFM($afmfile, $map); if ($enc) { $diff = MakeFontEncoding($map); } else { $diff = ''; } $fd = MakeFontDescriptor($fm, empty($map)); $s = '<?php' . "\n"; $s .= '$type=\'TrueType' . "';\n"; $s .= '$name=\'' . $fm['FontName'] . "';\n"; $s .= '$desc=' . $fd . ";\n"; if (!isset($fm['UnderlinePosition'])) { $fm['UnderlinePosition'] = -100; } if (!isset($fm['UnderlineThickness'])) { $fm['UnderlineThickness'] = 50; } $s .= '$up=' . $fm['UnderlinePosition'] . ";\n"; $s .= '$ut=' . $fm['UnderlineThickness'] . ";\n"; $w = MakeWidthArray($fm); $s .= '$cw=' . $w . ";\n"; $s .= '$enc=\'' . $enc . "';\n"; $s .= '$diff=\'' . $diff . "';\n"; $basename = substr(basename($afmfile), 0, -4); if ($fontfile) { //Embedded font if (!file_exists($fontfile)) { die('<B>Error:</B> font file not found: ' . $fontfile); } if (function_exists('gzcompress')) { $f = fopen($fontfile, 'rb'); $ttf = fread($f, filesize($fontfile)); fclose($f); $cmp = $basename . '.z'; SaveToFile($cmp, gzcompress($ttf), 'b'); $s .= '$file=\'' . $cmp . "';\n"; echo 'Font file compressed (' . $cmp . ')<BR>'; } else { echo '<B>Notice:</B> font file could not be compressed (gzcompress not available)<BR>'; $s .= '$file=\'' . basename($fontfile) . "';\n"; } $s .= '$originalsize=' . filesize($fontfile) . ";\n"; } else { //Not embedded font $s .= '$file=' . "'';\n"; } $s .= "?>\n"; SaveToFile($basename . '.php', $s); echo 'Font definition file generated (' . $basename . '.php' . ')<BR>'; }