SVGGraphShape.php 8.35 KB
Newer Older
Hamza Arfaoui's avatar
Hamza Arfaoui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
<?php
/**
 * Copyright (C) 2015-2016 Graham Breach
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * For more information, please contact <graham@goat1000.com>
 */

require_once "SVGGraphCoords.php";

define("SVGG_SHAPE_ABOVE", 1);
define("SVGG_SHAPE_BELOW", 0);

/**
 * Arbitrary shapes for adding to graphs
 */
class SVGGraphShapeList {

  private $graph;
  private $shapes = array();

  public function __construct(&$graph)
  {
    $this->graph = $graph;
  }

  /**
   * Load shapes from options list
   */
  public function Load(&$settings)
  {
    if(!isset($settings['shape']))
      return;
  
    if(!is_array($settings['shape']) || !isset($settings['shape'][0]))
      throw new Exception('Malformed shape option');

    if(!is_array($settings['shape'][0])) {
      $this->AddShape($settings['shape']);
    } else {
      foreach($settings['shape'] as $shape) {
        $this->AddShape($shape);
      }
    }
  }

  /**
   * Draw all the shapes for the selected depth
   */
  public function Draw($depth)
  {
    $content = array();
    foreach($this->shapes as $shape) {
      if($shape->Depth($depth))
        $content[] = $shape->Draw($this->graph);
    }
    return implode($content);
  }

  /**
   * Adds a shape from config array
   */
  private function AddShape(&$shape_array)
  {
    $shape = $shape_array[0];
    unset($shape_array[0]);

    $class_map = array(
      'circle' => 'SVGGraphCircle',
      'ellipse' => 'SVGGraphEllipse',
      'rect' => 'SVGGraphRect',
      'line' => 'SVGGraphLine',
      'polyline' => 'SVGGraphPolyLine',
      'polygon' => 'SVGGraphPolygon',
      'path' => 'SVGGraphPath',
    );

    if(isset($class_map[$shape]) && class_exists($class_map[$shape])) {
      $depth = SVGG_SHAPE_BELOW;
      if(isset($shape_array['depth'])) {
        if($shape_array['depth'] == 'above')
          $depth = SVGG_SHAPE_ABOVE;
      }
      if(isset($shape_array['clip_to_grid']) && $shape_array['clip_to_grid'] &&
        method_exists($this->graph, 'GridClipPath')) {
        $clip_id = $this->graph->GridClipPath();
        $shape_array['clip-path'] = "url(#{$clip_id})";
      }
      unset($shape_array['depth'], $shape_array['clip_to_grid']);
      $this->shapes[] = new $class_map[$shape]($shape_array, $depth);
    } else {
      throw new Exception("Unknown shape [{$shape}]");
    }
  }
}

abstract class SVGGraphShape {

  protected $depth = SVGG_SHAPE_BELOW;
  protected $element = '';
  protected $link = NULL;
  protected $link_target = '_blank';
  protected $coords = NULL;

  /**
   * attributes required to draw shape
   */
  protected $required = array();

  /**
   * attributes that support coordinate transformation
   */
  protected $transform = array();

  /**
   * coordinate pairs for dependent transforns - don't include them in
   * $transform or they will be transformed twice
   */
  protected $transform_pairs = array();

  /**
   * colour gradients/patterns, and whether to allow gradients
   */
  private $colour_convert = array(
    'stroke' => true,
    'fill' => false
  );

  /**
   * default attributes for all shapes
   */
  protected $attrs = array(
    'stroke' => '#000',
    'fill' => 'none'
  );

  public function __construct(&$attrs, $depth)
  {
    $this->attrs = array_merge($this->attrs, $attrs);
    $this->depth = $depth;

    $missing = array();
    foreach($this->required as $opt)
      if(!isset($this->attrs[$opt]))
        $missing[] = $opt;

    if(count($missing))
      throw new Exception("{$this->element} attribute(s) not found: " .
        implode(', ', $missing));

    if(isset($this->attrs['href']))
      $this->link = $this->attrs['href'];
    if(isset($this->attrs['xlink:href']))
      $this->link = $this->attrs['xlink:href'];
    if(isset($this->attrs['target']))
      $this->link_target = $this->attrs['target'];
    unset($this->attrs['href'], $this->attrs['xlink:href'],
      $this->attrs['target']);
  }

