[cairo] connecting lines

Carl Worth cworth at cworth.org
Wed Nov 7 11:18:31 PST 2007


On Wed, 7 Nov 2007 21:02:24 +0200, Donn wrote:
> I reproduced the error, here is some code.

Thanks for the code.

And it makes a lot more sense since it shows you drawing text and
_then_ cairo_arc, (your previous message said arc then text which
wouldn't explain the bug).

> I'm sure I'm being thick, which is par for the course :)

No, you're not being thick.

There's a bit of a trap in cairo_arc. If the path is non-empty, then
cairo_arc first adds a line to the beginning of the arc, and then
draws the arc. This is definitely what's desired when you want an arc
as an integrated part of a path, (think of a rounded rectangle for
example), where the stroke should be continuous, nicely joined, etc.

But when you actually want an entirely separate circle, (as in your
case), that leading line is really obnoxious and not desired at all.

In ancient versions of cairo, it was hard to get rid of that initial
line, (you would have to add a cairo_move_to just before cairo_arc and
potentially perform some trigonometry to calculate the correct point).

Fortunately, since cairo 1.2 we've added a new function called
cairo_new_sub_path that effectively "picks up the pen" just like
cairo_move_to does, but doesn't also place it down anywhere like
move_to does.

So you can fix your program with an additional line:


>         ctx.move_to(-120 - x_bearing, -120 - y_bearing)
>         ctx.show_text("15")
>
>         print "##Draw Circle"
>

	  ctx.new_sub_path()

>         ctx.arc(0,0, 25, 0, 2.0 * math.pi)
>         ctx.set_source_rgb(1, 0, 1)
>         ctx.fill_preserve()

I checked the documentation for cairo_arc and it does mention the
initial line:

	If there is a current point, an initial line segment will be
	added to the path to connect the current point to the
	beginning of the arc.

But it didn't mention cairo_new_sub_path. I've now added the following
sentence there which I hope will help avoid similar confusion for
others in the future:

	If this initial line is undesired, it can be avoided by
	calling cairo_new_sub_path() before calling cairo_arc().

Keep having fun with cairo,

-Carl
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.cairographics.org/archives/cairo/attachments/20071107/42f4b49c/attachment.pgp 


More information about the cairo mailing list