yfede
May 14, 2015, 11:04am
1
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
jules
May 15, 2015, 10:08am
2
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
yfede
May 19, 2015, 10:00am
4
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);
}
}
jules
May 19, 2015, 10:06am
5
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.
jules
May 21, 2015, 9:26am
6
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);
}
yfede
May 21, 2015, 2:44pm
7
that could do, but.. out of curiosity, why not leave it in parseSubElements()?
jules
May 21, 2015, 2:52pm
8
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)
yfede
May 21, 2015, 4:30pm
9
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