# SVG clipPath support

**URL:** <https://forum.juce.com/t/svg-clippath-support/4109>\
**Category:** General JUCE discussion\
**Created:** [July 20, 2009, 3:44pm UTC](https://forum.juce.com/t/svg-clippath-support/4109 "2009-07-20T15:44:28Z")\
**Posts on this page:** 16\
**Page:** 1

<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:** [July 20, 2009, 3:44pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/1 "2009-07-20T15:44:28Z")

</div>

I’ve noticed that the current implementation of the SVG parser does not support the clipPath tag.

Are there chances it will be supported in future?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 20, 2009, 5:12pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/2 "2009-07-20T17:12:14Z")

</div>

No immediate plans, but maybe one day. It’s actually a tricky one to implement though - clipping to paths is quite a tough problem to perform quickly.

---

<div class="post-metadata">

**Author:** ![jrlanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jrlanglois/32/21372_2.png) [@jrlanglois](https://forum.juce.com/u/jrlanglois)\
**Post date:** [February 15, 2012, 7:12pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/3 "2012-02-15T19:12:25Z")

</div>

Hey Jules,

Have there been any updates on this since 2009?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 15, 2012, 7:42pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/4 "2012-02-15T19:42:06Z")

</div>

The graphics classes do support that now, but no, I’ve not been back and updated the SVG classes to handle it yet.

---

<div class="post-metadata">

**Author:** ![yairadix](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairadix/32/1114_2.png) [@yairadix](https://forum.juce.com/u/yairadix)\
**Post date:** [December 30, 2015, 5:28pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/5 "2015-12-30T17:28:10Z")

</div>

Here's my new&nbsp;patches to add clipPath support to JUCE:

- add clipPath support to DrawableShape:&nbsp;https://github.com/soundradix/JUCE/commit/063a2bddd21bcfa46c8525b0be36a10f035e6954
- add clipPaths to SVG parser:&nbsp;https://github.com/soundradix/JUCE/commit/0cd01c33fa947f635458c5d226bf553459d4256a
- add \<defs\>/\<use\> support\*:&nbsp;https://github.com/soundradix/JUCE/commit/afbc104f31d5c1a3933c202bb82104bd4ca1dd4b

\* looks&nbsp;like in SVGs made in Adobe Illustrator,&nbsp;clipPaths always use \<use\> tags which refers to paths previously declared in \<defs\>. For my app to draw the SVGs created by our designer, clipPath support was meaningless without also supporting these

As usual, I hope to see these (code changes or the features implemented otherwise) added to JUCE so I won't have to maintain merges. Cheers!&nbsp;Yair

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 30, 2015, 6:57pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/6 "2015-12-30T18:57:54Z")

</div>

Thanks! Will have a look v. soon..

---

<div class="post-metadata">

**Author:** ![rory](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rory/32/534_2.png) [@rory](https://forum.juce.com/u/rory)\
**Post date:** [January 5, 2016, 3:46pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/7 "2016-01-05T15:46:13Z")

</div>

Thanks for posting these. I'm just returning to using SVGs and these will come in useful, I've&nbsp;no doubt. Can't wait to see these and the other SVG patches in the main tip soon. Another nice addition would be the ability to extract a given layer from an SVG.&nbsp;I imagine it would be trivial enough to do.&nbsp;

---

<div class="post-metadata">

**Author:** ![yairadix](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairadix/32/1114_2.png) [@yairadix](https://forum.juce.com/u/yairadix)\
**Post date:** [January 10, 2016, 2:51pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/8 "2016-01-10T14:51:31Z")

</div>

Btw, just refactored my changes to better conform SVGParser's style, using findElementForId etc.

Cheers, Yair

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 11, 2016, 12:50pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/9 "2016-01-11T12:50:04Z")

</div>

FYI I've just been having a look at this, but hit a whole lot more problems than I expected, so can't finish it now.. There's no sense in me implementing something that's not fully correct, and I don't have any more time right now to spend on it, so&nbsp;I'm going to commit what I've done so far (I've fixed the dash stuff at least, and got some of the basic code for parsing the clip paths in place), and will have to revisit it at some point later.

The main issue is that the clip-path attribute may be applied to lots of SVG elements, not just DrawablePath, so would require changes to all the classes, or maybe the Drawable base class.

