TrimmedTable.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /** Zend_Pdf_Cmap */
  21. require_once 'Zend/Pdf/Cmap.php';
  22. /**
  23. * Implements the "trimmed table mapping" character map (type 6).
  24. *
  25. * This table type is preferred over the {@link Zend_Pdf_Cmap_SegmentToDelta}
  26. * table when the Unicode characters covered by the font fall into a single
  27. * contiguous range.
  28. *
  29. * @package Zend_Pdf
  30. * @subpackage Fonts
  31. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Pdf_Cmap_TrimmedTable extends Zend_Pdf_Cmap
  35. {
  36. /**** Instance Variables ****/
  37. /**
  38. * The starting character code covered by this table.
  39. * @var integer
  40. */
  41. protected $_startCode = 0;
  42. /**
  43. * The ending character code covered by this table.
  44. * @var integer
  45. */
  46. protected $_endCode = 0;
  47. /**
  48. * Glyph index array. Stores the actual glyph numbers.
  49. * @var array
  50. */
  51. protected $_glyphIndexArray = array();
  52. /**** Public Interface ****/
  53. /* Concrete Class Implementation */
  54. /**
  55. * Returns an array of glyph numbers corresponding to the Unicode characters.
  56. *
  57. * If a particular character doesn't exist in this font, the special 'missing
  58. * character glyph' will be substituted.
  59. *
  60. * See also {@link glyphNumberForCharacter()}.
  61. *
  62. * @param array $characterCodes Array of Unicode character codes (code points).
  63. * @return array Array of glyph numbers.
  64. */
  65. public function glyphNumbersForCharacters($characterCodes)
  66. {
  67. $glyphNumbers = array();
  68. foreach ($characterCodes as $key => $characterCode) {
  69. if (($characterCode < $this->_startCode) || ($characterCode > $this->_endCode)) {
  70. $glyphNumbers[$key] = Zend_Pdf_Cmap::MISSING_CHARACTER_GLYPH;
  71. continue;
  72. }
  73. $glyphIndex = $characterCode - $this->_startCode;
  74. $glyphNumbers[$key] = $this->_glyphIndexArray[$glyphIndex];
  75. }
  76. return $glyphNumbers;
  77. }
  78. /**
  79. * Returns the glyph number corresponding to the Unicode character.
  80. *
  81. * If a particular character doesn't exist in this font, the special 'missing
  82. * character glyph' will be substituted.
  83. *
  84. * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
  85. * operations.
  86. *
  87. * @param integer $characterCode Unicode character code (code point).
  88. * @return integer Glyph number.
  89. */
  90. public function glyphNumberForCharacter($characterCode)
  91. {
  92. if (($characterCode < $this->_startCode) || ($characterCode > $this->_endCode)) {
  93. return Zend_Pdf_Cmap::MISSING_CHARACTER_GLYPH;
  94. }
  95. $glyphIndex = $characterCode - $this->_startCode;
  96. return $this->_glyphIndexArray[$glyphIndex];
  97. }
  98. /**
  99. * Returns an array containing the Unicode characters that have entries in
  100. * this character map.
  101. *
  102. * @return array Unicode character codes.
  103. */
  104. public function getCoveredCharacters()
  105. {
  106. $characterCodes = array();
  107. for ($code = $this->_startCode; $code <= $this->_endCode; $code++) {
  108. $characterCodes[] = $code;
  109. }
  110. return $characterCodes;
  111. }
  112. /**
  113. * Returns an array containing the glyphs numbers that have entries in this character map.
  114. * Keys are Unicode character codes (integers)
  115. *
  116. * This functionality is partially covered by glyphNumbersForCharacters(getCoveredCharacters())
  117. * call, but this method do it in more effective way (prepare complete list instead of searching
  118. * glyph for each character code).
  119. *
  120. * @internal
  121. * @return array Array representing <Unicode character code> => <glyph number> pairs.
  122. */
  123. public function getCoveredCharactersGlyphs()
  124. {
  125. $glyphNumbers = array();
  126. for ($code = $this->_startCode; $code <= $this->_endCode; $code++) {
  127. $glyphNumbers[$code] = $this->_glyphIndexArray[$code - $this->_startCode];
  128. }
  129. return $glyphNumbers;
  130. }
  131. /* Object Lifecycle */
  132. /**
  133. * Object constructor
  134. *
  135. * Parses the raw binary table data. Throws an exception if the table is
  136. * malformed.
  137. *
  138. * @param string $cmapData Raw binary cmap table data.
  139. * @throws Zend_Pdf_Exception
  140. */
  141. public function __construct($cmapData)
  142. {
  143. /* Sanity check: The table should be at least 9 bytes in size.
  144. */
  145. $actualLength = strlen($cmapData);
  146. if ($actualLength < 9) {
  147. throw new Zend_Pdf_Exception('Insufficient table data',
  148. Zend_Pdf_Exception::CMAP_TABLE_DATA_TOO_SMALL);
  149. }
  150. /* Sanity check: Make sure this is right data for this table type.
  151. */
  152. $type = $this->_extractUInt2($cmapData, 0);
  153. if ($type != Zend_Pdf_Cmap::TYPE_TRIMMED_TABLE) {
  154. throw new Zend_Pdf_Exception('Wrong cmap table type',
  155. Zend_Pdf_Exception::CMAP_WRONG_TABLE_TYPE);
  156. }
  157. $length = $this->_extractUInt2($cmapData, 2);
  158. if ($length != $actualLength) {
  159. throw new Zend_Pdf_Exception("Table length ($length) does not match actual length ($actualLength)",
  160. Zend_Pdf_Exception::CMAP_WRONG_TABLE_LENGTH);
  161. }
  162. /* Mapping tables should be language-independent. The font may not work
  163. * as expected if they are not. Unfortunately, many font files in the
  164. * wild incorrectly record a language ID in this field, so we can't
  165. * call this a failure.
  166. */
  167. $language = $this->_extractUInt2($cmapData, 4);
  168. if ($language != 0) {
  169. // Record a warning here somehow?
  170. }
  171. $this->_startCode = $this->_extractUInt2($cmapData, 6);
  172. $entryCount = $this->_extractUInt2($cmapData, 8);
  173. $expectedCount = ($length - 10) >> 1;
  174. if ($entryCount != $expectedCount) {
  175. throw new Zend_Pdf_Exception("Entry count is wrong; expected: $expectedCount; actual: $entryCount",
  176. Zend_Pdf_Exception::CMAP_WRONG_ENTRY_COUNT);
  177. }
  178. $this->_endCode = $this->_startCode + $entryCount - 1;
  179. $offset = 10;
  180. for ($i = 0; $i < $entryCount; $i++, $offset += 2) {
  181. $this->_glyphIndexArray[] = $this->_extractUInt2($cmapData, $offset);
  182. }
  183. /* Sanity check: After reading all of the data, we should be at the end
  184. * of the table.
  185. */
  186. if ($offset != $length) {
  187. throw new Zend_Pdf_Exception("Ending offset ($offset) does not match length ($length)",
  188. Zend_Pdf_Exception::CMAP_FINAL_OFFSET_NOT_LENGTH);
  189. }
  190. }
  191. }