/library/Zend/Pdf/BinaryParser/Font/AbstractFont.php

https://github.com/JojoBombardo/zf2 · PHP · 218 lines · 61 code · 33 blank · 124 comment · 6 complexity · e3a4cbb0b0362c1a89171ee6a0de6741 MD5 · raw file

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