* @package cleversvg * @subpackage elements */ class csPath extends csBaseShape { protected $xml_node_name = 'path', $path_array = array(); /** * Class constructor * */ public function __construct($path_data = array(), $attrs = array()) { $this->setPathDataArray($path_data); $this->setFill('none'); $this->attributes = array_merge($attrs, $this->attributes); } /** * Compute current path data array * */ protected function computePath() { $path_string = ''; foreach ($this->path_data_array as $move) { if (isset($move[1])) { $path_string .= sprintf('%s %s', $move[0], implode(' ', $move[1])); } else { $path_string .= $move[0]; } } $this->setPathDataString(trim($path_string)); } /** * Sets the path data from an array * * @param array */ public function setPathDataArray($path_data_array) { $this->path_data_array = $path_data_array; $this->computePath(); } /** * Sets the path string * * @param string */ public function setPathDataString($path_data_string) { $last_char = substr($path_data_string, strlen($path_data_string)-1, 1); if (!strtolower($last_char) === 'z') { $path_data_string .= ' z'; } $this->setAttribute('d', $path_data_string); } /** * Init path data array and ready to receive path building directions * */ public function begin() { $this->path_data_array = array(); } public function end() { $this->path_data_array[] = array('Z'); $this->computePath(); } /** * Move to a point * * @param int $x * @param int $y */ public function moveTo($x, $y) { $this->path_data_array[] = array('M', array($x, $y)); } /** * Draws a line moving to a point * * @param int $x * @param int $y */ public function lineTo($x, $y) { $this->path_data_array[] = array('L', array($x, $y)); } /** * Draws a horizontal line moving to a point * * @param int $x */ public function hLineTo($x) { $this->path_data_array[] = array('H', array($x)); } /** * Draws a vertical line moving to a point * * @param int $y */ public function vLineTo($y) { $this->path_data_array[] = array('V', array($y)); } /** * Draws a curve moving to a point * */ public function curveTo($x, $y, $x1, $y1, $x2, $y2) { $this->path_data_array[] = array('C', array($x, $y, $x1, $y1, $x2, $y2)); } /** * Draws a smooth curve moving to a point * */ public function smoothCurveTo($x, $y, $x2, $y2) { $this->path_data_array[] = array('S', array($x, $y, $x2, $y2)); } /** * The x-axis coordinate of one corner of the rectangular region into which an * embedded 'svg' element is placed. * * @param int */ public function setX($x) { $this->setAttribute('x', $x); } /** * The y-axis coordinate of one corner of the rectangular region into which an * embedded 'svg' element is placed. * * @param int */ public function setY($y) { $this->setAttribute('y', $y); } }