PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/xampp/htdocs/magento/lib/Zend/Pdf/Element/Object/Stream.php

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