14
14
use Svg \Tag \Circle ;
15
15
use Svg \Tag \Ellipse ;
16
16
use Svg \Tag \Group ;
17
+ use Svg \Tag \ClipPath ;
17
18
use Svg \Tag \Image ;
18
19
use Svg \Tag \Line ;
19
20
use Svg \Tag \LinearGradient ;
23
24
use Svg \Tag \Rect ;
24
25
use Svg \Tag \Stop ;
25
26
use Svg \Tag \Text ;
27
+ use Svg \Tag \StyleTag ;
28
+ use Svg \Tag \UseTag ;
26
29
27
30
class Document extends AbstractTag
28
31
{
29
32
protected $ filename ;
30
- protected $ inDefs = false ;
33
+ public $ inDefs = false ;
31
34
32
35
protected $ x ;
33
36
protected $ y ;
@@ -50,6 +53,9 @@ class Document extends AbstractTag
50
53
/** @var AbstractTag[] */
51
54
protected $ defs = array ();
52
55
56
+ /** @var \Sabberworm\CSS\CSSList\Document[] */
57
+ protected $ styleSheets = array ();
58
+
53
59
public function loadFile ($ filename )
54
60
{
55
61
$ this ->filename = $ filename ;
@@ -132,12 +138,12 @@ function ($parser, $name) {}
132
138
public function handleSizeAttributes ($ attributes ){
133
139
if ($ this ->width === null ) {
134
140
if (isset ($ attributes ["width " ])) {
135
- $ width = ( int ) $ attributes ["width " ];
141
+ $ width = Style:: convertSize ( $ attributes ["width " ], 400 ) ;
136
142
$ this ->width = $ width ;
137
143
}
138
144
139
145
if (isset ($ attributes ["height " ])) {
140
- $ height = ( int ) $ attributes ["height " ];
146
+ $ height = Style:: convertSize ( $ attributes ["height " ], 300 ) ;
141
147
$ this ->height = $ height ;
142
148
}
143
149
@@ -166,17 +172,35 @@ public function handleSizeAttributes($attributes){
166
172
);
167
173
}
168
174
169
- protected function getDocument (){
175
+ public function getDocument (){
170
176
return $ this ;
171
177
}
172
178
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 )
174
198
{
175
199
$ surface = $ this ->getSurface ();
176
200
177
201
$ style = new DefaultStyle ();
178
202
$ style ->inherit ($ this );
179
- $ style ->fromAttributes ($ attribs );
203
+ $ style ->fromAttributes ($ attributes );
180
204
181
205
$ this ->setStyle ($ style );
182
206
@@ -211,6 +235,12 @@ protected function svgOffset($attributes)
211
235
$ this ->handleSizeAttributes ($ attributes );
212
236
}
213
237
238
+ public function getDef ($ id ) {
239
+ $ id = ltrim ($ id , "# " );
240
+
241
+ return isset ($ this ->defs [$ id ]) ? $ this ->defs [$ id ] : null ;
242
+ }
243
+
214
244
private function _tagStart ($ parser , $ name , $ attributes )
215
245
{
216
246
$ this ->x = 0 ;
@@ -227,7 +257,7 @@ private function _tagStart($parser, $name, $attributes)
227
257
228
258
case 'svg ' :
229
259
if (count ($ this ->attributes )) {
230
- $ tag = new Group ($ this );
260
+ $ tag = new Group ($ this , $ name );
231
261
}
232
262
else {
233
263
$ tag = $ this ;
@@ -236,72 +266,90 @@ private function _tagStart($parser, $name, $attributes)
236
266
break ;
237
267
238
268
case 'path ' :
239
- $ tag = new Path ($ this );
269
+ $ tag = new Path ($ this , $ name );
240
270
break ;
241
271
242
272
case 'rect ' :
243
- $ tag = new Rect ($ this );
273
+ $ tag = new Rect ($ this , $ name );
244
274
break ;
245
275
246
276
case 'circle ' :
247
- $ tag = new Circle ($ this );
277
+ $ tag = new Circle ($ this , $ name );
248
278
break ;
249
279
250
280
case 'ellipse ' :
251
- $ tag = new Ellipse ($ this );
281
+ $ tag = new Ellipse ($ this , $ name );
252
282
break ;
253
283
254
284
case 'image ' :
255
- $ tag = new Image ($ this );
285
+ $ tag = new Image ($ this , $ name );
256
286
break ;
257
287
258
288
case 'line ' :
259
- $ tag = new Line ($ this );
289
+ $ tag = new Line ($ this , $ name );
260
290
break ;
261
291
262
292
case 'polyline ' :
263
- $ tag = new Polyline ($ this );
293
+ $ tag = new Polyline ($ this , $ name );
264
294
break ;
265
295
266
296
case 'polygon ' :
267
- $ tag = new Polygon ($ this );
297
+ $ tag = new Polygon ($ this , $ name );
268
298
break ;
269
299
270
300
case 'lineargradient ' :
271
- $ tag = new LinearGradient ($ this );
301
+ $ tag = new LinearGradient ($ this , $ name );
272
302
break ;
273
303
274
304
case 'radialgradient ' :
275
- $ tag = new LinearGradient ($ this );
305
+ $ tag = new LinearGradient ($ this , $ name );
276
306
break ;
277
307
278
308
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 );
280
314
break ;
281
315
282
316
case 'a ' :
283
- $ tag = new Anchor ($ this );
317
+ $ tag = new Anchor ($ this , $ name );
284
318
break ;
285
319
286
320
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 );
288
331
break ;
289
332
290
333
case 'text ' :
291
- $ tag = new Text ($ this );
334
+ $ tag = new Text ($ this , $ name );
292
335
break ;
293
336
}
294
337
295
338
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 ;
299
341
}
300
342
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 ;
303
347
}
304
348
}
349
+
350
+ $ this ->stack [] = $ tag ;
351
+
352
+ $ tag ->handle ($ attributes );
305
353
} else {
306
354
echo "Unknown: ' $ name' \n" ;
307
355
}
@@ -311,7 +359,7 @@ function _charData($parser, $data)
311
359
{
312
360
$ stack_top = end ($ this ->stack );
313
361
314
- if ($ stack_top instanceof Text) {
362
+ if ($ stack_top instanceof Text || $ stack_top instanceof StyleTag ) {
315
363
$ stack_top ->appendText ($ data );
316
364
}
317
365
}
@@ -337,16 +385,18 @@ function _tagEnd($parser, $name)
337
385
case 'radialgradient ' :
338
386
case 'lineargradient ' :
339
387
case 'stop ' :
388
+ case 'style ' :
340
389
case 'text ' :
341
390
case 'g ' :
391
+ case 'symbol ' :
392
+ case 'clippath ' :
393
+ case 'use ' :
342
394
case 'a ' :
343
- if (!$ this ->inDefs ) {
344
- $ tag = array_pop ($ this ->stack );
345
- }
395
+ $ tag = array_pop ($ this ->stack );
346
396
break ;
347
397
}
348
398
349
- if ($ tag ) {
399
+ if (! $ this -> inDefs && $ tag ) {
350
400
$ tag ->handleEnd ();
351
401
}
352
402
}
0 commit comments