source-class-Com.Tecnick.Pdf.Font.Import.Core

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * Core.php
  4:  *
  5:  * @since       2011-05-23
  6:  * @category    Library
  7:  * @package     PdfFont
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-pdf-font
 12:  *
 13:  * This file is part of tc-lib-pdf-font software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Pdf\Font\Import;
 17: 
 18: use \Com\Tecnick\File\File;
 19: use \Com\Tecnick\Unicode\Data\Encoding;
 20: use \Com\Tecnick\Pdf\Font\Exception as FontException;
 21: 
 22: /**
 23:  * Com\Tecnick\Pdf\Font\Import\Core
 24:  *
 25:  * @since       2011-05-23
 26:  * @category    Library
 27:  * @package     PdfFont
 28:  * @author      Nicola Asuni <info@tecnick.com>
 29:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 30:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 31:  * @link        https://github.com/tecnickcom/tc-lib-pdf-font
 32:  */
 33: class Core
 34: {
 35:     /**
 36:      * Content of the input font file
 37:      *
 38:      * @var string
 39:      */
 40:     protected $font = '';
 41: 
 42:     /**
 43:      * Extracted font metrics
 44:      *
 45:      * @var array
 46:      */
 47:     protected $fdt = array();
 48: 
 49:     /**
 50:      *
 51:      * @param string $font    Content of the input font file
 52:      * @param array  $fdt Extracted font metrics
 53:      *
 54:      * @throws FontException in case of error
 55:      */
 56:     public function __construct($font, $fdt)
 57:     {
 58:         $this->font = $font;
 59:         $this->fdt = $fdt;
 60:         $this->process();
 61:     }
 62: 
 63:     /**
 64:      * Get all the extracted font metrics
 65:      *
 66:      * @return string
 67:      */
 68:     public function getFontMetrics()
 69:     {
 70:         return $this->fdt;
 71:     }
 72: 
 73:     /**
 74:      * Set Flags
 75:      */
 76:     protected function setFlags()
 77:     {
 78:         if (($this->fdt['FontName'] == 'Symbol') || ($this->fdt['FontName'] == 'ZapfDingbats')) {
 79:             $this->fdt['Flags'] |= 4;
 80:         } else {
 81:             $this->fdt['Flags'] |= 32;
 82:         }
 83:         if ($this->fdt['IsFixedPitch']) {
 84:             $this->fdt['Flags'] |= 1;
 85:         }
 86:         if ($this->fdt['ItalicAngle'] != 0) {
 87:             $this->fdt['Flags'] |= 64;
 88:         }
 89:     }
 90: 
 91:     /**
 92:      * Set Char widths
 93:      *
 94:      * @param array $cwidths Extracted widths
 95:      */
 96:     protected function setCharWidths($cwidths)
 97:     {
 98:         $this->fdt['MissingWidth'] = 600;
 99:         if (!empty($cwidths[32])) {
100:             $this->fdt['MissingWidth'] = $cwidths[32];
101:         }
102:         $this->fdt['MaxWidth'] = $this->fdt['MissingWidth'];
103:         $this->fdt['AvgWidth'] = 0;
104:         $this->fdt['cw'] = '';
105:         for ($cid = 0; $cid <= 255; ++$cid) {
106:             if (isset($cwidths[$cid])) {
107:                 if ($cwidths[$cid] > $this->fdt['MaxWidth']) {
108:                     $this->fdt['MaxWidth'] = $cwidths[$cid];
109:                 }
110:                 $this->fdt['AvgWidth'] += $cwidths[$cid];
111:                 $this->fdt['cw'] .= ',"'.$cid.'":'.$cwidths[$cid];
112:             } else {
113:                 $this->fdt['cw'] .= ',"'.$cid.'":'.$this->fdt['MissingWidth'];
114:             }
115:         }
116:         $this->fdt['AvgWidth'] = round($this->fdt['AvgWidth'] / count($cwidths));
117:     }
118: 
119:     /**
120:      * Extract Metrics
121:      */
122:     protected function extractMetrics()
123:     {
124:         $cwd = array();
125:         $this->fdt['cbbox'] = '';
126:         $lines = explode("\n", str_replace("\r", '', $this->font));
127:         // process each row
128:         foreach ($lines as $row) {
129:             $col = explode(' ', rtrim($row));
130:             if (count($col) > 1) {
131:                 $this->processMetricRow($col, $cwd);
132:             }
133:         }
134:         $this->fdt['Leading'] = 0;
135:         $this->setCharWidths($cwd);
136:     }
137: 
138:     /**
139:      * Extract Metrics
140:      *
141:      * @param array $col Array containing row elements to process
142:      * @param array $cwd Array contianing cid widths
143:      */
144:     protected function processMetricRow($col, &$cwd)
145:     {
146:         if (($col[0] == 'C') && (($cid = (int)$col[1]) >= 0)) {
147:             // character metrics
148:             $cwd[$cid] = (int)$col[4];
149:             if (!empty($col[14])) {
150:                 //cbbox
151:                 $this->fdt['cbbox'] .= ',"'.$cid.'":['.$col[10].','.$col[11].','.$col[12].','.$col[13].']';
152:             }
153:         } elseif (in_array(
154:             $col[0],
155:             array(
156:                 'FontName',
157:                 'FullName',
158:                 'FamilyName',
159:                 'Weight',
160:                 'CharacterSet',
161:                 'Version',
162:                 'EncodingScheme'
163:             )
164:         )) {
165:             $this->fdt[$col[0]] = (string)$col[1];
166:         } elseif (in_array(
167:             $col[0],
168:             array(
169:                 'ItalicAngle',
170:                 'UnderlinePosition',
171:                 'UnderlineThickness',
172:                 'CapHeight',
173:                 'XHeight',
174:                 'Ascender',
175:                 'Descender',
176:                 'StdHW',
177:                 'StdVW'
178:             )
179:         )) {
180:             $this->fdt[$col[0]] = (int)$col[1];
181:         } elseif ($col[0] == 'IsFixedPitch') {
182:             $this->fdt[$col[0]] = ($col[1] == 'true');
183:         } elseif ($col[0] == 'FontBBox') {
184:             $this->fdt[$col[0]] = array((int)$col[1], (int)$col[2], (int)$col[3], (int)$col[4]);
185:         }
186:     }
187: 
188:     /**
189:      * Map values to the correct key name
190:      */
191:     protected function remapValues()
192:     {
193:         $map = array(
194:             'FullName'           => 'name',
195:             'UnderlinePosition'  => 'underlinePosition',
196:             'UnderlineThickness' => 'underlineThickness',
197:             'ItalicAngle'        => 'italicAngle',
198:             'Ascender'           => 'Ascent',
199:             'Descender'          => 'Descent',
200:             'StdVW'              => 'StemV',
201:             'StdHW'              => 'StemH',
202:             
203:         );
204: 
205:         foreach ($map as $old => $new) {
206:             $this->fdt[$new] = $this->fdt[$old];
207:         }
208: 
209:         $this->fdt['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $this->fdt['name']);
210:         $this->fdt['bbox'] = implode(' ', $this->fdt['FontBBox']);
211: 
212:         if (empty($this->fdt['XHeight'])) {
213:             $this->fdt['XHeight'] = 0;
214:         }
215:     }
216: 
217:     /**
218:      * Set Missing values
219:      */
220:     protected function setMissingValues()
221:     {
222:         if (!isset($this->fdt['Descender'])) {
223:             $this->fdt['Descender'] = $this->fdt['FontBBox'][1];
224:         }
225:         if (!isset($this->fdt['Ascender'])) {
226:             $this->fdt['Ascender'] = $this->fdt['FontBBox'][3];
227:         }
228:         if (!isset($this->fdt['CapHeight'])) {
229:             $this->fdt['CapHeight'] = $this->fdt['Ascender'];
230:         }
231:     }
232: 
233:     /**
234:      * Process Core font
235:      */
236:     protected function process()
237:     {
238:         $this->extractMetrics();
239:         $this->setFlags();
240:         $this->setMissingValues();
241:         $this->remapValues();
242:     }
243: }
244: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy