svgwrite
svgwrite copied to clipboard
documentation: use of svg foreignObject
Please provide an example of how to use svg foreignObject.
This is not supported by svgwrite.
However, I was able to do it with some monkey-patching:
class ForeignObject(svgwrite.base.BaseElement, svgwrite.mixins.Transform, svgwrite.container.Presentation):
elementname = 'foreignObject'
def __init__(self, obj, **extra):
super().__init__(**extra)
self.obj = obj
def get_xml(self):
xml = super().get_xml()
xml.append(svgwrite.etree.etree.fromstring(self.obj))
return xml
svgwrite.elementfactory.factoryelements['foreignObject'] = ForeignObject
Example:
d.add(ForeignObject(x=0, y=0, width=50, height=50, obj=f'<div xmlns="http://www.w3.org/1999/xhtml">This is some really <em>long</em> HTML text</div>'))
This could probably be added to svgwrite relatively easily.
Looking forward for a "non monkey patch" solution of this.
Foreign object are specially useful for text wrapping since textArea is not supported almost anywhere.
Created a fork with this monkey-patch - https://github.com/Andrej730/svgwrite