pythonsvgcairolibrsvgrsvg

Differences while rendering SVG with librsvg and Python


Depending upon rendering an SVG either as a whole document or as a single element shows differences in rendering.

I created a simple SVG graphic using Inkscape and want to render it using Python. I decided librsvg was the way to go. This is my SVG, saved from Inkscape as "normal SVG" (without Inkscape-specific extensions).

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   width="23.105469mm"
   height="23.10545mm"
   viewBox="0 0 23.105469 23.10545"
   version="1.1"
   id="svg1380"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:svg="http://www.w3.org/2000/svg">
  <defs
     id="defs1377">
    <radialGradient
       xlink:href="#SphereBlueGlow"
       id="radialGradient17266-1-5-3"
       cx="206.91444"
       cy="205.5472"
       fx="206.91444"
       fy="205.5472"
       r="11.552734"
       gradientTransform="translate(-188.33616,-3.103272)"
       gradientUnits="userSpaceOnUse" />
    <linearGradient
       id="SphereBlueGlow">
      <stop
         style="stop-color:#ffffff;stop-opacity:1"
         offset="0"
         id="stop954-7" />
      <stop
         style="stop-color:#44ccff;stop-opacity:0"
         offset="0.69538838"
         id="stop956-4" />
      <stop
         style="stop-color:#000000;stop-opacity:0"
         offset="1"
         id="stop958-1" />
    </linearGradient>
    <radialGradient
       xlink:href="#SphereSpecularReflection"
       id="radialGradient868-5-1-3-3-5-3-5-8-5-2-9-0-9-3-9-2-0-2"
       gradientUnits="userSpaceOnUse"
       gradientTransform="matrix(0.11379011,0.15082671,-0.14646196,0.11049697,33.91806,171.06396)"
       cx="60.713989"
       cy="169.90594"
       fx="60.713989"
       fy="169.90594"
       r="37.436264" />
    <linearGradient
       id="SphereSpecularReflection">
      <stop
         style="stop-color:#e6e6e6;stop-opacity:1"
         offset="0"
         id="stop944" />
      <stop
         style="stop-color:#8e8e8e;stop-opacity:0"
         offset="0.37699515"
         id="stop946" />
      <stop
         style="stop-color:#000000;stop-opacity:0.02313977"
         offset="1"
         id="stop948" />
    </linearGradient>
  </defs>
  <g
     id="layer1"
     transform="translate(3.4848234,-128.62724)">
    <g
       id="zauberplatzPassiv"
       transform="translate(-10.51038,-62.263795)">
      <ellipse
         style="mix-blend-mode:normal;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.155336;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:0.529412"
         id="path833-3-7-9-8-0-7-1-4-0-2-1-9-9-7-6-44-5-9"
         cx="18.578291"
         cy="202.44376"
         rx="11.552734"
         ry="11.552725" />
      <ellipse
         style="mix-blend-mode:normal;fill:url(#radialGradient17266-1-5-3);fill-opacity:1;stroke:none;stroke-width:0.155336;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:0.529412"
         id="path833-3-7-9-8-0-7-1-4-0-2-1-9-9-7-6-4-6-5-2"
         cx="18.578291"
         cy="202.44376"
         rx="11.552734"
         ry="11.552725" />
      <ellipse
         style="mix-blend-mode:hard-light;fill:url(#radialGradient868-5-1-3-3-5-3-5-8-5-2-9-0-9-3-9-2-0-2);fill-opacity:1;stroke:none;stroke-width:0.155336;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:0.529412"
         id="path833-1-7-1-6-8-3-7-2-0-6-1-8-0-8-4-0-9-0"
         cx="18.578291"
         cy="202.44376"
         rx="11.552734"
         ry="11.552725" />
    </g>
  </g>
</svg>

My Python code for rendering (hopefully it survives the reduction for this question):

import gi
gi.require_version('Rsvg', '2.0')
from gi.repository import Rsvg
import cairo

def render_image(svg):
    ratio = svg.props.em / svg.props.dpi_x
    svg.set_dpi(160 / ratio)

    dim = svg.get_dimensions()
    # create the cairo context
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, dim.width, dim.height)
    context = cairo.Context(surface)
    svg.render_cairo(context)
    surface.write_to_png('sphere_used.image.png')

def render_elements (svg):
    rect = Rsvg.Rectangle()
    rect.x = 0
    rect.y = 0
    rect.width = 160
    rect.height = 160
    # create the cairo context
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(rect.width), int(rect.height))
    context = cairo.Context(surface)
    svg.render_element(context, '#zauberplatzPassiv', rect)
    surface.write_to_png('sphere_used.element.png')

