wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.ImageOutputStrategy.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.ImageOutputStrategy.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/output/class.OutputStrategy.php");
20 /**
21  * Some constants describing the line type
22  */
23 define("LINETYPE_DIRECT", 0);
24 define("LINETYPE_ROUTED", 1);
25 /**
26  * @class ImageOutputStrategy
27  * @ingroup Output
28  * @brief This OutputStrategy outputs a tree of objects into an image file. It must be configured
29  * with a map that was calculated by a LayoutVisitor.
30  *
31  * @author ingo herwig <ingo@wemove.com>
32  */
34 {
35  var $_format = null;
36  var $_file = '';
37  var $_map = null;
38  var $_img = null;
39  var $_width = 0;
40  var $_height = 0;
41  var $_xscale = 0;
42  var $_yscale = 0;
43  var $_border = 0;
44  var $_bgColor = null;
45  var $_txtColor = null;
46  var $_lineColor = null;
47  var $_labelDim = null;
48  var $_textPos = null;
49  var $_usemap = '';
50  /**
51  * Constructor.
52  * @param format Image format name [IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP].
53  * @param file The output file name.
54  * @param map The position map provided by LayoutVisitor.
55  * @param lineType The linetype to use [LINETYPE_DIRECT|LINETYPE_ROUTED] DEFAULT LINETYPE_DIRECT.
56  * @param scale The image scale (will be xscale) DEFAULT 100.
57  * @param aspect The image aspect (aspect = xscale/yscale) DEFAULT 0.5.
58  * @param border The image border [px] DEFAULT 50.
59  * @param usemap Name of the HTML ImageMap to write to stdout ['' means no map] DEFAULT ''.
60  */
61  function ImageOutputStrategy($format, $file, $map, $lineType=LINETYPE_DIRECT, $scale=100, $aspect=0.5, $border=50, $usemap='')
62  {
63  if (!(ImageTypes() & $format))
64  WCMFException::throwEx($format." image support is disabled.", __FILE__, __LINE__);
65  if (!is_array($map))
66  WCMFException::throwEx("Parameter map is no array.", __FILE__, __LINE__);
67  $this->_format = $format;
68  $this->_file = $file;
69  $this->_map = $map;
70  $this->_lineType = $lineType;
71  $this->_xscale = $scale;
72  $this->_yscale = $scale/$aspect;
73  $this->_border = $border;
74  $this->_usemap = $usemap;
75  // define label dimensions relative to connector position
76  $this->_labelDim['left'] = -10;
77  $this->_labelDim['top'] = -10;
78  $this->_labelDim['right'] = 80;
79  $this->_labelDim['bottom'] = 20;
80  // define text position relative to connector position
81  $this->_textPos['left'] = -5;
82  $this->_textPos['top'] = -8;
83  }
84  /**
85  * Write the image header.
86  */
87  function writeHeader()
88  {
89  // calculate bounding box
90  while (list ($key, $val) = each ($this->_map)) {
91  if($val->x >= $this->_width)
92  $this->_width = $val->x;
93  if($val->y >= $this->_height)
94  $this->_height = $val->y;
95  }
96  $this->_width = $this->_width * $this->_xscale + $this->_labelDim['right'] - $this->_labelDim['left'] + 2*$this->_border;
97  $this->_height = $this->_height * $this->_yscale + $this->_labelDim['bottom'] - $this->_labelDim['top'] + 2*$this->_border;
98  $this->_img = ImageCreate($this->_width,$this->_height);
99  $this->_bgColor = ImageColorAllocate($this->_img,255,255,255);
100  $this->_txtColor = ImageColorAllocate($this->_img,0,128,192);
101  $this->_lineColor = $this->_txtColor;
102  ImageFilledRectangle($this->_img,0,0,$this->_width,$this->_height,$this->_bgColor);
103 
104  if ($this->_usemap != '')
105  echo "\n".'<map name="'.$this->_usemap.'">'."\n";
106  }
107  /**
108  * Write the image footer.
109  */
110  function writeFooter()
111  {
112  ImageString($this->_img,1,$this->_width-350,$this->_height-10,'wemove digital solutions. '.date ("l dS of F Y h:i:s A"),$this->_txtColor);
113  if ($this->_format & IMG_GIF)
114  ImageGIF($this->_img, $this->_file);
115  if ($this->_format & IMG_PNG)
116  ImagePNG($this->_img, $this->_file);
117  if ($this->_format & IMG_JPEG)
118  ImageJPEG($this->_img, $this->_file);
119  if ($this->_format & IMG_WBMP)
120  ImageWBMP($this->_img, $this->_file);
121 
122  if ($this->_usemap != '')
123  echo "\n".'</map>'."\n";
124  }
125  /**
126  * Write the object's content.
127  * @param obj The object to write.
128  */
129  function writeObject(&$obj)
130  {
131  $oid = $obj->getOID();
132  $x = $this->_map[$oid]->x * $this->_xscale - $this->_labelDim['left'] + $this->_border;
133  $y = $this->_map[$oid]->y * $this->_yscale - $this->_labelDim['top'] + $this->_border;
134 
135  $statusStr = '';
136  if ($obj->getState() == STATE_DIRTY)
137  $statusStr = 'M';
138  if ($obj->getState() == STATE_NEW)
139  $statusStr = 'N';
140  if ($obj->getState() == STATE_DELETED)
141  $statusStr = 'D';
142 
143  // print label
144  ImageRectangle($this->_img,
145  $x + $this->_labelDim['left'],
146  $y + $this->_labelDim['top'],
147  $x + $this->_labelDim['right'],
148  $y + $this->_labelDim['bottom'],
149  $this->_txtColor);
150  // write text
151  ImageString($this->_img,1,
152  $x + $this->_textPos['left'],
153  $y + $this->_textPos['top'],
154  $obj->getType(),
156  if (strlen($oid) > 7)
157  $idStr = "...".subStr($oid, strlen($oid)-4, 4);
158  else
159  $idStr = $oid;
160  ImageString($this->_img,5,
161  $x + $this->_textPos['left'],
162  $y + $this->_textPos['top']+14,
163  $idStr.' '.$statusStr,
164  $this->_txtColor);
165 
166  // draw line
167  $parent = & $obj->getParent();
168  if ($parent)
169  $this->drawConnectionLine($parent->getOID(), $oid);
170 
171  // print map
172  if ($this->_usemap != '')
173  {
174  echo '<area shape="rect" coords="'.
175  ($x + $this->_labelDim['left']).','.
176  ($y + $this->_labelDim['top']).','.
177  ($x + $this->_labelDim['right']).','.
178  ($y + $this->_labelDim['bottom'] + 8*$this->_map[$oid]->z).
179  '" onclick="javascript:if (nodeClicked) nodeClicked(\''.$obj->getOID().'\')" alt="'.$obj->getOID().'">'."\n";
180  }
181  }
182  /**
183  * Draw connection line.
184  * @attention Internal use only.
185  * @param poid The parent object's object id.
186  * @param oid The object's object id.
187  */
188  function drawConnectionLine($poid, $oid)
189  {
190  list($start, $end) = $this->calculateEndPoints($poid, $oid);
191  if($this->_lineType == LINETYPE_DIRECT)
192  $this->drawDirectLine($start, $end);
193  else if($this->_lineType == LINETYPE_ROUTED)
194  $this->drawRoutedLine($start, $end);
195  }
196  /**
197  * Draw direct line.
198  * @attention Internal use only.
199  * @param start The start point (Position).
200  * @param end The end point (Position).
201  */
202  function drawDirectLine($start, $end)
203  {
204  ImageLine($this->_img,
205  $start->x,
206  $start->y,
207  $end->x,
208  $end->y,
209  $this->_lineColor);
210  }
211  /**
212  * Draw routed line.
213  * @attention Internal use only.
214  * @param start The start point (Position).
215  * @param end The end point (Position).
216  */
217  function drawRoutedLine($start, $end)
218  {
219  if ($this->_map["type"] == MAPTYPE_HORIZONTAL)
220  {
221  ImageLine($this->_img,
222  $start->x,
223  $start->y,
224  $start->x,
225  $start->y-($start->y-$end->y)/2,
226  $this->_lineColor);
227  ImageLine($this->_img,
228  $start->x,
229  $start->y-($start->y-$end->y)/2,
230  $end->x,
231  $start->y-($start->y-$end->y)/2,
232  $this->_lineColor);
233  ImageLine($this->_img,
234  $end->x,
235  $start->y-($start->y-$end->y)/2,
236  $end->x,
237  $end->y,
238  $this->_lineColor);
239  }
240  else
241  {
242  ImageLine($this->_img,
243  $start->x,
244  $start->y,
245  $start->x+($end->x-$start->x)/2,
246  $start->y,
247  $this->_lineColor);
248  ImageLine($this->_img,
249  $start->x+($end->x-$start->x)/2,
250  $start->y,
251  $start->x+($end->x-$start->x)/2,
252  $end->y,
253  $this->_lineColor);
254  ImageLine($this->_img,
255  $start->x+($end->x-$start->x)/2,
256  $end->y,
257  $end->x,
258  $end->y,
259  $this->_lineColor);
260  }
261  }
262  /**
263  * Calculate line end points.
264  * @attention Internal use only.
265  * @param poid The parent object's object id.
266  * @param oid The object's object id.
267  * @return Array containing start and end position
268  */
269  function calculateEndPoints($poid, $oid)
270  {
271  // from child...
272  if ($this->_map["type"] == MAPTYPE_HORIZONTAL)
273  {
274  // connect from mid top...
275  $x1 = $this->_map[$oid]->x * $this->_xscale + ($this->_labelDim['right'] - $this->_labelDim['left'])/2 + $this->_border;
276  $y1 = $this->_map[$oid]->y * $this->_yscale + $this->_border - 1;
277  }
278  else
279  {
280  // connect from mid left...
281  $x1 = $this->_map[$oid]->x * $this->_xscale + $this->_border - 1;
282  $y1 = $this->_map[$oid]->y * $this->_yscale + ($this->_labelDim['bottom'] - $this->_labelDim['top'])/2 + $this->_border;
283  }
284  // ...to parent
285  if ($this->_map["type"] == MAPTYPE_HORIZONTAL)
286  {
287  // ...to mid bottom
288  $x2 = $this->_map[$poid]->x * $this->_xscale + ($this->_labelDim['right'] - $this->_labelDim['left'])/2 + $this->_border;
289  $y2 = $this->_map[$poid]->y * $this->_yscale + ($this->_labelDim['bottom'] - $this->_labelDim['top']) + $this->_border + 1;
290  }
291  else
292  {
293  // ...to mid right
294  $x2 = $this->_map[$poid]->x * $this->_xscale + $this->_labelDim['right'] - $this->_labelDim['left'] + $this->_border + 1;
295  $y2 = $this->_map[$poid]->y * $this->_yscale + ($this->_labelDim['bottom'] - $this->_labelDim['top'])/2 + $this->_border;
296  }
297  return array(new Position($x1,$y1,0), new Position($x2,$y2,0));
298  }
299 }
300 ?>
301 
const STATE_DIRTY
const STATE_DELETED
OutputStrategy is used to write an object's content to a destination (called 'document') using a spec...
This OutputStrategy outputs a tree of objects into an image file. It must be configured with a map th...
ImageOutputStrategy($format, $file, $map, $lineType=LINETYPE_DIRECT, $scale=100, $aspect=0.5, $border=50, $usemap='')
throwEx($message, $file='', $line='')
const LINETYPE_DIRECT
const STATE_NEW