Resource.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if ($resource instanceof Zend_Pdf_Element_Object) {
  66. $this->_objectFactory = $resource->getFactory();
  67. $this->_resource = $resource;
  68. return;
  69. }
  70. require_once 'Zend/Pdf/ElementFactory.php';
  71. $this->_objectFactory = Zend_Pdf_ElementFactory::createFactory(1);
  72. if ($resource instanceof Zend_Pdf_Element) {
  73. $this->_resource = $this->_objectFactory->newObject($resource);
  74. } else {
  75. $this->_resource = $this->_objectFactory->newStreamObject($resource);
  76. }
  77. }
  78. /**
  79. * Clone page, extract it and dependent objects from the current document,
  80. * so it can be used within other docs.
  81. */
  82. public function __clone()
  83. {
  84. $factory = Zend_Pdf_ElementFactory::createFactory(1);
  85. $processed = array();
  86. // Clone dictionary object.
  87. // Do it explicitly to prevent sharing page attributes between different
  88. // results of clonePage() operation (other resources are still shared)
  89. $dictionary = new Zend_Pdf_Element_Dictionary();
  90. foreach ($this->_pageDictionary->getKeys() as $key) {
  91. $dictionary->$key = $this->_pageDictionary->$key->makeClone($factory->getFactory(),
  92. $processed,
  93. Zend_Pdf_Element::CLONE_MODE_SKIP_PAGES);
  94. }
  95. $this->_pageDictionary = $factory->newObject($dictionary);
  96. $this->_objectFactory = $factory;
  97. $this->_attached = false;
  98. $this->_style = null;
  99. $this->_font = null;
  100. }
  101. /**
  102. * Clone page, extract it and dependent objects from the current document,
  103. * so it can be used within other docs.
  104. *
  105. * @internal
  106. * @param Zend_Pdf_ElementFactory_Interface $factory
  107. * @param array $processed
  108. * @return Zend_Pdf_Page
  109. */
  110. public function clonePage($factory, &$processed)
  111. {
  112. // Clone dictionary object.
  113. // Do it explicitly to prevent sharing page attributes between different
  114. // results of clonePage() operation (other resources are still shared)
  115. $dictionary = new Zend_Pdf_Element_Dictionary();
  116. foreach ($this->_pageDictionary->getKeys() as $key) {
  117. $dictionary->$key = $this->_pageDictionary->$key->makeClone($factory->getFactory(),
  118. $processed,
  119. Zend_Pdf_Element::CLONE_MODE_SKIP_PAGES);
  120. }
  121. $clonedPage = new Zend_Pdf_Page($factory->newObject($dictionary), $factory);
  122. $clonedPage->_attached = false;
  123. return $clonedPage;
  124. }
  125. /**
  126. * Get resource.
  127. * Used to reference resource in an internal PDF data structures (resource dictionaries)
  128. *
  129. * @internal
  130. * @return Zend_Pdf_Element_Object
  131. */
  132. public function getResource()
  133. {
  134. return $this->_resource;
  135. }
  136. /**
  137. * Get factory.
  138. *
  139. * @internal
  140. * @return Zend_Pdf_ElementFactory_Interface
  141. */
  142. public function getFactory()
  143. {
  144. return $this->_objectFactory;
  145. }
  146. }