[ Index ] |
|
Code source de SPIP Agora 1.4 |
1 <?php 2 /***************************************************** 3 * This file is part of Agora, web based content management system. 4 * 5 * Agora is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; version 2 of the License. 8 * 9 * Agora is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details (file "COPYING"). 13 * 14 * Copyright © Arnaud Martin, Antoine Pitrou et Philippe Rivière. 15 * List of authors detailed in "copyright_fr.html" file. 16 * E-mail : agora@sig.premier-ministre.gouv.fr 17 * Web site : http://www.agora.gouv.fr 18 *****************************************************/ 19 /**************************************************************************** 20 * Utilitaire de génération de fichier de définition de police * 21 * Version: 1.1 * 22 * Date: 31/07/2002 * 23 ****************************************************************************/ 24 25 function ReadMap ($enc) { 26 //Read a map file 27 $file = dirname(__FILE__). '/' . strtolower($enc). '.map'; 28 $a = file($file); 29 30 if (empty($a)) 31 die('<B>Error:</B> encoding not found: ' . $enc); 32 33 $cc2gn = array(); 34 35 foreach ($a as $l) { 36 $e = explode(' ', chop($l)); 37 $cc = hexdec(substr($e[0], 1)); 38 $gn = $e[2]; 39 $cc2gn[$cc] = $gn; 40 } 41 42 for ($i = 0; $i <= 255; $i++) 43 if (!isset($cc2gn[$i])) 44 $cc2gn[$i] = '.notdef'; 45 46 return $cc2gn; 47 } 48 49 function ReadAFM ($file, &$map) { 50 //Read a font metric file 51 $a = file($file); 52 53 if (empty($a)) 54 die('File not found'); 55 56 $widths = array(); 57 $fm = array(); 58 $fix = array('Edot' => 'Edotaccent', 'edot' => 'edotaccent', 'Zdot' => 'Zdotaccent', 59 'zdot' => 'zdotaccent', 'Odblacute' => 'Ohungarumlaut', 'odblacute' => 'ohungarumlaut', 60 'Udblacute' => 'Uhungarumlaut', 'udblacute' => 'uhungarumlaut', 'Gcedilla' => 'Gcommaaccent', 61 'gcedilla' => 'gcommaaccent', 'Kcedilla' => 'Kcommaaccent', 'kcedilla' => 'kcommaaccent', 62 'Lcedilla' => 'Lcommaaccent', 'lcedilla' => 'lcommaaccent', 'Ncedilla' => 'Ncommaaccent', 63 'ncedilla' => 'ncommaaccent', 'Rcedilla' => 'Rcommaaccent', 'rcedilla' => 'rcommaaccent', 64 'Scedilla' => 'Scommaaccent', 'scedilla' => 'scommaaccent', 'Tcedilla' => 'Tcommaaccent', 65 'tcedilla' => 'tcommaaccent', 'Dslash' => 'Dcroat', 'dslash' => 'dcroat', 66 'Dmacron' => 'Dcroat', 'dmacron' => 'dcroat'); 67 68 foreach ($a as $l) { 69 $e = explode(' ', chop($l)); 70 71 if (count($e) < 2) 72 continue; 73 74 $code = $e[0]; 75 $param = $e[1]; 76 if ($code == 'C') { 77 //Character metrics 78 $cc = (int)$e[1]; 79 $w = $e[4]; 80 $gn = $e[7]; 81 82 if (substr($gn, -4) == '20AC') 83 $gn = 'Euro'; 84 85 if (isset($fix[$gn])) { 86 //Fix incorrect glyph name 87 foreach ($map as $c => $n) 88 if ($n == $fix[$gn]) 89 $map[$c] = $gn; 90 } 91 92 if (empty($map)) { 93 //Symbolic font: use built-in encoding 94 $widths[$cc] = $w; 95 } 96 else { 97 $widths[$gn] = $w; 98 if ($gn == 'X') 99 $fm['CapXHeight'] = $e[13]; 100 } 101 if ($gn == '.notdef') 102 $fm['MissingWidth'] = $w; 103 } 104 elseif ($code == 'FontName') 105 $fm['FontName'] = $param; 106 elseif ($code == 'Weight') 107 $fm['Weight'] = $param; 108 elseif ($code == 'ItalicAngle') 109 $fm['ItalicAngle'] = (double)$param; 110 elseif ($code == 'Ascender') 111 $fm['Ascender'] = (int)$param; 112 elseif ($code == 'Descender') 113 $fm['Descender'] = (int)$param; 114 elseif ($code == 'UnderlineThickness') 115 $fm['UnderlineThickness'] = (int)$param; 116 elseif ($code == 'UnderlinePosition') 117 $fm['UnderlinePosition'] = (int)$param; 118 elseif ($code == 'IsFixedPitch') 119 $fm['IsFixedPitch'] = ($param == 'true'); 120 elseif ($code == 'FontBBox') 121 $fm['FontBBox'] = array($e[1], $e[2], $e[3], $e[4]); 122 elseif ($code == 'CapHeight') 123 $fm['CapHeight'] = (int)$param; 124 elseif ($code == 'StdVW') 125 $fm['StdVW'] = (int)$param; 126 } 127 128 if (!isset($fm['FontName'])) 129 die('FontName not found'); 130 131 if (!empty($map)) { 132 if (!isset($widths['.notdef'])) 133 $widths['.notdef'] = 600; 134 135 if (!isset($widths['Delta'])and isset($widths['increment'])) 136 $widths['Delta'] = $widths['increment']; 137 //Order widths according to map 138 for ($i = 0; $i <= 255; $i++) { 139 if (!isset($widths[$map[$i]])) { 140 echo '<B>Warning:</B> character ' . $map[$i]. ' is missing<BR>'; 141 $widths[$i] = $widths['.notdef']; 142 } 143 else 144 $widths[$i] = $widths[$map[$i]]; 145 } 146 } 147 148 $fm['Widths'] = $widths; 149 return $fm; 150 } 151 152 function MakeFontDescriptor ($fm, $symbolic) { 153 //Ascent 154 $asc = (isset($fm['Ascender']) ? $fm['Ascender'] : 1000); 155 $fd = "array('Ascent'=>" . $asc; 156 //Descent 157 $desc = (isset($fm['Descender']) ? $fm['Descender'] : -200); 158 $fd .= ",'Descent'=>" . $desc; 159 160 //CapHeight 161 if (isset($fm['CapHeight'])) 162 $ch = $fm['CapHeight']; 163 elseif (isset($fm['CapXHeight'])) 164 $ch = $fm['CapXHeight']; 165 else 166 $ch = $asc; 167 168 $fd .= ",'CapHeight'=>" . $ch; 169 //Flags 170 $flags = 0; 171 172 if (isset($fm['IsFixedPitch'])and $fm['IsFixedPitch']) 173 $flags += 1 << 0; 174 175 if ($symbolic) 176 $flags += 1 << 2; 177 178 if (!$symbolic) 179 $flags += 1 << 5; 180 181 if (isset($fm['ItalicAngle'])and $fm['ItalicAngle'] != 0) 182 $flags += 1 << 6; 183 184 $fd .= ",'Flags'=>" . $flags; 185 186 //FontBBox 187 if (isset($fm['FontBBox'])) 188 $fbb = $fm['FontBBox']; 189 else 190 $fbb = array(0, $des - 100, 1000, $asc + 100); 191 192 $fd .= ",'FontBBox'=>'[" . $fbb[0]. ' ' . $fbb[1]. ' ' . $fbb[2]. ' ' . $fbb[3]. "]'"; 193 //ItalicAngle 194 $ia = (isset($fm['ItalicAngle']) ? $fm['ItalicAngle'] : 0); 195 $fd .= ",'ItalicAngle'=>" . $ia; 196 197 //StemV 198 if (isset($fm['StdVW'])) 199 $stemv = $fm['StdVW']; 200 elseif (isset($fm['Weight'])and eregi('(bold|black)', $fm['Weight'])) 201 $stemv = 120; 202 else 203 $stemv = 70; 204 205 $fd .= ",'StemV'=>" . $stemv; 206 207 //MissingWidth 208 if (isset($fm['MissingWidth'])) 209 $fd .= ",'MissingWidth'=>" . $fm['MissingWidth']; 210 211 $fd .= ')'; 212 return $fd; 213 } 214 215 function MakeWidthArray ($fm) { 216 //Make character width array 217 $s = "array(\n\t"; 218 $cw = $fm['Widths']; 219 220 for ($i = 0; $i <= 255; $i++) { 221 if (chr($i) == "'") 222 $s .= "'\\''"; 223 elseif (chr($i) == "\\") 224 $s .= "'\\\\'"; 225 elseif ($i >= 32 and $i <= 126) 226 $s .= "'" . chr($i). "'"; 227 else 228 $s .= "chr($i)"; 229 230 $s .= "=>" . $fm['Widths'][$i]; 231 232 if ($i < 255) 233 $s .= ","; 234 if (($i + 1) % 22 == 0) 235 $s .= "\n\t"; 236 } 237 238 $s .= ")"; 239 return $s; 240 } 241 242 function MakeFontEncoding ($map) { 243 //Build differences from reference encoding 244 $ref = ReadMap('cp1252'); 245 $s = ''; 246 $last = 0; 247 248 for ($i = 32; $i <= 255; $i++) { 249 if ($map[$i] != $ref[$i]) { 250 if ($i != $last + 1) 251 $s .= $i . ' '; 252 253 $last = $i; 254 $s .= '/' . $map[$i]. ' '; 255 } 256 } 257 258 return chop($s); 259 } 260 261 function SaveToFile ($file, $s, $mode = 't') { 262 $f = fopen($file, 'w' . $mode); 263 264 if (!$f) 265 die('Can\'t write to file ' . $file); 266 267 fwrite($f, $s, strlen($s)); 268 fclose($f); 269 } 270 271 function ReadShort ($f) { 272 $a = unpack('n1n', fread($f, 2)); 273 return $a['n']; 274 } 275 276 function ReadLong ($f) { 277 $a = unpack('N1N', fread($f, 4)); 278 return $a['N']; 279 } 280 281 function CheckTTF ($file) { 282 //Check if font license allows embedding 283 $f = fopen($file, 'rb'); 284 285 if (!$f) 286 die('<B>Error:</B> Can\'t open ' . $file); 287 288 //Extract number of tables 289 fseek($f, 4, SEEK_CUR); 290 $nb = ReadShort($f); 291 fseek($f, 6, SEEK_CUR); 292 //Seek OS/2 table 293 $found = false; 294 295 for ($i = 0; $i < $nb; $i++) { 296 if (fread($f, 4) == 'OS/2') { 297 $found = true; 298 break; 299 } 300 fseek($f, 12, SEEK_CUR); 301 } 302 303 if (!$found) { 304 fclose($f); 305 return; 306 } 307 308 fseek($f, 4, SEEK_CUR); 309 $offset = ReadLong($f); 310 fseek($f, $offset, SEEK_SET); 311 //Extract fsType flags 312 fseek($f, 8, SEEK_CUR); 313 $fsType = ReadShort($f); 314 $rl = ($fsType & 0x02) != 0; 315 $pp = ($fsType & 0x04) != 0; 316 $e = ($fsType & 0x08) != 0; 317 fclose($f); 318 319 if ($rl and !$pp and !$e) 320 echo '<B>Warning:</B> font license does not allow embedding'; 321 } 322 323 /**************************************************************************** 324 * $fontfile : chemin du fichier TTF (ou chaîne vide si pas d'incorporation) * 325 * $afmfile : chemin du fichier AFM * 326 * $enc : encodage (ou chaîne vide si la police est symbolique) * 327 * $patch : patch optionnel pour l'encodage * 328 * $type : type de la police si $fontfile est vide * 329 ****************************************************************************/ 330 function MakeFont ($fontfile, $afmfile, $enc = 'cp1252', $patch = array(), $type = 'TrueType') { 331 //Generate a font definition file 332 set_magic_quotes_runtime(0); 333 334 if ($enc) { 335 $map = ReadMap($enc); 336 foreach ($patch as $cc => $gn) 337 $map[$cc] = $gn; 338 } 339 else 340 $map = array(); 341 342 if (!file_exists($afmfile)) 343 die('<B>Error:</B> AFM file not found: ' . $afmfile); 344 345 $fm = ReadAFM($afmfile, $map); 346 347 if ($enc) 348 $diff = MakeFontEncoding($map); 349 else 350 $diff = ''; 351 352 $fd = MakeFontDescriptor($fm, empty($map)); 353 354 //Find font type 355 if ($fontfile) { 356 $ext = strtolower(substr($fontfile, -3)); 357 if ($ext == 'ttf') 358 $type = 'TrueType'; 359 elseif ($ext == 'pfb') 360 $type = 'Type1'; 361 else 362 die('<B>Error:</B> unrecognized font file extension: ' . $ext); 363 } 364 else { 365 if ($type != 'TrueType' and $type != 'Type1') 366 die('<B>Error:</B> incorrect font type: ' . $type); 367 } 368 369 //Start generation 370 $s = '<?php' . "\n"; 371 $s .= '$type=\'' . $type . "';\n"; 372 $s .= '$name=\'' . $fm['FontName']. "';\n"; 373 $s .= '$desc=' . $fd . ";\n"; 374 375 if (!isset($fm['UnderlinePosition'])) 376 $fm['UnderlinePosition'] = -100; 377 378 if (!isset($fm['UnderlineThickness'])) 379 $fm['UnderlineThickness'] = 50; 380 381 $s .= '$up=' . $fm['UnderlinePosition']. ";\n"; 382 $s .= '$ut=' . $fm['UnderlineThickness']. ";\n"; 383 $w = MakeWidthArray($fm); 384 $s .= '$cw=' . $w . ";\n"; 385 $s .= '$enc=\'' . $enc . "';\n"; 386 $s .= '$diff=\'' . $diff . "';\n"; 387 $basename = substr(basename($afmfile), 0, -4); 388 389 if ($fontfile) { 390 //Embedded font 391 if (!file_exists($fontfile)) 392 die('<B>Error:</B> font file not found: ' . $fontfile); 393 394 if ($type == 'TrueType') 395 CheckTTF($fontfile); 396 397 $f = fopen($fontfile, 'rb'); 398 399 if (!$f) 400 die('<B>Error:</B> Can\'t open ' . $fontfile); 401 402 $file = fread($f, filesize($fontfile)); 403 fclose($f); 404 405 if ($type == 'Type1') { 406 //Find first two sections and discard third one 407 $pos = strpos($file, 'eexec'); 408 409 if (!$pos) 410 die('<B>Error:</B> font file does not seem to be valid Type1'); 411 412 $size1 = $pos + 6; 413 $pos = strpos($file, '00000000'); 414 415 if (!$pos) 416 die('<B>Error:</B> font file does not seem to be valid Type1'); 417 418 $size2 = $pos - $size1; 419 $file = substr($file, 0, $size1 + $size2); 420 } 421 422 if (function_exists('gzcompress')) { 423 $cmp = $basename . '.z'; 424 SaveToFile($cmp, gzcompress($file), 'b'); 425 $s .= '$file=\'' . $cmp . "';\n"; 426 echo 'Font file compressed (' . $cmp . ')<BR>'; 427 } 428 else { 429 $s .= '$file=\'' . basename($fontfile). "';\n"; 430 echo '<B>Notice:</B> font file could not be compressed (gzcompress not available)<BR>'; 431 } 432 if ($type == 'Type1') { 433 $s .= '$size1=' . $size1 . ";\n"; 434 $s .= '$size2=' . $size2 . ";\n"; 435 } 436 else 437 $s .= '$originalsize=' . filesize($fontfile). ";\n"; 438 } 439 else { 440 //Not embedded font 441 $s .= '$file=' . "'';\n"; 442 } 443 444 $s .= "?>\n"; 445 SaveToFile($basename . '.php', $s); 446 echo 'Font definition file generated (' . $basename . '.php' . ')<BR>'; 447 } 448 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 14:40:03 2007 | par Balluche grâce à PHPXref 0.7 |