[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Pdf / Cmap / TrimmedTable.php
blobdbb2d081424b3813ee39bf578e2ccb35bc89dcbc
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Pdf
17 * @subpackage Fonts
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
23 /** Zend_Pdf_Cmap */
24 require_once 'Zend/Pdf/Cmap.php';
27 /**
28 * Implements the "trimmed table mapping" character map (type 6).
30 * This table type is preferred over the {@link Zend_Pdf_Cmap_SegmentToDelta}
31 * table when the Unicode characters covered by the font fall into a single
32 * contiguous range.
34 * @package Zend_Pdf
35 * @subpackage Fonts
36 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_Pdf_Cmap_TrimmedTable extends Zend_Pdf_Cmap
41 /**** Instance Variables ****/
44 /**
45 * The starting character code covered by this table.
46 * @var integer
48 protected $_startCode = 0;
50 /**
51 * The ending character code covered by this table.
52 * @var integer
54 protected $_endCode = 0;
56 /**
57 * Glyph index array. Stores the actual glyph numbers.
58 * @var array
60 protected $_glyphIndexArray = array();
64 /**** Public Interface ****/
67 /* Concrete Class Implementation */
69 /**
70 * Returns an array of glyph numbers corresponding to the Unicode characters.
72 * If a particular character doesn't exist in this font, the special 'missing
73 * character glyph' will be substituted.
75 * See also {@link glyphNumberForCharacter()}.
77 * @param array $characterCodes Array of Unicode character codes (code points).
78 * @return array Array of glyph numbers.
80 public function glyphNumbersForCharacters($characterCodes)
82 $glyphNumbers = array();
83 foreach ($characterCodes as $key => $characterCode) {
85 if (($characterCode < $this->_startCode) || ($characterCode > $this->_endCode)) {
86 $glyphNumbers[$key] = Zend_Pdf_Cmap::MISSING_CHARACTER_GLYPH;
87 continue;
90 $glyphIndex = $characterCode - $this->_startCode;
91 $glyphNumbers[$key] = $this->_glyphIndexArray[$glyphIndex];
94 return $glyphNumbers;
97 /**
98 * Returns the glyph number corresponding to the Unicode character.
100 * If a particular character doesn't exist in this font, the special 'missing
101 * character glyph' will be substituted.
103 * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
104 * operations.
106 * @param integer $characterCode Unicode character code (code point).
107 * @return integer Glyph number.
109 public function glyphNumberForCharacter($characterCode)
111 if (($characterCode < $this->_startCode) || ($characterCode > $this->_endCode)) {
112 return Zend_Pdf_Cmap::MISSING_CHARACTER_GLYPH;
114 $glyphIndex = $characterCode - $this->_startCode;
115 return $this->_glyphIndexArray[$glyphIndex];
119 * Returns an array containing the Unicode characters that have entries in
120 * this character map.
122 * @return array Unicode character codes.
124 public function getCoveredCharacters()
126 $characterCodes = array();
127 for ($code = $this->_startCode; $code <= $this->_endCode; $code++) {
128 $characterCodes[] = $code;
130 return $characterCodes;
135 * Returns an array containing the glyphs numbers that have entries in this character map.
136 * Keys are Unicode character codes (integers)
138 * This functionality is partially covered by glyphNumbersForCharacters(getCoveredCharacters())
139 * call, but this method do it in more effective way (prepare complete list instead of searching
140 * glyph for each character code).
142 * @internal
143 * @return array Array representing <Unicode character code> => <glyph number> pairs.
145 public function getCoveredCharactersGlyphs()
147 $glyphNumbers = array();
148 for ($code = $this->_startCode; $code <= $this->_endCode; $code++) {
149 $glyphNumbers[$code] = $this->_glyphIndexArray[$code - $this->_startCode];
152 return $glyphNumbers;
156 /* Object Lifecycle */
159 * Object constructor
161 * Parses the raw binary table data. Throws an exception if the table is
162 * malformed.
164 * @param string $cmapData Raw binary cmap table data.
165 * @throws Zend_Pdf_Exception
167 public function __construct($cmapData)
169 /* Sanity check: The table should be at least 9 bytes in size.
171 $actualLength = strlen($cmapData);
172 if ($actualLength < 9) {
173 require_once 'Zend/Pdf/Exception.php';
174 throw new Zend_Pdf_Exception('Insufficient table data',
175 Zend_Pdf_Exception::CMAP_TABLE_DATA_TOO_SMALL);
178 /* Sanity check: Make sure this is right data for this table type.
180 $type = $this->_extractUInt2($cmapData, 0);
181 if ($type != Zend_Pdf_Cmap::TYPE_TRIMMED_TABLE) {
182 require_once 'Zend/Pdf/Exception.php';
183 throw new Zend_Pdf_Exception('Wrong cmap table type',
184 Zend_Pdf_Exception::CMAP_WRONG_TABLE_TYPE);
187 $length = $this->_extractUInt2($cmapData, 2);
188 if ($length != $actualLength) {
189 require_once 'Zend/Pdf/Exception.php';
190 throw new Zend_Pdf_Exception("Table length ($length) does not match actual length ($actualLength)",
191 Zend_Pdf_Exception::CMAP_WRONG_TABLE_LENGTH);
194 /* Mapping tables should be language-independent. The font may not work
195 * as expected if they are not. Unfortunately, many font files in the
196 * wild incorrectly record a language ID in this field, so we can't
197 * call this a failure.
199 $language = $this->_extractUInt2($cmapData, 4);
200 if ($language != 0) {
201 // Record a warning here somehow?
204 $this->_startCode = $this->_extractUInt2($cmapData, 6);
206 $entryCount = $this->_extractUInt2($cmapData, 8);
207 $expectedCount = ($length - 10) >> 1;
208 if ($entryCount != $expectedCount) {
209 require_once 'Zend/Pdf/Exception.php';
210 throw new Zend_Pdf_Exception("Entry count is wrong; expected: $expectedCount; actual: $entryCount",
211 Zend_Pdf_Exception::CMAP_WRONG_ENTRY_COUNT);
214 $this->_endCode = $this->_startCode + $entryCount - 1;
216 $offset = 10;
217 for ($i = 0; $i < $entryCount; $i++, $offset += 2) {
218 $this->_glyphIndexArray[] = $this->_extractUInt2($cmapData, $offset);
221 /* Sanity check: After reading all of the data, we should be at the end
222 * of the table.
224 if ($offset != $length) {
225 require_once 'Zend/Pdf/Exception.php';
226 throw new Zend_Pdf_Exception("Ending offset ($offset) does not match length ($length)",
227 Zend_Pdf_Exception::CMAP_FINAL_OFFSET_NOT_LENGTH);