PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/devblocks/libs/ZendFramework/Zend/Pdf/Cmap.php

https://github.com/sluther/portsensor
PHP | 325 lines | 84 code | 50 blank | 191 comment | 7 complexity | 1c47d203cafb825e4a6dcc720450d51d MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  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-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Pdf_Exception */
  21. require_once 'Zend/Pdf/Exception.php';
  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-2007 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. throw new Zend_Pdf_Exception('High byte mapping cmap currently unsupported',
  140. Zend_Pdf_Exception::CMAP_TYPE_UNSUPPORTED);
  141. case Zend_Pdf_Cmap::TYPE_SEGMENT_TO_DELTA:
  142. return new Zend_Pdf_Cmap_SegmentToDelta($cmapData);
  143. case Zend_Pdf_Cmap::TYPE_TRIMMED_TABLE:
  144. return new Zend_Pdf_Cmap_TrimmedTable($cmapData);
  145. case Zend_Pdf_Cmap::TYPE_MIXED_COVERAGE:
  146. throw new Zend_Pdf_Exception('Mixed coverage cmap currently unsupported',
  147. Zend_Pdf_Exception::CMAP_TYPE_UNSUPPORTED);
  148. case Zend_Pdf_Cmap::TYPE_TRIMMED_ARRAY:
  149. throw new Zend_Pdf_Exception('Trimmed array cmap currently unsupported',
  150. Zend_Pdf_Exception::CMAP_TYPE_UNSUPPORTED);
  151. case Zend_Pdf_Cmap::TYPE_SEGMENTED_COVERAGE:
  152. throw new Zend_Pdf_Exception('Segmented coverage cmap currently unsupported',
  153. Zend_Pdf_Exception::CMAP_TYPE_UNSUPPORTED);
  154. default:
  155. throw new Zend_Pdf_Exception("Unknown cmap type: $cmapType",
  156. Zend_Pdf_Exception::CMAP_UNKNOWN_TYPE);
  157. }
  158. }
  159. /* Abstract Methods */
  160. /**
  161. * Object constructor
  162. *
  163. * Parses the raw binary table data. Throws an exception if the table is
  164. * malformed.
  165. *
  166. * @param string $cmapData Raw binary cmap table data.
  167. * @throws Zend_Pdf_Exception
  168. */
  169. abstract public function __construct($cmapData);
  170. /**
  171. * Returns an array of glyph numbers corresponding to the Unicode characters.
  172. *
  173. * If a particular character doesn't exist in this font, the special 'missing
  174. * character glyph' will be substituted.
  175. *
  176. * See also {@link glyphNumberForCharacter()}.
  177. *
  178. * @param array $characterCodes Array of Unicode character codes (code points).
  179. * @return array Array of glyph numbers.
  180. */
  181. abstract public function glyphNumbersForCharacters($characterCodes);
  182. /**
  183. * Returns the glyph number corresponding to the Unicode character.
  184. *
  185. * If a particular character doesn't exist in this font, the special 'missing
  186. * character glyph' will be substituted.
  187. *
  188. * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
  189. * operations.
  190. *
  191. * @param integer $characterCode Unicode character code (code point).
  192. * @return integer Glyph number.
  193. */
  194. abstract public function glyphNumberForCharacter($characterCode);
  195. /**
  196. * Returns an array containing the Unicode characters that have entries in
  197. * this character map.
  198. *
  199. * @return array Unicode character codes.
  200. */
  201. abstract public function getCoveredCharacters();
  202. /**** Internal Methods ****/
  203. /* Internal Utility Methods */
  204. /**
  205. * Extracts a signed 2-byte integer from a string.
  206. *
  207. * Integers are always big-endian. Throws an exception if the index is out
  208. * of range.
  209. *
  210. * @param string &$data
  211. * @param integer $index Position in string of integer.
  212. * @return integer
  213. * @throws Zend_Pdf_Exception
  214. */
  215. protected function _extractInt2(&$data, $index)
  216. {
  217. if (($index < 0) | (($index + 1) > strlen($data))) {
  218. throw new Zend_Pdf_Exception("Index out of range: $index",
  219. Zend_Pdf_Exception::INDEX_OUT_OF_RANGE);
  220. }
  221. $number = ord($data[$index]);
  222. if (($number & 0x80) == 0x80) { // negative
  223. $number = ~((((~ $number) & 0xff) << 8) | ((~ ord($data[++$index])) & 0xff));
  224. } else {
  225. $number = ($number << 8) | ord($data[++$index]);
  226. }
  227. return $number;
  228. }
  229. /**
  230. * Extracts an unsigned 2-byte integer from a string.
  231. *
  232. * Integers are always big-endian. Throws an exception if the index is out
  233. * of range.
  234. *
  235. * @param string &$data
  236. * @param integer $index Position in string of integer.
  237. * @return integer
  238. * @throws Zend_Pdf_Exception
  239. */
  240. protected function _extractUInt2(&$data, $index)
  241. {
  242. if (($index < 0) | (($index + 1) > strlen($data))) {
  243. throw new Zend_Pdf_Exception("Index out of range: $index",
  244. Zend_Pdf_Exception::INDEX_OUT_OF_RANGE);
  245. }
  246. $number = (ord($data[$index]) << 8) | ord($data[++$index]);
  247. return $number;
  248. }
  249. /**
  250. * Extracts an unsigned 4-byte integer from a string.
  251. *
  252. * Integers are always big-endian. Throws an exception if the index is out
  253. * of range.
  254. *
  255. * NOTE: If you ask for a 4-byte unsigned integer on a 32-bit machine, the
  256. * resulting value WILL BE SIGNED because PHP uses signed integers internally
  257. * for everything. To guarantee portability, be sure to use bitwise or
  258. * similar operators on large integers!
  259. *
  260. * @param string &$data
  261. * @param integer $index Position in string of integer.
  262. * @return integer
  263. * @throws Zend_Pdf_Exception
  264. */
  265. protected function _extractUInt4(&$data, $index)
  266. {
  267. if (($index < 0) | (($index + 3) > strlen($data))) {
  268. throw new Zend_Pdf_Exception("Index out of range: $index",
  269. Zend_Pdf_Exception::INDEX_OUT_OF_RANGE);
  270. }
  271. $number = (ord($data[$index]) << 24) | (ord($data[++$index]) << 16) |
  272. (ord($data[++$index]) << 8) | ord($data[++$index]);
  273. return $number;
  274. }
  275. }