PageRenderTime 126ms CodeModel.GetById 60ms app.highlight 14ms RepoModel.GetById 46ms app.codeStats 0ms

/library/Zend/Pdf/Resource/Font/CidFont.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 482 lines | 181 code | 56 blank | 245 comment | 32 complexity | 937665bae8387c262c414ad34b88d3dc MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @package    Zend_Pdf
 16 * @subpackage Fonts
 17 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 19 */
 20
 21/** Zend_Pdf_Resource_Font */
 22require_once 'Zend/Pdf/Resource/Font.php';
 23
 24/** Zend_Pdf_FileParser_Font_OpenType */
 25require_once 'Zend/Pdf/FileParser/Font/OpenType.php';
 26
 27/** Zend_Pdf_Cmap */
 28require_once 'Zend/Pdf/Cmap.php';
 29
 30
 31
 32/**
 33 * Adobe PDF CIDFont font object implementation
 34 * 
 35 * A CIDFont program contains glyph descriptions that are accessed using a CID as
 36 * the character selector. There are two types of CIDFont. A Type 0 CIDFont contains
 37 * glyph descriptions based on Adobe’s Type 1 font format, whereas those in a
 38 * Type 2 CIDFont are based on the TrueType font format.
 39 *
 40 * A CIDFont dictionary is a PDF object that contains information about a CIDFont program. 
 41 * Although its Type value is Font, a CIDFont is not actually a font. It does not have an Encoding 
 42 * entry, it cannot be listed in the Font subdictionary of a resource dictionary, and it cannot be 
 43 * used as the operand of the Tf operator. It is used only as a descendant of a Type 0 font. 
 44 * The CMap in the Type 0 font is what defines the encoding that maps character codes to CIDs 
 45 * in the CIDFont. 
 46 * 
 47 * Font objects should be normally be obtained from the factory methods
 48 * {@link Zend_Pdf_Font::fontWithName} and {@link Zend_Pdf_Font::fontWithPath}.
 49 *
 50 * @package    Zend_Pdf
 51 * @subpackage Fonts
 52 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 53 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 54 */
 55abstract class Zend_Pdf_Resource_Font_CidFont extends Zend_Pdf_Resource_Font
 56{
 57    /**
 58     * Object representing the font's cmap (character to glyph map).
 59     * @var Zend_Pdf_Cmap
 60     */
 61    protected $_cmap = null;
 62
 63    /**
 64     * Array containing the widths of each character that have entries in used character map.
 65     *
 66     * @var array
 67     */
 68    protected $_charWidths = null;
 69
 70    /**
 71     * Width for characters missed in the font
 72     * 
 73     * @var integer
 74     */
 75    protected $_missingCharWidth = 0;
 76    
 77
 78    /**
 79     * Object constructor
 80     *
 81     * @param Zend_Pdf_FileParser_Font_OpenType $fontParser Font parser object
 82     *   containing OpenType file.
 83     * @param integer $embeddingOptions Options for font embedding.
 84     * @throws Zend_Pdf_Exception
 85     */
 86    public function __construct(Zend_Pdf_FileParser_Font_OpenType $fontParser)
 87    {
 88        parent::__construct();
 89
 90        $fontParser->parse();
 91        
 92
 93        /* Object properties */
 94
 95        $this->_fontNames = $fontParser->names;
 96
 97        $this->_isBold       = $fontParser->isBold;
 98        $this->_isItalic     = $fontParser->isItalic;
 99        $this->_isMonospaced = $fontParser->isMonospaced;
100
101        $this->_underlinePosition  = $fontParser->underlinePosition;
102        $this->_underlineThickness = $fontParser->underlineThickness;
103        $this->_strikePosition     = $fontParser->strikePosition;
104        $this->_strikeThickness    = $fontParser->strikeThickness;
105
106        $this->_unitsPerEm = $fontParser->unitsPerEm;
107
108        $this->_ascent  = $fontParser->ascent;
109        $this->_descent = $fontParser->descent;
110        $this->_lineGap = $fontParser->lineGap;
111
112
113        $this->_cmap = $fontParser->cmap;
114
115
116        /* Resource dictionary */
117
118        $baseFont = $this->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en', 'UTF-8');
119        $this->_resource->BaseFont = new Zend_Pdf_Element_Name($baseFont);
120
121
122        /**
123         * Prepare widths array.
124         */
125        /* Constract characters widths array using font CMap and glyphs widths array */
126        $glyphWidths = $fontParser->glyphWidths;
127        $charGlyphs  = $this->_cmap->getCoveredCharactersGlyphs();
128        $charWidths  = array();
129        foreach ($charGlyphs as $charCode => $glyph) {
130        	$charWidths[$charCode] = $glyphWidths[$glyph];
131        }
132        $this->_charWidths       = $charWidths;
133        $this->_missingCharWidth = $glyphWidths[0];  
134
135        /* Width array optimization. Step1: extract default value */
136        $widthFrequencies = array_count_values($charWidths);
137        $defaultWidth          = null;
138        $defaultWidthFrequency = -1;
139        foreach ($widthFrequencies as $width => $frequency) {
140        	if ($frequency > $defaultWidthFrequency) {
141        		$defaultWidth          = $width;
142        		$defaultWidthFrequency = $frequency;
143        	}
144        }
145
146        // Store default value in the font dictionary
147        $this->_resource->DW = new Zend_Pdf_Element_Numeric($this->toEmSpace($defaultWidth));
148        
149        // Remove characters which corresponds to default width from the widths array
150        $defWidthChars = array_keys($charWidths, $defaultWidth);
151        foreach ($defWidthChars as $charCode) {
152        	unset($charWidths[$charCode]);
153        } 
154
155        // Order cheracter widths aray by character codes
156        ksort($charWidths, SORT_NUMERIC);
157
158        /* Width array optimization. Step2: Compact character codes sequences */
159        $lastCharCode = -1;
160        $widthsSequences = array();
161        foreach ($charWidths as $charCode => $width) {
162        	if ($lastCharCode == -1) {
163                $charCodesSequense = array();
164                $sequenceStartCode = $charCode;
165        	} else if ($charCode != $lastCharCode + 1) {
166        		// New chracters sequence detected
167        		$widthsSequences[$sequenceStartCode] = $charCodesSequense; 
168        		$charCodesSequense = array();
169        		$sequenceStartCode = $charCode;
170        	}
171        	$charCodesSequense[] = $width;
172        	$lastCharCode = $charCode;
173        }
174        // Save last sequence, if widths array is not empty (it may happens for monospaced fonts)
175        if (count($charWidths) != 0) {
176        	$widthsSequences[$sequenceStartCode] = $charCodesSequense;
177        } 
178
179        $pdfCharsWidths = array();
180        foreach ($widthsSequences as $startCode => $widthsSequence) {
181            /* Width array optimization. Step3: Compact widths sequences */
182            $pdfWidths        = array();
183            $lastWidth        = -1;
184            $widthsInSequence = 0;
185            foreach ($widthsSequence as $width) {
186            	if ($lastWidth != $width) {
187            		// New width is detected
188            		if ($widthsInSequence != 0) {
189                        // Previous width value was a part of the widths sequence. Save it as 'c_1st c_last w'.
190                        $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode);                         // First character code
191            			$pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode + $widthsInSequence - 1); // Last character code
192                        $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($lastWidth));       // Width 
193
194                        // Reset widths sequence
195                        $startCode = $startCode + $widthsInSequence;
196                        $widthsInSequence = 0;
197            		}
198
199                    // Collect new width
200            		$pdfWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($width));
201
202            		$lastWidth = $width;
203            	} else {
204            		// Width is equal to previous
205            		if (count($pdfWidths) != 0) {
206            			// We already have some widths collected 
207            			// So, we've just detected new widths sequence
208            			
209            			// Remove last element from widths list, since it's a part of widths sequence
210            			array_pop($pdfWidths);
211
212            			// and write the rest if it's not empty 
213            			if (count($pdfWidths) != 0) {
214                            // Save it as 'c_1st [w1 w2 ... wn]'.
215                            $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
216            				$pdfCharsWidths[] = new Zend_Pdf_Element_Array($pdfWidths);   // Widths array
217
218                            // Reset widths collection
219                            $startCode += count($pdfWidths);
220                            $pdfWidths = array();
221            			}
222
223                        $widthsInSequence = 2;
224            		} else {
225                        // Continue widths sequence
226                        $widthsInSequence++;
227            		}
228            	}
229            }
230
231            // Check if we have widths collection or widths sequence to wite it down
232            if (count($pdfWidths) != 0) {
233            	// We have some widths collected
234                // Save it as 'c_1st [w1 w2 ... wn]'.
235                $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
236                $pdfCharsWidths[] = new Zend_Pdf_Element_Array($pdfWidths);   // Widths array
237            } else if ($widthsInSequence != 0){
238                // We have widths sequence
239                // Save it as 'c_1st c_last w'.
240                $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode);                         // First character code
241                $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode + $widthsInSequence - 1); // Last character code
242                $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($lastWidth));       // Width 
243            }
244        }
245
246        /* Create the Zend_Pdf_Element_Array object and add it to the font's
247         * object factory and resource dictionary.
248         */
249        $widthsArrayElement = new Zend_Pdf_Element_Array($pdfCharsWidths);
250        $widthsObject = $this->_objectFactory->newObject($widthsArrayElement);
251        $this->_resource->W = $widthsObject;
252
253        
254        /* CIDSystemInfo dictionary */
255        $cidSystemInfo = new Zend_Pdf_Element_Dictionary();
256        $cidSystemInfo->Registry   = new Zend_Pdf_Element_String('Adobe');
257        $cidSystemInfo->Ordering   = new Zend_Pdf_Element_String('UCS');
258        $cidSystemInfo->Supplement = new Zend_Pdf_Element_Numeric(0);
259        $cidSystemInfoObject            = $this->_objectFactory->newObject($cidSystemInfo);
260        $this->_resource->CIDSystemInfo = $cidSystemInfoObject;
261    }
262
263    
264    
265    /**
266     * Returns an array of glyph numbers corresponding to the Unicode characters.
267     *
268     * If a particular character doesn't exist in this font, the special 'missing
269     * character glyph' will be substituted.
270     *
271     * See also {@link glyphNumberForCharacter()}.
272     *
273     * @param array $characterCodes Array of Unicode character codes (code points).
274     * @return array Array of glyph numbers.
275     */
276    public function glyphNumbersForCharacters($characterCodes)
277    {
278        /**
279         * CIDFont object is not actually a font. It does not have an Encoding entry, 
280         * it cannot be listed in the Font subdictionary of a resource dictionary, and 
281         * it cannot be used as the operand of the Tf operator.
282         * 
283         * Throw an exception.
284         */
285        throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
286    }
287
288    /**
289     * Returns the glyph number corresponding to the Unicode character.
290     *
291     * If a particular character doesn't exist in this font, the special 'missing
292     * character glyph' will be substituted.
293     *
294     * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
295     * operations.
296     *
297     * @param integer $characterCode Unicode character code (code point).
298     * @return integer Glyph number.
299     */
300    public function glyphNumberForCharacter($characterCode)
301    {
302        /**
303         * CIDFont object is not actually a font. It does not have an Encoding entry, 
304         * it cannot be listed in the Font subdictionary of a resource dictionary, and 
305         * it cannot be used as the operand of the Tf operator.
306         * 
307         * Throw an exception.
308         */
309        throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
310    }
311
312
313    /**
314     * Returns a number between 0 and 1 inclusive that indicates the percentage
315     * of characters in the string which are covered by glyphs in this font.
316     *
317     * Since no one font will contain glyphs for the entire Unicode character
318     * range, this method can be used to help locate a suitable font when the
319     * actual contents of the string are not known.
320     *
321     * Note that some fonts lie about the characters they support. Additionally,
322     * fonts don't usually contain glyphs for control characters such as tabs
323     * and line breaks, so it is rare that you will get back a full 1.0 score.
324     * The resulting value should be considered informational only.
325     *
326     * @param string $string
327     * @param string $charEncoding (optional) Character encoding of source text.
328     *   If omitted, uses 'current locale'.
329     * @return float
330     */
331    public function getCoveredPercentage($string, $charEncoding = '')
332    {
333        /* Convert the string to UTF-16BE encoding so we can match the string's
334         * character codes to those found in the cmap.
335         */
336        if ($charEncoding != 'UTF-16BE') {
337            $string = iconv($charEncoding, 'UTF-16BE', $string);
338        }
339
340        $charCount = iconv_strlen($string, 'UTF-16BE');
341        if ($charCount == 0) {
342            return 0;
343        }
344
345        /* Calculate the score by doing a lookup for each character.
346         */
347        $score = 0;
348        $maxIndex = strlen($string);
349        for ($i = 0; $i < $maxIndex; $i++) {
350            /**
351             * @todo Properly handle characters encoded as surrogate pairs.
352             */
353            $charCode = (ord($string[$i]) << 8) | ord($string[++$i]);
354            /* This could probably be optimized a bit with a binary search...
355             */
356            if (isset($this->_charWidths[$charCode])) {
357                $score++;
358            }
359        }
360        return $score / $charCount;
361    }
362
363    /**
364     * Returns the widths of the Chars.
365     *
366     * The widths are expressed in the font's glyph space. You are responsible
367     * for converting to user space as necessary. See {@link unitsPerEm()}.
368     *
369     * See also {@link widthForChar()}.
370     *
371     * @param array &$glyphNumbers Array of glyph numbers.
372     * @return array Array of glyph widths (integers).
373     */
374    public function widthsForChars($charCodes)
375    {
376        $widths = array();
377        foreach ($charCodes as $key => $charCode) {
378            if (!isset($this->_charWidths[$charCode])) {
379                $widths[$key] = $this->_missingCharWidth;
380            } else {
381                $widths[$key] = $this->_charWidths[$charCode];
382            }
383        }
384        return $widths;
385    }
386
387    /**
388     * Returns the width of the character.
389     *
390     * Like {@link widthsForChars()} but used for one char at a time.
391     *
392     * @param integer $charCode
393     * @return integer
394     */
395    public function widthForChar($charCode)
396    {
397        if (!isset($this->_charWidths[$charCode])) {
398            return $this->_missingCharWidth;
399        }
400        return $this->_charWidths[$charCode];
401    }
402    
403    /**
404     * Returns the widths of the glyphs.
405     *
406     * @param array &$glyphNumbers Array of glyph numbers.
407     * @return array Array of glyph widths (integers).
408     * @throws Zend_Pdf_Exception
409     */
410    public function widthsForGlyphs($glyphNumbers)
411    {
412        /**
413         * CIDFont object is not actually a font. It does not have an Encoding entry, 
414         * it cannot be listed in the Font subdictionary of a resource dictionary, and 
415         * it cannot be used as the operand of the Tf operator.
416         * 
417         * Throw an exception.
418         */
419        throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
420    }
421
422    /**
423     * Returns the width of the glyph.
424     *
425     * Like {@link widthsForGlyphs()} but used for one glyph at a time.
426     *
427     * @param integer $glyphNumber
428     * @return integer
429     * @throws Zend_Pdf_Exception
430     */
431    public function widthForGlyph($glyphNumber)
432    {
433        /**
434         * CIDFont object is not actually a font. It does not have an Encoding entry, 
435         * it cannot be listed in the Font subdictionary of a resource dictionary, and 
436         * it cannot be used as the operand of the Tf operator.
437         * 
438         * Throw an exception.
439         */
440        throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
441    }
442
443    /**
444     * Convert string to the font encoding.
445     *
446     * @param string $string
447     * @param string $charEncoding Character encoding of source text.
448     * @return string
449     * @throws Zend_Pdf_Exception
450     *      */
451    public function encodeString($string, $charEncoding)
452    {
453        /**
454         * CIDFont object is not actually a font. It does not have an Encoding entry, 
455         * it cannot be listed in the Font subdictionary of a resource dictionary, and 
456         * it cannot be used as the operand of the Tf operator.
457         * 
458         * Throw an exception.
459         */
460    	throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
461    }
462
463    /**
464     * Convert string from the font encoding.
465     *
466     * @param string $string
467     * @param string $charEncoding Character encoding of resulting text.
468     * @return string
469     * @throws Zend_Pdf_Exception
470     */
471    public function decodeString($string, $charEncoding)
472    {
473    	/**
474         * CIDFont object is not actually a font. It does not have an Encoding entry, 
475         * it cannot be listed in the Font subdictionary of a resource dictionary, and 
476         * it cannot be used as the operand of the Tf operator.
477         * 
478         * Throw an exception.
479    	 */
480        throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
481    }
482}