Skip to content

Commit e30f8e9

Browse files
committed
Add basic support of stylesheets (classes and tage names) thanks to sabberworm/php-css-parser
Support display: none style (not inherited for now) Beggining of gradients support Support for <defs> Add a default SVG size
1 parent 3e8d23b commit e30f8e9

24 files changed

+521
-137
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
"psr-0": {
1515
"Svg\\": "src/"
1616
}
17+
},
18+
"require": {
19+
"sabberworm/php-css-parser": "6.0.*"
1720
}
1821
}

src/Svg/DefaultStyle.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class DefaultStyle extends Style
1212
{
1313
public $color = '';
1414
public $opacity = 1.0;
15+
public $display = 'inline';
1516

1617
public $fill = 'black';
1718
public $fillOpacity = 1.0;

src/Svg/Document.php

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Svg\Tag\Circle;
1515
use Svg\Tag\Ellipse;
1616
use Svg\Tag\Group;
17+
use Svg\Tag\ClipPath;
1718
use Svg\Tag\Image;
1819
use Svg\Tag\Line;
1920
use Svg\Tag\LinearGradient;
@@ -23,11 +24,13 @@
2324
use Svg\Tag\Rect;
2425
use Svg\Tag\Stop;
2526
use Svg\Tag\Text;
27+
use Svg\Tag\StyleTag;
28+
use Svg\Tag\UseTag;
2629

2730
class Document extends AbstractTag
2831
{
2932
protected $filename;
30-
protected $inDefs = false;
33+
public $inDefs = false;
3134

3235
protected $x;
3336
protected $y;
@@ -50,6 +53,9 @@ class Document extends AbstractTag
5053
/** @var AbstractTag[] */
5154
protected $defs = array();
5255

56+
/** @var \Sabberworm\CSS\CSSList\Document[] */
57+
protected $styleSheets = array();
58+
5359
public function loadFile($filename)
5460
{
5561
$this->filename = $filename;
@@ -132,12 +138,12 @@ function ($parser, $name) {}
132138
public function handleSizeAttributes($attributes){
133139
if ($this->width === null) {
134140
if (isset($attributes["width"])) {
135-
$width = (int)$attributes["width"];
141+
$width = Style::convertSize($attributes["width"], 400);
136142
$this->width = $width;
137143
}
138144

139145
if (isset($attributes["height"])) {
140-
$height = (int)$attributes["height"];
146+
$height = Style::convertSize($attributes["height"], 300);
141147
$this->height = $height;
142148
}
143149

@@ -166,17 +172,35 @@ public function handleSizeAttributes($attributes){
166172
);
167173
}
168174

169-
protected function getDocument(){
175+
public function getDocument(){
170176
return $this;
171177
}
172178

173-
protected function before($attribs)
179+
/**
180+
* Append a style sheet
181+
*
182+
* @param \Sabberworm\CSS\CSSList\Document $stylesheet
183+
*/
184+
public function appendStyleSheet($stylesheet) {
185+
$this->styleSheets[] = $stylesheet;
186+
}
187+
188+
/**
189+
* Get the document style sheets
190+
*
191+
* @return \Sabberworm\CSS\CSSList\Document[]
192+
*/
193+
public function getStyleSheets() {
194+
return $this->styleSheets;
195+
}
196+
197+
protected function before($attributes)
174198
{
175199
$surface = $this->getSurface();
176200

177201
$style = new DefaultStyle();
178202
$style->inherit($this);
179-
$style->fromAttributes($attribs);
203+
$style->fromAttributes($attributes);
180204

181205
$this->setStyle($style);
182206

@@ -211,6 +235,12 @@ protected function svgOffset($attributes)
211235
$this->handleSizeAttributes($attributes);
212236
}
213237

238+
public function getDef($id) {
239+
$id = ltrim($id, "#");
240+
241+
return isset($this->defs[$id]) ? $this->defs[$id] : null;
242+
}
243+
214244
private function _tagStart($parser, $name, $attributes)
215245
{
216246
$this->x = 0;
@@ -227,7 +257,7 @@ private function _tagStart($parser, $name, $attributes)
227257

228258
case 'svg':
229259
if (count($this->attributes)) {
230-
$tag = new Group($this);
260+
$tag = new Group($this, $name);
231261
}
232262
else {
233263
$tag = $this;
@@ -236,72 +266,90 @@ private function _tagStart($parser, $name, $attributes)
236266
break;
237267

238268
case 'path':
239-
$tag = new Path($this);
269+
$tag = new Path($this, $name);
240270
break;
241271

242272
case 'rect':
243-
$tag = new Rect($this);
273+
$tag = new Rect($this, $name);
244274
break;
245275

246276
case 'circle':
247-
$tag = new Circle($this);
277+
$tag = new Circle($this, $name);
248278
break;
249279

250280
case 'ellipse':
251-
$tag = new Ellipse($this);
281+
$tag = new Ellipse($this, $name);
252282
break;
253283

254284
case 'image':
255-
$tag = new Image($this);
285+
$tag = new Image($this, $name);
256286
break;
257287

258288
case 'line':
259-
$tag = new Line($this);
289+
$tag = new Line($this, $name);
260290
break;
261291

262292
case 'polyline':
263-
$tag = new Polyline($this);
293+
$tag = new Polyline($this, $name);
264294
break;
265295

266296
case 'polygon':
267-
$tag = new Polygon($this);
297+
$tag = new Polygon($this, $name);
268298
break;
269299

270300
case 'lineargradient':
271-
$tag = new LinearGradient($this);
301+
$tag = new LinearGradient($this, $name);
272302
break;
273303

274304
case 'radialgradient':
275-
$tag = new LinearGradient($this);
305+
$tag = new LinearGradient($this, $name);
276306
break;
277307

278308
case 'stop':
279-
$tag = new Stop($this);
309+
$tag = new Stop($this, $name);
310+
break;
311+
312+
case 'style':
313+
$tag = new StyleTag($this, $name);
280314
break;
281315

282316
case 'a':
283-
$tag = new Anchor($this);
317+
$tag = new Anchor($this, $name);
284318
break;
285319

286320
case 'g':
287-
$tag = new Group($this);
321+
case 'symbol':
322+
$tag = new Group($this, $name);
323+
break;
324+
325+
case 'clippath':
326+
$tag = new ClipPath($this, $name);
327+
break;
328+
329+
case 'use':
330+
$tag = new UseTag($this, $name);
288331
break;
289332

290333
case 'text':
291-
$tag = new Text($this);
334+
$tag = new Text($this, $name);
292335
break;
293336
}
294337

295338
if ($tag) {
296-
if (!$this->inDefs) {
297-
$this->stack[] = $tag;
298-
$tag->handle($attributes);
339+
if (isset($attributes["id"])) {
340+
$this->defs[$attributes["id"]] = $tag;
299341
}
300342
else {
301-
if (isset($attributes["id"])) {
302-
$this->defs[$attributes["id"]] = $tag;
343+
/** @var AbstractTag $top */
344+
$top = end($this->stack);
345+
if ($top && $top != $tag) {
346+
$top->children[] = $tag;
303347
}
304348
}
349+
350+
$this->stack[] = $tag;
351+
352+
$tag->handle($attributes);
305353
} else {
306354
echo "Unknown: '$name'\n";
307355
}
@@ -311,7 +359,7 @@ function _charData($parser, $data)
311359
{
312360
$stack_top = end($this->stack);
313361

314-
if ($stack_top instanceof Text) {
362+
if ($stack_top instanceof Text || $stack_top instanceof StyleTag) {
315363
$stack_top->appendText($data);
316364
}
317365
}
@@ -337,16 +385,18 @@ function _tagEnd($parser, $name)
337385
case 'radialgradient':
338386
case 'lineargradient':
339387
case 'stop':
388+
case 'style':
340389
case 'text':
341390
case 'g':
391+
case 'symbol':
392+
case 'clippath':
393+
case 'use':
342394
case 'a':
343-
if (!$this->inDefs) {
344-
$tag = array_pop($this->stack);
345-
}
395+
$tag = array_pop($this->stack);
346396
break;
347397
}
348398

349-
if ($tag) {
399+
if (!$this->inDefs && $tag) {
350400
$tag->handleEnd();
351401
}
352402
}

src/Svg/Gradient/Stop.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* @package php-svg-lib
4+
* @link http://github.com/PhenX/php-svg-lib
5+
* @author Fabien Ménager <[email protected]>
6+
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7+
*/
8+
9+
namespace Svg\Gradient;
10+
11+
class Stop
12+
{
13+
public $offset;
14+
public $color;
15+
public $opacity = 1.0;
16+
}

0 commit comments

Comments
 (0)