Also, Drawables at the moment need to be able to save/load their state to a ValueTree, so it's not trivial to add features like dashes or extra paths without suporting their serialisation.. This is messy, and is actually a feature that&nbsp;we'll probably deprecate and remove in future, but it needs to be taken into account at the moment.

I fixed some other issues with your dash parser, which was ignoring coordinate types and some other things, but I had to remove your hack to workaround Adobe having zero-length lines because what you wrote would have given the path parser O(n\*n) complexity!&nbsp;Not sure what could be done as an alternative, and I didn't have an example to try, but maybe you could find a more appropriate&nbsp;place to handle that particular case.

---

<div class="post-metadata">

**Author:** ![yairadix](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairadix/32/1114_2.png) [@yairadix](https://forum.juce.com/u/yairadix)\
**Post date:** [January 14, 2016, 1:44pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/10 "2016-01-14T13:44:37Z")

</div>

Thanks for adding these!&nbsp;Now my diff from mainstream JUCE to support clipPaths is much smaller.

After merging with your changes, all still works good, except an assert triggers in XmlElement::hasTagName&nbsp;from&nbsp;applyClipPath, at&nbsp;xmlPath-\>hasTagNameIgnoringNamespace ("clippath"), because it seems like it should be "clipPath" rather than "clippath". &nbsp;see&nbsp;https://developer.mozilla.org/en-US/docs/Web/SVG/Element/clipPath

Cheers, Yair

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 15, 2016, 8:51am UTC](https://forum.juce.com/t/svg-clippath-support/4109/11 "2016-01-15T08:51:14Z")

</div>

Damn, I deliberately changed it to "clippath" because I was referring to this page:&nbsp;https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/clip-path

..but that&nbsp;page is obviously full of typos! Thanks, will change it back..

---

<div class="post-metadata">

**Author:** ![yairadix](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairadix/32/1114_2.png) [@yairadix](https://forum.juce.com/u/yairadix)\
**Post date:** [February 29, 2016, 5:01pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/12 "2016-02-29T17:01:54Z")

</div>

> The main issue is that the clip-path attribute may be applied to lots of SVG elements, not just DrawablePath, so would require changes to all the classes, or maybe the Drawable base class.

I'd like to implement this&nbsp;(mainly for masks, because my SVGs have masks applied on group elements) but I need a little bit of&nbsp;advice:

Suppose we'd add a clip-path and mask to the base class, how would it be used, specifically for&nbsp;DrawableComposite?

Iiuc, for a DrawableComposite, its paintEntireComponent will just invoke its child drawables's paint and those would not be affected by any clipping/masking of the parent.. Should they no longer be child components added to the parent via addAndMakeVisible etc??

Cheers, Yair

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 1, 2016, 9:38am UTC](https://forum.juce.com/t/svg-clippath-support/4109/13 "2016-03-01T09:38:40Z")

</div>

Yes.. that does make it tricky. I'd really like to undo the fact that Drawables are Components, but it'd probably break a lot of code.

---

<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:** [March 8, 2016, 11:08pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/14 "2016-03-08T23:08:00Z")

</div>

\*cough\* \*cough\* +1 to undo and deprecate Drawable as Components \*cough\* \*cough\*&nbsp;

Then, you could create a.. BlackboardComponent? CanvasComponent? where all the "new" Drawables (Paintables?) can be stitched together, and it will take care of keeping a reference to them and paint them at each of its paint() callback..

This would be an approach that takes the best of the two extremes: having Drawables as full fledged Components on one side, and having to paint them explicitly into the Graphics at each paint() call on the other side (which is as it was done before they were Components, for the new people reading this)

---

<div class="post-metadata">

**Author:** ![yairadix](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairadix/32/1114_2.png) [@yairadix](https://forum.juce.com/u/yairadix)\
**Post date:** [March 13, 2016, 4:19pm UTC](https://forum.juce.com/t/svg-clippath-support/4109/15 "2016-03-13T16:19:10Z")

</div>

Btw what if clipping and masking were features that are available to all components, like "transform" is currently? It would probably also allow for some cool UIs and transition effects :)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 14, 2016, 11:05am UTC](https://forum.juce.com/t/svg-clippath-support/4109/16 "2016-03-14T11:05:32Z")

</div>

Definitely an interesting idea! Would need some careful refactoring in the rendering classes, but it could indeed create some good effects.
