Unit conversion of percentage values in the SVG DOM

Consider the following simple example:

 <svg width="8cm" height="4cm" version="1.1"
     xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink">
   <defs>
     <symbol id="mySymbol">
       <rect x="25%" y="25%" width="50%" height="50%"/>
     </symbol>
   </defs>
   <use x="2cm" y="1cm" width="4cm" height="2cm"
       xlink:href="#mySymbol"/>
 </svg>

This renders just fine, of course; it results in a 2cm x 1cm black
rectangle centered in the 8cm x 4cm viewport.

Now imagine that our user agent supports manipulation of the SVG object
tree via the DOM. We can get at the <rect> element with code that looks
something like this:

 defsElement = (SVGDefsElement)doc.documentElement.firsChild;
 symbolElement = (SVGSymbolElement)defsElement.firstChild;
 rectElement = (SVGRectElement)symbolElement.firstChild;

So far so good. But what if we do this:

 rectElement.x.animVal.convertToSpecifiedUnits(SVG_LENGTHTYPE_CM);

That is, we're asking for the originally specified length percentage
("25%") to be converted to an absolute length in centimetres. The
problem is that inside a <symbol> element inside a <defs> element, the
enclosing viewport of the <rect> is not well-defined.

So what do we do? The easy way out is to go ahead and use the viewport
established by the <svg> element enclosing the <rect> (which means that
"25%" will be converted to "2cm"). This has the undesirable effect of
changing the appearance of the rendered document--a result that would be
unexpected from a simple change in units.

Another possibility is to raise an exception, but the description for
SVGLength.convertToSpecifiedUnits states "No Exceptions."

These are the only two things I can think of. Any other ideas?

Steve Schafer
Fenestra Technologies Corp
http://www.fenestra.com/

Received on Friday, 19 September 2003 11:41:30 UTC