108f248e4SAndreas Gohr<?php 218622736SAndreas Gohr 308f248e4SAndreas Gohr/** 408f248e4SAndreas Gohr * EasySVG - Generate SVG from PHP 508f248e4SAndreas Gohr * @author Simon Tarchichi <kartsims@gmail.com> 608f248e4SAndreas Gohr * @version 0.1b 708f248e4SAndreas Gohr * 808f248e4SAndreas Gohr * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform 908f248e4SAndreas Gohr * @see http://stackoverflow.com/questions/14684846/flattening-svg-matrix-transforms-in-inkscape 1008f248e4SAndreas Gohr * @see http://stackoverflow.com/questions/7742148/how-to-convert-text-to-svg-paths 1108f248e4SAndreas Gohr */ 1218622736SAndreas Gohrclass EasySVG 1318622736SAndreas Gohr{ 1408f248e4SAndreas Gohr protected $font; 1508f248e4SAndreas Gohr protected $svg; 1608f248e4SAndreas Gohr 1718622736SAndreas Gohr public function __construct() 1818622736SAndreas Gohr { 1908f248e4SAndreas Gohr // default font data 20*09b1e97eSAndreas Gohr $this->font = new stdClass(); 2108f248e4SAndreas Gohr $this->font->id = ''; 2208f248e4SAndreas Gohr $this->font->horizAdvX = 0; 2308f248e4SAndreas Gohr $this->font->unitsPerEm = 0; 2408f248e4SAndreas Gohr $this->font->ascent = 0; 2508f248e4SAndreas Gohr $this->font->descent = 0; 26*09b1e97eSAndreas Gohr $this->font->glyphs = []; 2708f248e4SAndreas Gohr $this->font->size = 20; 2808f248e4SAndreas Gohr $this->font->color = ''; 2908f248e4SAndreas Gohr $this->font->lineHeight = 1; 3008f248e4SAndreas Gohr $this->font->letterSpacing = 0; 3108f248e4SAndreas Gohr 3208f248e4SAndreas Gohr $this->clearSVG(); 3308f248e4SAndreas Gohr } 3408f248e4SAndreas Gohr 3518622736SAndreas Gohr public function clearSVG() 3618622736SAndreas Gohr { 3708f248e4SAndreas Gohr $this->svg = new SimpleXMLElement('<svg></svg>'); 3808f248e4SAndreas Gohr } 3908f248e4SAndreas Gohr 4008f248e4SAndreas Gohr /** 4108f248e4SAndreas Gohr * Function takes UTF-8 encoded string and returns unicode number for every character. 4208f248e4SAndreas Gohr * @param string $str 4308f248e4SAndreas Gohr * @return string 4408f248e4SAndreas Gohr */ 4518622736SAndreas Gohr private function _utf8ToUnicode($str) 4618622736SAndreas Gohr { 47*09b1e97eSAndreas Gohr $unicode = []; 48*09b1e97eSAndreas Gohr $values = []; 4908f248e4SAndreas Gohr $lookingFor = 1; 5008f248e4SAndreas Gohr 5108f248e4SAndreas Gohr for ($i = 0; $i < strlen($str); $i++) { 5208f248e4SAndreas Gohr $thisValue = ord($str[$i]); 5318622736SAndreas Gohr if ($thisValue < 128) { 5418622736SAndreas Gohr $unicode[] = $thisValue; 5518622736SAndreas Gohr } else { 5608f248e4SAndreas Gohr if (count($values) == 0) $lookingFor = ($thisValue < 224) ? 2 : 3; 5708f248e4SAndreas Gohr $values[] = $thisValue; 5808f248e4SAndreas Gohr if (count($values) == $lookingFor) { 5908f248e4SAndreas Gohr $number = ($lookingFor == 3) ? 6008f248e4SAndreas Gohr (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64) : 6108f248e4SAndreas Gohr (($values[0] % 32) * 64) + ($values[1] % 64); 6208f248e4SAndreas Gohr 6308f248e4SAndreas Gohr $unicode[] = $number; 64*09b1e97eSAndreas Gohr $values = []; 6508f248e4SAndreas Gohr $lookingFor = 1; 6608f248e4SAndreas Gohr } 6708f248e4SAndreas Gohr } 6808f248e4SAndreas Gohr } 6908f248e4SAndreas Gohr 7008f248e4SAndreas Gohr return $unicode; 7108f248e4SAndreas Gohr } 7208f248e4SAndreas Gohr 7308f248e4SAndreas Gohr /** 7408f248e4SAndreas Gohr * Set font params (short-hand method) 7508f248e4SAndreas Gohr * @param string $filepath 7608f248e4SAndreas Gohr * @param integer $size 7708f248e4SAndreas Gohr * @param string $color 7808f248e4SAndreas Gohr */ 7918622736SAndreas Gohr public function setFont($filepath, $size, $color) 8018622736SAndreas Gohr { 8108f248e4SAndreas Gohr $this->setFontSVG($filepath); 8208f248e4SAndreas Gohr $this->setFontSize($size); 8308f248e4SAndreas Gohr $this->setFontColor($color); 8408f248e4SAndreas Gohr } 8508f248e4SAndreas Gohr 8608f248e4SAndreas Gohr /** 8708f248e4SAndreas Gohr * Set font size for display 8808f248e4SAndreas Gohr * @param int $size 8908f248e4SAndreas Gohr * @return void 9008f248e4SAndreas Gohr */ 9118622736SAndreas Gohr public function setFontSize($size) 9218622736SAndreas Gohr { 9308f248e4SAndreas Gohr $this->font->size = $size; 9408f248e4SAndreas Gohr } 9508f248e4SAndreas Gohr 9608f248e4SAndreas Gohr /** 9708f248e4SAndreas Gohr * Set font color 9808f248e4SAndreas Gohr * @param string $color 9908f248e4SAndreas Gohr * @return void 10008f248e4SAndreas Gohr */ 10118622736SAndreas Gohr public function setFontColor($color) 10218622736SAndreas Gohr { 10308f248e4SAndreas Gohr $this->font->color = $color; 10408f248e4SAndreas Gohr } 10508f248e4SAndreas Gohr 10608f248e4SAndreas Gohr /** 10708f248e4SAndreas Gohr * Set the line height from default (1) to custom value 10808f248e4SAndreas Gohr * @param float $value 10908f248e4SAndreas Gohr * @return void 11008f248e4SAndreas Gohr */ 11118622736SAndreas Gohr public function setLineHeight($value) 11218622736SAndreas Gohr { 11308f248e4SAndreas Gohr $this->font->lineHeight = $value; 11408f248e4SAndreas Gohr } 11508f248e4SAndreas Gohr 11608f248e4SAndreas Gohr /** 11708f248e4SAndreas Gohr * Set the letter spacing from default (0) to custom value 11808f248e4SAndreas Gohr * @param float $value 11908f248e4SAndreas Gohr * @return void 12008f248e4SAndreas Gohr */ 12118622736SAndreas Gohr public function setLetterSpacing($value) 12218622736SAndreas Gohr { 12308f248e4SAndreas Gohr $this->font->letterSpacing = $value; 12408f248e4SAndreas Gohr } 12508f248e4SAndreas Gohr 12608f248e4SAndreas Gohr /** 12708f248e4SAndreas Gohr * Function takes path to SVG font (local path) and processes its xml 12808f248e4SAndreas Gohr * to get path representation of every character and additional 12908f248e4SAndreas Gohr * font parameters 13008f248e4SAndreas Gohr * @param string $filepath 13108f248e4SAndreas Gohr * @return void 13208f248e4SAndreas Gohr */ 13318622736SAndreas Gohr public function setFontSVG($filepath) 13418622736SAndreas Gohr { 135*09b1e97eSAndreas Gohr $this->font->glyphs = []; 136*09b1e97eSAndreas Gohr $z = new XMLReader(); 13708f248e4SAndreas Gohr $z->open($filepath); 13808f248e4SAndreas Gohr 13908f248e4SAndreas Gohr // move to the first <product /> node 14008f248e4SAndreas Gohr while ($z->read()) { 14108f248e4SAndreas Gohr $name = $z->name; 14208f248e4SAndreas Gohr 14308f248e4SAndreas Gohr if ($z->nodeType == XMLReader::ELEMENT) { 14408f248e4SAndreas Gohr if ($name == 'font') { 14508f248e4SAndreas Gohr $this->font->id = $z->getAttribute('id'); 14608f248e4SAndreas Gohr $this->font->horizAdvX = $z->getAttribute('horiz-adv-x'); 14708f248e4SAndreas Gohr } 14808f248e4SAndreas Gohr 14908f248e4SAndreas Gohr if ($name == 'font-face') { 15008f248e4SAndreas Gohr $this->font->unitsPerEm = $z->getAttribute('units-per-em'); 15108f248e4SAndreas Gohr $this->font->ascent = $z->getAttribute('ascent'); 15208f248e4SAndreas Gohr $this->font->descent = $z->getAttribute('descent'); 15308f248e4SAndreas Gohr } 15408f248e4SAndreas Gohr 15508f248e4SAndreas Gohr if ($name == 'glyph') { 15608f248e4SAndreas Gohr $unicode = $z->getAttribute('unicode'); 15708f248e4SAndreas Gohr $unicode = $this->_utf8ToUnicode($unicode); 15808f248e4SAndreas Gohr 15908f248e4SAndreas Gohr if (isset($unicode[0])) { 16008f248e4SAndreas Gohr $unicode = $unicode[0]; 16108f248e4SAndreas Gohr 16208f248e4SAndreas Gohr $this->font->glyphs[$unicode] = new stdClass(); 16308f248e4SAndreas Gohr $this->font->glyphs[$unicode]->horizAdvX = $z->getAttribute('horiz-adv-x'); 16408f248e4SAndreas Gohr if (empty($this->font->glyphs[$unicode]->horizAdvX)) { 16508f248e4SAndreas Gohr $this->font->glyphs[$unicode]->horizAdvX = $this->font->horizAdvX; 16608f248e4SAndreas Gohr } 16708f248e4SAndreas Gohr $this->font->glyphs[$unicode]->d = $z->getAttribute('d'); 16808f248e4SAndreas Gohr 16908f248e4SAndreas Gohr // save em value for letter spacing (109 is unicode for the letter 'm') 17008f248e4SAndreas Gohr if ($unicode == '109') { 17108f248e4SAndreas Gohr $this->font->em = $this->font->glyphs[$unicode]->horizAdvX; 17208f248e4SAndreas Gohr } 17308f248e4SAndreas Gohr } 17408f248e4SAndreas Gohr } 17508f248e4SAndreas Gohr } 17608f248e4SAndreas Gohr } 17708f248e4SAndreas Gohr } 17808f248e4SAndreas Gohr 17908f248e4SAndreas Gohr /** 18008f248e4SAndreas Gohr * Add a path to the SVG 18108f248e4SAndreas Gohr * @param string $def 18208f248e4SAndreas Gohr * @param array $attributes 18308f248e4SAndreas Gohr * @return SimpleXMLElement 18408f248e4SAndreas Gohr */ 185*09b1e97eSAndreas Gohr public function addPath($def, $attributes = []) 18618622736SAndreas Gohr { 18708f248e4SAndreas Gohr $path = $this->svg->addChild('path'); 18808f248e4SAndreas Gohr foreach ($attributes as $key => $value) { 18908f248e4SAndreas Gohr $path->addAttribute($key, $value); 19008f248e4SAndreas Gohr } 19108f248e4SAndreas Gohr $path->addAttribute('d', $def); 19208f248e4SAndreas Gohr return $path; 19308f248e4SAndreas Gohr } 19408f248e4SAndreas Gohr 19508f248e4SAndreas Gohr /** 19608f248e4SAndreas Gohr * Add a text to the SVG 19708f248e4SAndreas Gohr * @param string $def 19808f248e4SAndreas Gohr * @param float $x 19908f248e4SAndreas Gohr * @param float $y 20008f248e4SAndreas Gohr * @param array $attributes 20108f248e4SAndreas Gohr * @return SimpleXMLElement 20208f248e4SAndreas Gohr */ 203*09b1e97eSAndreas Gohr public function addText($text, $x = 0, $y = 0, $attributes = []) 20418622736SAndreas Gohr { 20508f248e4SAndreas Gohr $def = $this->textDef($text); 20608f248e4SAndreas Gohr 20708f248e4SAndreas Gohr if ($x != 0 || $y != 0) { 20808f248e4SAndreas Gohr $def = $this->defTranslate($def, $x, $y); 20908f248e4SAndreas Gohr } 21008f248e4SAndreas Gohr 21108f248e4SAndreas Gohr if ($this->font->color) { 21208f248e4SAndreas Gohr $attributes['fill'] = $this->font->color; 21308f248e4SAndreas Gohr } 21408f248e4SAndreas Gohr 21508f248e4SAndreas Gohr return $this->addPath($def, $attributes); 21608f248e4SAndreas Gohr } 21708f248e4SAndreas Gohr 21808f248e4SAndreas Gohr /** 21908f248e4SAndreas Gohr * Function takes UTF-8 encoded string and size, returns xml for SVG paths representing this string. 22008f248e4SAndreas Gohr * @param string $text UTF-8 encoded text 22108f248e4SAndreas Gohr * @return string xml for text converted into SVG paths 22208f248e4SAndreas Gohr */ 22318622736SAndreas Gohr public function textDef($text) 22418622736SAndreas Gohr { 225*09b1e97eSAndreas Gohr $def = []; 22608f248e4SAndreas Gohr 22708f248e4SAndreas Gohr $horizAdvX = 0; 22808f248e4SAndreas Gohr $horizAdvY = $this->font->ascent + $this->font->descent; 229*09b1e97eSAndreas Gohr $fontSize = (float) $this->font->size / $this->font->unitsPerEm; 23008f248e4SAndreas Gohr $text = $this->_utf8ToUnicode($text); 231*09b1e97eSAndreas Gohr $counter = count($text); 23208f248e4SAndreas Gohr 233*09b1e97eSAndreas Gohr for ($i = 0; $i < $counter; $i++) { 23408f248e4SAndreas Gohr $letter = $text[$i]; 23508f248e4SAndreas Gohr 23608f248e4SAndreas Gohr // line break support (10 is unicode for linebreak) 23708f248e4SAndreas Gohr if ($letter == 10) { 23808f248e4SAndreas Gohr $horizAdvX = 0; 23908f248e4SAndreas Gohr $horizAdvY += $this->font->lineHeight * ($this->font->ascent + $this->font->descent); 24008f248e4SAndreas Gohr continue; 24108f248e4SAndreas Gohr } 24208f248e4SAndreas Gohr 24308f248e4SAndreas Gohr // extract character definition 24408f248e4SAndreas Gohr $d = $this->font->glyphs[$letter]->d; 24508f248e4SAndreas Gohr 24608f248e4SAndreas Gohr // transform typo from original SVG format to straight display 24708f248e4SAndreas Gohr $d = $this->defScale($d, $fontSize, -$fontSize); 24808f248e4SAndreas Gohr $d = $this->defTranslate($d, $horizAdvX, $horizAdvY * $fontSize * 2); 24908f248e4SAndreas Gohr 25008f248e4SAndreas Gohr $def[] = $d; 25108f248e4SAndreas Gohr 25208f248e4SAndreas Gohr // next letter's position 25308f248e4SAndreas Gohr $horizAdvX += $this->font->glyphs[$letter]->horizAdvX * $fontSize + $this->font->em * $this->font->letterSpacing * $fontSize; 25408f248e4SAndreas Gohr } 25508f248e4SAndreas Gohr return implode(' ', $def); 25608f248e4SAndreas Gohr } 25708f248e4SAndreas Gohr 25808f248e4SAndreas Gohr /** 25908f248e4SAndreas Gohr * Function takes UTF-8 encoded string and size, returns width and height of the whole text 26008f248e4SAndreas Gohr * @param string $text UTF-8 encoded text 26108f248e4SAndreas Gohr * @return array ($width, $height) 26208f248e4SAndreas Gohr */ 26318622736SAndreas Gohr public function textDimensions($text) 26418622736SAndreas Gohr { 265*09b1e97eSAndreas Gohr $fontSize = (float) $this->font->size / $this->font->unitsPerEm; 26608f248e4SAndreas Gohr $text = $this->_utf8ToUnicode($text); 26708f248e4SAndreas Gohr 26808f248e4SAndreas Gohr $lineWidth = 0; 26908f248e4SAndreas Gohr $lineHeight = ($this->font->ascent + $this->font->descent) * $fontSize * 2; 27008f248e4SAndreas Gohr 27108f248e4SAndreas Gohr $width = 0; 27208f248e4SAndreas Gohr $height = $lineHeight; 273*09b1e97eSAndreas Gohr $counter = count($text); 27408f248e4SAndreas Gohr 275*09b1e97eSAndreas Gohr for ($i = 0; $i < $counter; $i++) { 27608f248e4SAndreas Gohr $letter = $text[$i]; 27708f248e4SAndreas Gohr 27808f248e4SAndreas Gohr // line break support (10 is unicode for linebreak) 27908f248e4SAndreas Gohr if ($letter == 10) { 28008f248e4SAndreas Gohr $width = $lineWidth > $width ? $lineWidth : $width; 28108f248e4SAndreas Gohr $height += $lineHeight * $this->font->lineHeight; 28208f248e4SAndreas Gohr $lineWidth = 0; 28308f248e4SAndreas Gohr continue; 28408f248e4SAndreas Gohr } 28508f248e4SAndreas Gohr 28608f248e4SAndreas Gohr $lineWidth += $this->font->glyphs[$letter]->horizAdvX * $fontSize + $this->font->em * $this->font->letterSpacing * $fontSize; 28708f248e4SAndreas Gohr } 28808f248e4SAndreas Gohr 28908f248e4SAndreas Gohr // only keep the widest line's width 29008f248e4SAndreas Gohr $width = $lineWidth > $width ? $lineWidth : $width; 29108f248e4SAndreas Gohr 292*09b1e97eSAndreas Gohr return [$width, $height]; 29308f248e4SAndreas Gohr } 29408f248e4SAndreas Gohr 29508f248e4SAndreas Gohr /** 29608f248e4SAndreas Gohr * Function takes unicode character and returns the UTF-8 equivalent 29708f248e4SAndreas Gohr * @param string $str 29808f248e4SAndreas Gohr * @return string 29908f248e4SAndreas Gohr */ 30018622736SAndreas Gohr public function unicodeDef($unicode) 30118622736SAndreas Gohr { 30208f248e4SAndreas Gohr 30308f248e4SAndreas Gohr $horizAdvY = $this->font->ascent + $this->font->descent; 304*09b1e97eSAndreas Gohr $fontSize = (float) $this->font->size / $this->font->unitsPerEm; 30508f248e4SAndreas Gohr 30608f248e4SAndreas Gohr // extract character definition 30708f248e4SAndreas Gohr $d = $this->font->glyphs[hexdec($unicode)]->d; 30808f248e4SAndreas Gohr 30908f248e4SAndreas Gohr // transform typo from original SVG format to straight display 31008f248e4SAndreas Gohr $d = $this->defScale($d, $fontSize, -$fontSize); 31108f248e4SAndreas Gohr $d = $this->defTranslate($d, 0, $horizAdvY * $fontSize * 2); 31208f248e4SAndreas Gohr 31308f248e4SAndreas Gohr return $d; 31408f248e4SAndreas Gohr } 31508f248e4SAndreas Gohr 31608f248e4SAndreas Gohr /** 31708f248e4SAndreas Gohr * Returns the character width, as set in the font file 31808f248e4SAndreas Gohr * @param string $str 31908f248e4SAndreas Gohr * @param boolean $is_unicode 32008f248e4SAndreas Gohr * @return float 32108f248e4SAndreas Gohr */ 32218622736SAndreas Gohr public function characterWidth($char, $is_unicode = false) 32318622736SAndreas Gohr { 32408f248e4SAndreas Gohr if ($is_unicode) { 32508f248e4SAndreas Gohr $letter = hexdec($char); 32618622736SAndreas Gohr } else { 32708f248e4SAndreas Gohr $letter = $this->_utf8ToUnicode($char); 32808f248e4SAndreas Gohr } 32908f248e4SAndreas Gohr 33018622736SAndreas Gohr if (!isset($this->font->glyphs[$letter])) { 33118622736SAndreas Gohr return null; 33218622736SAndreas Gohr } 33308f248e4SAndreas Gohr 334*09b1e97eSAndreas Gohr $fontSize = (float) $this->font->size / $this->font->unitsPerEm; 33508f248e4SAndreas Gohr return $this->font->glyphs[$letter]->horizAdvX * $fontSize; 33608f248e4SAndreas Gohr } 33708f248e4SAndreas Gohr 33808f248e4SAndreas Gohr /** 33908f248e4SAndreas Gohr * Applies a translate transformation to definition 34008f248e4SAndreas Gohr * @param string $def definition 34108f248e4SAndreas Gohr * @param float $x 34208f248e4SAndreas Gohr * @param float $y 34308f248e4SAndreas Gohr * @return string 34408f248e4SAndreas Gohr */ 34518622736SAndreas Gohr public function defTranslate($def, $x = 0, $y = 0) 34618622736SAndreas Gohr { 347*09b1e97eSAndreas Gohr return $this->defApplyMatrix($def, [1, 0, 0, 1, $x, $y]); 34808f248e4SAndreas Gohr } 34908f248e4SAndreas Gohr 35008f248e4SAndreas Gohr /** 35108f248e4SAndreas Gohr * Applies a translate transformation to definition 35208f248e4SAndreas Gohr * @param string $def Definition 35308f248e4SAndreas Gohr * @param integer $angle Rotation angle (degrees) 35408f248e4SAndreas Gohr * @param integer $x X coordinate of rotation center 35508f248e4SAndreas Gohr * @param integer $y Y coordinate of rotation center 35608f248e4SAndreas Gohr * @return string 35708f248e4SAndreas Gohr */ 35818622736SAndreas Gohr public function defRotate($def, $angle, $x = 0, $y = 0) 35918622736SAndreas Gohr { 36008f248e4SAndreas Gohr if ($x == 0 && $y == 0) { 36108f248e4SAndreas Gohr $angle = deg2rad($angle); 362*09b1e97eSAndreas Gohr return $this->defApplyMatrix($def, [cos($angle), sin($angle), -sin($angle), cos($angle), 0, 0]); 36308f248e4SAndreas Gohr } 36408f248e4SAndreas Gohr 36508f248e4SAndreas Gohr // rotate by a given point 36608f248e4SAndreas Gohr $def = $this->defTranslate($def, $x, $y); 36708f248e4SAndreas Gohr $def = $this->defRotate($def, $angle); 36808f248e4SAndreas Gohr $def = $this->defTranslate($def, -$x, -$y); 36908f248e4SAndreas Gohr return $def; 37008f248e4SAndreas Gohr } 37108f248e4SAndreas Gohr 37208f248e4SAndreas Gohr /** 37308f248e4SAndreas Gohr * Applies a scale transformation to definition 37408f248e4SAndreas Gohr * @param string $def definition 37508f248e4SAndreas Gohr * @param integer $x 37608f248e4SAndreas Gohr * @param integer $y 37708f248e4SAndreas Gohr * @return string 37808f248e4SAndreas Gohr */ 37918622736SAndreas Gohr public function defScale($def, $x = 1, $y = 1) 38018622736SAndreas Gohr { 381*09b1e97eSAndreas Gohr return $this->defApplyMatrix($def, [$x, 0, 0, $y, 0, 0]); 38208f248e4SAndreas Gohr } 38308f248e4SAndreas Gohr 38408f248e4SAndreas Gohr /** 38508f248e4SAndreas Gohr * Calculates the new definition with the matrix applied 38608f248e4SAndreas Gohr * @param string $def 38708f248e4SAndreas Gohr * @param array $matrix 38808f248e4SAndreas Gohr * @return string 38908f248e4SAndreas Gohr */ 39018622736SAndreas Gohr public function defApplyMatrix($def, $matrix) 39118622736SAndreas Gohr { 39208f248e4SAndreas Gohr 39308f248e4SAndreas Gohr // if there are several shapes in this definition, do the operation for each 39408f248e4SAndreas Gohr preg_match_all('/M[^zZ]*[zZ]/', $def, $shapes); 39508f248e4SAndreas Gohr $shapes = $shapes[0]; 39608f248e4SAndreas Gohr if (count($shapes) > 1) { 39718622736SAndreas Gohr foreach ($shapes as &$shape) { 39808f248e4SAndreas Gohr $shape = $this->defApplyMatrix($shape, $matrix); 39918622736SAndreas Gohr } 40008f248e4SAndreas Gohr return implode(' ', $shapes); 40108f248e4SAndreas Gohr } 40208f248e4SAndreas Gohr 40308f248e4SAndreas Gohr preg_match_all('/[a-zA-Z]+[^a-zA-Z]*/', $def, $instructions); 40408f248e4SAndreas Gohr $instructions = $instructions[0]; 40508f248e4SAndreas Gohr foreach ($instructions as &$instruction) { 40608f248e4SAndreas Gohr $i = preg_replace('/[^a-zA-Z]*/', '', $instruction); 40708f248e4SAndreas Gohr preg_match_all('/\-?[0-9\.]+/', $instruction, $coords); 40808f248e4SAndreas Gohr $coords = $coords[0]; 40908f248e4SAndreas Gohr 41008f248e4SAndreas Gohr if (empty($coords)) { 41108f248e4SAndreas Gohr continue; 41208f248e4SAndreas Gohr } 41308f248e4SAndreas Gohr 414*09b1e97eSAndreas Gohr $new_coords = []; 41508f248e4SAndreas Gohr while (count($coords) > 0) { 41608f248e4SAndreas Gohr // do the matrix calculation stuff 417*09b1e97eSAndreas Gohr [$a, $b, $c, $d, $e, $f] = $matrix; 41808f248e4SAndreas Gohr 41908f248e4SAndreas Gohr // exception for relative instruction 42008f248e4SAndreas Gohr if (preg_match('/[a-z]/', $i)) { 42108f248e4SAndreas Gohr $e = 0; 42208f248e4SAndreas Gohr $f = 0; 42308f248e4SAndreas Gohr } 42408f248e4SAndreas Gohr 42508f248e4SAndreas Gohr // convert horizontal lineto (relative) 42608f248e4SAndreas Gohr if ($i == 'h') { 42708f248e4SAndreas Gohr $i = 'l'; 428*09b1e97eSAndreas Gohr $x = (float) array_shift($coords); 42908f248e4SAndreas Gohr $y = 0; 43008f248e4SAndreas Gohr 43108f248e4SAndreas Gohr // add new point's coordinates 432*09b1e97eSAndreas Gohr $current_point = [$a * $x + $e, $b * $x + $f]; 433*09b1e97eSAndreas Gohr $new_coords = [...$new_coords, ...$current_point]; 43418622736SAndreas Gohr } // convert vertical lineto (relative) 43508f248e4SAndreas Gohr elseif ($i == 'v') { 43608f248e4SAndreas Gohr $i = 'l'; 43708f248e4SAndreas Gohr $x = 0; 438*09b1e97eSAndreas Gohr $y = (float) array_shift($coords); 43908f248e4SAndreas Gohr 44008f248e4SAndreas Gohr // add new point's coordinates 441*09b1e97eSAndreas Gohr $current_point = [$c * $y + $e, $d * $y + $f]; 442*09b1e97eSAndreas Gohr $new_coords = [...$new_coords, ...$current_point]; 44318622736SAndreas Gohr } // convert quadratic bezier curve (relative) 44408f248e4SAndreas Gohr elseif ($i == 'q') { 445*09b1e97eSAndreas Gohr $x = (float) array_shift($coords); 446*09b1e97eSAndreas Gohr $y = (float) array_shift($coords); 44708f248e4SAndreas Gohr 44808f248e4SAndreas Gohr // add new point's coordinates 449*09b1e97eSAndreas Gohr $current_point = [$a * $x + $c * $y + $e, $b * $x + $d * $y + $f]; 450*09b1e97eSAndreas Gohr $new_coords = [...$new_coords, ...$current_point]; 45108f248e4SAndreas Gohr 45208f248e4SAndreas Gohr // same for 2nd point 453*09b1e97eSAndreas Gohr $x = (float) array_shift($coords); 454*09b1e97eSAndreas Gohr $y = (float) array_shift($coords); 45508f248e4SAndreas Gohr 45608f248e4SAndreas Gohr // add new point's coordinates 457*09b1e97eSAndreas Gohr $current_point = [$a * $x + $c * $y + $e, $b * $x + $d * $y + $f]; 45808f248e4SAndreas Gohr $new_coords = array_merge($new_coords, $current_point); 45908f248e4SAndreas Gohr } 46008f248e4SAndreas Gohr 46108f248e4SAndreas Gohr // every other commands 46208f248e4SAndreas Gohr // @TODO: handle 'a,c,s' (elliptic arc curve) commands 46308f248e4SAndreas Gohr // cf. http://www.w3.org/TR/SVG/paths.html#PathDataCurveCommands 46408f248e4SAndreas Gohr else { 465*09b1e97eSAndreas Gohr $x = (float) array_shift($coords); 466*09b1e97eSAndreas Gohr $y = (float) array_shift($coords); 46708f248e4SAndreas Gohr 46808f248e4SAndreas Gohr // add new point's coordinates 469*09b1e97eSAndreas Gohr $current_point = [$a * $x + $c * $y + $e, $b * $x + $d * $y + $f]; 470*09b1e97eSAndreas Gohr $new_coords = [...$new_coords, ...$current_point]; 47108f248e4SAndreas Gohr } 47208f248e4SAndreas Gohr } 47308f248e4SAndreas Gohr 47408f248e4SAndreas Gohr $instruction = $i . implode(',', $new_coords); 47508f248e4SAndreas Gohr 47608f248e4SAndreas Gohr // remove useless commas 47708f248e4SAndreas Gohr $instruction = preg_replace('/,\-/', '-', $instruction); 47808f248e4SAndreas Gohr } 47908f248e4SAndreas Gohr 48008f248e4SAndreas Gohr return implode('', $instructions); 48108f248e4SAndreas Gohr } 48208f248e4SAndreas Gohr 48308f248e4SAndreas Gohr 48408f248e4SAndreas Gohr 48508f248e4SAndreas Gohr /** 48608f248e4SAndreas Gohr * 48708f248e4SAndreas Gohr * Short-hand methods 48808f248e4SAndreas Gohr * 48908f248e4SAndreas Gohr */ 49008f248e4SAndreas Gohr 49108f248e4SAndreas Gohr /** 49208f248e4SAndreas Gohr * Return full SVG XML 49308f248e4SAndreas Gohr * @return string 49408f248e4SAndreas Gohr */ 49518622736SAndreas Gohr public function asXML() 49618622736SAndreas Gohr { 49708f248e4SAndreas Gohr return $this->svg->asXML(); 49808f248e4SAndreas Gohr } 49908f248e4SAndreas Gohr 50008f248e4SAndreas Gohr /** 50108f248e4SAndreas Gohr * Adds an attribute to the SVG 50208f248e4SAndreas Gohr * @param string $key 50308f248e4SAndreas Gohr * @param string $value 50408f248e4SAndreas Gohr */ 50518622736SAndreas Gohr public function addAttribute($key, $value) 50618622736SAndreas Gohr { 50708f248e4SAndreas Gohr return $this->svg->addAttribute($key, $value); 50808f248e4SAndreas Gohr } 50908f248e4SAndreas Gohr} 510