xref: /plugin/captcha/EasySVG.php (revision b15da4f28e27ee8133481c8a0c1f8b90d2d9ba88)
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
2009b1e97eSAndreas 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;
2609b1e97eSAndreas 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     */
45*b15da4f2SAndreas Gohr    private function utf8ToUnicode($str)
4618622736SAndreas Gohr    {
4709b1e97eSAndreas Gohr        $unicode = [];
4809b1e97eSAndreas 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;
6409b1e97eSAndreas 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    {
13509b1e97eSAndreas Gohr        $this->font->glyphs = [];
13609b1e97eSAndreas 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');
157*b15da4f2SAndreas 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     */
18509b1e97eSAndreas 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     */
20309b1e97eSAndreas 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    {
22509b1e97eSAndreas Gohr        $def = [];
22608f248e4SAndreas Gohr
22708f248e4SAndreas Gohr        $horizAdvX = 0;
22808f248e4SAndreas Gohr        $horizAdvY = $this->font->ascent + $this->font->descent;
22909b1e97eSAndreas Gohr        $fontSize = (float) $this->font->size / $this->font->unitsPerEm;
230*b15da4f2SAndreas Gohr        $text = $this->utf8ToUnicode($text);
23109b1e97eSAndreas Gohr        $counter = count($text);
23208f248e4SAndreas Gohr
23309b1e97eSAndreas 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
253*b15da4f2SAndreas Gohr            $horizAdvX += $this->font->glyphs[$letter]->horizAdvX * $fontSize +
254*b15da4f2SAndreas Gohr                $this->font->em * $this->font->letterSpacing * $fontSize;
25508f248e4SAndreas Gohr        }
25608f248e4SAndreas Gohr        return implode(' ', $def);
25708f248e4SAndreas Gohr    }
25808f248e4SAndreas Gohr
25908f248e4SAndreas Gohr    /**
26008f248e4SAndreas Gohr     * Function takes UTF-8 encoded string and size, returns width and height of the whole text
26108f248e4SAndreas Gohr     * @param string $text UTF-8 encoded text
26208f248e4SAndreas Gohr     * @return array ($width, $height)
26308f248e4SAndreas Gohr     */
26418622736SAndreas Gohr    public function textDimensions($text)
26518622736SAndreas Gohr    {
26609b1e97eSAndreas Gohr        $fontSize = (float) $this->font->size / $this->font->unitsPerEm;
267*b15da4f2SAndreas Gohr        $text = $this->utf8ToUnicode($text);
26808f248e4SAndreas Gohr
26908f248e4SAndreas Gohr        $lineWidth = 0;
27008f248e4SAndreas Gohr        $lineHeight = ($this->font->ascent + $this->font->descent) * $fontSize * 2;
27108f248e4SAndreas Gohr
27208f248e4SAndreas Gohr        $width = 0;
27308f248e4SAndreas Gohr        $height = $lineHeight;
27409b1e97eSAndreas Gohr        $counter = count($text);
27508f248e4SAndreas Gohr
27609b1e97eSAndreas Gohr        for ($i = 0; $i < $counter; $i++) {
27708f248e4SAndreas Gohr            $letter = $text[$i];
27808f248e4SAndreas Gohr
27908f248e4SAndreas Gohr            // line break support (10 is unicode for linebreak)
28008f248e4SAndreas Gohr            if ($letter == 10) {
28108f248e4SAndreas Gohr                $width = $lineWidth > $width ? $lineWidth : $width;
28208f248e4SAndreas Gohr                $height += $lineHeight * $this->font->lineHeight;
28308f248e4SAndreas Gohr                $lineWidth = 0;
28408f248e4SAndreas Gohr                continue;
28508f248e4SAndreas Gohr            }
28608f248e4SAndreas Gohr
287*b15da4f2SAndreas Gohr            $lineWidth += $this->font->glyphs[$letter]->horizAdvX * $fontSize +
288*b15da4f2SAndreas Gohr                $this->font->em * $this->font->letterSpacing * $fontSize;
28908f248e4SAndreas Gohr        }
29008f248e4SAndreas Gohr
29108f248e4SAndreas Gohr        // only keep the widest line's width
29208f248e4SAndreas Gohr        $width = $lineWidth > $width ? $lineWidth : $width;
29308f248e4SAndreas Gohr
29409b1e97eSAndreas Gohr        return [$width, $height];
29508f248e4SAndreas Gohr    }
29608f248e4SAndreas Gohr
29708f248e4SAndreas Gohr    /**
29808f248e4SAndreas Gohr     * Function takes unicode character and returns the UTF-8 equivalent
29908f248e4SAndreas Gohr     * @param string $str
30008f248e4SAndreas Gohr     * @return string
30108f248e4SAndreas Gohr     */
30218622736SAndreas Gohr    public function unicodeDef($unicode)
30318622736SAndreas Gohr    {
30408f248e4SAndreas Gohr
30508f248e4SAndreas Gohr        $horizAdvY = $this->font->ascent + $this->font->descent;
30609b1e97eSAndreas Gohr        $fontSize = (float) $this->font->size / $this->font->unitsPerEm;
30708f248e4SAndreas Gohr
30808f248e4SAndreas Gohr        // extract character definition
30908f248e4SAndreas Gohr        $d = $this->font->glyphs[hexdec($unicode)]->d;
31008f248e4SAndreas Gohr
31108f248e4SAndreas Gohr        // transform typo from original SVG format to straight display
31208f248e4SAndreas Gohr        $d = $this->defScale($d, $fontSize, -$fontSize);
31308f248e4SAndreas Gohr        $d = $this->defTranslate($d, 0, $horizAdvY * $fontSize * 2);
31408f248e4SAndreas Gohr
31508f248e4SAndreas Gohr        return $d;
31608f248e4SAndreas Gohr    }
31708f248e4SAndreas Gohr
31808f248e4SAndreas Gohr    /**
31908f248e4SAndreas Gohr     * Returns the character width, as set in the font file
32008f248e4SAndreas Gohr     * @param string $str
32108f248e4SAndreas Gohr     * @param boolean $is_unicode
32208f248e4SAndreas Gohr     * @return float
32308f248e4SAndreas Gohr     */
32418622736SAndreas Gohr    public function characterWidth($char, $is_unicode = false)
32518622736SAndreas Gohr    {
32608f248e4SAndreas Gohr        if ($is_unicode) {
32708f248e4SAndreas Gohr            $letter = hexdec($char);
32818622736SAndreas Gohr        } else {
329*b15da4f2SAndreas Gohr            $letter = $this->utf8ToUnicode($char);
33008f248e4SAndreas Gohr        }
33108f248e4SAndreas Gohr
33218622736SAndreas Gohr        if (!isset($this->font->glyphs[$letter])) {
33318622736SAndreas Gohr            return null;
33418622736SAndreas Gohr        }
33508f248e4SAndreas Gohr
33609b1e97eSAndreas Gohr        $fontSize = (float) $this->font->size / $this->font->unitsPerEm;
33708f248e4SAndreas Gohr        return $this->font->glyphs[$letter]->horizAdvX * $fontSize;
33808f248e4SAndreas Gohr    }
33908f248e4SAndreas Gohr
34008f248e4SAndreas Gohr    /**
34108f248e4SAndreas Gohr     * Applies a translate transformation to definition
34208f248e4SAndreas Gohr     * @param string $def definition
34308f248e4SAndreas Gohr     * @param float $x
34408f248e4SAndreas Gohr     * @param float $y
34508f248e4SAndreas Gohr     * @return string
34608f248e4SAndreas Gohr     */
34718622736SAndreas Gohr    public function defTranslate($def, $x = 0, $y = 0)
34818622736SAndreas Gohr    {
34909b1e97eSAndreas Gohr        return $this->defApplyMatrix($def, [1, 0, 0, 1, $x, $y]);
35008f248e4SAndreas Gohr    }
35108f248e4SAndreas Gohr
35208f248e4SAndreas Gohr    /**
35308f248e4SAndreas Gohr     * Applies a translate transformation to definition
35408f248e4SAndreas Gohr     * @param string $def Definition
35508f248e4SAndreas Gohr     * @param integer $angle Rotation angle (degrees)
35608f248e4SAndreas Gohr     * @param integer $x X coordinate of rotation center
35708f248e4SAndreas Gohr     * @param integer $y Y coordinate of rotation center
35808f248e4SAndreas Gohr     * @return string
35908f248e4SAndreas Gohr     */
36018622736SAndreas Gohr    public function defRotate($def, $angle, $x = 0, $y = 0)
36118622736SAndreas Gohr    {
36208f248e4SAndreas Gohr        if ($x == 0 && $y == 0) {
36308f248e4SAndreas Gohr            $angle = deg2rad($angle);
36409b1e97eSAndreas Gohr            return $this->defApplyMatrix($def, [cos($angle), sin($angle), -sin($angle), cos($angle), 0, 0]);
36508f248e4SAndreas Gohr        }
36608f248e4SAndreas Gohr
36708f248e4SAndreas Gohr        // rotate by a given point
36808f248e4SAndreas Gohr        $def = $this->defTranslate($def, $x, $y);
36908f248e4SAndreas Gohr        $def = $this->defRotate($def, $angle);
37008f248e4SAndreas Gohr        $def = $this->defTranslate($def, -$x, -$y);
37108f248e4SAndreas Gohr        return $def;
37208f248e4SAndreas Gohr    }
37308f248e4SAndreas Gohr
37408f248e4SAndreas Gohr    /**
37508f248e4SAndreas Gohr     * Applies a scale transformation to definition
37608f248e4SAndreas Gohr     * @param string $def definition
37708f248e4SAndreas Gohr     * @param integer $x
37808f248e4SAndreas Gohr     * @param integer $y
37908f248e4SAndreas Gohr     * @return string
38008f248e4SAndreas Gohr     */
38118622736SAndreas Gohr    public function defScale($def, $x = 1, $y = 1)
38218622736SAndreas Gohr    {
38309b1e97eSAndreas Gohr        return $this->defApplyMatrix($def, [$x, 0, 0, $y, 0, 0]);
38408f248e4SAndreas Gohr    }
38508f248e4SAndreas Gohr
38608f248e4SAndreas Gohr    /**
38708f248e4SAndreas Gohr     * Calculates the new definition with the matrix applied
38808f248e4SAndreas Gohr     * @param string $def
38908f248e4SAndreas Gohr     * @param array $matrix
39008f248e4SAndreas Gohr     * @return string
39108f248e4SAndreas Gohr     */
39218622736SAndreas Gohr    public function defApplyMatrix($def, $matrix)
39318622736SAndreas Gohr    {
39408f248e4SAndreas Gohr
39508f248e4SAndreas Gohr        // if there are several shapes in this definition, do the operation for each
39608f248e4SAndreas Gohr        preg_match_all('/M[^zZ]*[zZ]/', $def, $shapes);
39708f248e4SAndreas Gohr        $shapes = $shapes[0];
39808f248e4SAndreas Gohr        if (count($shapes) > 1) {
39918622736SAndreas Gohr            foreach ($shapes as &$shape) {
40008f248e4SAndreas Gohr                $shape = $this->defApplyMatrix($shape, $matrix);
40118622736SAndreas Gohr            }
40208f248e4SAndreas Gohr            return implode(' ', $shapes);
40308f248e4SAndreas Gohr        }
40408f248e4SAndreas Gohr
40508f248e4SAndreas Gohr        preg_match_all('/[a-zA-Z]+[^a-zA-Z]*/', $def, $instructions);
40608f248e4SAndreas Gohr        $instructions = $instructions[0];
40708f248e4SAndreas Gohr        foreach ($instructions as &$instruction) {
40808f248e4SAndreas Gohr            $i = preg_replace('/[^a-zA-Z]*/', '', $instruction);
40908f248e4SAndreas Gohr            preg_match_all('/\-?[0-9\.]+/', $instruction, $coords);
41008f248e4SAndreas Gohr            $coords = $coords[0];
41108f248e4SAndreas Gohr
41208f248e4SAndreas Gohr            if (empty($coords)) {
41308f248e4SAndreas Gohr                continue;
41408f248e4SAndreas Gohr            }
41508f248e4SAndreas Gohr
41609b1e97eSAndreas Gohr            $new_coords = [];
41708f248e4SAndreas Gohr            while (count($coords) > 0) {
41808f248e4SAndreas Gohr                // do the matrix calculation stuff
41909b1e97eSAndreas Gohr                [$a, $b, $c, $d, $e, $f] = $matrix;
42008f248e4SAndreas Gohr
42108f248e4SAndreas Gohr                // exception for relative instruction
42208f248e4SAndreas Gohr                if (preg_match('/[a-z]/', $i)) {
42308f248e4SAndreas Gohr                    $e = 0;
42408f248e4SAndreas Gohr                    $f = 0;
42508f248e4SAndreas Gohr                }
42608f248e4SAndreas Gohr
42708f248e4SAndreas Gohr                // convert horizontal lineto (relative)
42808f248e4SAndreas Gohr                if ($i == 'h') {
42908f248e4SAndreas Gohr                    $i = 'l';
43009b1e97eSAndreas Gohr                    $x = (float) array_shift($coords);
43108f248e4SAndreas Gohr                    $y = 0;
43208f248e4SAndreas Gohr
43308f248e4SAndreas Gohr                    // add new point's coordinates
43409b1e97eSAndreas Gohr                    $current_point = [$a * $x + $e, $b * $x + $f];
43509b1e97eSAndreas Gohr                    $new_coords = [...$new_coords, ...$current_point];
436*b15da4f2SAndreas Gohr                } elseif ($i == 'v') {
437*b15da4f2SAndreas Gohr                    // convert vertical lineto (relative)
43808f248e4SAndreas Gohr                    $i = 'l';
43908f248e4SAndreas Gohr                    $x = 0;
44009b1e97eSAndreas Gohr                    $y = (float) array_shift($coords);
44108f248e4SAndreas Gohr
44208f248e4SAndreas Gohr                    // add new point's coordinates
44309b1e97eSAndreas Gohr                    $current_point = [$c * $y + $e, $d * $y + $f];
44409b1e97eSAndreas Gohr                    $new_coords = [...$new_coords, ...$current_point];
445*b15da4f2SAndreas Gohr                } elseif ($i == 'q') {
446*b15da4f2SAndreas Gohr                    // convert quadratic bezier curve (relative)
44709b1e97eSAndreas Gohr                    $x = (float) array_shift($coords);
44809b1e97eSAndreas Gohr                    $y = (float) array_shift($coords);
44908f248e4SAndreas Gohr
45008f248e4SAndreas Gohr                    // add new point's coordinates
45109b1e97eSAndreas Gohr                    $current_point = [$a * $x + $c * $y + $e, $b * $x + $d * $y + $f];
45209b1e97eSAndreas Gohr                    $new_coords = [...$new_coords, ...$current_point];
45308f248e4SAndreas Gohr
45408f248e4SAndreas Gohr                    // same for 2nd point
45509b1e97eSAndreas Gohr                    $x = (float) array_shift($coords);
45609b1e97eSAndreas Gohr                    $y = (float) array_shift($coords);
45708f248e4SAndreas Gohr
45808f248e4SAndreas Gohr                    // add new point's coordinates
45909b1e97eSAndreas Gohr                    $current_point = [$a * $x + $c * $y + $e, $b * $x + $d * $y + $f];
46008f248e4SAndreas Gohr                    $new_coords = array_merge($new_coords, $current_point);
461*b15da4f2SAndreas Gohr                } else {
46208f248e4SAndreas Gohr                    // every other commands
46308f248e4SAndreas Gohr                    // @TODO: handle 'a,c,s' (elliptic arc curve) commands
46408f248e4SAndreas Gohr                    // cf. http://www.w3.org/TR/SVG/paths.html#PathDataCurveCommands
465*b15da4f2SAndreas Gohr
46609b1e97eSAndreas Gohr                    $x = (float) array_shift($coords);
46709b1e97eSAndreas Gohr                    $y = (float) array_shift($coords);
46808f248e4SAndreas Gohr
46908f248e4SAndreas Gohr                    // add new point's coordinates
47009b1e97eSAndreas Gohr                    $current_point = [$a * $x + $c * $y + $e, $b * $x + $d * $y + $f];
47109b1e97eSAndreas Gohr                    $new_coords = [...$new_coords, ...$current_point];
47208f248e4SAndreas Gohr                }
47308f248e4SAndreas Gohr            }
47408f248e4SAndreas Gohr
47508f248e4SAndreas Gohr            $instruction = $i . implode(',', $new_coords);
47608f248e4SAndreas Gohr
47708f248e4SAndreas Gohr            // remove useless commas
47808f248e4SAndreas Gohr            $instruction = preg_replace('/,\-/', '-', $instruction);
47908f248e4SAndreas Gohr        }
48008f248e4SAndreas Gohr
48108f248e4SAndreas Gohr        return implode('', $instructions);
48208f248e4SAndreas Gohr    }
48308f248e4SAndreas Gohr
48408f248e4SAndreas Gohr
48508f248e4SAndreas Gohr
48608f248e4SAndreas Gohr    /**
48708f248e4SAndreas Gohr     *
48808f248e4SAndreas Gohr     * Short-hand methods
48908f248e4SAndreas Gohr     *
49008f248e4SAndreas Gohr     */
49108f248e4SAndreas Gohr
49208f248e4SAndreas Gohr    /**
49308f248e4SAndreas Gohr     * Return full SVG XML
49408f248e4SAndreas Gohr     * @return string
49508f248e4SAndreas Gohr     */
49618622736SAndreas Gohr    public function asXML()
49718622736SAndreas Gohr    {
49808f248e4SAndreas Gohr        return $this->svg->asXML();
49908f248e4SAndreas Gohr    }
50008f248e4SAndreas Gohr
50108f248e4SAndreas Gohr    /**
50208f248e4SAndreas Gohr     * Adds an attribute to the SVG
50308f248e4SAndreas Gohr     * @param string $key
50408f248e4SAndreas Gohr     * @param string $value
50508f248e4SAndreas Gohr     */
50618622736SAndreas Gohr    public function addAttribute($key, $value)
50718622736SAndreas Gohr    {
50808f248e4SAndreas Gohr        return $this->svg->addAttribute($key, $value);
50908f248e4SAndreas Gohr    }
51008f248e4SAndreas Gohr}
511