SVG visibility support

I have some SVGs generated with Inkscape where some elements have been hidden using the following attribute

display="none"

I have noticed that when those SVGs are loaded with JUCE, those elements aren't hidden as expected.

Would it be possible to add support for this to the current SVG parser?

Thanks in advance

Sounds like a good request - do you know if all elements support this, or just some of them?

https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/display

In the mean time, I have hacked my own quick-and-dirty solution this way in juce_SVGParser.cpp, see if it can be of any help/inspiration

    void parseSubElements (const XmlPath& xml, DrawableComposite& parentDrawable)
    {
        forEachXmlChildElement (*xml, e)
        {
            Drawable* drawable = parseSubElement (xml.getChild (e));
            if (drawable == nullptr)
                continue;
            drawable->setVisible (e->getStringAttribute ("display") != "none");
            parentDrawable.addChildComponent (drawable);
        }
    }

Thanks, will take a look!

BTW, I'd really recommend that you get into the habit of using the "if (Object* x = ..." pattern to check for nullptr, e.g.


        forEachXmlChildElement (*xml, e)
        {
            if (Drawable* drawable = parseSubElement (xml.getChild (e)))

            {
                drawable->setVisible (e->getStringAttribute ("display") != "none");
                parentDrawable.addChildComponent (drawable);

            }
        }

We push this pattern really hard in our internal coding standards, because it makes it impossible to dereference a null pointer.

Ok.. I don't have an example file to test, but presumably this simple change would work?

 

    static void setDrawableID (Drawable& d, const XmlPath& xml)
    {
        String compID (xml->getStringAttribute ("id"));
        d.setName (compID);
        d.setComponentID (compID);

        if (xml->getStringAttribute ("display") == "none")
            d.setVisible (false);
    }

that could do, but.. out of curiosity, why not leave it in parseSubElements()?

Just seemed more sensible to put it in that method (which I'd probably choose to rename as something like "addCommonAttributes" if this change was made)

fair enough, although I would name it setCommonAttributes(). After all, they all are set..() operations of aspects that are already there :)

Anyway, I'm ok with the change, I can't see anything wrong with it