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

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:  * Import.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;
 17: 
 18: use \Com\Tecnick\Pdf\Font\ImportUtil;
 19: use \Com\Tecnick\File\Byte;
 20: use \Com\Tecnick\File\Dir;
 21: use \Com\Tecnick\File\File;
 22: use \Com\Tecnick\Unicode\Data\Encoding;
 23: use \Com\Tecnick\Pdf\Font\UniToCid;
 24: use \Com\Tecnick\Pdf\Font\Exception as FontException;
 25: 
 26: /**
 27:  * Com\Tecnick\Pdf\Font\Import
 28:  *
 29:  * @since       2011-05-23
 30:  * @category    Library
 31:  * @package     PdfFont
 32:  * @author      Nicola Asuni <info@tecnick.com>
 33:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 34:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 35:  * @link        https://github.com/tecnickcom/tc-lib-pdf-font
 36:  */
 37: class Import extends ImportUtil
 38: {
 39:     /**
 40:      * Import the specified font and create output files.
 41:      *
 42:      * @param string $file           Font file to process
 43:      * @param string $output_path    Output path for generated font files (must be writeable by the web server).
 44:      *                               Leave null for default font folder.
 45:      * @param string $type           Font type. Leave empty for autodetect mode. Valid values are:
 46:      *                                   Core (AFM - Adobe Font Metrics)
 47:      *                                   TrueTypeUnicode
 48:      *                                   TrueType
 49:      *                                   Type1
 50:      *                                   CID0JP (CID-0 Japanese)
 51:      *                                   CID0KR (CID-0 Korean)
 52:      *                                   CID0CS (CID-0 Chinese Simplified)
 53:      *                                   CID0CT (CID-0 Chinese Traditional)
 54:      * @param string $encoding       Name of the encoding table to use. Leave empty for default mode.
 55:      *                               Omit this parameter for TrueType Unicode and symbolic fonts
 56:      *                               like Symbol or ZapfDingBats.
 57:      * @param int    $flags          Unsigned 32-bit integer containing flags specifying various characteristics
 58:      *                               of the font as described in "PDF32000:2008 - 9.8.2 Font Descriptor Flags":
 59:      *                                   +1 for fixed width font
 60:      *                                   +4 for symbol or +32 for non-symbol
 61:      *                                   +64 for italic
 62:      *                               Note: Fixed and Italic mode are generally autodetected, so you have to set it to
 63:      *                                     32 = non-symbolic font (default) or 4 = symbolic font.
 64:      * @param int    $platform_id    Platform ID for CMAP table to extract.
 65:      *                               For a Unicode font for Windows this value should be 3, for Macintosh should be 1.
 66:      * @param int    $encoding_id    Encoding ID for CMAP table to extract.
 67:      *                               For a Unicode font for Windows this value should be 1, for Macintosh should be 0.
 68:      *                               When Platform ID is 3, legal values for Encoding ID are:
 69:      *                                0 = Symbol,
 70:      *                                1 = Unicode,
 71:      *                                2 = ShiftJIS,
 72:      *                                3 = PRC,
 73:      *                                4 = Big5,
 74:      *                                5 = Wansung,
 75:      *                                6 = Johab,
 76:      *                                7 = Reserved,
 77:      *                                8 = Reserved,
 78:      *                                9 = Reserved,
 79:      *                               10 = UCS-4.
 80:      * @param bool   $linked         If true, links the font file to system font instead of copying the font data
 81:      *                               (not transportable).
 82:      *                               Note: this option do not work with Type1 fonts.
 83:      *
 84:      * @throws FontException in case of error
 85:      */
 86:     public function __construct(
 87:         $file,
 88:         $output_path = null,
 89:         $type = null,
 90:         $encoding = null,
 91:         $flags = 32,
 92:         $platform_id = 3,
 93:         $encoding_id = 1,
 94:         $linked = false
 95:     ) {
 96:         $this->fdt['input_file'] = $file;
 97:         $this->fdt['file_name'] = $this->makeFontName($file);
 98:         if (empty($this->fdt['file_name'])) {
 99:             throw new FontException('the font name is empty');
100:         }
101:         $this->fdt['dir'] = $this->findOutputPath($output_path);
102:         $this->fdt['datafile'] = $this->fdt['dir'].$this->fdt['file_name'].'.json';
103:         if (@file_exists($this->fdt['datafile'])) {
104:             throw new FontException('this font has been already imported: '.$this->fdt['datafile']);
105:         }
106: 
107:         // get font data
108:         if (!is_file($file) || ($this->font = @file_get_contents($file)) === false) {
109:             throw new FontException('unable to read the input font file: '.$file);
110:         }
111:         $this->fbyte = new Byte($this->font);
112:         
113:         $this->fdt['settype'] = $type;
114:         $this->fdt['type'] = $this->getFontType($type);
115:         $this->fdt['isUnicode'] = (($this->fdt['type'] == 'TrueTypeUnicode') || ($this->fdt['type'] == 'cidfont0'));
116:         $this->fdt['Flags'] = intval($flags);
117:         $this->initFlags();
118:         $this->fdt['enc'] = $this->getEncodingTable($encoding);
119:         $this->fdt['diff'] = $this->getEncodingDiff();
120:         $this->fdt['originalsize'] = strlen($this->font);
121:         $this->fdt['ctg'] = $this->fdt['file_name'].'.ctg.z';
122:         $this->fdt['platform_id'] = intval($platform_id);
123:         $this->fdt['encoding_id'] = intval($encoding_id);
124:         $this->fdt['linked'] = (bool)$linked;
125: 
126:         if ($this->fdt['type'] == 'Core') {
127:             $processor = new \Com\Tecnick\Pdf\Font\Import\Core($this->font, $this->fdt);
128:         } elseif ($this->fdt['type'] == 'Type1') {
129:             $processor = new \Com\Tecnick\Pdf\Font\Import\TypeOne($this->font, $this->fdt);
130:         } else {
131:             $processor = new \Com\Tecnick\Pdf\Font\Import\TrueType($this->font, $this->fdt, $this->fbyte);
132:         }
133:         $this->fdt = $processor->getFontMetrics();
134:         $this->saveFontData();
135:     }
136: 
137:     /**
138:      * Get all the extracted font metrics
139:      *
140:      * @return string
141:      */
142:     public function getFontMetrics()
143:     {
144:         return $this->fdt;
145:     }
146: 
147:     /**
148:      * Get the output font name
149:      *
150:      * @return string
151:      */
152:     public function getFontName()
153:     {
154:         return $this->fdt['file_name'];
155:     }
156: 
157:     /**
158:      * Initialize font flags from font name
159:      *
160:      * @param int $flags
161:      *
162:      * return int
163:      */
164:     protected function initFlags()
165:     {
166:         $filename = strtolower(basename($this->fdt['input_file']));
167: 
168:         if ((strpos($filename, 'mono') !== false)
169:             || (strpos($filename, 'courier') !== false)
170:             || (strpos($filename, 'fixed') !== false)
171:         ) {
172:             $this->fdt['Flags'] |= 1;
173:         }
174: 
175:         if ((strpos($filename, 'symbol') !== false)
176:             || (strpos($filename, 'zapfdingbats') !== false)
177:         ) {
178:             $this->fdt['Flags'] |= 4;
179:         }
180: 
181:         if ((strpos($filename, 'italic') !== false)
182:             || (strpos($filename, 'oblique') !== false)
183:         ) {
184:             $this->fdt['Flags'] |= 64;
185:         }
186:     }
187: 
188:     /**
189:      * Save the eported metadata font file
190:      */
191:     protected function saveFontData()
192:     {
193:         $pfile = '{'
194:             .'"type":"'.$this->fdt['type'].'"'
195:             .',"name":"'.$this->fdt['name'].'"'
196:             .',"up":'.$this->fdt['underlinePosition']
197:             .',"ut":'.$this->fdt['underlineThickness']
198:             .',"dw":'.(($this->fdt['MissingWidth'] > 0) ? $this->fdt['MissingWidth'] : $this->fdt['AvgWidth'])
199:             .',"diff":"'.$this->fdt['diff'].'"'
200:             .',"platform_id":'.$this->fdt['platform_id']
201:             .',"encoding_id":'.$this->fdt['encoding_id'];
202: 
203:         if ($this->fdt['type'] == 'Core') {
204:             // Core
205:             $pfile .= ',"enc":""';
206:         } elseif ($this->fdt['type'] == 'Type1') {
207:             // Type 1
208:             $pfile .= ',"enc":"'.$this->fdt['enc'].'"'
209:                 .',"file":"'.$this->fdt['file'].'"'
210:                 .',"size1":'.$this->fdt['size1']
211:                 .',"size2":'.$this->fdt['size2'];
212:         } else {
213:             $pfile .= ',"originalsize":'.$this->fdt['originalsize'];
214:             if ($this->fdt['type'] == 'cidfont0') {
215:                 $pfile .= ','.UniToCid::$type[$this->fdt['settype']];
216:             } else {
217:                 // TrueType
218:                 $pfile .= ',"enc":"'.$this->fdt['enc'].'"'
219:                     .',"file":"'.$this->fdt['file'].'"'
220:                     .',"ctg":"'.$this->fdt['ctg'].'"';
221:                 // create CIDToGIDMap
222:                 $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
223:                 foreach ($this->fdt['ctgdata'] as $cid => $gid) {
224:                     $cidtogidmap = $this->updateCIDtoGIDmap($cidtogidmap, $cid, $gid);
225:                 }
226:                 // store compressed CIDToGIDMap
227:                 $file = new File();
228:                 $fpt = $file->fopenLocal($this->fdt['dir'].$this->fdt['ctg'], 'wb');
229:                 fwrite($fpt, gzcompress($cidtogidmap));
230:                 fclose($fpt);
231:             }
232:         }
233:         if ($this->fdt['isUnicode']) {
234:             $pfile .=',"isUnicode":true';
235:         } else {
236:             $pfile .=',"isUnicode":false';
237:         }
238: 
239:         $pfile .= ',"desc":{'
240:             .'"Flags":'.$this->fdt['Flags']
241:             .',"FontBBox":"['.$this->fdt['bbox'].']"'
242:             .',"ItalicAngle":'.$this->fdt['italicAngle']
243:             .',"Ascent":'.$this->fdt['Ascent']
244:             .',"Descent":'.$this->fdt['Descent']
245:             .',"Leading":'.$this->fdt['Leading']
246:             .',"CapHeight":'.$this->fdt['CapHeight']
247:             .',"XHeight":'.$this->fdt['XHeight']
248:             .',"StemV":'.$this->fdt['StemV']
249:             .',"StemH":'.$this->fdt['StemH']
250:             .',"AvgWidth":'.$this->fdt['AvgWidth']
251:             .',"MaxWidth":'.$this->fdt['MaxWidth']
252:             .',"MissingWidth":'.$this->fdt['MissingWidth']
253:             .'}';
254:         if (!empty($this->fdt['cbbox'])) {
255:             $pfile .= ',"cbbox":{'.substr($this->fdt['cbbox'], 1).'}';
256:         }
257:         $pfile .= ',"cw":{'.substr($this->fdt['cw'], 1).'}';
258:         $pfile .= '}'."\n";
259: 
260:         // store file
261:         $file = new File();
262:         $fpt = $file->fopenLocal($this->fdt['datafile'], 'wb');
263:         fwrite($fpt, $pfile);
264:         fclose($fpt);
265:     }
266: }
267: 
 

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