Thalia
Thalia

Reputation: 14615

Svg with parameters that loads in Qt

Basic needs:

I am working on a Qt application that replaces colors in an svg.
A nice implementation is to parse the svg xml, and replace the colors as found with a good color match.
Unfortunately, the application must run on a platform with very limited speed and memory, and loading the svg into the QSvgRenderer from an xml (or text string) is extremely slow.
So - my current implementation is to string replace the occurrences of hex known colors in the QByteArray loaded by the renderer.
A big limitation - if I want to replace a fill color with a pen color, and they match, I end up with a broken blob.
A second limitation: I can only have 2 defined colors, a fill and a pen.

What I would like:

I want to be able to create a "parameterized" svg, where I can replace "color1", "color2", "color3" defined at the top, with whatever colors the user chooses.

Note - the svg has to be loaded by the QSvgRenderer, so the parameter values can't be in an outside html or js.
The svg must be self-contained... with no outside caller requirement.
But I can replace in code the parameter value before load.
I just want to be able to replace parameters in a single location, instead of actual values inside the xml everywhere they occur.

What I have tried:

I have read in the svg documentation that it is possible to create parameterized values. This is from an example, as much as I understand it...
w3.org example

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 110 40" width="100%" height="100%">
  <title>Reusable Button</title>
  <desc>Takes parameters from parent document's embedding element.</desc>

  <defs>
    <ref id="paramFill" param="color" default="blue"/>
    <ref id="paramText" param="text-label">button</ref>
    <ref id="paramStroke" param="outline" default="navy"/>
  </defs>

  <g>
    <rect id="button_rect" x="5" y="5" width="100" height="30" rx="15" ry="15" fill="url(#paramFill)" stroke="url(#paramStroke)" />
    <text id="button_label" x="55" y="30" text-anchor="middle" font-size="25" fill="black" font-family="Verdana">
      <tref xlink:href="#paramText" />
    </text>
  </g>

</svg>

Unfortunately Qt doesn't load it and browsers show as error.

Second example: from S.O: Define color in SVG

<?xml version="1.0"?>
<svg width="704" height="702" xmlns="http://www.w3.org/2000/svg">

<style>
.myfill { fill:red }
</style>

    <g fill="blue">
    <rect x="0" y="0" width="704" height="702" class="myfill" />
    </g>

</svg>

This loads in browser correctly as red, but Qt loads it with blue - so clearly it does not support the parameter value.

Is there any possible version of svg that uses parameters, that can be supported by Qt ?
Can somebody please help fix either of my examples or give a correct / better example ?
Thank you.

Qt version: 4.8

Upvotes: 1

Views: 615

Answers (1)

ccprog
ccprog

Reputation: 21811

A long, long time ago, when some SVG renderers did not support style sheets, I solved this with XML entities:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
    "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY red "#ff0000">
]>
<svg width="704" height="702" xmlns="http://www.w3.org/2000/svg">
    <g fill="blue">
         <rect x="0" y="0" width="704" height="702" fill="&red;" />
    </g>
</svg>

Upvotes: 3

Related Questions