Cmap.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. * @category Zend
  16. * @package Zend_Pdf
  17. * @subpackage Fonts
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Pdf_Cmap_ByteEncoding */
  23. require_once 'Zend/Pdf/Cmap/ByteEncoding.php';
  24. /** Zend_Pdf_Cmap_ByteEncoding_Static */
  25. require_once 'Zend/Pdf/Cmap/ByteEncoding/Static.php';
  26. /** Zend_Pdf_Cmap_SegmentToDelta */
  27. require_once 'Zend/Pdf/Cmap/SegmentToDelta.php';
  28. /** Zend_Pdf_Cmap_TrimmedTable */
  29. require_once 'Zend/Pdf/Cmap/TrimmedTable.php';
  30. /**
  31. * Abstract helper class for {@link Zend_Pdf_Resource_Font} which manages font
  32. * character maps.
  33. *
  34. * Defines the public interface for concrete subclasses which are responsible
  35. * for mapping Unicode characters to the font's glyph numbers. Also provides
  36. * shared utility methods.
  37. *
  38. * Cmap objects should ordinarily be obtained through the factory method
  39. * {@link cmapWithTypeData()}.
  40. *
  41. * The supported character map types are those found in the OpenType spec. For
  42. * additional detail on the internal binary format of these tables, see:
  43. * <ul>
  44. * <li>{@link http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6cmap.html}
  45. * <li>{@link http://www.microsoft.com/OpenType/OTSpec/cmap.htm}
  46. * <li>{@link http://partners.adobe.com/public/developer/opentype/index_cmap.html}
  47. * </ul>
  48. *
  49. * @todo Write code for Zend_Pdf_FontCmap_HighByteMapping class.
  50. * @todo Write code for Zend_Pdf_FontCmap_MixedCoverage class.
  51. * @todo Write code for Zend_Pdf_FontCmap_TrimmedArray class.
  52. * @todo Write code for Zend_Pdf_FontCmap_SegmentedCoverage class.
  53. *
  54. * @package Zend_Pdf
  55. * @subpackage Fonts
  56. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  57. * @license http://framework.zend.com/license/new-bsd New BSD License
  58. */
  59. abstract class Zend_Pdf_Cmap
  60. {
  61. /**** Class Constants ****/
  62. /* Cmap Table Types */
  63. /**
  64. * Byte Encoding character map table type.
  65. */
  66. const TYPE_BYTE_ENCODING = 0x00;
  67. /**
  68. * High Byte Mapping character map table type.
  69. */
  70. const TYPE_HIGH_BYTE_MAPPING = 0x02;
  71. /**
  72. * Segment Value to Delta Mapping character map table type.
  73. */
  74. const TYPE_SEGMENT_TO_DELTA = 0x04;
  75. /**
  76. * Trimmed Table character map table type.
  77. */
  78. const TYPE_TRIMMED_TABLE = 0x06;
  79. /**
  80. * Mixed Coverage character map table type.
  81. */
  82. const TYPE_MIXED_COVERAGE = 0x08;
  83. /**
  84. * Trimmed Array character map table type.
  85. */
  86. const TYPE_TRIMMED_ARRAY = 0x0a;
  87. /**
  88. * Segmented Coverage character map table type.
  89. */
  90. const TYPE_SEGMENTED_COVERAGE = 0x0c;
  91. /**
  92. * Static Byte Encoding character map table type. Variant of
  93. * {@link TYPE_BYTEENCODING}.
  94. */
  95. const TYPE_BYTE_ENCODING_STATIC = 0xf1;
  96. /**
  97. * Unknown character map table type.
  98. */
  99. const TYPE_UNKNOWN = 0xff;
  100. /* Special Glyph Names */
  101. /**
  102. * Glyph representing missing characters.
  103. */
  104. const MISSING_CHARACTER_GLYPH = 0x00;
  105. /**** Public Interface ****/
  106. /* Factory Methods */
  107. /**
  108. * Instantiates the appropriate concrete subclass based on the type of cmap
  109. * table and returns the instance.
  110. *
  111. * The cmap type must be one of the following values:
  112. * <ul>
  113. * <li>{@link Zend_Pdf_Cmap::TYPE_BYTE_ENCODING}
  114. * <li>{@link Zend_Pdf_Cmap::TYPE_BYTE_ENCODING_STATIC}
  115. * <li>{@link Zend_Pdf_Cmap::TYPE_HIGH_BYTE_MAPPING}
  116. * <li>{@link Zend_Pdf_Cmap::TYPE_SEGMENT_TO_DELTA}
  117. * <li>{@link Zend_Pdf_Cmap::TYPE_TRIMMED_TABLE}
  118. * <li>{@link Zend_Pdf_Cmap::TYPE_MIXED_COVERAGE}
  119. * <li>{@link Zend_Pdf_Cmap::TYPE_TRIMMED_ARRAY}
  120. * <li>{@link Zend_Pdf_Cmap::TYPE_SEGMENTED_COVERAGE}
  121. * </ul>
  122. *
  123. * Throws an exception if the table type is invalid or the cmap table data
  124. * cannot be validated.
  125. *
  126. * @param integer $cmapType Type of cmap.
  127. * @param mixed $cmapData Cmap table data. Usually a string or array.
  128. * @return Zend_Pdf_Cmap
  129. * @throws Zend_Pdf_Exception
  130. */
  131. public static function cmapWithTypeData($cmapType, $cmapData)
  132. {
  133. switch ($cmapType) {
  134. case Zend_Pdf_Cmap::TYPE_BYTE_ENCODING:
  135. return new Zend_Pdf_Cmap_ByteEncoding($cmapData);
  136. case Zend_Pdf_Cmap::TYPE_BYTE_ENCODING_STATIC:
  137. return new Zend_Pdf_Cmap_ByteEncoding_Static($cmapData);
  138. case Zend_Pdf_Cmap::TYPE_HIGH_BYTE_MAPPING:
  139. require_once 'Zend/Pdf/Exception.php';
  140. throw new Zend_Pdf_Exception('High byte mapping cmap currently unsupported',
  141. Zend_Pdf_Exception::CMAP_TYPE_UNSUPPORTED);
  142. case Zend_Pdf_Cmap::TYPE_SEGMENT_TO_DELTA:
  143. return new Zend_Pdf_Cmap_SegmentToDelta($cmapData);
  144. case Zend_Pdf_Cmap::TYPE_TRIMMED_TABLE:
  145. return new Zend_Pdf_Cmap_TrimmedTable($cmapData);
  146. case Zend_Pdf_Cmap::TYPE_MIXED_COVERAGE:
  147. require_once 'Zend/Pdf/Exception.php';
  148. throw new Zend_Pdf_Exception('Mixed coverage cmap currently unsupported',
  149. Zend_Pdf_Exception::CMAP_TYPE_UNSUPPORTED);
  150. case Zend_Pdf_Cmap::TYPE_TRIMMED_ARRAY:
  151. require_once 'Zend/Pdf/Exception.php';
  152. throw new Zend_Pdf_Exception('Trimmed array cmap currently unsupported',
  153. Zend_Pdf_Exception::CMAP_TYPE_UNSUPPORTED);
  154. case Zend_Pdf_Cmap::TYPE_SEGMENTED_COVERAGE:
  155. require_once 'Zend/Pdf/Exception.php';
  156. throw new Zend_Pdf_Exception('Segmented coverage cmap currently unsupported',
  157. Zend_Pdf_Exception::CMAP_TYPE_UNSUPPORTED);
  158. default:
  159. require_once 'Zend/Pdf/Exception.php';
  160. throw new Zend_Pdf_Exception("Unknown cmap type: $cmapType",
  161. Zend_Pdf_Exception::CMAP_UNKNOWN_TYPE);
  162. }
  163. }
  164. /* Abstract Methods */
  165. /**
  166. * Object constructor
  167. *
  168. * Parses the raw binary table data. Throws an exception if the table is
  169. * malformed.
  170. *
  171. * @param string $cmapData Raw binary cmap table data.
  172. * @throws Zend_Pdf_Exception
  173. */
  174. abstract public function __construct($cmapData);
  175. /**
  176. * Returns an array of glyph numbers corresponding to the Unicode characters.
  177. *
  178. * If a particular character doesn't exist in this font, the special 'missing
  179. * character glyph' will be substituted.
  180. *
  181. * See also {@link glyphNumberForCharacter()}.
  182. *
  183. * @param array $characterCodes Array of Unicode character codes (code points).
  184. * @return array Array of glyph numbers.
  185. */
  186. abstract public function glyphNumbersForCharacters($characterCodes);
  187. /**
  188. * Returns the glyph number corresponding to the Unicode character.
  189. *
  190. * If a particular character doesn't exist in this font, the special 'missing
  191. * character glyph' will be substituted.
  192. *
  193. * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
  194. * operations.
  195. *
  196. * @param integer $characterCode Unicode character code (code point).
  197. * @return integer Glyph number.
  198. */
  199. abstract public function glyphNumberForCharacter($characterCode);
  200. /**
  201. * Returns an array containing the Unicode characters that have entries in
  202. * this character map.
  203. *
  204. * @return array Unicode character codes.
  205. */
  206. abstract public function getCoveredCharacters();
  207. /**
  208. * Returns an array containing the glyphs numbers that have entries in this character map.
  209. * Keys are Unicode character codes (integers)
  210. *
  211. * This functionality is partially covered by glyphNumbersForCharacters(getCoveredCharacters())
  212. * call, but this method do it in more effective way (prepare complete list instead of searching
  213. * glyph for each character code).
  214. *
  215. * @internal
  216. * @return array Array representing <Unicode character code> => <glyph number> pairs.
  217. */
  218. abstract public function getCoveredCharactersGlyphs();
  219. /**** Internal Methods ****/
  220. /* Internal Utility Methods */
  221. /**
  222. * Extracts a signed 2-byte integer from a string.
  223. *
  224. * Integers are always big-endian. Throws an exception if the index is out
  225. * of range.
  226. *
  227. * @param string &$data
  228. * @param integer $index Position in string of integer.
  229. * @return integer
  230. * @throws Zend_Pdf_Exception
  231. */
  232. protected function _extractInt2(&$data, $index)
  233. {
  234. if (($index < 0) | (($index + 1) > strlen($data))) {
  235. require_once 'Zend/Pdf/Exception.php';
  236. throw new Zend_Pdf_Exception("Index out of range: $index",
  237. Zend_Pdf_Exception::INDEX_OUT_OF_RANGE);
  238. }
  239. $number = ord($data[$index]);
  240. if (($number & 0x80) == 0x80) { // negative
  241. $number = ~((((~ $number) & 0xff) << 8) | ((~ ord($data[++$index])) & 0xff));
  242. } else {
  243. $number = ($number << 8) | ord($data[++$index]);
  244. }
  245. return $number;
  246. }
  247. /**
  248. * Extracts an unsigned 2-byte integer from a string.
  249. *
  250. * Integers are always big-endian. Throws an exception if the index is out
  251. * of range.
  252. *
  253. * @param string &$data
  254. * @param integer $index Position in string of integer.
  255. * @return integer
  256. * @throws Zend_Pdf_Exception
  257. */
  258. protected function _extractUInt2(&$data, $index)
  259. {
  260. if (($index < 0) | (($index + 1) > strlen($data))) {
  261. require_once 'Zend/Pdf/Exception.php';
  262. throw new Zend_Pdf_Exception("Index out of range: $index",
  263. Zend_Pdf_Exception::INDEX_OUT_OF_RANGE);
  264. }
  265. $number = (ord($data[$index]) << 8) | ord($data[++$index]);
  266. return $number;
  267. }
  268. /**
  269. * Extracts an unsigned 4-byte integer from a string.
  270. *
  271. * Integers are always big-endian. Throws an exception if the index is out
  272. * of range.
  273. *
  274. * NOTE: If you ask for a 4-byte unsigned integer on a 32-bit machine, the
  275. * resulting value WILL BE SIGNED because PHP uses signed integers internally
  276. * for everything. To guarantee portability, be sure to use bitwise or
  277. * similar operators on large integers!
  278. *
  279. * @param string &$data
  280. * @param integer $index Position in string of integer.
  281. * @return integer
  282. * @throws Zend_Pdf_Exception
  283. */
  284. protected function _extractUInt4(&$data, $index)
  285. {
  286. if (($index < 0) | (($index + 3) > strlen($data))) {
  287. require_once 'Zend/Pdf/Exception.php';
  288. throw new Zend_Pdf_Exception("Index out of range: $index",
  289. Zend_Pdf_Exception::INDEX_OUT_OF_RANGE);
  290. }
  291. $number = (ord($data[$index]) << 24) | (ord($data[++$index]) << 16) |
  292. (ord($data[++$index]) << 8) | ord($data[++$index]);
  293. return $number;
  294. }
  295. }