]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/tcpdf/fonts/utils/makefont.php
Release 6.5.0
[Github/sugarcrm.git] / include / tcpdf / fonts / utils / makefont.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
18
19 r47930 - 2009-06-02 16:21:39 -0700 (Tue, 02 Jun 2009) - jenny - Updating with changes from bsoufflet.
20
21
22 */
23
24
25 // BEGIN SUGARCRM SPECIFIC
26 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
27 // END SUGARCRM SPECIFIC
28
29 //============================================================+
30 // File name   : makefont.php
31 // Begin       : 2004-12-31
32 // Last Update : 2008-12-06
33 // Version     : 1.2.004
34 // License     : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
35 //      ----------------------------------------------------------------------------
36 //      Copyright (C) 2008  Nicola Asuni - Tecnick.com S.r.l.
37 //
38 //      This program is free software: you can redistribute it and/or modify
39 //      it under the terms of the GNU Lesser General Public License as published by
40 //      the Free Software Foundation, either version 2.1 of the License, or
41 //      (at your option) any later version.
42 //
43 //      This program is distributed in the hope that it will be useful,
44 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
45 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46 //      GNU Lesser General Public License for more details.
47 //
48 //      You should have received a copy of the GNU Lesser General Public License
49 //      along with this program.  If not, see <http://www.gnu.org/licenses/>.
50 //
51 //      See LICENSE.TXT file for more information.
52 //  ----------------------------------------------------------------------------
53 //
54 // Description : Utility to generate font definition files fot TCPDF
55 //
56 // Authors: Nicola Asuni, Olivier Plathey, Steven Wittens
57 //
58 // (c) Copyright:
59 //               Nicola Asuni
60 //               Tecnick.com S.r.l.
61 //               Via della Pace, 11
62 //               09044 Quartucciu (CA)
63 //               ITALY
64 //               www.tecnick.com
65 //               info@tecnick.com
66 //============================================================+
67
68 /**
69  * Utility to generate font definition files fot TCPDF.
70  * @author Nicola Asuni, Olivier Plathey, Steven Wittens
71  * @copyright 2004-2009 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
72  * @package com.tecnick.tcpdf
73  * @link http://www.tcpdf.org
74  * @license http://www.gnu.org/copyleft/lesser.html LGPL
75 */
76
77 /**
78  *
79  * @param string $fontfile path to font file (TTF, OTF or PFB).
80  * @param string $fmfile font metrics file (UFM or AFM).
81  * @param boolean $embedded Set to false to not embed the font, true otherwise (default).
82  * @param string $enc Name of the encoding table to use. Omit this parameter for TrueType Unicode, OpenType Unicode and symbolic fonts like Symbol or ZapfDingBats.
83  * @param array $patch Optional modification of the encoding
84  */
85 function MakeFont($fontfile, $fmfile, $embedded=true, $enc='cp1252', $patch=array()/* BEGIN SUGARCRM SPECIFIC */, $cidInfo=""/* END SUGARCRM SPECIFIC */) {
86         //Generate a font definition file
87         set_magic_quotes_runtime(0);
88         ini_set('auto_detect_line_endings', '1');
89         if (!file_exists($fontfile)) {
90                 die('Error: file not found: '.$fontfile);
91         }
92         if (!file_exists($fmfile)) {
93                 die('Error: file not found: '.$fmfile);
94         }
95         $cidtogidmap = '';
96         $map = array();
97         $diff = '';
98         $dw = 0; // default width
99         $ffext = strtolower(substr($fontfile, -3));
100         $fmext = strtolower(substr($fmfile, -3));
101         if ($fmext == 'afm') {
102                 if (($ffext == 'ttf') OR ($ffext == 'otf')) {
103                         $type = 'TrueType';
104                 } elseif ($ffext == 'pfb') {
105                         $type = 'Type1';
106                 } else {
107                         die('Error: unrecognized font file extension: '.$ffext);
108                 }
109                 if ($enc) {
110                         $map = ReadMap($enc);
111                         foreach ($patch as $cc => $gn) {
112                                 $map[$cc] = $gn;
113                         }
114                 }
115                 $fm = ReadAFM($fmfile, $map);
116                 if (isset($widths['.notdef'])) {
117                         $dw = $widths['.notdef'];
118                 }
119                 if ($enc) {
120                         $diff = MakeFontEncoding($map);
121                 }
122                 $fd = MakeFontDescriptor($fm, empty($map));
123         } elseif ($fmext == 'ufm') {
124                 $enc = '';
125                 if (($ffext == 'ttf') OR ($ffext == 'otf')) {
126                         $type = 'TrueTypeUnicode';
127                 } else {
128                         die('Error: not a TrueType font: '.$ffext);
129                 }
130                 $fm = ReadUFM($fmfile, $cidtogidmap);
131                 $dw = $fm['MissingWidth'];
132                 $fd = MakeFontDescriptor($fm, false);
133         }
134         //Start generation
135         $s = '<?php
136
137 /*
138
139 Modification information for LGPL compliance
140
141 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
142
143 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
144
145 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
146
147 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
148
149 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
150
151 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
152
153 r47930 - 2009-06-02 16:21:39 -0700 (Tue, 02 Jun 2009) - jenny - Updating with changes from bsoufflet.
154
155
156 */
157
158 '."\n";
159            // BEGIN SUGARCRM SPECIFIC
160     if($embedded){
161     // END SUGARCRM SPECIFIC
162            $s .= '$type=\''.$type."';\n";
163         // BEGIN SUGARCRM SPECIFIC
164     }else{
165         $s .= '$type=\''."cidfont0';\n";
166     }
167     // END SUGARCRM SPECIFIC
168         $s .= '$name=\''.$fm['FontName']."';\n";
169         $s .= '$desc='.$fd.";\n";
170         if (!isset($fm['UnderlinePosition'])) {
171                 $fm['UnderlinePosition'] = -100;
172         }
173         if (!isset($fm['UnderlineThickness'])) {
174                 $fm['UnderlineThickness'] = 50;
175         }
176         $s .= '$up='.$fm['UnderlinePosition'].";\n";
177         $s .= '$ut='.$fm['UnderlineThickness'].";\n";
178         if ($dw <= 0) {
179                 if (isset($fm['Widths'][32]) AND ($fm['Widths'][32] > 0)) {
180                         // assign default space width
181                         $dw = $fm['Widths'][32];
182                 } else {
183                         $dw = 600;
184                 }
185         }
186         // BEGIN SUGARCRM SPECIFIC
187         if($embedded){
188         // END SUGARCRM SPECIFIC
189             $s .= '$dw='.$dw.";\n";
190         // BEGIN SUGARCRM SPECIFIC
191         }else{
192             $s .= '$dw='."1000;\n";
193         }
194         // END SUGARCRM SPECIFIC
195         $w = MakeWidthArray($fm);
196         $s .= '$cw='.$w.";\n";
197         // BEGIN SUGARCRM SPECIFIC
198     if($embedded){
199     // END SUGARCRM SPECIFIC
200            $s .= '$enc=\''.$enc."';\n";
201         // BEGIN SUGARCRM SPECIFIC
202     }
203     // END SUGARCRM SPECIFIC
204         $s .= '$diff=\''.$diff."';\n";
205         $basename = substr(basename($fmfile), 0, -4);
206     // BEGIN SUGARCRM SPECIFIC
207         $dirname = dirname($fmfile);
208     // END SUGARCRM SPECIFIC
209         if ($embedded) {
210                 //Embedded font
211                 if (($type == 'TrueType') OR ($type == 'TrueTypeUnicode')) {
212                         CheckTTF($fontfile);
213                 }
214                 $f = fopen($fontfile,'rb');
215                 if (!$f) {
216                         die('Error: Unable to open '.$fontfile);
217                 }
218                 $file = stream_get_contents($f);
219                 fclose($f);
220                 if ($type == 'Type1') {
221                         //Find first two sections and discard third one
222                         $header = (ord($file{0}) == 128);
223                         if ($header) {
224                                 //Strip first binary header
225                                 $file = substr($file, 6);
226                         }
227                         $pos = strpos($file, 'eexec');
228                         if (!$pos) {
229                                 die('Error: font file does not seem to be valid Type1');
230                         }
231                         $size1 = $pos + 6;
232                         if ($header AND (ord($file{$size1}) == 128)) {
233                                 //Strip second binary header
234                                 $file = substr($file, 0, $size1).substr($file, $size1+6);
235                         }
236                         $pos = strpos($file, '00000000');
237                         if (!$pos) {
238                                 die('Error: font file does not seem to be valid Type1');
239                         }
240                         $size2 = $pos - $size1;
241                         $file = substr($file, 0, ($size1 + $size2));
242                 }
243                 $basename = strtolower($basename);
244                 if (function_exists('gzcompress')) {
245                         $cmp = $basename.'.z';
246                         // BEGIN SUGARCRM SPECIFIC
247                         /*
248                         // END SUGARCRM SPECIFIC
249                         SaveToFile($cmp, gzcompress($file, 9), 'b');
250                         // BEGIN SUGARCRM SPECIFIC
251             */
252                         SaveToFile($dirname."/".$cmp, gzcompress($file, 9), 'b');
253             // END SUGARCRM SPECIFIC
254                         $s .= '$file=\''.$cmp."';\n";
255                         print "Font file compressed (".$cmp.")\n";
256                         if (!empty($cidtogidmap)) {
257                                 $cmp = $basename.'.ctg.z';
258                         // BEGIN SUGARCRM SPECIFIC
259                 /*
260                 // END SUGARCRM SPECIFIC
261                                 SaveToFile($cmp, gzcompress($cidtogidmap, 9), 'b');
262                 // BEGIN SUGARCRM SPECIFIC
263                 */
264                                 SaveToFile($dirname."/".$cmp, gzcompress($cidtogidmap, 9), 'b');
265                 // END SUGARCRM SPECIFIC
266                                 print "CIDToGIDMap created and compressed (".$cmp.")\n";
267                                 $s .= '$ctg=\''.$cmp."';\n";
268                         }
269                 } else {
270                         $s .= '$file=\''.basename($fontfile)."';\n";
271                         print "Notice: font file could not be compressed (zlib extension not available)\n";
272                         if (!empty($cidtogidmap)) {
273                                 $cmp = $basename.'.ctg';
274                                 // BEGIN SUGARCRM SPECIFIC
275                 /*
276                 // END SUGARCRM SPECIFIC
277                                 $f = fopen($cmp, 'wb');
278                 // BEGIN SUGARCRM SPECIFIC
279                 */
280                                 $f = fopen($dirname."/".$cmp, 'wb');
281                 // END SUGARCRM SPECIFIC
282                                 fwrite($f, $cidtogidmap);
283                                 fclose($f);
284                                 print "CIDToGIDMap created (".$cmp.")\n";
285                                 $s .= '$ctg=\''.$cmp."';\n";
286                         }
287                 }
288                 if($type == 'Type1') {
289                         $s .= '$size1='.$size1.";\n";
290                         $s .= '$size2='.$size2.";\n";
291                 } else {
292                         $s.='$originalsize='.filesize($fontfile).";\n";
293                 }
294         } else {
295                 //Not embedded font
296         // BEGIN SUGARCRM SPECIFIC
297         /*
298         // END SUGARCRM SPECIFIC
299             $s .= '$file='."'';\n";
300         // BEGIN SUGARCRM SPECIFIC
301         */
302             $s .= $cidInfo;
303         // END SUGARCRM SPECIFIC
304         }
305         $s .= "?>";
306     // BEGIN SUGARCRM SPECIFIC
307     /*
308     // END SUGARCRM SPECIFIC
309         SaveToFile($basename.'.php',$s);
310     // BEGIN SUGARCRM SPECIFIC
311     */
312         SaveToFile($dirname."/".$basename.'.php',$s);
313     // END SUGARCRM SPECIFIC
314         print "Font definition file generated (".$basename.".php)\n";
315         // BEGIN SUGARCRM SPECIFIC
316     return $dirname."/".$basename;
317     // END SUGARCRM SPECIFIC
318 }
319
320 /**
321  * Read the specified encoding map.
322  * @param string $enc map name (see /enc/ folder for valid names).
323  */
324 function ReadMap($enc) {
325         //Read a map file
326         $file = dirname(__FILE__).'/enc/'.strtolower($enc).'.map';
327         $a = file($file);
328         if (empty($a)) {
329                 die('Error: encoding not found: '.$enc);
330         }
331         $cc2gn = array();
332         foreach ($a as $l) {
333                 if ($l{0} == '!') {
334                         $e = preg_split('/[ \\t]+/',rtrim($l));
335                         $cc = hexdec(substr($e[0],1));
336                         $gn = $e[2];
337                         $cc2gn[$cc] = $gn;
338                 }
339         }
340         for($i = 0; $i <= 255; $i++) {
341                 if(!isset($cc2gn[$i])) {
342                         $cc2gn[$i] = '.notdef';
343                 }
344         }
345         return $cc2gn;
346 }
347
348 /**
349  * Read UFM file
350  */
351 function ReadUFM($file, &$cidtogidmap) {
352         //Prepare empty CIDToGIDMap
353         $cidtogidmap = str_pad('', (256 * 256 * 2), "\x00");
354         //Read a font metric file
355         $a = file($file);
356         if (empty($a)) {
357                 die('File not found');
358         }
359         $widths = array();
360         $fm = array();
361         foreach($a as $l) {
362                 $e = explode(' ',chop($l));
363                 if(count($e) < 2) {
364                         continue;
365                 }
366                 $code = $e[0];
367                 $param = $e[1];
368                 if($code == 'U') {
369                         // U 827 ; WX 0 ; N squaresubnosp ; G 675 ;
370                         //Character metrics
371                         $cc = (int)$e[1];
372                         if ($cc != -1) {
373                         $gn = $e[7];
374                         $w = $e[4];
375                         $glyph = $e[10];
376                         $widths[$cc] = $w;
377                         if($cc == ord('X')) {
378                                 $fm['CapXHeight'] = $e[13];
379                         }
380                         // Set GID
381                         if (($cc >= 0) AND ($cc < 0xFFFF) AND $glyph) {
382                                 $cidtogidmap{($cc * 2)} = chr($glyph >> 8);
383                                 $cidtogidmap{(($cc * 2) + 1)} = chr($glyph & 0xFF);
384                         }
385                 }
386                 if(($gn == '.notdef') AND (!isset($fm['MissingWidth']))) {
387                         $fm['MissingWidth'] = $w;
388                 }
389                 } elseif($code == 'FontName') {
390                         $fm['FontName'] = $param;
391                 } elseif($code == 'Weight') {
392                         $fm['Weight'] = $param;
393                 } elseif($code == 'ItalicAngle') {
394                         $fm['ItalicAngle'] = (double)$param;
395                 } elseif($code == 'Ascender') {
396                         $fm['Ascender'] = (int)$param;
397                 } elseif($code == 'Descender') {
398                         $fm['Descender'] = (int)$param;
399                 } elseif($code == 'UnderlineThickness') {
400                         $fm['UnderlineThickness'] = (int)$param;
401                 } elseif($code == 'UnderlinePosition') {
402                         $fm['UnderlinePosition'] = (int)$param;
403                 } elseif($code == 'IsFixedPitch') {
404                         $fm['IsFixedPitch'] = ($param == 'true');
405                 } elseif($code == 'FontBBox') {
406                         $fm['FontBBox'] = array($e[1], $e[2], $e[3], $e[4]);
407                 } elseif($code == 'CapHeight') {
408                         $fm['CapHeight'] = (int)$param;
409                 } elseif($code == 'StdVW') {
410                         $fm['StdVW'] = (int)$param;
411                 }
412         }
413         if(!isset($fm['MissingWidth'])) {
414                 $fm['MissingWidth'] = 600;
415         }
416         if(!isset($fm['FontName'])) {
417                 die('FontName not found');
418         }
419         $fm['Widths'] = $widths;
420         return $fm;
421 }
422
423 /**
424  * Read AFM file
425  */
426 function ReadAFM($file,&$map) {
427         //Read a font metric file
428         $a = file($file);
429         if(empty($a)) {
430                 die('File not found');
431         }
432         $widths = array();
433         $fm = array();
434         $fix = array(
435                 'Edot'=>'Edotaccent',
436                 'edot'=>'edotaccent',
437                 'Idot'=>'Idotaccent',
438                 'Zdot'=>'Zdotaccent',
439                 'zdot'=>'zdotaccent',
440                 'Odblacute' => 'Ohungarumlaut',
441                 'odblacute' => 'ohungarumlaut',
442                 'Udblacute'=>'Uhungarumlaut',
443                 'udblacute'=>'uhungarumlaut',
444                 'Gcedilla'=>'Gcommaaccent'
445                 ,'gcedilla'=>'gcommaaccent',
446                 'Kcedilla'=>'Kcommaaccent',
447                 'kcedilla'=>'kcommaaccent',
448                 'Lcedilla'=>'Lcommaaccent',
449                 'lcedilla'=>'lcommaaccent',
450                 'Ncedilla'=>'Ncommaaccent',
451                 'ncedilla'=>'ncommaaccent',
452                 'Rcedilla'=>'Rcommaaccent',
453                 'rcedilla'=>'rcommaaccent',
454                 'Scedilla'=>'Scommaaccent',
455                 'scedilla'=>'scommaaccent',
456                 'Tcedilla'=>'Tcommaaccent',
457                 'tcedilla'=>'tcommaaccent',
458                 'Dslash'=>'Dcroat',
459                 'dslash'=>'dcroat',
460                 'Dmacron'=>'Dcroat',
461                 'dmacron'=>'dcroat',
462                 'combininggraveaccent'=>'gravecomb',
463                 'combininghookabove'=>'hookabovecomb',
464                 'combiningtildeaccent'=>'tildecomb',
465                 'combiningacuteaccent'=>'acutecomb',
466                 'combiningdotbelow'=>'dotbelowcomb',
467                 'dongsign'=>'dong'
468                 );
469         foreach($a as $l) {
470                 $e = explode(' ', rtrim($l));
471                 if (count($e) < 2) {
472                         continue;
473                 }
474                 $code = $e[0];
475                 $param = $e[1];
476                 if ($code == 'C') {
477                         //Character metrics
478                         $cc = (int)$e[1];
479                         $w = $e[4];
480                         $gn = $e[7];
481                         if (substr($gn, -4) == '20AC') {
482                                 $gn = 'Euro';
483                         }
484                         if (isset($fix[$gn])) {
485                                 //Fix incorrect glyph name
486                                 foreach ($map as $c => $n) {
487                                         if ($n == $fix[$gn]) {
488                                                 $map[$c] = $gn;
489                                         }
490                                 }
491                         }
492                         if (empty($map)) {
493                                 //Symbolic font: use built-in encoding
494                                 $widths[$cc] = $w;
495                         } else {
496                                 $widths[$gn] = $w;
497                                 if($gn == 'X') {
498                                         $fm['CapXHeight'] = $e[13];
499                                 }
500                         }
501                         if($gn == '.notdef') {
502                                 $fm['MissingWidth'] = $w;
503                         }
504                 } elseif($code == 'FontName') {
505                         $fm['FontName'] = $param;
506                 } elseif($code == 'Weight') {
507                         $fm['Weight'] = $param;
508                 } elseif($code == 'ItalicAngle') {
509                         $fm['ItalicAngle'] = (double)$param;
510                 } elseif($code == 'Ascender') {
511                         $fm['Ascender'] = (int)$param;
512                 } elseif($code == 'Descender') {
513                         $fm['Descender'] = (int)$param;
514                 } elseif($code == 'UnderlineThickness') {
515                         $fm['UnderlineThickness'] = (int)$param;
516                 } elseif($code == 'UnderlinePosition') {
517                         $fm['UnderlinePosition'] = (int)$param;
518                 } elseif($code == 'IsFixedPitch') {
519                         $fm['IsFixedPitch'] = ($param == 'true');
520                 } elseif($code == 'FontBBox') {
521                         $fm['FontBBox'] = array($e[1], $e[2], $e[3], $e[4]);
522                 } elseif($code == 'CapHeight') {
523                         $fm['CapHeight'] = (int)$param;
524                 } elseif($code == 'StdVW') {
525                         $fm['StdVW'] = (int)$param;
526                 }
527         }
528         if (!isset($fm['FontName'])) {
529                 die('FontName not found');
530         }
531         if (!empty($map)) {
532                 if (!isset($widths['.notdef'])) {
533                         $widths['.notdef'] = 600;
534                 }
535                 if (!isset($widths['Delta']) AND isset($widths['increment'])) {
536                         $widths['Delta'] = $widths['increment'];
537                 }
538                 //Order widths according to map
539                 for ($i = 0; $i <= 255; $i++) {
540                         if (!isset($widths[$map[$i]])) {
541                                 print "Warning: character ".$map[$i]." is missing\n";
542                                 $widths[$i] = $widths['.notdef'];
543                         } else {
544                                 $widths[$i] = $widths[$map[$i]];
545                         }
546                 }
547         }
548         $fm['Widths'] = $widths;
549         return $fm;
550 }
551
552 function MakeFontDescriptor($fm, $symbolic=false) {
553         //Ascent
554         $asc = (isset($fm['Ascender']) ? $fm['Ascender'] : 1000);
555         $fd = "array('Ascent'=>".$asc;
556         //Descent
557         $desc = (isset($fm['Descender']) ? $fm['Descender'] : -200);
558         $fd .= ",'Descent'=>".$desc;
559         //CapHeight
560         if (isset($fm['CapHeight'])) {
561                 $ch = $fm['CapHeight'];
562         } elseif (isset($fm['CapXHeight'])) {
563                 $ch = $fm['CapXHeight'];
564         } else {
565                 $ch = $asc;
566         }
567         $fd .= ",'CapHeight'=>".$ch;
568         //Flags
569         $flags = 0;
570         if (isset($fm['IsFixedPitch']) AND $fm['IsFixedPitch']) {
571                 $flags += 1<<0;
572         }
573         if ($symbolic) {
574                 $flags += 1<<2;
575         } else {
576                 $flags += 1<<5;
577         }
578         if (isset($fm['ItalicAngle']) AND ($fm['ItalicAngle'] != 0)) {
579                 $flags += 1<<6;
580         }
581         $fd .= ",'Flags'=>".$flags;
582         //FontBBox
583         if (isset($fm['FontBBox'])) {
584                 $fbb = $fm['FontBBox'];
585         } else {
586                 $fbb = array(0, ($desc - 100), 1000, ($asc + 100));
587         }
588         $fd .= ",'FontBBox'=>'[".$fbb[0].' '.$fbb[1].' '.$fbb[2].' '.$fbb[3]."]'";
589         //ItalicAngle
590         $ia = (isset($fm['ItalicAngle']) ? $fm['ItalicAngle'] : 0);
591         $fd .= ",'ItalicAngle'=>".$ia;
592         //StemV
593         if (isset($fm['StdVW'])) {
594                 $stemv = $fm['StdVW'];
595         } elseif (isset($fm['Weight']) and eregi('(bold|black)', $fm['Weight'])) {
596                 $stemv = 120;
597         } else {
598                 $stemv = 70;
599         }
600         $fd .= ",'StemV'=>".$stemv;
601         //MissingWidth
602         if(isset($fm['MissingWidth'])) {
603                 $fd .= ",'MissingWidth'=>".$fm['MissingWidth'];
604         }
605         $fd .= ')';
606         return $fd;
607 }
608
609 function MakeWidthArray($fm) {
610         //Make character width array
611         $s = 'array(';
612         $cw = $fm['Widths'];
613         $els = array();
614         $c = 0;
615         foreach ($cw as $i => $w) {
616                 if (is_numeric($i)) {
617                         $els[] = (((($c++)%10) == 0) ? "\n" : '').$i.'=>'.$w;
618                 }
619         }
620         $s .= implode(',', $els);
621         $s .= ')';
622         return $s;
623 }
624
625 function MakeFontEncoding($map) {
626         //Build differences from reference encoding
627         $ref = ReadMap('cp1252');
628         $s = '';
629         $last = 0;
630         for ($i = 32; $i <= 255; $i++) {
631                 if ($map[$i] != $ref[$i]) {
632                         if ($i != $last+1) {
633                                 $s .= $i.' ';
634                         }
635                         $last = $i;
636                         $s .= '/'.$map[$i].' ';
637                 }
638         }
639         return rtrim($s);
640 }
641
642 function SaveToFile($file, $s, $mode='t') {
643         $f = fopen($file, 'w'.$mode);
644         if(!$f) {
645                 die('Can\'t write to file '.$file);
646         }
647         fwrite($f, $s, strlen($s));
648         fclose($f);
649 }
650
651 function ReadShort($f) {
652         $a = unpack('n1n', fread($f, 2));
653         return $a['n'];
654 }
655
656 function ReadLong($f) {
657         $a = unpack('N1N', fread($f, 4));
658         return $a['N'];
659 }
660
661 function CheckTTF($file) {
662         //Check if font license allows embedding
663         $f = fopen($file, 'rb');
664         if (!$f) {
665                 die('Error: unable to open '.$file);
666         }
667         //Extract number of tables
668         fseek($f, 4, SEEK_CUR);
669         $nb = ReadShort($f);
670         fseek($f, 6, SEEK_CUR);
671         //Seek OS/2 table
672         $found = false;
673         for ($i = 0; $i < $nb; $i++) {
674                 if (fread($f, 4) == 'OS/2') {
675                         $found = true;
676                         break;
677                 }
678                 fseek($f, 12, SEEK_CUR);
679         }
680         if (!$found) {
681                 fclose($f);
682                 return;
683         }
684         fseek($f, 4, SEEK_CUR);
685         $offset = ReadLong($f);
686         fseek($f, $offset, SEEK_SET);
687         //Extract fsType flags
688         fseek($f, 8, SEEK_CUR);
689         $fsType = ReadShort($f);
690         $rl = ($fsType & 0x02) != 0;
691         $pp = ($fsType & 0x04) != 0;
692         $e = ($fsType & 0x08) != 0;
693         fclose($f);
694         if($rl AND (!$pp) AND (!$e)) {
695                 print "Warning: font license does not allow embedding\n";
696         }
697 }
698 // BEGIN SUGARCRM SPECIFIC
699 /*
700 // END SUGARCRM SPECIFIC
701
702 $arg = $GLOBALS['argv'];
703 if (count($arg) >= 3) {
704         ob_start();
705         array_shift($arg);
706         if (sizeof($arg) == 3) {
707                 $arg[3] = $arg[2];
708                 $arg[2] = true;
709         } else {
710                 if (!isset($arg[2])) {
711                         $arg[2] = true;
712                 }
713                 if (!isset($arg[3])) {
714                         $arg[3] = 'cp1252';
715                 }
716         }
717         if (!isset($arg[4])) {
718                 $arg[4] = array();
719         }
720         MakeFont($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]);
721         $t = ob_get_clean();
722         print preg_replace('!<BR( /)?>!i', "\n", $t);
723 } else {
724         print "Usage: makefont.php <ttf/otf/pfb file> <afm/ufm file> <encoding> <patch>\n";
725 }
726
727 // BEGIN SUGARCRM SPECIFIC
728 */
729 // END SUGARCRM SPECIFIC
730 ?>