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

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:  * OutFont.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\Unicode\Data\Identity;
 19: use \Com\Tecnick\Pdf\Encrypt\Encrypt;
 20: use \Com\Tecnick\Pdf\Font\Exception as FontException;
 21: 
 22: /**
 23:  * Com\Tecnick\Pdf\Font\OutFont
 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 OutFont extends \Com\Tecnick\Pdf\Font\OutUtil
 34: {
 35:     /**
 36:      * Current PDF object number
 37:      *
 38:      * @var int
 39:      */
 40:     protected $pon;
 41: 
 42:     /**
 43:      * Encrypt object
 44:      *
 45:      * @var Encrypt
 46:      */
 47:     protected $enc;
 48: 
 49:     /**
 50:      * Get the PDF output string for a CID-0 font.
 51:      * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
 52:      *
 53:      * @param array $font Font to process
 54:      *
 55:      * return string
 56:      */
 57:     protected function getCid0(array $font)
 58:     {
 59:         $cidoffset = 0;
 60:         if (!isset($font['cw'][1])) {
 61:             $cidoffset = 31;
 62:         }
 63:         $this->uniToCid($font, $cidoffset);
 64:         $name = $font['name'];
 65:         $longname = $name;
 66:         if (!empty($font['enc'])) {
 67:             $longname .= '-'.$font['enc'];
 68:         }
 69:         
 70:         // obj 1
 71:         $out = $font['n'].' 0 obj'."\n"
 72:             .'<</Type /Font'
 73:             .' /Subtype /Type0'
 74:             .' /BaseFont /'.$longname
 75:             .' /Name /F'.$font['i'];
 76:         if (!empty($font['enc'])) {
 77:             $out .= ' /Encoding /'.$font['enc'];
 78:         }
 79:         $out .= ' /DescendantFonts ['.($this->pon + 1).' 0 R]'
 80:             .' >>'."\n"
 81:             .'endobj'."\n";
 82: 
 83:         // obj 2
 84:         $out .= (++$this->pon).' 0 obj'."\n"
 85:             .'<</Type /Font'
 86:             .' /Subtype /CIDFontType0'
 87:             .' /BaseFont /'.$name;
 88:         $cidinfo = '/Registry '.$this->enc->escapeDataString($font['cidinfo']['Registry'], $this->pon)
 89:             .' /Ordering '.$this->enc->escapeDataString($font['cidinfo']['Ordering'], $this->pon)
 90:             .' /Supplement '.$font['cidinfo']['Supplement'];
 91:         $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>'
 92:             .' /FontDescriptor '.($this->pon + 1).' 0 R'
 93:             .' /DW '.$font['dw']."\n"
 94:             .$this->getCharWidths($font, $cidoffset)
 95:             .' >>'."\n"
 96:             .'endobj'."\n";
 97: 
 98:         // obj 3
 99:         $out .= (++$this->pon).' 0 obj'."\n"
100:             .'<</Type /FontDescriptor /FontName /'.$name;
101:         foreach ($font['desc'] as $key => $val) {
102:             if ($key != 'Style') {
103:                 $out .= $this->getKeyValOut($key, $val);
104:             }
105:         }
106:         $out .= '>>'."\n"
107:             .'endobj'."\n";
108: 
109:         return $out;
110:     }
111: 
112:     /**
113:      * Convert Unicode to CID
114:      *
115:      * @param array $font      Font to process
116:      * @param int   $cidoffset Offset for CID values
117:      *
118:      * @return array Processed font
119:      */
120:     protected function uniToCid(array &$font, $cidoffset)
121:     {
122:         if (isset($font['cidinfo']['uni2cid'])) {
123:             // convert unicode to cid.
124:             $uni2cid = $font['cidinfo']['uni2cid'];
125:             $chw = array();
126:             foreach ($font['cw'] as $uni => $width) {
127:                 if (isset($uni2cid[$uni])) {
128:                     $chw[($uni2cid[$uni] + $cidoffset)] = $width;
129:                 } elseif ($uni < 256) {
130:                     $chw[$uni] = $width;
131:                 } // else unknown character
132:             }
133:             $font['cw'] = array_merge($font['cw'], $chw);
134:         }
135:     }
136: 
137:     /**
138:      * Get the PDF output string for a TrueTypeUnicode font.
139:      * Based on PDF Reference 1.3 (section 5)
140:      *
141:      * @param array $font Font to process
142:      *
143:      * return string
144:      */
145:     protected function getTrueTypeUnicode(array $font)
146:     {
147:         $fontname = '';
148:         if ($font['subset']) {
149:             // change name for font subsetting
150:             $subtag = sprintf('%06u', $font['i']);
151:             $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
152:             $fontname .= $subtag.'+';
153:         }
154:         $fontname .= $font['name'];
155: 
156:         // Type0 Font
157:         // A composite font composed of other fonts, organized hierarchically
158: 
159:         // obj 1
160:         $out = $font['n'].' 0 obj'."\n"
161:             .'<< /Type /Font'
162:             .' /Subtype /Type0'
163:             .' /BaseFont /'.$fontname
164:             .' /Name /F'.$font['i']
165:             .' /Encoding /'.$font['enc']
166:             .' /ToUnicode '.($this->pon + 1).' 0 R'
167:             .' /DescendantFonts ['.($this->pon + 2).' 0 R]'
168:             .' >>'."\n"
169:             .'endobj'."\n";
170: 
171:         // ToUnicode Object
172:         $out .= (++$this->pon).' 0 obj'."\n";
173:         $stream = $this->enc->encryptString(gzcompress(Identity::CIDHMAP), $this->pon); // ToUnicode map for Identity-H
174:         $out .= '<</Filter /FlateDecode /Length '.strlen($stream).'>> stream'."\n"
175:             .$stream."\n"
176:             .'endstream'."\n"
177:             .'endobj'."\n";
178: 
179:         // CIDFontType2
180:         // A CIDFont whose glyph descriptions are based on TrueType font technology
181:         $out .= (++$this->pon).' 0 obj'."\n"
182:             .'<< /Type /Font'
183:             .' /Subtype /CIDFontType2'
184:             .' /BaseFont /'.$fontname;
185:         // A dictionary containing entries that define the character collection of the CIDFont.
186:         $cidinfo = '/Registry '.$this->enc->escapeDataString($font['cidinfo']['Registry'], $this->pon)
187:             .' /Ordering '.$this->enc->escapeDataString($font['cidinfo']['Ordering'], $this->pon)
188:             .' /Supplement '.$font['cidinfo']['Supplement'];
189:         $out .= ' /CIDSystemInfo << '.$cidinfo.' >>'
190:             .' /FontDescriptor '.($this->pon + 1).' 0 R'
191:             .' /DW '.$font['dw']."\n"
192:             .$this->getCharWidths($font, 0);
193:         if (!empty($font['ctg'])) {
194:             $out .= "\n".'/CIDToGIDMap '.($this->pon + 2).' 0 R';
195:         }
196:         $out .= ' >>'."\n"
197:             .'endobj'."\n";
198: 
199:         // Font descriptor
200:         // A font descriptor describing the CIDFont default metrics other than its glyph widths
201:         $out .= (++$this->pon).' 0 obj'."\n"
202:             .'<< /Type /FontDescriptor'
203:             .' /FontName /'.$fontname;
204:         foreach ($font['desc'] as $key => $val) {
205:             $out .= $this->getKeyValOut($key, $val);
206:         }
207: 
208:         if (!empty($font['file_n'])) {
209:             // A stream containing a TrueType font
210:             $out .= ' /FontFile2 '.$font['file_n'].' 0 R';
211:         }
212:         $out .= ' >>'."\n"
213:             .'endobj'."\n";
214: 
215:         if (!empty($font['ctg'])) {
216:             $out .= (++$this->pon).' 0 obj'."\n";
217:             // Embed CIDToGIDMap
218:             // A specification of the mapping from CIDs to glyph indices
219:             // search and get CTG font file to embedd
220:             $ctgfile = strtolower($font['ctg']);
221:             // search and get ctg font file to embedd
222:             $fontfile = $this->getFontFullPath($font['dir'], $ctgfile);
223:             $stream = $this->enc->encryptString(file_get_contents($fontfile), $this->pon);
224:             $out .= '<< /Length '.strlen($stream).'';
225:             if (substr($fontfile, -2) == '.z') { // check file extension
226:                 // Decompresses data encoded using the public-domain
227:                 // zlib/deflate compression method, reproducing the
228:                 // original text or binary data
229:                 $out .= ' /Filter /FlateDecode';
230:             }
231:             $out .= ' >>'
232:                 .' stream'."\n"
233:                 .$stream."\n"
234:                 .'endstream'."\n"
235:                 .'endobj'."\n";
236:         }
237: 
238:         return $out;
239:     }
240: 
241:     /**
242:      * Get the PDF output string for a Core font.
243:      *
244:      * @param array $font Font to process
245:      *
246:      * return string
247:      */
248:     protected function getCore(array $font)
249:     {
250:         $out = $font['n'].' 0 obj'."\n"
251:             .'<</Type /Font'
252:             .' /Subtype /Type1'
253:             .' /BaseFont /'.$font['name']
254:             .' /Name /F'.$font['i'];
255:         if (($font['family'] != 'symbol') && ($font['family'] != 'zapfdingbats')) {
256:             $out .= ' /Encoding /WinAnsiEncoding';
257:         }
258:         $out .= ' >>'."\n"
259:             .'endobj'."\n";
260:         return $out;
261:     }
262: 
263:     /**
264:      * Get the PDF output string for a Core font.
265:      *
266:      * @param array $font Font to process
267:      *
268:      * return string
269:      */
270:     protected function getTrueType(array $font)
271:     {
272:         // obj 1
273:         $out = $font['n'].' 0 obj'."\n"
274:             .'<</Type /Font'
275:             .' /Subtype /'.$font['type']
276:             .' /BaseFont /'.$font['name']
277:             .' /Name /F'.$font['i']
278:             .' /FirstChar 32 /LastChar 255'
279:             .' /Widths '.($this->pon + 1).' 0 R'
280:             .' /FontDescriptor '.($this->pon + 2).' 0 R';
281:         if (!empty($font['enc'])) {
282:             if (isset($font['diff_n'])) {
283:                 $out .= ' /Encoding '.$font['diff_n'].' 0 R';
284:             } else {
285:                 $out .= ' /Encoding /WinAnsiEncoding';
286:             }
287:         }
288:         $out .= ' >>'."\n"
289:             .'endobj'."\n";
290: 
291:         // obj 2 - Widths
292:         $out .= (++$this->pon).' 0 obj'."\n"
293:             .'[';
294:         for ($idx = 32; $idx < 256; ++$idx) {
295:             if (isset($font['cw'][$idx])) {
296:                 $out .= $font['cw'][$idx].' ';
297:             } else {
298:                 $out .= $font['dw'].' ';
299:             }
300:         }
301:         $out .= ']'."\n"
302:             .'endobj'."\n";
303: 
304:         // obj 3 - Descriptor
305:         $out .= (++$this->pon).' 0 obj'."\n"
306:             .'<</Type /FontDescriptor /FontName /'.$font['name'];
307:         foreach ($font['desc'] as $fdk => $fdv) {
308:             $out .= $this->getKeyValOut($fdk, $fdv);
309:         }
310:         if (!empty($font['file'])) {
311:             $out .= ' /FontFile'.($font['type'] == 'Type1' ? '' : '2').' '.$font['file_n'].' 0 R';
312:         }
313:         $out .= '>>'."\n"
314:             .'endobj'."\n";
315: 
316:         return $out;
317:     }
318: 
319:     /**
320:      * Returns the formatted key/value PDF string
321:      *
322:      * @param string $key   Key name
323:      * @param mixed  $value Value
324:      *
325:      * @return string
326:      */
327:     protected function getKeyValOut($key, $val)
328:     {
329:         if (is_float($val)) {
330:             $val = sprintf('%F', $val);
331:         }
332:         return ' /'.$key.' '.$val.'';
333:     }
334: }
335: 
 

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