ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
makefont.php File Reference

Go to the source code of this file.

Namespaces

namespace  com\tecnick\tcpdf
 PHP class to creates array representations for 2D barcodes to be used with TCPDF.

Functions

 MakeFont ($fontfile, $fmfile, $embedded=true, $enc='cp1252', $patch=array())
 ReadMap ($enc)
 Read the specified encoding map.
 ReadUFM ($file, &$cidtogidmap)
 Read UFM file.
 ReadAFM ($file, &$map)
 Read AFM file.
 MakeFontDescriptor ($fm, $symbolic=false)
 MakeWidthArray ($fm)
 MakeFontEncoding ($map)
 SaveToFile ($file, $s, $mode='t')
 ReadShort ($f)
 ReadLong ($f)
 CheckTTF ($file)

Variables

 $arg = $GLOBALS['argv']

Function Documentation

CheckTTF (   $file)

Definition at line 550 of file makefont.php.

References $f, $file, ReadLong(), and ReadShort().

Referenced by MakeFont().

{
//Check if font license allows embedding
$f = fopen($file, 'rb');
if (!$f) {
die('Error: unable to open '.$file);
}
//Extract number of tables
fseek($f, 4, SEEK_CUR);
$nb = ReadShort($f);
fseek($f, 6, SEEK_CUR);
//Seek OS/2 table
$found = false;
for ($i = 0; $i < $nb; $i++) {
if (fread($f, 4) == 'OS/2') {
$found = true;
break;
}
fseek($f, 12, SEEK_CUR);
}
if (!$found) {
fclose($f);
return;
}
fseek($f, 4, SEEK_CUR);
$offset = ReadLong($f);
fseek($f, $offset, SEEK_SET);
//Extract fsType flags
fseek($f, 8, SEEK_CUR);
$fsType = ReadShort($f);
$rl = ($fsType & 0x02) != 0;
$pp = ($fsType & 0x04) != 0;
$e = ($fsType & 0x08) != 0;
fclose($f);
if($rl AND (!$pp) AND (!$e)) {
print "Warning: font license does not allow embedding\n";
}
}
$arg = $GLOBALS['argv'];
if (count($arg) >= 3) {
ob_start();
array_shift($arg);
if (sizeof($arg) == 3) {
$arg[3] = $arg[2];
$arg[2] = true;
} else {
if (!isset($arg[2])) {
$arg[2] = true;
}
if (!isset($arg[3])) {
$arg[3] = 'cp1252';
}
}
if (!isset($arg[4])) {
$arg[4] = array();
}
MakeFont($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]);
$t = ob_get_clean();
print preg_replace('!<BR( /)?>!i', "\n", $t);
} else {

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

MakeFont (   $fontfile,
  $fmfile,
  $embedded = true,
  $enc = 'cp1252',
  $patch = array() 
)
Parameters
string$fontfilepath to font file (TTF, OTF or PFB).
string$fmfilefont metrics file (UFM or AFM).
boolean$embeddedSet to false to not embed the font, true otherwise (default).
string$encName of the encoding table to use. Omit this parameter for TrueType Unicode, OpenType Unicode and symbolic fonts like Symbol or ZapfDingBats.
array$patchOptional modification of the encoding

Definition at line 60 of file makefont.php.

References $diff, $dw, $enc, $f, $file, $pos, $type, CheckTTF(), MakeFontDescriptor(), MakeFontEncoding(), MakeWidthArray(), ReadAFM(), ReadMap(), ReadUFM(), and SaveToFile().

{
//Generate a font definition file
set_magic_quotes_runtime(0);
ini_set('auto_detect_line_endings', '1');
if (!file_exists($fontfile)) {
die('Error: file not found: '.$fontfile);
}
if (!file_exists($fmfile)) {
die('Error: file not found: '.$fmfile);
}
$cidtogidmap = '';
$map = array();
$diff = '';
$dw = 0; // default width
$ffext = strtolower(substr($fontfile, -3));
$fmext = strtolower(substr($fmfile, -3));
if ($fmext == 'afm') {
if (($ffext == 'ttf') OR ($ffext == 'otf')) {
$type = 'TrueType';
} elseif ($ffext == 'pfb') {
$type = 'Type1';
} else {
die('Error: unrecognized font file extension: '.$ffext);
}
if ($enc) {
$map = ReadMap($enc);
foreach ($patch as $cc => $gn) {
$map[$cc] = $gn;
}
}
$fm = ReadAFM($fmfile, $map);
if (isset($widths['.notdef'])) {
$dw = $widths['.notdef'];
}
if ($enc) {
}
$fd = MakeFontDescriptor($fm, empty($map));
} elseif ($fmext == 'ufm') {
$enc = '';
if (($ffext == 'ttf') OR ($ffext == 'otf')) {
$type = 'TrueTypeUnicode';
} else {
die('Error: not a TrueType font: '.$ffext);
}
$fm = ReadUFM($fmfile, $cidtogidmap);
$dw = $fm['MissingWidth'];
$fd = MakeFontDescriptor($fm, false);
}
//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";
if ($dw <= 0) {
if (isset($fm['Widths'][32]) AND ($fm['Widths'][32] > 0)) {
// assign default space width
$dw = $fm['Widths'][32];
} else {
$dw = 600;
}
}
$s .= '$dw='.$dw.";\n";
$w = MakeWidthArray($fm);
$s .= '$cw='.$w.";\n";
$s .= '$enc=\''.$enc."';\n";
$s .= '$diff=\''.$diff."';\n";
$basename = substr(basename($fmfile), 0, -4);
if ($embedded) {
//Embedded font
if (($type == 'TrueType') OR ($type == 'TrueTypeUnicode')) {
CheckTTF($fontfile);
}
$f = fopen($fontfile,'rb');
if (!$f) {
die('Error: Unable to 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('Error: 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('Error: font file does not seem to be valid Type1');
}
$size2 = $pos - $size1;
$file = substr($file, 0, ($size1 + $size2));
}
$basename = strtolower($basename);
if (function_exists('gzcompress')) {
$cmp = $basename.'.z';
SaveToFile($cmp, gzcompress($file, 9), 'b');
$s .= '$file=\''.$cmp."';\n";
print "Font file compressed (".$cmp.")\n";
if (!empty($cidtogidmap)) {
$cmp = $basename.'.ctg.z';
SaveToFile($cmp, gzcompress($cidtogidmap, 9), 'b');
print "CIDToGIDMap created and compressed (".$cmp.")\n";
$s .= '$ctg=\''.$cmp."';\n";
}
} else {
$s .= '$file=\''.basename($fontfile)."';\n";
print "Notice: font file could not be compressed (zlib extension not available)\n";
if (!empty($cidtogidmap)) {
$cmp = $basename.'.ctg';
$f = fopen($cmp, 'wb');
fwrite($f, $cidtogidmap);
fclose($f);
print "CIDToGIDMap created (".$cmp.")\n";
$s .= '$ctg=\''.$cmp."';\n";
}
}
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 .= '// --- EOF ---';
SaveToFile($basename.'.php',$s);
print "Font definition file generated (".$basename.".php)\n";
}

+ Here is the call graph for this function:

MakeFontDescriptor (   $fm,
  $symbolic = false 
)

Definition at line 441 of file makefont.php.

References $desc.

Referenced by MakeFont().

{
//Ascent
$asc = (isset($fm['Ascender']) ? $fm['Ascender'] : 1000);
$fd = "array('Ascent'=>".$asc;
//Descent
$desc = (isset($fm['Descender']) ? $fm['Descender'] : -200);
$fd .= ",'Descent'=>".$desc;
//CapHeight
if (isset($fm['CapHeight'])) {
$ch = $fm['CapHeight'];
} elseif (isset($fm['CapXHeight'])) {
$ch = $fm['CapXHeight'];
} else {
$ch = $asc;
}
$fd .= ",'CapHeight'=>".$ch;
//Flags
$flags = 0;
if (isset($fm['IsFixedPitch']) AND $fm['IsFixedPitch']) {
$flags += 1<<0;
}
if ($symbolic) {
$flags += 1<<2;
} else {
$flags += 1<<5;
}
if (isset($fm['ItalicAngle']) AND ($fm['ItalicAngle'] != 0)) {
$flags += 1<<6;
}
$fd .= ",'Flags'=>".$flags;
//FontBBox
if (isset($fm['FontBBox'])) {
$fbb = $fm['FontBBox'];
} else {
$fbb = array(0, ($desc - 100), 1000, ($asc + 100));
}
$fd .= ",'FontBBox'=>'[".$fbb[0].' '.$fbb[1].' '.$fbb[2].' '.$fbb[3]."]'";
//ItalicAngle
$ia = (isset($fm['ItalicAngle']) ? $fm['ItalicAngle'] : 0);
$fd .= ",'ItalicAngle'=>".$ia;
//StemV
if (isset($fm['StdVW'])) {
$stemv = $fm['StdVW'];
} elseif (isset($fm['Weight']) AND preg_match('/(bold|black)/i', $fm['Weight'])) {
$stemv = 120;
} else {
$stemv = 70;
}
$fd .= ",'StemV'=>".$stemv;
//MissingWidth
if(isset($fm['MissingWidth'])) {
$fd .= ",'MissingWidth'=>".$fm['MissingWidth'];
}
$fd .= ')';
return $fd;
}

+ Here is the caller graph for this function:

MakeFontEncoding (   $map)

Definition at line 514 of file makefont.php.

References ReadMap().

Referenced by MakeFont().

{
//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);
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

MakeWidthArray (   $fm)

Definition at line 498 of file makefont.php.

References $cw.

Referenced by MakeFont().

{
//Make character width array
$s = 'array(';
$cw = $fm['Widths'];
$els = array();
$c = 0;
foreach ($cw as $i => $w) {
if (is_numeric($i)) {
$els[] = (((($c++)%10) == 0) ? "\n" : '').$i.'=>'.$w;
}
}
$s .= implode(',', $els);
$s .= ')';
return $s;
}

+ Here is the caller graph for this function:

ReadAFM (   $file,
$map 
)

Read AFM file.

Definition at line 315 of file makefont.php.

References $file.

Referenced by MakeFont().

{
//Read a font metric file
$a = file($file);
if(empty($a)) {
die('File not found');
}
$widths = array();
$fm = array();
$fix = array(
'Edot'=>'Edotaccent',
'edot'=>'edotaccent',
'Idot'=>'Idotaccent',
'Zdot'=>'Zdotaccent',
'zdot'=>'zdotaccent',
'Odblacute' => 'Ohungarumlaut',
'odblacute' => 'ohungarumlaut',
'Udblacute'=>'Uhungarumlaut',
'udblacute'=>'uhungarumlaut',
'Gcedilla'=>'Gcommaaccent'
,'gcedilla'=>'gcommaaccent',
'Kcedilla'=>'Kcommaaccent',
'kcedilla'=>'kcommaaccent',
'Lcedilla'=>'Lcommaaccent',
'lcedilla'=>'lcommaaccent',
'Ncedilla'=>'Ncommaaccent',
'ncedilla'=>'ncommaaccent',
'Rcedilla'=>'Rcommaaccent',
'rcedilla'=>'rcommaaccent',
'Scedilla'=>'Scommaaccent',
'scedilla'=>'scommaaccent',
'Tcedilla'=>'Tcommaaccent',
'tcedilla'=>'tcommaaccent',
'Dslash'=>'Dcroat',
'dslash'=>'dcroat',
'Dmacron'=>'Dcroat',
'dmacron'=>'dcroat',
'combininggraveaccent'=>'gravecomb',
'combininghookabove'=>'hookabovecomb',
'combiningtildeaccent'=>'tildecomb',
'combiningacuteaccent'=>'acutecomb',
'combiningdotbelow'=>'dotbelowcomb',
'dongsign'=>'dong'
);
foreach($a as $l) {
$e = explode(' ', rtrim($l));
if (count($e) < 2) {
continue;
}
$code = $e[0];
$param = $e[1];
if ($code == 'C') {
//Character metrics
$cc = (int)$e[1];
$w = $e[4];
$gn = $e[7];
if (substr($gn, -4) == '20AC') {
$gn = 'Euro';
}
if (isset($fix[$gn])) {
//Fix incorrect glyph name
foreach ($map as $c => $n) {
if ($n == $fix[$gn]) {
$map[$c] = $gn;
}
}
}
if (empty($map)) {
//Symbolic font: use built-in encoding
$widths[$cc] = $w;
} else {
$widths[$gn] = $w;
if($gn == 'X') {
$fm['CapXHeight'] = $e[13];
}
}
if($gn == '.notdef') {
$fm['MissingWidth'] = $w;
}
} elseif($code == 'FontName') {
$fm['FontName'] = $param;
} elseif($code == 'Weight') {
$fm['Weight'] = $param;
} elseif($code == 'ItalicAngle') {
$fm['ItalicAngle'] = (double)$param;
} elseif($code == 'Ascender') {
$fm['Ascender'] = (int)$param;
} elseif($code == 'Descender') {
$fm['Descender'] = (int)$param;
} elseif($code == 'UnderlineThickness') {
$fm['UnderlineThickness'] = (int)$param;
} elseif($code == 'UnderlinePosition') {
$fm['UnderlinePosition'] = (int)$param;
} elseif($code == 'IsFixedPitch') {
$fm['IsFixedPitch'] = ($param == 'true');
} elseif($code == 'FontBBox') {
$fm['FontBBox'] = array($e[1], $e[2], $e[3], $e[4]);
} elseif($code == 'CapHeight') {
$fm['CapHeight'] = (int)$param;
} elseif($code == 'StdVW') {
$fm['StdVW'] = (int)$param;
}
}
if (!isset($fm['FontName'])) {
die('FontName not found');
}
if (!empty($map)) {
if (!isset($widths['.notdef'])) {
$widths['.notdef'] = 600;
}
if (!isset($widths['Delta']) AND isset($widths['increment'])) {
$widths['Delta'] = $widths['increment'];
}
//Order widths according to map
for ($i = 0; $i <= 255; $i++) {
if (!isset($widths[$map[$i]])) {
print "Warning: character ".$map[$i]." is missing\n";
$widths[$i] = $widths['.notdef'];
} else {
$widths[$i] = $widths[$map[$i]];
}
}
}
$fm['Widths'] = $widths;
return $fm;
}

+ Here is the caller graph for this function:

ReadLong (   $f)

Definition at line 545 of file makefont.php.

References $f.

Referenced by CheckTTF().

{
$a = unpack('N1N', fread($f, 4));
return $a['N'];
}

+ Here is the caller graph for this function:

ReadMap (   $enc)

Read the specified encoding map.

Parameters
string$encmap name (see /enc/ folder for valid names).

Definition at line 213 of file makefont.php.

References $enc, $file, and $l.

Referenced by MakeFont(), and MakeFontEncoding().

{
//Read a map file
$file = dirname(__FILE__).'/enc/'.strtolower($enc).'.map';
$a = file($file);
if (empty($a)) {
die('Error: encoding not found: '.$enc);
}
$cc2gn = array();
foreach ($a as $l) {
if ($l{0} == '!') {
$e = preg_split('/[ \\t]+/',rtrim($l));
$cc = hexdec(substr($e[0],1));
$gn = $e[2];
$cc2gn[$cc] = $gn;
}
}
for($i = 0; $i <= 255; $i++) {
if(!isset($cc2gn[$i])) {
$cc2gn[$i] = '.notdef';
}
}
return $cc2gn;
}

+ Here is the caller graph for this function:

ReadShort (   $f)

Definition at line 540 of file makefont.php.

References $f.

Referenced by CheckTTF().

{
$a = unpack('n1n', fread($f, 2));
return $a['n'];
}

+ Here is the caller graph for this function:

ReadUFM (   $file,
$cidtogidmap 
)

Read UFM file.

Definition at line 240 of file makefont.php.

References $file, and $l.

Referenced by MakeFont().

{
//Prepare empty CIDToGIDMap
$cidtogidmap = str_pad('', (256 * 256 * 2), "\x00");
//Read a font metric file
$a = file($file);
if (empty($a)) {
die('File not found');
}
$widths = array();
$fm = array();
foreach($a as $l) {
$e = explode(' ',chop($l));
if(count($e) < 2) {
continue;
}
$code = $e[0];
$param = $e[1];
if($code == 'U') {
// U 827 ; WX 0 ; N squaresubnosp ; G 675 ;
//Character metrics
$cc = (int)$e[1];
if ($cc != -1) {
$gn = $e[7];
$w = $e[4];
$glyph = $e[10];
$widths[$cc] = $w;
if($cc == ord('X')) {
$fm['CapXHeight'] = $e[13];
}
// Set GID
if (($cc >= 0) AND ($cc < 0xFFFF) AND $glyph) {
$cidtogidmap{($cc * 2)} = chr($glyph >> 8);
$cidtogidmap{(($cc * 2) + 1)} = chr($glyph & 0xFF);
}
}
if((isset($gn) AND ($gn == '.notdef')) AND (!isset($fm['MissingWidth']))) {
$fm['MissingWidth'] = $w;
}
} elseif($code == 'FontName') {
$fm['FontName'] = $param;
} elseif($code == 'Weight') {
$fm['Weight'] = $param;
} elseif($code == 'ItalicAngle') {
$fm['ItalicAngle'] = (double)$param;
} elseif($code == 'Ascender') {
$fm['Ascender'] = (int)$param;
} elseif($code == 'Descender') {
$fm['Descender'] = (int)$param;
} elseif($code == 'UnderlineThickness') {
$fm['UnderlineThickness'] = (int)$param;
} elseif($code == 'UnderlinePosition') {
$fm['UnderlinePosition'] = (int)$param;
} elseif($code == 'IsFixedPitch') {
$fm['IsFixedPitch'] = ($param == 'true');
} elseif($code == 'FontBBox') {
$fm['FontBBox'] = array($e[1], $e[2], $e[3], $e[4]);
} elseif($code == 'CapHeight') {
$fm['CapHeight'] = (int)$param;
} elseif($code == 'StdVW') {
$fm['StdVW'] = (int)$param;
}
}
if(!isset($fm['MissingWidth'])) {
$fm['MissingWidth'] = 600;
}
if(!isset($fm['FontName'])) {
die('FontName not found');
}
$fm['Widths'] = $widths;
return $fm;
}

+ Here is the caller graph for this function:

SaveToFile (   $file,
  $s,
  $mode = 't' 
)

Definition at line 531 of file makefont.php.

References $f, and $file.

Referenced by MakeFont().

{
$f = fopen($file, 'w'.$mode);
if(!$f) {
die('Can\'t write to file '.$file);
}
fwrite($f, $s, strlen($s));
fclose($f);
}

+ Here is the caller graph for this function:

Variable Documentation

$arg = $GLOBALS['argv']

Definition at line 588 of file makefont.php.

Referenced by PHPExcel_Calculation\_processTokenStack(), PHPExcel_Calculation_Statistical\AVEDEV(), PHPExcel_Calculation_Statistical\AVERAGE(), PHPExcel_Calculation_Statistical\AVERAGEA(), PHPExcel_Calculation_Statistical\AVERAGEIF(), PHPExcel_Calculation_TextData\CONCATENATE(), PHPExcel_Calculation_Statistical\COUNT(), PHPExcel_Calculation_Statistical\COUNTA(), PHPExcel_Calculation_Statistical\COUNTBLANK(), PHPExcel_Calculation_Statistical\COUNTIF(), PHPExcel_Calculation_Statistical\DEVSQ(), ilHTMLToPDFTransformerUsingWebkit\getArgs(), ilBMFWSDL\getSchemaType(), ilValidator\handleErr(), PHPExcel_Calculation_Statistical\HARMEAN(), PHPExcel_Calculation_Engineering\IMPRODUCT(), PHPExcel_Calculation_Engineering\IMSUM(), PHPExcel_Calculation_Statistical\KURT(), PHPExcel_Calculation_Statistical\LARGE(), PHPExcel_Calculation_Logical\LOGICAL_AND(), PHPExcel_Calculation_Logical\LOGICAL_OR(), PHPExcel_Calculation_Statistical\MAX(), PHPExcel_Calculation_Statistical\MAXA(), PHPExcel_Calculation_Statistical\MAXIF(), PHPExcel_Calculation_Statistical\MEDIAN(), PHPExcel_Calculation_Statistical\MIN(), PHPExcel_Calculation_Statistical\MINA(), PHPExcel_Calculation_Statistical\MINIF(), PHPExcel_Calculation_Statistical\MODE(), PHPExcel_Calculation_MathTrig\MULTINOMIAL(), PHPExcel_Calculation_Statistical\PERCENTILE(), PHPExcel_Calculation_MathTrig\PRODUCT(), PHPExcel_Calculation_MathTrig\QUOTIENT(), PHPExcel_Calculation_MathTrig\SERIESSUM(), PHPlot\SetCallback(), Spreadsheet_Excel_Writer_Format\setPattern(), PHPExcel_Calculation_Statistical\SKEW(), PHPExcel_Calculation_Statistical\SMALL(), ilCronCheck\start(), PHPExcel_Calculation_Statistical\STDEV(), PHPExcel_Calculation_Statistical\STDEVA(), PHPExcel_Calculation_Statistical\STDEVP(), PHPExcel_Calculation_Statistical\STDEVPA(), PHPExcel_Calculation_MathTrig\SUM(), PHPExcel_Calculation_MathTrig\SUMIF(), PHPExcel_Calculation_MathTrig\SUMSQ(), phpCAS\traceBegin(), PHPExcel_Calculation_Statistical\TRIMMEAN(), PHPExcel_Calculation_Statistical\VARA(), PHPExcel_Calculation_Statistical\VARFunc(), PHPExcel_Calculation_Statistical\VARP(), PHPExcel_Calculation_Statistical\VARPA(), and vd().