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

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:  * ImportUtil.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\File\Byte;
 19: use \Com\Tecnick\File\Dir;
 20: use \Com\Tecnick\File\File;
 21: use \Com\Tecnick\Unicode\Data\Encoding;
 22: use \Com\Tecnick\Pdf\Font\Import\TypeOne;
 23: use \Com\Tecnick\Pdf\Font\Import\TrueType;
 24: use \Com\Tecnick\Pdf\Font\UniToCid;
 25: use \Com\Tecnick\Pdf\Font\Exception as FontException;
 26: 
 27: /**
 28:  * Com\Tecnick\Pdf\Font\ImportUtil
 29:  *
 30:  * @since       2011-05-23
 31:  * @category    Library
 32:  * @package     PdfFont
 33:  * @author      Nicola Asuni <info@tecnick.com>
 34:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 35:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 36:  * @link        https://github.com/tecnickcom/tc-lib-pdf-font
 37:  */
 38: abstract class ImportUtil
 39: {
 40:     /**
 41:      * Content of the input font file
 42:      *
 43:      * @var string
 44:      */
 45:     protected $font = '';
 46: 
 47:     /**
 48:      * Object used to read font bytes
 49:      *
 50:      * @var \Com\Tecnick\File\Byte
 51:      */
 52:     protected $fbyte;
 53: 
 54:     /**
 55:      * Extracted font metrics
 56:      *
 57:      * @var array
 58:      */
 59:     protected $fdt = array();
 60: 
 61:     /**
 62:      * Make the output font name
 63:      *
 64:      * @param string $font_file Input font file
 65:      *
 66:      * @return string
 67:      */
 68:     protected function makeFontName($font_file)
 69:     {
 70:         $font_path_parts = pathinfo($font_file);
 71:         return str_replace(
 72:             array('bold', 'oblique', 'italic', 'regular'),
 73:             array('b', 'i', 'i', ''),
 74:             preg_replace('/[^a-z0-9_]/', '', strtolower($font_path_parts['filename']))
 75:         );
 76:     }
 77: 
 78:     /**
 79:      * Find the path where to store the processed font.
 80:      *
 81:      * @param string $output_path    Output path for generated font files (must be writeable by the web server).
 82:      *                               Leave null for default font folder (K_PATH_FONTS).
 83:      *
 84:      * @return string
 85:      */
 86:     protected function findOutputPath($output_path = null)
 87:     {
 88:         if (!empty($output_path) && is_writable($output_path)) {
 89:             return $output_path;
 90:         }
 91:         if (defined('K_PATH_FONTS') && is_writable(K_PATH_FONTS)) {
 92:             return K_PATH_FONTS;
 93:         }
 94:         $dirobj = new Dir();
 95:         $dir = $dirobj->findParentDir('fonts', __DIR__);
 96:         if ($dir == '/') {
 97:             $dir = sys_get_temp_dir();
 98:         }
 99:         if (substr($dir, -1) !== '/') {
100:             $dir .= '/';
101:         }
102:         return $dir;
103:     }
104: 
105:     /**
106:      * Get the font type
107:      *
108:      * @param string $font_type      Font type. Leave empty for autodetect mode.
109:      *
110:      * @return string
111:      */
112:     protected function getFontType($font_type)
113:     {
114:         // autodetect font type
115:         if (empty($font_type)) {
116:             if (substr($this->font, 0, 16) == 'StartFontMetrics') {
117:                 // AFM type - we use this type only for the 14 Core fonts
118:                 return 'Core';
119:             }
120:             if (substr($this->font, 0, 4) == 'OTTO') {
121:                 throw new FontException('Unsupported font format: OpenType with CFF data');
122:             }
123:             if ($this->fbyte->getULong(0) == 0x10000) {
124:                 return 'TrueTypeUnicode';
125:             }
126:             return 'Type1';
127:         }
128:         if (strpos($font_type, 'CID0') === 0) {
129:             return 'cidfont0';
130:         }
131:         if (in_array($font_type, array('Core', 'Type1', 'TrueType', 'TrueTypeUnicode'))) {
132:             return $font_type;
133:         }
134:         throw new FontException('unknown or unsupported font type: '.$font_type);
135:     }
136: 
137:     /**
138:      * Get the encoding table
139:      *
140:      * @param string $encoding  Name of the encoding table to use. Leave empty for default mode.
141:      *                          Omit this parameter for TrueType Unicode and symbolic fonts
142:      *                          like Symbol or ZapfDingBats.
143:      */
144:     protected function getEncodingTable($encoding)
145:     {
146:         if (empty($encoding) && ($this->fdt['type'] == 'Type1') && (($this->fdt['Flags'] & 4) == 0)) {
147:             return 'cp1252';
148:         }
149:         return preg_replace('/[^A-Za-z0-9_\-]/', '', $encoding);
150:     }
151: 
152:     /**
153:      * If required, get differences between the reference encoding (cp1252) and the current encoding
154:      *
155:      * @return string
156:      */
157:     protected function getEncodingDiff()
158:     {
159:         $diff = '';
160:         if ((($this->fdt['type'] == 'TrueType') || ($this->fdt['type'] == 'Type1'))
161:             && (!empty($this->fdt['enc'])
162:             && ($this->fdt['enc'] != 'cp1252')
163:             && isset(Encoding::$map[$this->fdt['enc']]))
164:         ) {
165:             // build differences from reference encoding
166:             $enc_ref = Encoding::$map['cp1252'];
167:             $enc_target = Encoding::$map[$this->fdt['enc']];
168:             $last = 0;
169:             for ($idx = 32; $idx <= 255; ++$idx) {
170:                 if ($enc_target[$idx] != $enc_ref[$idx]) {
171:                     if ($idx != ($last + 1)) {
172:                         $diff .= $idx.' ';
173:                     }
174:                     $last = $idx;
175:                     $diff .= '/'.$enc_target[$idx].' ';
176:                 }
177:             }
178:         }
179:         return $diff;
180:     }
181: 
182:     /**
183:      * Update the CIDToGIDMap string with a new value
184:      *
185:      * @param string $map CIDToGIDMap.
186:      * @param int    $cid CID value.
187:      * @param int    $gid GID value.
188:      *
189:      * @return string
190:      */
191:     protected function updateCIDtoGIDmap($map, $cid, $gid)
192:     {
193:         if (($cid >= 0) && ($cid <= 0xFFFF) && ($gid >= 0)) {
194:             if ($gid > 0xFFFF) {
195:                 $gid -= 0x10000;
196:             }
197:             $map[($cid * 2)] = chr($gid >> 8);
198:             $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
199:         }
200:         return $map;
201:     }
202: }
203: 
 

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