ElementFactory.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 SplObjectStorage
  76. */
  77. private $_removedObjects;
  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. $this->_removedObjects = new SplObjectStorage();
  129. }
  130. /**
  131. * Factory generator
  132. *
  133. * @param integer $objCount
  134. * @return Zend_Pdf_ElementFactory_Interface
  135. */
  136. static public function createFactory($objCount)
  137. {
  138. return new Zend_Pdf_ElementFactory_Proxy(new Zend_Pdf_ElementFactory($objCount));
  139. }
  140. /**
  141. * Close factory and clean-up resources
  142. *
  143. * @internal
  144. */
  145. public function close()
  146. {
  147. $this->_modifiedObjects = null;
  148. $this->_removedObjects = null;
  149. $this->_attachedFactories = null;
  150. foreach ($this->_registeredObjects as $obj) {
  151. $obj->cleanUp();
  152. }
  153. $this->_registeredObjects = null;
  154. }
  155. /**
  156. * Get source factory object
  157. *
  158. * @return Zend_Pdf_ElementFactory
  159. */
  160. public function resolve()
  161. {
  162. return $this;
  163. }
  164. /**
  165. * Get factory ID
  166. *
  167. * @return integer
  168. */
  169. public function getId()
  170. {
  171. return $this->_factoryId;
  172. }
  173. /**
  174. * Set object counter
  175. *
  176. * @param integer $objCount
  177. */
  178. public function setObjectCount($objCount)
  179. {
  180. $this->_objectCount = (int)$objCount;
  181. }
  182. /**
  183. * Get object counter
  184. *
  185. * @return integer
  186. */
  187. public function getObjectCount()
  188. {
  189. $count = $this->_objectCount;
  190. foreach ($this->_attachedFactories as $attached) {
  191. $count += $attached->getObjectCount() - 1; // -1 as "0" object is a special case and shared between factories
  192. }
  193. return $count;
  194. }
  195. /**
  196. * Attach factory to the current;
  197. *
  198. * @param Zend_Pdf_ElementFactory_Interface $factory
  199. */
  200. public function attach(Zend_Pdf_ElementFactory_Interface $factory)
  201. {
  202. if ( $factory === $this || isset($this->_attachedFactories[$factory->getId()])) {
  203. /**
  204. * Don't attach factory twice.
  205. * We do not check recusively because of nature of attach operation
  206. * (Pages are always attached to the Documents, Fonts are always attached
  207. * to the pages even if pages already use Document level object factory and so on)
  208. */
  209. return;
  210. }
  211. $this->_attachedFactories[$factory->getId()] = $factory;
  212. }
  213. /**
  214. * Calculate object enumeration shift.
  215. *
  216. * @param Zend_Pdf_ElementFactory_Interface $factory
  217. * @return integer
  218. */
  219. public function calculateShift(Zend_Pdf_ElementFactory_Interface $factory)
  220. {
  221. if ($factory === $this) {
  222. return 0;
  223. }
  224. if (isset($this->_shiftCalculationCache[$factory->_factoryId])) {
  225. return $this->_shiftCalculationCache[$factory->_factoryId];
  226. }
  227. $shift = $this->_objectCount - 1;
  228. foreach ($this->_attachedFactories as $subFactory) {
  229. $subFactoryShift = $subFactory->calculateShift($factory);
  230. if ($subFactoryShift != -1) {
  231. // context found
  232. $this->_shiftCalculationCache[$factory->_factoryId] = $shift + $subFactoryShift;
  233. return $shift + $subFactoryShift;
  234. } else {
  235. $shift += $subFactory->getObjectCount()-1;
  236. }
  237. }
  238. $this->_shiftCalculationCache[$factory->_factoryId] = -1;
  239. return -1;
  240. }
  241. /**
  242. * Clean enumeration shift cache.
  243. * Has to be used after PDF render operation to let followed updates be correct.
  244. *
  245. * @param Zend_Pdf_ElementFactory_Interface $factory
  246. * @return integer
  247. */
  248. public function cleanEnumerationShiftCache()
  249. {
  250. $this->_shiftCalculationCache = array();
  251. foreach ($this->_attachedFactories as $attached) {
  252. $attached->cleanEnumerationShiftCache();
  253. }
  254. }
  255. /**
  256. * Retrive object enumeration shift.
  257. *
  258. * @param Zend_Pdf_ElementFactory_Interface $factory
  259. * @return integer
  260. * @throws Zend_Pdf_Exception
  261. */
  262. public function getEnumerationShift(Zend_Pdf_ElementFactory_Interface $factory)
  263. {
  264. if (($shift = $this->calculateShift($factory)) == -1) {
  265. throw new Zend_Pdf_Exception('Wrong object context');
  266. }
  267. return $shift;
  268. }
  269. /**
  270. * Mark object as modified in context of current factory.
  271. *
  272. * @param Zend_Pdf_Element_Object $obj
  273. * @throws Zend_Pdf_Exception
  274. */
  275. public function markAsModified(Zend_Pdf_Element_Object $obj)
  276. {
  277. if ($obj->getFactory() !== $this) {
  278. throw new Zend_Pdf_Exception('Object is not generated by this factory');
  279. }
  280. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  281. }
  282. /**
  283. * Remove object in context of current factory.
  284. *
  285. * @param Zend_Pdf_Element_Object $obj
  286. * @throws Zend_Pdf_Exception
  287. */
  288. public function remove(Zend_Pdf_Element_Object $obj)
  289. {
  290. if (!$obj->compareFactory($this)) {
  291. throw new Zend_Pdf_Exception('Object is not generated by this factory');
  292. }
  293. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  294. $this->_removedObjects->attach($obj);
  295. }
  296. /**
  297. * Generate new Zend_Pdf_Element_Object
  298. *
  299. * @todo Reusage of the freed object. It's not a support of new feature, but only improvement.
  300. *
  301. * @param Zend_Pdf_Element $objectValue
  302. * @return Zend_Pdf_Element_Object
  303. */
  304. public function newObject(Zend_Pdf_Element $objectValue)
  305. {
  306. $obj = new Zend_Pdf_Element_Object($objectValue, $this->_objectCount++, 0, $this);
  307. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  308. return $obj;
  309. }
  310. /**
  311. * Generate new Zend_Pdf_Element_Object_Stream
  312. *
  313. * @todo Reusage of the freed object. It's not a support of new feature, but only improvement.
  314. *
  315. * @param mixed $objectValue
  316. * @return Zend_Pdf_Element_Object_Stream
  317. */
  318. public function newStreamObject($streamValue)
  319. {
  320. $obj = new Zend_Pdf_Element_Object_Stream($streamValue, $this->_objectCount++, 0, $this);
  321. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  322. return $obj;
  323. }
  324. /**
  325. * Enumerate modified objects.
  326. * Returns array of Zend_Pdf_UpdateInfoContainer
  327. *
  328. * @param Zend_Pdf_ElementFactory_Interface $rootFactory
  329. * @return array
  330. */
  331. public function listModifiedObjects($rootFactory = null)
  332. {
  333. if ($rootFactory == null) {
  334. $rootFactory = $this;
  335. $shift = 0;
  336. } else {
  337. $shift = $rootFactory->getEnumerationShift($this);
  338. }
  339. ksort($this->_modifiedObjects);
  340. $result = array();
  341. foreach ($this->_modifiedObjects as $objNum => $obj) {
  342. if ($this->_removedObjects->contains($obj)) {
  343. $result[$objNum+$shift] = new Zend_Pdf_UpdateInfoContainer($objNum + $shift,
  344. $obj->getGenNum()+1,
  345. true);
  346. } else {
  347. $result[$objNum+$shift] = new Zend_Pdf_UpdateInfoContainer($objNum + $shift,
  348. $obj->getGenNum(),
  349. false,
  350. $obj->dump($rootFactory));
  351. }
  352. }
  353. foreach ($this->_attachedFactories as $factory) {
  354. $result += $factory->listModifiedObjects($rootFactory);
  355. }
  356. return $result;
  357. }
  358. /**
  359. * Register object in the factory
  360. *
  361. * It's used to clear "parent object" referencies when factory is closed and clean up resources
  362. *
  363. * @param string $refString
  364. * @param Zend_Pdf_Element_Object $obj
  365. */
  366. public function registerObject(Zend_Pdf_Element_Object $obj, $refString)
  367. {
  368. $this->_registeredObjects[$refString] = $obj;
  369. }
  370. /**
  371. * Fetch object specified by reference
  372. *
  373. * @param string $refString
  374. * @return Zend_Pdf_Element_Object|null
  375. */
  376. public function fetchObject($refString)
  377. {
  378. if (!isset($this->_registeredObjects[$refString])) {
  379. return null;
  380. }
  381. return $this->_registeredObjects[$refString];
  382. }
  383. /**
  384. * Check if PDF file was modified
  385. *
  386. * @return boolean
  387. */
  388. public function isModified()
  389. {
  390. if (count($this->_modifiedObjects) != 0) {
  391. return true;
  392. }
  393. foreach ($this->_attachedFactories as $subFactory) {
  394. if ($subFactory->isModified()) {
  395. return true;
  396. }
  397. }
  398. return false;
  399. }
  400. }