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

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:  * OutUtil.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\Exception as FontException;
 20: 
 21: /**
 22:  * Com\Tecnick\Pdf\Font\OutUtil
 23:  *
 24:  * @since       2011-05-23
 25:  * @category    Library
 26:  * @package     PdfFont
 27:  * @author      Nicola Asuni <info@tecnick.com>
 28:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 29:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 30:  * @link        https://github.com/tecnickcom/tc-lib-pdf-font
 31:  */
 32: abstract class OutUtil
 33: {
 34:     /**
 35:      * Return font full path
 36:      *
 37:      * @param string $fontdir Original font directory
 38:      * @param string $file    Font file name.
 39:      *
 40:      * @return string Font full path or empty string
 41:      */
 42:     protected function getFontFullPath($fontdir, $file)
 43:     {
 44:         $dirobj = new Dir();
 45:         // directories where to search for the font definition file
 46:         $dirs = array_unique(
 47:             array(
 48:                 '',
 49:                 $fontdir,
 50:                 (defined('K_PATH_FONTS') ? K_PATH_FONTS : ''),
 51:                 $dirobj->findParentDir('fonts', __DIR__),
 52:             )
 53:         );
 54:         foreach ($dirs as $dir) {
 55:             if (@is_readable($dir.$file)) {
 56:                 return $dir.$file;
 57:             }
 58:         }
 59:         throw new FontException('Unable to locate the file: '.$file);
 60:     }
 61: 
 62:     /**
 63:      * Outputs font widths
 64:      *
 65:      * @param array $font      Font to process
 66:      * @param int   $cidoffset Offset for CID values
 67:      *
 68:      * @return string PDF command string for font widths
 69:      */
 70:     protected function getCharWidths(array $font, $cidoffset = 0)
 71:     {
 72:         ksort($font['cw']);
 73:         $range = $this->getWidthRanges($font, $cidoffset);
 74:         // output data
 75:         $wdt = '';
 76:         foreach ($range as $kdx => $wds) {
 77:             if (count(array_count_values($wds)) == 1) {
 78:                 // interval mode is more compact
 79:                 $wdt .= ' '.$kdx.' '.($kdx + count($wds) - 1).' '.$wds[0];
 80:             } else {
 81:                 // range mode
 82:                 $wdt .= ' '.$kdx.' [ '.implode(' ', $wds).' ]';
 83:             }
 84:         }
 85:         return '/W ['.$wdt.' ]';
 86:     }
 87: 
 88:     /**
 89:      * get width ranges of characters
 90:      *
 91:      * @param array $font      Font to process
 92:      * @param int   $cidoffset Offset for CID values
 93:      *
 94:      * @return array
 95:      */
 96:     protected function getWidthRanges(array $font, $cidoffset = 0)
 97:     {
 98:         $range = array();
 99:         $rangeid = 0;
100:         $prevcid = -2;
101:         $prevwidth = -1;
102:         $interval = false;
103:         // for each character
104:         foreach ($font['cw'] as $cid => $width) {
105:             $cid -= $cidoffset;
106:             if ($font['subset'] && (!isset($font['subsetchars'][$cid]))) {
107:                 // ignore the unused characters (font subsetting)
108:                 continue;
109:             }
110:             if ($width != $font['dw']) {
111:                 if ($cid == ($prevcid + 1)) {
112:                     // consecutive CID
113:                     if ($width == $prevwidth) {
114:                         if ($width == $range[$rangeid][0]) {
115:                             $range[$rangeid][] = $width;
116:                         } else {
117:                             array_pop($range[$rangeid]);
118:                             // new range
119:                             $rangeid = $prevcid;
120:                             $range[$rangeid] = array();
121:                             $range[$rangeid][] = $prevwidth;
122:                             $range[$rangeid][] = $width;
123:                         }
124:                         $interval = true;
125:                         $range[$rangeid]['interval'] = true;
126:                     } else {
127:                         if ($interval) {
128:                             // new range
129:                             $rangeid = $cid;
130:                             $range[$rangeid] = array();
131:                             $range[$rangeid][] = $width;
132:                         } else {
133:                             $range[$rangeid][] = $width;
134:                         }
135:                         $interval = false;
136:                     }
137:                 } else {
138:                     // new range
139:                     $rangeid = $cid;
140:                     $range[$rangeid] = array();
141:                     $range[$rangeid][] = $width;
142:                     $interval = false;
143:                 }
144:                 $prevcid = $cid;
145:                 $prevwidth = $width;
146:             }
147:         }
148:         return $this->optimizeWidthRanges($range);
149:     }
150: 
151:     /**
152:      * Optimize width ranges
153:      *
154:      * @param array $range Widht Ranges
155:      *
156:      * @return array
157:      */
158:     protected function optimizeWidthRanges($range)
159:     {
160:         $prevk = -1;
161:         $nextk = -1;
162:         $prevint = false;
163:         foreach ($range as $kdx => $wds) {
164:             $cws = count($wds);
165:             if (($kdx == $nextk) && (!$prevint) && ((!isset($wds['interval'])) || ($cws < 4))) {
166:                 unset($range[$kdx]['interval']);
167:                 $range[$prevk] = array_merge($range[$prevk], $range[$kdx]);
168:                 unset($range[$kdx]);
169:             } else {
170:                 $prevk = $kdx;
171:             }
172:             $nextk = $kdx + $cws;
173:             if (isset($wds['interval'])) {
174:                 if ($cws > 3) {
175:                     $prevint = true;
176:                 } else {
177:                     $prevint = false;
178:                 }
179:                 if (isset($range[$kdx]['interval'])) {
180:                     unset($range[$kdx]['interval']);
181:                 }
182:                 --$nextk;
183:             } else {
184:                 $prevint = false;
185:             }
186:         }
187:         return $range;
188:     }
189: }
190: 
 

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