if __name__ == '__main__':
    # use rsvg to render the cairo context
    svg = Rsvg.Handle().new_from_file(INPUTFILE)
    render_image(svg)
    render_elements(svg)

The output of render_image(svg) seems identical to what I did in Inkscape: sphere as rendered as a complete image by librsvg (Rsvg.Handle().render_cairo(context))

In contrast, the output of render_element(svg) misses the specular reflection: spehere as rendered as a single element by librsvg (Rsvg.Handle().render_element(context, element_id, viewport))

Obviously, librsvg supports all the SVG features used and is capable of correctly displaying the image. But when selecting a single element, there's something going wrong. Is there anything I could do on the SVG or Python side to have them both look identical?

I could imagine that the order of applying the different SVG translations is different in either case, resulting in the reflection being rendered off-screen. But I do not know SVG good enough.


Solution

  • The culprit is mix-blend-mode:hard-light;.

    I cleaned up the SVG, reset all the translations, but the highlight kept missing. Only after setting the mix-blend-mode from hard-light to normal it reappeared.

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!-- Created with Inkscape (http://www.inkscape.org/) -->
    
    <svg
       width="160px"
       height="160px"
       viewBox="0 0 160 160"
       version="1.1"
       id="svg1380"
       xmlns:xlink="http://www.w3.org/1999/xlink"
       xmlns="http://www.w3.org/2000/svg"
       xmlns:svg="http://www.w3.org/2000/svg">
      <defs
         id="defs1377">
        <radialGradient
           xlink:href="#SphereBlueGlow"
           id="circular_glow"
           cx="80"
           cy="80"
           fx="80"
           fy="80"
           r="80"
           gradientUnits="userSpaceOnUse" />
        <linearGradient
           id="SphereBlueGlow">
          <stop
             style="stop-color:#ffffff;stop-opacity:1"
             offset="0"
             id="stop954-7" />
          <stop
             style="stop-color:#44ccff;stop-opacity:0"
             offset="0.69538838"
             id="stop956-4" />
          <stop
             style="stop-color:#000000;stop-opacity:0"
             offset="1"
             id="stop958-1" />
        </linearGradient>
        <radialGradient
           xlink:href="#SphereSpecularReflection"
           id="circular_specular_reflection"
           gradientUnits="userSpaceOnUse"
           gradientTransform="matrix(0.65,0,0,0.65,10,5)"
           cx="80"
           cy="80"
           fx="80"
           fy="80"
           r="80" />
        <linearGradient
           id="SphereSpecularReflection">
          <stop
             style="stop-color:#e6e6e6;stop-opacity:1"
             offset="0"
             id="stop000" />
         <stop
             style="stop-color:#e6e6e6;stop-opacity:1"
             offset="0.05"
             id="stop040" />
          <stop
             style="stop-color:#e6e6e6;stop-opacity:0.5"
             offset="0.14"
             id="stop060" />
          <stop
             style="stop-color:#e6e6e6;stop-opacity:0.1"
             offset="0.2"
             id="stop080" />
          <stop
             style="stop-color:#e6e6e6;stop-opacity:0"
             offset="0.3"
             id="stop020" />
          <stop
             style="stop-color:#e6e6e6;stop-opacity:0"
             offset="1"
             id="stop100" />
        </linearGradient>
      </defs>
      <g
         id="layer1">
        <g
           id="zauberplatzPassiv">
          <ellipse
             style="mix-blend-mode:normal;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.155336;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:0.529412"
             id="black-ellipse"
             cx="80"
             cy="80"
             rx="80"
             ry="80" />
          <ellipse
             style="mix-blend-mode:normal;fill:url(#circular_glow);fill-opacity:1;stroke:none;stroke-width:0.155336;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:0.529412"
             id="glow-ellipse"
             cx="80"
             cy="80"
             rx="80"
             ry="80" />
          <ellipse
             style="mix-blend-mode:normal;fill:url(#circular_specular_reflection);fill-opacity:1;stroke:none;stroke-width:0.155336;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:0.529412"
             id="highlight-ellipse"
             cx="80"
             cy="80"
             rx="80"
             ry="80" />
        </g>
      </g>
    </svg>
    

    I had to edit the gradient to make it fit better into this blend mode, but the result looks good enough for me: enter image description here

    I am still curious what's so special about hard-light, so if any librsvg experts come along they can maybe shed some light on it.