W3C home > Mailing lists > Public > www-svg@w3.org > October 2003

Re: Unit conversion of percentage values in the SVG DOM

From: Dean Jackson <dean@w3.org>
Date: Tue, 7 Oct 2003 01:18:52 +1000
To: Steve Schafer <steve@fenestra.com>
Cc: www-svg@w3.org
Message-ID: <20031006151852.GG6869@grorg.org>

nearly ancient history, but better late than

On Fri 19 Sep 2003, Steve Schafer wrote:

> 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);

Aside: you probably want to modify the baseValue rather than
the animVal

> 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.

The enclosing viewport of the <rect> is defined by
the <symbol>.

> 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.

Why? I can't see a change in the rect from that call.
I thought you said the 25% was displayed as 2cm.

> 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?

Maybe I'm confused as well :)

Received on Monday, 6 October 2003 11:18:55 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 22:53:59 UTC