Runtime SVG import


#1

Is there a way to dynamically read SVG files from disk to produce Drawables in JUCE?


#2

We do something similar to this for the JUCE splash screen:

It’s not quite directly from disk as we’re storing the SVG content as a string, but it should point you in the right direction.


#3

Thank you. For anyone else interested:

Drawable* svgToDrawable(const std::string& filePath)
{
		std::string line, svgString;
		std::ifstream svgfile(filePath);
		if (svgfile.is_open())
		{
			while(std::getline(svgfile, line))
				svgString += line + "\n";
			myfile.close();
		}
		else
		{
			DBG("Unable to open file");
		}
		const char* svgData = svgString.c_str();

		ScopedPointer<XmlElement> svgXml(XmlDocument::parse(svgData));
		return Drawable::createFromSVG(*svgXml);
}

#4

That seems like a lot of code just to implement what’s already provided by

Drawable::createFromSVGFile (const File &svgFile)

Am I missing something?


#5

Ah I just saw this was added in a newer JUCE version than I was using.


#6

Also, you may greatly reduce your code by using the overload of XmlDocument::parse() that takes a File as its argument.
That one takes care of the reading from the file for you, essentially replacing the whole first part of your function.