Font.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 FileParser
  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_FileParser */
  23. require_once 'Zend/Pdf/FileParser.php';
  24. /**
  25. * Abstract helper class for {@link Zend_Pdf_Font} that parses font files.
  26. *
  27. * Defines the public interface for concrete subclasses which are responsible
  28. * for parsing the raw binary data from the font file on disk. Also provides
  29. * a debug logging interface and a couple of shared utility methods.
  30. *
  31. * @package Zend_Pdf
  32. * @subpackage FileParser
  33. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_Pdf_FileParser_Font extends Zend_Pdf_FileParser
  37. {
  38. /**** Instance Variables ****/
  39. /**
  40. * Array of parsed font properties. Used with {@link __get()} and
  41. * {@link __set()}.
  42. * @var array
  43. */
  44. private $_fontProperties = array();
  45. /**
  46. * Flag indicating whether or not debug logging is active.
  47. * @var boolean
  48. */
  49. private $_debug = false;
  50. /**** Public Interface ****/
  51. /* Object Lifecycle */
  52. /**
  53. * Object constructor.
  54. *
  55. * Validates the data source and enables debug logging if so configured.
  56. *
  57. * @param Zend_Pdf_FileParserDataSource $dataSource
  58. * @throws Zend_Pdf_Exception
  59. */
  60. public function __construct(Zend_Pdf_FileParserDataSource $dataSource)
  61. {
  62. parent::__construct($dataSource);
  63. $this->fontType = Zend_Pdf_Font::TYPE_UNKNOWN;
  64. }
  65. /* Accessors */
  66. /**
  67. * Get handler
  68. *
  69. * @param string $property
  70. * @return mixed
  71. */
  72. public function __get($property)
  73. {
  74. if (isset($this->_fontProperties[$property])) {
  75. return $this->_fontProperties[$property];
  76. } else {
  77. return null;
  78. }
  79. }
  80. /* NOTE: The set handler is defined below in the internal methods group. */
  81. /* Parser Methods */
  82. /**
  83. * Reads the Unicode UTF-16-encoded string from the binary file at the
  84. * current offset location. Overridden to fix return character set at UTF-16BE.
  85. *
  86. * @todo Deal with to-dos in the parent method.
  87. *
  88. * @param integer $byteCount Number of bytes (characters * 2) to return.
  89. * @param integer $byteOrder (optional) Big- or little-endian byte order.
  90. * Use the BYTE_ORDER_ constants defined in {@link Zend_Pdf_FileParser}. If
  91. * omitted, uses big-endian.
  92. * @param string $characterSet (optional) --Ignored--
  93. * @return string
  94. * @throws Zend_Pdf_Exception
  95. */
  96. public function readStringUTF16($byteCount,
  97. $byteOrder = Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN,
  98. $characterSet = '')
  99. {
  100. return parent::readStringUTF16($byteCount, $byteOrder, 'UTF-16BE');
  101. }
  102. /**
  103. * Reads the Mac Roman-encoded string from the binary file at the current
  104. * offset location. Overridden to fix return character set at UTF-16BE.
  105. *
  106. * @param integer $byteCount Number of bytes (characters) to return.
  107. * @param string $characterSet (optional) --Ignored--
  108. * @return string
  109. * @throws Zend_Pdf_Exception
  110. */
  111. public function readStringMacRoman($byteCount, $characterSet = '')
  112. {
  113. return parent::readStringMacRoman($byteCount, 'UTF-16BE');
  114. }
  115. /**
  116. * Reads the Pascal string from the binary file at the current offset
  117. * location. Overridden to fix return character set at UTF-16BE.
  118. *
  119. * @param string $characterSet (optional) --Ignored--
  120. * @param integer $lengthBytes (optional) Number of bytes that make up the
  121. * length. Default is 1.
  122. * @return string
  123. * @throws Zend_Pdf_Exception
  124. */
  125. public function readStringPascal($characterSet = '', $lengthBytes = 1)
  126. {
  127. return parent::readStringPascal('UTF-16BE');
  128. }
  129. /* Utility Methods */
  130. /**
  131. * Writes the entire font properties array to STDOUT. Used only for debugging.
  132. */
  133. public function writeDebug()
  134. {
  135. print_r($this->_fontProperties);
  136. }
  137. /**** Internal Methods ****/
  138. /* Internal Accessors */
  139. /**
  140. * Set handler
  141. *
  142. * NOTE: This method is protected. Other classes may freely interrogate
  143. * the font properties, but only this and its subclasses may set them.
  144. *
  145. * @param string $property
  146. * @param mixed $value
  147. */
  148. public function __set($property, $value)
  149. {
  150. if ($value === null) {
  151. unset($this->_fontProperties[$property]);
  152. } else {
  153. $this->_fontProperties[$property] = $value;
  154. }
  155. }
  156. /* Internal Utility Methods */
  157. /**
  158. * If debug logging is enabled, writes the log message.
  159. *
  160. * The log message is a sprintf() style string and any number of arguments
  161. * may accompany it as additional parameters.
  162. *
  163. * @param string $message
  164. * @param mixed (optional, multiple) Additional arguments
  165. */
  166. protected function _debugLog($message)
  167. {
  168. if (! $this->_debug) {
  169. return;
  170. }
  171. if (func_num_args() > 1) {
  172. $args = func_get_args();
  173. $message = array_shift($args);
  174. $message = vsprintf($message, $args);
  175. }
  176. $logger = new Zend_Log();
  177. $logger->log($message, Zend_Log::DEBUG);
  178. }
  179. }