ElementFactory.php 12 KB

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