PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/zend/Zend/Pdf/Outline.php

http://zoop.googlecode.com/
PHP | 373 lines | 129 code | 44 blank | 200 comment | 9 complexity | 9be559c8a996678a2d6d05910111932d MD5 | raw file
  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 * @subpackage Actions
 18 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Outline.php 20096 2010-01-06 02:05:09Z bkarwin $
 21 */
 22
 23
 24/**
 25 * Abstract PDF outline representation class
 26 *
 27 * @todo Implement an ability to associate an outline item with a structure element (PDF 1.3 feature)
 28 *
 29 * @package    Zend_Pdf
 30 * @subpackage Outlines
 31 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 32 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 33 */
 34abstract class Zend_Pdf_Outline implements RecursiveIterator, Countable
 35{
 36    /**
 37     * True if outline is open.
 38     *
 39     * @var boolean
 40     */
 41    protected $_open = false;
 42
 43    /**
 44     * Array of child outlines (array of Zend_Pdf_Outline objects)
 45     *
 46     * @var array
 47     */
 48    public $childOutlines = array();
 49
 50
 51    /**
 52     * Get outline title.
 53     *
 54     * @return string
 55     */
 56    abstract public function getTitle();
 57
 58    /**
 59     * Set outline title
 60     *
 61     * @param string $title
 62     * @return Zend_Pdf_Outline
 63     */
 64    abstract public function setTitle($title);
 65
 66    /**
 67     * Returns true if outline item is open by default
 68     *
 69     * @return boolean
 70     */
 71    public function isOpen()
 72    {
 73        return $this->_open;
 74    }
 75
 76    /**
 77     * Sets 'isOpen' outline flag
 78     *
 79     * @param boolean $isOpen
 80     * @return Zend_Pdf_Outline
 81     */
 82    public function setIsOpen($isOpen)
 83    {
 84        $this->_open = $isOpen;
 85        return $this;
 86    }
 87
 88    /**
 89     * Returns true if outline item is displayed in italic
 90     *
 91     * @return boolean
 92     */
 93    abstract public function isItalic();
 94
 95    /**
 96     * Sets 'isItalic' outline flag
 97     *
 98     * @param boolean $isItalic
 99     * @return Zend_Pdf_Outline
100     */
101    abstract public function setIsItalic($isItalic);
102
103    /**
104     * Returns true if outline item is displayed in bold
105     *
106     * @return boolean
107     */
108    abstract public function isBold();
109
110    /**
111     * Sets 'isBold' outline flag
112     *
113     * @param boolean $isBold
114     * @return Zend_Pdf_Outline
115     */
116    abstract public function setIsBold($isBold);
117
118
119    /**
120     * Get outline text color.
121     *
122     * @return Zend_Pdf_Color_Rgb
123     */
124    abstract public function getColor();
125
126    /**
127     * Set outline text color.
128     * (null means default color which is black)
129     *
130     * @param Zend_Pdf_Color_Rgb $color
131     * @return Zend_Pdf_Outline
132     */
133    abstract public function setColor(Zend_Pdf_Color_Rgb $color);
134
135    /**
136     * Get outline target.
137     *
138     * @return Zend_Pdf_Target
139     */
140    abstract public function getTarget();
141
142    /**
143     * Set outline target.
144     * Null means no target
145     *
146     * @param Zend_Pdf_Target|string $target
147     * @return Zend_Pdf_Outline
148     */
149    abstract public function setTarget($target = null);
150
151    /**
152     * Get outline options
153     *
154     * @return array
155     */
156    public function getOptions()
157    {
158        return array('title'  => $this->_title,
159                     'open'   => $this->_open,
160                     'color'  => $this->_color,
161                     'italic' => $this->_italic,
162                     'bold'   => $this->_bold,
163                     'target' => $this->_target);
164    }
165
166    /**
167     * Set outline options
168     *
169     * @param array $options
170     * @return Zend_Pdf_Action
171     * @throws Zend_Pdf_Exception
172     */
173    public function setOptions(array $options)
174    {
175        foreach ($options as $key => $value) {
176            switch ($key) {
177                case 'title':
178                    $this->setTitle($value);
179                    break;
180
181                case 'open':
182                    $this->setIsOpen($value);
183                    break;
184
185                case 'color':
186                    $this->setColor($value);
187                    break;
188                case 'italic':
189                    $this->setIsItalic($value);
190                    break;
191
192                case 'bold':
193                    $this->setIsBold($value);
194                    break;
195
196                case 'target':
197                    $this->setTarget($value);
198                    break;
199
200                default:
201                    require_once 'Zend/Pdf/Exception.php';
202                    throw new Zend_Pdf_Exception("Unknown option name - '$key'.");
203                    break;
204            }
205        }
206
207        return $this;
208    }
209
210    /**
211     * Create new Outline object
212     *
213     * It provides two forms of input parameters:
214     *
215     * 1. Zend_Pdf_Outline::create(string $title[, Zend_Pdf_Target $target])
216     * 2. Zend_Pdf_Outline::create(array $options)
217     *
218     * Second form allows to provide outline options as an array.
219     * The followed options are supported:
220     *   'title'  - string, outline title, required
221     *   'open'   - boolean, true if outline entry is open (default value is false)
222     *   'color'  - Zend_Pdf_Color_Rgb object, true if outline entry is open (default value is null - black)
223     *   'italic' - boolean, true if outline entry is displayed in italic (default value is false)
224     *   'bold'   - boolean, true if outline entry is displayed in bold (default value is false)
225     *   'target' - Zend_Pdf_Target object or string, outline item destination
226     *
227     * @return Zend_Pdf_Outline
228     * @throws Zend_Pdf_Exception
229     */
230    public static function create($param1, $param2 = null)
231    {
232        require_once 'Zend/Pdf/Outline/Created.php';
233        if (is_string($param1)) {
234            if ($param2 !== null  &&  !($param2 instanceof Zend_Pdf_Target  ||  is_string($param2))) {
235                require_once 'Zend/Pdf/Exception.php';
236                throw new Zend_Pdf_Exception('Outline create method takes $title (string) and $target (Zend_Pdf_Target or string) or an array as an input');
237            }
238
239            return new Zend_Pdf_Outline_Created(array('title'  => $param1,
240                                                      'target' => $param2));
241        } else {
242            if (!is_array($param1)  ||  $param2 !== null) {
243                require_once 'Zend/Pdf/Exception.php';
244                throw new Zend_Pdf_Exception('Outline create method takes $title (string) and $destination (Zend_Pdf_Destination) or an array as an input');
245            }
246
247            return new Zend_Pdf_Outline_Created($param1);
248        }
249    }
250
251    /**
252     * Returns number of the total number of open items at all levels of the outline.
253     *
254     * @internal
255     * @return integer
256     */
257    public function openOutlinesCount()
258    {
259        $count = 1; // Include this outline
260
261        if ($this->isOpen()) {
262            foreach ($this->childOutlines as $child) {
263                $count += $child->openOutlinesCount();
264            }
265        }
266
267        return $count;
268    }
269
270    /**
271     * Dump Outline and its child outlines into PDF structures
272     *
273     * Returns dictionary indirect object or reference
274     *
275     * @param Zend_Pdf_ElementFactory    $factory object factory for newly created indirect objects
276     * @param boolean $updateNavigation  Update navigation flag
277     * @param Zend_Pdf_Element $parent   Parent outline dictionary reference
278     * @param Zend_Pdf_Element $prev     Previous outline dictionary reference
279     * @param SplObjectStorage $processedOutlines  List of already processed outlines
280     * @return Zend_Pdf_Element
281     */
282    abstract public function dumpOutline(Zend_Pdf_ElementFactory_Interface $factory,
283                                                                           $updateNavigation,
284                                                          Zend_Pdf_Element $parent,
285                                                          Zend_Pdf_Element $prev = null,
286                                                          SplObjectStorage $processedOutlines = null);
287
288
289    ////////////////////////////////////////////////////////////////////////
290    //  RecursiveIterator interface methods
291    //////////////
292
293    /**
294     * Returns the child outline.
295     *
296     * @return Zend_Pdf_Outline
297     */
298    public function current()
299    {
300        return current($this->childOutlines);
301    }
302
303    /**
304     * Returns current iterator key
305     *
306     * @return integer
307     */
308    public function key()
309    {
310        return key($this->childOutlines);
311    }
312
313    /**
314     * Go to next child
315     */
316    public function next()
317    {
318        return next($this->childOutlines);
319    }
320
321    /**
322     * Rewind children
323     */
324    public function rewind()
325    {
326        return reset($this->childOutlines);
327    }
328
329    /**
330     * Check if current position is valid
331     *
332     * @return boolean
333     */
334    public function valid()
335    {
336        return current($this->childOutlines) !== false;
337    }
338
339    /**
340     * Returns the child outline.
341     *
342     * @return Zend_Pdf_Outline|null
343     */
344    public function getChildren()
345    {
346        return current($this->childOutlines);
347    }
348
349    /**
350     * Implements RecursiveIterator interface.
351     *
352     * @return bool  whether container has any pages
353     */
354    public function hasChildren()
355    {
356        return count($this->childOutlines) > 0;
357    }
358
359
360    ////////////////////////////////////////////////////////////////////////
361    //  Countable interface methods
362    //////////////
363
364    /**
365     * count()
366     *
367     * @return int
368     */
369    public function count()
370    {
371        return count($this->childOutlines);
372    }
373}