2
0

ElementFactory.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. /** Zend_Pdf_ElementFactory_Interface */
  20. require_once 'Zend/Pdf/ElementFactory/Interface.php';
  21. /** Zend_Pdf_ElementFactory_Proxy */
  22. require_once 'Zend/Pdf/ElementFactory/Proxy.php';
  23. /** Zend_Pdf_Element */
  24. require_once 'Zend/Pdf/Element.php';
  25. /** Zend_Pdf_Element_Array */
  26. require_once 'Zend/Pdf/Element/Array.php';
  27. /** Zend_Pdf_Element_String_Binary */
  28. require_once 'Zend/Pdf/Element/String/Binary.php';
  29. /** Zend_Pdf_Element_Boolean */
  30. require_once 'Zend/Pdf/Element/Boolean.php';
  31. /** Zend_Pdf_Element_Dictionary */
  32. require_once 'Zend/Pdf/Element/Dictionary.php';
  33. /** Zend_Pdf_Element_Name */
  34. require_once 'Zend/Pdf/Element/Name.php';
  35. /** Zend_Pdf_Element_Numeric */
  36. require_once 'Zend/Pdf/Element/Numeric.php';
  37. /** Zend_Pdf_Element_Object */
  38. require_once 'Zend/Pdf/Element/Object.php';
  39. /** Zend_Pdf_Element_Reference */
  40. require_once 'Zend/Pdf/Element/Reference.php';
  41. /** Zend_Pdf_Element_Object_Stream */
  42. require_once 'Zend/Pdf/Element/Object/Stream.php';
  43. /** Zend_Pdf_Element_String */
  44. require_once 'Zend/Pdf/Element/String.php';
  45. /** Zend_Pdf_Element_Null */
  46. require_once 'Zend/Pdf/Element/Null.php';
  47. /** Zend_Pdf_UpdateInfoContainer */
  48. require_once 'Zend/Pdf/UpdateInfoContainer.php';
  49. /**
  50. * PDF element factory.
  51. * Responsibility is to log PDF changes
  52. *
  53. * @package Zend_Pdf
  54. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  55. * @license http://framework.zend.com/license/new-bsd New BSD License
  56. */
  57. class Zend_Pdf_ElementFactory implements Zend_Pdf_ElementFactory_Interface
  58. {
  59. /**
  60. * List of the modified objects.
  61. * Also contains new and removed objects
  62. *
  63. * Array: ojbectNumber => Zend_Pdf_Element_Object
  64. *
  65. * @var array
  66. */
  67. private $_modifiedObjects = array();
  68. /**
  69. * List of the removed objects
  70. *
  71. * Array: ojbectNumber => Zend_Pdf_Element_Object
  72. *
  73. * @var array
  74. */
  75. private $_removedObjects = array();
  76. /**
  77. * List of registered objects.
  78. * Used for resources clean up when factory is destroyed.
  79. *
  80. * Array of Zend_Pdf_Element objects
  81. *
  82. * @var array
  83. */
  84. private $_registeredObjects = array();
  85. /**
  86. * PDF object counter.
  87. * Actually it's an object number for new PDF object
  88. *
  89. * @var integer
  90. */
  91. private $_objectCount;
  92. /**
  93. * List of the attached object factories.
  94. * Array of Zend_Pdf_ElementFactory_Interface objects
  95. *
  96. * @var array
  97. */
  98. private $_attachedFactories = array();
  99. /**
  100. * Factory internal id
  101. *
  102. * @var integer
  103. */
  104. private $_factoryId;
  105. /**
  106. * Identity, used for factory id generation
  107. *
  108. * @var integer
  109. */
  110. private static $_identity = 0;
  111. /**
  112. * Internal cache to save calculated shifts
  113. *
  114. * @var array
  115. */
  116. private $_shiftCalculationCache = array();
  117. /**
  118. * Object constructor
  119. *
  120. * @param integer $objCount
  121. */
  122. public function __construct($objCount)
  123. {
  124. $this->_objectCount = (int)$objCount;
  125. $this->_factoryId = self::$_identity++;
  126. }
  127. /**
  128. * Factory generator
  129. *
  130. * @param integer $objCount
  131. * @return Zend_Pdf_ElementFactory_Interface
  132. */
  133. static public function createFactory($objCount)
  134. {
  135. return new Zend_Pdf_ElementFactory_Proxy(new Zend_Pdf_ElementFactory($objCount));
  136. }
  137. /**
  138. * Close factory and clean-up resources
  139. *
  140. * @internal
  141. */
  142. public function close()
  143. {
  144. $this->_modifiedObjects = null;
  145. $this->_removedObjects = null;
  146. $this->_attachedFactories = null;
  147. foreach ($this->_registeredObjects as $obj) {
  148. $obj->cleanUp();
  149. }
  150. $this->_registeredObjects = null;
  151. }
  152. /**
  153. * Get source factory object
  154. *
  155. * @return Zend_Pdf_ElementFactory
  156. */
  157. public function resolve()
  158. {
  159. return $this;
  160. }
  161. /**
  162. * Get factory ID
  163. *
  164. * @return integer
  165. */
  166. public function getId()
  167. {
  168. return $this->_factoryId;
  169. }
  170. /**
  171. * Set object counter
  172. *
  173. * @param integer $objCount
  174. */
  175. public function setObjectCount($objCount)
  176. {
  177. $this->_objectCount = (int)$objCount;
  178. }
  179. /**
  180. * Get object counter
  181. *
  182. * @return integer
  183. */
  184. public function getObjectCount()
  185. {
  186. $count = $this->_objectCount;
  187. foreach ($this->_attachedFactories as $attached) {
  188. $count += $attached->getObjectCount() - 1; // -1 as "0" object is a special case and shared between factories
  189. }
  190. return $count;
  191. }
  192. /**
  193. * Attach factory to the current;
  194. *
  195. * @param Zend_Pdf_ElementFactory_Interface $factory
  196. */
  197. public function attach(Zend_Pdf_ElementFactory_Interface $factory)
  198. {
  199. if ( $factory === $this || isset($this->_attachedFactories[$factory->getId()])) {
  200. /**
  201. * Don't attach factory twice.
  202. * We do not check recusively because of nature of attach operation
  203. * (Pages are always attached to the Documents, Fonts are always attached
  204. * to the pages even if pages already use Document level object factory and so on)
  205. */
  206. return;
  207. }
  208. $this->_attachedFactories[$factory->getId()] = $factory;
  209. }
  210. /**
  211. * Calculate object enumeration shift.
  212. *
  213. * @internal
  214. * @param Zend_Pdf_ElementFactory_Interface $factory
  215. * @return integer
  216. */
  217. public function calculateShift(Zend_Pdf_ElementFactory_Interface $factory)
  218. {
  219. if ($factory === $this) {
  220. return 0;
  221. }
  222. if (isset($this->_shiftCalculationCache[$factory->_factoryId])) {
  223. return $this->_shiftCalculationCache[$factory->_factoryId];
  224. }
  225. $shift = $this->_objectCount - 1;
  226. foreach ($this->_attachedFactories as $subFactory) {
  227. $subFactoryShift = $subFactory->calculateShift($factory);
  228. if ($subFactoryShift != -1) {
  229. // context found
  230. $this->_shiftCalculationCache[$factory->_factoryId] = $shift + $subFactoryShift;
  231. return $shift + $subFactoryShift;
  232. } else {
  233. $shift += $subFactory->getObjectCount()-1;
  234. }
  235. }
  236. $this->_shiftCalculationCache[$factory->_factoryId] = -1;
  237. return -1;
  238. }
  239. /**
  240. * Retrive object enumeration shift.
  241. *
  242. * @param Zend_Pdf_ElementFactory_Interface $factory
  243. * @return integer
  244. * @throws Zend_Pdf_Exception
  245. */
  246. public function getEnumerationShift(Zend_Pdf_ElementFactory_Interface $factory)
  247. {
  248. if (($shift = $this->calculateShift($factory)) == -1) {
  249. throw new Zend_Pdf_Exception('Wrong object context');
  250. }
  251. return $shift;
  252. }
  253. /**
  254. * Mark object as modified in context of current factory.
  255. *
  256. * @param Zend_Pdf_Element_Object $obj
  257. * @throws Zend_Pdf_Exception
  258. */
  259. public function markAsModified(Zend_Pdf_Element_Object $obj)
  260. {
  261. if ($obj->getFactory() !== $this) {
  262. throw new Zend_Pdf_Exception('Object is not generated by this factory');
  263. }
  264. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  265. }
  266. /**
  267. * Remove object in context of current factory.
  268. *
  269. * @param Zend_Pdf_Element_Object $obj
  270. * @throws Zend_Pdf_Exception
  271. */
  272. public function remove(Zend_Pdf_Element_Object $obj)
  273. {
  274. if (!$obj->compareFactory($this)) {
  275. throw new Zend_Pdf_Exception('Object is not generated by this factory');
  276. }
  277. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  278. $this-> _removedObjects[$obj->getObjNum()] = $obj;
  279. }
  280. /**
  281. * Generate new Zend_Pdf_Element_Object
  282. *
  283. * @todo Reusage of the freed object. It's not a support of new feature, but only improvement.
  284. *
  285. * @param Zend_Pdf_Element $objectValue
  286. * @return Zend_Pdf_Element_Object
  287. */
  288. public function newObject(Zend_Pdf_Element $objectValue)
  289. {
  290. $obj = new Zend_Pdf_Element_Object($objectValue, $this->_objectCount++, 0, $this);
  291. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  292. return $obj;
  293. }
  294. /**
  295. * Generate new Zend_Pdf_Element_Object_Stream
  296. *
  297. * @todo Reusage of the freed object. It's not a support of new feature, but only improvement.
  298. *
  299. * @param mixed $objectValue
  300. * @return Zend_Pdf_Element_Object_Stream
  301. */
  302. public function newStreamObject($streamValue)
  303. {
  304. $obj = new Zend_Pdf_Element_Object_Stream($streamValue, $this->_objectCount++, 0, $this);
  305. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  306. return $obj;
  307. }
  308. /**
  309. * Enumerate modified objects.
  310. * Returns array of Zend_Pdf_UpdateInfoContainer
  311. *
  312. * @param Zend_Pdf_ElementFactory_Interface $rootFactory
  313. * @return array
  314. */
  315. public function listModifiedObjects($rootFactory = null)
  316. {
  317. if ($rootFactory == null) {
  318. $rootFactory = $this;
  319. $shift = 0;
  320. } else {
  321. $shift = $rootFactory->getEnumerationShift($this);
  322. }
  323. ksort($this->_modifiedObjects);
  324. $result = array();
  325. foreach ($this->_modifiedObjects as $objNum => $obj) {
  326. if (key_exists($objNum, $this->_removedObjects)) {
  327. $result[$objNum+$shift] = new Zend_Pdf_UpdateInfoContainer($objNum + $shift,
  328. $obj->getGenNum()+1,
  329. true);
  330. } else {
  331. $result[$objNum+$shift] = new Zend_Pdf_UpdateInfoContainer($objNum + $shift,
  332. $obj->getGenNum(),
  333. false,
  334. $obj->dump($rootFactory));
  335. }
  336. }
  337. foreach ($this->_attachedFactories as $factory) {
  338. $result += $factory->listModifiedObjects($rootFactory);
  339. }
  340. return $result;
  341. }
  342. /**
  343. * Register object in the factory
  344. *
  345. * It's used to clear "parent object" referencies when factory is closed and clean up resources
  346. *
  347. * @param Zend_Pdf_Element_Object $obj
  348. */
  349. public function registerObject($obj)
  350. {
  351. $this->_registeredObjects[] = $obj;
  352. }
  353. /**
  354. * Check if PDF file was modified
  355. *
  356. * @return boolean
  357. */
  358. public function isModified()
  359. {
  360. if (count($this->_modifiedObjects) != 0) {
  361. return true;
  362. }
  363. foreach ($this->_attachedFactories as $subFactory) {
  364. if ($subFactory->isModified()) {
  365. return true;
  366. }
  367. }
  368. return false;
  369. }
  370. }