ElementFactory.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. * @internal
  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. * Retrive object enumeration shift.
  243. *
  244. * @param Zend_Pdf_ElementFactory_Interface $factory
  245. * @return integer
  246. * @throws Zend_Pdf_Exception
  247. */
  248. public function getEnumerationShift(Zend_Pdf_ElementFactory_Interface $factory)
  249. {
  250. if (($shift = $this->calculateShift($factory)) == -1) {
  251. throw new Zend_Pdf_Exception('Wrong object context');
  252. }
  253. return $shift;
  254. }
  255. /**
  256. * Mark object as modified in context of current factory.
  257. *
  258. * @param Zend_Pdf_Element_Object $obj
  259. * @throws Zend_Pdf_Exception
  260. */
  261. public function markAsModified(Zend_Pdf_Element_Object $obj)
  262. {
  263. if ($obj->getFactory() !== $this) {
  264. throw new Zend_Pdf_Exception('Object is not generated by this factory');
  265. }
  266. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  267. }
  268. /**
  269. * Remove object in context of current factory.
  270. *
  271. * @param Zend_Pdf_Element_Object $obj
  272. * @throws Zend_Pdf_Exception
  273. */
  274. public function remove(Zend_Pdf_Element_Object $obj)
  275. {
  276. if (!$obj->compareFactory($this)) {
  277. throw new Zend_Pdf_Exception('Object is not generated by this factory');
  278. }
  279. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  280. $this-> _removedObjects[$obj->getObjNum()] = $obj;
  281. }
  282. /**
  283. * Generate new Zend_Pdf_Element_Object
  284. *
  285. * @todo Reusage of the freed object. It's not a support of new feature, but only improvement.
  286. *
  287. * @param Zend_Pdf_Element $objectValue
  288. * @return Zend_Pdf_Element_Object
  289. */
  290. public function newObject(Zend_Pdf_Element $objectValue)
  291. {
  292. $obj = new Zend_Pdf_Element_Object($objectValue, $this->_objectCount++, 0, $this);
  293. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  294. return $obj;
  295. }
  296. /**
  297. * Generate new Zend_Pdf_Element_Object_Stream
  298. *
  299. * @todo Reusage of the freed object. It's not a support of new feature, but only improvement.
  300. *
  301. * @param mixed $objectValue
  302. * @return Zend_Pdf_Element_Object_Stream
  303. */
  304. public function newStreamObject($streamValue)
  305. {
  306. $obj = new Zend_Pdf_Element_Object_Stream($streamValue, $this->_objectCount++, 0, $this);
  307. $this->_modifiedObjects[$obj->getObjNum()] = $obj;
  308. return $obj;
  309. }
  310. /**
  311. * Enumerate modified objects.
  312. * Returns array of Zend_Pdf_UpdateInfoContainer
  313. *
  314. * @param Zend_Pdf_ElementFactory_Interface $rootFactory
  315. * @return array
  316. */
  317. public function listModifiedObjects($rootFactory = null)
  318. {
  319. if ($rootFactory == null) {
  320. $rootFactory = $this;
  321. $shift = 0;
  322. } else {
  323. $shift = $rootFactory->getEnumerationShift($this);
  324. }
  325. ksort($this->_modifiedObjects);
  326. $result = array();
  327. foreach ($this->_modifiedObjects as $objNum => $obj) {
  328. if (key_exists($objNum, $this->_removedObjects)) {
  329. $result[$objNum+$shift] = new Zend_Pdf_UpdateInfoContainer($objNum + $shift,
  330. $obj->getGenNum()+1,
  331. true);
  332. } else {
  333. $result[$objNum+$shift] = new Zend_Pdf_UpdateInfoContainer($objNum + $shift,
  334. $obj->getGenNum(),
  335. false,
  336. $obj->dump($rootFactory));
  337. }
  338. }
  339. foreach ($this->_attachedFactories as $factory) {
  340. $result += $factory->listModifiedObjects($rootFactory);
  341. }
  342. return $result;
  343. }
  344. /**
  345. * Register object in the factory
  346. *
  347. * It's used to clear "parent object" referencies when factory is closed and clean up resources
  348. *
  349. * @param string $refString
  350. * @param Zend_Pdf_Element_Object $obj
  351. */
  352. public function registerObject(Zend_Pdf_Element_Object $obj, $refString)
  353. {
  354. $this->_registeredObjects[$refString] = $obj;
  355. }
  356. /**
  357. * Fetch object specified by reference
  358. *
  359. * @param string $refString
  360. * @return Zend_Pdf_Element_Object|null
  361. */
  362. public function fetchObject($refString)
  363. {
  364. if (!isset($this->_registeredObjects[$refString])) {
  365. return null;
  366. }
  367. return $this->_registeredObjects[$refString];
  368. }
  369. /**
  370. * Check if PDF file was modified
  371. *
  372. * @return boolean
  373. */
  374. public function isModified()
  375. {
  376. if (count($this->_modifiedObjects) != 0) {
  377. return true;
  378. }
  379. foreach ($this->_attachedFactories as $subFactory) {
  380. if ($subFactory->isModified()) {
  381. return true;
  382. }
  383. }
  384. return false;
  385. }
  386. }