vvvv/SVG not rendering text

3k Views Asked by At

I'm trying to use vvvv/SVG library (https://github.com/vvvv/SVG , current version) in my C# project. I've managed to do some simple drawing, however ended on strange behaviour when rendering text.

Following code adds text element to SvgDocument, but no text is rendered in bitmap.

        this.SvgDoc = new SvgDocument();
        this.SvgDoc.Width = new SvgUnit(400);
        this.SvgDoc.Height = new SvgUnit(400);

        SvgText txt = new SvgText("TEST");
        txt.X.Add(new SvgUnit(100));
        txt.Y.Add(new SvgUnit(100));
        //txt.FontFamily = "Arial";
        txt.FontSize = new SvgUnit(15);
        txt.Fill = new SvgColourServer(Color.Black);
        this.SvgDoc.Children.Add(txt);

        var bmp2 = this.SvgDoc.Draw();
        this.PBoxRes.Image = bmp2;

If I try to add eg. circle node, it renders ok. Text will not render until I store svg to string, parse it with SvgDocument.FromSvg() and render bitmap after that. That's working, however too slow with larger SVG and minor changes. Is there something I can do to make the text renderable imediatelly, without reparsing?

2

There are 2 best solutions below

1
On BEST ANSWER

try to add this:

SvgContentNode newContent = new SvgContentNode();
newContent.Content = "Text";
txt.Nodes.Add(newContent);
0
On

I stumbled across the same issue and it took me some time to figure it out. It seems that an SvgText needs children in order to be included in the XML of the resulting .svg file.

For me, including the text as a nested content node did the trick:

new SvgDocument
{
    Width = 100,
    Height = 100,
    FontSize = 10,
    Nodes =
    {
        new SvgText
        {
            Nodes = {new SvgContentNode {Content = "my text"}},
            X = {10},
            Y = {10}
        }
    }
}

I'm not sure if this is the best way to add text, but it creates the XML tag <text x="10" y="10">my text</text> in the resulting .svg file, which seems correct to me.