Friday, April 19, 2013

SVG - Gradient in XSL with FOP

graidient.svg

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <defs>
    <linearGradient id="linearGradient" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:green" />
      <stop offset="50%" style="stop-color:yellow" />
      <stop offset="100%" style="stop-color:red" />
    </linearGradient>
  </defs>
</svg>  

gradient.xsl

<fo:instream-foreign-object>
<svg:svg version="1.1" preserveAspectRatio="xMinYMin"               xmlns:svg="http://www.w3.org/2000/svg">

<svg:rect x="0" y="0" height="2cm"
<svg:rect x="0" y="100" width="{$width}" height="2cm" style="fill:url('gradient.svg#linearGradient'); stroke: none;" />

</svg:svg>
</fo:instream-foreign-object>

Result


 

Reference


I have problems with SVG referring to gradients etc. using "uri(#stuff)".
I get a MalformedURLException.

This is really a "resolving relative URI" problem with some twists. The problem is that the #stuff URL fragment identifier is resolved within the current SVG document. So the reference must be valid within the XML subset and it cannot reference other SVG documents in the same XML file. Some options to try:
  • Put the SVG into a separate file and use it with fo:external-graphics.
  • Use a separate SVG file which contains only the gradient (and perhaps other SVG stuff you want to reference) and point an absolute URL to it: fill="url(file:///c:/refstuff/grad.svg#PurpleToWhite)".
  • Same as above but use a relative URL: fill="url(grad.svg#PurpleToWhite)". This may be easier to deploy.
  • Make sure that the reference is valid in the current SVG document.
In any case, the referenced stuff has to be pointed to by an URL. It doesn't necessarily have to be a file URL, HTTP should also work. Also, expect a performance hit in all cases, because another XML file has to be retrieved and parsed.
Ultimately, both FOP and especially Batik should be fixed to make your code work as expected, but this will not only take some time but also some effort by a standard committee in order to make the semantics of this kind of references in embedded SVG clearer.


Source: http://xmlgraphics.apache.org/fop/faq.html 

0 comments:

Post a Comment