I needed a way to add a “stroke” (outline) and a drop effect to a transparent PNG image based on its alpha mask, and the only solution I could find was to use custom SVG filters. (Note: The web application for which I need these effects is for my personal use, so it’s normal that this solution is not compatible with older browsers. Moving ...)
I had never used SVG before, but it was pretty simple to create custom hatch and drop filters. Unfortunately, I could not find a way to create a combined effect without actually copying and pasting the filters into a new one, as shown in the code below:
<svg width="0" height="0" xmlns="http://www.w3.org/2000/svg">
<filter id="drop-shadow">
<feGaussianBlur in="SourceAlpha" stdDeviation="4" />
<feOffset result="m_offsetBlurred" dx="12" dy="12" />
<feFlood result="m_floodTrans50" flood-color="rgba(0,0,0,0.5)" />
<feComposite result="m_offsetBlurredTrans50" in="m_floodTrans50" in2="m_offsetBlurred" operator="in" />
<feMerge>
<feMergeNode in="m_offsetBlurredTrans50" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
<filter id="outer-stroke">
<feFlood result="m_floodRect" flood-color="black" />
<feMorphology result="m_expandedMask" in="SourceAlpha" operator="dilate" radius="1" />
<feComposite result="m_expandedColored" in="m_floodRect" in2="m_expandedMask" operator="in" />
<feBlend in="SourceGraphic" in2="m_expandedColored" mode="normal" />
</filter>
<filter id="outer-stroke-drop-shadow">
<feFlood result="m_floodRect" flood-color="black" />
<feMorphology result="m_expandedMask" in="SourceAlpha" operator="dilate" radius="1" />
<feComposite result="m_expandedColored" in="m_floodRect" in2="m_expandedMask" operator="in" />
<feBlend result="m_stroked" in="SourceGraphic" in2="m_expandedColored" mode="normal" />
<feGaussianBlur result="m_blurred" in="SourceAlpha" stdDeviation="4" />
<feOffset result="m_offsetBlurred" in="m_blurred" dx="12" dy="12" />
<feFlood result="m_floodTrans50" flood-color="rgba(0,0,0,0.5)" />
<feComposite result="m_offsetBlurredTrans50" in="m_floodTrans50" in2="m_offsetBlurred" operator="in" />
<feMerge>
<feMergeNode in="m_offsetBlurredTrans50" />
<feMergeNode in="m_stroked" />
</feMerge>
</filter>
</svg>
<style>
.fx_drop_shadow { filter: url('#drop-shadow'); }
.fx_outer_stroke { filter: url('#outer-stroke'); }
.fx_outer_stroke_drop_shadow { filter: url('#outer-stroke-drop-shadow'); }
</style>
<div>
<img src="gfx/odd_shape.png" />
<img src="gfx/odd_shape.png" class="fx_drop_shadow" />
<img src="gfx/odd_shape.png" class="fx_outer_stroke" />
<img src="gfx/odd_shape.png" class="fx_outer_stroke_drop_shadow" />
</div>
Here's how the code above will display in an HTML5 document:

And here is the original PNG graphics (odd_shape.png):

Question 1: . How can I reuse the first 2 filters ( drop-shadowand outer-stroke), so I can just apply them in the combined filter ( outer-stroke-drop-shadow) instead of copying and pasting them.
Question 2: Is it possible to parameterize custom filters so that I can specify parameters such as stroke color or shadow transparency? This would make them even more reusable.
.