svgwrite icon indicating copy to clipboard operation
svgwrite copied to clipboard

documentation: use of svg foreignObject

Open viviparous opened this issue 5 years ago • 3 comments

Please provide an example of how to use svg foreignObject.

viviparous avatar Jan 11 '20 11:01 viviparous

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.

jotaf98 avatar Feb 29 '20 03:02 jotaf98

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.

cristianoccazinsp avatar May 26 '20 16:05 cristianoccazinsp

Created a fork with this monkey-patch - https://github.com/Andrej730/svgwrite

Andrej730 avatar May 02 '23 10:05 Andrej730