It is sometimes useful to know the length of a path. Using Javascript and the SVG DOM method, it is possible to find out the length of a path. An alternative approach is to calculate the length of the path. To do this the length of each subpath must be calculated. The maximum length of a cubic is the sum of the distances between the points. So, for example:

<path d="Mcx,cyCx1,y1,x2,y2,x3,y3"/>

has a maximum length of the distance between (cx,cy) and (x1,y1) plus the distance between (x1,y1) and (x2,y2) plus the distance between (x2,y2) and (x3,y3). The minimum length is the distance between (cx,cy) and (x3,y3). The difference between the two values is the maximum error in the estimate. By recursively breaking each curve into two, the difference between the maximum and minimum values decreases until an acceptable level o9f accuracy can be obtained.

The transformation takes place in two stages. The transformation curve_length calculates the length of each line or cubic adding this as an attribute l to each command together with an attribute er which indicates the estimated maximum error in the length value. The transformation path_length sums the individual lengths and errors, adding the length and error to the path as a whole and the individual subpaths. These two transformations are normally used together to calculate the length of a path. For example:

java -jar ../../library/saxon6.jar  -o t1.xml test.svg   library/break_path.xsl
java -jar ../../library/saxon6.jar  -o t2.xml t1.xml     library/make_abs.xsl
java -jar ../../library/saxon9.jar  -t t2.xml            library/add_subpath.xsl >  t3.xml
java -jar ../../library/saxon6.jar  -o t4.xml t3.xml     library/make_xml.xsl
java -jar ../../library/saxon6.jar  -o t5.xml t4.xml     library/curve_length.xsl
java -jar ../../library/saxon6.jar  -o path_length.xml   t5.xml library/path_length.xsl

If test.svg consisted of the path:

<path  d="M100,250L150,350C200,0 300,800 400,200"/>

The curve_length transformation would generate:

<path>
  <s>
    <c t="N" o="M" cx="0" cy="0" x3="100" y3="250"/>
    <c l="111.803" er="0" t="L" o="L" cx="100" cy="250" x3="150" y3="350" />
    <c l="581.615" er="0.156" t="C" o="C" cx="150" cy="350" x1="200" y1="0" x2="300" y2="800" x3="400" y3="200" />
    <c t="E" o="" cx="400" cy="200" x1="0" y1="0"/>
  </s>
</path>

and the output from path_length.xsl would be:

<path l="693.418" er="0.156">
  <s l="693.418" er="0.156">
    <c t="N" o="M" cx="0" cy="0" x3="100" y3="250"/>
    <c t="L" o="L" cx="100" cy="250" x3="150" y3="350"/>
    <c t="C" o="C" cx="150" cy="350" x1="200" y1="0" x2="300" y2="800" x3="400" y3="200"/>
    <c t="E" o="" cx="400" cy="200" x1="0" y1="0"/>
  </s>
</path>

It is up to the user to decide what to do with the information generated.

The transformation variables.xsl contains a set of parameters including:

<xsl:variable name="eps" select="0.01" />

This defines the accuracy required for each cubic. The user can modify the value of eps.