1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5/**
6 * Image_Graph - PEAR PHP OO Graph Rendering Utility.
7 *
8 * PHP version 5
9 *
10 * LICENSE: This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version. This library is distributed in the hope that it
14 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
16 * General Public License for more details. You should have received a copy of
17 * the GNU Lesser General Public License along with this library; if not, write
18 * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 *
21 * @category   Images
22 * @package    Image_Graph
23 * @subpackage Layout
24 * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
25 * @author     Stefan Neufeind <pear.neufeind@speedpartner.de>
26 * @copyright  2003-2009 The PHP Group
27 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
28 * @version    SVN: $Id: Layout.php 291756 2009-12-06 02:55:46Z neufeind $
29 * @link       http://pear.php.net/package/Image_Graph
30 */
31
32/**
33 * Include file Image/Graph/Plotarea/Element.php
34 */
35require_once 'Image/Graph/Plotarea/Element.php';
36
37/**
38 * Defines an area of the graph that can be layout'ed.
39 *
40 * Any class that extends this abstract class can be used within a layout on the canvas.
41 *
42 * @category   Images
43 * @package    Image_Graph
44 * @subpackage Layout
45 * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
46 * @author     Stefan Neufeind <pear.neufeind@speedpartner.de>
47 * @copyright  2003-2009 The PHP Group
48 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
49 * @version    Release: 0.8.0
50 * @link       http://pear.php.net/package/Image_Graph
51 * @abstract
52 */
53class Image_Graph_Layout extends Image_Graph_Plotarea_Element
54{
55
56    /**
57     * Has the coordinates already been updated?
58     * @var bool
59     * @access private
60     */
61    var $_updated = false;
62
63    /**
64     * Alignment of the area for each vertice (left, top, right, bottom)
65     * @var array
66     * @access private
67     */
68    var $_alignSize = array ('left' => 0, 'top' => 0, 'right' => 0, 'bottom' => 0);
69
70    /**
71     * Image_Graph_Layout [Constructor]
72     */
73    function Image_Graph_Layout()
74    {
75        parent::__construct();
76        $this->_padding = array('left' => 2, 'top' => 2, 'right' => 2, 'bottom' => 2);
77    }
78
79    /**
80     * Resets the elements
81     *
82     * @return void
83     * @access private
84     */
85    function _reset()
86    {
87        parent::_reset();
88        $this->_updated = false;
89    }
90
91    /**
92     * Calculate the edge offset for a specific edge
93     *
94     * @param array $alignSize  The alignment of the edge
95     * @param int   $offset     The offset/posision of the at 0% edge
96     * @param int   $total      The total size (width or height) of the element
97     * @param int   $multiplier +/- 1 if the edge should pushed either toward more
98     *   negative or positive values
99     *
100     * @return int Edge offset
101     * @since 0.3.0dev2
102     * @access private
103     */
104    function _calcEdgeOffset($alignSize, $offset, $total, $multiplier)
105    {
106        if ($alignSize['unit'] == 'percentage') {
107            return $offset + $multiplier * ($total * $alignSize['value'] / 100);
108        } elseif ($alignSize['unit'] == 'pixels') {
109            if (($alignSize['value'] == 'auto_part1') || ($alignSize['value'] == 'auto_part2')) {
110                $alignSize['value'] = $multiplier * $this->_parent->_getAbsolute($alignSize['value']);
111            }
112            if ($alignSize['value'] < 0) {
113                return $offset + $multiplier * ($total + $alignSize['value']);
114            } else {
115                return $offset + $multiplier * $alignSize['value'];
116            }
117        }
118        return $offset;
119    }
120
121    /**
122     * Calculate the edges
123     *
124     * @return void
125     * @access private
126     */
127    function _calcEdges()
128    {
129        if ((is_array($this->_alignSize)) && (!$this->_updated)) {
130            $left = $this->_calcEdgeOffset(
131                $this->_alignSize['left'],
132                $this->_parent->_fillLeft(),
133                $this->_parent->_fillWidth(),
134                +1
135            );
136            $top = $this->_calcEdgeOffset(
137                $this->_alignSize['top'],
138                $this->_parent->_fillTop(),
139                $this->_parent->_fillHeight(),
140                +1
141            );
142            $right = $this->_calcEdgeOffset(
143                $this->_alignSize['right'],
144                $this->_parent->_fillRight(),
145                $this->_parent->_fillWidth(),
146                -1
147            );
148            $bottom = $this->_calcEdgeOffset(
149                $this->_alignSize['bottom'],
150                $this->_parent->_fillBottom(),
151                $this->_parent->_fillHeight(),
152                -1
153            );
154
155            $this->_setCoords(
156                $left + $this->_padding['left'],
157                $top + $this->_padding['top'],
158                $right - $this->_padding['right'],
159                $bottom - $this->_padding['bottom']
160            );
161        }
162    }
163
164    /**
165     * Update coordinates
166     *
167     * @return void
168     * @access private
169     */
170    function _updateCoords()
171    {
172        $this->_calcEdges();
173        parent::_updateCoords();
174    }
175
176    /**
177     * Pushes an edge of area a specific distance 'into' the canvas
178     *
179     * @param int $edge The edge of the canvas to align relative to
180     * @param int $size The number of pixels or the percentage of the canvas total size to occupy relative to the selected alignment edge
181     *
182     * @return void
183     * @access private
184     */
185    function _push($edge, $size = '100%')
186    {
187        $result = array();
188        if (preg_match("/([0-9]*)\%/", $size, $result)) {
189            $this->_alignSize[$edge] = array(
190                'value' => min(100, max(0, $result[1])),
191                'unit' => 'percentage'
192            );
193        } else {
194            $this->_alignSize[$edge] = array(
195                'value' => $size,
196                'unit' => 'pixels'
197            );
198        }
199    }
200
201    /**
202     * Sets the coordinates of the element
203     *
204     * @param int $left   The leftmost pixel of the element on the canvas
205     * @param int $top    The topmost pixel of the element on the canvas
206     * @param int $right  The rightmost pixel of the element on the canvas
207     * @param int $bottom The bottommost pixel of the element on the canvas
208     *
209     * @return void
210     * @access private
211     */
212    function _setCoords($left, $top, $right, $bottom)
213    {
214        parent::_setCoords($left, $top, $right, $bottom);
215        $this->_updated = true;
216    }
217
218    /**
219     * Returns the calculated "auto" size
220     *
221     * @return int The calculated auto size
222     * @access private
223     */
224    function _getAutoSize()
225    {
226        return false;
227    }
228
229}
230
231?>
232