PageRenderTime 36ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/framework/vendor/zend/Zend/Pdf/Element/Object/Stream.php

http://zoop.googlecode.com/
PHP | 422 lines | 242 code | 56 blank | 124 comment | 42 complexity | e8c099ebc2a49a552c9d5bdd9f6102f5 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Stream.php 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /** Internally used classes */
  22. require_once 'Zend/Pdf/Element/Stream.php';
  23. require_once 'Zend/Pdf/Element/Dictionary.php';
  24. require_once 'Zend/Pdf/Element/Numeric.php';
  25. /** Zend_Pdf_Element_Object */
  26. require_once 'Zend/Pdf/Element/Object.php';
  27. /**
  28. * PDF file 'stream object' element implementation
  29. *
  30. * @category Zend
  31. * @package Zend_Pdf
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Pdf_Element_Object_Stream extends Zend_Pdf_Element_Object
  36. {
  37. /**
  38. * StreamObject dictionary
  39. * Required enries:
  40. * Length
  41. *
  42. * @var Zend_Pdf_Element_Dictionary
  43. */
  44. private $_dictionary;
  45. /**
  46. * Flag which signals, that stream is decoded
  47. *
  48. * @var boolean
  49. */
  50. private $_streamDecoded;
  51. /**
  52. * Stored original stream object dictionary.
  53. * Used to decode stream during an access time.
  54. *
  55. * The only properties, which affect decoding, are sored here.
  56. *
  57. * @var array|null
  58. */
  59. private $_originalDictionary = null;
  60. /**
  61. * Object constructor
  62. *
  63. * @param mixed $val
  64. * @param integer $objNum
  65. * @param integer $genNum
  66. * @param Zend_Pdf_ElementFactory $factory
  67. * @param Zend_Pdf_Element_Dictionary|null $dictionary
  68. * @throws Zend_Pdf_Exception
  69. */
  70. public function __construct($val, $objNum, $genNum, Zend_Pdf_ElementFactory $factory, $dictionary = null)
  71. {
  72. parent::__construct(new Zend_Pdf_Element_Stream($val), $objNum, $genNum, $factory);
  73. if ($dictionary === null) {
  74. $this->_dictionary = new Zend_Pdf_Element_Dictionary();
  75. $this->_dictionary->Length = new Zend_Pdf_Element_Numeric(strlen( $val ));
  76. $this->_streamDecoded = true;
  77. } else {
  78. $this->_dictionary = $dictionary;
  79. $this->_streamDecoded = false;
  80. }
  81. }
  82. /**
  83. * Store original dictionary information in $_originalDictionary class member.
  84. * Used to store information and to normalize filters information before defiltering.
  85. *
  86. */
  87. private function _storeOriginalDictionary()
  88. {
  89. $this->_originalDictionary = array();
  90. $this->_originalDictionary['Filter'] = array();
  91. $this->_originalDictionary['DecodeParms'] = array();
  92. if ($this->_dictionary->Filter === null) {
  93. // Do nothing.
  94. } else if ($this->_dictionary->Filter->getType() == Zend_Pdf_Element::TYPE_ARRAY) {
  95. foreach ($this->_dictionary->Filter->items as $id => $filter) {
  96. $this->_originalDictionary['Filter'][$id] = $filter->value;
  97. $this->_originalDictionary['DecodeParms'][$id] = array();
  98. if ($this->_dictionary->DecodeParms !== null ) {
  99. if ($this->_dictionary->DecodeParms->items[$id] !== null &&
  100. $this->_dictionary->DecodeParms->items[$id]->value !== null ) {
  101. foreach ($this->_dictionary->DecodeParms->items[$id]->getKeys() as $paramKey) {
  102. $this->_originalDictionary['DecodeParms'][$id][$paramKey] =
  103. $this->_dictionary->DecodeParms->items[$id]->$paramKey->value;
  104. }
  105. }
  106. }
  107. }
  108. } else if ($this->_dictionary->Filter->getType() != Zend_Pdf_Element::TYPE_NULL) {
  109. $this->_originalDictionary['Filter'][0] = $this->_dictionary->Filter->value;
  110. $this->_originalDictionary['DecodeParms'][0] = array();
  111. if ($this->_dictionary->DecodeParms !== null ) {
  112. foreach ($this->_dictionary->DecodeParms->getKeys() as $paramKey) {
  113. $this->_originalDictionary['DecodeParms'][0][$paramKey] =
  114. $this->_dictionary->DecodeParms->$paramKey->value;
  115. }
  116. }
  117. }
  118. if ($this->_dictionary->F !== null) {
  119. $this->_originalDictionary['F'] = $this->_dictionary->F->value;
  120. }
  121. $this->_originalDictionary['FFilter'] = array();
  122. $this->_originalDictionary['FDecodeParms'] = array();
  123. if ($this->_dictionary->FFilter === null) {
  124. // Do nothing.
  125. } else if ($this->_dictionary->FFilter->getType() == Zend_Pdf_Element::TYPE_ARRAY) {
  126. foreach ($this->_dictionary->FFilter->items as $id => $filter) {
  127. $this->_originalDictionary['FFilter'][$id] = $filter->value;
  128. $this->_originalDictionary['FDecodeParms'][$id] = array();
  129. if ($this->_dictionary->FDecodeParms !== null ) {
  130. if ($this->_dictionary->FDecodeParms->items[$id] !== null &&
  131. $this->_dictionary->FDecodeParms->items[$id]->value !== null) {
  132. foreach ($this->_dictionary->FDecodeParms->items[$id]->getKeys() as $paramKey) {
  133. $this->_originalDictionary['FDecodeParms'][$id][$paramKey] =
  134. $this->_dictionary->FDecodeParms->items[$id]->items[$paramKey]->value;
  135. }
  136. }
  137. }
  138. }
  139. } else {
  140. $this->_originalDictionary['FFilter'][0] = $this->_dictionary->FFilter->value;
  141. $this->_originalDictionary['FDecodeParms'][0] = array();
  142. if ($this->_dictionary->FDecodeParms !== null ) {
  143. foreach ($this->_dictionary->FDecodeParms->getKeys() as $paramKey) {
  144. $this->_originalDictionary['FDecodeParms'][0][$paramKey] =
  145. $this->_dictionary->FDecodeParms->items[$paramKey]->value;
  146. }
  147. }
  148. }
  149. }
  150. /**
  151. * Decode stream
  152. *
  153. * @throws Zend_Pdf_Exception
  154. */
  155. private function _decodeStream()
  156. {
  157. if ($this->_originalDictionary === null) {
  158. $this->_storeOriginalDictionary();
  159. }
  160. /**
  161. * All applied stream filters must be processed to decode stream.
  162. * If we don't recognize any of applied filetrs an exception should be thrown here
  163. */
  164. if (isset($this->_originalDictionary['F'])) {
  165. /** @todo Check, how external files can be processed. */
  166. require_once 'Zend/Pdf/Exception.php';
  167. throw new Zend_Pdf_Exception('External filters are not supported now.');
  168. }
  169. foreach ($this->_originalDictionary['Filter'] as $id => $filterName ) {
  170. $valueRef = &$this->_value->value->getRef();
  171. $this->_value->value->touch();
  172. switch ($filterName) {
  173. case 'ASCIIHexDecode':
  174. require_once 'Zend/Pdf/Filter/AsciiHex.php';
  175. $valueRef = Zend_Pdf_Filter_AsciiHex::decode($valueRef);
  176. break;
  177. case 'ASCII85Decode':
  178. require_once 'Zend/Pdf/Filter/Ascii85.php';
  179. $valueRef = Zend_Pdf_Filter_Ascii85::decode($valueRef);
  180. break;
  181. case 'FlateDecode':
  182. require_once 'Zend/Pdf/Filter/Compression/Flate.php';
  183. $valueRef = Zend_Pdf_Filter_Compression_Flate::decode($valueRef,
  184. $this->_originalDictionary['DecodeParms'][$id]);
  185. break;
  186. case 'LZWDecode':
  187. require_once 'Zend/Pdf/Filter/Compression/Lzw.php';
  188. $valueRef = Zend_Pdf_Filter_Compression_Lzw::decode($valueRef,
  189. $this->_originalDictionary['DecodeParms'][$id]);
  190. break;
  191. case 'RunLengthDecode':
  192. require_once 'Zend/Pdf/Filter/RunLength.php';
  193. $valueRef = Zend_Pdf_Filter_RunLength::decode($valueRef);
  194. break;
  195. default:
  196. require_once 'Zend/Pdf/Exception.php';
  197. throw new Zend_Pdf_Exception('Unknown stream filter: \'' . $filterName . '\'.');
  198. }
  199. }
  200. $this->_streamDecoded = true;
  201. }
  202. /**
  203. * Encode stream
  204. *
  205. * @throws Zend_Pdf_Exception
  206. */
  207. private function _encodeStream()
  208. {
  209. /**
  210. * All applied stream filters must be processed to encode stream.
  211. * If we don't recognize any of applied filetrs an exception should be thrown here
  212. */
  213. if (isset($this->_originalDictionary['F'])) {
  214. /** @todo Check, how external files can be processed. */
  215. require_once 'Zend/Pdf/Exception.php';
  216. throw new Zend_Pdf_Exception('External filters are not supported now.');
  217. }
  218. $filters = array_reverse($this->_originalDictionary['Filter'], true);
  219. foreach ($filters as $id => $filterName ) {
  220. $valueRef = &$this->_value->value->getRef();
  221. $this->_value->value->touch();
  222. switch ($filterName) {
  223. case 'ASCIIHexDecode':
  224. require_once 'Zend/Pdf/Filter/AsciiHex.php';
  225. $valueRef = Zend_Pdf_Filter_AsciiHex::encode($valueRef);
  226. break;
  227. case 'ASCII85Decode':
  228. require_once 'Zend/Pdf/Filter/Ascii85.php';
  229. $valueRef = Zend_Pdf_Filter_Ascii85::encode($valueRef);
  230. break;
  231. case 'FlateDecode':
  232. require_once 'Zend/Pdf/Filter/Compression/Flate.php';
  233. $valueRef = Zend_Pdf_Filter_Compression_Flate::encode($valueRef,
  234. $this->_originalDictionary['DecodeParms'][$id]);
  235. break;
  236. case 'LZWDecode':
  237. require_once 'Zend/Pdf/Filter/Compression/Lzw.php';
  238. $valueRef = Zend_Pdf_Filter_Compression_Lzw::encode($valueRef,
  239. $this->_originalDictionary['DecodeParms'][$id]);
  240. break;
  241. case 'RunLengthDecode':
  242. require_once 'Zend/Pdf/Filter/RunLength.php';
  243. $valueRef = Zend_Pdf_Filter_RunLength::encode($valueRef);
  244. break;
  245. default:
  246. require_once 'Zend/Pdf/Exception.php';
  247. throw new Zend_Pdf_Exception('Unknown stream filter: \'' . $filterName . '\'.');
  248. }
  249. }
  250. $this->_streamDecoded = false;
  251. }
  252. /**
  253. * Get handler
  254. *
  255. * @param string $property
  256. * @return mixed
  257. * @throws Zend_Pdf_Exception
  258. */
  259. public function __get($property)
  260. {
  261. if ($property == 'dictionary') {
  262. /**
  263. * If stream is note decoded yet, then store original decoding options (do it only once).
  264. */
  265. if (( !$this->_streamDecoded ) && ($this->_originalDictionary === null)) {
  266. $this->_storeOriginalDictionary();
  267. }
  268. return $this->_dictionary;
  269. }
  270. if ($property == 'value') {
  271. if (!$this->_streamDecoded) {
  272. $this->_decodeStream();
  273. }
  274. return $this->_value->value->getRef();
  275. }
  276. require_once 'Zend/Pdf/Exception.php';
  277. throw new Zend_Pdf_Exception('Unknown stream object property requested.');
  278. }
  279. /**
  280. * Set handler
  281. *
  282. * @param string $property
  283. * @param mixed $value
  284. */
  285. public function __set($property, $value)
  286. {
  287. if ($property == 'value') {
  288. $valueRef = &$this->_value->value->getRef();
  289. $valueRef = $value;
  290. $this->_value->value->touch();
  291. $this->_streamDecoded = true;
  292. return;
  293. }
  294. require_once 'Zend/Pdf/Exception.php';
  295. throw new Zend_Pdf_Exception('Unknown stream object property: \'' . $property . '\'.');
  296. }
  297. /**
  298. * Treat stream data as already encoded
  299. */
  300. public function skipFilters()
  301. {
  302. $this->_streamDecoded = false;
  303. }
  304. /**
  305. * Call handler
  306. *
  307. * @param string $method
  308. * @param array $args
  309. * @return mixed
  310. */
  311. public function __call($method, $args)
  312. {
  313. if (!$this->_streamDecoded) {
  314. $this->_decodeStream();
  315. }
  316. switch (count($args)) {
  317. case 0:
  318. return $this->_value->$method();
  319. case 1:
  320. return $this->_value->$method($args[0]);
  321. default:
  322. require_once 'Zend/Pdf/Exception.php';
  323. throw new Zend_Pdf_Exception('Unsupported number of arguments');
  324. }
  325. }
  326. /**
  327. * Dump object to a string to save within PDF file
  328. *
  329. * $factory parameter defines operation context.
  330. *
  331. * @param Zend_Pdf_ElementFactory $factory
  332. * @return string
  333. */
  334. public function dump(Zend_Pdf_ElementFactory $factory)
  335. {
  336. $shift = $factory->getEnumerationShift($this->_factory);
  337. if ($this->_streamDecoded) {
  338. $this->_storeOriginalDictionary();
  339. $this->_encodeStream();
  340. } else if ($this->_originalDictionary != null) {
  341. $startDictionary = $this->_originalDictionary;
  342. $this->_storeOriginalDictionary();
  343. $newDictionary = $this->_originalDictionary;
  344. if ($startDictionary !== $newDictionary) {
  345. $this->_originalDictionary = $startDictionary;
  346. $this->_decodeStream();
  347. $this->_originalDictionary = $newDictionary;
  348. $this->_encodeStream();
  349. }
  350. }
  351. // Update stream length
  352. $this->dictionary->Length->value = $this->_value->length();
  353. return $this->_objNum + $shift . " " . $this->_genNum . " obj \n"
  354. . $this->dictionary->toString($factory) . "\n"
  355. . $this->_value->toString($factory) . "\n"
  356. . "endobj\n";
  357. }
  358. /**
  359. * Clean up resources, used by object
  360. */
  361. public function cleanUp()
  362. {
  363. $this->_dictionary = null;
  364. $this->_value = null;
  365. }
  366. }