I'm writing a plugin to facilitate creating SVG symbols for use in Twig. For instance, this could be an desired HTML output:
<!DOCTYPE html>
<html>
<head>
<title>SVG Symbols</title>
<style>
body { margin: 20px; }
.svg-large { width: 500px; fill: yellow;}
.svg-small { width: 200px; fill: red; }
.svg-tiny { width: 100px; fill: #888; }
</style>
</head>
<body xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Define SVG symbols in the document. -->
<svg style="display:none;">
<symbol id="scary-smiley">
<circle cx="10" cy="10" r="9.5" stroke-width="1"
stroke="black" />
<circle cx="6" cy="7" r="1.5" fill="black"/>
<circle cx="14" cy="7" r="1.5" fill="black"/>
<image xlink:href="https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Teeth_by_David_Shankbone.jpg/320px-Teeth_by_David_Shankbone.jpg"
width="10" height="5.2" x="5" y="11"/>
</symbol>
</svg>
<h1>SVG Example</h1>
<!--
Then you can use the symbols as often as you want.
Note that the CSS above sets the width only, the height
scales accordingly. Firefox and Chrome (at least) require
the viewbox to be set on the referencing SVG element for
this to work as expected.
-->
<svg class="svg-large" viewBox="0 0 20 20">
<use xlink:href="#scary-smiley"/>
</svg>
<svg class="svg-small" viewBox="0 0 20 20">
<use xlink:href="#scary-smiley"/>
</svg>
<svg class="svg-tiny" viewBox="0 0 20 20">
<use xlink:href="#scary-smiley"/>
</svg>
</body>
</html>
(Note the image element inside the SVG!)
My plugin tries to mimic Grav's asset manager, so I can do:
{# Add an SVG file as an asset. #}
{% do add_svg_symbol('theme://images/scary-smiley.svg', 'scary-smiley') %}
{# Insert it all as symbols wrapped in <svg style="display: none"> as above. #}
{{ svg_symbols() }}
{# Use them in the document. #}
{{ use_svg_symbol('scary-smiley', 'svg-large') }}
Most of it is done. What's missing is properly handling the URL pointing to the SVG file. I would like to use URLs like theme://image/my-file.svg
The plugin then needs to a) resolve that to the file's path (so it can process the SVG) and b) create an URL like http://user/themes/<my theme>/images/ from that (so it can replace xlink:href URLs inside image tags in the SVG.
I'm not all that good with PHP (and PHP always seems to do what I expect the least). I was wondering whether I should try extending the Assets class?
Otherwise, are there any library functions to facilitate this? The Assets class has a lot of code for dealing with this and I'd rather not just copy and paste it ...