@@ -113,6 +113,18 @@ def _python_to_svg_str(val):
113
113
else :
114
114
# Common case
115
115
return ' ' .join ([_python_to_svg_str (v ) for v in val ])
116
+ try :
117
+ # Simple Inkscape Scripting objects are converted to a string of
118
+ # the form "url(#id)".
119
+ return val .get_inkex_object ().get_id (as_url = 2 )
120
+ except AttributeError :
121
+ pass
122
+ try :
123
+ # inkex objects also are converted to a string of the form
124
+ # "url(#id)".
125
+ return val .get_id (as_url = 2 )
126
+ except AttributeError :
127
+ pass
116
128
return str (val ) # Everything else is converted to a string as usual.
117
129
118
130
@@ -369,6 +381,18 @@ def svg(self, xmlns=False, pretty_print=False):
369
381
return obj .tostring ().decode ('utf-8' )
370
382
371
383
384
+ class GenericReprMixin ():
385
+ '''Provide a generic __repr__ method for Simple Inkscape Scripting
386
+ classes that don't define a more customized __repr__.'''
387
+
388
+ def __repr__ (self ):
389
+ iobj = self .get_inkex_object ()
390
+ return '<%s %s %s>' % \
391
+ (self .__class__ .__name__ ,
392
+ iobj .TAG ,
393
+ iobj .get_id (1 ))
394
+
395
+
372
396
class SimpleObject (SVGOutputMixin ):
373
397
'Encapsulate an Inkscape object and additional metadata.'
374
398
@@ -428,11 +452,15 @@ def __init__(self, obj, transform, conn_avoid, clip_path_obj, mask_obj,
428
452
_simple_top .svg_attach .append (obj )
429
453
self .parent = None
430
454
431
- def __str__ (self ):
432
- '''Return the object as a string of the form "url(#id)". This
433
- enables the object to be used as a value in style key=value
434
- arguments such as shape_inside.'''
435
- return self ._inkscape_obj .get_id (as_url = 2 )
455
+ def __repr__ (self ):
456
+ 'Return a unique description of the object as a string.'
457
+ iobj = self .get_inkex_object ()
458
+ bbox = iobj .bounding_box ()
459
+ return '<%s %s (%s) %s>' % \
460
+ (self .__class__ .__name__ ,
461
+ iobj .TAG ,
462
+ bbox .center ,
463
+ iobj .get_id (1 ))
436
464
437
465
def __eq__ (self , other ):
438
466
'''Two SimpleObjects are equal if they encapsulate the same
@@ -1512,7 +1540,7 @@ def __init__(self, obj, transform, conn_avoid, clip_path_obj, mask_obj,
1512
1540
self ._self_type = 'hyperlink'
1513
1541
1514
1542
1515
- class SimpleFilter (SVGOutputMixin ):
1543
+ class SimpleFilter (SVGOutputMixin , GenericReprMixin ):
1516
1544
'Represent an SVG filter effect.'
1517
1545
1518
1546
def __init__ (self , name = None , pt1 = None , pt2 = None , filter_units = None ,
@@ -1612,7 +1640,7 @@ def add(self, ftype, **kw_args):
1612
1640
return self .SimpleFilterPrimitive (self , 'fe' + ftype , ** kw_args )
1613
1641
1614
1642
1615
- class SimpleGradient (SVGOutputMixin ):
1643
+ class SimpleGradient (SVGOutputMixin , GenericReprMixin ):
1616
1644
'Virtual base class for an SVG linear or radial gradient pattern.'
1617
1645
1618
1646
# Map Inkscape repetition names to SVG names.
@@ -1708,7 +1736,7 @@ def __init__(self, center=None, radius=None, focus=None, fr=None,
1708
1736
self .grad = grad
1709
1737
1710
1738
1711
- class SimplePathEffect (SVGOutputMixin ):
1739
+ class SimplePathEffect (SVGOutputMixin , GenericReprMixin ):
1712
1740
'Represent an Inkscape live path effect.'
1713
1741
1714
1742
def __init__ (self , effect , ** kwargs ):
@@ -1751,6 +1779,15 @@ def get_inkex_object(self):
1751
1779
"Return the guide's underlying inkex object."
1752
1780
return self ._inkscape_obj
1753
1781
1782
+ def __repr__ (self ):
1783
+ 'Return a unique description of the guide as a string.'
1784
+ iobj = self .get_inkex_object ()
1785
+ return '<%s %s %s %s>' % \
1786
+ (self .__class__ .__name__ ,
1787
+ iobj .TAG ,
1788
+ self ._pos ,
1789
+ iobj .get_id (1 ))
1790
+
1754
1791
def _move_to_wrapper (self , pos , angle ):
1755
1792
"Wrap inkex's move_to with a coordinate transformation."
1756
1793
global _simple_top
@@ -1840,7 +1877,7 @@ def _from_inkex_object(self, iobj):
1840
1877
return SimpleGuide (pos , angle , _inkex_object = iobj )
1841
1878
1842
1879
1843
- class SimpleCanvas :
1880
+ class SimpleCanvas () :
1844
1881
'Get and set the canvas size and viewbox.'
1845
1882
1846
1883
def __init__ (self , svg_root ):
@@ -2031,7 +2068,7 @@ def resize_to_content(self, objs=None):
2031
2068
self .true_height = '%.10g%s' % (tht , uht )
2032
2069
2033
2070
2034
- class SimplePage (SVGOutputMixin ):
2071
+ class SimplePage (SVGOutputMixin , GenericReprMixin ):
2035
2072
'Represent an Inkscape page.'
2036
2073
2037
2074
def __init__ (self , number , name = None , pos = None , size = None , iobj = None ):
@@ -2112,7 +2149,7 @@ class RectangularForeignObject(inkex.ForeignObject,
2112
2149
pass
2113
2150
2114
2151
2115
- class SimpleMetadata :
2152
+ class SimpleMetadata ( GenericReprMixin ) :
2116
2153
'Provide a simple interface to document metadata.'
2117
2154
2118
2155
def __init__ (self ):
@@ -2292,12 +2329,19 @@ def __init__(self):
2292
2329
}
2293
2330
self ._licenses ['Open Font License' ] = self ._licenses ['OFL' ]
2294
2331
2332
+ def get_inkex_object (self ):
2333
+ '''Return the SimpleMetadata's underlying inkex object.
2334
+ Regrettably, this exhibits the side effect of creating a
2335
+ <metadata> tag if one does not already exist (tested with
2336
+ Inkscape 1.3.2.'''
2337
+ global _simple_top
2338
+ return _simple_top .svg_root .metadata
2339
+
2295
2340
def _search_hierarchy (self , * args ):
2296
2341
'''Search an XML hierarchy given tuples of (namespace, tag).
2297
2342
<rdf:RDF> is the implicit first element of the hierarchy. This
2298
2343
method returns the final node.'''
2299
- global _simple_top
2300
- elt = _simple_top .svg_root .metadata
2344
+ elt = self .get_inkex_object ()
2301
2345
for ns_tag in [(self .rdf , 'RDF' )] + list (args ):
2302
2346
child = elt .find ('./{%s}%s' % ns_tag )
2303
2347
if child is None :
@@ -2309,8 +2353,7 @@ def _create_hierarchy(self, *args):
2309
2353
'''Create an XML hierarchy given tuples of (namespace, tag).
2310
2354
<rdf:RDF> is the implicit first element of the hierarchy. This
2311
2355
method returns the final node.'''
2312
- global _simple_top
2313
- elt = _simple_top .svg_root .metadata
2356
+ elt = self .get_inkex_object ()
2314
2357
for ns_tag in [(self .rdf , 'RDF' )] + list (args ):
2315
2358
child = elt .find ('./{%s}%s' % ns_tag )
2316
2359
if child is None :
0 commit comments