  /**
   * returns true if the depth is correct
   */
  public function Depth($d)
  {
    return $this->depth == $d;
  }

  /**
   * draws the shape
   */
  public function Draw(&$graph)
  {
    $this->coords = new SVGGraphCoords($graph);

    $attributes = array();
    foreach($this->attrs as $attr => $value) {
      if(!is_null($value)) {
        if(isset($this->transform[$attr])) {
          $val = $this->coords->Transform($value, $this->transform[$attr]);
        } else {
          $val = isset($this->colour_convert[$attr]) ? 
            $graph->ParseColour($value, NULL, $this->colour_convert[$attr]) :
            $value;
        }
        $attr = str_replace('_', '-', $attr);
        $attributes[$attr] = $val;
      }
    }
    $this->TransformCoordinates($attributes);
    $element = $this->DrawElement($graph, $attributes);
    if(!is_null($this->link)) {
      $link = array('xlink:href' => $this->link);
      if(!is_null($this->link_target))
        $link['target'] = $this->link_target;
      $element = $graph->Element('a', $link, NULL, $element);
    }
    return $element;
  }

  /**
   * Transform coordinate pairs
   */
  protected function TransformCoordinates(&$attributes)
  {
    if(count($this->transform_pairs)) {
      foreach($this->transform_pairs as $pair) {
        $coords = $this->coords->TransformCoords($attributes[$pair[0]],
          $attributes[$pair[1]]);
        $attributes[$pair[0]] = $coords[0];
        $attributes[$pair[1]] = $coords[1];
      }
    }
  }

  /**
   * Performs the conversion to SVG fragment
   */
  protected function DrawElement(&$graph, &$attributes)
  {
    return $graph->Element($this->element, $attributes);
  }

}

class SVGGraphCircle extends SVGGraphShape {
  protected $element = 'circle';
  protected $required = array('cx','cy','r');
  protected $transform = array('r' => 'y');
  protected $transform_pairs = array(array('cx', 'cy'));
}

class SVGGraphEllipse extends SVGGraphShape {
  protected $element = 'ellipse';
  protected $required = array('cx','cy','rx','ry');
  protected $transform = array('rx' => 'x', 'ry' => 'y');
  protected $transform_pairs = array(array('cx', 'cy'));
}

class SVGGraphRect extends SVGGraphShape {
  protected $element = 'rect';
  protected $required = array('x','y','width','height');
  protected $transform = array('width' => 'x', 'height' => 'y');
  protected $transform_pairs = array(array('x', 'y'));
}

class SVGGraphLine extends SVGGraphShape {
  protected $element = 'line';
  protected $required = array('x1','y1','x2','y2');
  protected $transform_pairs = array(array('x1', 'y1'), array('x2','y2'));
}

class SVGGraphPath extends SVGGraphShape {
  protected $element = 'path';
  protected $required = array('d');
}

class SVGGraphPolyLine extends SVGGraphShape {
  protected $element = 'polyline';
  protected $required = array('points');

  public function __construct(&$attrs, $depth)
  {
    parent::__construct($attrs, $depth);
    if(!is_array($this->attrs['points']))
      $this->attrs['points'] = explode(' ', $this->attrs['points']);
    $count = count($this->attrs['points']);
    if($count < 4 || $count % 2 == 1)
      throw new Exception("Shape must have at least 2 pairs of points");
  }

  /**
   * Override to transform pairs of points
   */
  protected function TransformCoordinates(&$attributes)
  {
    $count = count($attributes['points']);
    for($i = 0; $i < $count; $i += 2) {
      $x = $attributes['points'][$i];
      $y = $attributes['points'][$i + 1];
      $coords = $this->coords->TransformCoords($x, $y);
      $attributes['points'][$i] = $coords[0];
      $attributes['points'][$i + 1] = $coords[1];
    }
  }

  /**
   * Override to build the points attribute
   */
  protected function DrawElement(&$graph, &$attributes)
  {
    $attributes['points'] = implode(' ', $attributes['points']);
    return parent::DrawElement($graph, $attributes);
  }
}

class SVGGraphPolygon extends SVGGraphPolyLine {
  protected $element = 'polygon';
}