# Runtime SVG import

**URL:** <https://forum.juce.com/t/runtime-svg-import/23972>\
**Category:** General JUCE discussion\
**Created:** [August 30, 2017, 2:35pm UTC](https://forum.juce.com/t/runtime-svg-import/23972 "2017-08-30T14:35:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreasLAN](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@AndreasLAN](https://forum.juce.com/u/AndreasLAN)\
**Post date:** [August 30, 2017, 2:35pm UTC](https://forum.juce.com/t/runtime-svg-import/23972/1 "2017-08-30T14:35:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [August 30, 2017, 2:41pm UTC](https://forum.juce.com/t/runtime-svg-import/23972/2 "2017-08-30T14:41:48Z")

</div>

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

> <https://github.com/WeAreROLI/JUCE/blob/master/modules/juce_gui_basics/misc/juce_JUCESplashScreen.cpp#L254>

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.

---

<div class="post-metadata">

**Author:** ![AndreasLAN](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@AndreasLAN](https://forum.juce.com/u/AndreasLAN)\
**Post date:** [August 30, 2017, 3:04pm UTC](https://forum.juce.com/t/runtime-svg-import/23972/3 "2017-08-30T15:04:14Z")

</div>

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);
}
```

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [August 30, 2017, 4:24pm UTC](https://forum.juce.com/t/runtime-svg-import/23972/4 "2017-08-30T16:24:41Z")

</div>

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

`Drawable::createFromSVGFile (const File &svgFile)`

Am I missing something?

---

<div class="post-metadata">

**Author:** ![AndreasLAN](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@AndreasLAN](https://forum.juce.com/u/AndreasLAN)\
**Post date:** [August 30, 2017, 7:52pm UTC](https://forum.juce.com/t/runtime-svg-import/23972/5 "2017-08-30T19:52:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [August 31, 2017, 8:09am UTC](https://forum.juce.com/t/runtime-svg-import/23972/6 "2017-08-31T08:09:07Z")

</div>

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.
