Bug 12033 - SVG needs ability to calculate path lengths
Summary: SVG needs ability to calculate path lengths
Alias: None
Product: WebKit
Classification: Unclassified
Component: SVG (show other bugs)
Version: 420+
Hardware: Mac OS X 10.4
: P2 Normal
Assignee: Eric Seidel (no email)
: 11975 (view as bug list)
Depends on:
Blocks: 11975
  Show dependency treegraph
Reported: 2006-12-29 20:58 PST by Eric Seidel (no email)
Modified: 2006-12-31 14:08 PST (History)
1 user (show)

See Also:

first (incomplete) stab (13.73 KB, patch)
2006-12-29 23:27 PST, Eric Seidel (no email)
no flags Details | Formatted Diff | Diff
Working patch (18.22 KB, patch)
2006-12-31 10:45 PST, Eric Seidel (no email)
sam: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Seidel (no email) 2006-12-29 20:58:39 PST
SVG needs ability to calculate path lengths

This is arguably a platform bug as much as an SVG bug.

SVG requires the ability to calculate path lengths.  Both total path length, as well as the ability to identify a segment at a given length along a path.

Calculating the length of a bezier curve is non-trivial, and most algorithms use successive approximation of the curve length.

We have several options here:
1.  I expect CoreGraphics (or some other library on OS X) has this functionality (possibly not via public methods) that with a little help from the Apple folks, we could find and expose via WebKitSystemInterface.
2.  mozilla has their own code for this: http://lxr.mozilla.org/seamonkey/source/content/svg/content/src/nsSVGPathSeg.cpp#90 which we potentially could borrow, since I believe mozilla's triple license is compatible with WebKit's BSD/LGPL licensing.
4.  There may be some other library or code snippet out there that we could use
5.  We could write our own.  I'm sure one of us has a CS book which documents a decent algorithm or two.  There are also various guides on the web: (e.g.

In any case, this will require a non-trivial amount of effort, but the results of which enable quite a few SVG features (including text-on-path).
Comment 1 Eric Seidel (no email) 2006-12-29 23:27:54 PST
Created attachment 12116 [details]
first (incomplete) stab

What can I say?  I felt inspired.

It's kinda amusing how the 100+ lines of mozilla code which I used to start this, *completely* morphed and turned into about 20 lines of WebKit code.

This is still not done.  This only supports calculating path lengths from Path objects, not yet from SVGPathSegList objects, but that second part won't be hard to add.

When adding the SVGPathSegList code, we'll also add the ability to walk a path and find the segment at a specific length.

The only tricky part about adding the SVGPathSegList support is handling SVG Arc segments.  There is additional mozilla code we can use to convert from Arcs to curves however.
Comment 2 Eric Seidel (no email) 2006-12-31 10:45:03 PST
Created attachment 12136 [details]
Working patch

I decided to chuck the little remaining mozilla code and re-wrote it.  We use a similar text length algorithm, but (IMO) a much nicer implementation thereof.
Comment 3 Eric Seidel (no email) 2006-12-31 12:00:57 PST
*** Bug 11975 has been marked as a duplicate of this bug. ***
Comment 4 Sam Weinig 2006-12-31 14:08:20 PST
Landed in r18493.