Resource.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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-2010 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. /**
  22. * PDF file Resource abstraction
  23. *
  24. * @package Zend_Pdf
  25. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. abstract class Zend_Pdf_Resource
  29. {
  30. /**
  31. * Each Pdf resource (fonts, images, ...) interacts with a PDF itself.
  32. * It creates appropriate PDF objects, structures and sometime embedded files.
  33. * Resources are referenced in content streams by names, which are stored in
  34. * a page resource dictionaries.
  35. *
  36. * Thus, resources must be attached to the PDF.
  37. *
  38. * Resource abstraction uses own PDF object factory to store all necessary information.
  39. * At the render time internal object factory is appended to the global PDF file
  40. * factory.
  41. *
  42. * Resource abstraction also cashes information about rendered PDF files and
  43. * doesn't duplicate resource description each time then Resource is rendered
  44. * (referenced).
  45. *
  46. * @var Zend_Pdf_ElementFactory_Interface
  47. */
  48. protected $_objectFactory;
  49. /**
  50. * Main resource object
  51. *
  52. * @var Zend_Pdf_Element_Object
  53. */
  54. protected $_resource;
  55. /**
  56. * Object constructor.
  57. *
  58. * If resource is not a Zend_Pdf_Element object, then stream object with specified value is
  59. * generated.
  60. *
  61. * @param Zend_Pdf_Element|string $resource
  62. */
  63. public function __construct($resource)
  64. {
  65. require_once 'Zend/Pdf/ElementFactory.php';
  66. $this->_objectFactory = Zend_Pdf_ElementFactory::createFactory(1);
  67. if ($resource instanceof Zend_Pdf_Element) {
  68. $this->_resource = $this->_objectFactory->newObject($resource);
  69. } else {
  70. $this->_resource = $this->_objectFactory->newStreamObject($resource);
  71. }
  72. }
  73. /**
  74. * Clone page, extract it and dependent objects from the current document,
  75. * so it can be used within other docs.
  76. */
  77. public function __clone()
  78. {
  79. $factory = Zend_Pdf_ElementFactory::createFactory(1);
  80. $processed = array();
  81. // Clone dictionary object.
  82. // Do it explicitly to prevent sharing page attributes between different
  83. // results of clonePage() operation (other resources are still shared)
  84. $dictionary = new Zend_Pdf_Element_Dictionary();
  85. foreach ($this->_pageDictionary->getKeys() as $key) {
  86. $dictionary->$key = $this->_pageDictionary->$key->makeClone($factory->getFactory(),
  87. $processed,
  88. Zend_Pdf_Element::CLONE_MODE_SKIP_PAGES);
  89. }
  90. $this->_pageDictionary = $factory->newObject($dictionary);
  91. $this->_objFactory = $factory;
  92. $this->_attached = false;
  93. $this->_style = null;
  94. $this->_font = null;
  95. }
  96. /**
  97. * Clone page, extract it and dependent objects from the current document,
  98. * so it can be used within other docs.
  99. *
  100. * @internal
  101. * @param Zend_Pdf_ElementFactory_Interface $factory
  102. * @param array $processed
  103. * @return Zend_Pdf_Page
  104. */
  105. public function clonePage($factory, &$processed)
  106. {
  107. // Clone dictionary object.
  108. // Do it explicitly to prevent sharing page attributes between different
  109. // results of clonePage() operation (other resources are still shared)
  110. $dictionary = new Zend_Pdf_Element_Dictionary();
  111. foreach ($this->_pageDictionary->getKeys() as $key) {
  112. $dictionary->$key = $this->_pageDictionary->$key->makeClone($factory->getFactory(),
  113. $processed,
  114. Zend_Pdf_Element::CLONE_MODE_SKIP_PAGES);
  115. }
  116. $clonedPage = new Zend_Pdf_Page($factory->newObject($dictionary), $factory);
  117. $clonedPage->_attached = false;
  118. return $clonedPage;
  119. }
  120. /**
  121. * Get resource.
  122. * Used to reference resource in an internal PDF data structures (resource dictionaries)
  123. *
  124. * @internal
  125. * @return Zend_Pdf_Element_Object
  126. */
  127. public function getResource()
  128. {
  129. return $this->_resource;
  130. }
  131. /**
  132. * Get factory.
  133. *
  134. * @internal
  135. * @return Zend_Pdf_ElementFactory_Interface
  136. */
  137. public function getFactory()
  138. {
  139. return $this->_objectFactory;
  140. }
  141. }