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

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:  * Output.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\Subset;
 19: use \Com\Tecnick\Pdf\Encrypt\Encrypt;
 20: use \Com\Tecnick\Pdf\Font\Exception as FontException;
 21: 
 22: /**
 23:  * Com\Tecnick\Pdf\Font\Output
 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 Output extends \Com\Tecnick\Pdf\Font\OutFont
 34: {
 35:     /**
 36:      * Array of imported fonts data
 37:      *
 38:      * @var array
 39:      */
 40:     protected $fonts;
 41: 
 42:     /**
 43:      * Array of character subsets for each font file
 44:      *
 45:      * @var array
 46:      */
 47:     protected $subchars = array();
 48: 
 49:     /**
 50:      * PDF string block to return containinf the fonts definitions
 51:      *
 52:      * @var string
 53:      */
 54:     protected $out = '';
 55: 
 56:     /**
 57:      * Map methods used to process each font type
 58:      *
 59:      * @var array
 60:      */
 61:     protected static $map = array(
 62:         'core'            => 'getCore',
 63:         'cidfont0'        => 'getCid0',
 64:         'type1'           => 'getTrueType',
 65:         'truetype'        => 'getTrueType',
 66:         'truetypeunicode' => 'getTrueTypeUnicode'
 67:     );
 68: 
 69:     /**
 70:      * Initialize font data
 71:      *
 72:      * @param array   $font Array of imported fonts data
 73:      * @param int     $pon  Current PDF Object Number
 74:      * @param Encrypt $enc  Encrypt object
 75:      */
 76:     public function __construct(array $fonts, $pon, Encrypt $enc)
 77:     {
 78:         $this->fonts = $fonts;
 79:         $this->pon = $pon;
 80:         $this->enc = $enc;
 81: 
 82:         $this->out = $this->getEncodingDiffs();
 83:         $this->out .= $this->getFontFiles();
 84:         $this->out .= $this->getFontDefinitions();
 85:     }
 86: 
 87:     /**
 88:      * Returns current PDF object number
 89:      *
 90:      * @return int
 91:      */
 92:     public function getObjectNumber()
 93:     {
 94:         return $this->pon;
 95:     }
 96: 
 97:     /**
 98:      * Returns the PDF fonts block
 99:      *
100:      * @return string
101:      */
102:     public function getFontsBlock()
103:     {
104:         return $this->out;
105:     }
106: 
107:     /**
108:      * Get the PDF output string for font encoding diffs
109:      *
110:      * return string
111:      */
112:     protected function getEncodingDiffs()
113:     {
114:         $out = '';
115:         $done = array(); // store processed items to avoid duplication
116:         foreach ($this->fonts as $fkey => $font) {
117:             if (!empty($font['diff'])) {
118:                 $dkey = md5($font['diff']);
119:                 if (!isset($done[$dkey])) {
120:                     $out .= (++$this->pon).' 0 obj'."\n"
121:                         .'<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$font['diff'].'] >>'."\n"
122:                         .'endobj'."\n";
123:                     $done[$dkey] = $this->pon;
124:                 }
125:                 $this->fonts[$fkey]['diff_n'] = $done[$dkey];
126:             }
127: 
128:             // extract the character subset
129:             if (!empty($font['file'])) {
130:                 $file_key = md5($font['file']);
131:                 if (empty($this->subchars[$file_key])) {
132:                     $this->subchars[$file_key] = $font['subsetchars'];
133:                 } else {
134:                     $this->subchars[$file_key] = array_merge($this->subchars[$file_key], $font['subsetchars']);
135:                 }
136:             }
137:         }
138:         return $out;
139:     }
140: 
141:     /**
142:      * Get the PDF output string for font files
143:      *
144:      * return string
145:      */
146:     protected function getFontFiles()
147:     {
148:         $out = '';
149:         $done = array(); // store processed items to avoid duplication
150:         foreach ($this->fonts as $fkey => $font) {
151:             if (!empty($font['file'])) {
152:                 $dkey = md5($font['file']);
153:                 if (!isset($done[$dkey])) {
154:                     $fontfile = $this->getFontFullPath($font['dir'], $font['file']);
155:                     $font_data = file_get_contents($fontfile);
156:                     if ($font['subset']) {
157:                         $font_data = gzuncompress($font_data);
158:                         $sub = new Subset($font_data, $font, $this->subchars[md5($font['file'])]);
159:                         $font_data = $sub->getSubsetFont();
160:                         $font['length1'] = strlen($font_data);
161:                         $font_data = gzcompress($font_data);
162:                     }
163:                     ++$this->pon;
164:                     $stream = $this->enc->encryptString($font_data, $this->pon);
165:                     $out .= $this->pon.' 0 obj'."\n"
166:                         .'<< /Length '.strlen($stream)
167:                         .' /Filter /FlateDecode'
168:                         .' /Length1 '.$font['length1'];
169:                     if (isset($font['length2'])) {
170:                         $out .= ' /Length2 '.$font['length2']
171:                             .' /Length3 0';
172:                     }
173:                     $out .= ' >>'
174:                         .' stream'."\n"
175:                         .$stream."\n"
176:                         .'endstream'."\n"
177:                         .'endobj'."\n";
178:                     $done[$dkey] = $this->pon;
179:                 }
180:                 $this->fonts[$fkey]['file_n'] = $done[$dkey];
181:             }
182:         }
183:         return $out;
184:     }
185: 
186:     /**
187:      * Get the PDF output string for fonts
188:      *
189:      * return string
190:      */
191:     protected function getFontDefinitions()
192:     {
193:         $out = '';
194:         foreach ($this->fonts as $font) {
195:             if (!isset(self::$map[strtolower($font['type'])])) {
196:                 throw new FontException('Unsupported font type: '.$font['type']);
197:             }
198:             $method = self::$map[strtolower($font['type'])];
199:             $out .= $this->$method($font);
200:         }
201:         return $out;
202:     }
203: }
204: 
 

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