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

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:  * Load.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\Dir;
 19: use \Com\Tecnick\Pdf\Font\Core;
 20: use \Com\Tecnick\Pdf\Font\Exception as FontException;
 21: 
 22: /**
 23:  * Com\Tecnick\Pdf\Font\Load
 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: abstract class Load
 34: {
 35:     /**
 36:      * Load the font data
 37:      *
 38:      * @throws FontException in case of error
 39:      */
 40:     public function load()
 41:     {
 42:         $this->data = array_merge($this->data, $this->getFontInfo());
 43:         $this->checkType();
 44:         $this->setName();
 45:         $this->setDefaultWidth();
 46:         if (($this->data['type'] == 'Core') || $this->data['fakestyle']) {
 47:             $this->setArtificialStyles();
 48:         }
 49:         $this->setFileData();
 50:     }
 51: 
 52:     /**
 53:      * Load the font data
 54:      *
 55:      * @return array Font data
 56:      *
 57:      * @throws FontException in case of error
 58:      */
 59:     protected function getFontInfo()
 60:     {
 61:         $this->findFontFile();
 62: 
 63:         // read the font definition file
 64:         if (!@is_readable($this->data['ifile'])) {
 65:             throw new FontException('unable to read file: '.$this->data['ifile']);
 66:         }
 67: 
 68:         $fdt = @file_get_contents($this->data['ifile']);
 69:         $fdt = @json_decode($fdt, true);
 70:         if ($fdt === null) {
 71:             throw new FontException('JSON decoding error ['.json_last_error().']');
 72:         }
 73: 
 74:         if (empty($fdt['type']) || empty($fdt['cw'])) {
 75:             throw new FontException('fhe font definition file has a bad format: '.$this->data['ifile']);
 76:         }
 77: 
 78:         return $fdt;
 79:     }
 80: 
 81:     /**
 82:      * Returns a list of font directories
 83:      *
 84:      * @return array Font directories
 85:      */
 86:     protected function findFontDirectories()
 87:     {
 88:         $dirobj = new Dir();
 89:         $dirs =  array('');
 90:         if (defined('K_PATH_FONTS')) {
 91:             $dirs[] = K_PATH_FONTS;
 92:             $dirs = array_merge($dirs, array_filter(glob(K_PATH_FONTS.'/*'), 'is_dir'));
 93:         }
 94:         $parent_font_dir = $dirobj->findParentDir('fonts', __DIR__);
 95:         if (!empty($parent_font_dir)) {
 96:             $dirs[] = $parent_font_dir;
 97:             $dirs = array_merge($dirs, array_filter(glob($parent_font_dir.'/*'), 'is_dir'));
 98:         }
 99:         return array_unique($dirs);
100:     }
101: 
102:     /**
103:      * Load the font data
104:      *
105:      * @return array Font data
106:      *
107:      * @throws FontException in case of error
108:      */
109:     protected function findFontFile()
110:     {
111:         if (!empty($this->data['ifile'])) {
112:             return;
113:         }
114: 
115:         $this->data['ifile'] = strtolower($this->data['key']).'.json';
116:  
117:         // directories where to search for the font definition file
118:         $dirs = $this->findFontDirectories();
119: 
120:         // find font definition file names
121:         $files = array_unique(
122:             array(
123:                 strtolower($this->data['key']).'.json',
124:                 strtolower($this->data['family']).'.json'
125:             )
126:         );
127: 
128:         foreach ($files as $file) {
129:             foreach ($dirs as $dir) {
130:                 if (@is_readable($dir.$file)) {
131:                     $this->data['ifile'] = $dir.$file;
132:                     break 2;
133:                 }
134:             }
135:             // we haven't found the version with style variations
136:             $this->data['fakestyle'] = true;
137:         }
138:     }
139: 
140:     /**
141:      * Set default width
142:      */
143:     protected function setDefaultWidth()
144:     {
145:         if (!empty($this->data['dw'])) {
146:             return;
147:         }
148:         if (isset($this->data['desc']['MissingWidth']) && ($this->data['desc']['MissingWidth'] > 0)) {
149:             $this->data['dw'] = $this->data['desc']['MissingWidth'];
150:         } elseif (!empty($this->data['cw'][32])) {
151:             $this->data['dw'] = $this->data['cw'][32];
152:         } else {
153:             $this->data['dw'] = 600;
154:         }
155:     }
156: 
157:     /**
158:      * Check Font Type
159:      */
160:     protected function checkType()
161:     {
162:         if (in_array($this->data['type'], array('Core', 'Type1', 'TrueType', 'TrueTypeUnicode', 'cidfont0'))) {
163:             return;
164:         }
165:         throw new FontException('Unknow font type: '.$this->data['type']);
166:     }
167: 
168:     /**
169:      * Set name
170:      */
171:     protected function setName()
172:     {
173:         if ($this->data['type'] == 'Core') {
174:             $this->data['name'] = Core::$font[$this->data['key']];
175:             $this->data['subset'] = false;
176:         } elseif (($this->data['type'] == 'Type1') || ($this->data['type'] == 'TrueType')) {
177:             $this->data['subset'] = false;
178:         } elseif ($this->data['type'] == 'TrueTypeUnicode') {
179:             $this->data['enc'] = 'Identity-H';
180:         } elseif (($this->data['type'] == 'cidfont0') && ($this->data['pdfa'])) {
181:             throw new FontException('CID0 fonts are not supported, all fonts must be embedded in PDF/A mode!');
182:         }
183:         if (empty($this->data['name'])) {
184:             $this->data['name'] = $this->data['key'];
185:         }
186:     }
187: 
188:     /**
189:      * Set artificial styles if the font variation file is missing
190:      */
191:     protected function setArtificialStyles()
192:     {
193:         // artificial bold
194:         if ($this->data['mode']['bold']) {
195:             $this->data['name'] .= 'Bold';
196:             if (isset($this->data['desc']['StemV'])) {
197:                 $this->data['desc']['StemV'] = round($this->data['desc']['StemV'] * 1.75);
198:             } else {
199:                 $this->data['desc']['StemV'] = 123;
200:             }
201:         }
202:         // artificial italic
203:         if ($this->data['mode']['italic']) {
204:             $this->data['name'] .= 'Italic';
205:             if (isset($this->data['desc']['ItalicAngle'])) {
206:                 $this->data['desc']['ItalicAngle'] -= 11;
207:             } else {
208:                 $this->data['desc']['ItalicAngle'] = -11;
209:             }
210:             if (isset($this->data['desc']['Flags'])) {
211:                 $this->data['desc']['Flags'] |= 64; //bit 7
212:             } else {
213:                 $this->data['desc']['Flags'] = 64;
214:             }
215:         }
216:     }
217: 
218:     /**
219:      * Set File data
220:      */
221:     public function setFileData()
222:     {
223:         if (empty($this->data['file'])) {
224:             return;
225:         }
226:         if (strpos($this->data['type'], 'TrueType') !== false) {
227:             $this->data['length1'] = $this->data['originalsize'];
228:             $this->data['length2'] = false;
229:         } elseif ($this->data['type'] != 'Core') {
230:             $this->data['length1'] = $this->data['size1'];
231:             $this->data['length2'] = $this->data['size2'];
232:         }
233:     }
234: }
235: 
 

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