File indexing completed on 2024-06-16 05:30:16

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Pdf
0017  * @subpackage Fonts
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Zend_Pdf_Cmap */
0024 // require_once 'Zend/Pdf/Cmap.php';
0025 
0026 
0027 /**
0028  * Implements the "trimmed table mapping" character map (type 6).
0029  *
0030  * This table type is preferred over the {@link Zend_Pdf_Cmap_SegmentToDelta}
0031  * table when the Unicode characters covered by the font fall into a single
0032  * contiguous range.
0033  *
0034  * @package    Zend_Pdf
0035  * @subpackage Fonts
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_Pdf_Cmap_TrimmedTable extends Zend_Pdf_Cmap
0040 {
0041   /**** Instance Variables ****/
0042 
0043 
0044     /**
0045      * The starting character code covered by this table.
0046      * @var integer
0047      */
0048     protected $_startCode = 0;
0049 
0050     /**
0051      * The ending character code covered by this table.
0052      * @var integer
0053      */
0054     protected $_endCode = 0;
0055 
0056     /**
0057      * Glyph index array. Stores the actual glyph numbers.
0058      * @var array
0059      */
0060     protected $_glyphIndexArray = array();
0061 
0062 
0063 
0064   /**** Public Interface ****/
0065 
0066 
0067   /* Concrete Class Implementation */
0068 
0069     /**
0070      * Returns an array of glyph numbers corresponding to the Unicode characters.
0071      *
0072      * If a particular character doesn't exist in this font, the special 'missing
0073      * character glyph' will be substituted.
0074      *
0075      * See also {@link glyphNumberForCharacter()}.
0076      *
0077      * @param array $characterCodes Array of Unicode character codes (code points).
0078      * @return array Array of glyph numbers.
0079      */
0080     public function glyphNumbersForCharacters($characterCodes)
0081     {
0082         $glyphNumbers = array();
0083         foreach ($characterCodes as $key => $characterCode) {
0084 
0085             if (($characterCode < $this->_startCode) || ($characterCode > $this->_endCode)) {
0086                 $glyphNumbers[$key] = Zend_Pdf_Cmap::MISSING_CHARACTER_GLYPH;
0087                 continue;
0088             }
0089 
0090             $glyphIndex = $characterCode - $this->_startCode;
0091             $glyphNumbers[$key] = $this->_glyphIndexArray[$glyphIndex];
0092 
0093         }
0094         return $glyphNumbers;
0095     }
0096 
0097     /**
0098      * Returns the glyph number corresponding to the Unicode character.
0099      *
0100      * If a particular character doesn't exist in this font, the special 'missing
0101      * character glyph' will be substituted.
0102      *
0103      * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
0104      * operations.
0105      *
0106      * @param integer $characterCode Unicode character code (code point).
0107      * @return integer Glyph number.
0108      */
0109     public function glyphNumberForCharacter($characterCode)
0110     {
0111         if (($characterCode < $this->_startCode) || ($characterCode > $this->_endCode)) {
0112             return Zend_Pdf_Cmap::MISSING_CHARACTER_GLYPH;
0113         }
0114         $glyphIndex = $characterCode - $this->_startCode;
0115         return $this->_glyphIndexArray[$glyphIndex];
0116     }
0117 
0118     /**
0119      * Returns an array containing the Unicode characters that have entries in
0120      * this character map.
0121      *
0122      * @return array Unicode character codes.
0123      */
0124     public function getCoveredCharacters()
0125     {
0126         $characterCodes = array();
0127         for ($code = $this->_startCode; $code <= $this->_endCode; $code++) {
0128             $characterCodes[] = $code;
0129         }
0130         return $characterCodes;
0131     }
0132 
0133 
0134     /**
0135      * Returns an array containing the glyphs numbers that have entries in this character map.
0136      * Keys are Unicode character codes (integers)
0137      *
0138      * This functionality is partially covered by glyphNumbersForCharacters(getCoveredCharacters())
0139      * call, but this method do it in more effective way (prepare complete list instead of searching
0140      * glyph for each character code).
0141      *
0142      * @internal
0143      * @return array Array representing <Unicode character code> => <glyph number> pairs.
0144      */
0145     public function getCoveredCharactersGlyphs()
0146     {
0147         $glyphNumbers = array();
0148         for ($code = $this->_startCode; $code <= $this->_endCode; $code++) {
0149             $glyphNumbers[$code] = $this->_glyphIndexArray[$code - $this->_startCode];
0150         }
0151 
0152         return $glyphNumbers;
0153     }
0154 
0155 
0156   /* Object Lifecycle */
0157 
0158     /**
0159      * Object constructor
0160      *
0161      * Parses the raw binary table data. Throws an exception if the table is
0162      * malformed.
0163      *
0164      * @param string $cmapData Raw binary cmap table data.
0165      * @throws Zend_Pdf_Exception
0166      */
0167     public function __construct($cmapData)
0168     {
0169         /* Sanity check: The table should be at least 9 bytes in size.
0170          */
0171         $actualLength = strlen($cmapData);
0172         if ($actualLength < 9) {
0173             // require_once 'Zend/Pdf/Exception.php';
0174             throw new Zend_Pdf_Exception('Insufficient table data',
0175                                          Zend_Pdf_Exception::CMAP_TABLE_DATA_TOO_SMALL);
0176         }
0177 
0178         /* Sanity check: Make sure this is right data for this table type.
0179          */
0180         $type = $this->_extractUInt2($cmapData, 0);
0181         if ($type != Zend_Pdf_Cmap::TYPE_TRIMMED_TABLE) {
0182             // require_once 'Zend/Pdf/Exception.php';
0183             throw new Zend_Pdf_Exception('Wrong cmap table type',
0184                                          Zend_Pdf_Exception::CMAP_WRONG_TABLE_TYPE);
0185         }
0186 
0187         $length = $this->_extractUInt2($cmapData, 2);
0188         if ($length != $actualLength) {
0189             // require_once 'Zend/Pdf/Exception.php';
0190             throw new Zend_Pdf_Exception("Table length ($length) does not match actual length ($actualLength)",
0191                                          Zend_Pdf_Exception::CMAP_WRONG_TABLE_LENGTH);
0192         }
0193 
0194         /* Mapping tables should be language-independent. The font may not work
0195          * as expected if they are not. Unfortunately, many font files in the
0196          * wild incorrectly record a language ID in this field, so we can't
0197          * call this a failure.
0198          */
0199         $language = $this->_extractUInt2($cmapData, 4);
0200         if ($language != 0) {
0201             // Record a warning here somehow?
0202         }
0203 
0204         $this->_startCode = $this->_extractUInt2($cmapData, 6);
0205 
0206         $entryCount = $this->_extractUInt2($cmapData, 8);
0207         $expectedCount = ($length - 10) >> 1;
0208         if ($entryCount != $expectedCount) {
0209             // require_once 'Zend/Pdf/Exception.php';
0210             throw new Zend_Pdf_Exception("Entry count is wrong; expected: $expectedCount; actual: $entryCount",
0211                                          Zend_Pdf_Exception::CMAP_WRONG_ENTRY_COUNT);
0212         }
0213 
0214         $this->_endCode = $this->_startCode + $entryCount - 1;
0215 
0216         $offset = 10;
0217         for ($i = 0; $i < $entryCount; $i++, $offset += 2) {
0218             $this->_glyphIndexArray[] = $this->_extractUInt2($cmapData, $offset);
0219         }
0220 
0221         /* Sanity check: After reading all of the data, we should be at the end
0222          * of the table.
0223          */
0224         if ($offset != $length) {
0225             // require_once 'Zend/Pdf/Exception.php';
0226             throw new Zend_Pdf_Exception("Ending offset ($offset) does not match length ($length)",
0227                                          Zend_Pdf_Exception::CMAP_FINAL_OFFSET_NOT_LENGTH);
0228         }
0229     }
0230 
0231 }